ICU 76.1 76.1
|
A string-like object that points to a sized piece of memory. More...
#include <stringpiece.h>
Public Member Functions | |
StringPiece () | |
Default constructor, creates an empty StringPiece. | |
StringPiece (const char *str) | |
Constructs from a NUL-terminated const char * pointer. | |
StringPiece (const char8_t *str) | |
Constructs from a NUL-terminated const char8_t * pointer. | |
StringPiece (std::nullptr_t p) | |
Constructs an empty StringPiece. | |
StringPiece (const std::string &str) | |
Constructs from a std::string. | |
StringPiece (const std::u8string &str) | |
Constructs from a std::u8string. | |
template<typename T , typename = std::enable_if_t< (std::is_same_v<decltype(T().data()), const char*> ) && std::is_same_v<decltype(T().size()), size_t>>> | |
StringPiece (T str) | |
Constructs from some other implementation of a string piece class, from any C++ record type that has these two methods: | |
StringPiece (const char *offset, int32_t len) | |
Constructs from a const char * pointer and a specified length. | |
StringPiece (const char8_t *str, int32_t len) | |
Constructs from a const char8_t * pointer and a specified length. | |
StringPiece (const StringPiece &x, int32_t pos) | |
Substring of another StringPiece. | |
StringPiece (const StringPiece &x, int32_t pos, int32_t len) | |
Substring of another StringPiece. | |
operator std::string_view () const | |
Converts to a std::string_view(). | |
const char * | data () const |
Returns the string pointer. | |
int32_t | size () const |
Returns the string length. | |
int32_t | length () const |
Returns the string length. | |
UBool | empty () const |
Returns whether the string is empty. | |
void | clear () |
Sets to an empty string. | |
void | set (const char *xdata, int32_t len) |
Reset the stringpiece to refer to new data. | |
void | set (const char *str) |
Reset the stringpiece to refer to new data. | |
void | set (const char8_t *xdata, int32_t len) |
Resets the stringpiece to refer to new data. | |
void | set (const char8_t *str) |
Resets the stringpiece to refer to new data. | |
void | remove_prefix (int32_t n) |
Removes the first n string units. | |
void | remove_suffix (int32_t n) |
Removes the last n string units. | |
int32_t | find (StringPiece needle, int32_t offset) |
Searches the StringPiece for the given search string (needle);. | |
int32_t | compare (StringPiece other) |
Compares this StringPiece with the other StringPiece, with semantics similar to std::string::compare(). | |
StringPiece | substr (int32_t pos, int32_t len=npos) const |
Returns a substring of this StringPiece. | |
Static Public Attributes | |
static const int32_t | npos |
Maximum integer, used as a default value for substring methods. | |
A string-like object that points to a sized piece of memory.
We provide non-explicit singleton constructors so users can pass in a "const char*" or a "string" wherever a "StringPiece" is expected.
Functions or methods may use StringPiece parameters to accept either a "const char*" or a "string" value that will be implicitly converted to a StringPiece.
Systematic usage of StringPiece is encouraged as it will reduce unnecessary conversions from "const char*" to "string" and back again.
Definition at line 61 of file stringpiece.h.
|
inline |
Default constructor, creates an empty StringPiece.
Definition at line 71 of file stringpiece.h.
Constructs from a NUL-terminated const char * pointer.
str | a NUL-terminated const char * pointer |
Constructs from a NUL-terminated const char8_t * pointer.
str | a NUL-terminated const char8_t * pointer |
Definition at line 85 of file stringpiece.h.
|
inline |
Constructs an empty StringPiece.
Needed for type disambiguation from multiple other overloads.
p | nullptr |
Definition at line 93 of file stringpiece.h.
|
inline |
|
inline |
|
inline |
Constructs from some other implementation of a string piece class, from any C++ record type that has these two methods:
The other string piece class will typically be std::string_view from C++17 or absl::string_view from Abseil.
Starting with C++20, data() may also return a const char8_t* pointer, as from std::u8string_view.
str | the other string piece |
Definition at line 141 of file stringpiece.h.
Constructs from a const char * pointer and a specified length.
offset | a const char * pointer (need not be terminated) |
len | the length of the string; must be non-negative |
Definition at line 151 of file stringpiece.h.
Constructs from a const char8_t * pointer and a specified length.
str | a const char8_t * pointer (need not be terminated) |
len | the length of the string; must be non-negative |
Definition at line 159 of file stringpiece.h.
icu::StringPiece::StringPiece | ( | const StringPiece & | x, |
int32_t | pos | ||
) |
Substring of another StringPiece.
x | the other StringPiece |
pos | start position in x; must be non-negative and <= x.length(). |
icu::StringPiece::StringPiece | ( | const StringPiece & | x, |
int32_t | pos, | ||
int32_t | len | ||
) |
Substring of another StringPiece.
x | the other StringPiece |
pos | start position in x; must be non-negative and <= x.length(). |
len | length of the substring; must be non-negative and will be pinned to at most x.length() - pos. |
|
inline |
int32_t icu::StringPiece::compare | ( | StringPiece | other | ) |
Compares this StringPiece with the other StringPiece, with semantics similar to std::string::compare().
other | The string to compare to. |
Returns the string pointer.
May be nullptr if it is empty.
data() may return a pointer to a buffer with embedded NULs, and the returned buffer may or may not be null terminated. Therefore it is typically a mistake to pass data() to a routine that expects a NUL terminated string.
Definition at line 200 of file stringpiece.h.
|
inline |
Returns whether the string is empty.
Definition at line 218 of file stringpiece.h.
int32_t icu::StringPiece::find | ( | StringPiece | needle, |
int32_t | offset | ||
) |
Searches the StringPiece for the given search string (needle);.
needle | The string for which to search. |
offset | Where to start searching within this string (haystack). |
|
inline |
Returns the string length.
Same as size().
Definition at line 212 of file stringpiece.h.
|
inline |
Converts to a std::string_view().
Definition at line 185 of file stringpiece.h.
Removes the first n string units.
n | prefix length, must be non-negative and <=length() |
Definition at line 267 of file stringpiece.h.
Removes the last n string units.
n | suffix length, must be non-negative and <=length() |
Definition at line 282 of file stringpiece.h.
Reset the stringpiece to refer to new data.
str | a pointer to a NUL-terminated string. |
Reset the stringpiece to refer to new data.
xdata | pointer the new string data. Need not be nul terminated. |
len | the length of the new data |
Definition at line 232 of file stringpiece.h.
Resets the stringpiece to refer to new data.
str | a pointer to a NUL-terminated string. |
Definition at line 257 of file stringpiece.h.
Resets the stringpiece to refer to new data.
xdata | pointer the new string data. Need not be NUL-terminated. |
len | the length of the new data |
Definition at line 248 of file stringpiece.h.
|
inline |
Returns the string length.
Same as length().
Definition at line 206 of file stringpiece.h.
|
inline |
Returns a substring of this StringPiece.
pos | start position; must be non-negative and <= length(). |
len | length of the substring; must be non-negative and will be pinned to at most length() - pos. |
Definition at line 324 of file stringpiece.h.
Maximum integer, used as a default value for substring methods.
Definition at line 314 of file stringpiece.h.