summaryrefslogtreecommitdiff
path: root/src/db
diff options
context:
space:
mode:
authorMax Kellermann <max@musicpd.org>2020-04-02 19:26:12 +0200
committerMax Kellermann <max@musicpd.org>2020-04-02 20:08:00 +0200
commitdadf054fbb197e78de33a0705d98514a5320e391 (patch)
tree794c447fdf7f365e05e6aefb8e1ed17de0784898 /src/db
parent6593b5998a23c46caacf9978cb353a9c878f7501 (diff)
db/simple/Directory: reimplement LookupDirectory() without allocations
Use std::string_view to avoid modifying the string for the temporary null terminators.
Diffstat (limited to 'src/db')
-rw-r--r--src/db/plugins/simple/Directory.cxx38
1 files changed, 13 insertions, 25 deletions
diff --git a/src/db/plugins/simple/Directory.cxx b/src/db/plugins/simple/Directory.cxx
index 17ee140b2..56eaa3877 100644
--- a/src/db/plugins/simple/Directory.cxx
+++ b/src/db/plugins/simple/Directory.cxx
@@ -30,9 +30,9 @@
#include "song/Filter.hxx"
#include "lib/icu/Collate.hxx"
#include "fs/Traits.hxx"
-#include "util/Alloc.hxx"
#include "util/DeleteDisposer.hxx"
#include "util/StringCompare.hxx"
+#include "util/StringView.hxx"
#include <cassert>
@@ -127,24 +127,24 @@ Directory::PruneEmpty() noexcept
}
Directory::LookupResult
-Directory::LookupDirectory(const char *uri) noexcept
+Directory::LookupDirectory(const char *_uri) noexcept
{
assert(holding_db_lock());
- assert(uri != nullptr);
+ assert(_uri != nullptr);
- if (isRootDirectory(uri))
+ if (isRootDirectory(_uri))
return { this, nullptr };
- char *duplicated = xstrdup(uri), *name = duplicated;
+ StringView uri(_uri);
Directory *d = this;
- while (true) {
- char *slash = strchr(name, '/');
- if (slash == name)
+ do {
+ auto s = uri.Split(PathTraitsUTF8::SEPARATOR);
+ if (s.first.empty())
break;
- if (slash != nullptr)
- *slash = '\0';
+ const auto name = s.first;
+ const auto rest = s.second;
Directory *tmp = d->FindChild(name);
if (tmp == nullptr)
@@ -153,22 +153,10 @@ Directory::LookupDirectory(const char *uri) noexcept
d = tmp;
- if (slash == nullptr) {
- /* found everything */
- name = nullptr;
- break;
- }
-
- name = slash + 1;
- }
-
- free(duplicated);
-
- const char *rest = name == nullptr
- ? nullptr
- : uri + (name - duplicated);
+ uri = rest;
+ } while (uri != nullptr);
- return { d, rest };
+ return { d, uri.data };
}
void