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).
This commit is contained in:
Vadim Zeitlin 2022-06-23 21:45:46 +02:00
parent 96a917eaa8
commit cffbea5a0f
2 changed files with 16 additions and 1 deletions

View file

@ -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);

View file

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