1 //===-- CommandHistory.cpp --------------------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include <inttypes.h>
11
12 #include "lldb/Interpreter/CommandHistory.h"
13
14 using namespace lldb;
15 using namespace lldb_private;
16
CommandHistory()17 CommandHistory::CommandHistory() : m_mutex(), m_history() {}
18
~CommandHistory()19 CommandHistory::~CommandHistory() {}
20
GetSize() const21 size_t CommandHistory::GetSize() const {
22 std::lock_guard<std::recursive_mutex> guard(m_mutex);
23 return m_history.size();
24 }
25
IsEmpty() const26 bool CommandHistory::IsEmpty() const {
27 std::lock_guard<std::recursive_mutex> guard(m_mutex);
28 return m_history.empty();
29 }
30
31 llvm::Optional<llvm::StringRef>
FindString(llvm::StringRef input_str) const32 CommandHistory::FindString(llvm::StringRef input_str) const {
33 std::lock_guard<std::recursive_mutex> guard(m_mutex);
34 if (input_str.size() < 2)
35 return llvm::None;
36
37 if (input_str[0] != g_repeat_char)
38 return llvm::None;
39
40 if (input_str[1] == g_repeat_char) {
41 if (m_history.empty())
42 return llvm::None;
43 return llvm::StringRef(m_history.back());
44 }
45
46 input_str = input_str.drop_front();
47
48 size_t idx = 0;
49 if (input_str.front() == '-') {
50 if (input_str.drop_front(1).getAsInteger(0, idx))
51 return llvm::None;
52 if (idx >= m_history.size())
53 return llvm::None;
54 idx = m_history.size() - idx;
55 } else {
56 if (input_str.getAsInteger(0, idx))
57 return llvm::None;
58 if (idx >= m_history.size())
59 return llvm::None;
60 }
61
62 return llvm::StringRef(m_history[idx]);
63 }
64
GetStringAtIndex(size_t idx) const65 llvm::StringRef CommandHistory::GetStringAtIndex(size_t idx) const {
66 std::lock_guard<std::recursive_mutex> guard(m_mutex);
67 if (idx < m_history.size())
68 return m_history[idx];
69 return "";
70 }
71
operator [](size_t idx) const72 llvm::StringRef CommandHistory::operator[](size_t idx) const {
73 return GetStringAtIndex(idx);
74 }
75
GetRecentmostString() const76 llvm::StringRef CommandHistory::GetRecentmostString() const {
77 std::lock_guard<std::recursive_mutex> guard(m_mutex);
78 if (m_history.empty())
79 return "";
80 return m_history.back();
81 }
82
AppendString(llvm::StringRef str,bool reject_if_dupe)83 void CommandHistory::AppendString(llvm::StringRef str, bool reject_if_dupe) {
84 std::lock_guard<std::recursive_mutex> guard(m_mutex);
85 if (reject_if_dupe) {
86 if (!m_history.empty()) {
87 if (str == m_history.back())
88 return;
89 }
90 }
91 m_history.push_back(str);
92 }
93
Clear()94 void CommandHistory::Clear() {
95 std::lock_guard<std::recursive_mutex> guard(m_mutex);
96 m_history.clear();
97 }
98
Dump(Stream & stream,size_t start_idx,size_t stop_idx) const99 void CommandHistory::Dump(Stream &stream, size_t start_idx,
100 size_t stop_idx) const {
101 std::lock_guard<std::recursive_mutex> guard(m_mutex);
102 stop_idx = std::min(stop_idx + 1, m_history.size());
103 for (size_t counter = start_idx; counter < stop_idx; counter++) {
104 const std::string hist_item = m_history[counter];
105 if (!hist_item.empty()) {
106 stream.Indent();
107 stream.Printf("%4" PRIu64 ": %s\n", (uint64_t)counter, hist_item.c_str());
108 }
109 }
110 }
111