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]")