aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/uri/other.cpp
blob: 7e02d70e25352962919454aa582f0f041727ccee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "other.h"
 
#include <util/string/util.h>
#include <util/system/yassert.h>
 
/********************************************************/
/********************************************************/

static const Tr InvertTr(".:/?#", "\005\004\003\002\001");
static const Tr RevertTr("\005\004\003\002\001", ".:/?#");

void TrspChars(char* s) { 
    InvertTr.Do(s);
}

void UnTrspChars(char* s) { 
    RevertTr.Do(s);
}

void TrspChars(char* s, size_t l) { 
    InvertTr.Do(s, l);
}

void UnTrspChars(char* s, size_t l) { 
    RevertTr.Do(s, l);
}

void TrspChars(const char* s, char* d) { 
    InvertTr.Do(s, d);
}

void UnTrspChars(const char* s, char* d) { 
    RevertTr.Do(s, d);
}

void InvertDomain(char* begin, char* end) {
    // skip schema if it is present
    const auto dotPos = TStringBuf{begin, end}.find('.');
    if (dotPos == TStringBuf::npos)
        return; // no need to invert anything
    const auto schemaendPos = TStringBuf{begin, end}.find("://", 3);
    if (schemaendPos < dotPos)
        begin += schemaendPos + 3;
    char* sl = (char*)memchr(begin, '/', end - begin);
    char* cl = (char*)memchr(begin, ':', sl ? sl - begin : end - begin);
    end = cl ? cl : (sl ? sl : end);

    // invert string
    for (size_t i = 0, n = end - begin; i < n / 2; ++i)
        DoSwap(begin[i], begin[n - i - 1]);

    // invert back each host name segment
    char* b = begin;
    while (true) {
        char* e = (char*)memchr(b, '.', end - b);
        if (!e)
            e = end;
        for (size_t i = 0, n = e - b; i < n / 2; ++i)
            DoSwap(b[i], b[n - i - 1]);
        if (e == end) 
            break;
        b = e + 1;
    }
}

void InvertUrl(char* begin, char* end) {
    char* slash = strchr(begin, '/'); 
    if (slash) {
        *slash = 0;
    }
    strlwr(begin);
    if (slash) {
        *slash = '/';
    }
    InvertDomain(begin, end);
    TrspChars(begin);
}

void RevertUrl(char* begin, char* end) {
    UnTrspChars(begin);
    InvertDomain(begin, end);
}