mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-27 02:01:42 +00:00
[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:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user