LLVM  13.0.0git
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 LLVM_DEMANGLE_STRINGVIEW_H
14 #define LLVM_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 Pos, size_t Len = npos) const {
40  assert(Pos <= size());
41  return StringView(begin() + Pos, std::min(Len, size() - Pos));
42  }
43 
44  size_t find(char C, size_t From = 0) const {
45  size_t FindBegin = std::min(From, size());
46  // Avoid calling memchr with nullptr.
47  if (FindBegin < size()) {
48  // Just forward to memchr, which is faster than a hand-rolled loop.
49  if (const void *P = ::memchr(First + FindBegin, C, size() - FindBegin))
50  return size_t(static_cast<const char *>(P) - First);
51  }
52  return npos;
53  }
54 
55  StringView dropFront(size_t N = 1) const {
56  if (N >= size())
57  N = size();
58  return StringView(First + N, Last);
59  }
60 
61  StringView dropBack(size_t N = 1) const {
62  if (N >= size())
63  N = size();
64  return StringView(First, Last - N);
65  }
66 
67  char front() const {
68  assert(!empty());
69  return *begin();
70  }
71 
72  char back() const {
73  assert(!empty());
74  return *(end() - 1);
75  }
76 
77  char popFront() {
78  assert(!empty());
79  return *First++;
80  }
81 
82  bool consumeFront(char C) {
83  if (!startsWith(C))
84  return false;
85  *this = dropFront(1);
86  return true;
87  }
88 
90  if (!startsWith(S))
91  return false;
92  *this = dropFront(S.size());
93  return true;
94  }
95 
96  bool startsWith(char C) const { return !empty() && *begin() == C; }
97 
98  bool startsWith(StringView Str) const {
99  if (Str.size() > size())
100  return false;
101  return std::equal(Str.begin(), Str.end(), begin());
102  }
103 
104  const char &operator[](size_t Idx) const { return *(begin() + Idx); }
105 
106  const char *begin() const { return First; }
107  const char *end() const { return Last; }
108  size_t size() const { return static_cast<size_t>(Last - First); }
109  bool empty() const { return First == Last; }
110 };
111 
112 inline bool operator==(const StringView &LHS, const StringView &RHS) {
113  return LHS.size() == RHS.size() &&
114  std::equal(LHS.begin(), LHS.end(), RHS.begin());
115 }
116 
118 
119 #endif
StringView::StringView
StringView()
Definition: StringView.h:37
StringView::StringView
StringView(const char *First_, size_t Len)
Definition: StringView.h:34
StringView::operator[]
const char & operator[](size_t Idx) const
Definition: StringView.h:104
StringView::size
size_t size() const
Definition: StringView.h:108
P
This currently compiles esp xmm0 movsd esp eax eax esp ret We should use not the dag combiner This is because dagcombine2 needs to be able to see through the X86ISD::Wrapper which DAGCombine can t really do The code for turning x load into a single vector load is target independent and should be moved to the dag combiner The code for turning x load into a vector load can only handle a direct load from a global or a direct load from the stack It should be generalized to handle any load from P
Definition: README-SSE.txt:411
StringView::StringView
StringView(const char(&Str)[N])
Definition: StringView.h:31
size_t
llvm::lltok::equal
@ equal
Definition: LLToken.h:25
StringView::substr
StringView substr(size_t Pos, size_t Len=npos) const
Definition: StringView.h:39
StringView::consumeFront
bool consumeFront(char C)
Definition: StringView.h:82
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
First
into llvm powi allowing the code generator to produce balanced multiplication trees First
Definition: README.txt:54
StringView::npos
static const size_t npos
Definition: StringView.h:28
StringView::popFront
char popFront()
Definition: StringView.h:77
StringView::end
const char * end() const
Definition: StringView.h:107
StringView::startsWith
bool startsWith(StringView Str) const
Definition: StringView.h:98
DEMANGLE_NAMESPACE_END
#define DEMANGLE_NAMESPACE_END
Definition: DemangleConfig.h:90
StringView::StringView
StringView(const char *Str)
Definition: StringView.h:36
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringView::find
size_t find(char C, size_t From=0) const
Definition: StringView.h:44
llvm::min
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:357
StringView::startsWith
bool startsWith(char C) const
Definition: StringView.h:96
StringView::front
char front() const
Definition: StringView.h:67
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
DemangleConfig.h
operator==
bool operator==(const StringView &LHS, const StringView &RHS)
Definition: StringView.h:112
std
Definition: BitVector.h:838
StringView::back
char back() const
Definition: StringView.h:72
DEMANGLE_NAMESPACE_BEGIN
#define DEMANGLE_NAMESPACE_BEGIN
Definition: DemangleConfig.h:89
StringView::dropBack
StringView dropBack(size_t N=1) const
Definition: StringView.h:61
StringView::begin
const char * begin() const
Definition: StringView.h:106
StringView::dropFront
StringView dropFront(size_t N=1) const
Definition: StringView.h:55
N
#define N
StringView::empty
bool empty() const
Definition: StringView.h:109
StringView
Definition: StringView.h:23
From
BlockVerifier::State From
Definition: BlockVerifier.cpp:55
StringView::consumeFront
bool consumeFront(StringView S)
Definition: StringView.h:89
StringView::StringView
StringView(const char *First_, const char *Last_)
Definition: StringView.h:32