ICU 78.2 78.2

stringpiece.h
Go to the documentation of this file.
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
30 #include "unicode/utypes.h"
31
32 #if U_SHOW_CPLUSPLUS_API
33
34 #include <cstddef>
35 #include <string_view>
36 #include <type_traits>
37
38 #include "unicode/uobject.h"
39 #include "unicode/std_string.h"
40
41 // Arghh! I wish C++ literals were "string".
42
43 U_NAMESPACE_BEGIN
44
61 class U_COMMON_API StringPiece : public UMemory {
62 private:
63 const char* ptr_;
64 int32_t length_;
65
66 public:
71 StringPiece() : ptr_(nullptr), length_(0) { }
72
78 StringPiece(const char* str);
79 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
85 StringPiece(const char8_t* str) : StringPiece(reinterpret_cast<const char*>(str)) {}
86 #endif
93 StringPiece(std::nullptr_t p) : ptr_(p), length_(0) {}
94
99 StringPiece(const std::string& str)
100 : ptr_(str.data()), length_(static_cast<int32_t>(str.size())) { }
101 #if defined(__cpp_lib_char8_t) || defined(U_IN_DOXYGEN)
106 StringPiece(const std::u8string& str)
107 : ptr_(reinterpret_cast<const char*>(str.data())),
108 length_(static_cast<int32_t>(str.size())) { }
109 #endif
110
133 template <typename T,
134 typename = std::enable_if_t<
135 (std::is_same_v<decltype(T().data()), const char*>
136 #if defined(__cpp_char8_t)
137 || std::is_same_v<decltype(T().data()), const char8_t*>
138 #endif
139 ) &&
140 std::is_same_v<decltype(T().size()), size_t>>>
141 StringPiece(T str)
142 : ptr_(reinterpret_cast<const char*>(str.data())),
143 length_(static_cast<int32_t>(str.size())) {}
144
151 StringPiece(const char* offset, int32_t len) : ptr_(offset), length_(len) { }
152 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
159 StringPiece(const char8_t* str, int32_t len) :
160 StringPiece(reinterpret_cast<const char*>(str), len) {}
161 #endif
162
169 StringPiece(const StringPiece& x, int32_t pos);
178 StringPiece(const StringPiece& x, int32_t pos, int32_t len);
179
180 #ifndef U_HIDE_INTERNAL_API
185 inline operator std::string_view() const {
186 return {data(), static_cast<std::string_view::size_type>(size())};
187 }
188 #endif // U_HIDE_INTERNAL_API
189
200 const char* data() const { return ptr_; }
206 int32_t size() const { return length_; }
212 int32_t length() const { return length_; }
218 UBool empty() const { return length_ == 0; }
219
224 void clear() { ptr_ = nullptr; length_ = 0; }
225
232 void set(const char* xdata, int32_t len) { ptr_ = xdata; length_ = len; }
233
239 void set(const char* str);
240
241 #if defined(__cpp_char8_t) || defined(U_IN_DOXYGEN)
248 inline void set(const char8_t* xdata, int32_t len) {
249 set(reinterpret_cast<const char*>(xdata), len);
250 }
251
257 inline void set(const char8_t* str) {
258 set(reinterpret_cast<const char*>(str));
259 }
260 #endif
261
267 void remove_prefix(int32_t n) {
268 if (n >= 0) {
269 if (n > length_) {
270 n = length_;
271 }
272 ptr_ += n;
273 length_ -= n;
274 }
275 }
276
282 void remove_suffix(int32_t n) {
283 if (n >= 0) {
284 if (n <= length_) {
285 length_ -= n;
286 } else {
287 length_ = 0;
288 }
289 }
290 }
291
299 int32_t find(StringPiece needle, int32_t offset);
300
308 int32_t compare(StringPiece other);
309
314 static const int32_t npos; // = 0x7fffffff;
315
324 StringPiece substr(int32_t pos, int32_t len = npos) const {
325 return StringPiece(*this, pos, len);
326 }
327 };
328
336 U_COMMON_API UBool U_EXPORT2
337 operator==(const StringPiece& x, const StringPiece& y);
338
346 inline bool operator!=(const StringPiece& x, const StringPiece& y) {
347 return !(x == y);
348 }
349
350 U_NAMESPACE_END
351
352 #endif /* U_SHOW_CPLUSPLUS_API */
353
354 #endif // __STRINGPIECE_H__
icu::StringPiece
A string-like object that points to a sized piece of memory.
Definition: stringpiece.h:61
icu::StringPiece::substr
StringPiece substr(int32_t pos, int32_t len=npos) const
Returns a substring of this StringPiece.
Definition: stringpiece.h:324
icu::StringPiece::empty
UBool empty() const
Returns whether the string is empty.
Definition: stringpiece.h:218
icu::StringPiece::StringPiece
StringPiece(const char *str)
Constructs from a NUL-terminated const char * pointer.
icu::StringPiece::set
void set(const char *str)
Reset the stringpiece to refer to new data.
icu::StringPiece::StringPiece
StringPiece(const StringPiece &x, int32_t pos, int32_t len)
Substring of another StringPiece.
icu::StringPiece::data
const char * data() const
Returns the string pointer.
Definition: stringpiece.h:200
icu::StringPiece::StringPiece
StringPiece(T str)
Constructs from some other implementation of a string piece class, from any C++ record type that has ...
Definition: stringpiece.h:141
icu::StringPiece::StringPiece
StringPiece(const char8_t *str)
Constructs from a NUL-terminated const char8_t * pointer.
Definition: stringpiece.h:85
icu::StringPiece::StringPiece
StringPiece(const std::u8string &str)
Constructs from a std::u8string.
Definition: stringpiece.h:106
icu::StringPiece::find
int32_t find(StringPiece needle, int32_t offset)
Searches the StringPiece for the given search string (needle);.
icu::StringPiece::npos
static const int32_t npos
Maximum integer, used as a default value for substring methods.
Definition: stringpiece.h:314
icu::StringPiece::clear
void clear()
Sets to an empty string.
Definition: stringpiece.h:224
icu::StringPiece::set
void set(const char8_t *xdata, int32_t len)
Resets the stringpiece to refer to new data.
Definition: stringpiece.h:248
icu::StringPiece::StringPiece
StringPiece()
Default constructor, creates an empty StringPiece.
Definition: stringpiece.h:71
icu::StringPiece::StringPiece
StringPiece(std::nullptr_t p)
Constructs an empty StringPiece.
Definition: stringpiece.h:93
icu::StringPiece::set
void set(const char8_t *str)
Resets the stringpiece to refer to new data.
Definition: stringpiece.h:257
icu::StringPiece::set
void set(const char *xdata, int32_t len)
Reset the stringpiece to refer to new data.
Definition: stringpiece.h:232
icu::StringPiece::size
int32_t size() const
Returns the string length.
Definition: stringpiece.h:206
icu::StringPiece::remove_prefix
void remove_prefix(int32_t n)
Removes the first n string units.
Definition: stringpiece.h:267
icu::StringPiece::compare
int32_t compare(StringPiece other)
Compares this StringPiece with the other StringPiece, with semantics similar to std::string::compare(...
icu::StringPiece::remove_suffix
void remove_suffix(int32_t n)
Removes the last n string units.
Definition: stringpiece.h:282
icu::StringPiece::StringPiece
StringPiece(const char8_t *str, int32_t len)
Constructs from a const char8_t * pointer and a specified length.
Definition: stringpiece.h:159
icu::StringPiece::length
int32_t length() const
Returns the string length.
Definition: stringpiece.h:212
icu::StringPiece::StringPiece
StringPiece(const StringPiece &x, int32_t pos)
Substring of another StringPiece.
icu::StringPiece::StringPiece
StringPiece(const std::string &str)
Constructs from a std::string.
Definition: stringpiece.h:99
icu::StringPiece::StringPiece
StringPiece(const char *offset, int32_t len)
Constructs from a const char * pointer and a specified length.
Definition: stringpiece.h:151
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:115
icu::operator==
U_COMMON_API UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
icu::operator!=
bool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:346
std_string.h
C++ API: Central ICU header for including the C++ standard <string> header and for related definition...
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
uobject.h
C++ API: Common ICU base class UObject.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
U_COMMON_API
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside.
Definition: utypes.h:315