From dcd19c0592d3dc55fcb67fc6ec4a4a3d5b9ae6c6 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 26 Oct 2021 12:52:52 +0200 Subject: [PATCH] config/Path: use StringView::Split() --- src/config/Path.cxx | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/src/config/Path.cxx b/src/config/Path.cxx index b0dbff763..9639e884b 100644 --- a/src/config/Path.cxx +++ b/src/config/Path.cxx @@ -23,11 +23,10 @@ #include "fs/Traits.hxx" #include "fs/StandardDirectory.hxx" #include "util/RuntimeError.hxx" +#include "util/StringView.hxx" #include -#include - #ifndef _WIN32 #include @@ -96,30 +95,18 @@ ParsePath(const char *path) if (*path == '\0') return GetConfiguredHome(); - AllocatedPath home = nullptr; - if (*path == '/') { - home = GetConfiguredHome(); - ++path; + + return GetConfiguredHome() / + AllocatedPath::FromUTF8Throw(path); } else { - const char *slash = std::strchr(path, '/'); - const char *end = slash == nullptr - ? path + strlen(path) - : slash; - const std::string user(path, end); - home = GetHome(user.c_str()); + const auto [user, rest] = + StringView{path}.Split('/'); - if (slash == nullptr) - return home; - - path = slash + 1; + return GetHome(std::string{user}.c_str()) + / AllocatedPath::FromUTF8Throw(rest); } - - if (home.IsNull()) - return nullptr; - - return home / AllocatedPath::FromUTF8Throw(path); } else if (!PathTraitsUTF8::IsAbsolute(path)) { throw FormatRuntimeError("not an absolute path: %s", path); } else {