Line data Source code
1 : #include "str_utils.h"
2 : #include <iostream>
3 : #include <string>
4 : #include <algorithm>
5 : #include <vector>
6 :
7 :
8 : namespace Utils
9 : {
10 :
11 : static bool case_insensitive_compare(const std::string& s1, const std::string& s2);
12 :
13 : //! std::isspace(char) can only handle chars in the range [0,255]
14 : //! so we need to cast the it's argument to unsigned char
15 : // trim from start (in place)
16 44102966 : std::string& str_ltrim(std::string& s)
17 : {
18 44102966 : auto it = std::find_if(s.begin(), s.end(),
19 44381509 : [](unsigned char c) {
20 44381509 : return !std::isspace(c);
21 : });
22 44102966 : s.erase(s.begin(), it);
23 44102966 : return s;
24 : }
25 :
26 : // trim from end (in place)
27 44102966 : std::string& str_rtrim(std::string& s)
28 : {
29 44102966 : auto it = std::find_if(s.rbegin(), s.rend(),
30 46227824 : [](unsigned char c) {
31 46227824 : return !std::isspace(c);
32 44102966 : });
33 44102966 : s.erase(it.base(), s.end());
34 44102966 : return s;
35 : }
36 :
37 : // trim from both ends (in place)
38 44102966 : std::string& str_trim(std::string& s)
39 : {
40 44102966 : return str_ltrim(str_rtrim(s));
41 : }
42 :
43 10 : std::string& str_ltrim(std::string& s, /*unsigned*/ char toRemove)
44 : {
45 10 : auto it = std::find_if(s.begin(), s.end(),
46 20 : [toRemove](/*unsigned*/ char c) {
47 20 : return c != toRemove;
48 : });
49 10 : s.erase(s.begin(), it);
50 10 : return s;
51 : }
52 :
53 : // trim from end (in place)
54 0 : std::string& str_rtrim(std::string& s, /*unsigned*/ char toRemove)
55 : {
56 0 : auto it = std::find_if(s.rbegin(), s.rend(),
57 0 : [toRemove](/*unsigned*/ char c) {
58 0 : return c != toRemove;
59 0 : });
60 0 : s.erase(it.base(), s.end());
61 0 : return s;
62 : }
63 :
64 : // trim from both ends (in place)
65 0 : std::string& str_trim(std::string& s, /*unsigned*/ char toRemove)
66 : {
67 0 : return str_ltrim(str_rtrim(s, toRemove), toRemove);
68 : }
69 :
70 : // trim from start (copying)
71 0 : std::string str_ltrim_copy(const std::string& s)
72 : {
73 0 : auto copy(s);
74 0 : return str_ltrim(copy);
75 0 : }
76 :
77 : // trim from end (copying)
78 0 : std::string str_rtrim_copy(const std::string& s)
79 : {
80 0 : auto copy(s);
81 0 : return str_rtrim(copy);
82 0 : }
83 :
84 : // trim from both ends (copying)
85 0 : std::string str_trim_copy(const std::string& s)
86 : {
87 0 : auto copy(s);
88 0 : return str_trim(copy);
89 0 : }
90 :
91 0 : std::string str_ltrim_copy(const std::string& s, /*unsigned*/ char toRemove)
92 : {
93 0 : auto copy(s);
94 0 : return str_ltrim(copy, toRemove);
95 0 : }
96 :
97 : // trim from end (copying)
98 0 : std::string str_rtrim_copy(const std::string& s, /*unsigned*/ char toRemove)
99 : {
100 0 : auto copy(s);
101 0 : return str_rtrim(copy, toRemove);
102 0 : }
103 :
104 : // trim from both ends (copying)
105 0 : std::string str_trim_copy(const std::string& s, /*unsigned*/ char toRemove)
106 : {
107 0 : auto copy(s);
108 0 : return str_trim(copy, toRemove);
109 0 : }
110 :
111 0 : void str_to_lower(std::string& s)
112 : {
113 0 : std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); });
114 0 : }
115 :
116 0 : void str_to_upper(std::string& s)
117 : {
118 0 : std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::toupper(c); });
119 0 : }
120 :
121 749 : std::string str_to_lower_copy(const std::string& s)
122 : {
123 749 : std::string copy = s;
124 3745 : std::transform(copy.begin(), copy.end(), copy.begin(), [](unsigned char c) { return std::tolower(c); });
125 749 : return copy;
126 : }
127 :
128 0 : std::string str_to_upper_copy(const std::string& s)
129 : {
130 0 : std::string copy;
131 0 : std::transform(copy.begin(), copy.end(), copy.begin(), [](unsigned char c) { return std::toupper(c); });
132 0 : return copy;
133 : }
134 :
135 0 : bool str_iequals(const std::string& s1, const std::string& s2)
136 : {
137 0 : return case_insensitive_compare(s1, s2);
138 : }
139 :
140 0 : std::vector<std::string>::iterator find_str_icmp(std::vector<std::string>::iterator first, std::vector<std::string>::iterator last, const std::string& val)
141 : {
142 0 : return std::find_if(first, last, [&val](const std::string& s) { return case_insensitive_compare(s, val); });
143 : }
144 :
145 0 : std::vector<std::string>::const_iterator find_str_icmp(const std::vector<std::string>::const_iterator first, const std::vector<std::string>::const_iterator last, const std::string& val)
146 : {
147 0 : return std::find_if(first, last, [&val](const std::string& s) { return case_insensitive_compare(s, val); });
148 : }
149 :
150 0 : static bool case_insensitive_compare(const std::string& s1, const std::string& s2)
151 : {
152 0 : return std::equal(
153 : s1.begin(), s1.end(),
154 : s2.begin(), s2.end(),
155 0 : [](char c1, char c2)
156 : {
157 0 : return std::tolower(c1) == std::tolower(c2);
158 0 : });
159 : }
160 :
161 : // UTILS_EXPORT std::string Utils::str_replace(const std::string& s, const std::string& from, const std::string& to)
162 : // {
163 : // std::string result;
164 : //result.reserve(s.length()); // avoids a few memory allocations
165 :
166 : //std::string::size_type lastPos = 0;
167 : //std::string::size_type findPos;
168 :
169 : // while (std::string::npos != (findPos = s.find(from, lastPos)))
170 : // {
171 : // result.append(s, lastPos, findPos - lastPos);
172 : // result += to;
173 : // lastPos = findPos + from.length();
174 : //}
175 :
176 : //// Care for the rest after last occurrence
177 : //result += s.substr(lastPos);
178 :
179 : //return result;
180 : // }
181 : }
|