7 #ifndef _FCITX_UTILS_STRINGUTILS_H_ 8 #define _FCITX_UTILS_STRINGUTILS_H_ 16 #include <initializer_list> 20 #include <string_view> 23 #include <fcitx-utils/fcitxutils_export.h> 24 #include <fcitx-utils/stringutils_details.h> 29 FCITXUTILS_EXPORT
bool startsWith(std::string_view str,
30 std::string_view prefix);
33 inline bool startsWith(std::string_view str,
char prefix) {
34 return !str.empty() && str.front() == prefix;
38 FCITXUTILS_EXPORT
bool endsWith(std::string_view str, std::string_view suffix);
41 inline bool endsWith(std::string_view str,
char suffix) {
42 return !str.empty() && str.back() == suffix;
46 inline bool isConcatOf(std::string_view str, std::string_view sub1,
47 std::string_view sub2) {
48 return str.size() == sub1.size() + sub2.size() &&
startsWith(str, sub1) &&
56 FCITXUTILS_EXPORT std::pair<std::string::size_type, std::string::size_type>
62 FCITXUTILS_EXPORT std::string_view
trimView(std::string_view);
66 FCITXUTILS_EXPORT std::string
trim(std::string_view str);
69 FCITXUTILS_EXPORT std::vector<std::string>
split(std::string_view str,
70 std::string_view delim);
72 enum class SplitBehavior { KeepEmpty, SkipEmpty };
75 FCITXUTILS_EXPORT std::vector<std::string>
76 split(std::string_view str, std::string_view delim, SplitBehavior behavior);
79 FCITXUTILS_EXPORT std::string
replaceAll(std::string str,
80 const std::string &before,
81 const std::string &after);
86 FCITXUTILS_EXPORT
const char *
backwardSearch(
const char *haystack,
size_t l,
87 const char *needle,
size_t ol,
93 const char *needle,
size_t ol,
103 FCITXUTILS_EXPORT
size_t backwardSearch(
const std::string &haystack,
104 const std::string &needle,
size_t from);
107 template <
typename Iter,
typename T>
108 inline std::string
join(Iter start, Iter end, T &&delim) {
114 for (; start != end; start++) {
122 template <
typename C,
typename T>
123 inline std::string
join(C &&container, T &&delim) {
126 return join(begin(container), end(container), delim);
130 template <
typename C,
typename T>
131 inline std::string
join(std::initializer_list<C> &&container, T &&delim) {
134 return join(begin(container), end(container), delim);
137 template <
typename... Args>
138 std::string concat(
const Args &...args) {
140 return concatPieces({
static_cast<const UniversalPiece &
>(
145 template <
typename FirstArg,
typename... Args>
146 std::string joinPath(
const FirstArg &firstArg,
const Args &...args) {
148 return concatPathPieces(
149 {
static_cast<const UniversalPiece &
>(
150 UniversalPieceHelper<FirstArg>::forward(firstArg))
152 static_cast<const UniversalPiece &
>(
153 UniversalPieceHelper<Args>::forward(args))
157 constexpr
bool literalEqual(
char const *a,
char const *b) {
158 return *a == *b && (*a ==
'\0' || literalEqual(a + 1, b + 1));
162 FCITXUTILS_EXPORT
bool unescape(std::string &str,
bool unescapeQuote);
172 FCITXUTILS_EXPORT std::optional<std::string>
183 FCITXUTILS_EXPORT std::string
escapeForValue(std::string_view str);
194 std::string_view prefix);
198 #endif // _FCITX_UTILS_STRINGUTILS_H_ 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.
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...
bool endsWith(std::string_view str, char suffix)
Check if a string ends with a suffix char.
std::vector< std::string > split(std::string_view str, std::string_view delim, SplitBehavior behavior)
Split the string by delim.
std::string_view trimView(std::string_view str)
Trim the white space in string view.
std::string join(std::initializer_list< C > &&container, T &&delim)
Join the strings with delim.
const char * backwardSearch(const char *haystack, size_t l, const char *needle, size_t ol, size_t from)
Search string needle of size ol in string haystack.
std::string replaceAll(std::string str, const std::string &before, const std::string &after)
Replace all substring appearance of before with after.
std::string escapeForValue(std::string_view str)
escape a string, add quote if needed.
std::string trim(std::string_view str)
Trim the white space in str.
bool isConcatOf(std::string_view str, std::string_view sub1, std::string_view sub2)
Check if a string is a concatenation of two other strings.
bool startsWith(std::string_view str, char prefix)
Check if a string starts with a prefix char.