stringpiece.h 10 KB

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