26#ifndef MYSQLSHDK_LIBS_UTILS_UTILS_STRING_H_
27#define MYSQLSHDK_LIBS_UTILS_UTILS_STRING_H_
38#include <unordered_map>
39#include <unordered_set>
43#include "mysqlrouter/jit_executor_plugin_export.h"
52template <
typename Char,
typename F>
53inline std::basic_string<Char>
transform(std::basic_string_view<Char> s,
55 std::basic_string<Char>
r(s);
83 return ::_stricmp(a, b);
85 return ::strcasecmp(a, b);
91 return ::_wcsicmp(a, b);
93 return ::wcscasecmp(a, b);
99 return ::_strnicmp(a, b,
n);
101 return ::strncasecmp(a, b,
n);
107 return ::_wcsnicmp(a, b,
n);
109 return ::wcsncasecmp(a, b,
n);
114 bool operator()(
const std::string &a,
const std::string &b)
const {
115 return a.compare(b) < 0;
118 bool operator()(
const std::wstring &a,
const std::wstring &b)
const {
119 return a.compare(b) < 0;
124 bool operator()(
const std::string &a,
const std::string &b)
const {
128 bool operator()(
const std::wstring &a,
const std::wstring &b)
const {
137 bool operator()(
const std::string &a,
const std::string &b)
const {
142 bool operator()(
const std::wstring &a,
const std::wstring &b)
const {
149 bool operator()(
const std::string &a,
const std::string &b)
const {
150 return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
153 bool operator()(
const std::wstring &a,
const std::wstring &b)
const {
154 return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end());
160template <
typename Char>
162 std::basic_string_view<Char> b) {
163 if (a.length() != b.length())
return false;
164 return str_casecmp(a.data(), b.data(), a.length()) == 0;
167template <
typename Char,
typename...
T>
168inline bool str_caseeq(std::basic_string_view<Char> a,
T &&...tokens) {
169 return (str_caseeq_pair<Char>(a, std::forward<T>(tokens)) || ...);
174template <
typename...
T>
175inline bool str_caseeq(std::string_view a, std::string_view token,
180template <
typename...
T>
181inline bool str_caseeq(std::wstring_view a, std::wstring_view token,
189template <
typename Char>
191 std::basic_string_view<Char> prefix) {
192 return s.compare(0, prefix.length(), prefix) == 0;
195template <
typename Char,
typename...
T>
197 return (str_beginswith_pair<Char>(s, std::forward<T>(prefixes)) || ...);
202template <
typename...
T>
208template <
typename...
T>
216template <
typename Char>
218 std::basic_string_view<Char> prefix) {
219 if (s.length() < prefix.length())
return false;
220 return str_casecmp(s.data(), prefix.data(), prefix.length()) == 0;
223template <
typename Char,
typename...
T>
225 return (str_ibeginswith_pair<Char>(s, std::forward<T>(prefixes)) || ...);
230template <
typename...
T>
236template <
typename...
T>
245template <
typename Char>
247 std::basic_string_view<Char> suffix) {
248 if (suffix.length() > s.length())
return false;
249 return s.compare(s.length() - suffix.length(), suffix.length(), suffix) == 0;
252template <
typename Char,
typename...
T>
254 return (str_endswith_pair<Char>(s, std::forward<T>(suffixes)) || ...);
259template <
typename...
T>
265template <
typename...
T>
273template <
typename Char>
275 std::basic_string_view<Char> suffix) {
276 if (suffix.length() > s.length())
return false;
277 return str_casecmp(s.data() + s.length() - suffix.length(), suffix.data(),
278 suffix.length()) == 0;
281template <
typename Char,
typename...
T>
283 return (str_iendswith_pair<Char>(s, std::forward<T>(suffixes)) || ...);
288template <
typename...
T>
294template <
typename...
T>
300const char *
str_casestr(
const char *haystack,
const char *needle);
304inline size_t str_span(
const std::string &s1,
const std::string &s2) {
306 while (
p < s1.size() &&
p < s2.size()) {
307 if (s1[
p] != s2[
p])
return p;
310 if (
p == s1.size() &&
p == s2.size())
return std::string::npos;
315template <
class TOutput = std::
string>
317 std::string_view sep,
318 bool *found_sep =
nullptr) {
319 static_assert(std::is_same_v<TOutput, std::string> ||
320 std::is_same_v<TOutput, std::string_view>,
321 "Type must either be std::string or std::string_view");
323 auto p = s.find(sep);
326 *found_sep =
p != std::string_view::npos;
329 if (
p == std::string_view::npos)
330 return std::make_pair(TOutput{s}, TOutput{
""});
332 return std::make_pair(TOutput{s.substr(0,
p)},
333 TOutput{s.substr(
p + sep.length())});
337template <
class TOutput = std::
string>
339 std::string_view sep) {
340 static_assert(std::is_same_v<TOutput, std::string> ||
341 std::is_same_v<TOutput, std::string_view>,
342 "Type must either be std::string or std::string_view");
344 auto p = s.find(sep);
345 if (
p == std::string_view::npos) {
346 return std::make_pair(TOutput{s}, TOutput{
""});
350 return std::make_pair(TOutput{s.substr(0,
p)}, TOutput{s.substr(
p)});
355 const std::string &sep) {
356 auto p = s->find(sep);
357 if (
p == std::string::npos) {
358 std::string tmp = *s;
362 std::string tmp = s->substr(0,
p + sep.length());
363 s->erase(0,
p + sep.length());
383 std::string_view input, std::string_view separator_chars =
" \r\n\t",
384 int maxsplit = -1,
bool compress =
false) {
385 std::vector<std::string> ret_val;
386 size_t index = 0, new_find = 0;
387 const size_t end = input.size();
389 while (new_find < end) {
391 new_find = input.find_first_of(separator_chars,
index);
393 new_find = std::string_view::npos;
395 if (new_find != std::string_view::npos) {
399 ret_val.push_back(std::string{input.substr(
index, new_find -
index)});
401 index = new_find + 1;
403 ret_val.push_back(std::string{input.substr(
index)});
424template <
class TCallback>
426 std::string_view separator_chars =
" \r\n\t",
427 int maxsplit = -1,
bool compress =
false) {
428 static_assert(std::is_invocable_r_v<bool, TCallback, std::string_view>);
430 size_t index = 0, new_find = 0;
431 const auto end = input.size();
433 while (new_find < end) {
435 new_find = input.find_first_of(separator_chars,
index);
437 new_find = std::string_view::npos;
439 if (new_find != std::string_view::npos) {
443 if (!f(input.substr(
index, new_find -
index)))
return false;
445 index = new_find + 1;
447 if (!f(input.substr(
index)))
return false;
455 std::string_view chars =
" \r\n\t");
457 std::string_view chars =
" \r\n\t");
459 std::string_view chars =
" \r\n\t");
461std::string
str_strip(std::string_view s, std::string_view chars =
" \r\n\t");
462std::string
str_lstrip(std::string_view s, std::string_view chars =
" \r\n\t");
463std::string
str_rstrip(std::string_view s, std::string_view chars =
" \r\n\t");
465inline std::string
str_ljust(
const std::string &s,
size_t width,
467 if (s.size() < width)
return s + std::string(width - s.size(), pad);
471inline std::string
str_rjust(
const std::string &s,
size_t width,
473 if (s.size() < width)
return std::string(width - s.size(), pad).append(s);
481#if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4))
482std::string
str_format(
const char *formats, ...)
483 __attribute__((__format__(__printf__, 1, 2)));
485std::string
str_format(_In_z_ _Printf_format_string_
const char *
format, ...);
487std::string
str_format(
const char *formats, ...);
490template <
typename Iter>
492 if (
begin == end)
return {};
497 while (++
begin != end) {
505template <
typename Iter,
typename CTransform>
508 if (
begin == end)
return {};
513 while (++
begin != end) {
526template <
typename C,
typename CTransform>
530 std::forward<CTransform>(f));
533std::string JIT_EXECUTOR_PLUGIN_EXPORT
str_replace(std::string_view s,
534 std::string_view from,
535 std::string_view to);
537template <
typename... Args>
538inline std::string JIT_EXECUTOR_PLUGIN_EXPORT
str_replace(std::string_view s,
539 std::string_view from,
541 const Args &...rest) {
551std::string
string_to_hex(std::string_view s,
bool prefix =
true);
560std::string
quote_string(
const std::string &s,
char quote);
576#define STRINGIFY(s) STRINGIFY_(s)
577#define STRINGIFY_(s) #s
586 std::size_t line_width);
601std::pair<std::string::size_type, std::string::size_type>
get_quote_span(
602 const char quote_char,
const std::string &
str);
619std::wstring
utf8_to_wide(
const char *utf8,
const size_t utf8_length);
644std::string
wide_to_utf8(
const wchar_t *wide,
const size_t wide_length);
662std::string
truncate(
const std::string &
str,
const size_t max_length);
674 const size_t max_length);
684std::wstring
truncate(
const std::wstring &
str,
const size_t max_length);
696 const size_t max_length);
711std::string
pctencode(std::string_view s);
716std::string
pctdecode(std::string_view s);
732 return std::hash<std::string_view>{}(txt);
735 [[nodiscard]]
size_t operator()(std::string_view txt)
const {
736 return std::hash<std::string_view>{}(txt);
739 [[nodiscard]]
size_t operator()(
const std::string &txt)
const {
740 return std::hash<std::string>{}(txt);
744template <
typename Key,
typename T>
746 std::unordered_map<Key, T, string_hash, std::equal_to<>>;
748template <
typename Key>
const char * p
Definition: ctype-mb.cc:1227
#define T
Definition: jit_executor_value.cc:373
#define F
Definition: jit_executor_value.cc:374
static int compress(PACK_MRG_INFO *file, char *join_name)
Definition: myisampack.cc:466
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1084
Definition: atomics_array.h:39
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
bool index(const std::string &value, const String &search_for, uint32_t *idx)
Definition: contains.h:75
char tolower(const char &ch)
Definition: parsing_helpers.h:41
const char * begin(const char *const c)
Definition: base64.h:44
size_t size(const char *const c)
Definition: base64.h:46
mutable_buffer buffer(void *p, size_t n) noexcept
Definition: buffer.h:418
bool str_ibeginswith(std::basic_string_view< Char > s, T &&...prefixes)
Definition: utils_string.h:224
bool str_beginswith(std::basic_string_view< Char > s, T &&...prefixes)
Definition: utils_string.h:196
bool str_beginswith_pair(std::basic_string_view< Char > s, std::basic_string_view< Char > prefix)
Definition: utils_string.h:190
bool str_ibeginswith_pair(std::basic_string_view< Char > s, std::basic_string_view< Char > prefix)
Definition: utils_string.h:217
bool str_endswith(std::basic_string_view< Char > s, T &&...suffixes)
Definition: utils_string.h:253
bool str_caseeq(std::basic_string_view< Char > a, T &&...tokens)
Definition: utils_string.h:168
bool str_endswith_pair(std::basic_string_view< Char > s, std::basic_string_view< Char > suffix)
Definition: utils_string.h:246
bool str_iendswith(std::basic_string_view< Char > s, T &&...suffixes)
Definition: utils_string.h:282
std::basic_string< Char > transform(std::basic_string_view< Char > s, F fun)
Definition: utils_string.h:53
bool str_iendswith_pair(std::basic_string_view< Char > s, std::basic_string_view< Char > suffix)
Definition: utils_string.h:274
bool str_caseeq_pair(std::basic_string_view< Char > a, std::basic_string_view< Char > b)
Definition: utils_string.h:161
Definition: file_system_exceptions.h:34
bool str_beginswith(std::string_view s, std::string_view prefix, T &&...prefixes)
Definition: utils_string.h:203
bool str_ibeginswith(std::string_view s, std::string_view prefix, T &&...prefixes)
Definition: utils_string.h:231
bool str_iendswith(std::string_view s, std::string_view suffix, T &&...suffixes)
Definition: utils_string.h:289
std::string str_partition_after_inpl(std::string *s, const std::string &sep)
Partition a string in 2 after separator, in place, if present.
Definition: utils_string.h:354
std::string pctdecode(std::string_view s)
Decodes a string that is percent encoded based on RFC-3986.
Definition: utils_string.cc:568
std::string wide_to_utf8(const std::wstring &wide)
Convert UTF-16/UTF-32 (platform dependent) string to UTF-8 string.
Definition: utils_string.cc:357
size_t bits_to_string_hex_size(int nbits)
Definition: utils_string.cc:191
std::string str_upper(std::string_view s)
Convert a copy of an ASCII string to uppercase and return.
Definition: utils_string.h:63
int str_casecmp(const char *a, const char *b)
Compares 2 strings case insensitive (for ascii)
Definition: utils_string.h:81
void clear_buffer(char *buffer, size_t size)
Definition: utils_string.cc:51
std::string utf8_lower(std::string_view s)
Definition: utils_string.h:755
std::string_view str_strip_view(std::string_view s, std::string_view chars)
Strip a string out of blank chars.
Definition: utils_string.cc:73
bool str_caseeq(std::string_view a, std::string_view token, T &&...tokens)
Definition: utils_string.h:175
std::string str_rjust(const std::string &s, size_t width, char pad=' ')
Definition: utils_string.h:471
std::vector< std::string > str_split(std::string_view input, std::string_view separator_chars=" \r\n\t", int maxsplit=-1, bool compress=false)
Splits string based on each of the individual characters of the separator string.
Definition: utils_string.h:382
std::string str_lower(std::string_view s)
Convert a copy of an ASCII string to lowercase and return.
Definition: utils_string.h:72
bool str_endswith(std::string_view s, std::string_view suffix, T &&...suffixes)
Definition: utils_string.h:260
std::string bits_to_string(uint64_t bits, int nbits)
Definition: utils_string.cc:169
std::string utf8_upper(std::string_view s)
Definition: utils_string.h:751
std::string string_to_hex(std::string_view s, bool prefix)
Definition: utils_string.cc:199
std::unordered_map< Key, T, string_hash, std::equal_to<> > heterogeneous_map
Definition: utils_string.h:746
std::vector< std::string > str_break_into_lines(const std::string &line, std::size_t line_width)
Breaks string into lines of specified width without breaking words.
Definition: utils_string.cc:239
std::wstring utf8_to_wide(const std::string &utf8)
Convert UTF-8 string to UTF-16/UTF-32 (platform dependent) string.
Definition: utils_string.cc:349
std::string str_strip(std::string_view s, std::string_view chars)
Definition: utils_string.cc:93
std::string str_format(const char *formats,...)
Return a formatted a string.
Definition: utils_string.cc:105
std::string get_random_string(size_t size, const char *source)
Returns a string of the given size created with random characters from the provided source.
Definition: utils_string.cc:588
bool is_valid_utf8(std::string_view s)
Checks if the given string contains only valid UTF-8 code points.
Definition: utils_string.cc:462
std::string truncate(const std::string &str, const size_t max_length)
Truncates the given string to max_length code points.
Definition: utils_string.cc:418
std::pair< std::string::size_type, std::string::size_type > get_quote_span(const char quote_char, const std::string &str)
Auxiliary function to get the quotes span (i.e., start and end positions) for the given string.
Definition: utils_string.cc:276
std::pair< TOutput, TOutput > str_partition(std::string_view s, std::string_view sep, bool *found_sep=nullptr)
Partition a string in 2 at a separator, if present.
Definition: utils_string.h:316
std::string pctencode(std::string_view s)
Generates a percent encoded string based on RFC-3986, only unreserved characters are not encoded.
Definition: utils_string.cc:546
std::string str_lstrip(std::string_view s, std::string_view chars)
Definition: utils_string.cc:97
const char * str_casestr(const char *haystack, const char *needle)
Definition: utils_string.cc:612
std::string str_replace(std::string_view s, std::string_view from, std::string_view to)
Definition: utils_string.cc:141
std::string str_rstrip(std::string_view s, std::string_view chars)
Definition: utils_string.cc:101
std::string quote_string(const std::string &s, char quote)
Escape quote and \ chars.
Definition: utils_string.cc:217
std::string str_ljust(const std::string &s, size_t width, char pad=' ')
Definition: utils_string.h:465
size_t str_span(const std::string &s1, const std::string &s2)
Return position of the first difference in the strings or npos if they're the same.
Definition: utils_string.h:304
std::pair< TOutput, TOutput > str_partition_after(std::string_view s, std::string_view sep)
Partition a string in 2 after separator, in place, if present.
Definition: utils_string.h:338
bool str_itersplit(std::string_view input, TCallback &&f, std::string_view separator_chars=" \r\n\t", int maxsplit=-1, bool compress=false)
Split the given input string and call the functor for each token.
Definition: utils_string.h:425
std::string str_join(Iter begin, Iter end, std::string_view sep)
Definition: utils_string.h:491
std::string bits_to_string_hex(uint64_t bits, int nbits)
Definition: utils_string.cc:183
std::string unquote_string(std::string_view s, char quote)
Inverse of quote_string().
Definition: utils_string.cc:224
std::string_view str_lstrip_view(std::string_view s, std::string_view chars)
Definition: utils_string.cc:81
std::unordered_set< Key, string_hash, std::equal_to<> > heterogeneous_set
Definition: utils_string.h:749
std::pair< uint64_t, int > string_to_bits(std::string_view s)
Definition: utils_string.cc:175
std::string_view str_rstrip_view(std::string_view s, std::string_view chars)
Definition: utils_string.cc:87
const mysql_service_registry_t * r
Definition: pfs_example_plugin_employee.cc:86
repeated Source source
Definition: replication_asynchronous_connection_failover.proto:42
Definition: utils_string.h:133
bool operator()(const std::wstring &a, const std::wstring &b) const
Definition: utils_string.h:142
bool operator()(const std::string &a, const std::string &b) const
Definition: utils_string.h:137
bool case_sensitive
Definition: utils_string.h:134
Case_comparator(bool p_case_sensitive)
Definition: utils_string.h:135
Definition: utils_string.h:123
bool operator()(const std::wstring &a, const std::wstring &b) const
Definition: utils_string.h:128
bool operator()(const std::string &a, const std::string &b) const
Definition: utils_string.h:124
Definition: utils_string.h:113
bool operator()(const std::string &a, const std::string &b) const
Definition: utils_string.h:114
bool operator()(const std::wstring &a, const std::wstring &b) const
Definition: utils_string.h:118
Definition: utils_string.h:148
bool operator()(const std::string &a, const std::string &b) const
Definition: utils_string.h:149
bool operator()(const std::wstring &a, const std::wstring &b) const
Definition: utils_string.h:153
Transparent hashing function, enables heterogeneous lookup in unordered containers.
Definition: utils_string.h:728
size_t operator()(std::string_view txt) const
Definition: utils_string.h:735
size_t operator()(const char *txt) const
Definition: utils_string.h:731
size_t operator()(const std::string &txt) const
Definition: utils_string.h:739
void is_transparent
Definition: utils_string.h:729
int n
Definition: xcom_base.cc:509