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/Host/StringConvert.h"
13 #include "lldb/Interpreter/CommandHistory.h"
16 using namespace lldb_private;
18 CommandHistory::CommandHistory() : m_mutex(), m_history() {}
20 CommandHistory::~CommandHistory() {}
22 size_t CommandHistory::GetSize() const {
23 std::lock_guard<std::recursive_mutex> guard(m_mutex);
24 return m_history.size();
27 bool CommandHistory::IsEmpty() const {
28 std::lock_guard<std::recursive_mutex> guard(m_mutex);
29 return m_history.empty();
32 llvm::Optional<llvm::StringRef>
33 CommandHistory::FindString(llvm::StringRef input_str) const {
34 std::lock_guard<std::recursive_mutex> guard(m_mutex);
35 if (input_str.size() < 2)
38 if (input_str[0] != g_repeat_char)
41 if (input_str[1] == g_repeat_char) {
42 if (m_history.empty())
44 return llvm::StringRef(m_history.back());
47 input_str = input_str.drop_front();
50 if (input_str.front() == '-') {
51 if (input_str.drop_front(2).getAsInteger(0, idx))
53 if (idx >= m_history.size())
55 idx = m_history.size() - idx;
57 if (input_str.drop_front().getAsInteger(0, idx))
59 if (idx >= m_history.size())
63 return llvm::StringRef(m_history[idx]);
66 llvm::StringRef CommandHistory::GetStringAtIndex(size_t idx) const {
67 std::lock_guard<std::recursive_mutex> guard(m_mutex);
68 if (idx < m_history.size())
69 return m_history[idx];
73 llvm::StringRef CommandHistory::operator[](size_t idx) const {
74 return GetStringAtIndex(idx);
77 llvm::StringRef CommandHistory::GetRecentmostString() const {
78 std::lock_guard<std::recursive_mutex> guard(m_mutex);
79 if (m_history.empty())
81 return m_history.back();
84 void CommandHistory::AppendString(llvm::StringRef str, bool reject_if_dupe) {
85 std::lock_guard<std::recursive_mutex> guard(m_mutex);
87 if (!m_history.empty()) {
88 if (str == m_history.back())
92 m_history.push_back(str);
95 void CommandHistory::Clear() {
96 std::lock_guard<std::recursive_mutex> guard(m_mutex);
100 void CommandHistory::Dump(Stream &stream, size_t start_idx,
101 size_t stop_idx) const {
102 std::lock_guard<std::recursive_mutex> guard(m_mutex);
103 stop_idx = std::min(stop_idx + 1, m_history.size());
104 for (size_t counter = start_idx; counter < stop_idx; counter++) {
105 const std::string hist_item = m_history[counter];
106 if (!hist_item.empty()) {
108 stream.Printf("%4" PRIu64 ": %s\n", (uint64_t)counter, hist_item.c_str());