99 lines
2.7 KiB
C
Raw Normal View History

2016-09-18 20:50:08 -07:00
#pragma once
#include <vcpkg/base/cstringview.h>
#include <vector>
2017-01-05 12:47:08 -08:00
namespace vcpkg::Strings::details
2016-09-18 20:50:08 -07:00
{
template<class T>
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(); }
2016-09-18 20:50:08 -07:00
inline const char* to_printf_arg(const char* s) { return s; }
2016-09-18 20:50:08 -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)
{
return s;
}
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
{
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))...);
}
std::wstring to_utf16(const CStringView& s);
2016-09-18 20:50:08 -07:00
std::string to_utf8(const wchar_t* 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);
std::string::const_iterator case_insensitive_ascii_find(const std::string& s, const std::string& pattern);
2016-10-04 14:44:19 -07:00
bool case_insensitive_ascii_contains(const std::string& s, const std::string& pattern);
bool case_insensitive_ascii_equals(const CStringView left, const CStringView right);
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
bool case_insensitive_ascii_starts_with(const std::string& s, const std::string& pattern);
template<class Container, class Transformer>
std::string join(const char* delimiter, const Container& v, Transformer transformer)
2017-01-27 20:09:40 -08:00
{
const auto begin = v.begin();
const auto end = v.end();
if (begin == end)
2017-01-27 20:09:40 -08:00
{
return std::string();
2017-01-27 20:09:40 -08:00
}
std::string output;
output.append(transformer(*begin));
for (auto it = std::next(begin); it != end; ++it)
2017-01-27 20:09:40 -08:00
{
output.append(delimiter);
output.append(transformer(*it));
2017-01-27 20:09:40 -08:00
}
return output;
}
template<class Container>
std::string join(const char* delimiter, const Container& v)
2017-01-27 20:09:40 -08:00
{
using Element = decltype(*v.begin());
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);
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);
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
}