• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- StringView.h -------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //
9 // This file is copied from llvm/lib/Demangle/StringView.h.
10 //===----------------------------------------------------------------------===//
11 
12 #ifndef LIBCXX_DEMANGLE_STRINGVIEW_H
13 #define LIBCXX_DEMANGLE_STRINGVIEW_H
14 
15 #include <algorithm>
16 #include <cassert>
17 #include <cstring>
18 
19 namespace {
20 class StringView {
21   const char *First;
22   const char *Last;
23 
24 public:
25   template <size_t N>
StringView(const char (& Str)[N])26   StringView(const char (&Str)[N]) : First(Str), Last(Str + N - 1) {}
StringView(const char * First_,const char * Last_)27   StringView(const char *First_, const char *Last_)
28       : First(First_), Last(Last_) {}
StringView(const char * First_,size_t Len)29   StringView(const char *First_, size_t Len)
30       : First(First_), Last(First_ + Len) {}
StringView(const char * Str)31   StringView(const char *Str) : First(Str), Last(Str + std::strlen(Str)) {}
StringView()32   StringView() : First(nullptr), Last(nullptr) {}
33 
substr(size_t From)34   StringView substr(size_t From) const {
35     return StringView(begin() + From, size() - From);
36   }
37 
substr(size_t From,size_t To)38   StringView substr(size_t From, size_t To) const {
39     if (To >= size())
40       To = size() - 1;
41     if (From >= size())
42       From = size() - 1;
43     return StringView(First + From, First + To);
44   }
45 
46   StringView dropFront(size_t N = 1) const {
47     if (N >= size())
48       N = size() - 1;
49     return StringView(First + N, Last);
50   }
51 
front()52   char front() const {
53     assert(!empty());
54     return *begin();
55   }
56 
popFront()57   char popFront() {
58     assert(!empty());
59     return *First++;
60   }
61 
consumeFront(char C)62   bool consumeFront(char C) {
63     if (!startsWith(C))
64       return false;
65     *this = dropFront(1);
66     return true;
67   }
68 
consumeFront(StringView S)69   bool consumeFront(StringView S) {
70     if (!startsWith(S))
71       return false;
72     *this = dropFront(S.size());
73     return true;
74   }
75 
startsWith(char C)76   bool startsWith(char C) const { return !empty() && *begin() == C; }
77 
startsWith(StringView Str)78   bool startsWith(StringView Str) const {
79     if (Str.size() > size())
80       return false;
81     return std::equal(Str.begin(), Str.end(), begin());
82   }
83 
84   const char &operator[](size_t Idx) const { return *(begin() + Idx); }
85 
begin()86   const char *begin() const { return First; }
end()87   const char *end() const { return Last; }
size()88   size_t size() const { return static_cast<size_t>(Last - First); }
empty()89   bool empty() const { return First == Last; }
90 };
91 
92 inline bool operator==(const StringView &LHS, const StringView &RHS) {
93   return LHS.size() == RHS.size() &&
94          std::equal(LHS.begin(), LHS.end(), RHS.begin());
95 }
96 } // namespace
97 
98 #endif
99