[wpiutil] Change StringExtras split() to template (#7636)

It now calls back a function for each part rather than creating a SmallVector.
This commit is contained in:
Peter Johnson
2025-01-05 20:53:43 -08:00
committed by GitHub
parent 0f6693594c
commit 03d9e96877
17 changed files with 161 additions and 171 deletions

View File

@@ -54,12 +54,10 @@ std::string_view NetworkTable::NormalizeKey(std::string_view key,
buf.push_back(PATH_SEPARATOR_CHAR);
}
// for each path element, add it with a slash following
wpi::SmallVector<std::string_view, 16> parts;
wpi::split(key, parts, PATH_SEPARATOR_CHAR, -1, false);
for (auto i = parts.begin(); i != parts.end(); ++i) {
buf.append(i->begin(), i->end());
wpi::split(key, PATH_SEPARATOR_CHAR, -1, false, [&](auto part) {
buf.append(part.begin(), part.end());
buf.push_back(PATH_SEPARATOR_CHAR);
}
});
// remove trailing slash if the input key didn't have one
if (!key.empty() && key.back() != PATH_SEPARATOR_CHAR) {
buf.pop_back();
@@ -72,19 +70,17 @@ std::vector<std::string> NetworkTable::GetHierarchy(std::string_view key) {
hierarchy.emplace_back(1, PATH_SEPARATOR_CHAR);
// for each path element, add it to the end of what we built previously
wpi::SmallString<128> path;
wpi::SmallVector<std::string_view, 16> parts;
wpi::split(key, parts, PATH_SEPARATOR_CHAR, -1, false);
if (!parts.empty()) {
for (auto i = parts.begin(); i != parts.end(); ++i) {
path += PATH_SEPARATOR_CHAR;
path += *i;
hierarchy.emplace_back(path.str());
}
// handle trailing slash
if (key.back() == PATH_SEPARATOR_CHAR) {
path += PATH_SEPARATOR_CHAR;
hierarchy.emplace_back(path.str());
}
bool any = false;
wpi::split(key, PATH_SEPARATOR_CHAR, -1, false, [&](auto part) {
any = true;
path += PATH_SEPARATOR_CHAR;
path += part;
hierarchy.emplace_back(path.str());
});
// handle trailing slash
if (any && key.back() == PATH_SEPARATOR_CHAR) {
path += PATH_SEPARATOR_CHAR;
hierarchy.emplace_back(path.str());
}
return hierarchy;
}