[NFC][Coroutines] Use structured binding with llvm::enumerate in CoroSplit (#116879)
[llvm-project.git] / lldb / unittests / TestingSupport / MockTildeExpressionResolver.cpp
blob08381b34e6769fcb03417618b2ff288f64b1f1af
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 //===----------------------------------------------------------------------===//
9 #include "MockTildeExpressionResolver.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/Support/Path.h"
13 using namespace lldb_private;
14 using namespace llvm;
16 MockTildeExpressionResolver::MockTildeExpressionResolver(StringRef CurrentUser,
17 StringRef HomeDir)
18 : CurrentUser(CurrentUser) {
19 UserDirectories.insert(std::make_pair(CurrentUser, HomeDir));
22 void MockTildeExpressionResolver::AddKnownUser(StringRef User,
23 StringRef HomeDir) {
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));
35 CurrentUser = User;
38 bool MockTildeExpressionResolver::ResolveExact(StringRef Expr,
39 SmallVectorImpl<char> &Output) {
40 Output.clear();
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(Dir.begin(), Dir.end());
49 return true;
52 for (const auto &User : UserDirectories) {
53 if (User.getKey() != Expr)
54 continue;
55 Output.append(User.getValue().begin(), User.getValue().end());
56 return true;
58 return false;
61 bool MockTildeExpressionResolver::ResolvePartial(StringRef Expr,
62 StringSet<> &Output) {
63 Output.clear();
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();
70 SmallString<16> QualifiedName("~");
71 for (const auto &User : UserDirectories) {
72 if (!User.getKey().starts_with(Expr))
73 continue;
74 QualifiedName.resize(1);
75 QualifiedName.append(User.getKey().begin(), User.getKey().end());
76 Output.insert(QualifiedName);
79 return !Output.empty();