File: StringUtils.cpp

package info (click to toggle)
ausaxs 1.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 72,592 kB
  • sloc: cpp: 49,853; ansic: 6,901; python: 730; makefile: 18
file content (172 lines) | stat: -rw-r--r-- 4,945 bytes parent folder | download
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// SPDX-License-Identifier: LGPL-3.0-or-later
// Author: Kristian Lytje

#include <utility/StringUtils.h>
#include <utility/Exceptions.h>
#include <math/ConstexprMath.h>

#include <algorithm>
#include <cmath>
#include <array>

using namespace ausaxs;

std::string utility::remove_quotation_marks(std::string s) {
    if (s.size() > 1 && s[0] == '"' && s[s.size()-1] == '"') {
        return s.substr(1, s.size()-2);
    }
    return s;
}

std::string utility::remove_spaces(std::string s) {
    std::string::iterator end_pos = std::remove(s.begin(), s.end(), ' ');
    s.erase(end_pos, s.end());
    return s;
}

std::string utility::round_double(double d, int decimals) {
    std::string s = std::to_string(std::round(d*std::pow(10, decimals))/std::pow(10, decimals));
    auto dot = s.find('.');
    if (dot == std::string::npos) {
        return s;
    }
    return s.substr(0, dot + 1 + decimals);
}

std::vector<std::string> utility::split(std::string_view str, char delimiter) {
    std::vector<std::string> tokens;
    std::size_t i = 0;
    while (i < str.size()) {
        while (i < str.size() && str[i] == delimiter) {
            ++i;
        }
        std::size_t start = i;
        while (i < str.size() && str[i] != delimiter) {
            ++i;
        }
        if (start < i) {
            tokens.push_back(std::string(str.substr(start, i-start)));
        }
    }
    return tokens;
}

std::vector<std::string> utility::split(std::string_view s, std::string_view delimiters) {
    std::vector<std::string> tokens;

    static_assert(constexpr_math::pow(2, 8*sizeof(char)) == 256, "Unexpected char size");
    std::array<bool, 256> table{};
    for (auto c : delimiters) {table[c] = true;}

    // skip leading delimiters
    unsigned int start = 0;
    while (start < s.size() && table[s[start]]) {
        ++start;
    }

    // iterate through the rest of the string
    for (unsigned int i = start; i < s.size(); ++i) {
        if (!table[s[i]]) {continue;}

        // add token to vector
        tokens.push_back(std::string(s.substr(start, i-start)));

        ++i; // start from next char
        while (i < s.size() && table[s[i]]) {
            ++i;
        }
        start = i;
    }

    // add last token to vector
    if (start < s.size()) {
        tokens.push_back(std::string(s.substr(start)));
    }
    return tokens;
}

std::string utility::join(std::vector<std::string> v, std::string_view separator) {
    std::string s;
    for (unsigned int i = 0; i < v.size(); i++) {
        s += v[i];
        if (i != v.size()-1) {
            s += separator;
        }
    }
    return s;
}

std::string utility::remove_all(std::string_view s, std::string_view remove) {
    static_assert(constexpr_math::pow(2, 8*sizeof(char)) == 256, "Unexpected char size");
    std::array<bool, 256> table{};
    for (auto c : remove) {table[c] = true;}

    std::string new_s;
    new_s.reserve(s.size());
    for (auto c : s) {
        if (!table[c]) {
            new_s.push_back(static_cast<char>(c));
        }
    }
    return new_s;
}

std::string_view utility::remove_leading(std::string_view s, std::string_view remove) {
    static_assert(constexpr_math::pow(2, 8*sizeof(char)) == 256, "Unexpected char size");
    std::array<bool, 256> table{};
    for (auto c : remove) {table[c] = true;}

    unsigned int start = 0;
    while (start < s.size() && table[s[start]]) {
        ++start;
    }
    return s.substr(start);
}

std::string_view utility::remove_trailing(std::string_view s, std::string_view remove) {
    static_assert(constexpr_math::pow(2, 8*sizeof(char)) == 256, "Unexpected char size");
    std::array<bool, 256> table{};
    for (auto c : remove) {table[c] = true;}

    unsigned int end = s.size();
    while (end > 0 && table[s[end-1]]) {
        --end;
    }
    return s.substr(0, end);
}

std::string_view utility::remove_leading_and_trailing(std::string_view s, std::string_view remove) {
    static_assert(constexpr_math::pow(2, 8*sizeof(char)) == 256, "Unexpected char size");
    std::array<bool, 256> table{};
    for (auto c : remove) {table[c] = true;}

    unsigned int start = 0;
    while (start < s.size() && table[s[start]]) {
        ++start;
    }
    if (start == s.size()) {return s;}

    unsigned int end = s.size();
    while (end > start && table[s[end-1]]) {
        --end;
    }
    return s.substr(start, end-start);
}

std::string utility::to_lowercase(std::string_view s) {
    std::string new_s;
    for (auto c : s) {
        new_s += std::tolower(c);
    }
    return new_s;
}

bool utility::parse_bool(std::string_view s) {
    auto lower = to_lowercase(s);
    if (lower == "true" || lower == "yes" || lower == "1") {
        return true;
    } else if (lower == "false" || lower == "no" || lower == "0") {
        return false;
    }
    throw except::invalid_argument("utility::parse_bool: \"" + std::string(s) + "\" cannot be interpreted as a boolean value.");
}