aboutsummaryrefslogtreecommitdiffstats
path: root/util/string/util.cpp
blob: 3579dde04d625e90936b934f3f82a8e3ce940c4f (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
#include "util.h"

#include <util/generic/utility.h>

#include <cstdio> 
#include <cstdarg> 
#include <cstdlib>
 
int a2i(const TString& s) {
    return atoi(s.c_str());
}

//============================== span =====================================

void str_spn::init(const char* charset, bool extended) {
    // chars_table_1 is necessary to avoid some unexpected
    // multi-threading issues
    ui8 chars_table_1[256];
    memset(chars_table_1, 0, sizeof(chars_table_1));
    if (extended) {
        for (const char* cs = charset; *cs; cs++) {
            if (cs[1] == '-' && cs[2] != 0) {
                for (int c = (ui8)*cs; c <= (ui8)cs[2]; c++) {
                    chars_table_1[c] = 1;
                }
                cs += 2;
                continue;
            }
            chars_table_1[(ui8)*cs] = 1;
        }
    } else {
        for (; *charset; charset++) {
            chars_table_1[(ui8)*charset] = 1;
        }
    }
    memcpy(chars_table, chars_table_1, 256);
    chars_table_1[0] = 1;
    for (int n = 0; n < 256; n++) {
        c_chars_table[n] = !chars_table_1[n];
    }
}

Tr::Tr(const char* from, const char* to) {
    for (size_t n = 0; n < 256; n++) {
        Map[n] = (char)n;
    }
    for (; *from && *to; from++, to++) {
        Map[(ui8)*from] = *to;
    }
}

size_t Tr::FindFirstChangePosition(const TString& str) const {
    for (auto it = str.begin(); it != str.end(); ++it) {
        if (ConvertChar(*it) != *it) {
            return it - str.begin();
        }
    }

    return TString::npos;
}

void Tr::Do(TString& str) const {
    const size_t changePosition = FindFirstChangePosition(str);

    if (changePosition == TString::npos) {
        return;
    }

    for (auto it = str.begin() + changePosition; it != str.end(); ++it) {
        *it = ConvertChar(*it);
    }
}