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.
6 // Copyright 2001 and onwards Google Inc.
7 // Author: Sanjay Ghemawat
9 // This code is a contribution of Google code, and the style used here is
10 // a compromise between the original Google code and the ICU coding guidelines.
11 // For example, data types are ICU-ified (size_t,int->int32_t),
12 // and API comments doxygen-ified, but function names and behavior are
13 // as in the original, if possible.
14 // Assertion-style error handling, not available in ICU, was changed to
15 // parameter "pinning" similar to UnicodeString.
17 // In addition, this is only a partial port of the original Google code,
18 // limited to what was needed so far. The (nearly) complete original code
19 // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
20 // (see ICU ticket 6765, r25517).
22 #ifndef __STRINGPIECE_H__
23 #define __STRINGPIECE_H__
27 * \brief C++ API: StringPiece: Read-only byte string wrapper class.
30 #include "unicode/utypes.h"
31 #include "unicode/uobject.h"
32 #include "unicode/std_string.h"
34 // Arghh! I wish C++ literals were "string".
39 * A string-like object that points to a sized piece of memory.
41 * We provide non-explicit singleton constructors so users can pass
42 * in a "const char*" or a "string" wherever a "StringPiece" is
45 * Functions or methods may use StringPiece parameters to accept either a
46 * "const char*" or a "string" value that will be implicitly converted to a
49 * Systematic usage of StringPiece is encouraged as it will reduce unnecessary
50 * conversions from "const char*" to "string" and back again.
54 class U_COMMON_API StringPiece : public UMemory {
61 * Default constructor, creates an empty StringPiece.
64 StringPiece() : ptr_(NULL), length_(0) { }
66 * Constructs from a NUL-terminated const char * pointer.
67 * @param str a NUL-terminated const char * pointer
70 StringPiece(const char* str);
73 * Constructs from a std::string.
76 StringPiece(const std::string& str)
77 : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
80 * Constructs from a const char * pointer and a specified length.
81 * @param offset a const char * pointer (need not be terminated)
82 * @param len the length of the string; must be non-negative
85 StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }
87 * Substring of another StringPiece.
88 * @param x the other StringPiece
89 * @param pos start position in x; must be non-negative and <= x.length().
92 StringPiece(const StringPiece& x, int32_t pos);
94 * Substring of another StringPiece.
95 * @param x the other StringPiece
96 * @param pos start position in x; must be non-negative and <= x.length().
97 * @param len length of the substring;
98 * must be non-negative and will be pinned to at most x.length() - pos.
101 StringPiece(const StringPiece& x, int32_t pos, int32_t len);
104 * Returns the string pointer. May be NULL if it is empty.
106 * data() may return a pointer to a buffer with embedded NULs, and the
107 * returned buffer may or may not be null terminated. Therefore it is
108 * typically a mistake to pass data() to a routine that expects a NUL
110 * @return the string pointer
113 const char* data() const { return ptr_; }
115 * Returns the string length. Same as length().
116 * @return the string length
119 int32_t size() const { return length_; }
121 * Returns the string length. Same as size().
122 * @return the string length
125 int32_t length() const { return length_; }
127 * Returns whether the string is empty.
128 * @return TRUE if the string is empty
131 UBool empty() const { return length_ == 0; }
134 * Sets to an empty string.
137 void clear() { ptr_ = NULL; length_ = 0; }
140 * Reset the stringpiece to refer to new data.
141 * @param xdata pointer the new string data. Need not be nul terminated.
142 * @param len the length of the new data
145 void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; }
148 * Reset the stringpiece to refer to new data.
149 * @param str a pointer to a NUL-terminated string.
152 void set(const char* str);
155 * Removes the first n string units.
156 * @param n prefix length, must be non-negative and <=length()
159 void remove_prefix(int32_t n) {
170 * Removes the last n string units.
171 * @param n suffix length, must be non-negative and <=length()
174 void remove_suffix(int32_t n) {
185 * Maximum integer, used as a default value for substring methods.
188 static const int32_t npos; // = 0x7fffffff;
191 * Returns a substring of this StringPiece.
192 * @param pos start position; must be non-negative and <= length().
193 * @param len length of the substring;
194 * must be non-negative and will be pinned to at most length() - pos.
195 * @return the substring StringPiece
198 StringPiece substr(int32_t pos, int32_t len = npos) const {
199 return StringPiece(*this, pos, len);
204 * Global operator == for StringPiece
205 * @param x The first StringPiece to compare.
206 * @param y The second StringPiece to compare.
207 * @return TRUE if the string data is equal
210 U_EXPORT UBool U_EXPORT2
211 operator==(const StringPiece& x, const StringPiece& y);
214 * Global operator != for StringPiece
215 * @param x The first StringPiece to compare.
216 * @param y The second StringPiece to compare.
217 * @return TRUE if the string data is not equal
220 inline UBool operator!=(const StringPiece& x, const StringPiece& y) {
226 #endif // __STRINGPIECE_H__