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.
The String implementation must be explicitly initialized before use; see static_initialize(). Explicit initialization is used because static constructors and other automatic initialization tricks don't work in the kernel. However, at user level, you can declare a String::Initializer object to initialize the library.
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.
Out-of-memory strings have zero characters, but they aren't equal to other empty strings. If s is a normal String (even an empty string), and oom is an out-of-memory string, then s < oom.
All 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 an out-of-memory string.
Public Types | |
| typedef int64_t | int_large_t |
| typedef uint64_t | uint_large_t |
| typedef const char * | const_iterator |
| typedef const_iterator | iterator |
| typedef int(String::*) | unspecified_bool_type () const |
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 char *begin, const char *end) | |
| Construct a String containing the characters from begin to end. | |
| 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. | |
| int | length () const |
| Return the string's length. | |
| const char * | data () const |
| Return a pointer to the string's data. | |
| 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. | |
| operator unspecified_bool_type () const | |
| Return true iff the string is nonempty. | |
| bool | operator! () const |
| Return true iff the string is empty. | |
| 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. | |
| const char * | c_str () const |
| Null-terminate the string. | |
| uint32_t | hashcode () const |
| Returns a 32-bit hash function of this string's characters. | |
| bool | equals (const char *s, int len) const |
| Return true iff 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. | |
| String | substring (const char *begin, const char *end) const |
| Return a substring of the current string starting at begin and ending before end. | |
| 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. | |
| 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. | |
| bool | starts_with (const String &x) const |
| Return true iff 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. | |
| 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 | trim_space () const |
| Return a substring with spaces trimmed from the end. | |
| String | quoted_hex () const |
| Return a hex-quoted version of the string. | |
| String & | operator= (const String &x) |
| Assign this string to x. | |
| String & | operator= (const char *cstr) |
| Assign this string to the C string cstr. | |
| void | append (const char *s, int len) |
| Append the first len characters of s to this string. | |
| void | append (const char *begin, const char *end) |
| Appends the data from begin to end to the end of this string. | |
| void | append_fill (int c, int len) |
| Append len copies of character c to this string. | |
| char * | append_garbage (int len) |
| Append len unknown characters to this string. | |
| String & | operator+= (const String &x) |
| Append a copy of x to the end of this string. | |
| String & | operator+= (const char *cstr) |
| Append a copy of the C string cstr to the end of this string. | |
| String & | operator+= (char c) |
| Append the character c to the end of this string. | |
| bool | data_shared () const |
| Return true iff the String's data is shared or immutable. | |
| 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 |
| Return true iff this is an out-of-memory string. | |
Static Public Member Functions | |
| static void | static_initialize () |
| Initialize the String implementation. | |
| static void | static_cleanup () |
| Clean up the String implementation. | |
| static const String & | make_empty () |
| Return a const reference to an empty String. | |
| static String | make_garbage (int len) |
| Return a String containing len unknown characters. | |
| static String | make_stable (const char *s, int len=-1) |
| Return a String that directly references the first len characters of s. | |
| static String | make_stable (const char *begin, const char *end) |
| Return a String that directly references the character data in [begin, end). | |
| static String | make_numeric (int_large_t x, int base=10, bool uppercase=true) |
| Create and return a string representation of x. | |
| static String | make_numeric (uint_large_t x, int base=10, bool uppercase=true) |
| static const String & | empty_string () |
| Return a const reference to an empty String. | |
| static String | garbage_string (int len) |
| Return a String containing len unknown characters. | |
| static String | stable_string (const char *s, int len=-1) |
| Return a String that directly references the first len characters of s. | |
| static String | stable_string (const char *begin, const char *end) |
| Return a String that directly references the character data in [begin, end). | |
| static String | numeric_string (int_large_t x, int base=10, bool uppercase=true) |
| Create and return a string representation of x. | |
| static String | numeric_string (uint_large_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 [begin, end). | |
| 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 an out-of-memory String. | |
| static const char * | out_of_memory_data () |
| Return the data pointer used for out-of-memory strings. | |
Related Functions | |
| (Note that these are not member functions.) | |
| 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. | |
Classes | |
| class | Initializer |
| Initializes the String implementation. More... | |
| String::String | ( | ) | [inline] |
Construct an empty String (with length 0).
| String::String | ( | const char * | cstr | ) | [inline] |
Construct a String containing the C string cstr.
| cstr | a null-terminated C string |
| String::String | ( | const char * | s, | |
| int | len | |||
| ) | [inline] |
Construct a String containing the first len characters of string s.
| s | a string | |
| len | number of characters to take from s. If len < 0, then takes strlen(s) characters. |
| String::String | ( | const char * | begin, | |
| const char * | end | |||
| ) | [inline] |
Construct a String containing the characters from begin to end.
| begin | first character in string (begin iterator) | |
| end | pointer one past last character in string (end iterator) |
| String::String | ( | bool | x | ) | [inline, explicit] |
Construct a String equal to "true" or "false" depending on the value of x.
| String::String | ( | char | c | ) | [inline, explicit] |
Construct a String containing the single character c.
| String::String | ( | int | x | ) | [explicit] |
Construct a base-10 string representation of x.
| String::String | ( | double | x | ) | [explicit] |
Construct a base-10 string representation of x.
| String::~String | ( | ) | [inline] |
Destroy a String, freeing memory if necessary.
| void String::static_initialize | ( | ) | [static] |
Initialize the String implementation.
| void String::static_cleanup | ( | ) | [static] |
Clean up the String implementation.
| static const String& String::make_empty | ( | ) | [inline, static] |
Return a const reference to an empty String.
May be quicker than String::String().
| String String::make_stable | ( | const char * | s, | |
| int | len = -1 | |||
| ) | [static] |
Return a String that directly references the first len characters of s.
This function is suitable for static constant strings whose data is known to stay around forever, such as C string constants. If len < 0, treats s as a null-terminated C string.
| static String String::make_stable | ( | const char * | begin, | |
| const char * | end | |||
| ) | [inline, static] |
Return a String that directly references the character data in [begin, end).
| begin | pointer to the first character in the character data | |
| end | pointer one beyond the last character in the character data (but see the warning) |
| String String::make_numeric | ( | int_large_t | x, | |
| int | base = 10, |
|||
| bool | uppercase = true | |||
| ) | [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 |
| const String & String::empty_string | ( | ) | [inline, static] |
| String String::garbage_string | ( | int | len | ) | [inline, static] |
| String String::stable_string | ( | const char * | s, | |
| int | len = -1 | |||
| ) | [inline, static] |
Return a String that directly references the first len characters of s.
| String String::stable_string | ( | const char * | begin, | |
| const char * | end | |||
| ) | [inline, static] |
Return a String that directly references the character data in [begin, end).
| String String::numeric_string | ( | int_large_t | x, | |
| int | base = 10, |
|||
| bool | uppercase = true | |||
| ) | [inline, static] |
| int String::length | ( | ) | const [inline] |
Return the string's length.
| const char* String::data | ( | ) | const [inline] |
Return a pointer to the string's data.
Only the first length() characters are valid, and the string data might not be null-terminated.
| const_iterator String::begin | ( | ) | const [inline] |
Return an iterator for the first character in the string.
String iterators are simply pointers into string data, so they are quite efficient.
| const_iterator String::end | ( | ) | const [inline] |
Return an iterator for the end of the string.
The return value points one character beyond the last character in the string.
| String::operator unspecified_bool_type | ( | ) | const [inline] |
Return true iff the string is nonempty.
| bool String::operator! | ( | ) | const [inline] |
Return true iff the string is empty.
| char String::operator[] | ( | int | i | ) | const [inline] |
| char String::at | ( | int | i | ) | const [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().
| char String::front | ( | ) | const [inline] |
Return the first character in the string.
Does not check bounds. Same as (*this)[0].
| char String::back | ( | ) | const [inline] |
Return the last character in the string.
Does not check bounds. Same as (*this)[length() - 1].
| const char* String::c_str | ( | ) | const [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.
| uint32_t String::hashcode | ( | const char * | begin, | |
| const char * | end | |||
| ) | [static] |
Return a 32-bit hash function of the characters in [begin, end).
Uses Paul Hsieh's "SuperFastHash" algorithm, described at http://www.azillionmonkeys.com/qed/hash.html This hash function uses all characters in the string.
| uint32_t String::hashcode | ( | ) | const [inline] |
Returns a 32-bit hash function of this string's characters.
Equivalent to String::hashcode(begin(), end()). Uses Paul Hsieh's "SuperFastHash."
| bool String::equals | ( | const char * | s, | |
| int | len | |||
| ) | const |
Return true iff this string is equal to the data in s.
| s | string data to compare to | |
| len | length of s |
Compare two strings.
| a | first string to compare | |
| b | second string to compare |
| int String::compare | ( | const String & | x | ) | const [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 |
| String String::substring | ( | const char * | begin, | |
| const char * | end | |||
| ) | const [inline] |
Return a substring of the current string starting at begin and ending before end.
| begin | pointer to the first substring character | |
| end | pointer one beyond the last substring character |
| 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 |
| String String::substring | ( | int | pos | ) | const [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).
| 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 |
| 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 |
| 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 |
| bool String::starts_with | ( | const String & | x | ) | const [inline] |
Return true iff 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 |
| 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::trim_space | ( | ) | const |
Return a substring with spaces trimmed from the end.
| String String::quoted_hex | ( | ) | const |
Return a hex-quoted version of the string.
For example, the string "Abcd" would convert to "\<41626364>".
| String& String::operator= | ( | const char * | cstr | ) | [inline] |
Assign this string to the C string cstr.
| void String::append | ( | const char * | s, | |
| int | len | |||
| ) |
Append the first len characters of s to this string.
| s | data to append | |
| len | length of data |
| void String::append | ( | const char * | begin, | |
| const char * | end | |||
| ) | [inline] |
Appends the data from begin to end to the end of this string.
Does nothing if begin >= end.
| void String::append_fill | ( | int | c, | |
| int | len | |||
| ) |
Append len copies of character c to this string.
| char * String::append_garbage | ( | int | len | ) |
Append len unknown characters to this string.
Append a copy of x to the end of this string.
Returns the result.
| String& String::operator+= | ( | const char * | cstr | ) | [inline] |
Append a copy of the C string cstr to the end of this string.
Returns the result.
| String& String::operator+= | ( | char | c | ) | [inline] |
Append the character c to the end of this string.
Returns the result.
| bool String::data_shared | ( | ) | const [inline] |
Return true iff the String's data is shared or immutable.
| String String::compact | ( | ) | const [inline] |
| char * String::mutable_data | ( | ) |
Ensure the string's data is unshared and return a mutable pointer to it.
| char * String::mutable_c_str | ( | ) |
| bool String::out_of_memory | ( | ) | const [inline] |
Return true iff this is an out-of-memory string.
| static const String& String::make_out_of_memory | ( | ) | [inline, static] |
Return a const reference to an out-of-memory String.
| static const char* String::out_of_memory_data | ( | ) | [inline, static] |
Return the data pointer used for out-of-memory strings.
The returned value may be dereferenced; it points to a null character.
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.
1.5.1