StringView.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //===--- StringView.h ----------------*- mode:c++;eval:(read-only-mode) -*-===//
  2. // Do not edit! See README.txt.
  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. // There are two copies of this file in the source tree. The one under
  12. // libcxxabi is the original and the one under llvm is the copy. Use
  13. // cp-to-llvm.sh to update the copy. See README.txt for more details.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef DEMANGLE_STRINGVIEW_H
  17. #define DEMANGLE_STRINGVIEW_H
  18. #include "DemangleConfig.h"
  19. #include <cassert>
  20. #include <cstring>
  21. DEMANGLE_NAMESPACE_BEGIN
  22. class StringView {
  23. const char *First;
  24. const char *Last;
  25. public:
  26. static const size_t npos = ~size_t(0);
  27. template <size_t N>
  28. StringView(const char (&Str)[N]) : First(Str), Last(Str + N - 1) {}
  29. StringView(const char *First_, const char *Last_)
  30. : First(First_), Last(Last_) {}
  31. StringView(const char *First_, size_t Len)
  32. : First(First_), Last(First_ + Len) {}
  33. StringView(const char *Str) : First(Str), Last(Str + std::strlen(Str)) {}
  34. StringView() : First(nullptr), Last(nullptr) {}
  35. StringView substr(size_t Pos, size_t Len = npos) const {
  36. assert(Pos <= size());
  37. if (Len > size() - Pos)
  38. Len = size() - Pos;
  39. return StringView(begin() + Pos, Len);
  40. }
  41. size_t find(char C, size_t From = 0) const {
  42. // Avoid calling memchr with nullptr.
  43. if (From < size()) {
  44. // Just forward to memchr, which is faster than a hand-rolled loop.
  45. if (const void *P = ::memchr(First + From, C, size() - From))
  46. return size_t(static_cast<const char *>(P) - First);
  47. }
  48. return npos;
  49. }
  50. StringView dropFront(size_t N = 1) const {
  51. if (N >= size())
  52. N = size();
  53. return StringView(First + N, Last);
  54. }
  55. StringView dropBack(size_t N = 1) const {
  56. if (N >= size())
  57. N = size();
  58. return StringView(First, Last - N);
  59. }
  60. char front() const {
  61. assert(!empty());
  62. return *begin();
  63. }
  64. char back() const {
  65. assert(!empty());
  66. return *(end() - 1);
  67. }
  68. char popFront() {
  69. assert(!empty());
  70. return *First++;
  71. }
  72. bool consumeFront(char C) {
  73. if (!startsWith(C))
  74. return false;
  75. *this = dropFront(1);
  76. return true;
  77. }
  78. bool consumeFront(StringView S) {
  79. if (!startsWith(S))
  80. return false;
  81. *this = dropFront(S.size());
  82. return true;
  83. }
  84. bool startsWith(char C) const { return !empty() && *begin() == C; }
  85. bool startsWith(StringView Str) const {
  86. if (Str.size() > size())
  87. return false;
  88. return std::strncmp(Str.begin(), begin(), Str.size()) == 0;
  89. }
  90. const char &operator[](size_t Idx) const { return *(begin() + Idx); }
  91. const char *begin() const { return First; }
  92. const char *end() const { return Last; }
  93. size_t size() const { return static_cast<size_t>(Last - First); }
  94. bool empty() const { return First == Last; }
  95. };
  96. inline bool operator==(const StringView &LHS, const StringView &RHS) {
  97. return LHS.size() == RHS.size() &&
  98. std::strncmp(LHS.begin(), RHS.begin(), LHS.size()) == 0;
  99. }
  100. DEMANGLE_NAMESPACE_END
  101. #endif