vcpkg/toolsrc/include/vcpkg_Strings.h

114 lines
3.4 KiB
C
Raw Normal View History

2016-09-19 11:50:08 +08:00
#pragma once
2017-04-04 05:21:51 +08:00
#include "CStringView.h"
#include <vector>
2017-01-06 04:47:08 +08:00
namespace vcpkg::Strings::details
2016-09-19 11:50:08 +08: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-19 11:50:08 +08:00
inline const char* to_printf_arg(const char* s) { return s; }
2016-09-19 11:50:08 +08:00
inline int to_printf_arg(const int s) { return s; }
2016-09-19 11:50:08 +08:00
inline long long to_printf_arg(const long long s) { return s; }
inline unsigned long to_printf_arg(const unsigned long s) { return s; }
2016-10-11 06:03:48 +08:00
inline size_t to_printf_arg(const size_t s) { return s; }
2016-10-01 02:21:51 +08:00
inline double to_printf_arg(const double s) { return s; }
2016-09-19 11:50:08 +08:00
std::string format_internal(const char* fmtstr, ...);
inline const wchar_t* to_wprintf_arg(const std::wstring& s) { return s.c_str(); }
2016-09-19 11:50:08 +08:00
inline const wchar_t* to_wprintf_arg(const wchar_t* s) { return s; }
2016-09-19 11:50:08 +08:00
std::wstring wformat_internal(const wchar_t* fmtstr, ...);
2017-01-06 04:47:08 +08:00
}
2016-09-19 11:50:08 +08:00
2017-01-06 04:47:08 +08:00
namespace vcpkg::Strings
2016-09-19 11:50:08 +08:00
{
2017-08-29 10:27:32 +08:00
static constexpr const char* EMPTY = "";
static constexpr const wchar_t* WEMPTY = L"";
2017-08-29 09:45:30 +08:00
template<class... Args>
std::string format(const char* fmtstr, const Args&... args)
2016-09-19 11:50:08 +08:00
{
using vcpkg::Strings::details::to_printf_arg;
return details::format_internal(fmtstr, to_printf_arg(to_printf_arg(args))...);
}
template<class... Args>
std::wstring wformat(const wchar_t* fmtstr, const Args&... args)
2016-09-19 11:50:08 +08:00
{
using vcpkg::Strings::details::to_wprintf_arg;
return details::wformat_internal(fmtstr, to_wprintf_arg(to_wprintf_arg(args))...);
2016-09-19 11:50:08 +08:00
}
std::wstring to_utf16(const CStringView s);
2016-09-19 11:50:08 +08:00
std::string to_utf8(const CWStringView w);
2016-09-19 11:50:08 +08:00
std::string::const_iterator case_insensitive_ascii_find(const std::string& s, const std::string& pattern);
2016-10-05 05:44:19 +08:00
bool case_insensitive_ascii_contains(const std::string& s, const std::string& pattern);
int case_insensitive_ascii_compare(const CStringView left, const CStringView right);
2016-10-05 05:44:19 +08:00
std::string ascii_to_lowercase(const std::string& input);
2016-11-03 10:34:30 +08:00
bool case_insensitive_ascii_starts_with(const std::string& s, const std::string& pattern);
template<class Container, class Transformer, class CharType>
std::basic_string<CharType> join(const CharType* delimiter, const Container& v, Transformer transformer)
2017-01-28 12:09:40 +08:00
{
const auto begin = v.begin();
const auto end = v.end();
if (begin == end)
2017-01-28 12:09:40 +08:00
{
return std::basic_string<CharType>();
2017-01-28 12:09:40 +08:00
}
std::basic_string<CharType> output;
output.append(transformer(*begin));
for (auto it = std::next(begin); it != end; ++it)
2017-01-28 12:09:40 +08:00
{
output.append(delimiter);
output.append(transformer(*it));
2017-01-28 12:09:40 +08:00
}
return output;
}
template<class Container, class CharType>
std::basic_string<CharType> join(const CharType* delimiter, const Container& v)
2017-01-28 12:09:40 +08:00
{
using Element = decltype(*v.begin());
return join(delimiter, v, [](const Element& x) -> const Element& { return x; });
2017-03-09 14:33:28 +08:00
}
2016-12-16 09:09:14 +08:00
void trim(std::string* s);
std::string trimmed(const std::string& s);
void trim_all_and_remove_whitespace_strings(std::vector<std::string>* strings);
2017-01-24 07: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-06 04:47:08 +08:00
}