stringpiece.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // A StringPiece points to part or all of a string, Cord, double-quoted string
  31. // literal, or other string-like object. A StringPiece does *not* own the
  32. // string to which it points. A StringPiece is not null-terminated.
  33. //
  34. // You can use StringPiece as a function or method parameter. A StringPiece
  35. // parameter can receive a double-quoted string literal argument, a "const
  36. // char*" argument, a string argument, or a StringPiece argument with no data
  37. // copying. Systematic use of StringPiece for arguments reduces data
  38. // copies and strlen() calls.
  39. //
  40. // Prefer passing StringPieces by value:
  41. // void MyFunction(StringPiece arg);
  42. // If circumstances require, you may also pass by const reference:
  43. // void MyFunction(const StringPiece& arg); // not preferred
  44. // Both of these have the same lifetime semantics. Passing by value
  45. // generates slightly smaller code. For more discussion, see the thread
  46. // go/stringpiecebyvalue on c-users.
  47. //
  48. // StringPiece is also suitable for local variables if you know that
  49. // the lifetime of the underlying object is longer than the lifetime
  50. // of your StringPiece variable.
  51. //
  52. // Beware of binding a StringPiece to a temporary:
  53. // StringPiece sp = obj.MethodReturningString(); // BAD: lifetime problem
  54. //
  55. // This code is okay:
  56. // string str = obj.MethodReturningString(); // str owns its contents
  57. // StringPiece sp(str); // GOOD, because str outlives sp
  58. //
  59. // StringPiece is sometimes a poor choice for a return value and usually a poor
  60. // choice for a data member. If you do use a StringPiece this way, it is your
  61. // responsibility to ensure that the object pointed to by the StringPiece
  62. // outlives the StringPiece.
  63. //
  64. // A StringPiece may represent just part of a string; thus the name "Piece".
  65. // For example, when splitting a string, vector<StringPiece> is a natural data
  66. // type for the output. For another example, a Cord is a non-contiguous,
  67. // potentially very long string-like object. The Cord class has an interface
  68. // that iteratively provides StringPiece objects that point to the
  69. // successive pieces of a Cord object.
  70. //
  71. // A StringPiece is not null-terminated. If you write code that scans a
  72. // StringPiece, you must check its length before reading any characters.
  73. // Common idioms that work on null-terminated strings do not work on
  74. // StringPiece objects.
  75. //
  76. // There are several ways to create a null StringPiece:
  77. // StringPiece()
  78. // StringPiece(nullptr)
  79. // StringPiece(nullptr, 0)
  80. // For all of the above, sp.data() == nullptr, sp.length() == 0,
  81. // and sp.empty() == true. Also, if you create a StringPiece with
  82. // a non-null pointer then sp.data() != nullptr. Once created,
  83. // sp.data() will stay either nullptr or not-nullptr, except if you call
  84. // sp.clear() or sp.set().
  85. //
  86. // Thus, you can use StringPiece(nullptr) to signal an out-of-band value
  87. // that is different from other StringPiece values. This is similar
  88. // to the way that const char* p1 = nullptr; is different from
  89. // const char* p2 = "";.
  90. //
  91. // There are many ways to create an empty StringPiece:
  92. // StringPiece()
  93. // StringPiece(nullptr)
  94. // StringPiece(nullptr, 0)
  95. // StringPiece("")
  96. // StringPiece("", 0)
  97. // StringPiece("abcdef", 0)
  98. // StringPiece("abcdef"+6, 0)
  99. // For all of the above, sp.length() will be 0 and sp.empty() will be true.
  100. // For some empty StringPiece values, sp.data() will be nullptr.
  101. // For some empty StringPiece values, sp.data() will not be nullptr.
  102. //
  103. // Be careful not to confuse: null StringPiece and empty StringPiece.
  104. // The set of empty StringPieces properly includes the set of null StringPieces.
  105. // That is, every null StringPiece is an empty StringPiece,
  106. // but some non-null StringPieces are empty Stringpieces too.
  107. //
  108. // All empty StringPiece values compare equal to each other.
  109. // Even a null StringPieces compares equal to a non-null empty StringPiece:
  110. // StringPiece() == StringPiece("", 0)
  111. // StringPiece(nullptr) == StringPiece("abc", 0)
  112. // StringPiece(nullptr, 0) == StringPiece("abcdef"+6, 0)
  113. //
  114. // Look carefully at this example:
  115. // StringPiece("") == nullptr
  116. // True or false? TRUE, because StringPiece::operator== converts
  117. // the right-hand side from nullptr to StringPiece(nullptr),
  118. // and then compares two zero-length spans of characters.
  119. // However, we are working to make this example produce a compile error.
  120. //
  121. // Suppose you want to write:
  122. // bool TestWhat?(StringPiece sp) { return sp == nullptr; } // BAD
  123. // Do not do that. Write one of these instead:
  124. // bool TestNull(StringPiece sp) { return sp.data() == nullptr; }
  125. // bool TestEmpty(StringPiece sp) { return sp.empty(); }
  126. // The intent of TestWhat? is unclear. Did you mean TestNull or TestEmpty?
  127. // Right now, TestWhat? behaves likes TestEmpty.
  128. // We are working to make TestWhat? produce a compile error.
  129. // TestNull is good to test for an out-of-band signal.
  130. // TestEmpty is good to test for an empty StringPiece.
  131. //
  132. // Caveats (again):
  133. // (1) The lifetime of the pointed-to string (or piece of a string)
  134. // must be longer than the lifetime of the StringPiece.
  135. // (2) There may or may not be a '\0' character after the end of
  136. // StringPiece data.
  137. // (3) A null StringPiece is empty.
  138. // An empty StringPiece may or may not be a null StringPiece.
  139. #ifndef GOOGLE_PROTOBUF_STUBS_STRINGPIECE_H_
  140. #define GOOGLE_PROTOBUF_STUBS_STRINGPIECE_H_
  141. #include <assert.h>
  142. #include <stddef.h>
  143. #include <string.h>
  144. #include <iosfwd>
  145. #include <limits>
  146. #include <string>
  147. #if defined(__cpp_lib_string_view)
  148. #include <string_view>
  149. #endif
  150. #include <google/protobuf/stubs/hash.h>
  151. #include <google/protobuf/port_def.inc>
  152. namespace google {
  153. namespace protobuf {
  154. namespace stringpiece_internal {
  155. class PROTOBUF_EXPORT StringPiece {
  156. public:
  157. using traits_type = std::char_traits<char>;
  158. using value_type = char;
  159. using pointer = char*;
  160. using const_pointer = const char*;
  161. using reference = char&;
  162. using const_reference = const char&;
  163. using const_iterator = const char*;
  164. using iterator = const_iterator;
  165. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  166. using reverse_iterator = const_reverse_iterator;
  167. using size_type = size_t;
  168. using difference_type = std::ptrdiff_t;
  169. private:
  170. const char* ptr_;
  171. size_type length_;
  172. static constexpr size_type kMaxSize =
  173. (std::numeric_limits<difference_type>::max)();
  174. static size_type CheckSize(size_type size) {
  175. #if !defined(NDEBUG) || defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0
  176. if (PROTOBUF_PREDICT_FALSE(size > kMaxSize)) {
  177. // Some people grep for this message in logs
  178. // so take care if you ever change it.
  179. LogFatalSizeTooBig(size, "string length exceeds max size");
  180. }
  181. #endif
  182. return size;
  183. }
  184. // Out-of-line error path.
  185. static void LogFatalSizeTooBig(size_type size, const char* details);
  186. public:
  187. // We provide non-explicit singleton constructors so users can pass
  188. // in a "const char*" or a "string" wherever a "StringPiece" is
  189. // expected.
  190. //
  191. // Style guide exception granted:
  192. // http://goto/style-guide-exception-20978288
  193. StringPiece() : ptr_(nullptr), length_(0) {}
  194. StringPiece(const char* str) // NOLINT(runtime/explicit)
  195. : ptr_(str), length_(0) {
  196. if (str != nullptr) {
  197. length_ = CheckSize(strlen(str));
  198. }
  199. }
  200. template <class Allocator>
  201. StringPiece( // NOLINT(runtime/explicit)
  202. const std::basic_string<char, std::char_traits<char>, Allocator>& str)
  203. : ptr_(str.data()), length_(0) {
  204. length_ = CheckSize(str.size());
  205. }
  206. #if defined(__cpp_lib_string_view)
  207. StringPiece( // NOLINT(runtime/explicit)
  208. std::string_view str)
  209. : ptr_(str.data()), length_(0) {
  210. length_ = CheckSize(str.size());
  211. }
  212. #endif
  213. StringPiece(const char* offset, size_type len)
  214. : ptr_(offset), length_(CheckSize(len)) {}
  215. // data() may return a pointer to a buffer with embedded NULs, and the
  216. // returned buffer may or may not be null terminated. Therefore it is
  217. // typically a mistake to pass data() to a routine that expects a NUL
  218. // terminated string.
  219. const_pointer data() const { return ptr_; }
  220. size_type size() const { return length_; }
  221. size_type length() const { return length_; }
  222. bool empty() const { return length_ == 0; }
  223. char operator[](size_type i) const {
  224. assert(i < length_);
  225. return ptr_[i];
  226. }
  227. void remove_prefix(size_type n) {
  228. assert(length_ >= n);
  229. ptr_ += n;
  230. length_ -= n;
  231. }
  232. void remove_suffix(size_type n) {
  233. assert(length_ >= n);
  234. length_ -= n;
  235. }
  236. // returns {-1, 0, 1}
  237. int compare(StringPiece x) const {
  238. size_type min_size = length_ < x.length_ ? length_ : x.length_;
  239. int r = memcmp(ptr_, x.ptr_, static_cast<size_t>(min_size));
  240. if (r < 0) return -1;
  241. if (r > 0) return 1;
  242. if (length_ < x.length_) return -1;
  243. if (length_ > x.length_) return 1;
  244. return 0;
  245. }
  246. std::string as_string() const { return ToString(); }
  247. // We also define ToString() here, since many other string-like
  248. // interfaces name the routine that converts to a C++ string
  249. // "ToString", and it's confusing to have the method that does that
  250. // for a StringPiece be called "as_string()". We also leave the
  251. // "as_string()" method defined here for existing code.
  252. std::string ToString() const {
  253. if (ptr_ == nullptr) return "";
  254. return std::string(data(), static_cast<size_type>(size()));
  255. }
  256. explicit operator std::string() const { return ToString(); }
  257. void CopyToString(std::string* target) const;
  258. void AppendToString(std::string* target) const;
  259. bool starts_with(StringPiece x) const {
  260. return (length_ >= x.length_) &&
  261. (memcmp(ptr_, x.ptr_, static_cast<size_t>(x.length_)) == 0);
  262. }
  263. bool ends_with(StringPiece x) const {
  264. return ((length_ >= x.length_) &&
  265. (memcmp(ptr_ + (length_-x.length_), x.ptr_,
  266. static_cast<size_t>(x.length_)) == 0));
  267. }
  268. // Checks whether StringPiece starts with x and if so advances the beginning
  269. // of it to past the match. It's basically a shortcut for starts_with
  270. // followed by remove_prefix.
  271. bool Consume(StringPiece x);
  272. // Like above but for the end of the string.
  273. bool ConsumeFromEnd(StringPiece x);
  274. // standard STL container boilerplate
  275. static const size_type npos;
  276. const_iterator begin() const { return ptr_; }
  277. const_iterator end() const { return ptr_ + length_; }
  278. const_reverse_iterator rbegin() const {
  279. return const_reverse_iterator(ptr_ + length_);
  280. }
  281. const_reverse_iterator rend() const {
  282. return const_reverse_iterator(ptr_);
  283. }
  284. size_type max_size() const { return length_; }
  285. size_type capacity() const { return length_; }
  286. // cpplint.py emits a false positive [build/include_what_you_use]
  287. size_type copy(char* buf, size_type n, size_type pos = 0) const; // NOLINT
  288. bool contains(StringPiece s) const;
  289. size_type find(StringPiece s, size_type pos = 0) const;
  290. size_type find(char c, size_type pos = 0) const;
  291. size_type rfind(StringPiece s, size_type pos = npos) const;
  292. size_type rfind(char c, size_type pos = npos) const;
  293. size_type find_first_of(StringPiece s, size_type pos = 0) const;
  294. size_type find_first_of(char c, size_type pos = 0) const {
  295. return find(c, pos);
  296. }
  297. size_type find_first_not_of(StringPiece s, size_type pos = 0) const;
  298. size_type find_first_not_of(char c, size_type pos = 0) const;
  299. size_type find_last_of(StringPiece s, size_type pos = npos) const;
  300. size_type find_last_of(char c, size_type pos = npos) const {
  301. return rfind(c, pos);
  302. }
  303. size_type find_last_not_of(StringPiece s, size_type pos = npos) const;
  304. size_type find_last_not_of(char c, size_type pos = npos) const;
  305. StringPiece substr(size_type pos, size_type n = npos) const;
  306. };
  307. // This large function is defined inline so that in a fairly common case where
  308. // one of the arguments is a literal, the compiler can elide a lot of the
  309. // following comparisons.
  310. inline bool operator==(StringPiece x, StringPiece y) {
  311. StringPiece::size_type len = x.size();
  312. if (len != y.size()) {
  313. return false;
  314. }
  315. return x.data() == y.data() || len <= 0 ||
  316. memcmp(x.data(), y.data(), static_cast<size_t>(len)) == 0;
  317. }
  318. inline bool operator!=(StringPiece x, StringPiece y) {
  319. return !(x == y);
  320. }
  321. inline bool operator<(StringPiece x, StringPiece y) {
  322. const StringPiece::size_type min_size =
  323. x.size() < y.size() ? x.size() : y.size();
  324. const int r = memcmp(x.data(), y.data(), static_cast<size_t>(min_size));
  325. return (r < 0) || (r == 0 && x.size() < y.size());
  326. }
  327. inline bool operator>(StringPiece x, StringPiece y) {
  328. return y < x;
  329. }
  330. inline bool operator<=(StringPiece x, StringPiece y) {
  331. return !(x > y);
  332. }
  333. inline bool operator>=(StringPiece x, StringPiece y) {
  334. return !(x < y);
  335. }
  336. // allow StringPiece to be logged
  337. extern std::ostream& operator<<(std::ostream& o, StringPiece piece);
  338. } // namespace stringpiece_internal
  339. using ::google::protobuf::stringpiece_internal::StringPiece;
  340. } // namespace protobuf
  341. } // namespace google
  342. GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_START
  343. template<> struct hash<StringPiece> {
  344. size_t operator()(const StringPiece& s) const {
  345. size_t result = 0;
  346. for (const char *str = s.data(), *end = str + s.size(); str < end; str++) {
  347. result = 5 * result + static_cast<size_t>(*str);
  348. }
  349. return result;
  350. }
  351. };
  352. GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_END
  353. #include <google/protobuf/port_undef.inc>
  354. #endif // STRINGS_STRINGPIECE_H_