db/update/Walk: pass std::string_view to DirectoryMakeUriParentChecked()
Split the string into path segments with StringView::Split(). This prepares to eliminate all allocations from the method.
This commit is contained in:
parent
f13f66487a
commit
2c02a04566
@ -409,28 +409,29 @@ UpdateWalk::DirectoryMakeChildChecked(Directory &parent,
|
|||||||
|
|
||||||
inline Directory *
|
inline Directory *
|
||||||
UpdateWalk::DirectoryMakeUriParentChecked(Directory &root,
|
UpdateWalk::DirectoryMakeUriParentChecked(Directory &root,
|
||||||
const char *uri) noexcept
|
std::string_view _uri) noexcept
|
||||||
{
|
{
|
||||||
Directory *directory = &root;
|
Directory *directory = &root;
|
||||||
char *duplicated = xstrdup(uri);
|
StringView uri(_uri);
|
||||||
char *name_utf8 = duplicated, *slash;
|
|
||||||
|
|
||||||
while ((slash = strchr(name_utf8, '/')) != nullptr) {
|
while (true) {
|
||||||
*slash = 0;
|
auto s = uri.Split('/');
|
||||||
|
const std::string_view name = s.first;
|
||||||
if (StringIsEmpty(name_utf8))
|
const auto rest = s.second;
|
||||||
continue;
|
if (rest == nullptr)
|
||||||
|
|
||||||
directory = DirectoryMakeChildChecked(*directory,
|
|
||||||
duplicated,
|
|
||||||
name_utf8);
|
|
||||||
if (directory == nullptr)
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
name_utf8 = slash + 1;
|
if (!name.empty()) {
|
||||||
|
directory = DirectoryMakeChildChecked(*directory,
|
||||||
|
std::string(name).c_str(),
|
||||||
|
s.first);
|
||||||
|
if (directory == nullptr)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
uri = s.second;
|
||||||
}
|
}
|
||||||
|
|
||||||
free(duplicated);
|
|
||||||
return directory;
|
return directory;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,7 +166,7 @@ private:
|
|||||||
std::string_view name_utf8) noexcept;
|
std::string_view name_utf8) noexcept;
|
||||||
|
|
||||||
Directory *DirectoryMakeUriParentChecked(Directory &root,
|
Directory *DirectoryMakeUriParentChecked(Directory &root,
|
||||||
const char *uri) noexcept;
|
std::string_view uri) noexcept;
|
||||||
|
|
||||||
void UpdateUri(Directory &root, const char *uri) noexcept;
|
void UpdateUri(Directory &root, const char *uri) noexcept;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user