diff --git a/llvm/include/llvm/Support/Path.h b/llvm/include/llvm/Support/Path.h index f0b2810cd7a9b..728b63c54c050 100644 --- a/llvm/include/llvm/Support/Path.h +++ b/llvm/include/llvm/Support/Path.h @@ -153,32 +153,23 @@ void replace_extension(SmallVectorImpl &path, const Twine &extension, /// @code /// /foo, /old, /new => /foo /// /old, /old, /new => /new -/// /old, /old/, /new, false => /old -/// /old, /old/, /new, true => /new +/// /old, /old/, /new => /old /// /old/foo, /old, /new => /new/foo /// /old/foo, /old/, /new => /new/foo /// /old/foo, /old/, /new/ => /new/foo /// /oldfoo, /old, /new => /oldfoo /// /foo, , /new => /new/foo /// /foo, , new => new/foo -/// /old/foo, /old, , false => /foo -/// /old/foo, /old, , true => foo +/// /old/foo, /old, => /foo /// @endcode /// /// @param Path If \a Path starts with \a OldPrefix modify to instead /// start with \a NewPrefix. -/// @param OldPrefix The path prefix to strip from \a Path. Any trailing -/// path separator is ignored if strict is true. +/// @param OldPrefix The path prefix to strip from \a Path. /// @param NewPrefix The path prefix to replace \a NewPrefix with. -/// @param style The path separator style -/// @param strict If strict is true, a directory separator following -/// \a OldPrefix will also be stripped. Otherwise, directory -/// separators will only be matched and stripped when present -/// in \a OldPrefix. /// @result true if \a Path begins with OldPrefix -bool replace_path_prefix(SmallVectorImpl &Path, - const StringRef &OldPrefix, const StringRef &NewPrefix, - Style style = Style::native, bool strict = false); +bool replace_path_prefix(SmallVectorImpl &Path, StringRef OldPrefix, + StringRef NewPrefix); /// Append to path. /// diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp index fa3bf47569e83..6f065b9c5a3c8 100644 --- a/llvm/lib/Support/Path.cpp +++ b/llvm/lib/Support/Path.cpp @@ -496,49 +496,25 @@ void replace_extension(SmallVectorImpl &path, const Twine &extension, path.append(ext.begin(), ext.end()); } -bool replace_path_prefix(SmallVectorImpl &Path, - const StringRef &OldPrefix, const StringRef &NewPrefix, - Style style, bool strict) { +bool replace_path_prefix(SmallVectorImpl &Path, StringRef OldPrefix, + StringRef NewPrefix) { if (OldPrefix.empty() && NewPrefix.empty()) return false; StringRef OrigPath(Path.begin(), Path.size()); - StringRef OldPrefixDir; - - if (!strict && OldPrefix.size() > OrigPath.size()) - return false; - - // Ensure OldPrefixDir does not have a trailing separator. - if (!OldPrefix.empty() && is_separator(OldPrefix.back())) - OldPrefixDir = parent_path(OldPrefix, style); - else - OldPrefixDir = OldPrefix; - - if (!OrigPath.startswith(OldPrefixDir)) + if (!OrigPath.startswith(OldPrefix)) return false; - if (OrigPath.size() > OldPrefixDir.size()) - if (!is_separator(OrigPath[OldPrefixDir.size()], style) && strict) - return false; - // If prefixes have the same size we can simply copy the new one over. - if (OldPrefixDir.size() == NewPrefix.size() && !strict) { + if (OldPrefix.size() == NewPrefix.size()) { llvm::copy(NewPrefix, Path.begin()); return true; } - StringRef RelPath = OrigPath.substr(OldPrefixDir.size()); + StringRef RelPath = OrigPath.substr(OldPrefix.size()); SmallString<256> NewPath; - path::append(NewPath, style, NewPrefix); - if (!RelPath.empty()) { - if (!is_separator(RelPath[0], style) || !strict) - path::append(NewPath, style, RelPath); - else - path::append(NewPath, style, relative_path(RelPath, style)); - } - + (Twine(NewPrefix) + RelPath).toVector(NewPath); Path.swap(NewPath); - return true; } diff --git a/llvm/unittests/Support/Path.cpp b/llvm/unittests/Support/Path.cpp index 671966b52dd0c..1a2ac1818eb04 100644 --- a/llvm/unittests/Support/Path.cpp +++ b/llvm/unittests/Support/Path.cpp @@ -1254,21 +1254,14 @@ TEST(Support, ReplacePathPrefix) { path::replace_path_prefix(Path, OldPrefix, EmptyPrefix); EXPECT_EQ(Path, "/foo"); Path = Path2; - path::replace_path_prefix(Path, OldPrefix, EmptyPrefix, path::Style::native, - true); + path::replace_path_prefix(Path, OldPrefixSep, EmptyPrefix); EXPECT_EQ(Path, "foo"); Path = Path3; - path::replace_path_prefix(Path, OldPrefix, NewPrefix, path::Style::native, - false); + path::replace_path_prefix(Path, OldPrefix, NewPrefix); EXPECT_EQ(Path, "/newnew/foo"); Path = Path3; - path::replace_path_prefix(Path, OldPrefix, NewPrefix, path::Style::native, - true); - EXPECT_EQ(Path, "/oldnew/foo"); - Path = Path3; - path::replace_path_prefix(Path, OldPrefixSep, NewPrefix, path::Style::native, - true); - EXPECT_EQ(Path, "/oldnew/foo"); + path::replace_path_prefix(Path, OldPrefix, NewPrefix2); + EXPECT_EQ(Path, "/longernewnew/foo"); Path = Path1; path::replace_path_prefix(Path, EmptyPrefix, NewPrefix); EXPECT_EQ(Path, "/new/foo"); @@ -1279,13 +1272,8 @@ TEST(Support, ReplacePathPrefix) { path::replace_path_prefix(Path, OldPrefix, NewPrefix); EXPECT_EQ(Path, "/new/"); Path = OldPrefix; - path::replace_path_prefix(Path, OldPrefixSep, NewPrefix, path::Style::native, - false); + path::replace_path_prefix(Path, OldPrefixSep, NewPrefix); EXPECT_EQ(Path, "/old"); - Path = OldPrefix; - path::replace_path_prefix(Path, OldPrefixSep, NewPrefix, path::Style::native, - true); - EXPECT_EQ(Path, "/new"); } TEST_F(FileSystemTest, OpenFileForRead) {