From 53877afcb023523d1468240400400d376d1a85e6 Mon Sep 17 00:00:00 2001 From: Zachary Turner Date: Fri, 18 Nov 2016 23:22:42 +0000 Subject: [PATCH] Convert CommandHistory functions to StringRef. llvm-svn: 287401 --- lldb/include/lldb/Interpreter/CommandHistory.h | 10 ++-- lldb/source/Interpreter/CommandHistory.cpp | 67 ++++++++++++++------------ lldb/source/Interpreter/CommandInterpreter.cpp | 18 +++---- 3 files changed, 50 insertions(+), 45 deletions(-) diff --git a/lldb/include/lldb/Interpreter/CommandHistory.h b/lldb/include/lldb/Interpreter/CommandHistory.h index b3d1843c..f1a6c85 100644 --- a/lldb/include/lldb/Interpreter/CommandHistory.h +++ b/lldb/include/lldb/Interpreter/CommandHistory.h @@ -33,15 +33,15 @@ public: bool IsEmpty() const; - const char *FindString(const char *input_str) const; + llvm::Optional FindString(llvm::StringRef input_str) const; - const char *GetStringAtIndex(size_t idx) const; + llvm::StringRef GetStringAtIndex(size_t idx) const; - const char *operator[](size_t idx) const; + llvm::StringRef operator[](size_t idx) const; - const char *GetRecentmostString() const; + llvm::StringRef GetRecentmostString() const; - void AppendString(const std::string &str, bool reject_if_dupe = true); + void AppendString(llvm::StringRef str, bool reject_if_dupe = true); void Clear(); diff --git a/lldb/source/Interpreter/CommandHistory.cpp b/lldb/source/Interpreter/CommandHistory.cpp index 09ee620..fda7a1a 100644 --- a/lldb/source/Interpreter/CommandHistory.cpp +++ b/lldb/source/Interpreter/CommandHistory.cpp @@ -29,57 +29,62 @@ bool CommandHistory::IsEmpty() const { return m_history.empty(); } -const char *CommandHistory::FindString(const char *input_str) const { +llvm::Optional +CommandHistory::FindString(llvm::StringRef input_str) const { std::lock_guard guard(m_mutex); - if (!input_str) - return nullptr; + if (input_str.size() < 2) + return llvm::None; + if (input_str[0] != g_repeat_char) - return nullptr; - if (input_str[1] == '-') { - bool success; - size_t idx = StringConvert::ToUInt32(input_str + 2, 0, 0, &success); - if (!success) - return nullptr; + return llvm::None; + + if (input_str[1] == g_repeat_char) { + if (m_history.empty()) + return llvm::None; + return m_history.back(); + } + + input_str = input_str.drop_front(); + + size_t idx = 0; + if (input_str.front() == '-') { + if (input_str.drop_front(2).getAsInteger(0, idx)) + return llvm::None; if (idx > m_history.size()) - return nullptr; + return llvm::None; idx = m_history.size() - idx; - return m_history[idx].c_str(); + return m_history[idx]; - } else if (input_str[1] == g_repeat_char) { - if (m_history.empty()) - return nullptr; - else - return m_history.back().c_str(); } else { - bool success; - uint32_t idx = StringConvert::ToUInt32(input_str + 1, 0, 0, &success); - if (!success) - return nullptr; + if (input_str.drop_front().getAsInteger(0, idx)) + return llvm::None; + if (idx > m_history.size()) + return llvm::None; if (idx >= m_history.size()) - return nullptr; - return m_history[idx].c_str(); + return llvm::None; + return m_history[idx]; } } -const char *CommandHistory::GetStringAtIndex(size_t idx) const { +llvm::StringRef CommandHistory::GetStringAtIndex(size_t idx) const { std::lock_guard guard(m_mutex); if (idx < m_history.size()) - return m_history[idx].c_str(); - return nullptr; + return m_history[idx]; + return ""; } -const char *CommandHistory::operator[](size_t idx) const { +llvm::StringRef CommandHistory::operator[](size_t idx) const { return GetStringAtIndex(idx); } -const char *CommandHistory::GetRecentmostString() const { +llvm::StringRef CommandHistory::GetRecentmostString() const { std::lock_guard guard(m_mutex); if (m_history.empty()) - return nullptr; - return m_history.back().c_str(); + return ""; + return m_history.back(); } -void CommandHistory::AppendString(const std::string &str, bool reject_if_dupe) { +void CommandHistory::AppendString(llvm::StringRef str, bool reject_if_dupe) { std::lock_guard guard(m_mutex); if (reject_if_dupe) { if (!m_history.empty()) { @@ -87,7 +92,7 @@ void CommandHistory::AppendString(const std::string &str, bool reject_if_dupe) { return; } } - m_history.push_back(std::string(str)); + m_history.push_back(str); } void CommandHistory::Clear() { diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp index 320214d..251f591 100644 --- a/lldb/source/Interpreter/CommandInterpreter.cpp +++ b/lldb/source/Interpreter/CommandInterpreter.cpp @@ -1564,17 +1564,18 @@ bool CommandInterpreter::HandleCommand(const char *command_line, else if (command_string[non_space] == m_comment_char) comment_command = true; else if (command_string[non_space] == CommandHistory::g_repeat_char) { - const char *history_string = - m_command_history.FindString(command_string.c_str() + non_space); - if (history_string == nullptr) { + llvm::StringRef search_str(command_string); + search_str = search_str.drop_front(non_space); + if (auto hist_str = m_command_history.FindString(search_str)) { + add_to_history = false; + command_string = *hist_str; + original_command_string = *hist_str; + } else { result.AppendErrorWithFormat("Could not find entry: %s in history", command_string.c_str()); result.SetStatus(eReturnStatusFailed); return false; } - add_to_history = false; - command_string = history_string; - original_command_string = history_string; } } @@ -1794,10 +1795,9 @@ int CommandInterpreter::HandleCompletion( if (first_arg[0] == m_comment_char) return 0; else if (first_arg[0] == CommandHistory::g_repeat_char) { - const char *history_string = m_command_history.FindString(first_arg); - if (history_string != nullptr) { + if (auto hist_str = m_command_history.FindString(first_arg)) { matches.Clear(); - matches.InsertStringAtIndex(0, history_string); + matches.InsertStringAtIndex(0, *hist_str); return -2; } else return 0; -- 2.7.4