Path: blob/main/contrib/llvm-project/lldb/source/Interpreter/CommandHistory.cpp
39587 views
//===-- CommandHistory.cpp ------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include <cinttypes>9#include <optional>1011#include "lldb/Interpreter/CommandHistory.h"1213using namespace lldb;14using namespace lldb_private;1516size_t CommandHistory::GetSize() const {17std::lock_guard<std::recursive_mutex> guard(m_mutex);18return m_history.size();19}2021bool CommandHistory::IsEmpty() const {22std::lock_guard<std::recursive_mutex> guard(m_mutex);23return m_history.empty();24}2526std::optional<llvm::StringRef>27CommandHistory::FindString(llvm::StringRef input_str) const {28std::lock_guard<std::recursive_mutex> guard(m_mutex);29if (input_str.size() < 2)30return std::nullopt;3132if (input_str[0] != g_repeat_char)33return std::nullopt;3435if (input_str[1] == g_repeat_char) {36if (m_history.empty())37return std::nullopt;38return llvm::StringRef(m_history.back());39}4041input_str = input_str.drop_front();4243size_t idx = 0;44if (input_str.front() == '-') {45if (input_str.drop_front(1).getAsInteger(0, idx))46return std::nullopt;47if (idx >= m_history.size())48return std::nullopt;49idx = m_history.size() - idx;50} else {51if (input_str.getAsInteger(0, idx))52return std::nullopt;53if (idx >= m_history.size())54return std::nullopt;55}5657return llvm::StringRef(m_history[idx]);58}5960llvm::StringRef CommandHistory::GetStringAtIndex(size_t idx) const {61std::lock_guard<std::recursive_mutex> guard(m_mutex);62if (idx < m_history.size())63return m_history[idx];64return "";65}6667llvm::StringRef CommandHistory::operator[](size_t idx) const {68return GetStringAtIndex(idx);69}7071llvm::StringRef CommandHistory::GetRecentmostString() const {72std::lock_guard<std::recursive_mutex> guard(m_mutex);73if (m_history.empty())74return "";75return m_history.back();76}7778void CommandHistory::AppendString(llvm::StringRef str, bool reject_if_dupe) {79std::lock_guard<std::recursive_mutex> guard(m_mutex);80if (reject_if_dupe) {81if (!m_history.empty()) {82if (str == m_history.back())83return;84}85}86m_history.push_back(std::string(str));87}8889void CommandHistory::Clear() {90std::lock_guard<std::recursive_mutex> guard(m_mutex);91m_history.clear();92}9394void CommandHistory::Dump(Stream &stream, size_t start_idx,95size_t stop_idx) const {96std::lock_guard<std::recursive_mutex> guard(m_mutex);97stop_idx = std::min(stop_idx + 1, m_history.size());98for (size_t counter = start_idx; counter < stop_idx; counter++) {99const std::string hist_item = m_history[counter];100if (!hist_item.empty()) {101stream.Indent();102stream.Printf("%4" PRIu64 ": %s\n", (uint64_t)counter, hist_item.c_str());103}104}105}106107108