StringView.h 3.4 KB

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