stringpiece.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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(NULL)
  79. // StringPiece(NULL, 0)
  80. // For all of the above, sp.data() == NULL, sp.length() == 0,
  81. // and sp.empty() == true. Also, if you create a StringPiece with
  82. // a non-NULL pointer then sp.data() != NULL. Once created,
  83. // sp.data() will stay either NULL or not-NULL, except if you call
  84. // sp.clear() or sp.set().
  85. //
  86. // Thus, you can use StringPiece(NULL) 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 = NULL; is different from
  89. // const char* p2 = "";.
  90. //
  91. // There are many ways to create an empty StringPiece:
  92. // StringPiece()
  93. // StringPiece(NULL)
  94. // StringPiece(NULL, 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 NULL.
  101. // For some empty StringPiece values, sp.data() will not be NULL.
  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(NULL) == StringPiece("abc", 0)
  112. // StringPiece(NULL, 0) == StringPiece("abcdef"+6, 0)
  113. //
  114. // Look carefully at this example:
  115. // StringPiece("") == NULL
  116. // True or false? TRUE, because StringPiece::operator== converts
  117. // the right-hand side from NULL to StringPiece(NULL),
  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 == NULL; } // BAD
  123. // Do not do that. Write one of these instead:
  124. // bool TestNull(StringPiece sp) { return sp.data() == NULL; }
  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/common.h>
  148. namespace google {
  149. namespace protobuf {
  150. // StringPiece has *two* size types.
  151. // StringPiece::size_type
  152. // is unsigned
  153. // is 32 bits in LP32, 64 bits in LP64, 64 bits in LLP64
  154. // no future changes intended
  155. // stringpiece_ssize_type
  156. // is signed
  157. // is 32 bits in LP32, 64 bits in LP64, 64 bits in LLP64
  158. // future changes intended: http://go/64BitStringPiece
  159. //
  160. typedef string::difference_type stringpiece_ssize_type;
  161. // STRINGPIECE_CHECK_SIZE protects us from 32-bit overflows.
  162. // TODO(mec): delete this after stringpiece_ssize_type goes 64 bit.
  163. #if !defined(NDEBUG)
  164. #define STRINGPIECE_CHECK_SIZE 1
  165. #elif defined(_FORTIFY_SOURCE) && _FORTIFY_SOURCE > 0
  166. #define STRINGPIECE_CHECK_SIZE 1
  167. #else
  168. #define STRINGPIECE_CHECK_SIZE 0
  169. #endif
  170. class LIBPROTOBUF_EXPORT StringPiece {
  171. private:
  172. const char* ptr_;
  173. stringpiece_ssize_type length_;
  174. // Prevent overflow in debug mode or fortified mode.
  175. // sizeof(stringpiece_ssize_type) may be smaller than sizeof(size_t).
  176. static stringpiece_ssize_type CheckedSsizeTFromSizeT(size_t size) {
  177. #if STRINGPIECE_CHECK_SIZE > 0
  178. #ifdef max
  179. #undef max
  180. #endif
  181. if (size > static_cast<size_t>(
  182. std::numeric_limits<stringpiece_ssize_type>::max())) {
  183. // Some people grep for this message in logs
  184. // so take care if you ever change it.
  185. LogFatalSizeTooBig(size, "size_t to int conversion");
  186. }
  187. #endif
  188. return static_cast<stringpiece_ssize_type>(size);
  189. }
  190. // Out-of-line error path.
  191. static void LogFatalSizeTooBig(size_t size, const char* details);
  192. public:
  193. // We provide non-explicit singleton constructors so users can pass
  194. // in a "const char*" or a "string" wherever a "StringPiece" is
  195. // expected.
  196. //
  197. // Style guide exception granted:
  198. // http://goto/style-guide-exception-20978288
  199. StringPiece() : ptr_(NULL), length_(0) {}
  200. StringPiece(const char* str) // NOLINT(runtime/explicit)
  201. : ptr_(str), length_(0) {
  202. if (str != NULL) {
  203. length_ = CheckedSsizeTFromSizeT(strlen(str));
  204. }
  205. }
  206. template <class Allocator>
  207. StringPiece( // NOLINT(runtime/explicit)
  208. const std::basic_string<char, std::char_traits<char>, Allocator>& str)
  209. : ptr_(str.data()), length_(0) {
  210. length_ = CheckedSsizeTFromSizeT(str.size());
  211. }
  212. #if defined(HAS_GLOBAL_STRING)
  213. template <class Allocator>
  214. StringPiece( // NOLINT(runtime/explicit)
  215. const basic_string<char, std::char_traits<char>, Allocator>& str)
  216. : ptr_(str.data()), length_(0) {
  217. length_ = CheckedSsizeTFromSizeT(str.size());
  218. }
  219. #endif
  220. StringPiece(const char* offset, stringpiece_ssize_type len)
  221. : ptr_(offset), length_(len) {
  222. assert(len >= 0);
  223. }
  224. // Substring of another StringPiece.
  225. // pos must be non-negative and <= x.length().
  226. StringPiece(StringPiece x, stringpiece_ssize_type pos);
  227. // Substring of another StringPiece.
  228. // pos must be non-negative and <= x.length().
  229. // len must be non-negative and will be pinned to at most x.length() - pos.
  230. StringPiece(StringPiece x,
  231. stringpiece_ssize_type pos,
  232. stringpiece_ssize_type len);
  233. // data() may return a pointer to a buffer with embedded NULs, and the
  234. // returned buffer may or may not be null terminated. Therefore it is
  235. // typically a mistake to pass data() to a routine that expects a NUL
  236. // terminated string.
  237. const char* data() const { return ptr_; }
  238. stringpiece_ssize_type size() const { return length_; }
  239. stringpiece_ssize_type length() const { return length_; }
  240. bool empty() const { return length_ == 0; }
  241. void clear() {
  242. ptr_ = NULL;
  243. length_ = 0;
  244. }
  245. void set(const char* data, stringpiece_ssize_type len) {
  246. assert(len >= 0);
  247. ptr_ = data;
  248. length_ = len;
  249. }
  250. void set(const char* str) {
  251. ptr_ = str;
  252. if (str != NULL)
  253. length_ = CheckedSsizeTFromSizeT(strlen(str));
  254. else
  255. length_ = 0;
  256. }
  257. void set(const void* data, stringpiece_ssize_type len) {
  258. ptr_ = reinterpret_cast<const char*>(data);
  259. length_ = len;
  260. }
  261. char operator[](stringpiece_ssize_type i) const {
  262. assert(0 <= i);
  263. assert(i < length_);
  264. return ptr_[i];
  265. }
  266. void remove_prefix(stringpiece_ssize_type n) {
  267. assert(length_ >= n);
  268. ptr_ += n;
  269. length_ -= n;
  270. }
  271. void remove_suffix(stringpiece_ssize_type n) {
  272. assert(length_ >= n);
  273. length_ -= n;
  274. }
  275. // returns {-1, 0, 1}
  276. int compare(StringPiece x) const {
  277. const stringpiece_ssize_type min_size =
  278. length_ < x.length_ ? length_ : x.length_;
  279. int r = memcmp(ptr_, x.ptr_, min_size);
  280. if (r < 0) return -1;
  281. if (r > 0) return 1;
  282. if (length_ < x.length_) return -1;
  283. if (length_ > x.length_) return 1;
  284. return 0;
  285. }
  286. string as_string() const {
  287. return ToString();
  288. }
  289. // We also define ToString() here, since many other string-like
  290. // interfaces name the routine that converts to a C++ string
  291. // "ToString", and it's confusing to have the method that does that
  292. // for a StringPiece be called "as_string()". We also leave the
  293. // "as_string()" method defined here for existing code.
  294. string ToString() const {
  295. if (ptr_ == NULL) return string();
  296. return string(data(), size());
  297. }
  298. operator string() const {
  299. return ToString();
  300. }
  301. void CopyToString(string* target) const;
  302. void AppendToString(string* target) const;
  303. bool starts_with(StringPiece x) const {
  304. return (length_ >= x.length_) && (memcmp(ptr_, x.ptr_, x.length_) == 0);
  305. }
  306. bool ends_with(StringPiece x) const {
  307. return ((length_ >= x.length_) &&
  308. (memcmp(ptr_ + (length_-x.length_), x.ptr_, x.length_) == 0));
  309. }
  310. // Checks whether StringPiece starts with x and if so advances the beginning
  311. // of it to past the match. It's basically a shortcut for starts_with
  312. // followed by remove_prefix.
  313. bool Consume(StringPiece x);
  314. // Like above but for the end of the string.
  315. bool ConsumeFromEnd(StringPiece x);
  316. // standard STL container boilerplate
  317. typedef char value_type;
  318. typedef const char* pointer;
  319. typedef const char& reference;
  320. typedef const char& const_reference;
  321. typedef size_t size_type;
  322. typedef ptrdiff_t difference_type;
  323. static const size_type npos;
  324. typedef const char* const_iterator;
  325. typedef const char* iterator;
  326. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  327. typedef std::reverse_iterator<iterator> reverse_iterator;
  328. iterator begin() const { return ptr_; }
  329. iterator end() const { return ptr_ + length_; }
  330. const_reverse_iterator rbegin() const {
  331. return const_reverse_iterator(ptr_ + length_);
  332. }
  333. const_reverse_iterator rend() const {
  334. return const_reverse_iterator(ptr_);
  335. }
  336. stringpiece_ssize_type max_size() const { return length_; }
  337. stringpiece_ssize_type capacity() const { return length_; }
  338. // cpplint.py emits a false positive [build/include_what_you_use]
  339. stringpiece_ssize_type copy(char* buf, size_type n, size_type pos = 0) const; // NOLINT
  340. bool contains(StringPiece s) const;
  341. stringpiece_ssize_type find(StringPiece s, size_type pos = 0) const;
  342. stringpiece_ssize_type find(char c, size_type pos = 0) const;
  343. stringpiece_ssize_type rfind(StringPiece s, size_type pos = npos) const;
  344. stringpiece_ssize_type rfind(char c, size_type pos = npos) const;
  345. stringpiece_ssize_type find_first_of(StringPiece s, size_type pos = 0) const;
  346. stringpiece_ssize_type find_first_of(char c, size_type pos = 0) const {
  347. return find(c, pos);
  348. }
  349. stringpiece_ssize_type find_first_not_of(StringPiece s,
  350. size_type pos = 0) const;
  351. stringpiece_ssize_type find_first_not_of(char c, size_type pos = 0) const;
  352. stringpiece_ssize_type find_last_of(StringPiece s,
  353. size_type pos = npos) const;
  354. stringpiece_ssize_type find_last_of(char c, size_type pos = npos) const {
  355. return rfind(c, pos);
  356. }
  357. stringpiece_ssize_type find_last_not_of(StringPiece s,
  358. size_type pos = npos) const;
  359. stringpiece_ssize_type find_last_not_of(char c, size_type pos = npos) const;
  360. StringPiece substr(size_type pos, size_type n = npos) const;
  361. };
  362. // This large function is defined inline so that in a fairly common case where
  363. // one of the arguments is a literal, the compiler can elide a lot of the
  364. // following comparisons.
  365. inline bool operator==(StringPiece x, StringPiece y) {
  366. stringpiece_ssize_type len = x.size();
  367. if (len != y.size()) {
  368. return false;
  369. }
  370. return x.data() == y.data() || len <= 0 ||
  371. memcmp(x.data(), y.data(), len) == 0;
  372. }
  373. inline bool operator!=(StringPiece x, StringPiece y) {
  374. return !(x == y);
  375. }
  376. inline bool operator<(StringPiece x, StringPiece y) {
  377. const stringpiece_ssize_type min_size =
  378. x.size() < y.size() ? x.size() : y.size();
  379. const int r = memcmp(x.data(), y.data(), min_size);
  380. return (r < 0) || (r == 0 && x.size() < y.size());
  381. }
  382. inline bool operator>(StringPiece x, StringPiece y) {
  383. return y < x;
  384. }
  385. inline bool operator<=(StringPiece x, StringPiece y) {
  386. return !(x > y);
  387. }
  388. inline bool operator>=(StringPiece x, StringPiece y) {
  389. return !(x < y);
  390. }
  391. // allow StringPiece to be logged
  392. extern std::ostream& operator<<(std::ostream& o, StringPiece piece);
  393. } // namespace protobuf
  394. } // namespace google
  395. #endif // STRINGS_STRINGPIECE_H_