1 //===--- StringView.h -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
9 // This file contains a limited version of LLVM's StringView class. It is
10 // copied here so that LLVMDemangle need not take a dependency on LLVMSupport.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_DEMANGLE_STRINGVIEW_H
14 #define LLVM_DEMANGLE_STRINGVIEW_H
25 static const size_t npos = ~size_t(0);
28 StringView(const char (&Str)[N]) : First(Str), Last(Str + N - 1) {}
29 StringView(const char *First_, const char *Last_)
30 : First(First_), Last(Last_) {}
31 StringView(const char *First_, size_t Len)
32 : First(First_), Last(First_ + Len) {}
33 StringView(const char *Str) : First(Str), Last(Str + std::strlen(Str)) {}
34 StringView() : First(nullptr), Last(nullptr) {}
36 StringView substr(size_t From) const {
37 return StringView(begin() + From, size() - From);
40 size_t find(char C, size_t From = 0) const {
41 size_t FindBegin = std::min(From, size());
42 // Avoid calling memchr with nullptr.
43 if (FindBegin < size()) {
44 // Just forward to memchr, which is faster than a hand-rolled loop.
45 if (const void *P = ::memchr(First + FindBegin, C, size() - FindBegin))
46 return static_cast<const char *>(P) - First;
51 StringView substr(size_t From, size_t To) const {
56 return StringView(First + From, First + To);
59 StringView dropFront(size_t N = 1) const {
62 return StringView(First + N, Last);
65 StringView dropBack(size_t N = 1) const {
68 return StringView(First, Last - N);
86 bool consumeFront(char C) {
93 bool consumeFront(StringView S) {
96 *this = dropFront(S.size());
100 bool startsWith(char C) const { return !empty() && *begin() == C; }
102 bool startsWith(StringView Str) const {
103 if (Str.size() > size())
105 return std::equal(Str.begin(), Str.end(), begin());
108 const char &operator[](size_t Idx) const { return *(begin() + Idx); }
110 const char *begin() const { return First; }
111 const char *end() const { return Last; }
112 size_t size() const { return static_cast<size_t>(Last - First); }
113 bool empty() const { return First == Last; }
116 inline bool operator==(const StringView &LHS, const StringView &RHS) {
117 return LHS.size() == RHS.size() &&
118 std::equal(LHS.begin(), LHS.end(), RHS.begin());