stringpiece.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. #include <google/protobuf/stubs/hash.h>
  148. #include <google/protobuf/port_def.inc>
  149. namespace google {
  150. namespace protobuf {
  151. namespace stringpiece_internal {
  152. class PROTOBUF_EXPORT StringPiece {
  153. public:
  154. using traits_type = std::char_traits<char>;
  155. using value_type = char;
  156. using pointer = char*;
  157. using const_pointer = const char*;
  158. using reference = char&;
  159. using const_reference = const char&;
  160. using const_iterator = const char*;
  161. using iterator = const_iterator;
  162. using const_reverse_iterator = std::reverse_iterator<const_iterator>;
  163. using reverse_iterator = const_reverse_iterator;
  164. using size_type = size_t;
  165. using difference_type = std::ptrdiff_t;
  166. private:
  167. const char* ptr_;
  168. size_type length_;
  169. static constexpr size_type kMaxSize =
  170. (std::numeric_limits<difference_type>::max)();
  171. static size_type CheckSize(size_type size) {
  172. #if !defined(NDEBUG) || defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0
  173. if (PROTOBUF_PREDICT_FALSE(size > kMaxSize)) {
  174. // Some people grep for this message in logs
  175. // so take care if you ever change it.
  176. LogFatalSizeTooBig(size, "string length exceeds max size");
  177. }
  178. #endif
  179. return size;
  180. }
  181. // Out-of-line error path.
  182. static void LogFatalSizeTooBig(size_type size, const char* details);
  183. public:
  184. // We provide non-explicit singleton constructors so users can pass
  185. // in a "const char*" or a "string" wherever a "StringPiece" is
  186. // expected.
  187. //
  188. // Style guide exception granted:
  189. // http://goto/style-guide-exception-20978288
  190. StringPiece() : ptr_(nullptr), length_(0) {}
  191. StringPiece(const char* str) // NOLINT(runtime/explicit)
  192. : ptr_(str), length_(0) {
  193. if (str != nullptr) {
  194. length_ = CheckSize(strlen(str));
  195. }
  196. }
  197. template <class Allocator>
  198. StringPiece( // NOLINT(runtime/explicit)
  199. const std::basic_string<char, std::char_traits<char>, Allocator>& str)
  200. : ptr_(str.data()), length_(0) {
  201. length_ = CheckSize(str.size());
  202. }
  203. StringPiece(const char* offset, size_type len)
  204. : ptr_(offset), length_(CheckSize(len)) {}
  205. // data() may return a pointer to a buffer with embedded NULs, and the
  206. // returned buffer may or may not be null terminated. Therefore it is
  207. // typically a mistake to pass data() to a routine that expects a NUL
  208. // terminated string.
  209. const_pointer data() const { return ptr_; }
  210. size_type size() const { return length_; }
  211. size_type length() const { return length_; }
  212. bool empty() const { return length_ == 0; }
  213. char operator[](size_type i) const {
  214. assert(i < length_);
  215. return ptr_[i];
  216. }
  217. void remove_prefix(size_type n) {
  218. assert(length_ >= n);
  219. ptr_ += n;
  220. length_ -= n;
  221. }
  222. void remove_suffix(size_type n) {
  223. assert(length_ >= n);
  224. length_ -= n;
  225. }
  226. // returns {-1, 0, 1}
  227. int compare(StringPiece x) const {
  228. size_type min_size = length_ < x.length_ ? length_ : x.length_;
  229. int r = memcmp(ptr_, x.ptr_, static_cast<size_t>(min_size));
  230. if (r < 0) return -1;
  231. if (r > 0) return 1;
  232. if (length_ < x.length_) return -1;
  233. if (length_ > x.length_) return 1;
  234. return 0;
  235. }
  236. std::string as_string() const { return ToString(); }
  237. // We also define ToString() here, since many other string-like
  238. // interfaces name the routine that converts to a C++ string
  239. // "ToString", and it's confusing to have the method that does that
  240. // for a StringPiece be called "as_string()". We also leave the
  241. // "as_string()" method defined here for existing code.
  242. std::string ToString() const {
  243. if (ptr_ == nullptr) return "";
  244. return std::string(data(), static_cast<size_type>(size()));
  245. }
  246. explicit operator std::string() const { return ToString(); }
  247. void CopyToString(std::string* target) const;
  248. void AppendToString(std::string* target) const;
  249. bool starts_with(StringPiece x) const {
  250. return (length_ >= x.length_) &&
  251. (memcmp(ptr_, x.ptr_, static_cast<size_t>(x.length_)) == 0);
  252. }
  253. bool ends_with(StringPiece x) const {
  254. return ((length_ >= x.length_) &&
  255. (memcmp(ptr_ + (length_-x.length_), x.ptr_,
  256. static_cast<size_t>(x.length_)) == 0));
  257. }
  258. // Checks whether StringPiece starts with x and if so advances the beginning
  259. // of it to past the match. It's basically a shortcut for starts_with
  260. // followed by remove_prefix.
  261. bool Consume(StringPiece x);
  262. // Like above but for the end of the string.
  263. bool ConsumeFromEnd(StringPiece x);
  264. // standard STL container boilerplate
  265. static const size_type npos;
  266. const_iterator begin() const { return ptr_; }
  267. const_iterator end() const { return ptr_ + length_; }
  268. const_reverse_iterator rbegin() const {
  269. return const_reverse_iterator(ptr_ + length_);
  270. }
  271. const_reverse_iterator rend() const {
  272. return const_reverse_iterator(ptr_);
  273. }
  274. size_type max_size() const { return length_; }
  275. size_type capacity() const { return length_; }
  276. // cpplint.py emits a false positive [build/include_what_you_use]
  277. size_type copy(char* buf, size_type n, size_type pos = 0) const; // NOLINT
  278. bool contains(StringPiece s) const;
  279. size_type find(StringPiece s, size_type pos = 0) const;
  280. size_type find(char c, size_type pos = 0) const;
  281. size_type rfind(StringPiece s, size_type pos = npos) const;
  282. size_type rfind(char c, size_type pos = npos) const;
  283. size_type find_first_of(StringPiece s, size_type pos = 0) const;
  284. size_type find_first_of(char c, size_type pos = 0) const {
  285. return find(c, pos);
  286. }
  287. size_type find_first_not_of(StringPiece s, size_type pos = 0) const;
  288. size_type find_first_not_of(char c, size_type pos = 0) const;
  289. size_type find_last_of(StringPiece s, size_type pos = npos) const;
  290. size_type find_last_of(char c, size_type pos = npos) const {
  291. return rfind(c, pos);
  292. }
  293. size_type find_last_not_of(StringPiece s, size_type pos = npos) const;
  294. size_type find_last_not_of(char c, size_type pos = npos) const;
  295. StringPiece substr(size_type pos, size_type n = npos) const;
  296. };
  297. // This large function is defined inline so that in a fairly common case where
  298. // one of the arguments is a literal, the compiler can elide a lot of the
  299. // following comparisons.
  300. inline bool operator==(StringPiece x, StringPiece y) {
  301. StringPiece::size_type len = x.size();
  302. if (len != y.size()) {
  303. return false;
  304. }
  305. return x.data() == y.data() || len <= 0 ||
  306. memcmp(x.data(), y.data(), static_cast<size_t>(len)) == 0;
  307. }
  308. inline bool operator!=(StringPiece x, StringPiece y) {
  309. return !(x == y);
  310. }
  311. inline bool operator<(StringPiece x, StringPiece y) {
  312. const StringPiece::size_type min_size =
  313. x.size() < y.size() ? x.size() : y.size();
  314. const int r = memcmp(x.data(), y.data(), static_cast<size_t>(min_size));
  315. return (r < 0) || (r == 0 && x.size() < y.size());
  316. }
  317. inline bool operator>(StringPiece x, StringPiece y) {
  318. return y < x;
  319. }
  320. inline bool operator<=(StringPiece x, StringPiece y) {
  321. return !(x > y);
  322. }
  323. inline bool operator>=(StringPiece x, StringPiece y) {
  324. return !(x < y);
  325. }
  326. // allow StringPiece to be logged
  327. extern std::ostream& operator<<(std::ostream& o, StringPiece piece);
  328. } // namespace stringpiece_internal
  329. using ::google::protobuf::stringpiece_internal::StringPiece;
  330. } // namespace protobuf
  331. } // namespace google
  332. GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_START
  333. template<> struct hash<StringPiece> {
  334. size_t operator()(const StringPiece& s) const {
  335. size_t result = 0;
  336. for (const char *str = s.data(), *end = str + s.size(); str < end; str++) {
  337. result = 5 * result + static_cast<size_t>(*str);
  338. }
  339. return result;
  340. }
  341. };
  342. GOOGLE_PROTOBUF_HASH_NAMESPACE_DECLARATION_END
  343. #include <google/protobuf/port_undef.inc>
  344. #endif // STRINGS_STRINGPIECE_H_