Index: clang-tools-extra/clangd/PathMapping.cpp =================================================================== --- clang-tools-extra/clangd/PathMapping.cpp +++ clang-tools-extra/clangd/PathMapping.cpp @@ -128,21 +128,6 @@ llvm::inconvertibleErrorCode()); } -// Returns whether a path mapping should take place for OrigPath -// i.e. MappingPath is a proper sub-path of OrigPath -bool mappingMatches(llvm::StringRef OrigPath, llvm::StringRef MappingPath) { - namespace path = llvm::sys::path; - auto OrigPathItr = path::begin(OrigPath, path::Style::posix); - auto OrigPathEnd = path::end(OrigPath); - auto MappingPathItr = path::begin(MappingPath, path::Style::posix); - auto MappingPathEnd = path::end(MappingPath); - if (std::distance(OrigPathItr, OrigPathEnd) < - std::distance(MappingPathItr, MappingPathEnd)) { - return false; - } - return std::equal(MappingPathItr, MappingPathEnd, OrigPathItr); -} - // Converts a unix/windows path to the path portion of a file URI // e.g. "C:\foo" -> "/C:/foo" llvm::Expected parsePath(llvm::StringRef Path) { @@ -207,11 +192,11 @@ const std::string &To = Dir == PathMapping::Direction::ClientToServer ? Mapping.ServerPath : Mapping.ClientPath; - if (mappingMatches(Uri->body(), From)) { - std::string MappedBody = std::move(Uri->body()); - MappedBody.replace(MappedBody.find(From), From.length(), To); - auto MappedUri = URI(Uri->scheme(), Uri->authority(), MappedBody.c_str()); - return MappedUri.toString(); + llvm::StringRef Body = Uri->body(); + if (Body.consume_front(From) && (Body.empty() || Body.front() == '/')) { + std::string MappedBody = (To + Body).str(); + return URI(Uri->scheme(), Uri->authority(), MappedBody.c_str()) + .toString(); } } return llvm::None;