[Lldb-commits] [lldb] r222058 - Removed a couple of static helpers in the data formatters, replaced with new general logic in StringLexer
Enrico Granata
egranata at apple.com
Fri Nov 14 14:58:11 PST 2014
Author: enrico
Date: Fri Nov 14 16:58:11 2014
New Revision: 222058
URL: http://llvm.org/viewvc/llvm-project?rev=222058&view=rev
Log:
Removed a couple of static helpers in the data formatters, replaced with new general logic in StringLexer
Modified:
lldb/trunk/include/lldb/DataFormatters/FormattersContainer.h
lldb/trunk/include/lldb/Utility/StringLexer.h
lldb/trunk/source/Utility/StringLexer.cpp
Modified: lldb/trunk/include/lldb/DataFormatters/FormattersContainer.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/DataFormatters/FormattersContainer.h?rev=222058&r1=222057&r2=222058&view=diff
==============================================================================
--- lldb/trunk/include/lldb/DataFormatters/FormattersContainer.h (original)
+++ lldb/trunk/include/lldb/DataFormatters/FormattersContainer.h Fri Nov 14 16:58:11 2014
@@ -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*
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;
Modified: lldb/trunk/include/lldb/Utility/StringLexer.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/StringLexer.h?rev=222058&r1=222057&r2=222058&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/StringLexer.h (original)
+++ lldb/trunk/include/lldb/Utility/StringLexer.h Fri Nov 14 16:58:11 2014
@@ -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);
Modified: lldb/trunk/source/Utility/StringLexer.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/StringLexer.cpp?rev=222058&r1=222057&r2=222058&view=diff
==============================================================================
--- lldb/trunk/source/Utility/StringLexer.cpp (original)
+++ lldb/trunk/source/Utility/StringLexer.cpp Fri Nov 14 16:58:11 2014
@@ -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()
{
More information about the lldb-commits
mailing list