1 //===--- Path.cpp -------------------------------------------*- C++-*------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "support/Path.h"
10 #include "llvm/Support/Path.h"
11 namespace clang {
12 namespace clangd {
13 
14 #ifdef CLANGD_PATH_CASE_INSENSITIVE
maybeCaseFoldPath(PathRef Path)15 std::string maybeCaseFoldPath(PathRef Path) { return Path.lower(); }
pathEqual(PathRef A,PathRef B)16 bool pathEqual(PathRef A, PathRef B) { return A.equals_lower(B); }
17 #else  // NOT CLANGD_PATH_CASE_INSENSITIVE
18 std::string maybeCaseFoldPath(PathRef Path) { return Path.str(); }
19 bool pathEqual(PathRef A, PathRef B) { return A == B; }
20 #endif // CLANGD_PATH_CASE_INSENSITIVE
21 
pathStartsWith(PathRef Ancestor,PathRef Path,llvm::sys::path::Style Style)22 bool pathStartsWith(PathRef Ancestor, PathRef Path,
23                     llvm::sys::path::Style Style) {
24   assert(llvm::sys::path::is_absolute(Ancestor) &&
25          llvm::sys::path::is_absolute(Path));
26   // If ancestor ends with a separator drop that, so that we can match /foo/ as
27   // a parent of /foo.
28   if (llvm::sys::path::is_separator(Ancestor.back(), Style))
29     Ancestor = Ancestor.drop_back();
30   // Ensure Path starts with Ancestor.
31   if (!pathEqual(Ancestor, Path.take_front(Ancestor.size())))
32     return false;
33   Path = Path.drop_front(Ancestor.size());
34   // Then make sure either two paths are equal or Path has a separator
35   // afterwards.
36   return Path.empty() || llvm::sys::path::is_separator(Path.front(), Style);
37 }
38 } // namespace clangd
39 } // namespace clang
40