#pragma once #include #include #include #include #include #include namespace vcpkg::Strings::details { template auto to_printf_arg(const T& t) -> decltype(t.to_string()) { return t.to_string(); } inline const char* to_printf_arg(const std::string& s) { return s.c_str(); } inline const char* to_printf_arg(const char* s) { return s; } template::value>> T to_printf_arg(T s) { return s; } std::string format_internal(const char* fmtstr, ...); inline void append_internal(std::string& into, char c) { into += c; } template()))> inline void append_internal(std::string& into, T x) { into += std::to_string(x); } inline void append_internal(std::string& into, const char* v) { into.append(v); } inline void append_internal(std::string& into, const std::string& s) { into.append(s); } template().to_string(std::declval()))> void append_internal(std::string& into, const T& t) { t.to_string(into); } template(), std::declval()))> void append_internal(std::string& into, const T& t) { to_string(into, t); } } namespace vcpkg::Strings { template std::string& append(std::string& into, const Arg& a) { details::append_internal(into, a); return into; } template std::string& append(std::string& into, const Arg& a, const Args&... args) { append(into, a); return append(into, args...); } template [[nodiscard]] std::string concat(const Args&... args) { std::string ret; append(ret, args...); return ret; } template std::string concat_or_view(const Args&... args) { return Strings::concat(args...); } template::value>> StringView concat_or_view(const T& v) { return v; } template std::string format(const char* fmtstr, const Args&... args) { using vcpkg::Strings::details::to_printf_arg; return details::format_internal(fmtstr, to_printf_arg(to_printf_arg(args))...); } #if defined(_WIN32) std::wstring to_utf16(StringView s); std::string to_utf8(const wchar_t* w); inline std::string to_utf8(const std::wstring& ws) { return to_utf8(ws.c_str()); } #endif std::string escape_string(std::string&& s, char char_to_escape, char escape_char); bool case_insensitive_ascii_contains(StringView s, StringView pattern); bool case_insensitive_ascii_equals(StringView left, StringView right); std::string ascii_to_lowercase(std::string&& s); std::string ascii_to_uppercase(std::string&& s); bool case_insensitive_ascii_starts_with(StringView s, StringView pattern); bool ends_with(StringView s, StringView pattern); bool starts_with(StringView s, StringView pattern); template std::string join(const char* delimiter, InputIterator begin, InputIterator end, Transformer transformer) { if (begin == end) { return std::string(); } std::string output; output.append(transformer(*begin)); for (auto it = std::next(begin); it != end; ++it) { output.append(delimiter); output.append(transformer(*it)); } return output; } template std::string join(const char* delimiter, const Container& v, Transformer transformer) { const auto begin = v.begin(); const auto end = v.end(); return join(delimiter, begin, end, transformer); } template std::string join(const char* delimiter, InputIterator begin, InputIterator end) { using Element = decltype(*begin); return join(delimiter, begin, end, [](const Element& x) -> const Element& { return x; }); } template std::string join(const char* delimiter, const Container& v) { using Element = decltype(*v.begin()); return join(delimiter, v, [](const Element& x) -> const Element& { return x; }); } std::string replace_all(std::string&& s, const std::string& search, StringView rep); std::string trim(std::string&& s); void trim_all_and_remove_whitespace_strings(std::vector* strings); std::vector split(const std::string& s, const std::string& delimiter); std::vector split(const std::string& s, const std::string& delimiter, size_t max_count); std::vector find_all_enclosed(StringView input, StringView left_delim, StringView right_delim); StringView find_exactly_one_enclosed(StringView input, StringView left_tag, StringView right_tag); Optional find_at_most_one_enclosed(StringView input, StringView left_tag, StringView right_tag); bool equals(StringView a, StringView b); template std::string serialize(const T& t) { std::string ret; serialize(t, ret); return ret; } const char* search(StringView haystack, StringView needle); bool contains(StringView haystack, StringView needle); // base 64 encoding with URL and filesafe alphabet (base64url) // based on IETF RFC 4648 // ignores padding, since one implicitly knows the length from the size of x template std::string b64url_encode(Integral x) { static_assert(std::is_integral_v); auto value = static_cast>(x); // 64 values, plus the implicit \0 constexpr static char map[0x41] = /* 0123456789ABCDEF */ /*0*/ "ABCDEFGHIJKLMNOP" /*1*/ "QRSTUVWXYZabcdef" /*2*/ "ghijklmnopqrstuv" /*3*/ "wxyz0123456789-_" ; constexpr static std::make_unsigned_t mask = 0x3F; constexpr static int shift = 5; std::string result; // reserve ceiling(number of bits / 3) result.reserve((sizeof(value) * 8 + 2) / 3); while (value != 0) { char mapped_value = map[value & mask]; result.push_back(mapped_value); } return result; } }