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