18 if (str ==
"1")
return true;
19 if ((str.size() == 4) && (strncasecmp(str.data(),
"true", 4) == 0))
21 if ((str.size() == 3) && (strncasecmp(str.data(),
"yes", 3) == 0))
28 std::string result(str);
35 if (
auto pos = str.find_last_not_of(chars); pos != string::npos) {
43 if (
auto pos = str.find_last_not_of(chars); pos != string::npos) {
51 while (!str.empty() && (chars.find(str.back()) != string_view::npos)) {
57 while (!str.empty() && (str.back() == chars)) {
64 str.erase(0, str.find_first_not_of(chars));
68 str.erase(0, str.find_first_not_of(chars));
70void trimLeft(string_view& str, string_view chars)
72 while (!str.empty() && (chars.find(str.front()) != string_view::npos)) {
78 while (!str.empty() && (str.front() == chars)) {
83void trim(string_view& str, string_view chars)
89void trim(string_view& str,
char chars)
95std::pair<string_view, string_view>
splitOnFirst(string_view str, string_view chars)
97 if (
auto pos = str.find_first_of(chars); pos == string_view::npos) {
98 return {str, string_view{}};
100 return {str.substr(0, pos), str.substr(pos + 1)};
103std::pair<string_view, string_view>
splitOnFirst(string_view str,
char chars)
105 if (
auto pos = str.find_first_of(chars); pos == string_view::npos) {
106 return {str, string_view{}};
108 return {str.substr(0, pos), str.substr(pos + 1)};
112std::pair<string_view, string_view>
splitOnLast(string_view str, string_view chars)
114 if (
auto pos = str.find_last_of(chars); pos == string_view::npos) {
115 return {string_view{}, str};
117 return {str.substr(0, pos), str.substr(pos + 1)};
120std::pair<string_view, string_view>
splitOnLast(string_view str,
char chars)
122 if (
auto pos = str.find_last_of(chars); pos == string_view::npos) {
123 return {string_view{}, str};
125 return {str.substr(0, pos), str.substr(pos + 1)};
140static unsigned parseNumber(string_view str)
143 auto r = stringToBase<10, unsigned>(str);
150static void checkRange(
unsigned x,
unsigned min,
unsigned max)
152 if ((min <= x) && (x <= max))
return;
154 "Out of range, should be between [, ", min,
", ", max,
159 unsigned min,
unsigned max)
163 if (str.empty())
return;
165 if (
auto pos = str.find(
'-'); pos == string_view::npos) {
166 auto x = parseNumber(str);
167 checkRange(x, min, max);
170 unsigned begin = parseNumber(str.substr(0, pos));
171 unsigned end = parseNumber(str.substr(pos + 1));
175 checkRange(
begin, min, max);
176 checkRange(
end, min, max);
188 auto next = str.find(
',');
189 string_view sub = (next == string_view::npos)
191 : str.substr(0, next++);
192 parseRange2(sub, result, min, max);
193 if (next == string_view::npos)
break;
194 str = str.substr(next);
199#if defined(__APPLE__)
201std::string fromCFString(CFStringRef str)
204 const char *cstr = CFStringGetCStringPtr(str, kCFStringEncodingUTF8);
207 return std::string(cstr);
211 CFIndex len = CFStringGetLength(str);
212 CFRange range = CFRangeMake(0, len);
213 CFIndex usedBufLen = 0;
215 str, range, kCFStringEncodingUTF8,
'?',
false,
nullptr, len, &usedBufLen);
216 UInt8 buffer[usedBufLen];
218 str, range, kCFStringEncodingUTF8,
'?',
false, buffer, len, &usedBufLen);
219 return std::string(
reinterpret_cast<const char*
>(buffer), usedBufLen);