2016-09-18 20:50:08 -07:00
|
|
|
#pragma once
|
|
|
|
|
2017-10-13 18:37:41 -07:00
|
|
|
#include <vcpkg/base/cstringview.h>
|
|
|
|
|
2017-04-27 17:56:06 -07:00
|
|
|
#include <vector>
|
2017-03-28 12:27:11 -07:00
|
|
|
|
2017-01-05 12:47:08 -08:00
|
|
|
namespace vcpkg::Strings::details
|
2016-09-18 20:50:08 -07:00
|
|
|
{
|
2017-04-07 16:13:08 -07:00
|
|
|
template<class T>
|
|
|
|
auto to_printf_arg(const T& t) -> decltype(t.to_string())
|
|
|
|
{
|
|
|
|
return t.to_string();
|
|
|
|
}
|
|
|
|
|
2017-04-27 17:56:06 -07:00
|
|
|
inline const char* to_printf_arg(const std::string& s) { return s.c_str(); }
|
2016-09-18 20:50:08 -07:00
|
|
|
|
2017-04-27 17:56:06 -07:00
|
|
|
inline const char* to_printf_arg(const char* s) { return s; }
|
2016-09-18 20:50:08 -07:00
|
|
|
|
2017-10-16 11:44:04 -07:00
|
|
|
template<class T, class = std::enable_if_t<std::is_arithmetic<T>::value>>
|
2017-10-16 13:52:35 -07:00
|
|
|
T to_printf_arg(T s)
|
2017-10-16 11:44:04 -07:00
|
|
|
{
|
|
|
|
return s;
|
|
|
|
}
|
2017-08-24 12:06:22 -07:00
|
|
|
|
2016-09-18 20:50:08 -07:00
|
|
|
std::string format_internal(const char* fmtstr, ...);
|
2017-01-05 12:47:08 -08:00
|
|
|
}
|
2016-09-18 20:50:08 -07:00
|
|
|
|
2017-01-05 12:47:08 -08:00
|
|
|
namespace vcpkg::Strings
|
2016-09-18 20:50:08 -07:00
|
|
|
{
|
2017-04-27 17:56:06 -07:00
|
|
|
template<class... Args>
|
|
|
|
std::string format(const char* fmtstr, const Args&... args)
|
2016-09-18 20:50:08 -07:00
|
|
|
{
|
|
|
|
using vcpkg::Strings::details::to_printf_arg;
|
|
|
|
return details::format_internal(fmtstr, to_printf_arg(to_printf_arg(args))...);
|
|
|
|
}
|
|
|
|
|
2017-10-16 13:50:28 -07:00
|
|
|
std::wstring to_utf16(const CStringView& s);
|
2016-09-18 20:50:08 -07:00
|
|
|
|
2017-10-16 13:50:28 -07:00
|
|
|
std::string to_utf8(const CWStringView& w);
|
2016-09-18 20:50:08 -07:00
|
|
|
|
2018-02-19 12:20:16 -08:00
|
|
|
std::string escape_string(const CStringView& s, char char_to_escape, char escape_char);
|
|
|
|
|
2016-10-04 15:23:44 -07:00
|
|
|
std::string::const_iterator case_insensitive_ascii_find(const std::string& s, const std::string& pattern);
|
2016-10-04 14:44:19 -07:00
|
|
|
|
2017-06-19 15:06:15 -07:00
|
|
|
bool case_insensitive_ascii_contains(const std::string& s, const std::string& pattern);
|
|
|
|
|
2017-10-16 11:44:04 -07:00
|
|
|
bool case_insensitive_ascii_equals(const CStringView left, const CStringView right);
|
2017-05-04 15:19:22 -07:00
|
|
|
|
2018-03-16 16:53:48 -07:00
|
|
|
std::string ascii_to_lowercase(std::string s);
|
|
|
|
|
|
|
|
std::string ascii_to_uppercase(std::string s);
|
2016-11-02 19:34:30 -07:00
|
|
|
|
2017-10-01 13:22:29 -07:00
|
|
|
bool case_insensitive_ascii_starts_with(const std::string& s, const std::string& pattern);
|
|
|
|
|
2017-10-16 13:54:38 -07:00
|
|
|
template<class Container, class Transformer>
|
|
|
|
std::string join(const char* delimiter, const Container& v, Transformer transformer)
|
2017-01-27 20:09:40 -08:00
|
|
|
{
|
2017-08-28 18:36:25 -07:00
|
|
|
const auto begin = v.begin();
|
|
|
|
const auto end = v.end();
|
|
|
|
|
|
|
|
if (begin == end)
|
2017-01-27 20:09:40 -08:00
|
|
|
{
|
2017-10-16 13:54:38 -07:00
|
|
|
return std::string();
|
2017-01-27 20:09:40 -08:00
|
|
|
}
|
|
|
|
|
2017-10-16 13:54:38 -07:00
|
|
|
std::string output;
|
2017-08-28 18:36:25 -07:00
|
|
|
output.append(transformer(*begin));
|
|
|
|
for (auto it = std::next(begin); it != end; ++it)
|
2017-01-27 20:09:40 -08:00
|
|
|
{
|
|
|
|
output.append(delimiter);
|
2017-08-28 18:36:25 -07:00
|
|
|
output.append(transformer(*it));
|
2017-01-27 20:09:40 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
2017-10-16 13:54:38 -07:00
|
|
|
template<class Container>
|
|
|
|
std::string join(const char* delimiter, const Container& v)
|
2017-01-27 20:09:40 -08:00
|
|
|
{
|
2017-08-28 18:36:25 -07:00
|
|
|
using Element = decltype(*v.begin());
|
2017-06-08 00:36:17 -07:00
|
|
|
return join(delimiter, v, [](const Element& x) -> const Element& { return x; });
|
2017-03-08 22:33:28 -08:00
|
|
|
}
|
|
|
|
|
2017-10-19 19:38:23 -07:00
|
|
|
std::string replace_all(std::string&& s, const std::string& search, const std::string& rep);
|
|
|
|
|
2017-10-20 16:43:44 -07:00
|
|
|
std::string trim(std::string&& s);
|
2016-12-16 16:02:19 -08:00
|
|
|
|
|
|
|
void trim_all_and_remove_whitespace_strings(std::vector<std::string>* strings);
|
2017-01-23 15:13:12 -08:00
|
|
|
|
|
|
|
std::vector<std::string> split(const std::string& s, const std::string& delimiter);
|
2017-04-12 22:48:52 -07:00
|
|
|
|
|
|
|
template<class T>
|
|
|
|
std::string serialize(const T& t)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
serialize(t, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2017-01-05 12:47:08 -08:00
|
|
|
}
|