1 //===-- MockTildeExpressionResolver.cpp -----------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "MockTildeExpressionResolver.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/Support/Path.h"
13 using namespace lldb_private
;
16 MockTildeExpressionResolver::MockTildeExpressionResolver(StringRef CurrentUser
,
18 : CurrentUser(CurrentUser
) {
19 UserDirectories
.insert(std::make_pair(CurrentUser
, HomeDir
));
22 void MockTildeExpressionResolver::AddKnownUser(StringRef User
,
24 assert(!UserDirectories
.contains(User
));
25 UserDirectories
.insert(std::make_pair(User
, HomeDir
));
28 void MockTildeExpressionResolver::Clear() {
29 CurrentUser
= StringRef();
30 UserDirectories
.clear();
33 void MockTildeExpressionResolver::SetCurrentUser(StringRef User
) {
34 assert(UserDirectories
.contains(User
));
38 bool MockTildeExpressionResolver::ResolveExact(StringRef Expr
,
39 SmallVectorImpl
<char> &Output
) {
43 Expr
, [](char c
) { return llvm::sys::path::is_separator(c
); }));
44 assert(Expr
.empty() || Expr
[0] == '~');
45 Expr
= Expr
.drop_front();
47 auto Dir
= UserDirectories
[CurrentUser
];
48 Output
.append(Dir
.begin(), Dir
.end());
52 for (const auto &User
: UserDirectories
) {
53 if (User
.getKey() != Expr
)
55 Output
.append(User
.getValue().begin(), User
.getValue().end());
61 bool MockTildeExpressionResolver::ResolvePartial(StringRef Expr
,
62 StringSet
<> &Output
) {
66 Expr
, [](char c
) { return llvm::sys::path::is_separator(c
); }));
67 assert(Expr
.empty() || Expr
[0] == '~');
68 Expr
= Expr
.drop_front();
70 SmallString
<16> QualifiedName("~");
71 for (const auto &User
: UserDirectories
) {
72 if (!User
.getKey().starts_with(Expr
))
74 QualifiedName
.resize(1);
75 QualifiedName
.append(User
.getKey().begin(), User
.getKey().end());
76 Output
.insert(QualifiedName
);
79 return !Output
.empty();