From f6f3c4c25b0aa5677a9760e64991ac28f6cf8e92 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 28 Nov 2022 17:01:45 +0100 Subject: [PATCH] fs/StandardDirectory: pass const string to ParseConfigLine() --- src/fs/StandardDirectory.cxx | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/fs/StandardDirectory.cxx b/src/fs/StandardDirectory.cxx index 07397cc77..08329380f 100644 --- a/src/fs/StandardDirectory.cxx +++ b/src/fs/StandardDirectory.cxx @@ -39,6 +39,7 @@ #endif #ifdef USE_XDG +#include "util/StringSplit.hxx" #include "util/StringStrip.hxx" #include "util/StringCompare.hxx" #include "fs/io/TextFile.hxx" @@ -136,7 +137,8 @@ static AllocatedPath GetStandardDir(int folder_id) static const char home_prefix[] = "$HOME/"; static bool -ParseConfigLine(char *line, const char *dir_name, AllocatedPath &result_dir) +ParseConfigLine(const char *line, const char *dir_name, + AllocatedPath &result_dir) { // strip leading white space line = StripLeft(line); @@ -171,34 +173,31 @@ ParseConfigLine(char *line, const char *dir_name, AllocatedPath &result_dir) home_relative = true; } - - char *line_end; // find end of the string + std::string_view path_view; if (quoted) { - line_end = std::strrchr(line, '"'); - if (line_end == nullptr) + const auto [pv, rest] = SplitLast(std::string_view{line}, '"'); + if (rest.data() == nullptr) return true; + + path_view = pv; } else { - line_end = StripRight(line, line + strlen(line)); + path_view = line; + path_view = StripRight(path_view); } // check for empty result - if (line == line_end) + if (path_view.empty()) return true; - *line_end = 0; - // build the result path - const auto path_fs = Path::FromFS(line); + auto result = AllocatedPath::FromFS(path_view); - AllocatedPath result = nullptr; if (home_relative) { auto home = GetHomeDir(); if (home.IsNull()) return true; - result = home / path_fs; - } else { - result = AllocatedPath(path_fs); + result = home / result; } if (IsValidDir(result.c_str())) {