]>
git.saurik.com Git - apple/icu.git/blob - icuSources/common/unicode/stringpiece.h
1 // Copyright (C) 2009-2012, International Business Machines
2 // Corporation and others. All Rights Reserved.
4 // Copyright 2001 and onwards Google Inc.
5 // Author: Sanjay Ghemawat
7 // This code is a contribution of Google code, and the style used here is
8 // a compromise between the original Google code and the ICU coding guidelines.
9 // For example, data types are ICU-ified (size_t,int->int32_t),
10 // and API comments doxygen-ified, but function names and behavior are
11 // as in the original, if possible.
12 // Assertion-style error handling, not available in ICU, was changed to
13 // parameter "pinning" similar to UnicodeString.
15 // In addition, this is only a partial port of the original Google code,
16 // limited to what was needed so far. The (nearly) complete original code
17 // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
18 // (see ICU ticket 6765, r25517).
20 #ifndef __STRINGPIECE_H__
21 #define __STRINGPIECE_H__
25 * \brief C++ API: StringPiece: Read-only byte string wrapper class.
28 #include "unicode/utypes.h"
29 #include "unicode/uobject.h"
30 #include "unicode/std_string.h"
32 // Arghh! I wish C++ literals were "string".
37 * A string-like object that points to a sized piece of memory.
39 * We provide non-explicit singleton constructors so users can pass
40 * in a "const char*" or a "string" wherever a "StringPiece" is
43 * Functions or methods may use const StringPiece& parameters to accept either
44 * a "const char*" or a "string" value that will be implicitly converted to
47 * Systematic usage of StringPiece is encouraged as it will reduce unnecessary
48 * conversions from "const char*" to "string" and back again.
52 class U_COMMON_API StringPiece
: public UMemory
{
59 * Default constructor, creates an empty StringPiece.
62 StringPiece() : ptr_(NULL
), length_(0) { }
64 * Constructs from a NUL-terminated const char * pointer.
65 * @param str a NUL-terminated const char * pointer
68 StringPiece(const char* str
);
71 * Constructs from a std::string.
74 StringPiece(const std::string
& str
)
75 : ptr_(str
.data()), length_(static_cast<int32_t>(str
.size())) { }
78 * Constructs from a const char * pointer and a specified length.
79 * @param offset a const char * pointer (need not be terminated)
80 * @param len the length of the string; must be non-negative
83 StringPiece(const char* offset
, int32_t len
) : ptr_(offset
), length_(len
) { }
85 * Substring of another StringPiece.
86 * @param x the other StringPiece
87 * @param pos start position in x; must be non-negative and <= x.length().
90 StringPiece(const StringPiece
& x
, int32_t pos
);
92 * Substring of another StringPiece.
93 * @param x the other StringPiece
94 * @param pos start position in x; must be non-negative and <= x.length().
95 * @param len length of the substring;
96 * must be non-negative and will be pinned to at most x.length() - pos.
99 StringPiece(const StringPiece
& x
, int32_t pos
, int32_t len
);
102 * Returns the string pointer. May be NULL if it is empty.
104 * data() may return a pointer to a buffer with embedded NULs, and the
105 * returned buffer may or may not be null terminated. Therefore it is
106 * typically a mistake to pass data() to a routine that expects a NUL
108 * @return the string pointer
111 const char* data() const { return ptr_
; }
113 * Returns the string length. Same as length().
114 * @return the string length
117 int32_t size() const { return length_
; }
119 * Returns the string length. Same as size().
120 * @return the string length
123 int32_t length() const { return length_
; }
125 * Returns whether the string is empty.
126 * @return TRUE if the string is empty
129 UBool
empty() const { return length_
== 0; }
132 * Sets to an empty string.
135 void clear() { ptr_
= NULL
; length_
= 0; }
138 * Reset the stringpiece to refer to new data.
139 * @param xdata pointer the new string data. Need not be nul terminated.
140 * @param len the length of the new data
143 void set(const char* xdata
, int32_t len
) { ptr_
= xdata
; length_
= len
; }
146 * Reset the stringpiece to refer to new data.
147 * @param str a pointer to a NUL-terminated string.
150 void set(const char* str
);
153 * Removes the first n string units.
154 * @param n prefix length, must be non-negative and <=length()
157 void remove_prefix(int32_t n
) {
168 * Removes the last n string units.
169 * @param n suffix length, must be non-negative and <=length()
172 void remove_suffix(int32_t n
) {
183 * Maximum integer, used as a default value for substring methods.
186 static const int32_t npos
= 0x7fffffff;
189 * Returns a substring of this StringPiece.
190 * @param pos start position; must be non-negative and <= length().
191 * @param len length of the substring;
192 * must be non-negative and will be pinned to at most length() - pos.
193 * @return the substring StringPiece
196 StringPiece
substr(int32_t pos
, int32_t len
= npos
) const {
197 return StringPiece(*this, pos
, len
);
202 * Global operator == for StringPiece
203 * @param x The first StringPiece to compare.
204 * @param y The second StringPiece to compare.
205 * @return TRUE if the string data is equal
208 U_EXPORT UBool U_EXPORT2
209 operator==(const StringPiece
& x
, const StringPiece
& y
);
212 * Global operator != for StringPiece
213 * @param x The first StringPiece to compare.
214 * @param y The second StringPiece to compare.
215 * @return TRUE if the string data is not equal
218 inline UBool
operator!=(const StringPiece
& x
, const StringPiece
& y
) {
224 #endif // __STRINGPIECE_H__