stringpiece.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. // Copyright (C) 2009-2013, International Business Machines
  4. // Corporation and others. All Rights Reserved.
  5. //
  6. // Copyright 2001 and onwards Google Inc.
  7. // Author: Sanjay Ghemawat
  8. // This code is a contribution of Google code, and the style used here is
  9. // a compromise between the original Google code and the ICU coding guidelines.
  10. // For example, data types are ICU-ified (size_t,int->int32_t),
  11. // and API comments doxygen-ified, but function names and behavior are
  12. // as in the original, if possible.
  13. // Assertion-style error handling, not available in ICU, was changed to
  14. // parameter "pinning" similar to UnicodeString.
  15. //
  16. // In addition, this is only a partial port of the original Google code,
  17. // limited to what was needed so far. The (nearly) complete original code
  18. // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
  19. // (see ICU ticket 6765, r25517).
  20. #ifndef __STRINGPIECE_H__
  21. #define __STRINGPIECE_H__
  22. /**
  23. * \file
  24. * \brief C++ API: StringPiece: Read-only byte string wrapper class.
  25. */
  26. #include "unicode/utypes.h"
  27. #if U_SHOW_CPLUSPLUS_API
  28. #include <cstddef>
  29. #include <type_traits>
  30. #include "unicode/uobject.h"
  31. #include "unicode/std_string.h"
  32. // Arghh! I wish C++ literals were "string".
  33. U_NAMESPACE_BEGIN
  34. /**
  35. * A string-like object that points to a sized piece of memory.
  36. *
  37. * We provide non-explicit singleton constructors so users can pass
  38. * in a "const char*" or a "string" wherever a "StringPiece" is
  39. * expected.
  40. *
  41. * Functions or methods may use StringPiece parameters to accept either a
  42. * "const char*" or a "string" value that will be implicitly converted to a
  43. * StringPiece.
  44. *
  45. * Systematic usage of StringPiece is encouraged as it will reduce unnecessary
  46. * conversions from "const char*" to "string" and back again.
  47. *
  48. * @stable ICU 4.2
  49. */
  50. class U_COMMON_API StringPiece : public UMemory {
  51. private:
  52. const char* ptr_;
  53. int32_t length_;
  54. public:
  55. /**
  56. * Default constructor, creates an empty StringPiece.
  57. * @stable ICU 4.2
  58. */
  59. StringPiece() : ptr_(nullptr), length_(0) { }
  60. /**
  61. * Constructs from a NUL-terminated const char * pointer.
  62. * @param str a NUL-terminated const char * pointer
  63. * @stable ICU 4.2
  64. */
  65. StringPiece(const char* str);
  66. #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
  67. /**
  68. * Constructs from a NUL-terminated const char8_t * pointer.
  69. * @param str a NUL-terminated const char8_t * pointer
  70. * @stable ICU 67
  71. */
  72. StringPiece(const char8_t* str) : StringPiece(reinterpret_cast<const char*>(str)) {}
  73. #endif
  74. /**
  75. * Constructs an empty StringPiece.
  76. * Needed for type disambiguation from multiple other overloads.
  77. * @param p nullptr
  78. * @stable ICU 67
  79. */
  80. StringPiece(std::nullptr_t p) : ptr_(p), length_(0) {}
  81. /**
  82. * Constructs from a std::string.
  83. * @stable ICU 4.2
  84. */
  85. StringPiece(const std::string& str)
  86. : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
  87. #if defined(__cpp_lib_char8_t) || defined(U_IN_DOXYGEN)
  88. /**
  89. * Constructs from a std::u8string.
  90. * @stable ICU 67
  91. */
  92. StringPiece(const std::u8string& str)
  93. : ptr_(reinterpret_cast<const char*>(str.data())),
  94. length_(static_cast<int32_t>(str.size())) { }
  95. #endif
  96. /**
  97. * Constructs from some other implementation of a string piece class, from any
  98. * C++ record type that has these two methods:
  99. *
  100. * \code{.cpp}
  101. *
  102. * struct OtherStringPieceClass {
  103. * const char* data(); // or const char8_t*
  104. * size_t size();
  105. * };
  106. *
  107. * \endcode
  108. *
  109. * The other string piece class will typically be std::string_view from C++17
  110. * or absl::string_view from Abseil.
  111. *
  112. * Starting with C++20, data() may also return a const char8_t* pointer,
  113. * as from std::u8string_view.
  114. *
  115. * @param str the other string piece
  116. * @stable ICU 65
  117. */
  118. template <typename T,
  119. typename = std::enable_if_t<
  120. (std::is_same_v<decltype(T().data()), const char*>
  121. #if defined(__cpp_char8_t)
  122. || std::is_same_v<decltype(T().data()), const char8_t*>
  123. #endif
  124. ) &&
  125. std::is_same_v<decltype(T().size()), size_t>>>
  126. StringPiece(T str)
  127. : ptr_(reinterpret_cast<const char*>(str.data())),
  128. length_(static_cast<int32_t>(str.size())) {}
  129. /**
  130. * Constructs from a const char * pointer and a specified length.
  131. * @param offset a const char * pointer (need not be terminated)
  132. * @param len the length of the string; must be non-negative
  133. * @stable ICU 4.2
  134. */
  135. StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }
  136. #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
  137. /**
  138. * Constructs from a const char8_t * pointer and a specified length.
  139. * @param str a const char8_t * pointer (need not be terminated)
  140. * @param len the length of the string; must be non-negative
  141. * @stable ICU 67
  142. */
  143. StringPiece(const char8_t* str, int32_t len) :
  144. StringPiece(reinterpret_cast<const char*>(str), len) {}
  145. #endif
  146. /**
  147. * Substring of another StringPiece.
  148. * @param x the other StringPiece
  149. * @param pos start position in x; must be non-negative and <= x.length().
  150. * @stable ICU 4.2
  151. */
  152. StringPiece(const StringPiece& x, int32_t pos);
  153. /**
  154. * Substring of another StringPiece.
  155. * @param x the other StringPiece
  156. * @param pos start position in x; must be non-negative and <= x.length().
  157. * @param len length of the substring;
  158. * must be non-negative and will be pinned to at most x.length() - pos.
  159. * @stable ICU 4.2
  160. */
  161. StringPiece(const StringPiece& x, int32_t pos, int32_t len);
  162. /**
  163. * Returns the string pointer. May be nullptr if it is empty.
  164. *
  165. * data() may return a pointer to a buffer with embedded NULs, and the
  166. * returned buffer may or may not be null terminated. Therefore it is
  167. * typically a mistake to pass data() to a routine that expects a NUL
  168. * terminated string.
  169. * @return the string pointer
  170. * @stable ICU 4.2
  171. */
  172. const char* data() const { return ptr_; }
  173. /**
  174. * Returns the string length. Same as length().
  175. * @return the string length
  176. * @stable ICU 4.2
  177. */
  178. int32_t size() const { return length_; }
  179. /**
  180. * Returns the string length. Same as size().
  181. * @return the string length
  182. * @stable ICU 4.2
  183. */
  184. int32_t length() const { return length_; }
  185. /**
  186. * Returns whether the string is empty.
  187. * @return true if the string is empty
  188. * @stable ICU 4.2
  189. */
  190. UBool empty() const { return length_ == 0; }
  191. /**
  192. * Sets to an empty string.
  193. * @stable ICU 4.2
  194. */
  195. void clear() { ptr_ = nullptr; length_ = 0; }
  196. /**
  197. * Reset the stringpiece to refer to new data.
  198. * @param xdata pointer the new string data. Need not be nul terminated.
  199. * @param len the length of the new data
  200. * @stable ICU 4.8
  201. */
  202. void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; }
  203. /**
  204. * Reset the stringpiece to refer to new data.
  205. * @param str a pointer to a NUL-terminated string.
  206. * @stable ICU 4.8
  207. */
  208. void set(const char* str);
  209. #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
  210. /**
  211. * Resets the stringpiece to refer to new data.
  212. * @param xdata pointer the new string data. Need not be NUL-terminated.
  213. * @param len the length of the new data
  214. * @stable ICU 67
  215. */
  216. inline void set(const char8_t* xdata, int32_t len) {
  217. set(reinterpret_cast<const char*>(xdata), len);
  218. }
  219. /**
  220. * Resets the stringpiece to refer to new data.
  221. * @param str a pointer to a NUL-terminated string.
  222. * @stable ICU 67
  223. */
  224. inline void set(const char8_t* str) {
  225. set(reinterpret_cast<const char*>(str));
  226. }
  227. #endif
  228. /**
  229. * Removes the first n string units.
  230. * @param n prefix length, must be non-negative and <=length()
  231. * @stable ICU 4.2
  232. */
  233. void remove_prefix(int32_t n) {
  234. if (n >= 0) {
  235. if (n > length_) {
  236. n = length_;
  237. }
  238. ptr_ += n;
  239. length_ -= n;
  240. }
  241. }
  242. /**
  243. * Removes the last n string units.
  244. * @param n suffix length, must be non-negative and <=length()
  245. * @stable ICU 4.2
  246. */
  247. void remove_suffix(int32_t n) {
  248. if (n >= 0) {
  249. if (n <= length_) {
  250. length_ -= n;
  251. } else {
  252. length_ = 0;
  253. }
  254. }
  255. }
  256. /**
  257. * Searches the StringPiece for the given search string (needle);
  258. * @param needle The string for which to search.
  259. * @param offset Where to start searching within this string (haystack).
  260. * @return The offset of needle in haystack, or -1 if not found.
  261. * @stable ICU 67
  262. */
  263. int32_t find(StringPiece needle, int32_t offset);
  264. /**
  265. * Compares this StringPiece with the other StringPiece, with semantics
  266. * similar to std::string::compare().
  267. * @param other The string to compare to.
  268. * @return below zero if this < other; above zero if this > other; 0 if this == other.
  269. * @stable ICU 67
  270. */
  271. int32_t compare(StringPiece other);
  272. /**
  273. * Maximum integer, used as a default value for substring methods.
  274. * @stable ICU 4.2
  275. */
  276. static const int32_t npos; // = 0x7fffffff;
  277. /**
  278. * Returns a substring of this StringPiece.
  279. * @param pos start position; must be non-negative and <= length().
  280. * @param len length of the substring;
  281. * must be non-negative and will be pinned to at most length() - pos.
  282. * @return the substring StringPiece
  283. * @stable ICU 4.2
  284. */
  285. StringPiece substr(int32_t pos, int32_t len = npos) const {
  286. return StringPiece(*this, pos, len);
  287. }
  288. };
  289. /**
  290. * Global operator == for StringPiece
  291. * @param x The first StringPiece to compare.
  292. * @param y The second StringPiece to compare.
  293. * @return true if the string data is equal
  294. * @stable ICU 4.8
  295. */
  296. U_EXPORT UBool U_EXPORT2
  297. operator==(const StringPiece& x, const StringPiece& y);
  298. /**
  299. * Global operator != for StringPiece
  300. * @param x The first StringPiece to compare.
  301. * @param y The second StringPiece to compare.
  302. * @return true if the string data is not equal
  303. * @stable ICU 4.8
  304. */
  305. inline bool operator!=(const StringPiece& x, const StringPiece& y) {
  306. return !(x == y);
  307. }
  308. U_NAMESPACE_END
  309. #endif /* U_SHOW_CPLUSPLUS_API */
  310. #endif // __STRINGPIECE_H__