11 #include <initializer_list> 14 #include <string_view> 17 #include <fcitx-utils/fcitxutils_export.h> 25 concatPieces(std::initializer_list<std::pair<const char *, std::size_t>> list) {
27 for (
auto pair : list) {
32 for (
const auto &pair : list) {
33 result.append(pair.first, pair.first + pair.second);
35 assert(result.size() == size);
39 std::string concatPathPieces(
40 std::initializer_list<std::pair<const char *, std::size_t>> list) {
46 bool firstPieceIsSlash =
false;
48 for (
const auto &pair : list) {
50 if (pair.first[pair.second - 1] ==
'/') {
51 firstPieceIsSlash =
true;
59 if (list.size() > 1 && firstPieceIsSlash) {
65 for (
auto pair : list) {
68 }
else if (firstPieceIsSlash) {
69 firstPieceIsSlash =
false;
74 result.append(pair.first, pair.first + pair.second);
76 assert(result.size() == size);
81 FCITXUTILS_DEPRECATED_EXPORT
bool startsWith(
const std::string &str,
82 const std::string &prefix) {
83 return str.starts_with(prefix);
86 bool startsWith(std::string_view str, std::string_view prefix) {
87 return str.starts_with(prefix);
90 FCITXUTILS_DEPRECATED_EXPORT
bool endsWith(
const std::string &str,
91 const std::string &suffix) {
92 return str.ends_with(suffix);
95 bool endsWith(std::string_view str, std::string_view suffix) {
96 return str.ends_with(suffix);
99 inline std::pair<std::string::size_type, std::string::size_type>
100 trimInplaceImpl(std::string_view str) {
101 auto start = str.find_first_not_of(FCITX_WHITESPACE);
102 if (start == std::string::npos) {
103 return {str.size(), str.size()};
106 auto end = str.size();
107 while (end > start && charutils::isspace(str[end - 1])) {
114 FCITXUTILS_DEPRECATED_EXPORT
115 std::pair<std::string::size_type, std::string::size_type>
117 return trimInplaceImpl(str);
120 std::pair<std::string::size_type, std::string::size_type>
122 return trimInplaceImpl(str);
125 FCITXUTILS_DEPRECATED_EXPORT
126 std::string
trim(
const std::string &str) {
return trim(std::string_view(str)); }
128 std::string
trim(std::string_view str) {
129 auto pair = trimInplaceImpl(str);
130 return {str.begin() + pair.first, str.begin() + pair.second};
135 return str.substr(pair.first, pair.second - pair.first);
138 FCITXUTILS_DEPRECATED_EXPORT
139 std::vector<std::string>
split(
const std::string &str,
const std::string &delim,
140 SplitBehavior behavior) {
141 return split(std::string_view(str), std::string_view(delim), behavior);
144 std::vector<std::string>
split(std::string_view str, std::string_view delim,
145 SplitBehavior behavior) {
146 std::vector<std::string> strings;
147 std::string::size_type lastPos;
148 std::string::size_type pos;
149 if (behavior == SplitBehavior::SkipEmpty) {
150 lastPos = str.find_first_not_of(delim, 0);
154 pos = str.find_first_of(delim, lastPos);
156 while (std::string::npos != pos || std::string::npos != lastPos) {
157 strings.push_back(std::string(str.substr(lastPos, pos - lastPos)));
158 if (behavior == SplitBehavior::SkipEmpty) {
159 lastPos = str.find_first_not_of(delim, pos);
161 if (pos == std::string::npos) {
166 pos = str.find_first_of(delim, lastPos);
172 FCITXUTILS_DEPRECATED_EXPORT std::vector<std::string>
173 split(
const std::string &str,
const std::string &delim) {
174 return split(std::string_view(str), std::string_view(delim));
177 std::vector<std::string>
split(std::string_view str, std::string_view delim) {
178 return split(str, delim, SplitBehavior::SkipEmpty);
181 std::string
replaceAll(std::string str,
const std::string &before,
182 const std::string &after) {
183 if (before.empty()) {
187 constexpr
int MAX_REPLACE_INDICES_NUM = 128;
190 std::string newString;
192 size_t indices[MAX_REPLACE_INDICES_NUM];
194 size_t newStringPos = 0;
195 size_t oldStringPos = 0;
197 auto copyAndMoveOn = [&newString, &newStringPos](std::string_view source,
204 newString.replace(newStringPos,
length, source, pos,
length);
211 while (nIndices < MAX_REPLACE_INDICES_NUM) {
212 pivot = str.find(before, pivot);
213 if (pivot == std::string::npos) {
217 indices[nIndices++] = pivot;
218 pivot += before.size();
223 lastLen = str.size() + nIndices * after.size() -
224 nIndices * before.size();
225 newString.resize(lastLen);
227 size_t newLen = lastLen + nIndices * after.size() -
228 nIndices * before.size();
230 newString.resize(newLen);
237 copyAndMoveOn(str, oldStringPos, indices[0] - oldStringPos);
238 copyAndMoveOn(after, 0, after.size());
240 for (
int i = 1; i < nIndices; i++) {
241 copyAndMoveOn(str, indices[i - 1] + before.size(),
242 indices[i] - (indices[i - 1] + before.size()));
243 copyAndMoveOn(after, 0, after.size());
246 oldStringPos = indices[nIndices - 1] + before.size();
248 }
while (pivot != std::string::npos);
254 copyAndMoveOn(str, oldStringPos, str.size() - oldStringPos);
255 newString.resize(newStringPos);
261 if (ol_minus_1 < sizeof(unsigned int) * CHAR_BIT) \ 262 hashHaystack -= (a) << ol_minus_1; \ 266 size_t ol,
size_t from) {
270 size_t delta = l - ol;
278 const char *end = haystack;
280 const unsigned int ol_minus_1 = ol - 1;
281 const char *n = needle + ol_minus_1;
282 const char *h = haystack + ol_minus_1;
283 unsigned int hashNeedle = 0;
284 unsigned int hashHaystack = 0;
286 for (idx = 0; idx < ol; ++idx) {
287 hashNeedle = ((hashNeedle << 1) + *(n - idx));
288 hashHaystack = ((hashHaystack << 1) + *(h - idx));
290 hashHaystack -= *haystack;
291 while (haystack >= end) {
292 hashHaystack += *haystack;
293 if (hashHaystack == hashNeedle && memcmp(needle, haystack, ol) == 0) {
297 REHASH(*(haystack + ol));
305 static_cast<const char *>(haystack), l, needle, ol, from));
310 const auto *cstr = haystack.c_str();
311 const auto *result =
backwardSearch(cstr, haystack.size(), needle.c_str(),
312 needle.size(), from);
314 return result - cstr;
316 return std::string::npos;
319 enum class UnescapeState { NORMAL, ESCAPE };
321 bool unescape(std::string &str,
bool unescapeQuote) {
328 UnescapeState state = UnescapeState::NORMAL;
331 case UnescapeState::NORMAL:
332 if (str[i] ==
'\\') {
333 state = UnescapeState::ESCAPE;
339 case UnescapeState::ESCAPE:
340 if (str[i] ==
'\\') {
343 }
else if (str[i] ==
'n') {
346 }
else if (str[i] ==
'\"' && unescapeQuote) {
352 state = UnescapeState::NORMAL;
361 bool unescapeQuote =
false;
363 if (str.size() >= 2 && str.front() ==
'"' && str.back() ==
'"') {
364 unescapeQuote =
true;
365 str = str.substr(1, str.size() - 2);
368 return std::string();
371 std::string value(str);
380 value.reserve(str.size());
381 const bool needQuote =
382 str.find_first_of(
"\f\r\t\v \"") != std::string::npos;
384 value.push_back(
'"');
389 value.append(
"\\\\");
395 value.append(
"\\\"");
403 value.push_back(
'"');
410 if (str.starts_with(prefix)) {
411 str = str.substr(prefix.size());
std::vector< std::string > split(std::string_view str, std::string_view delim)
Split the string by delim.
std::optional< std::string > unescapeForValue(std::string_view str)
unescape a string, that is potentially quoted.
bool unescape(std::string &str, bool unescapeQuote)
Inplace unescape a string contains slash, new line, optionally quote.
bool consumePrefix(std::string_view &str, std::string_view prefix)
Return a substring of input str if str starts with given prefix.
size_t length(Iter start, Iter end)
Return the number UTF-8 characters in the string iterator range.
std::pair< std::string::size_type, std::string::size_type > trimInplace(std::string_view str)
Trim the whitespace by returning start end end of first and list non whitespace character position...
std::string_view trimView(std::string_view str)
Trim the white space in string view.
bool endsWith(std::string_view str, std::string_view suffix)
Check if a string ends with a suffix.
std::string replaceAll(std::string str, const std::string &before, const std::string &after)
Replace all substring appearance of before with after.
bool startsWith(std::string_view str, std::string_view prefix)
Check if a string starts with a prefix.
std::string escapeForValue(std::string_view str)
escape a string, add quote if needed.
size_t backwardSearch(const std::string &haystack, const std::string &needle, size_t from)
Fast backward substring search.
std::string trim(std::string_view str)
Trim the white space in str.
Local independent API to detect character type.