vcpkg/toolsrc/src/vcpkg_Strings.cpp

119 lines
3.5 KiB
C++
Raw Normal View History

2016-09-19 11:50:08 +08:00
#include "vcpkg_Strings.h"
#include <cstdarg>
#include <algorithm>
#include <codecvt>
#include <iterator>
2016-12-16 09:09:14 +08:00
#include <functional>
#include <cctype>
2016-09-19 11:50:08 +08:00
namespace vcpkg {namespace Strings {namespace details
{
// To disambiguate between two overloads
2016-12-16 09:09:14 +08:00
static const auto isspace = [](const char c)
{
return std::isspace(c);
};
2016-09-19 11:50:08 +08:00
std::string format_internal(const char* fmtstr, ...)
{
va_list lst;
va_start(lst, fmtstr);
auto sz = _vscprintf(fmtstr, lst);
std::string output(sz, '\0');
_vsnprintf_s(&output[0], output.size() + 1, output.size() + 1, fmtstr, lst);
va_end(lst);
return output;
}
std::wstring wformat_internal(const wchar_t* fmtstr, ...)
2016-09-19 11:50:08 +08:00
{
va_list lst;
va_start(lst, fmtstr);
auto sz = _vscwprintf(fmtstr, lst);
std::wstring output(sz, '\0');
_vsnwprintf_s(&output[0], output.size() + 1, output.size() + 1, fmtstr, lst);
va_end(lst);
return output;
}
}}}
namespace vcpkg {namespace Strings
{
std::wstring utf8_to_utf16(const std::string& s)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> conversion;
return conversion.from_bytes(s);
}
std::string utf16_to_utf8(const std::wstring& w)
{
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> conversion;
return conversion.to_bytes(w);
}
std::string::const_iterator case_insensitive_ascii_find(const std::string& s, const std::string& pattern)
2016-09-19 11:50:08 +08:00
{
2016-10-05 05:44:19 +08:00
std::string pattern_as_lower_case;
std::transform(pattern.begin(), pattern.end(), back_inserter(pattern_as_lower_case), tolower);
return search(s.begin(), s.end(), pattern_as_lower_case.begin(), pattern_as_lower_case.end(), [](const char a, const char b)
2016-09-19 11:50:08 +08:00
{
2016-10-04 10:48:56 +08:00
return tolower(a) == b;
2016-09-19 11:50:08 +08:00
});
}
2016-10-05 05:44:19 +08:00
std::string ascii_to_lowercase(const std::string& input)
{
std::string output = input;
std::transform(output.begin(), output.end(), output.begin(), ::tolower);
return output;
}
2016-11-03 10:34:30 +08:00
std::string join(const std::vector<std::string>& v, const std::string& delimiter)
{
if (v.empty())
{
return std::string();
}
std::string output;
size_t size = v.size();
output.append(v.at(0));
for (size_t i = 1; i < size; ++i)
2016-11-03 10:34:30 +08:00
{
output.append(delimiter);
output.append(v.at(i));
}
return output;
}
2016-12-16 09:09:14 +08:00
void trim(std::string* s)
{
s->erase(s->begin(), std::find_if_not(s->begin(), s->end(), details::isspace));
s->erase(std::find_if_not(s->rbegin(), s->rend(), details::isspace).base(), s->end());
}
std::string trimmed(const std::string& s)
{
auto whitespace_front = std::find_if_not(s.begin(), s.end(), details::isspace);
auto whitespace_back = std::find_if_not(s.rbegin(), s.rend(), details::isspace).base();
return (whitespace_back <= whitespace_front ? std::string() : std::string(whitespace_front, whitespace_back));
}
void trim_all_and_remove_whitespace_strings(std::vector<std::string>* strings)
{
strings->erase(std::remove_if(strings->begin(), strings->end(), [](std::string& s)-> bool
{
trim(&s);
return s == "";
}), strings->end());
}
2016-09-19 11:50:08 +08:00
}}