stringpiece.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2001-2010 The RE2 Authors. All Rights Reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. #ifndef RE2_STRINGPIECE_H_
  5. #define RE2_STRINGPIECE_H_
  6. // A string-like object that points to a sized piece of memory.
  7. //
  8. // Functions or methods may use const StringPiece& parameters to accept either
  9. // a "const char*" or a "string" value that will be implicitly converted to
  10. // a StringPiece. The implicit conversion means that it is often appropriate
  11. // to include this .h file in other files rather than forward-declaring
  12. // StringPiece as would be appropriate for most other Google classes.
  13. //
  14. // Systematic usage of StringPiece is encouraged as it will reduce unnecessary
  15. // conversions from "const char*" to "string" and back again.
  16. //
  17. //
  18. // Arghh! I wish C++ literals were "string".
  19. // Doing this simplifies the logic below.
  20. #ifndef __has_include
  21. #define __has_include(x) 0
  22. #endif
  23. #include <stddef.h>
  24. #include <string.h>
  25. #include <algorithm>
  26. #include <iosfwd>
  27. #include <iterator>
  28. #include <string>
  29. #if __has_include(<string_view>) && __cplusplus >= 201703L
  30. #include <string_view>
  31. #endif
  32. namespace re2 {
  33. class StringPiece {
  34. public:
  35. typedef std::char_traits<char> traits_type;
  36. typedef char value_type;
  37. typedef char* pointer;
  38. typedef const char* const_pointer;
  39. typedef char& reference;
  40. typedef const char& const_reference;
  41. typedef const char* const_iterator;
  42. typedef const_iterator iterator;
  43. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  44. typedef const_reverse_iterator reverse_iterator;
  45. typedef size_t size_type;
  46. typedef ptrdiff_t difference_type;
  47. static const size_type npos = static_cast<size_type>(-1);
  48. // We provide non-explicit singleton constructors so users can pass
  49. // in a "const char*" or a "string" wherever a "StringPiece" is
  50. // expected.
  51. StringPiece()
  52. : data_(NULL), size_(0) {}
  53. #if __has_include(<string_view>) && __cplusplus >= 201703L
  54. StringPiece(const std::string_view& str)
  55. : data_(str.data()), size_(str.size()) {}
  56. #endif
  57. StringPiece(const std::string& str)
  58. : data_(str.data()), size_(str.size()) {}
  59. StringPiece(const char* str)
  60. : data_(str), size_(str == NULL ? 0 : strlen(str)) {}
  61. StringPiece(const char* str, size_type len)
  62. : data_(str), size_(len) {}
  63. const_iterator begin() const { return data_; }
  64. const_iterator end() const { return data_ + size_; }
  65. const_reverse_iterator rbegin() const {
  66. return const_reverse_iterator(data_ + size_);
  67. }
  68. const_reverse_iterator rend() const {
  69. return const_reverse_iterator(data_);
  70. }
  71. size_type size() const { return size_; }
  72. size_type length() const { return size_; }
  73. bool empty() const { return size_ == 0; }
  74. const_reference operator[](size_type i) const { return data_[i]; }
  75. const_pointer data() const { return data_; }
  76. void remove_prefix(size_type n) {
  77. data_ += n;
  78. size_ -= n;
  79. }
  80. void remove_suffix(size_type n) {
  81. size_ -= n;
  82. }
  83. void set(const char* str) {
  84. data_ = str;
  85. size_ = str == NULL ? 0 : strlen(str);
  86. }
  87. void set(const char* str, size_type len) {
  88. data_ = str;
  89. size_ = len;
  90. }
  91. // Converts to `std::basic_string`.
  92. template <typename A>
  93. explicit operator std::basic_string<char, traits_type, A>() const {
  94. if (!data_) return {};
  95. return std::basic_string<char, traits_type, A>(data_, size_);
  96. }
  97. std::string as_string() const {
  98. return std::string(data_, size_);
  99. }
  100. // We also define ToString() here, since many other string-like
  101. // interfaces name the routine that converts to a C++ string
  102. // "ToString", and it's confusing to have the method that does that
  103. // for a StringPiece be called "as_string()". We also leave the
  104. // "as_string()" method defined here for existing code.
  105. std::string ToString() const {
  106. return std::string(data_, size_);
  107. }
  108. void CopyToString(std::string* target) const {
  109. target->assign(data_, size_);
  110. }
  111. void AppendToString(std::string* target) const {
  112. target->append(data_, size_);
  113. }
  114. size_type copy(char* buf, size_type n, size_type pos = 0) const;
  115. StringPiece substr(size_type pos = 0, size_type n = npos) const;
  116. int compare(const StringPiece& x) const {
  117. size_type min_size = std::min(size(), x.size());
  118. if (min_size > 0) {
  119. int r = memcmp(data(), x.data(), min_size);
  120. if (r < 0) return -1;
  121. if (r > 0) return 1;
  122. }
  123. if (size() < x.size()) return -1;
  124. if (size() > x.size()) return 1;
  125. return 0;
  126. }
  127. // Does "this" start with "x"?
  128. bool starts_with(const StringPiece& x) const {
  129. return x.empty() ||
  130. (size() >= x.size() && memcmp(data(), x.data(), x.size()) == 0);
  131. }
  132. // Does "this" end with "x"?
  133. bool ends_with(const StringPiece& x) const {
  134. return x.empty() ||
  135. (size() >= x.size() &&
  136. memcmp(data() + (size() - x.size()), x.data(), x.size()) == 0);
  137. }
  138. bool contains(const StringPiece& s) const {
  139. return find(s) != npos;
  140. }
  141. size_type find(const StringPiece& s, size_type pos = 0) const;
  142. size_type find(char c, size_type pos = 0) const;
  143. size_type rfind(const StringPiece& s, size_type pos = npos) const;
  144. size_type rfind(char c, size_type pos = npos) const;
  145. private:
  146. const_pointer data_;
  147. size_type size_;
  148. };
  149. inline bool operator==(const StringPiece& x, const StringPiece& y) {
  150. StringPiece::size_type len = x.size();
  151. if (len != y.size()) return false;
  152. return x.data() == y.data() || len == 0 ||
  153. memcmp(x.data(), y.data(), len) == 0;
  154. }
  155. inline bool operator!=(const StringPiece& x, const StringPiece& y) {
  156. return !(x == y);
  157. }
  158. inline bool operator<(const StringPiece& x, const StringPiece& y) {
  159. StringPiece::size_type min_size = std::min(x.size(), y.size());
  160. int r = min_size == 0 ? 0 : memcmp(x.data(), y.data(), min_size);
  161. return (r < 0) || (r == 0 && x.size() < y.size());
  162. }
  163. inline bool operator>(const StringPiece& x, const StringPiece& y) {
  164. return y < x;
  165. }
  166. inline bool operator<=(const StringPiece& x, const StringPiece& y) {
  167. return !(x > y);
  168. }
  169. inline bool operator>=(const StringPiece& x, const StringPiece& y) {
  170. return !(x < y);
  171. }
  172. // Allow StringPiece to be logged.
  173. std::ostream& operator<<(std::ostream& o, const StringPiece& p);
  174. } // namespace re2
  175. #endif // RE2_STRINGPIECE_H_