summaryrefslogtreecommitdiff
path: root/externals/demangle/llvm/Demangle/StringView.h
diff options
context:
space:
mode:
authorGravatar Alexandre Bouvier2023-01-23 01:51:12 +0100
committerGravatar Alexandre Bouvier2023-01-23 06:23:00 +0100
commit34b1ea9c1925d0da9377973d25e10e9b5ec40e94 (patch)
tree0ada6de7ae81a250258a4243d5bd4d2cc1283aef /externals/demangle/llvm/Demangle/StringView.h
parentMerge pull request #9555 from abouvier/catch2-update (diff)
downloadyuzu-34b1ea9c1925d0da9377973d25e10e9b5ec40e94.tar.gz
yuzu-34b1ea9c1925d0da9377973d25e10e9b5ec40e94.tar.xz
yuzu-34b1ea9c1925d0da9377973d25e10e9b5ec40e94.zip
cmake: prefer system llvm library
Diffstat (limited to 'externals/demangle/llvm/Demangle/StringView.h')
-rw-r--r--externals/demangle/llvm/Demangle/StringView.h127
1 files changed, 127 insertions, 0 deletions
diff --git a/externals/demangle/llvm/Demangle/StringView.h b/externals/demangle/llvm/Demangle/StringView.h
new file mode 100644
index 000000000..44d2b18a3
--- /dev/null
+++ b/externals/demangle/llvm/Demangle/StringView.h
@@ -0,0 +1,127 @@
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-FileCopyrightText: Part of the LLVM Project
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7//
8//===----------------------------------------------------------------------===//
9//
10// FIXME: Use std::string_view instead when we support C++17.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef DEMANGLE_STRINGVIEW_H
15#define DEMANGLE_STRINGVIEW_H
16
17#include "DemangleConfig.h"
18#include <algorithm>
19#include <cassert>
20#include <cstring>
21
22DEMANGLE_NAMESPACE_BEGIN
23
24class StringView {
25 const char *First;
26 const char *Last;
27
28public:
29 static const size_t npos = ~size_t(0);
30
31 template <size_t N>
32 StringView(const char (&Str)[N]) : First(Str), Last(Str + N - 1) {}
33 StringView(const char *First_, const char *Last_)
34 : First(First_), Last(Last_) {}
35 StringView(const char *First_, size_t Len)
36 : First(First_), Last(First_ + Len) {}
37 StringView(const char *Str) : First(Str), Last(Str + std::strlen(Str)) {}
38 StringView() : First(nullptr), Last(nullptr) {}
39
40 StringView substr(size_t From) const {
41 return StringView(begin() + From, size() - From);
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 substr(size_t From, size_t To) const {
56 if (To >= size())
57 To = size() - 1;
58 if (From >= size())
59 From = size() - 1;
60 return StringView(First + From, First + To);
61 }
62
63 StringView dropFront(size_t N = 1) const {
64 if (N >= size())
65 N = size();
66 return StringView(First + N, Last);
67 }
68
69 StringView dropBack(size_t N = 1) const {
70 if (N >= size())
71 N = size();
72 return StringView(First, Last - N);
73 }
74
75 char front() const {
76 assert(!empty());
77 return *begin();
78 }
79
80 char back() const {
81 assert(!empty());
82 return *(end() - 1);
83 }
84
85 char popFront() {
86 assert(!empty());
87 return *First++;
88 }
89
90 bool consumeFront(char C) {
91 if (!startsWith(C))
92 return false;
93 *this = dropFront(1);
94 return true;
95 }
96
97 bool consumeFront(StringView S) {
98 if (!startsWith(S))
99 return false;
100 *this = dropFront(S.size());
101 return true;
102 }
103
104 bool startsWith(char C) const { return !empty() && *begin() == C; }
105
106 bool startsWith(StringView Str) const {
107 if (Str.size() > size())
108 return false;
109 return std::equal(Str.begin(), Str.end(), begin());
110 }
111
112 const char &operator[](size_t Idx) const { return *(begin() + Idx); }
113
114 const char *begin() const { return First; }
115 const char *end() const { return Last; }
116 size_t size() const { return static_cast<size_t>(Last - First); }
117 bool empty() const { return First == Last; }
118};
119
120inline bool operator==(const StringView &LHS, const StringView &RHS) {
121 return LHS.size() == RHS.size() &&
122 std::equal(LHS.begin(), LHS.end(), RHS.begin());
123}
124
125DEMANGLE_NAMESPACE_END
126
127#endif