From e65a28f36fcd9e3391ccfe4e81d6a807c986fa08 Mon Sep 17 00:00:00 2001 From: Enrico Granata Date: Fri, 14 Nov 2014 22:58:11 +0000 Subject: [PATCH] Removed a couple of static helpers in the data formatters, replaced with new general logic in StringLexer llvm-svn: 222058 --- .../lldb/DataFormatters/FormattersContainer.h | 43 ++++++---------------- lldb/include/lldb/Utility/StringLexer.h | 12 +++++- lldb/source/Utility/StringLexer.cpp | 42 +++++++++++++++++++++ 3 files changed, 65 insertions(+), 32 deletions(-) diff --git a/lldb/include/lldb/DataFormatters/FormattersContainer.h b/lldb/include/lldb/DataFormatters/FormattersContainer.h index 19e6c7e..0f9904a 100644 --- a/lldb/include/lldb/DataFormatters/FormattersContainer.h +++ b/lldb/include/lldb/DataFormatters/FormattersContainer.h @@ -39,6 +39,8 @@ #include "lldb/Target/StackFrame.h" #include "lldb/Target/TargetList.h" +#include "lldb/Utility/StringLexer.h" + namespace lldb_private { // this file (and its. cpp) contain the low-level implementation of LLDB Data Visualization @@ -59,18 +61,6 @@ public: }; -static inline bool -IsWhitespace (char c) -{ - return ( (c == ' ') || (c == '\t') || (c == '\v') || (c == '\f') ); -} - -static inline bool -HasPrefix (const char* str1, const char* str2) -{ - return ( ::strstr(str1, str2) == str1 ); -} - // if the user tries to add formatters for, say, "struct Foo" // those will not match any type because of the way we strip qualifiers from typenames // this method looks for the case where the user is adding a "class","struct","enum" or "union" Foo @@ -78,32 +68,23 @@ HasPrefix (const char* str1, const char* str2) static inline ConstString GetValidTypeName_Impl (const ConstString& type) { - int strip_len = 0; - - if ((bool)type == false) + if (type.IsEmpty()) return type; - const char* type_cstr = type.AsCString(); - - if ( HasPrefix(type_cstr, "class ") ) - strip_len = 6; - else if ( HasPrefix(type_cstr, "enum ") ) - strip_len = 5; - else if ( HasPrefix(type_cstr, "struct ") ) - strip_len = 7; - else if ( HasPrefix(type_cstr, "union ") ) - strip_len = 6; + std::string type_cstr(type.AsCString()); + lldb_utility::StringLexer type_lexer(type_cstr); - if (strip_len == 0) - return type; + type_lexer.AdvanceIf("class "); + type_lexer.AdvanceIf("enum "); + type_lexer.AdvanceIf("struct "); + type_lexer.AdvanceIf("union "); - type_cstr += strip_len; - while (IsWhitespace(*type_cstr) && ++type_cstr) + while (type_lexer.NextIf({' ','\t','\v','\f'}).first) ; - return ConstString(type_cstr); + return ConstString(type_lexer.GetUnlexed()); } - + template class FormattersContainer; diff --git a/lldb/include/lldb/Utility/StringLexer.h b/lldb/include/lldb/Utility/StringLexer.h index 1fffba6..ae6b393 100644 --- a/lldb/include/lldb/Utility/StringLexer.h +++ b/lldb/include/lldb/Utility/StringLexer.h @@ -10,8 +10,9 @@ #ifndef utility_StringLexer_h_ #define utility_StringLexer_h_ -#include +#include #include +#include namespace lldb_utility { @@ -34,6 +35,12 @@ public: bool NextIf (Character c); + std::pair + NextIf (std::initializer_list cs); + + bool + AdvanceIf (const std::string& token); + Character Next (); @@ -43,6 +50,9 @@ public: bool HasAny (Character c); + std::string + GetUnlexed (); + // This will assert if there are less than s characters preceding the cursor. void PutBack (Size s); diff --git a/lldb/source/Utility/StringLexer.cpp b/lldb/source/Utility/StringLexer.cpp index 4da4099..2f62d2c 100644 --- a/lldb/source/Utility/StringLexer.cpp +++ b/lldb/source/Utility/StringLexer.cpp @@ -42,6 +42,42 @@ StringLexer::NextIf (Character c) return false; } +std::pair +StringLexer::NextIf (std::initializer_list cs) +{ + auto val = Peek(); + for (auto c : cs) + { + if (val == c) + { + Next(); + return {true,c}; + } + } + return {false,0}; +} + +bool +StringLexer::AdvanceIf (const std::string& token) +{ + auto pos = m_position; + bool matches = true; + for (auto c : token) + { + if (!NextIf(c)) + { + matches = false; + break; + } + } + if (!matches) + { + m_position = pos; + return false; + } + return true; +} + StringLexer::Character StringLexer::Next () { @@ -69,6 +105,12 @@ StringLexer::HasAny (Character c) return m_data.find(c, m_position) != std::string::npos; } +std::string +StringLexer::GetUnlexed () +{ + return std::string(m_data, m_position); +} + void StringLexer::Consume() { -- 2.7.4