mirror of
https://github.com/Kitware/CMake.git
synced 2026-02-22 06:59:01 -06:00
cmStringAlgorithms: cmIsSpace, cmTrimWhitespace, cmEscapeQuotes, cmTokenize
This adds the following functions to `cmStringAlgorithms`: - `cmIsSpace` - `cmTrimWhitespace` (moved from `cmSystemTools::TrimWhitespace`) - `cmEscapeQuotes` (moved from `cmSystemTools::EscapeQuotes`) - `cmTokenize` (moved from `cmSystemTools::tokenize` and adapted to accept `cm::string_view`)
This commit is contained in:
@@ -5,6 +5,59 @@
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
|
||||
std::string cmTrimWhitespace(cm::string_view str)
|
||||
{
|
||||
auto start = str.begin();
|
||||
while (start != str.end() && cmIsSpace(*start)) {
|
||||
++start;
|
||||
}
|
||||
if (start == str.end()) {
|
||||
return std::string();
|
||||
}
|
||||
auto stop = str.end() - 1;
|
||||
while (cmIsSpace(*stop)) {
|
||||
--stop;
|
||||
}
|
||||
return std::string(start, stop + 1);
|
||||
}
|
||||
|
||||
std::string cmEscapeQuotes(cm::string_view str)
|
||||
{
|
||||
std::string result;
|
||||
result.reserve(str.size());
|
||||
for (const char ch : str) {
|
||||
if (ch == '"') {
|
||||
result += '\\';
|
||||
}
|
||||
result += ch;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<std::string> cmTokenize(cm::string_view str, cm::string_view sep)
|
||||
{
|
||||
std::vector<std::string> tokens;
|
||||
cm::string_view::size_type tokend = 0;
|
||||
|
||||
do {
|
||||
cm::string_view::size_type tokstart = str.find_first_not_of(sep, tokend);
|
||||
if (tokstart == cm::string_view::npos) {
|
||||
break; // no more tokens
|
||||
}
|
||||
tokend = str.find_first_of(sep, tokstart);
|
||||
if (tokend == cm::string_view::npos) {
|
||||
tokens.emplace_back(str.substr(tokstart));
|
||||
} else {
|
||||
tokens.emplace_back(str.substr(tokstart, tokend - tokstart));
|
||||
}
|
||||
} while (tokend != cm::string_view::npos);
|
||||
|
||||
if (tokens.empty()) {
|
||||
tokens.emplace_back();
|
||||
}
|
||||
return tokens;
|
||||
}
|
||||
|
||||
namespace {
|
||||
template <std::size_t N, typename T>
|
||||
inline void MakeDigits(cm::string_view& view, char (&digits)[N],
|
||||
|
||||
Reference in New Issue
Block a user