From f64a87a374f033af5a8d46a0da01332390e1f423 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Sat, 20 Oct 2018 19:39:40 +0200 Subject: [PATCH] path: Make working_directory wcstring Kinda weird that that one was a wchar_t* --- src/highlight.cpp | 4 ++-- src/path.cpp | 13 +++++-------- src/path.h | 4 ++-- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/highlight.cpp b/src/highlight.cpp index 321ce9874..7f3691af0 100644 --- a/src/highlight.cpp +++ b/src/highlight.cpp @@ -351,7 +351,7 @@ bool autosuggest_validate_from_history(const history_item_t &item, string_prefixes_string(cd_dir, L"--help") || string_prefixes_string(cd_dir, L"-h"); if (!is_help) { wcstring path; - bool can_cd = path_get_cdpath(cd_dir, &path, working_directory.c_str(), vars); + bool can_cd = path_get_cdpath(cd_dir, &path, working_directory, vars); if (can_cd && !paths_are_same_file(working_directory, path)) { suggestionOK = true; } @@ -1021,7 +1021,7 @@ static bool command_is_valid(const wcstring &cmd, enum parse_statement_decoratio // Implicit cd if (!is_valid && implicit_cd_ok) { - is_valid = path_can_be_implicit_cd(cmd, NULL, working_directory.c_str(), vars); + is_valid = path_can_be_implicit_cd(cmd, NULL, working_directory, vars); } // Return what we got. diff --git a/src/path.cpp b/src/path.cpp index 0e099e05b..4ed4e0c20 100644 --- a/src/path.cpp +++ b/src/path.cpp @@ -157,15 +157,12 @@ wcstring_list_t path_get_paths(const wcstring &cmd) { return paths; } -bool path_get_cdpath(const wcstring &dir, wcstring *out, const wchar_t *wd, +bool path_get_cdpath(const wcstring &dir, wcstring *out, const wcstring &wd, const env_vars_snapshot_t &env_vars) { int err = ENOENT; if (dir.empty()) return false; - if (wd) { - size_t len = wcslen(wd); - assert(wd[len - 1] == L'/'); - } + assert(wd.empty() || wd.back() == L'/'); wcstring_list_t paths; if (dir.at(0) == L'/') { @@ -175,7 +172,7 @@ bool path_get_cdpath(const wcstring &dir, wcstring *out, const wchar_t *wd, dir == L"." || dir == L"..") { // Path is relative to the working directory. wcstring path; - if (wd) path.append(wd); + if (!wd.empty()) path.append(wd); path.append(dir); paths.push_back(path); } else { @@ -189,7 +186,7 @@ bool path_get_cdpath(const wcstring &dir, wcstring *out, const wchar_t *wd, } for (wcstring next_path : cdpathsv) { if (next_path.empty()) next_path = L"."; - if (next_path == L"." && wd != NULL) { + if (next_path == L"." && !wd.empty()) { // next_path is just '.', and we have a working directory, so use the wd instead. // TODO: if next_path starts with ./ we need to replace the . with the wd. next_path = wd; @@ -221,7 +218,7 @@ bool path_get_cdpath(const wcstring &dir, wcstring *out, const wchar_t *wd, return success; } -bool path_can_be_implicit_cd(const wcstring &path, wcstring *out_path, const wchar_t *wd, +bool path_can_be_implicit_cd(const wcstring &path, wcstring *out_path, const wcstring &wd, const env_vars_snapshot_t &vars) { wcstring exp_path = path; expand_tilde(exp_path); diff --git a/src/path.h b/src/path.h index 371d0ef86..2e8d6a542 100644 --- a/src/path.h +++ b/src/path.h @@ -61,14 +61,14 @@ wcstring_list_t path_get_paths(const wcstring &cmd); /// \param vars The environment variable snapshot to use (for the CDPATH variable) /// \return 0 if the command can not be found, the path of the command otherwise. The path should be /// free'd with free(). -bool path_get_cdpath(const wcstring &dir, wcstring *out_or_NULL, const wchar_t *wd = NULL, +bool path_get_cdpath(const wcstring &dir, wcstring *out_or_NULL, const wcstring &wd = L"", const env_vars_snapshot_t &vars = env_vars_snapshot_t::current()); /// Returns whether the path can be used for an implicit cd command; if so, also returns the path by /// reference (if desired). This requires it to start with one of the allowed prefixes (., .., ~) /// and resolve to a directory. bool path_can_be_implicit_cd(const wcstring &path, wcstring *out_path = NULL, - const wchar_t *wd = NULL, + const wcstring &wd = L"", const env_vars_snapshot_t &vars = env_vars_snapshot_t::current()); /// Remove double slashes and trailing slashes from a path, e.g. transform foo//bar/ into foo/bar.