1 | //===-- MockTildeExpressionResolver.cpp -----------------------------------===// |
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 "MockTildeExpressionResolver.h" |
10 | #include "llvm/ADT/STLExtras.h" |
11 | #include "llvm/Support/Path.h" |
12 | |
13 | using namespace lldb_private; |
14 | using namespace llvm; |
15 | |
16 | MockTildeExpressionResolver::MockTildeExpressionResolver(StringRef CurrentUser, |
17 | StringRef HomeDir) |
18 | : CurrentUser(CurrentUser) { |
19 | UserDirectories.insert(KV: std::make_pair(x&: CurrentUser, y&: HomeDir)); |
20 | } |
21 | |
22 | void MockTildeExpressionResolver::AddKnownUser(StringRef User, |
23 | StringRef HomeDir) { |
24 | assert(!UserDirectories.contains(User)); |
25 | UserDirectories.insert(KV: std::make_pair(x&: User, y&: HomeDir)); |
26 | } |
27 | |
28 | void MockTildeExpressionResolver::Clear() { |
29 | CurrentUser = StringRef(); |
30 | UserDirectories.clear(); |
31 | } |
32 | |
33 | void MockTildeExpressionResolver::SetCurrentUser(StringRef User) { |
34 | assert(UserDirectories.contains(User)); |
35 | CurrentUser = User; |
36 | } |
37 | |
38 | bool MockTildeExpressionResolver::ResolveExact(StringRef Expr, |
39 | SmallVectorImpl<char> &Output) { |
40 | Output.clear(); |
41 | |
42 | assert(!llvm::any_of( |
43 | Expr, [](char c) { return llvm::sys::path::is_separator(c); })); |
44 | assert(Expr.empty() || Expr[0] == '~'); |
45 | Expr = Expr.drop_front(); |
46 | if (Expr.empty()) { |
47 | auto Dir = UserDirectories[CurrentUser]; |
48 | Output.append(in_start: Dir.begin(), in_end: Dir.end()); |
49 | return true; |
50 | } |
51 | |
52 | for (const auto &User : UserDirectories) { |
53 | if (User.getKey() != Expr) |
54 | continue; |
55 | Output.append(in_start: User.getValue().begin(), in_end: User.getValue().end()); |
56 | return true; |
57 | } |
58 | return false; |
59 | } |
60 | |
61 | bool MockTildeExpressionResolver::ResolvePartial(StringRef Expr, |
62 | StringSet<> &Output) { |
63 | Output.clear(); |
64 | |
65 | assert(!llvm::any_of( |
66 | Expr, [](char c) { return llvm::sys::path::is_separator(c); })); |
67 | assert(Expr.empty() || Expr[0] == '~'); |
68 | Expr = Expr.drop_front(); |
69 | |
70 | SmallString<16> QualifiedName("~" ); |
71 | for (const auto &User : UserDirectories) { |
72 | if (!User.getKey().starts_with(Prefix: Expr)) |
73 | continue; |
74 | QualifiedName.resize(N: 1); |
75 | QualifiedName.append(in_start: User.getKey().begin(), in_end: User.getKey().end()); |
76 | Output.insert(key: QualifiedName); |
77 | } |
78 | |
79 | return !Output.empty(); |
80 | } |
81 | |