diff options
-rw-r--r-- | lldb/include/lldb/DataFormatters/FormattersContainer.h | 43 | ||||
-rw-r--r-- | lldb/include/lldb/Utility/StringLexer.h | 12 | ||||
-rw-r--r-- | 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 19e6c7ec1c0..0f9904aece8 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<typename KeyType, typename ValueType> class FormattersContainer; diff --git a/lldb/include/lldb/Utility/StringLexer.h b/lldb/include/lldb/Utility/StringLexer.h index 1fffba606f3..ae6b393b0eb 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 <string> +#include <initializer_list> #include <list> +#include <string> namespace lldb_utility { @@ -34,6 +35,12 @@ public: bool NextIf (Character c); + std::pair<bool, Character> + NextIf (std::initializer_list<Character> 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 4da40995e19..2f62d2cedb4 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<bool, StringLexer::Character> +StringLexer::NextIf (std::initializer_list<Character> 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() { |