mirror of
https://github.com/Kitware/CMake.git
synced 2025-10-14 19:08:07 +08:00
StringAlgorithms: Refactor cmTokenize()
function
- Refactor and optimize the loop to make it shorter and faster - Make it push elements into an arbitrary (templated) output iterator - Make it a template on a separator type with the most used defaults - Add a backward compatible signature to return `std::vector<std::string>` - Add an alternative function `cmTokenizedView()` to return a vector of string views
This commit is contained in:
@@ -55,30 +55,6 @@ std::string cmEscapeQuotes(cm::string_view str)
|
||||
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