9
9
#define vsprintf_s vsprintf
10
10
#endif
11
11
12
- std::vector<std::string> StringHelper::Split (std::string s, const std::string& delimiter)
13
- {
12
+ std::vector<std::string> StringHelper::Split (std::string s, const std::string& delimiter) {
14
13
size_t pos_start = 0 , pos_end, delim_len = delimiter.length ();
15
14
std::string token;
16
15
std::vector<std::string> res;
@@ -25,14 +24,12 @@ std::vector<std::string> StringHelper::Split(std::string s, const std::string& d
25
24
return res;
26
25
}
27
26
28
- std::vector<std::string_view> StringHelper::Split (std::string_view s, const std::string& delimiter)
29
- {
27
+ std::vector<std::string_view> StringHelper::Split (std::string_view s, const std::string& delimiter) {
30
28
size_t pos_start = 0 , pos_end, delim_len = delimiter.length ();
31
29
std::string_view token;
32
30
std::vector<std::string_view> res;
33
31
34
- while ((pos_end = s.find (delimiter, pos_start)) != std::string_view::npos)
35
- {
32
+ while ((pos_end = s.find (delimiter, pos_start)) != std::string_view::npos) {
36
33
token = s.substr (pos_start, pos_end - pos_start);
37
34
pos_start = pos_end + delim_len;
38
35
res.push_back (token);
@@ -42,150 +39,122 @@ std::vector<std::string_view> StringHelper::Split(std::string_view s, const std:
42
39
return res;
43
40
}
44
41
45
- std::string StringHelper::Strip (std::string s, const std::string& delimiter)
46
- {
47
- size_t pos = 0 ;
48
- std::string token;
42
+ std::string StringHelper::Strip (std::string s, const std::string& delimiter) {
43
+ size_t pos = 0 ;
44
+ std::string token;
49
45
50
- while ((pos = s.find (delimiter)) != std::string::npos)
51
- {
52
- token = s.substr (0 , pos);
53
- s.erase (pos, pos + delimiter.length ());
54
- }
46
+ while ((pos = s.find (delimiter)) != std::string::npos) {
47
+ token = s.substr (0 , pos);
48
+ s.erase (pos, pos + delimiter.length ());
49
+ }
55
50
56
- return s;
51
+ return s;
57
52
}
58
53
59
- std::string StringHelper::Replace (std::string str, const std::string& from,
60
- const std::string& to)
61
- {
62
- size_t start_pos = str.find (from);
54
+ std::string StringHelper::Replace (std::string str, const std::string& from, const std::string& to) {
55
+ size_t start_pos = str.find (from);
63
56
64
- while (start_pos != std::string::npos)
65
- {
66
- str.replace (start_pos, from.length (), to);
67
- start_pos = str.find (from);
68
- }
57
+ while (start_pos != std::string::npos) {
58
+ str.replace (start_pos, from.length (), to);
59
+ start_pos = str.find (from);
60
+ }
69
61
70
- return str;
62
+ return str;
71
63
}
72
64
73
- void StringHelper::ReplaceOriginal (std::string& str, const std::string& from, const std::string& to)
74
- {
75
- size_t start_pos = str.find (from);
65
+ void StringHelper::ReplaceOriginal (std::string& str, const std::string& from, const std::string& to) {
66
+ size_t start_pos = str.find (from);
76
67
77
- while (start_pos != std::string::npos)
78
- {
79
- str.replace (start_pos, from.length (), to);
80
- start_pos = str.find (from);
81
- }
68
+ while (start_pos != std::string::npos) {
69
+ str.replace (start_pos, from.length (), to);
70
+ start_pos = str.find (from);
71
+ }
82
72
}
83
73
84
- bool StringHelper::StartsWith (const std::string& s, const std::string& input)
85
- {
74
+ bool StringHelper::StartsWith (const std::string& s, const std::string& input) {
86
75
#if __cplusplus >= 202002L
87
- return s.starts_with (input.c_str ());
76
+ return s.starts_with (input.c_str ());
88
77
#else
89
- return s.rfind (input, 0 ) == 0 ;
78
+ return s.rfind (input, 0 ) == 0 ;
90
79
#endif
91
80
}
92
81
93
- bool StringHelper::Contains (const std::string& s, const std::string& input)
94
- {
95
- return s.find (input) != std::string::npos;
82
+ bool StringHelper::Contains (const std::string& s, const std::string& input) {
83
+ return s.find (input) != std::string::npos;
96
84
}
97
85
98
- bool StringHelper::EndsWith (const std::string& s, const std::string& input)
99
- {
100
- size_t inputLen = strlen (input.c_str ());
101
- return s.rfind (input) == (s.size () - inputLen);
86
+ bool StringHelper::EndsWith (const std::string& s, const std::string& input) {
87
+ size_t inputLen = strlen (input.c_str ());
88
+ return s.rfind (input) == (s.size () - inputLen);
102
89
}
103
90
104
- std::string StringHelper::Sprintf (const char * format, ...)
105
- {
106
- char buffer[32768 ];
107
- // char buffer[2048];
108
- std::string output;
109
- va_list va;
91
+ std::string StringHelper::Sprintf (const char * format, ...) {
92
+ char buffer[32768 ];
93
+ // char buffer[2048];
94
+ std::string output;
95
+ va_list va;
110
96
111
- va_start (va, format);
112
- vsprintf_s (buffer, format, va);
113
- va_end (va);
97
+ va_start (va, format);
98
+ vsprintf_s (buffer, format, va);
99
+ va_end (va);
114
100
115
- output = buffer;
116
- return output;
101
+ output = buffer;
102
+ return output;
117
103
}
118
104
119
- std::string StringHelper::Implode (std::vector<std::string>& elements,
120
- const char * const separator)
121
- {
122
- return " " ;
105
+ std::string StringHelper::Implode (std::vector<std::string>& elements, const char * const separator) {
106
+ return " " ;
123
107
124
- // return std::accumulate(std::begin(elements), std::end(elements), std::string(),
125
- // [separator](std::string& ss, std::string& s) {
126
- // return ss.empty() ? s : ss + separator + s;
127
- // });
108
+ // return std::accumulate(std::begin(elements), std::end(elements), std::string(),
109
+ // [separator](std::string& ss, std::string& s) {
110
+ // return ss.empty() ? s : ss + separator + s;
111
+ // });
128
112
}
129
113
130
- int64_t StringHelper::StrToL (const std::string& str, int32_t base)
131
- {
132
- return std::strtoull (str.c_str (), nullptr , base);
114
+ int64_t StringHelper::StrToL (const std::string& str, int32_t base) {
115
+ return std::strtoull (str.c_str (), nullptr , base);
133
116
}
134
117
135
- std::string StringHelper::BoolStr (bool b)
136
- {
137
- return b ? " true" : " false" ;
118
+ std::string StringHelper::BoolStr (bool b) {
119
+ return b ? " true" : " false" ;
138
120
}
139
121
140
- bool StringHelper::HasOnlyDigits (const std::string& str)
141
- {
142
- return std::all_of (str.begin (), str.end (), ::isdigit);
122
+ bool StringHelper::HasOnlyDigits (const std::string& str) {
123
+ return std::all_of (str.begin (), str.end (), ::isdigit);
143
124
}
144
125
145
126
// Validate a hex string based on the c89 standard
146
127
// https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Integer-Constants
147
- bool StringHelper::IsValidHex (std::string_view str)
148
- {
149
- if (str.length () < 3 )
150
- {
151
- return false ;
152
- }
153
- if (str[0 ] == ' 0' && (str[1 ] == ' x' || str[1 ] == ' X' ))
154
- {
155
- return std::all_of (str.begin () + 2 , str.end (), ::isxdigit);
156
- }
157
- return false ;
158
- }
159
-
160
-
161
- bool StringHelper::IsValidHex (const std::string& str)
162
- {
163
- return IsValidHex (std::string_view (str.c_str ()));
164
- }
165
-
166
- bool StringHelper::IsValidOffset (std::string_view str)
167
- {
168
- if (str.length () == 1 )
169
- {
170
- // 0 is a valid offset
171
- return isdigit (str[0 ]);
172
- }
173
- return IsValidHex (str);
174
- }
175
-
176
- bool StringHelper::IsValidOffset (const std::string& str)
177
- {
178
- if (str.length () == 1 )
179
- {
180
- // 0 is a valid offset
181
- return isdigit (str[0 ]);
182
- }
183
- return IsValidHex (str);
184
- }
185
-
186
-
187
- bool StringHelper::IEquals (const std::string& a, const std::string& b)
188
- {
189
- return std::equal (a.begin (), a.end (), b.begin (), b.end (),
190
- [](char a, char b) { return tolower (a) == tolower (b); });
128
+ bool StringHelper::IsValidHex (std::string_view str) {
129
+ if (str.length () < 3 ) {
130
+ return false ;
131
+ }
132
+ if (str[0 ] == ' 0' && (str[1 ] == ' x' || str[1 ] == ' X' )) {
133
+ return std::all_of (str.begin () + 2 , str.end (), ::isxdigit);
134
+ }
135
+ return false ;
136
+ }
137
+
138
+ bool StringHelper::IsValidHex (const std::string& str) {
139
+ return IsValidHex (std::string_view (str.c_str ()));
140
+ }
141
+
142
+ bool StringHelper::IsValidOffset (std::string_view str) {
143
+ if (str.length () == 1 ) {
144
+ // 0 is a valid offset
145
+ return isdigit (str[0 ]);
146
+ }
147
+ return IsValidHex (str);
148
+ }
149
+
150
+ bool StringHelper::IsValidOffset (const std::string& str) {
151
+ if (str.length () == 1 ) {
152
+ // 0 is a valid offset
153
+ return isdigit (str[0 ]);
154
+ }
155
+ return IsValidHex (str);
156
+ }
157
+
158
+ bool StringHelper::IEquals (const std::string& a, const std::string& b) {
159
+ return std::equal (a.begin (), a.end (), b.begin (), b.end (), [](char a, char b) { return tolower (a) == tolower (b); });
191
160
}
0 commit comments