StringView.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #ifndef DEMANGLE_STRINGVIEW_H
  14. #define DEMANGLE_STRINGVIEW_H
  15. #include "DemangleConfig.h"
  16. #include <algorithm>
  17. #include <cassert>
  18. #include <cstring>
  19. DEMANGLE_NAMESPACE_BEGIN
  20. class StringView {
  21. const char *First;
  22. const char *Last;
  23. public:
  24. static const size_t npos = ~size_t(0);
  25. template <size_t N>
  26. StringView(const char (&Str)[N]) : First(Str), Last(Str + N - 1) {}
  27. StringView(const char *First_, const char *Last_)
  28. : First(First_), Last(Last_) {}
  29. StringView(const char *First_, size_t Len)
  30. : First(First_), Last(First_ + Len) {}
  31. StringView(const char *Str) : First(Str), Last(Str + std::strlen(Str)) {}
  32. StringView() : First(nullptr), Last(nullptr) {}
  33. StringView substr(size_t From) const {
  34. return StringView(begin() + From, size() - From);
  35. }
  36. size_t find(char C, size_t From = 0) const {
  37. size_t FindBegin = std::min(From, size());
  38. // Avoid calling memchr with nullptr.
  39. if (FindBegin < size()) {
  40. // Just forward to memchr, which is faster than a hand-rolled loop.
  41. if (const void *P = ::memchr(First + FindBegin, C, size() - FindBegin))
  42. return size_t(static_cast<const char *>(P) - First);
  43. }
  44. return npos;
  45. }
  46. StringView substr(size_t From, size_t To) const {
  47. if (To >= size())
  48. To = size() - 1;
  49. if (From >= size())
  50. From = size() - 1;
  51. return StringView(First + From, First + To);
  52. }
  53. StringView dropFront(size_t N = 1) const {
  54. if (N >= size())
  55. N = size();
  56. return StringView(First + N, Last);
  57. }
  58. StringView dropBack(size_t N = 1) const {
  59. if (N >= size())
  60. N = size();
  61. return StringView(First, Last - N);
  62. }
  63. char front() const {
  64. assert(!empty());
  65. return *begin();
  66. }
  67. char back() const {
  68. assert(!empty());
  69. return *(end() - 1);
  70. }
  71. char popFront() {
  72. assert(!empty());
  73. return *First++;
  74. }
  75. bool consumeFront(char C) {
  76. if (!startsWith(C))
  77. return false;
  78. *this = dropFront(1);
  79. return true;
  80. }
  81. bool consumeFront(StringView S) {
  82. if (!startsWith(S))
  83. return false;
  84. *this = dropFront(S.size());
  85. return true;
  86. }
  87. bool startsWith(char C) const { return !empty() && *begin() == C; }
  88. bool startsWith(StringView Str) const {
  89. if (Str.size() > size())
  90. return false;
  91. return std::equal(Str.begin(), Str.end(), begin());
  92. }
  93. const char &operator[](size_t Idx) const { return *(begin() + Idx); }
  94. const char *begin() const { return First; }
  95. const char *end() const { return Last; }
  96. size_t size() const { return static_cast<size_t>(Last - First); }
  97. bool empty() const { return First == Last; }
  98. };
  99. inline bool operator==(const StringView &LHS, const StringView &RHS) {
  100. return LHS.size() == RHS.size() &&
  101. std::equal(LHS.begin(), LHS.end(), RHS.begin());
  102. }
  103. DEMANGLE_NAMESPACE_END
  104. #endif