From cffbea5a0ffbd25e1fd02d0cfcb3ffaadb8fcfe2 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Thu, 23 Jun 2022 21:45:46 +0200 Subject: [PATCH] Don't replace multiple HOME occurrences Fix another bug in wxFileName::ReplaceHomeDir(): only replace the first occurrence of the home directory, not all of them (or even the first one if it appears in the middle of the string). --- src/common/filename.cpp | 7 ++++++- tests/filename/filenametest.cpp | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/common/filename.cpp b/src/common/filename.cpp index b664a5ef5a..bb419a4df8 100644 --- a/src/common/filename.cpp +++ b/src/common/filename.cpp @@ -1616,7 +1616,12 @@ bool wxFileName::ReplaceHomeDir(wxPathFormat format) wxString stringForm = GetPath(wxPATH_GET_VOLUME, format); // do not touch the file name and the extension - stringForm.Replace(homedir, "~"); + wxString rest; + if ( stringForm.StartsWith(homedir, &rest) ) + { + stringForm = "~"; + stringForm += rest; + } // Now assign ourselves the modified path: Assign(stringForm, GetFullName(), format); diff --git a/tests/filename/filenametest.cpp b/tests/filename/filenametest.cpp index 4327cf2307..d38d11883e 100644 --- a/tests/filename/filenametest.cpp +++ b/tests/filename/filenametest.cpp @@ -488,6 +488,16 @@ TEST_CASE("wxFileName::Replace", "[filename]") ); CHECK( fn.GetFullPath(wxPATH_UNIX) == "~/test1/test2/test3/some file" ); + + // Check that home directory appearing in the middle of the path doesn't + // get replaced (this only works under Unix where there are no volumes). +#ifdef __UNIX__ + wxFileName fn2(homedir + "/subdir" + homedir + "/subsubdir", "filename"); + INFO("fn2=" << fn2.GetFullPath()); + + fn2.ReplaceHomeDir(); + CHECK( fn2.GetFullPath() == "~/subdir" + homedir + "/subsubdir/filename" ); +#endif // __UNIX__ } TEST_CASE("wxFileName::GetHumanReadable", "[filename]")