aboutsummaryrefslogtreecommitdiff
path: root/src/StringUtils.cpp
blob: 8ed142fb3999c9c03b4c1c3bf45dd11592d6f0db (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
#include "../include/StringUtils.hpp"
#include <string.h>

namespace QuickMedia {
    template <typename T>
    static void string_split_t(const std::string &str, const T &delimiter, StringSplitCallback callback_func) {
        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(!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) {
        string_split_t(str, delimiter, callback_func);
    }

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

    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);
    }

    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) {
        auto it = std::search(str.begin() + start_index, str.end(), substr, substr + substr_len,
            [](char c1, char c2) {
                return std::toupper(c1) == std::toupper(c2);
            });
        if(it == str.end())
            return std::string::npos;
        return it - str.begin();
    }
}