You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

stringpiece.h 7.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. {
  34. class StringPiece
  35. {
  36. public:
  37. typedef std::char_traits<char> traits_type;
  38. typedef char value_type;
  39. typedef char* pointer;
  40. typedef const char* const_pointer;
  41. typedef char& reference;
  42. typedef const char& const_reference;
  43. typedef const char* const_iterator;
  44. typedef const_iterator iterator;
  45. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  46. typedef const_reverse_iterator reverse_iterator;
  47. typedef size_t size_type;
  48. typedef ptrdiff_t difference_type;
  49. static const size_type npos = static_cast<size_type>(-1);
  50. // We provide non-explicit singleton constructors so users can pass
  51. // in a "const char*" or a "string" wherever a "StringPiece" is
  52. // expected.
  53. StringPiece() :
  54. data_(NULL),
  55. size_(0)
  56. {
  57. }
  58. #if __has_include(<string_view>) && __cplusplus >= 201703L
  59. StringPiece(const std::string_view& str) :
  60. data_(str.data()),
  61. size_(str.size())
  62. {
  63. }
  64. #endif
  65. StringPiece(const std::string& str) :
  66. data_(str.data()),
  67. size_(str.size())
  68. {
  69. }
  70. StringPiece(const char* str) :
  71. data_(str),
  72. size_(str == NULL ? 0 : strlen(str))
  73. {
  74. }
  75. StringPiece(const char* str, size_type len) :
  76. data_(str),
  77. size_(len)
  78. {
  79. }
  80. const_iterator begin() const
  81. {
  82. return data_;
  83. }
  84. const_iterator end() const
  85. {
  86. return data_ + size_;
  87. }
  88. const_reverse_iterator rbegin() const
  89. {
  90. return const_reverse_iterator(data_ + size_);
  91. }
  92. const_reverse_iterator rend() const
  93. {
  94. return const_reverse_iterator(data_);
  95. }
  96. size_type size() const
  97. {
  98. return size_;
  99. }
  100. size_type length() const
  101. {
  102. return size_;
  103. }
  104. bool empty() const
  105. {
  106. return size_ == 0;
  107. }
  108. const_reference operator[](size_type i) const
  109. {
  110. return data_[i];
  111. }
  112. const_pointer data() const
  113. {
  114. return data_;
  115. }
  116. void remove_prefix(size_type n)
  117. {
  118. data_ += n;
  119. size_ -= n;
  120. }
  121. void remove_suffix(size_type n)
  122. {
  123. size_ -= n;
  124. }
  125. void set(const char* str)
  126. {
  127. data_ = str;
  128. size_ = str == NULL ? 0 : strlen(str);
  129. }
  130. void set(const char* str, size_type len)
  131. {
  132. data_ = str;
  133. size_ = len;
  134. }
  135. // Converts to `std::basic_string`.
  136. template<typename A>
  137. explicit operator std::basic_string<char, traits_type, A>() const
  138. {
  139. if (!data_)
  140. return {};
  141. return std::basic_string<char, traits_type, A>(data_, size_);
  142. }
  143. std::string as_string() const
  144. {
  145. return std::string(data_, size_);
  146. }
  147. // We also define ToString() here, since many other string-like
  148. // interfaces name the routine that converts to a C++ string
  149. // "ToString", and it's confusing to have the method that does that
  150. // for a StringPiece be called "as_string()". We also leave the
  151. // "as_string()" method defined here for existing code.
  152. std::string ToString() const
  153. {
  154. return std::string(data_, size_);
  155. }
  156. void CopyToString(std::string* target) const
  157. {
  158. target->assign(data_, size_);
  159. }
  160. void AppendToString(std::string* target) const
  161. {
  162. target->append(data_, size_);
  163. }
  164. size_type copy(char* buf, size_type n, size_type pos = 0) const;
  165. StringPiece substr(size_type pos = 0, size_type n = npos) const;
  166. int compare(const StringPiece& x) const
  167. {
  168. size_type min_size = std::min(size(), x.size());
  169. if (min_size > 0)
  170. {
  171. int r = memcmp(data(), x.data(), min_size);
  172. if (r < 0)
  173. return -1;
  174. if (r > 0)
  175. return 1;
  176. }
  177. if (size() < x.size())
  178. return -1;
  179. if (size() > x.size())
  180. return 1;
  181. return 0;
  182. }
  183. // Does "this" start with "x"?
  184. bool starts_with(const StringPiece& x) const
  185. {
  186. return x.empty() ||
  187. (size() >= x.size() && memcmp(data(), x.data(), x.size()) == 0);
  188. }
  189. // Does "this" end with "x"?
  190. bool ends_with(const StringPiece& x) const
  191. {
  192. return x.empty() ||
  193. (size() >= x.size() &&
  194. memcmp(data() + (size() - x.size()), x.data(), x.size()) == 0);
  195. }
  196. bool contains(const StringPiece& s) const
  197. {
  198. return find(s) != npos;
  199. }
  200. size_type find(const StringPiece& s, size_type pos = 0) const;
  201. size_type find(char c, size_type pos = 0) const;
  202. size_type rfind(const StringPiece& s, size_type pos = npos) const;
  203. size_type rfind(char c, size_type pos = npos) const;
  204. private:
  205. const_pointer data_;
  206. size_type size_;
  207. };
  208. inline bool operator==(const StringPiece& x, const StringPiece& y)
  209. {
  210. StringPiece::size_type len = x.size();
  211. if (len != y.size())
  212. return false;
  213. return x.data() == y.data() || len == 0 ||
  214. memcmp(x.data(), y.data(), len) == 0;
  215. }
  216. inline bool operator!=(const StringPiece& x, const StringPiece& y)
  217. {
  218. return !(x == y);
  219. }
  220. inline bool operator<(const StringPiece& x, const StringPiece& y)
  221. {
  222. StringPiece::size_type min_size = std::min(x.size(), y.size());
  223. int r = min_size == 0 ? 0 : memcmp(x.data(), y.data(), min_size);
  224. return (r < 0) || (r == 0 && x.size() < y.size());
  225. }
  226. inline bool operator>(const StringPiece& x, const StringPiece& y)
  227. {
  228. return y < x;
  229. }
  230. inline bool operator<=(const StringPiece& x, const StringPiece& y)
  231. {
  232. return !(x > y);
  233. }
  234. inline bool operator>=(const StringPiece& x, const StringPiece& y)
  235. {
  236. return !(x < y);
  237. }
  238. // Allow StringPiece to be logged.
  239. std::ostream& operator<<(std::ostream& o, const StringPiece& p);
  240. } // namespace re2
  241. #endif // RE2_STRINGPIECE_H_