aboutsummaryrefslogtreecommitdiff
path: root/src/StringUtils.cpp
blob: 5505a32c8ca57c1388424a61b54bac69f8594f1a (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "../include/StringUtils.hpp"
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

namespace QuickMedia {
    template <typename T>
    static void string_split_t(const std::string_view str, const T &delimiter, StringSplitCallback callback_func, bool include_empty) {
        size_t index = 0;
        while(index < str.size()) {
            size_t new_index = str.find(delimiter, index);
            if(new_index == std::string::npos)
                new_index = str.size();

            if(include_empty || new_index - index > 0) {
                if(!callback_func(str.data() + index, new_index - index))
                    break;
            }

            if constexpr(std::is_same<char, T>::value)
                index = new_index + 1;
            else
                index = new_index + delimiter.size();
        }
    }

    void string_split(const std::string &str, const std::string &delimiter, StringSplitCallback callback_func, bool include_empty) {
        string_split_t(std::string_view(str), delimiter, callback_func, include_empty);
    }

    void string_split(const std::string &str, char delimiter, StringSplitCallback callback_func, bool include_empty) {
        string_split_t(std::string_view(str), delimiter, callback_func, include_empty);
    }

    void string_split_view(const std::string_view str, const std::string &delimiter, StringSplitCallback callback_func, bool include_empty) {
        string_split_t(str, delimiter, callback_func, include_empty);
    }

    void string_split_view(const std::string_view str, char delimiter, StringSplitCallback callback_func, bool include_empty) {
        string_split_t(str, delimiter, callback_func, include_empty);
    }

    size_t string_replace_all(std::string &str, char old_char, char new_char) {
        size_t num_replaced_substrings = 0;
        for(char &c : str) {
            if(c == old_char) {
                c = new_char;
                ++num_replaced_substrings;
            }
        }
        return num_replaced_substrings;
    }

    size_t string_replace_all(std::string &str, char old_char, const std::string &new_str) {
        size_t num_replaced_substrings = 0;
        size_t index = 0;
        while(index < str.size()) {
            index = str.find(old_char, index);
            if(index == std::string::npos)
                break;
            str.replace(index, 1, new_str);
            index += new_str.size();
            ++num_replaced_substrings;
        }
        return num_replaced_substrings;
    }

    size_t string_replace_all(std::string &str, const std::string &old_str, const std::string &new_str) {
        size_t num_replaced_substrings = 0;
        size_t index = 0;
        while(index < str.size()) {
            index = str.find(old_str, index);
            if(index == std::string::npos)
                break;
            str.replace(index, old_str.size(), new_str);
            index += new_str.size();
            ++num_replaced_substrings;
        }
        return num_replaced_substrings;
    }

    static bool is_whitespace(char c) {
        return c == ' ' || c == '\n' || c == '\t' || c == '\v';
    }

    std::string strip(const std::string &str) {
        if(str.empty())
            return str;

        int start = 0;
        for(; start < (int)str.size(); ++start) {
            if(!is_whitespace(str[start]))
                break;
        }

        int end = str.size() - 1;
        for(; end >= start; --end) {
            if(!is_whitespace(str[end]))
                break;
        }

        return str.substr(start, end - start + 1);
    }

    void strip(const char *str, size_t size, size_t *new_size) {
        if(size == 0) {
            *new_size = 0;
            return;
        }

        int start = 0;
        for(; start < (int)size; ++start) {
            if(!is_whitespace(str[start]))
                break;
        }

        int end = (int)size - 1;
        for(; end >= start; --end) {
            if(!is_whitespace(str[end]))
                break;
        }

        *new_size = end - start + 1;
    }

    bool string_starts_with(const std::string &str, const char *sub) {
        size_t sub_len = strlen(sub);
        return sub_len == 0 || (str.size() >= sub_len && memcmp(str.c_str(), sub, sub_len) == 0);
    }

    bool string_ends_with(const std::string &str, const std::string &ends_with_str) {
        size_t ends_len = ends_with_str.size();
        return ends_len == 0 || (str.size() >= ends_len && memcmp(&str[str.size() - ends_len], ends_with_str.data(), ends_len) == 0);
    }

    size_t str_find_case_insensitive(const std::string &str, size_t start_index, const char *substr, size_t substr_len) {
        if(substr_len == 0)
            return 0;

        auto it = std::search(str.begin() + start_index, str.end(), substr, substr + substr_len,
            [](char c1, char c2) {
                return to_upper(c1) == to_upper(c2);
            });

        if(it == str.end())
            return std::string::npos;

        return it - str.begin();
    }

    // TODO: Support utf-8 case insensitive find
    bool string_find_fuzzy_case_insensitive(const std::string &str, const std::string &substr) {
        if(substr.empty()) return true;
        if(str.empty()) return false;

        size_t str_index = 0;
        bool full_match = true;

        string_split(substr, ' ', [&str, &str_index, &full_match](const char *str_part, size_t size) {
            if(size == 0) return true;

            size_t found_index = str_find_case_insensitive(str, str_index, str_part, size);
            if(found_index == std::string::npos) {
                full_match = false;
                return false;
            }

            str_index = found_index + size;
            return true;
        });

        return full_match;
    }

    char to_upper(char c) {
        if(c >= 'a' && c <= 'z')
            return c - 32;
        else
            return c;
    }

    bool strncase_equals(const char *str1, const char *str2, size_t length) {
        size_t i = 0;
        for(;;) {
            if(i == length)
                return true;
            ++i;

            const char c1 = *str1;
            const char c2 = *str2;
            if(to_upper(c1) != to_upper(c2))
                return false;
            else if(c1 == '\0')
                return true;

            ++str1;
            ++str2;
        }
    }

    bool strcase_equals(const char *str1, const char *str2) {
        for(;;) {
            const char c1 = *str1;
            const char c2 = *str2;

            if(to_upper(c1) != to_upper(c2))
                return false;
            else if(c1 == '\0')
                return true;

            ++str1;
            ++str2;
        }
    }

    bool to_num(const char *str, size_t size, int64_t &num) {
        if(size == 0)
            return false;

        size_t i = 0;
        const bool is_negative = size > 0 && str[0] == '-';
        if(is_negative)
            i = 1;

        num = 0;
        for(; i < size; ++i) {
            const char num_c = str[i] - '0';
            if(num_c < 0 || num_c > 9)
                return false;
            num = (num * 10) + num_c;
        }

        if(is_negative)
            num = -num;

        return true;
    }

    bool to_num(const char *str, size_t size, int &num) {
        int64_t num_i64 = 0;
        const bool res = to_num(str, size, num_i64);
        num = num_i64;
        return res;
    }

    bool to_num_hex(const char *str, size_t size, int &num) {
        if(size == 0)
            return false;

        size_t i = 0;
        const bool is_negative = size > 0 && str[0] == '-';
        if(is_negative)
            i = 1;

        num = 0;
        for(; i < size; ++i) {
            const signed char c = str[i];
            if(c - '0' <= 9)
                num = (num << 4) | (c - '0');
            else if(c - 'a' <= 'f' - 'a')
                num = (num << 4) | (10 + (c - 'a'));
            else if(c - 'A' <= 'F' - 'A')
                num = (num << 4) | (10 + (c - 'A'));
            else
                return false;
        }

        if(is_negative)
            num = -num;

        return true;
    }

    // Returns relative time as a string (approximation)
    std::string seconds_to_relative_time_str(time_t seconds) {
        seconds = std::max(0L, seconds);

        time_t minutes = seconds / 60;
        time_t hours = minutes / 60;
        time_t days = hours / 24;
        time_t months = days / 30;
        time_t years = days / 365;

        if(years >= 1)
            return std::to_string(years) + " year" + (years == 1 ? "" : "s") + " ago";
        else if(months >= 1)
            return std::to_string(months) + " month" + (months == 1 ? "" : "s") + " ago";
        else if(days >= 1)
            return std::to_string(days) + " day" + (days == 1 ? "" : "s") + " ago";
        else if(hours >= 1)
            return std::to_string(hours) + " hour" + (hours == 1 ? "" : "s") + " ago";
        else if(minutes >= 1)
            return std::to_string(minutes) + " minute" + (minutes == 1 ? "" : "s") + " ago";
        else
            return std::to_string(seconds) + " second" + (seconds == 1 ? "" : "s") + " ago";
    }

    std::string seconds_to_duration(int seconds) {
        seconds = std::max(0, seconds);

        int minutes = seconds / 60;
        int hours = minutes / 60;
        char buffer[32];

        if(hours >= 1) {
            minutes -= (hours * 60);
            seconds -= (hours * 60 * 60);
            seconds -= (minutes * 60);
            snprintf(buffer, sizeof(buffer), "%02d:%02d:%02d", hours, minutes, seconds);
        } else if(minutes >= 1) {
            seconds -= (minutes * 60);
            snprintf(buffer, sizeof(buffer), "%02d:%02d", minutes, seconds);
        } else {
            snprintf(buffer, sizeof(buffer), "00:%02d", seconds);
        }

        return buffer;
    }

    std::string number_separate_thousand_commas(const std::string &number) {
        const int num_commas = ((int)number.size() - 1) / 3;
        std::string result;
        result.resize(number.size() + num_commas);
        int result_index = (int)number.size() + num_commas - 1;
        int inc = 0;

        for(int i = (int)number.size() - 1; i >= 0; --i, ++inc) {
            if(inc > 0 && inc % 3 == 0) {
                result[result_index] = ',';
                --result_index;
            }

            result[result_index] = number[i];
            --result_index;
        }

        return result;
    }

    bool generate_random_characters(char *buffer, int buffer_size, const char *alphabet, size_t alphabet_size) {
        if(alphabet_size == 0)
            return false;

        int fd = open("/dev/urandom", O_RDONLY);
        if(fd == -1) {
            perror("/dev/urandom");
            return false;
        }

        if(read(fd, buffer, buffer_size) < buffer_size) {
            fprintf(stderr, "Failed to read %d bytes from /dev/urandom\n", buffer_size);
            close(fd);
            return false;
        }

        for(int i = 0; i < buffer_size; ++i) {
            unsigned char c = *(unsigned char*)&buffer[i];
            buffer[i] = alphabet[c % alphabet_size];
        }

        close(fd);
        return true;
    }
}