]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/unicode/stringpiece.h
d525f43288b2049bfa82b70c66546476d5308365
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.
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".
36 #if U_SHOW_CPLUSPLUS_API
40 * A string-like object that points to a sized piece of memory.
42 * We provide non-explicit singleton constructors so users can pass
43 * in a "const char*" or a "string" wherever a "StringPiece" is
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
50 * Systematic usage of StringPiece is encouraged as it will reduce unnecessary
51 * conversions from "const char*" to "string" and back again.
55 class U_COMMON_API StringPiece
: public UMemory
{
62 * Default constructor, creates an empty StringPiece.
65 StringPiece() : ptr_(NULL
), length_(0) { }
67 * Constructs from a NUL-terminated const char * pointer.
68 * @param str a NUL-terminated const char * pointer
71 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())) { }
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
84 StringPiece(const char* offset
, int32_t len
) : ptr_(offset
), length_(len
) { }
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().
91 StringPiece(const StringPiece
& x
, int32_t pos
);
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.
100 StringPiece(const StringPiece
& x
, int32_t pos
, int32_t len
);
103 * Returns the string pointer. May be NULL if it is empty.
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
109 * @return the string pointer
112 const char* data() const { return ptr_
; }
114 * Returns the string length. Same as length().
115 * @return the string length
118 int32_t size() const { return length_
; }
120 * Returns the string length. Same as size().
121 * @return the string length
124 int32_t length() const { return length_
; }
126 * Returns whether the string is empty.
127 * @return TRUE if the string is empty
130 UBool
empty() const { return length_
== 0; }
133 * Sets to an empty string.
136 void clear() { ptr_
= NULL
; length_
= 0; }
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
144 void set(const char* xdata
, int32_t len
) { ptr_
= xdata
; length_
= len
; }
147 * Reset the stringpiece to refer to new data.
148 * @param str a pointer to a NUL-terminated string.
151 void set(const char* str
);
154 * Removes the first n string units.
155 * @param n prefix length, must be non-negative and <=length()
158 void remove_prefix(int32_t n
) {
169 * Removes the last n string units.
170 * @param n suffix length, must be non-negative and <=length()
173 void remove_suffix(int32_t n
) {
184 * Maximum integer, used as a default value for substring methods.
187 static const int32_t npos
; // = 0x7fffffff;
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
197 StringPiece
substr(int32_t pos
, int32_t len
= npos
) const {
198 return StringPiece(*this, pos
, len
);
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
209 U_EXPORT UBool U_EXPORT2
210 operator==(const StringPiece
& x
, const StringPiece
& y
);
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
219 inline UBool
operator!=(const StringPiece
& x
, const StringPiece
& y
) {
224 #endif // U_SHOW_CPLUSPLUS_API
226 #endif // __STRINGPIECE_H__