|
Click
|
A string of characters. More...
Public Member Functions | |
| String () | |
| Construct an empty String (with length 0). | |
| String (const String &x) | |
| Construct a copy of the String x. | |
| String (const char *cstr) | |
| Construct a String containing the C string cstr. | |
| String (const char *s, int len) | |
| Construct a String containing the first len characters of string s. | |
| String (const unsigned char *s, int len) | |
| String (const char *first, const char *last) | |
| Construct a String containing the characters from first to last. | |
| String (const unsigned char *first, const unsigned char *last) | |
| String (bool x) | |
| Construct a String equal to "true" or "false" depending on the value of x. | |
| String (char c) | |
| Construct a String containing the single character c. | |
| String (unsigned char c) | |
| String (int x) | |
| Construct a base-10 string representation of x. | |
| String (unsigned x) | |
| String (long x) | |
| String (unsigned long x) | |
| String (int64_t x) | |
| String (uint64_t x) | |
| String (double x) | |
| Construct a base-10 string representation of x. | |
| ~String () | |
| Destroy a String, freeing memory if necessary. | |
| const char * | data () const |
| Return a pointer to the string's data. | |
| int | length () const |
| Return the string's length. | |
| const char * | c_str () const |
| Null-terminate the string. | |
| operator unspecified_bool_type () const | |
| Test if the string is nonempty. | |
| bool | empty () const |
| Test if the string is empty. | |
| bool | operator! () const |
| Test if the string is empty. | |
| const_iterator | begin () const |
| Return an iterator for the first character in the string. | |
| const_iterator | end () const |
| Return an iterator for the end of the string. | |
| char | operator[] (int i) const |
| Return the i th character in the string. | |
| char | at (int i) const |
| Return the i th character in the string. | |
| char | front () const |
| Return the first character in the string. | |
| char | back () const |
| Return the last character in the string. | |
| uint32_t | hashcode () const |
| Returns a 32-bit hash function of this string's characters. | |
| String | substring (const char *begin, const char *end) const |
| Return a substring of the current string starting at first and ending before last. | |
| String | substring (int pos, int len) const |
| Return a substring of this string, consisting of the len characters starting at index pos. | |
| String | substring (int pos) const |
| Return the suffix of the current string starting at index pos. | |
| String | trim () const |
| Return a substring with spaces trimmed from both sides. | |
| String | trim_space () const |
| Return a substring with spaces trimmed from the end. | |
| String | trim_space_left () const |
| Return a substring with spaces trimmed from the start. | |
| String | remove (char c) const |
| Return a string with from replaced by to. | |
| String | replace (char from, char to) const |
| Return a string with from replaced by to. | |
| String | replace (String from, String to) const |
| Return a string with from replaced by to. | |
| bool | equals (const String &x) const |
| Test if this string equals x. | |
| bool | equals (const char *s, int len) const |
| Test if this string is equal to the data in s. | |
| int | compare (const String &x) const |
| Compare this string with string x. | |
| int | compare (const char *s, int len) const |
| Compare this string with the data in s. | |
| bool | starts_with (const String &x) const |
| Test if this string begins with prefix x. | |
| bool | starts_with (const char *s, int len) const |
| Return true iff this string begins with the data in s. | |
| bool | glob_match (const String &pattern) const |
| Test whether this string matches the glob pattern. | |
| Vector< String > | split (char c) const |
| Split a string using an input character as a delimiter. The outcome of the split is a vector of tokens. | |
| int | find_left (char c, int start=0) const |
| Search for a character in a string. | |
| int | find_left (const String &x, int start=0) const |
| Search for a substring in a string. | |
| int | find_right (char c, int start=0x7FFFFFFF) const |
| Search for a character in a string. | |
| const char * | search (String pattern) |
| String | lower () const |
| Return a lowercased version of this string. | |
| String | upper () const |
| Return an uppercased version of this string. | |
| String | printable () const |
| Return a "printable" version of this string. | |
| String | quoted_hex () const |
| Return a hex-quoted version of the string. | |
| String | encode_json () const |
| Return this string's contents encoded for JSON. | |
| String & | operator= (const String &x) |
| Assign this string to x. | |
| String & | operator= (const char *cstr) |
| Assign this string to the C string cstr. | |
| void | swap (String &x) |
| Swap the values of this string and x. | |
| void | append (const String &x) |
| Append x to this string. | |
| void | append (const char *cstr) |
| Append the null-terminated C string cstr to this string. | |
| void | append (const char *s, int len) |
| Append the first len characters of s to this string. | |
| void | append (const char *first, const char *last) |
| Appends the data from first to last to this string. | |
| void | append (char c) |
| Append the character c to this string. | |
| void | append_fill (int c, int len) |
| Append len copies of character c to this string. | |
| char * | append_uninitialized (int len) |
| Append len unknown characters to this string. | |
| char * | append_garbage (int len) |
| String & | operator+= (const String &x) |
| Append x to this string. | |
| String & | operator+= (const char *cstr) |
| Append the null-terminated C string cstr to this string. | |
| String & | operator+= (char c) |
| Append the character c to this string. | |
| bool | is_shared () const |
| Test if the String's data is shared or immutable. | |
| bool | is_stable () const |
| Test if the String's data is immutable. | |
| String | unique () const |
| Return an unshared version of this String. | |
| String | unshared () const |
| Return an unshared version of this String. | |
| String | compact () const |
| Return a compact version of this String. | |
| char * | mutable_data () |
| Ensure the string's data is unshared and return a mutable pointer to it. | |
| char * | mutable_c_str () |
| Null-terminate the string and return a mutable pointer to its data. | |
| bool | out_of_memory () const |
| Test if this is an out-of-memory string. | |
Static Public Member Functions | |
| static const String & | make_empty () |
| Return a const reference to an empty String. | |
| static String | make_uninitialized (int len) |
| Return a String containing len unknown characters. | |
| static String | make_garbage (int len) |
| static String | make_stable (const char *cstr) |
| Return a String that directly references the C string cstr. | |
| static String | make_stable (const char *s, int len) |
| Return a String that directly references the first len characters of s. | |
| static String | make_stable (const char *first, const char *last) |
| Return a String that directly references the character data in [first, last). | |
| static String | make_numeric (intmax_t x, int base=10, bool uppercase=true) |
| Create and return a string representation of x. | |
| static String | make_numeric (uintmax_t x, int base=10, bool uppercase=true) |
| static uint32_t | hashcode (const char *begin, const char *end) |
| Return a 32-bit hash function of the characters in [first, last). | |
| static uint32_t | hashcode (const unsigned char *begin, const unsigned char *end) |
| static int | compare (const String &a, const String &b) |
| Compare two strings. | |
| static const String & | make_out_of_memory () |
| Return a const reference to a canonical out-of-memory String. | |
| static const char * | out_of_memory_data () |
| Return the data pointer used for out-of-memory strings. | |
| static int | out_of_memory_length () |
| Return the length of canonical out-of-memory strings. | |
| static const char * | empty_data () |
| Return the data pointer used for canonical empty strings. | |
| static const char * | skip_utf8_char (const char *first, const char *last) |
| Return a pointer to the next character in UTF-8 encoding. | |
| static const unsigned char * | skip_utf8_char (const unsigned char *first, const unsigned char *last) |
| Return a pointer to the next character in UTF-8 encoding. | |
Static Public Attributes | |
| static const char | bool_data [11] = "false\0true" |
Related Symbols | |
(Note that these are not member symbols.) | |
| bool | operator== (const String &a, const String &b) |
| Compares two strings for equality. | |
| bool | operator== (const char *a, const String &b) |
| bool | operator== (const String &a, const char *b) |
| bool | operator!= (const String &a, const String &b) |
| Compare two Strings for inequality. | |
| bool | operator!= (const char *a, const String &b) |
| bool | operator!= (const String &a, const char *b) |
| bool | operator< (const String &a, const String &b) |
| Compare two Strings. | |
| bool | operator<= (const String &a, const String &b) |
| Compare two Strings. | |
| bool | operator> (const String &a, const String &b) |
| Compare two Strings. | |
| bool | operator>= (const String &a, const String &b) |
| Compare two Strings. | |
| String | operator+ (String a, const String &b) |
| Concatenate the operands and return the result. | |
| String | operator+ (String a, const char *b) |
| String | operator+ (const char *a, const String &b) |
| String | operator+ (String a, char b) |
| Concatenate the operands and return the result. | |
A string of characters.
The String class represents a string of characters. Strings may be constructed from C strings, characters, numbers, and so forth. They may also be added together. The underlying character arrays are dynamically allocated; String operations allocate and free memory as needed. A String and its substrings generally share memory. Accessing a character by index takes O(1) time; so does creating a substring.
When there is not enough memory to create a particular string, a special "out-of-memory" string is returned instead. Out-of-memory strings are contagious: the result of any concatenation operation involving an out-of-memory string is another out-of-memory string. Thus, the final result of a series of String operations will be an out-of-memory string, even if the out-of-memory condition occurs in the middle.
The canonical out-of-memory string is 14 bytes long, and equals the UTF-8 encoding of "\U0001F4A3ENOMEM\U0001F4A3" (that is, U+1F4A3 BOMB + "ENOMEM" + U+1F4A3 BOMB). This sequence is unlikely to show up in normal text, compares high relative to most other textual strings, and is valid UTF-8.
All canonical out-of-memory strings are equal and share the same data(), which is different from the data() of any other string. See String::out_of_memory_data(). The String::make_out_of_memory() function returns a canonical out-of-memory string.
Other strings may also be out-of-memory strings. For example, String::make_stable(String::out_of_memory_data()) == String::make_out_of_memory(), and some (but not all) substrings of out-of-memory strings are also out-of-memory strings.
|
inline |
Construct an empty String (with length 0).
|
inline |
|
inline |
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inline |
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inlineexplicit |
Construct a String equal to "true" or "false" depending on the value of x.
|
inlineexplicit |
Construct a String containing the single character c.
|
inlineexplicit |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
explicit |
Construct a base-10 string representation of x.
never
|
explicit |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
explicit |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
explicit |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
explicit |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
explicit |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
explicit |
Construct a base-10 string representation of x.
|
inline |
Destroy a String, freeing memory if necessary.
|
inlinestatic |
Return a const reference to an empty String.
May be quicker than String::String().
|
inlinestatic |
Return a String containing len unknown characters.
|
inlinestatic |
Return a String that directly references the C string cstr.
never
The make_stable() functions are suitable for static constant strings whose data is known to stay around forever, such as C string constants.
|
inlinestatic |
|
inlinestatic |
Return a String that directly references the character data in [first, last).
| first | pointer to the first character in the character data |
| last | pointer one beyond the last character in the character data (but see the warning) |
This function is suitable for static constant strings whose data is known to stay around forever, such as C string constants. Returns an empty string if first >= last.
|
static |
Create and return a string representation of x.
| x | number |
| base | base; must be 8, 10, or 16, defaults to 10 |
| uppercase | if true, then use uppercase letters in base 16 |
|
static |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inline |
Return a pointer to the string's data.
Only the first length() characters are valid, and the string might not be null-terminated.
|
inline |
Return the string's length.
|
inline |
Null-terminate the string.
The terminating null character isn't considered part of the string, so this->length() doesn't change. Returns a corresponding C string pointer. The returned pointer is semi-temporary; it will persist until the string is destroyed or appended to.
|
inline |
Test if the string is nonempty.
|
inline |
Test if the string is empty.
|
inline |
Test if the string is empty.
|
inline |
Return an iterator for the first character in the string.
String iterators are simply pointers into string data, so they are quite efficient.
|
inline |
Return an iterator for the end of the string.
The return value points one character beyond the last character in the string.
|
inline |
|
inline |
Return the i th character in the string.
Checks bounds: an assertion will fail if i is less than 0 or not less than length().
|
inline |
Return the first character in the string.
Does not check bounds. Same as (*this)[0].
|
inline |
Return the last character in the string.
Does not check bounds. Same as (*this)[length() - 1].
|
static |
Return a 32-bit hash function of the characters in [first, last).
Uses Paul Hsieh's "SuperFastHash" algorithm, described at http://www.azillionmonkeys.com/qed/hash.html This hash function uses all characters in the string.
|
inlinestatic |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
|
inline |
Returns a 32-bit hash function of this string's characters.
Equivalent to String::hashcode(begin(), end()). Uses Paul Hsieh's "SuperFastHash."
|
inline |
Return a substring of the current string starting at first and ending before last.
| first | pointer to the first substring character |
| last | pointer one beyond the last substring character |
Returns an empty string if first >= last. Also returns an empty string if first or last is out of range (i.e., either less than this->begin() or greater than this->end()), but this should be considered a programming error; a future version may generate a warning for this case.
| String String::substring | ( | int | pos, |
| int | len | ||
| ) | const |
Return a substring of this string, consisting of the len characters starting at index pos.
| pos | substring's first position relative to the string |
| len | length of substring |
If pos is negative, starts that far from the end of the string. If len is negative, leaves that many characters off the end of the string. If pos and len specify a substring that is partly outside the string, only the part within the string is returned. If the substring is beyond either end of the string, returns an empty string (but this should be considered a programming error; a future version may generate a warning for this case).
|
inline |
Return the suffix of the current string starting at index pos.
If pos is negative, starts that far from the end of the string. If pos is so negative that the suffix starts outside the string, then the entire string is returned. If the substring is beyond the end of the string (pos > length()), returns an empty string (but this should be considered a programming error; a future version may generate a warning for this case).
| String String::trim | ( | ) | const |
Return a substring with spaces trimmed from both sides.
| String String::trim_space | ( | ) | const |
Return a substring with spaces trimmed from the end.
| String String::trim_space_left | ( | ) | const |
Return a substring with spaces trimmed from the start.
| String String::remove | ( | char | c | ) | const |
Return a string with from replaced by to.
| String String::replace | ( | char | from, |
| char | to | ||
| ) | const |
Return a string with from replaced by to.
|
inline |
Test if this string equals x.
|
inline |
Test if this string is equal to the data in s.
| s | string data to compare to |
| len | length of s |
Same as String::compare(*this, String(s, len)) == 0. If len < 0, then treats s as a null-terminated C string.
Compare two strings.
| a | first string to compare |
| b | second string to compare |
Returns 0 if a == b, negative if a < b in lexicographic order, and positive if a > b in lexicographic order. The lexicographic order treats all characters as unsigned.
|
inline |
Compare this string with string x.
Same as String::compare(*this, x).
| int String::compare | ( | const char * | s, |
| int | len | ||
| ) | const |
Compare this string with the data in s.
| s | string data to compare to |
| len | length of s |
Same as String::compare(*this, String(s, len)). If len < 0, then treats s as a null-terminated C string.
|
inline |
Test if this string begins with prefix x.
Same as String::starts_with(x.data(), x.length()).
| bool String::starts_with | ( | const char * | s, |
| int | len | ||
| ) | const |
Return true iff this string begins with the data in s.
| s | string data to compare to |
| len | length of s |
If len < 0, then treats s as a null-terminated C string.
| bool String::glob_match | ( | const String & | pattern | ) | const |
Test whether this string matches the glob pattern.
| pattern | glob pattern |
A glob pattern is a pattern that may contain wildcard characters. * in the pattern matches zero or more arbitrary characters in str; ? matches exactly one arbitrary character in str; [...] matches any of the characters in brackets; and [^...] matches any character not in the brackets. A backslash \ escapes a wildcard character so that it matches only itself.
Split a string using an input character as a delimiter. The outcome of the split is a vector of tokens.
| c | character to use as a delimiter |
Return a vector of tokens or an empty vector if no such delimiter exists.
| int String::find_left | ( | char | c, |
| int | start = 0 |
||
| ) | const |
Search for a character in a string.
| c | character to search for |
| start | initial search position |
Return the index of the leftmost occurence of c, starting at index start and working up to the end of the string. Returns -1 if c is not found.
| int String::find_left | ( | const String & | x, |
| int | start = 0 |
||
| ) | const |
Search for a substring in a string.
| x | substring to search for |
| start | initial search position |
Return the index of the leftmost occurence of the substring str, starting at index start and working up to the end of the string. Returns -1 if str is not found.
| int String::find_right | ( | char | c, |
| int | start = 0x7FFFFFFF |
||
| ) | const |
Search for a character in a string.
| c | character to search for |
| start | initial search position |
Return the index of the rightmost occurence of the character c, starting at index start and working back to the beginning of the string. Returns -1 if c is not found. start may start beyond the end of the string.
|
inline |
Search for the non-empty pattern in this string
| String String::lower | ( | ) | const |
Return a lowercased version of this string.
Translates the ASCII characters 'A' through 'Z' into their lowercase equivalents.
| String String::upper | ( | ) | const |
Return an uppercased version of this string.
Translates the ASCII characters 'a' through 'z' into their uppercase equivalents.
| String String::printable | ( | ) | const |
Return a "printable" version of this string.
Translates control characters 0-31 into "control" sequences, such as "^@" for the null character, and characters 127-255 into octal escape sequences, such as "\377" for 255.
| String String::quoted_hex | ( | ) | const |
Return a hex-quoted version of the string.
For example, the string "Abcd" would convert to "\<41626364>".
| String String::encode_json | ( | ) | const |
Return this string's contents encoded for JSON.
For instance, String("a\"").encode_json() == "a\\\"". Note that the double-quote characters that usually surround a JSON string are not included.
|
inline |
Assign this string to the C string cstr.
|
inline |
Swap the values of this string and x.
|
inline |
Append x to this string.
|
inline |
Append the null-terminated C string cstr to this string.
| cstr | data to append |
|
inline |
Append the first len characters of s to this string.
| s | data to append |
| len | length of data |
|
inline |
Appends the data from first to last to this string.
Does nothing if first >= last.
|
inline |
Append the character c to this string.
| void String::append_fill | ( | int | c, |
| int | len | ||
| ) |
Append len copies of character c to this string.
| char * String::append_uninitialized | ( | int | len | ) |
Append len unknown characters to this string.
The caller may safely modify the returned memory. Null is returned if the string becomes out-of-memory.
|
inline |
Append the null-terminated C string cstr to this string.
|
inline |
Append the character c to this string.
|
inline |
Test if the String's data is shared or immutable.
|
inline |
Test if the String's data is immutable.
|
inline |
Return an unshared version of this String.
The return value shares no data with any other non-stable String.
|
inline |
|
inline |
| char * String::mutable_data | ( | ) |
Ensure the string's data is unshared and return a mutable pointer to it.
| char * String::mutable_c_str | ( | ) |
Null-terminate the string and return a mutable pointer to its data.
|
inlinestatic |
Return a const reference to a canonical out-of-memory String.
|
inline |
Test if this is an out-of-memory string.
|
inlinestatic |
Return the data pointer used for out-of-memory strings.
|
inlinestatic |
Return the length of canonical out-of-memory strings.
|
inlinestatic |
Return the data pointer used for canonical empty strings.
The returned value may be dereferenced; it points to a null character.
|
inlinestatic |
Return a pointer to the next character in UTF-8 encoding.
If first doesn't point at a valid UTF-8 character, returns first.
|
static |
Return a pointer to the next character in UTF-8 encoding.
If first doesn't point at a valid UTF-8 character, returns first.
Compares two strings for equality.
Returns true iff the two operands have the same lengths and the same characters in the same order. At most one of the operands can be a null-terminated C string.
Compare two Strings for inequality.
Returns true iff !(a == b). At most one of the operands can be a null-terminated C string.
Concatenate the operands and return the result.
At most one of the two operands can be a null-terminated C string.
Concatenate the operands and return the result.
The second operand is a single character.