[Lldb-commits] [lldb] r287401 - Convert CommandHistory functions to StringRef.

Zachary Turner via lldb-commits lldb-commits at lists.llvm.org
Fri Nov 18 15:22:42 PST 2016


Author: zturner
Date: Fri Nov 18 17:22:42 2016
New Revision: 287401

URL: http://llvm.org/viewvc/llvm-project?rev=287401&view=rev
Log:
Convert CommandHistory functions to StringRef.

Modified:
    lldb/trunk/include/lldb/Interpreter/CommandHistory.h
    lldb/trunk/source/Interpreter/CommandHistory.cpp
    lldb/trunk/source/Interpreter/CommandInterpreter.cpp

Modified: lldb/trunk/include/lldb/Interpreter/CommandHistory.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Interpreter/CommandHistory.h?rev=287401&r1=287400&r2=287401&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Interpreter/CommandHistory.h (original)
+++ lldb/trunk/include/lldb/Interpreter/CommandHistory.h Fri Nov 18 17:22:42 2016
@@ -33,15 +33,15 @@ public:
 
   bool IsEmpty() const;
 
-  const char *FindString(const char *input_str) const;
+  llvm::Optional<llvm::StringRef> 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();
 

Modified: lldb/trunk/source/Interpreter/CommandHistory.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandHistory.cpp?rev=287401&r1=287400&r2=287401&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandHistory.cpp (original)
+++ lldb/trunk/source/Interpreter/CommandHistory.cpp Fri Nov 18 17:22:42 2016
@@ -29,57 +29,62 @@ bool CommandHistory::IsEmpty() const {
   return m_history.empty();
 }
 
-const char *CommandHistory::FindString(const char *input_str) const {
+llvm::Optional<llvm::StringRef>
+CommandHistory::FindString(llvm::StringRef input_str) const {
   std::lock_guard<std::recursive_mutex> 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<std::recursive_mutex> 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<std::recursive_mutex> 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<std::recursive_mutex> guard(m_mutex);
   if (reject_if_dupe) {
     if (!m_history.empty()) {
@@ -87,7 +92,7 @@ void CommandHistory::AppendString(const
         return;
     }
   }
-  m_history.push_back(std::string(str));
+  m_history.push_back(str);
 }
 
 void CommandHistory::Clear() {

Modified: lldb/trunk/source/Interpreter/CommandInterpreter.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/CommandInterpreter.cpp?rev=287401&r1=287400&r2=287401&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/CommandInterpreter.cpp (original)
+++ lldb/trunk/source/Interpreter/CommandInterpreter.cpp Fri Nov 18 17:22:42 2016
@@ -1564,17 +1564,18 @@ bool CommandInterpreter::HandleCommand(c
     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;




More information about the lldb-commits mailing list