2018-07-17 05:24:03 +08:00
|
|
|
//===--- StringView.h -------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// This file contains a limited version of LLVM's StringView class. It is
|
|
|
|
// copied here so that LLVMDemangle need not take a dependency on LLVMSupport.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_DEMANGLE_STRINGVIEW_H
|
|
|
|
#define LLVM_DEMANGLE_STRINGVIEW_H
|
|
|
|
|
|
|
|
#include <algorithm>
|
2018-07-18 03:42:29 +08:00
|
|
|
#include <cassert>
|
2018-07-18 03:48:46 +08:00
|
|
|
#include <cstring>
|
2018-07-17 05:24:03 +08:00
|
|
|
|
|
|
|
class StringView {
|
|
|
|
const char *First;
|
|
|
|
const char *Last;
|
|
|
|
|
|
|
|
public:
|
|
|
|
template <size_t N>
|
|
|
|
StringView(const char (&Str)[N]) : First(Str), Last(Str + N - 1) {}
|
|
|
|
StringView(const char *First_, const char *Last_)
|
|
|
|
: First(First_), Last(Last_) {}
|
2018-07-18 03:42:29 +08:00
|
|
|
StringView(const char *First_, size_t Len)
|
|
|
|
: First(First_), Last(First_ + Len) {}
|
2018-07-18 03:48:46 +08:00
|
|
|
StringView(const char *Str) : First(Str), Last(Str + std::strlen(Str)) {}
|
2018-07-17 05:24:03 +08:00
|
|
|
StringView() : First(nullptr), Last(nullptr) {}
|
|
|
|
|
2018-07-18 03:42:29 +08:00
|
|
|
StringView substr(size_t From) const {
|
|
|
|
return StringView(begin() + From, size() - From);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringView substr(size_t From, size_t To) const {
|
2018-07-17 05:24:03 +08:00
|
|
|
if (To >= size())
|
|
|
|
To = size() - 1;
|
|
|
|
if (From >= size())
|
|
|
|
From = size() - 1;
|
|
|
|
return StringView(First + From, First + To);
|
|
|
|
}
|
|
|
|
|
2018-07-18 03:42:29 +08:00
|
|
|
StringView dropFront(size_t N = 1) const {
|
2018-07-17 05:24:03 +08:00
|
|
|
if (N >= size())
|
|
|
|
N = size() - 1;
|
|
|
|
return StringView(First + N, Last);
|
|
|
|
}
|
|
|
|
|
2018-07-18 03:42:29 +08:00
|
|
|
char front() const {
|
|
|
|
assert(!empty());
|
|
|
|
return *begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
char popFront() {
|
|
|
|
assert(!empty());
|
|
|
|
return *First++;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool consumeFront(char C) {
|
|
|
|
if (!startsWith(C))
|
|
|
|
return false;
|
|
|
|
*this = dropFront(1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool consumeFront(StringView S) {
|
|
|
|
if (!startsWith(S))
|
|
|
|
return false;
|
|
|
|
*this = dropFront(S.size());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool startsWith(char C) const { return !empty() && *begin() == C; }
|
|
|
|
|
2018-07-17 05:24:03 +08:00
|
|
|
bool startsWith(StringView Str) const {
|
|
|
|
if (Str.size() > size())
|
|
|
|
return false;
|
|
|
|
return std::equal(Str.begin(), Str.end(), begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
const char &operator[](size_t Idx) const { return *(begin() + Idx); }
|
|
|
|
|
|
|
|
const char *begin() const { return First; }
|
|
|
|
const char *end() const { return Last; }
|
|
|
|
size_t size() const { return static_cast<size_t>(Last - First); }
|
|
|
|
bool empty() const { return First == Last; }
|
|
|
|
};
|
|
|
|
|
|
|
|
inline bool operator==(const StringView &LHS, const StringView &RHS) {
|
|
|
|
return LHS.size() == RHS.size() &&
|
|
|
|
std::equal(LHS.begin(), LHS.end(), RHS.begin());
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|