diff options
author | Sebastian Holtermann <sebholt@xwmw.org> | 2019-07-31 07:06:04 (GMT) |
---|---|---|
committer | Sebastian Holtermann <sebholt@xwmw.org> | 2019-08-05 09:25:30 (GMT) |
commit | 7fbcc16dcd92a80eb30baab93388a0b8e294969b (patch) | |
tree | 33958836b22312caaa67cd3eb331c5676e2cf8a7 /Source/cmStringAlgorithms.cxx | |
parent | 3ebd3fa51d229e0067e8ca24f8fa4ed35ee5dac8 (diff) | |
download | CMake-7fbcc16dcd92a80eb30baab93388a0b8e294969b.zip CMake-7fbcc16dcd92a80eb30baab93388a0b8e294969b.tar.gz CMake-7fbcc16dcd92a80eb30baab93388a0b8e294969b.tar.bz2 |
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`)
Diffstat (limited to 'Source/cmStringAlgorithms.cxx')
-rw-r--r-- | Source/cmStringAlgorithms.cxx | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/Source/cmStringAlgorithms.cxx b/Source/cmStringAlgorithms.cxx index 5deb9b0..5867a44 100644 --- a/Source/cmStringAlgorithms.cxx +++ b/Source/cmStringAlgorithms.cxx @@ -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], |