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/Host/StringConvert.h" 13 #include "lldb/Interpreter/CommandHistory.h" 14 15 using namespace lldb; 16 using namespace lldb_private; 17 18 CommandHistory::CommandHistory() : m_mutex(), m_history() {} 19 20 CommandHistory::~CommandHistory() {} 21 22 size_t CommandHistory::GetSize() const { 23 std::lock_guard<std::recursive_mutex> guard(m_mutex); 24 return m_history.size(); 25 } 26 27 bool CommandHistory::IsEmpty() const { 28 std::lock_guard<std::recursive_mutex> guard(m_mutex); 29 return m_history.empty(); 30 } 31 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) 36 return llvm::None; 37 38 if (input_str[0] != g_repeat_char) 39 return llvm::None; 40 41 if (input_str[1] == g_repeat_char) { 42 if (m_history.empty()) 43 return llvm::None; 44 return llvm::StringRef(m_history.back()); 45 } 46 47 input_str = input_str.drop_front(); 48 49 size_t idx = 0; 50 if (input_str.front() == '-') { 51 if (input_str.drop_front(2).getAsInteger(0, idx)) 52 return llvm::None; 53 if (idx >= m_history.size()) 54 return llvm::None; 55 idx = m_history.size() - idx; 56 } else { 57 if (input_str.drop_front().getAsInteger(0, idx)) 58 return llvm::None; 59 if (idx >= m_history.size()) 60 return llvm::None; 61 } 62 63 return llvm::StringRef(m_history[idx]); 64 } 65 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]; 70 return ""; 71 } 72 73 llvm::StringRef CommandHistory::operator[](size_t idx) const { 74 return GetStringAtIndex(idx); 75 } 76 77 llvm::StringRef CommandHistory::GetRecentmostString() const { 78 std::lock_guard<std::recursive_mutex> guard(m_mutex); 79 if (m_history.empty()) 80 return ""; 81 return m_history.back(); 82 } 83 84 void CommandHistory::AppendString(llvm::StringRef str, bool reject_if_dupe) { 85 std::lock_guard<std::recursive_mutex> guard(m_mutex); 86 if (reject_if_dupe) { 87 if (!m_history.empty()) { 88 if (str == m_history.back()) 89 return; 90 } 91 } 92 m_history.push_back(str); 93 } 94 95 void CommandHistory::Clear() { 96 std::lock_guard<std::recursive_mutex> guard(m_mutex); 97 m_history.clear(); 98 } 99 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()) { 107 stream.Indent(); 108 stream.Printf("%4" PRIu64 ": %s\n", (uint64_t)counter, hist_item.c_str()); 109 } 110 } 111 } 112