stringpiece.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright (C) 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. #include "unicode/uobject.h"
  28. #include "unicode/std_string.h"
  29. // Arghh! I wish C++ literals were "string".
  30. U_NAMESPACE_BEGIN
  31. /**
  32. * A string-like object that points to a sized piece of memory.
  33. *
  34. * We provide non-explicit singleton constructors so users can pass
  35. * in a "const char*" or a "string" wherever a "StringPiece" is
  36. * expected.
  37. *
  38. * Functions or methods may use StringPiece parameters to accept either a
  39. * "const char*" or a "string" value that will be implicitly converted to a
  40. * StringPiece.
  41. *
  42. * Systematic usage of StringPiece is encouraged as it will reduce unnecessary
  43. * conversions from "const char*" to "string" and back again.
  44. *
  45. * @stable ICU 4.2
  46. */
  47. class U_COMMON_API StringPiece : public UMemory {
  48. private:
  49. const char* ptr_;
  50. int32_t length_;
  51. public:
  52. /**
  53. * Default constructor, creates an empty StringPiece.
  54. * @stable ICU 4.2
  55. */
  56. StringPiece() : ptr_(NULL), length_(0) { }
  57. /**
  58. * Constructs from a NUL-terminated const char * pointer.
  59. * @param str a NUL-terminated const char * pointer
  60. * @stable ICU 4.2
  61. */
  62. StringPiece(const char* str);
  63. #if U_HAVE_STD_STRING
  64. /**
  65. * Constructs from a std::string.
  66. * @stable ICU 4.2
  67. */
  68. StringPiece(const std::string& str)
  69. : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
  70. #endif
  71. /**
  72. * Constructs from a const char * pointer and a specified length.
  73. * @param offset a const char * pointer (need not be terminated)
  74. * @param len the length of the string; must be non-negative
  75. * @stable ICU 4.2
  76. */
  77. StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }
  78. /**
  79. * Substring of another StringPiece.
  80. * @param x the other StringPiece
  81. * @param pos start position in x; must be non-negative and <= x.length().
  82. * @stable ICU 4.2
  83. */
  84. StringPiece(const StringPiece& x, int32_t pos);
  85. /**
  86. * Substring of another StringPiece.
  87. * @param x the other StringPiece
  88. * @param pos start position in x; must be non-negative and <= x.length().
  89. * @param len length of the substring;
  90. * must be non-negative and will be pinned to at most x.length() - pos.
  91. * @stable ICU 4.2
  92. */
  93. StringPiece(const StringPiece& x, int32_t pos, int32_t len);
  94. /**
  95. * Returns the string pointer. May be NULL if it is empty.
  96. *
  97. * data() may return a pointer to a buffer with embedded NULs, and the
  98. * returned buffer may or may not be null terminated. Therefore it is
  99. * typically a mistake to pass data() to a routine that expects a NUL
  100. * terminated string.
  101. * @return the string pointer
  102. * @stable ICU 4.2
  103. */
  104. const char* data() const { return ptr_; }
  105. /**
  106. * Returns the string length. Same as length().
  107. * @return the string length
  108. * @stable ICU 4.2
  109. */
  110. int32_t size() const { return length_; }
  111. /**
  112. * Returns the string length. Same as size().
  113. * @return the string length
  114. * @stable ICU 4.2
  115. */
  116. int32_t length() const { return length_; }
  117. /**
  118. * Returns whether the string is empty.
  119. * @return TRUE if the string is empty
  120. * @stable ICU 4.2
  121. */
  122. UBool empty() const { return length_ == 0; }
  123. /**
  124. * Sets to an empty string.
  125. * @stable ICU 4.2
  126. */
  127. void clear() { ptr_ = NULL; length_ = 0; }
  128. /**
  129. * Reset the stringpiece to refer to new data.
  130. * @param xdata pointer the new string data. Need not be nul terminated.
  131. * @param len the length of the new data
  132. * @stable ICU 4.8
  133. */
  134. void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; }
  135. /**
  136. * Reset the stringpiece to refer to new data.
  137. * @param str a pointer to a NUL-terminated string.
  138. * @stable ICU 4.8
  139. */
  140. void set(const char* str);
  141. /**
  142. * Removes the first n string units.
  143. * @param n prefix length, must be non-negative and <=length()
  144. * @stable ICU 4.2
  145. */
  146. void remove_prefix(int32_t n) {
  147. if (n >= 0) {
  148. if (n > length_) {
  149. n = length_;
  150. }
  151. ptr_ += n;
  152. length_ -= n;
  153. }
  154. }
  155. /**
  156. * Removes the last n string units.
  157. * @param n suffix length, must be non-negative and <=length()
  158. * @stable ICU 4.2
  159. */
  160. void remove_suffix(int32_t n) {
  161. if (n >= 0) {
  162. if (n <= length_) {
  163. length_ -= n;
  164. } else {
  165. length_ = 0;
  166. }
  167. }
  168. }
  169. /**
  170. * Maximum integer, used as a default value for substring methods.
  171. * @stable ICU 4.2
  172. */
  173. static const int32_t npos; // = 0x7fffffff;
  174. /**
  175. * Returns a substring of this StringPiece.
  176. * @param pos start position; must be non-negative and <= length().
  177. * @param len length of the substring;
  178. * must be non-negative and will be pinned to at most length() - pos.
  179. * @return the substring StringPiece
  180. * @stable ICU 4.2
  181. */
  182. StringPiece substr(int32_t pos, int32_t len = npos) const {
  183. return StringPiece(*this, pos, len);
  184. }
  185. };
  186. /**
  187. * Global operator == for StringPiece
  188. * @param x The first StringPiece to compare.
  189. * @param y The second StringPiece to compare.
  190. * @return TRUE if the string data is equal
  191. * @stable ICU 4.8
  192. */
  193. U_EXPORT UBool U_EXPORT2
  194. operator==(const StringPiece& x, const StringPiece& y);
  195. /**
  196. * Global operator != for StringPiece
  197. * @param x The first StringPiece to compare.
  198. * @param y The second StringPiece to compare.
  199. * @return TRUE if the string data is not equal
  200. * @stable ICU 4.8
  201. */
  202. inline UBool operator!=(const StringPiece& x, const StringPiece& y) {
  203. return !(x == y);
  204. }
  205. U_NAMESPACE_END
  206. #endif // __STRINGPIECE_H__