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 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 return m_history[idx]; 57 58 } else { 59 if (input_str.drop_front().getAsInteger(0, idx)) 60 return llvm::None; 61 if (idx > m_history.size()) 62 return llvm::None; 63 if (idx >= m_history.size()) 64 return llvm::None; 65 return m_history[idx]; 66 } 67 } 68 69 llvm::StringRef CommandHistory::GetStringAtIndex(size_t idx) const { 70 std::lock_guard<std::recursive_mutex> guard(m_mutex); 71 if (idx < m_history.size()) 72 return m_history[idx]; 73 return ""; 74 } 75 76 llvm::StringRef CommandHistory::operator[](size_t idx) const { 77 return GetStringAtIndex(idx); 78 } 79 80 llvm::StringRef CommandHistory::GetRecentmostString() const { 81 std::lock_guard<std::recursive_mutex> guard(m_mutex); 82 if (m_history.empty()) 83 return ""; 84 return m_history.back(); 85 } 86 87 void CommandHistory::AppendString(llvm::StringRef str, bool reject_if_dupe) { 88 std::lock_guard<std::recursive_mutex> guard(m_mutex); 89 if (reject_if_dupe) { 90 if (!m_history.empty()) { 91 if (str == m_history.back()) 92 return; 93 } 94 } 95 m_history.push_back(str); 96 } 97 98 void CommandHistory::Clear() { 99 std::lock_guard<std::recursive_mutex> guard(m_mutex); 100 m_history.clear(); 101 } 102 103 void CommandHistory::Dump(Stream &stream, size_t start_idx, 104 size_t stop_idx) const { 105 std::lock_guard<std::recursive_mutex> guard(m_mutex); 106 stop_idx = std::min(stop_idx + 1, m_history.size()); 107 for (size_t counter = start_idx; counter < stop_idx; counter++) { 108 const std::string hist_item = m_history[counter]; 109 if (!hist_item.empty()) { 110 stream.Indent(); 111 stream.Printf("%4" PRIu64 ": %s\n", (uint64_t)counter, hist_item.c_str()); 112 } 113 } 114 } 115