LLVM  9.0.0svn
StringView.h
Go to the documentation of this file.
1 //===--- StringView.h -------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // FIXME: Use std::string_view instead when we support C++17.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef DEMANGLE_STRINGVIEW_H
14 #define DEMANGLE_STRINGVIEW_H
15 
16 #include "DemangleConfig.h"
17 #include <algorithm>
18 #include <cassert>
19 #include <cstring>
20 
22 
23 class StringView {
24  const char *First;
25  const char *Last;
26 
27 public:
28  static const size_t npos = ~size_t(0);
29 
30  template <size_t N>
31  StringView(const char (&Str)[N]) : First(Str), Last(Str + N - 1) {}
32  StringView(const char *First_, const char *Last_)
33  : First(First_), Last(Last_) {}
34  StringView(const char *First_, size_t Len)
35  : First(First_), Last(First_ + Len) {}
36  StringView(const char *Str) : First(Str), Last(Str + std::strlen(Str)) {}
37  StringView() : First(nullptr), Last(nullptr) {}
38 
39  StringView substr(size_t From) const {
40  return StringView(begin() + From, size() - From);
41  }
42 
43  size_t find(char C, size_t From = 0) const {
44  size_t FindBegin = std::min(From, size());
45  // Avoid calling memchr with nullptr.
46  if (FindBegin < size()) {
47  // Just forward to memchr, which is faster than a hand-rolled loop.
48  if (const void *P = ::memchr(First + FindBegin, C, size() - FindBegin))
49  return size_t(static_cast<const char *>(P) - First);
50  }
51  return npos;
52  }
53 
54  StringView substr(size_t From, size_t To) const {
55  if (To >= size())
56  To = size() - 1;
57  if (From >= size())
58  From = size() - 1;
59  return StringView(First + From, First + To);
60  }
61 
62  StringView dropFront(size_t N = 1) const {
63  if (N >= size())
64  N = size();
65  return StringView(First + N, Last);
66  }
67 
68  StringView dropBack(size_t N = 1) const {
69  if (N >= size())
70  N = size();
71  return StringView(First, Last - N);
72  }
73 
74  char front() const {
75  assert(!empty());
76  return *begin();
77  }
78 
79  char back() const {
80  assert(!empty());
81  return *(end() - 1);
82  }
83 
84  char popFront() {
85  assert(!empty());
86  return *First++;
87  }
88 
89  bool consumeFront(char C) {
90  if (!startsWith(C))
91  return false;
92  *this = dropFront(1);
93  return true;
94  }
95 
97  if (!startsWith(S))
98  return false;
99  *this = dropFront(S.size());
100  return true;
101  }
102 
103  bool startsWith(char C) const { return !empty() && *begin() == C; }
104 
105  bool startsWith(StringView Str) const {
106  if (Str.size() > size())
107  return false;
108  return std::equal(Str.begin(), Str.end(), begin());
109  }
110 
111  const char &operator[](size_t Idx) const { return *(begin() + Idx); }
112 
113  const char *begin() const { return First; }
114  const char *end() const { return Last; }
115  size_t size() const { return static_cast<size_t>(Last - First); }
116  bool empty() const { return First == Last; }
117 };
118 
119 inline bool operator==(const StringView &LHS, const StringView &RHS) {
120  return LHS.size() == RHS.size() &&
121  std::equal(LHS.begin(), LHS.end(), RHS.begin());
122 }
123 
125 
126 #endif
uint64_t CallInst * C
bool startsWith(char C) const
Definition: StringView.h:103
StringView dropFront(size_t N=1) const
Definition: StringView.h:62
StringView(const char *Str)
Definition: StringView.h:36
bool startsWith(StringView Str) const
Definition: StringView.h:105
char popFront()
Definition: StringView.h:84
size_t find(char C, size_t From=0) const
Definition: StringView.h:43
#define DEMANGLE_NAMESPACE_END
Definition: BitVector.h:937
StringView dropBack(size_t N=1) const
Definition: StringView.h:68
StringView(const char *First_, const char *Last_)
Definition: StringView.h:32
bool consumeFront(StringView S)
Definition: StringView.h:96
char front() const
Definition: StringView.h:74
bool empty() const
Definition: StringView.h:116
const char * begin() const
Definition: StringView.h:113
StringView(const char(&Str)[N])
Definition: StringView.h:31
char back() const
Definition: StringView.h:79
#define P(N)
StringView substr(size_t From) const
Definition: StringView.h:39
#define DEMANGLE_NAMESPACE_BEGIN
const char * end() const
Definition: StringView.h:114
StringView(const char *First_, size_t Len)
Definition: StringView.h:34
BlockVerifier::State From
StringView substr(size_t From, size_t To) const
Definition: StringView.h:54
bool operator==(const StringView &LHS, const StringView &RHS)
Definition: StringView.h:119
#define N
const char & operator[](size_t Idx) const
Definition: StringView.h:111
size_t size() const
Definition: StringView.h:115
static const size_t npos
Definition: StringView.h:28
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool consumeFront(char C)
Definition: StringView.h:89