diff options
| author | Zachary Turner <zturner@google.com> | 2018-07-17 19:42:29 +0000 |
|---|---|---|
| committer | Zachary Turner <zturner@google.com> | 2018-07-17 19:42:29 +0000 |
| commit | 8a0efd09190e75c283dc929a8c739c76b5d0917e (patch) | |
| tree | 9b24026fede8e8207d0651fb280367a3feda4a5f /llvm/lib/Demangle/StringView.h | |
| parent | f989a92dea46f96548bdc83a394ff0a12c5ffa6b (diff) | |
| download | bcm5719-llvm-8a0efd09190e75c283dc929a8c739c76b5d0917e.tar.gz bcm5719-llvm-8a0efd09190e75c283dc929a8c739c76b5d0917e.zip | |
Add some helper functions to the demangle utility classes.
These are all methods that, while not currently used in the
Itanium demangler, are generally useful enough that it's
likely the itanium demangler could find a use for them. More
importantly, they are all necessary for the Microsoft demangler
which is up and coming in a subsequent patch. Rather than
combine these into a single monolithic patch, I think it makes
sense to commit this utility code first since it is very simple,
this way it won't detract from the substance of the MS demangler
patch.
llvm-svn: 337316
Diffstat (limited to 'llvm/lib/Demangle/StringView.h')
| -rw-r--r-- | llvm/lib/Demangle/StringView.h | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/llvm/lib/Demangle/StringView.h b/llvm/lib/Demangle/StringView.h index 6485e61630f..ca27b41d33b 100644 --- a/llvm/lib/Demangle/StringView.h +++ b/llvm/lib/Demangle/StringView.h @@ -14,6 +14,7 @@ #define LLVM_DEMANGLE_STRINGVIEW_H #include <algorithm> +#include <cassert> class StringView { const char *First; @@ -24,9 +25,16 @@ public: StringView(const char (&Str)[N]) : First(Str), Last(Str + N - 1) {} StringView(const char *First_, const char *Last_) : First(First_), Last(Last_) {} + StringView(const char *First_, size_t Len) + : First(First_), Last(First_ + Len) {} + StringView(const char *Str) : First(Str), Last(Str + strlen(Str)) {} StringView() : First(nullptr), Last(nullptr) {} - StringView substr(size_t From, size_t To) { + StringView substr(size_t From) const { + return StringView(begin() + From, size() - From); + } + + StringView substr(size_t From, size_t To) const { if (To >= size()) To = size() - 1; if (From >= size()) @@ -34,12 +42,38 @@ public: return StringView(First + From, First + To); } - StringView dropFront(size_t N) const { + StringView dropFront(size_t N = 1) const { if (N >= size()) N = size() - 1; return StringView(First + N, Last); } + char front() const { + assert(!empty()); + return *begin(); + } + + char popFront() { + assert(!empty()); + return *First++; + } + + bool consumeFront(char C) { + if (!startsWith(C)) + return false; + *this = dropFront(1); + return true; + } + + bool consumeFront(StringView S) { + if (!startsWith(S)) + return false; + *this = dropFront(S.size()); + return true; + } + + bool startsWith(char C) const { return !empty() && *begin() == C; } + bool startsWith(StringView Str) const { if (Str.size() > size()) return false; |

