1 //===-- CommandHistory.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 #include "lldb/Interpreter/CommandHistory.h"
15 using namespace lldb_private;
17 CommandHistory::CommandHistory() : m_mutex(), m_history() {}
19 CommandHistory::~CommandHistory() {}
21 size_t CommandHistory::GetSize() const {
22 std::lock_guard<std::recursive_mutex> guard(m_mutex);
23 return m_history.size();
26 bool CommandHistory::IsEmpty() const {
27 std::lock_guard<std::recursive_mutex> guard(m_mutex);
28 return m_history.empty();
31 llvm::Optional<llvm::StringRef>
32 CommandHistory::FindString(llvm::StringRef input_str) const {
33 std::lock_guard<std::recursive_mutex> guard(m_mutex);
34 if (input_str.size() < 2)
37 if (input_str[0] != g_repeat_char)
40 if (input_str[1] == g_repeat_char) {
41 if (m_history.empty())
43 return llvm::StringRef(m_history.back());
46 input_str = input_str.drop_front();
49 if (input_str.front() == '-') {
50 if (input_str.drop_front(1).getAsInteger(0, idx))
52 if (idx >= m_history.size())
54 idx = m_history.size() - idx;
56 if (input_str.getAsInteger(0, idx))
58 if (idx >= m_history.size())
62 return llvm::StringRef(m_history[idx]);
65 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];
72 llvm::StringRef CommandHistory::operator[](size_t idx) const {
73 return GetStringAtIndex(idx);
76 llvm::StringRef CommandHistory::GetRecentmostString() const {
77 std::lock_guard<std::recursive_mutex> guard(m_mutex);
78 if (m_history.empty())
80 return m_history.back();
83 void CommandHistory::AppendString(llvm::StringRef str, bool reject_if_dupe) {
84 std::lock_guard<std::recursive_mutex> guard(m_mutex);
86 if (!m_history.empty()) {
87 if (str == m_history.back())
91 m_history.push_back(str);
94 void CommandHistory::Clear() {
95 std::lock_guard<std::recursive_mutex> guard(m_mutex);
99 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()) {
107 stream.Printf("%4" PRIu64 ": %s\n", (uint64_t)counter, hist_item.c_str());