1 //===-- CommandHistory.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 <cinttypes>
10
11 #include "lldb/Interpreter/CommandHistory.h"
12
13 using namespace lldb;
14 using namespace lldb_private;
15
GetSize() const16 size_t CommandHistory::GetSize() const {
17 std::lock_guard<std::recursive_mutex> guard(m_mutex);
18 return m_history.size();
19 }
20
IsEmpty() const21 bool CommandHistory::IsEmpty() const {
22 std::lock_guard<std::recursive_mutex> guard(m_mutex);
23 return m_history.empty();
24 }
25
26 llvm::Optional<llvm::StringRef>
FindString(llvm::StringRef input_str) const27 CommandHistory::FindString(llvm::StringRef input_str) const {
28 std::lock_guard<std::recursive_mutex> guard(m_mutex);
29 if (input_str.size() < 2)
30 return llvm::None;
31
32 if (input_str[0] != g_repeat_char)
33 return llvm::None;
34
35 if (input_str[1] == g_repeat_char) {
36 if (m_history.empty())
37 return llvm::None;
38 return llvm::StringRef(m_history.back());
39 }
40
41 input_str = input_str.drop_front();
42
43 size_t idx = 0;
44 if (input_str.front() == '-') {
45 if (input_str.drop_front(1).getAsInteger(0, idx))
46 return llvm::None;
47 if (idx >= m_history.size())
48 return llvm::None;
49 idx = m_history.size() - idx;
50 } else {
51 if (input_str.getAsInteger(0, idx))
52 return llvm::None;
53 if (idx >= m_history.size())
54 return llvm::None;
55 }
56
57 return llvm::StringRef(m_history[idx]);
58 }
59
GetStringAtIndex(size_t idx) const60 llvm::StringRef CommandHistory::GetStringAtIndex(size_t idx) const {
61 std::lock_guard<std::recursive_mutex> guard(m_mutex);
62 if (idx < m_history.size())
63 return m_history[idx];
64 return "";
65 }
66
operator [](size_t idx) const67 llvm::StringRef CommandHistory::operator[](size_t idx) const {
68 return GetStringAtIndex(idx);
69 }
70
GetRecentmostString() const71 llvm::StringRef CommandHistory::GetRecentmostString() const {
72 std::lock_guard<std::recursive_mutex> guard(m_mutex);
73 if (m_history.empty())
74 return "";
75 return m_history.back();
76 }
77
AppendString(llvm::StringRef str,bool reject_if_dupe)78 void CommandHistory::AppendString(llvm::StringRef str, bool reject_if_dupe) {
79 std::lock_guard<std::recursive_mutex> guard(m_mutex);
80 if (reject_if_dupe) {
81 if (!m_history.empty()) {
82 if (str == m_history.back())
83 return;
84 }
85 }
86 m_history.push_back(std::string(str));
87 }
88
Clear()89 void CommandHistory::Clear() {
90 std::lock_guard<std::recursive_mutex> guard(m_mutex);
91 m_history.clear();
92 }
93
Dump(Stream & stream,size_t start_idx,size_t stop_idx) const94 void CommandHistory::Dump(Stream &stream, size_t start_idx,
95 size_t stop_idx) const {
96 std::lock_guard<std::recursive_mutex> guard(m_mutex);
97 stop_idx = std::min(stop_idx + 1, m_history.size());
98 for (size_t counter = start_idx; counter < stop_idx; counter++) {
99 const std::string hist_item = m_history[counter];
100 if (!hist_item.empty()) {
101 stream.Indent();
102 stream.Printf("%4" PRIu64 ": %s\n", (uint64_t)counter, hist_item.c_str());
103 }
104 }
105 }
106