1 //===-- CommandHistory.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 //===----------------------------------------------------------------------===//
12 #include "lldb/Interpreter/CommandHistory.h"
15 using namespace lldb_private
;
17 size_t CommandHistory::GetSize() const {
18 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
19 return m_history
.size();
22 bool CommandHistory::IsEmpty() const {
23 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
24 return m_history
.empty();
27 std::optional
<llvm::StringRef
>
28 CommandHistory::FindString(llvm::StringRef input_str
) const {
29 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
30 if (input_str
.size() < 2)
33 if (input_str
[0] != g_repeat_char
)
36 if (input_str
[1] == g_repeat_char
) {
37 if (m_history
.empty())
39 return llvm::StringRef(m_history
.back());
42 input_str
= input_str
.drop_front();
45 if (input_str
.front() == '-') {
46 if (input_str
.drop_front(1).getAsInteger(0, idx
))
48 if (idx
>= m_history
.size())
50 idx
= m_history
.size() - idx
;
52 if (input_str
.getAsInteger(0, idx
))
54 if (idx
>= m_history
.size())
58 return llvm::StringRef(m_history
[idx
]);
61 llvm::StringRef
CommandHistory::GetStringAtIndex(size_t idx
) const {
62 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
63 if (idx
< m_history
.size())
64 return m_history
[idx
];
68 llvm::StringRef
CommandHistory::operator[](size_t idx
) const {
69 return GetStringAtIndex(idx
);
72 llvm::StringRef
CommandHistory::GetRecentmostString() const {
73 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
74 if (m_history
.empty())
76 return m_history
.back();
79 void CommandHistory::AppendString(llvm::StringRef str
, bool reject_if_dupe
) {
80 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
82 if (!m_history
.empty()) {
83 if (str
== m_history
.back())
87 m_history
.push_back(std::string(str
));
90 void CommandHistory::Clear() {
91 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
95 void CommandHistory::Dump(Stream
&stream
, size_t start_idx
,
96 size_t stop_idx
) const {
97 std::lock_guard
<std::recursive_mutex
> guard(m_mutex
);
98 stop_idx
= std::min(stop_idx
+ 1, m_history
.size());
99 for (size_t counter
= start_idx
; counter
< stop_idx
; counter
++) {
100 const std::string hist_item
= m_history
[counter
];
101 if (!hist_item
.empty()) {
103 stream
.Printf("%4" PRIu64
": %s\n", (uint64_t)counter
, hist_item
.c_str());