]>
git.saurik.com Git - apple/icu.git/blob - 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.
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"
32 #if U_SHOW_CPLUSPLUS_API
35 #include <type_traits>
37 #include "unicode/uobject.h"
38 #include "unicode/std_string.h"
40 // Arghh! I wish C++ literals were "string".
45 * A string-like object that points to a sized piece of memory.
47 * We provide non-explicit singleton constructors so users can pass
48 * in a "const char*" or a "string" wherever a "StringPiece" is
51 * Functions or methods may use StringPiece parameters to accept either a
52 * "const char*" or a "string" value that will be implicitly converted to a
55 * Systematic usage of StringPiece is encouraged as it will reduce unnecessary
56 * conversions from "const char*" to "string" and back again.
60 class U_COMMON_API StringPiece
: public UMemory
{
67 * Default constructor, creates an empty StringPiece.
70 StringPiece() : ptr_(NULL
), length_(0) { }
72 * Constructs from a NUL-terminated const char * pointer.
73 * @param str a NUL-terminated const char * pointer
76 StringPiece(const char* str
);
78 * Constructs from a std::string.
81 StringPiece(const std::string
& str
)
82 : ptr_(str
.data()), length_(static_cast<int32_t>(str
.size())) { }
83 #ifndef U_HIDE_DRAFT_API
85 * Constructs from some other implementation of a string piece class, from any
86 * C++ record type that has these two methods:
90 * struct OtherStringPieceClass {
97 * The other string piece class will typically be std::string_view from C++17
98 * or absl::string_view from Abseil.
100 * @param str the other string piece
103 template <typename T
,
104 typename
= typename
std::enable_if
<
105 std::is_same
<decltype(T().data()), const char*>::value
&&
106 std::is_same
<decltype(T().size()), size_t>::value
>::type
>
108 : ptr_(str
.data()), length_(static_cast<int32_t>(str
.size())) {}
109 #endif // U_HIDE_DRAFT_API
111 * Constructs from a const char * pointer and a specified length.
112 * @param offset a const char * pointer (need not be terminated)
113 * @param len the length of the string; must be non-negative
116 StringPiece(const char* offset
, int32_t len
) : ptr_(offset
), length_(len
) { }
118 * Substring of another StringPiece.
119 * @param x the other StringPiece
120 * @param pos start position in x; must be non-negative and <= x.length().
123 StringPiece(const StringPiece
& x
, int32_t pos
);
125 * Substring of another StringPiece.
126 * @param x the other StringPiece
127 * @param pos start position in x; must be non-negative and <= x.length().
128 * @param len length of the substring;
129 * must be non-negative and will be pinned to at most x.length() - pos.
132 StringPiece(const StringPiece
& x
, int32_t pos
, int32_t len
);
135 * Returns the string pointer. May be NULL if it is empty.
137 * data() may return a pointer to a buffer with embedded NULs, and the
138 * returned buffer may or may not be null terminated. Therefore it is
139 * typically a mistake to pass data() to a routine that expects a NUL
141 * @return the string pointer
144 const char* data() const { return ptr_
; }
146 * Returns the string length. Same as length().
147 * @return the string length
150 int32_t size() const { return length_
; }
152 * Returns the string length. Same as size().
153 * @return the string length
156 int32_t length() const { return length_
; }
158 * Returns whether the string is empty.
159 * @return TRUE if the string is empty
162 UBool
empty() const { return length_
== 0; }
165 * Sets to an empty string.
168 void clear() { ptr_
= NULL
; length_
= 0; }
171 * Reset the stringpiece to refer to new data.
172 * @param xdata pointer the new string data. Need not be nul terminated.
173 * @param len the length of the new data
176 void set(const char* xdata
, int32_t len
) { ptr_
= xdata
; length_
= len
; }
179 * Reset the stringpiece to refer to new data.
180 * @param str a pointer to a NUL-terminated string.
183 void set(const char* str
);
186 * Removes the first n string units.
187 * @param n prefix length, must be non-negative and <=length()
190 void remove_prefix(int32_t n
) {
201 * Removes the last n string units.
202 * @param n suffix length, must be non-negative and <=length()
205 void remove_suffix(int32_t n
) {
216 * Maximum integer, used as a default value for substring methods.
219 static const int32_t npos
; // = 0x7fffffff;
222 * Returns a substring of this StringPiece.
223 * @param pos start position; must be non-negative and <= length().
224 * @param len length of the substring;
225 * must be non-negative and will be pinned to at most length() - pos.
226 * @return the substring StringPiece
229 StringPiece
substr(int32_t pos
, int32_t len
= npos
) const {
230 return StringPiece(*this, pos
, len
);
235 * Global operator == for StringPiece
236 * @param x The first StringPiece to compare.
237 * @param y The second StringPiece to compare.
238 * @return TRUE if the string data is equal
241 U_EXPORT UBool U_EXPORT2
242 operator==(const StringPiece
& x
, const StringPiece
& y
);
245 * Global operator != for StringPiece
246 * @param x The first StringPiece to compare.
247 * @param y The second StringPiece to compare.
248 * @return TRUE if the string data is not equal
251 inline UBool
operator!=(const StringPiece
& x
, const StringPiece
& y
) {
257 #endif /* U_SHOW_CPLUSPLUS_API */
259 #endif // __STRINGPIECE_H__