#ifndef FLUTTER_INAPPWEBVIEW_PLUGIN_UTIL_STRING_H_ #define FLUTTER_INAPPWEBVIEW_PLUGIN_UTIL_STRING_H_ #include #include #include #include #include #include "strconv.h" namespace flutter_inappwebview_plugin { template struct is_string : std::false_type {}; // Partial specialization - parameters used to qualify the specialization template struct is_string> : std::true_type {}; template using is_basic_string = is_string>; template static inline bool string_equals(const std::basic_string& s1, const std::basic_string& s2) { return s1.compare(s2) == 0; } template static inline bool string_equals(const std::basic_string& s1, const char* s2) { return s1.compare(s2) == 0; } template static inline bool string_equals(const char* s1, const std::basic_string& s2) { return s2.compare(s1) == 0; } static inline bool string_equals(const std::string& s1, const std::wstring& s2) { return string_equals(s1, wide_to_utf8(s2)); } static inline bool string_equals(const std::wstring& s1, const std::string& s2) { return string_equals(wide_to_utf8(s1), s2); } template static inline bool string_equals(const std::optional>& s1, const std::basic_string& s2) { return s1.has_value() ? string_equals(s1.value(), s2) : false; } template static inline bool string_equals(const std::basic_string& s1, const std::optional>& s2) { return s2.has_value() ? string_equals(s1, s2.value()) : false; } template static inline bool string_equals(const std::optional>& s1, const std::optional>& s2) { return s1.has_value() && s2.has_value() ? string_equals(s1.value(), s2.value()) : true; } static inline void replace_all(std::string& source, const std::string& from, const std::string& to) { std::string newString; newString.reserve(source.length()); // avoids a few memory allocations std::string::size_type lastPos = 0; std::string::size_type findPos; while (std::string::npos != (findPos = source.find(from, lastPos))) { newString.append(source, lastPos, findPos - lastPos); newString += to; lastPos = findPos + from.length(); } // Care for the rest after last occurrence newString += source.substr(lastPos); source.swap(newString); } static inline std::string replace_all_copy(const std::string& source, const std::string& from, const std::string& to) { std::string newString; newString.reserve(source.length()); // avoids a few memory allocations std::string::size_type lastPos = 0; std::string::size_type findPos; while (std::string::npos != (findPos = source.find(from, lastPos))) { newString.append(source, lastPos, findPos - lastPos); newString += to; lastPos = findPos + from.length(); } // Care for the rest after last occurrence newString += source.substr(lastPos); return newString; } template static inline std::basic_string join(const std::vector>& vec, const std::basic_string& delim) { return vec.empty() ? std::basic_string{ "" } : /* leave early if there are no items in the list */ std::accumulate( /* otherwise, accumulate */ ++vec.begin(), vec.end(), /* the range 2nd to after-last */ *vec.begin(), /* and start accumulating with the first item */ [delim](auto& a, auto& b) { return a + delim + b; }); } template static inline std::basic_string join(const std::vector>& vec, const char* delim) { return join(vec, std::basic_string{ delim }); } template static inline std::vector> split(const std::basic_string& s, std::basic_string delimiter) { size_t pos_start = 0, pos_end, delim_len = delimiter.length(); std::basic_string token; std::vector> res; while ((pos_end = s.find(delimiter, pos_start)) != std::basic_string::npos) { token = s.substr(pos_start, pos_end - pos_start); pos_start = pos_end + delim_len; res.push_back(token); } res.push_back(s.substr(pos_start)); return res; } template void to_lowercase(const std::basic_string& s) { std::transform(s.begin(), s.end(), s.begin(), [](const T v) { return static_cast(std::tolower(v)); }); } template std::basic_string to_lowercase_copy(const std::basic_string& s) { std::basic_string s2 = s; std::transform(s2.begin(), s2.end(), s2.begin(), [](const T v) { return static_cast(std::tolower(v)); }); return s2; } template void to_uppercase(const std::basic_string& s) { std::transform(s.begin(), s.end(), s.begin(), [](const T v) { return static_cast(std::toupper(v)); }); return s2; } template std::basic_string to_uppercase_copy(const std::basic_string& s) { std::basic_string s2 = s; std::transform(s2.begin(), s2.end(), s2.begin(), [](const T v) { return static_cast(std::toupper(v)); }); return s2; } } #endif //FLUTTER_INAPPWEBVIEW_PLUGIN_UTIL_STRING_H_