StringView.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- StringView.h ----------------*- mode:c++;eval:(read-only-mode) -*-===//
  7. // Do not edit! See README.txt.
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // FIXME: Use std::string_view instead when we support C++17.
  15. // There are two copies of this file in the source tree. The one under
  16. // libcxxabi is the original and the one under llvm is the copy. Use
  17. // cp-to-llvm.sh to update the copy. See README.txt for more details.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef DEMANGLE_STRINGVIEW_H
  21. #define DEMANGLE_STRINGVIEW_H
  22. #include "DemangleConfig.h"
  23. #include <cassert>
  24. #include <cstring>
  25. DEMANGLE_NAMESPACE_BEGIN
  26. class StringView {
  27. const char *First;
  28. const char *Last;
  29. public:
  30. static const size_t npos = ~size_t(0);
  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. StringView substr(size_t Pos, size_t Len = npos) const {
  40. assert(Pos <= size());
  41. if (Len > size() - Pos)
  42. Len = size() - Pos;
  43. return StringView(begin() + Pos, Len);
  44. }
  45. size_t find(char C, size_t From = 0) const {
  46. // Avoid calling memchr with nullptr.
  47. if (From < size()) {
  48. // Just forward to memchr, which is faster than a hand-rolled loop.
  49. if (const void *P = ::memchr(First + From, C, size() - From))
  50. return size_t(static_cast<const char *>(P) - First);
  51. }
  52. return npos;
  53. }
  54. StringView dropFront(size_t N = 1) const {
  55. if (N >= size())
  56. N = size();
  57. return StringView(First + N, Last);
  58. }
  59. StringView dropBack(size_t N = 1) const {
  60. if (N >= size())
  61. N = size();
  62. return StringView(First, Last - N);
  63. }
  64. char front() const {
  65. assert(!empty());
  66. return *begin();
  67. }
  68. char back() const {
  69. assert(!empty());
  70. return *(end() - 1);
  71. }
  72. char popFront() {
  73. assert(!empty());
  74. return *First++;
  75. }
  76. bool consumeFront(char C) {
  77. if (!startsWith(C))
  78. return false;
  79. *this = dropFront(1);
  80. return true;
  81. }
  82. bool consumeFront(StringView S) {
  83. if (!startsWith(S))
  84. return false;
  85. *this = dropFront(S.size());
  86. return true;
  87. }
  88. bool startsWith(char C) const { return !empty() && *begin() == C; }
  89. bool startsWith(StringView Str) const {
  90. if (Str.size() > size())
  91. return false;
  92. return std::strncmp(Str.begin(), begin(), Str.size()) == 0;
  93. }
  94. const char &operator[](size_t Idx) const { return *(begin() + Idx); }
  95. const char *begin() const { return First; }
  96. const char *end() const { return Last; }
  97. size_t size() const { return static_cast<size_t>(Last - First); }
  98. bool empty() const { return First == Last; }
  99. };
  100. inline bool operator==(const StringView &LHS, const StringView &RHS) {
  101. return LHS.size() == RHS.size() &&
  102. std::strncmp(LHS.begin(), RHS.begin(), LHS.size()) == 0;
  103. }
  104. DEMANGLE_NAMESPACE_END
  105. #endif
  106. #ifdef __GNUC__
  107. #pragma GCC diagnostic pop
  108. #endif