vcpkg/toolsrc/src/vcpkg_Strings.cpp

155 lines
4.5 KiB
C++
Raw Normal View History

2017-01-28 04:49:09 +08:00
#include "pch.h"
2016-09-19 11:50:08 +08:00
#include "vcpkg_Strings.h"
2017-01-06 04:47:08 +08:00
namespace vcpkg::Strings::details
2016-09-19 11:50:08 +08:00
{
// To disambiguate between two overloads
2016-12-16 09:09:14 +08:00
static const auto isspace = [](const char c)
{
return std::isspace(c);
};
static char tolower_char(const char c)
{
return static_cast<char>(std::tolower(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;
}
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
{
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), &details::tolower_char);
2016-10-05 05:44:19 +08:00
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
{
return details::tolower_char(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(), &details::tolower_char);
2016-10-05 05:44:19 +08:00
return output;
}
2016-11-03 10:34:30 +08:00
2017-01-28 12:09:40 +08:00
std::string join(const std::vector<std::string>& v, const std::string& prefix, const std::string& delimiter, const std::string& suffix)
2016-11-03 10:34:30 +08:00
{
2017-01-28 12:09:40 +08:00
return join(v, prefix, delimiter, suffix, [](const std::string& i) -> std::string
2016-11-03 10:34:30 +08:00
{
2017-01-28 12:09:40 +08:00
return i;
});
}
2016-11-03 10:34:30 +08:00
2017-01-28 12:09:40 +08:00
Joiner Joiner::on(const std::string& delimiter)
{
return Joiner(delimiter);
}
2016-11-03 10:34:30 +08:00
2017-01-28 12:09:40 +08:00
Joiner& Joiner::prefix(const std::string& prefix)
{
this->m_prefix = prefix;
return *this;
}
2016-11-03 10:34:30 +08:00
2017-01-28 12:09:40 +08:00
Joiner& Joiner::suffix(const std::string& suffix)
{
this->m_suffix = suffix;
return *this;
}
2016-11-03 10:34:30 +08:00
2017-01-28 12:09:40 +08:00
std::string Joiner::join(const std::vector<std::string>& v) const
{
return Strings::join(v, this->m_prefix, this->m_delimiter, this->m_suffix);
}
Joiner::Joiner(const std::string& delimiter) : m_prefix(""), m_delimiter(delimiter), m_suffix("")
{
2016-11-03 10:34:30 +08:00
}
2016-12-16 09:09:14 +08:00
void trim(std::string* s)
{
s->erase(std::find_if_not(s->rbegin(), s->rend(), details::isspace).base(), s->end());
2016-12-21 06:45:35 +08:00
s->erase(s->begin(), std::find_if_not(s->begin(), s->end(), details::isspace));
2016-12-16 09:09:14 +08:00
}
std::string trimmed(const std::string& s)
{
auto whitespace_back = std::find_if_not(s.rbegin(), s.rend(), details::isspace).base();
2016-12-21 06:45:35 +08:00
auto whitespace_front = std::find_if_not(s.begin(), whitespace_back, details::isspace);
return std::string(whitespace_front, whitespace_back);
2016-12-16 09:09:14 +08:00
}
void trim_all_and_remove_whitespace_strings(std::vector<std::string>* strings)
{
for (std::string& s : *strings)
{
trim(&s);
}
strings->erase(std::remove_if(strings->begin(), strings->end(), [](const std::string& s)-> bool
{
return s == "";
}), strings->end());
}
2017-01-24 07:13:12 +08:00
std::vector<std::string> split(const std::string& s, const std::string& delimiter)
{
std::vector<std::string> output;
size_t i = 0;
2017-01-28 12:09:40 +08:00
for (size_t pos = s.find(delimiter); pos != std::string::npos; pos = s.find(delimiter, pos))
2017-01-24 07:13:12 +08:00
{
output.push_back(s.substr(i, pos - i));
i = ++pos;
2017-01-24 08:50:29 +08:00
}
2017-01-24 07:13:12 +08:00
2017-01-24 08:50:29 +08:00
// Add the rest of the string after the last delimiter, unless there is nothing after it
if (i != s.length())
{
output.push_back(s.substr(i, s.length()));
2017-01-24 07:13:12 +08:00
}
return output;
}
2017-01-06 04:47:08 +08:00
}