String Class Reference

A character string class. More...

#include <String.h>

Inheritance diagram for String:

Inheritance graph
[legend]
List of all members.

Public Member Functions

 String (const char *str=NULL, uint32 maxLen=MUSCLE_NO_LIMIT)
 Constructor.
 String (const String &str)
 Copy Constructor.
 String (const String &str, uint32 beginIndex, uint32 endIndex=MUSCLE_NO_LIMIT)
 This constructor sets this String to be a substring of the specified String.
virtual ~String ()
 Destructor.
Stringoperator= (char val)
 Assignment Operator.
Stringoperator= (const char *val)
 Assignment Operator.
Stringoperator= (const String &rhs)
 Assignment Operator.
Stringoperator+= (const String &rhs)
 Append Operator.
Stringoperator+= (const char *rhs)
 Append Operator.
Stringoperator+= (char ch)
 Append Operator.
Stringoperator-= (const String &rhs)
 Remove Operator.
Stringoperator-= (const char ch)
 Remove Operator.
Stringoperator<< (const String &rhs)
 Append 'Stream' Operator.
Stringoperator<< (const char *rhs)
 Append 'Stream' Operator.
Stringoperator<< (int rhs)
 Append 'Stream' Operator.
Stringoperator<< (float rhs)
 Append 'Stream' Operator.
Stringoperator<< (bool rhs)
 Append 'Stream' Operator.
bool operator== (const String &rhs) const
 Comparison Operator.
bool operator== (const char *rhs) const
 Comparison Operator.
bool operator!= (const String &rhs) const
 Comparison Operator.
bool operator!= (const char *rhs) const
 Comparison Operator.
bool operator< (const String &rhs) const
 Comparison Operator.
bool operator< (const char *rhs) const
 Comparison Operator.
bool operator> (const String &rhs) const
 Comparison Operator.
bool operator> (const char *rhs) const
 Comparison Operator.
bool operator<= (const String &rhs) const
 Comparison Operator.
bool operator<= (const char *rhs) const
 Comparison Operator.
bool operator>= (const String &rhs) const
 Comparison Operator.
bool operator>= (const char *rhs) const
 Comparison Operator.
char operator[] (uint32 index) const
 Array Operator.
char & operator[] (uint32 index)
 Array Operator.
char CharAt (uint32 index) const
 Returns the character at the (index)'th position in the string.
int CompareTo (const String &rhs) const
 Compares this string to another string using strcmp().
int CompareTo (const char *rhs) const
 Compares this string to a C string using strcmp().
const char * Cstr () const
 Returns a read-only C-style pointer to our held character string.
const char * operator() () const
 Convenience synonym for Cstr().
void Clear ()
 Clears this string so that it contains no characters.
status_t SetCstr (const char *str, uint32 maxLen=MUSCLE_NO_LIMIT)
 Sets our state from the given C-style string.
status_t SetFromString (const String &str, uint32 beginIndex=0, uint32 endIndex=MUSCLE_NO_LIMIT)
 Sets our state from the given String.
bool IsEmpty () const
 Returns true iff this string is a zero-length string.
bool HasChars () const
 Returns true iff this string is not a zero-length string.
bool EndsWith (char c) const
 Returns true iff this string starts with (prefix).
bool EndsWith (const String &suffix) const
 Returns true iff this string ends with (suffix).
bool EndsWith (const char *suffix) const
 Returns true iff this string ends with (suffix).
bool Equals (const String &str) const
 Returns true iff this string is equal to (string), as determined by strcmp().
bool Equals (const char *str) const
 Returns true iff this string is equal to (str), as determined by strcmp().
bool Equals (char c) const
 Returns true iff this string contains a single character (c).
int IndexOf (char ch, uint32 fromIndex=0) const
 Returns the first index of (ch) in this string starting at or after (fromIndex), or -1 if not found.
int IndexOf (const String &str, uint32 fromIndex=0) const
 Returns the first index of substring (str) in this string starting at or after (fromIndex), or -1 if not found.
int IndexOf (const char *str, uint32 fromIndex=0) const
 Returns the first index of substring (str) in this string starting at or after (fromIndex), or -1 if not found.
int LastIndexOf (char ch, uint32 fromIndex=0) const
 Returns the last index of (ch) in this string starting at or after (fromIndex), or -1 if not found.
int LastIndexOf (const String &str) const
 Returns the last index of substring (str) in this string.
int LastIndexOf (const char *str) const
 Returns the last index of substring (str) in this string.
int LastIndexOf (const String &str, uint32 fromIndex) const
 Returns the last index of substring (str) in this string starting at or after (fromIndex), or -1 if not found.
int LastIndexOf (const char *str, uint32 fromIndex) const
 Returns the last index of substring (str) in this string starting at or after (fromIndex), or -1 if not found.
uint32 Length () const
 Returns the number of characters in the string (not including the terminating NUL byte).
uint32 GetNumInstancesOf (char ch) const
 Returns the number of instances of (c) in this string.
uint32 GetNumInstancesOf (const String &substring) const
 Returns the number of instances of (substring) in this string.
uint32 GetNumInstancesOf (const char *substring) const
 Returns the number of instances of (substring) in this string.
bool StartsWith (char c) const
 Returns true iff this string starts with (c).
bool StartsWith (const String &prefix) const
 Returns true iff this string starts with (prefix).
bool StartsWith (const char *prefix) const
 Returns true iff this string starts with (prefix).
bool StartsWith (const String &prefix, uint32 offset) const
 Returns true iff this string starts with the first (offset) characters of (prefix).
bool StartsWith (const char *prefix, uint32 offset) const
 Returns true iff this string starts with the first (offset) characters of (prefix).
String Prepend (const String &str, uint32 count=1) const
 Returns a string that consists of (count) copies of (str), followed by this string.
String Prepend (const char *str, uint32 count=1) const
 Returns a string that consists of (count) copies of (str), followed by this string.
String Append (const String &str, uint32 count=1) const
 Returns a string that consists of this string followed by (count) copies of (str).
String Append (const char *str, uint32 count=1) const
 Returns a string that consists of this string followed by (count) copies of (str).
String Pad (uint32 minLength, bool padOnRight=false, char padChar= ' ') const
 Returns a string that is like this string, but padded out to the specified minimum length with (padChar).
String Substring (uint32 beginIndex) const
 Returns a string that consists of only the last part of this string, starting with index (beginIndex).
String Substring (uint32 beginIndex, uint32 endIndex) const
 Returns a string that consists of only the characters in this string from range (beginIndex) to (endIndex-1).
String Substring (const String &markerString) const
 Returns a string that consists of only the last part of this string, starting with the first character after the last instance of (markerString).
String Substring (const char *markerString) const
 See the Substring(const String &) documentation for details.
String Substring (uint32 beginIndex, const String &markerString) const
 Returns a string that consists of only the characters in the string from range (beginIndex) until the character just before the first character in (markerString).
String Substring (uint32 beginIndex, const char *markerString) const
 See the Substring(uint32, const String &) documentation for details.
String ToLowerCase () const
 Returns an all lower-case version of this string.
String ToUpperCase () const
 Returns an all upper-case version of this string.
String Trim () const
 Returns an version of this string that has all leading and trailing whitespace removed.
void SwapContents (String &swapWithMe)
 Swaps the state of this string with (swapWithMe).
int CompareToIgnoreCase (const String &s) const
 Like CompareTo(), but case insensitive.
bool EndsWithIgnoreCase (char c) const
 Like EndsWith(), but case insensitive.
bool EndsWithIgnoreCase (const String &s) const
 Like EndsWith(), but case insensitive.
bool EqualsIgnoreCase (const String &s) const
 Like Equals(), but case insensitive.
bool EqualsIgnoreCase (char c) const
 Like Equals(), but case insensitive.
int IndexOfIgnoreCase (const String &s) const
 Like IndexOf(), but case insensitive.
int IndexOfIgnoreCase (const String &s, uint32 f) const
 Like IndexOf(), but case insensitive.
int IndexOfIgnoreCase (char ch) const
 Like IndexOf(), but case insensitive.
int IndexOfIgnoreCase (char ch, uint32 f) const
 Like IndexOf(), but case insensitive.
int LastIndexOfIgnoreCase (const String &s) const
 Like LastIndexOf(), but case insensitive.
int LastIndexOfIgnoreCase (const String &s, uint32 f) const
 Like LastIndexOf(), but case insensitive.
int LastIndexOfIgnoreCase (char ch) const
 Like LastIndexOf(), but case insensitive.
int LastIndexOfIgnoreCase (char ch, uint32 f) const
 Like LastIndexOf(), but case insensitive.
bool StartsWithIgnoreCase (char c) const
 Like EndsWith(), but case insensitive.
bool StartsWithIgnoreCase (const String &s) const
 Like StartsWith(), but case insensitive.
bool StartsWithIgnoreCase (const String &s, uint32 o) const
 Like StartsWith(), but case insensitive.
uint32 HashCode () const
 Returns a hash code for this string.
uint32 Replace (char replaceMe, char withMe)
 Replaces all instances of (oldChar) in this string with (newChar).
int32 Replace (const String &replaceMe, const String &withMe)
 Replaces all instances of (replaceMe) in this string with (withMe).
void Reverse ()
 Reverses the order of all characters in the string, so that e.g.
virtual bool IsFixedSize () const
 Part of the Flattenable interface.
virtual uint32 TypeCode () const
 Part of the Flattenable interface.
virtual uint32 FlattenedSize () const
 Part of the Flattenable interface.
virtual void Flatten (uint8 *buffer) const
 Part of the Flattenable interface.
virtual status_t Unflatten (const uint8 *buf, uint32 size)
 Unflattens a String from (buf).
status_t Prealloc (uint32 numChars)
 Makes sure that we have pre-allocated enough space for a NUL-terminated string at least (numChars) bytes long (not including the NUL byte).
String Arg (int8 value, const char *fmt="%i") const
 Returns a string like this string, but with the appropriate %# tokens replaced with a textual representation of the values passed in as (value).
String Arg (uint8 value, const char *fmt="%u") const
 As above, but for uint8 values.
String Arg (int16 value, const char *fmt="%i") const
 As above, but for int16 values.
String Arg (uint16 value, const char *fmt="%u") const
 As above, but for uint16 values.
String Arg (int32 value, const char *fmt=INT32_FORMAT_SPEC) const
 As above, but for int32 values.
String Arg (uint32 value, const char *fmt=UINT32_FORMAT_SPEC) const
 As above, but for uint32 values.
String Arg (int64 value, const char *fmt=INT64_FORMAT_SPEC) const
 As above, but for int64 values.
String Arg (uint64 value, const char *fmt=UINT64_FORMAT_SPEC) const
 As above, but for uint64 values.
String Arg (double value, const char *fmt="%f") const
 As above, but for double values.
String Arg (const String &value) const
 As above, but for string values.
String Arg (const char *value) const
 As above, but for C string values.
uint32 CalculateChecksum () const
 Returns a 32-bit checksum corresponding to this String's contents.
virtual bool AllowsTypeCode (uint32 code) const
 Should return true iff a buffer with uint32 (code) can be used to reconstruct this object's state.
status_t CopyTo (Flattenable &copyTo) const
 Causes (copyTo)'s state to set from this Flattenable, if possible.
status_t CopyFrom (const Flattenable &copyFrom)
 Causes our state to be set from (copyFrom)'s state, if possible.
status_t FlattenToByteBuffer (ByteBuffer &outBuf) const
 Convenience method.
Ref< ByteBufferFlattenToByteBuffer () const
 Convenience method.
status_t UnflattenFromByteBuffer (const ByteBuffer &buf)
 Convenience method.
status_t FlattenToDataIO (DataIO &outputStream, bool addSizeHeader) const
 Convenience method.
status_t UnflattenFromDataIO (DataIO &inputStream, int32 optReadSize, uint32 optMaxReadSize=MUSCLE_NO_LIMIT)
 Convenience method.

Static Public Member Functions

static void WriteData (uint8 *outBuf, uint32 *writeOffset, const void *copyFrom, uint32 blockSize)
 Convenience method for writing data into a byte buffer.
static status_t ReadData (const uint8 *inBuf, uint32 inputBufferBytes, uint32 *readOffset, void *copyTo, uint32 blockSize)
 Convenience method for safely reading bytes from a byte buffer.

Protected Member Functions

virtual status_t CopyFromImplementation (const Flattenable &copyFrom)
 Called by CopyFrom() and CopyTo().

Detailed Description

A character string class.

Represents a dynamically resizable NUL-terminated string.

Definition at line 29 of file String.h.


Constructor & Destructor Documentation

String::String ( const char *  str = NULL,
uint32  maxLen = MUSCLE_NO_LIMIT 
) [inline]

Constructor.

Parameters:
str If non-NULL, the initial value for this String.
maxLen The maximum number of characters to place into this String (not including the NUL terminator byte). Default is unlimited (i.e. scan the entire string no matter how long it is)

Definition at line 38 of file String.h.

References SetCstr().

Referenced by Substring().

String::String ( const String str  )  [inline]

Copy Constructor.

Parameters:
str String to become a copy of.

Definition at line 43 of file String.h.

References SetFromString().

String::String ( const String str,
uint32  beginIndex,
uint32  endIndex = MUSCLE_NO_LIMIT 
) [inline]

This constructor sets this String to be a substring of the specified String.

Parameters:
str String to become a copy of.
beginIndex Index of the first character in (str) to include.
endIndex Index after the last character in (str) to include. Defaults to a very large number, so that by default the entire remainder of the string is included.

Definition at line 51 of file String.h.

References SetFromString().


Member Function Documentation

String& String::operator= ( char  val  )  [inline]

Assignment Operator.

Sets this string to be a one-character string

Parameters:
val The character that should be the sole character in this String.

Definition at line 59 of file String.h.

References SetCstr().

String& String::operator= ( const char *  val  )  [inline]

Assignment Operator.

Parameters:
val Pointer to the C-style string to copy from. If NULL, this string will become "".

Definition at line 64 of file String.h.

References SetCstr().

String& String::operator= ( const String rhs  )  [inline]

Assignment Operator.

Parameters:
rhs String to become a copy of.

Definition at line 69 of file String.h.

References SetFromString().

String& String::operator+= ( const String rhs  ) 

Append Operator.

Parameters:
rhs A string to append to this string.

String& String::operator+= ( const char *  rhs  ) 

Append Operator.

Parameters:
rhs A string to append to this string. If NULL, this operation is a no-op.

String& String::operator+= ( char  ch  )  [inline]

Append Operator.

Parameters:
ch A character to append to this string.

Definition at line 84 of file String.h.

References Length().

String& String::operator-= ( const String rhs  ) 

Remove Operator.

Parameters:
rhs A substring to remove from this string; the last instance of the substring will be cut out. If (rhs) is not found, there is no effect.

String& String::operator-= ( const char  ch  ) 

Remove Operator.

Parameters:
ch A character to remove from this string; the last instance of this char will be cut out. If (ch) is not found, there is no effect.

String& String::operator<< ( const String rhs  )  [inline]

Append 'Stream' Operator.

Parameters:
rhs A String to append to this string.
Returns:
a non const String refrence to 'this' so you can chain appends.

Definition at line 112 of file String.h.

String& String::operator<< ( const char *  rhs  )  [inline]

Append 'Stream' Operator.

Parameters:
rhs A const char* to append to this string.
Returns:
a non const String refrence to 'this' so you can chain appends.

Definition at line 118 of file String.h.

String& String::operator<< ( int  rhs  ) 

Append 'Stream' Operator.

Parameters:
rhs An int to append to this string.
Returns:
a non const String refrence to 'this' so you can chain appends.

String& String::operator<< ( float  rhs  ) 

Append 'Stream' Operator.

Parameters:
rhs A float to append to this string. Formatting is set at 2 decimals of precision.
Returns:
a non const String refrence to 'this' so you can chain appends.

String& String::operator<< ( bool  rhs  ) 

Append 'Stream' Operator.

Parameters:
rhs A bool to append to this string. Converts to 'true' and 'false' strings appropriately.
Returns:
a non const String refrence to 'this' so you can chain appends.

bool String::operator== ( const String rhs  )  const [inline]

Comparison Operator.

Returns true if the two strings are equal (as determined by strcmp())

Parameters:
rhs A string to compare ourself with

Definition at line 141 of file String.h.

References Cstr(), and Length().

bool String::operator== ( const char *  rhs  )  const [inline]

Comparison Operator.

Returns true if the two strings are equal (as determined by strcmp())

Parameters:
rhs Pointer to a C string to compare with. NULL pointers are considered a synonym for "".

Definition at line 146 of file String.h.

References Cstr().

bool String::operator!= ( const String rhs  )  const [inline]

Comparison Operator.

Returns true if the two strings are not equal (as determined by strcmp())

Parameters:
rhs A string to compare ourself with

Definition at line 151 of file String.h.

bool String::operator!= ( const char *  rhs  )  const [inline]

Comparison Operator.

Returns true if the two strings are not equal (as determined by strcmp())

Parameters:
rhs Pointer to a C string to compare to. NULL pointers are considered a synonym for "".

Definition at line 156 of file String.h.

References Cstr().

bool String::operator< ( const String rhs  )  const [inline]

Comparison Operator.

Returns true if this string comes before (rhs) lexically.

Parameters:
rhs A string to compare ourself with

Definition at line 161 of file String.h.

References Cstr().

bool String::operator< ( const char *  rhs  )  const [inline]

Comparison Operator.

Returns true if this string comes before (rhs) lexically.

Parameters:
rhs Pointer to a C string to compare to. NULL pointers are considered a synonym for "".

Definition at line 166 of file String.h.

References Cstr().

bool String::operator> ( const String rhs  )  const [inline]

Comparison Operator.

Returns true if this string comes after (rhs) lexically.

Parameters:
rhs A string to compare ourself with

Definition at line 171 of file String.h.

References Cstr().

bool String::operator> ( const char *  rhs  )  const [inline]

Comparison Operator.

Returns true if this string comes after (rhs) lexically.

Parameters:
rhs Pointer to a C string to compare to. NULL pointers are considered a synonym for "".

Definition at line 176 of file String.h.

References Cstr().

bool String::operator<= ( const String rhs  )  const [inline]

Comparison Operator.

Returns true if the two strings are equal, or this string comes before (rhs) lexically.

Parameters:
rhs A string to compare ourself with

Definition at line 181 of file String.h.

References Cstr().

bool String::operator<= ( const char *  rhs  )  const [inline]

Comparison Operator.

Returns true if the two strings are equal, or this string comes before (rhs) lexically.

Parameters:
rhs Pointer to a C string to compare to. NULL pointers are considered a synonym for "".

Definition at line 186 of file String.h.

References Cstr().

bool String::operator>= ( const String rhs  )  const [inline]

Comparison Operator.

Returns true if the two strings are equal, or this string comes after (rhs) lexically.

Parameters:
rhs A string to compare ourself with

Definition at line 191 of file String.h.

References Cstr().

bool String::operator>= ( const char *  rhs  )  const [inline]

Comparison Operator.

Returns true if the two strings are equal, or this string comes after (rhs) lexically.

Parameters:
rhs Pointer to a C string to compare to. NULL pointers are considered a synonym for "".

Definition at line 196 of file String.h.

References Cstr().

char String::operator[] ( uint32  index  )  const [inline]

Array Operator.

Used to get easy access to the characters that make up this string.

Parameters:
index Index of the character to return. Be sure to only use valid indices!

Definition at line 201 of file String.h.

Referenced by CharAt().

char& String::operator[] ( uint32  index  )  [inline]

Array Operator.

Used to get easy access to the characters that make up this string.

Parameters:
index Index of the character to set. Be sure to only use valid indices!

Definition at line 206 of file String.h.

char String::CharAt ( uint32  index  )  const [inline]

Returns the character at the (index)'th position in the string.

Parameters:
index A value between 0 and (Length()-1), inclusive.
Returns:
A character value.

Definition at line 212 of file String.h.

References operator[]().

int String::CompareTo ( const String rhs  )  const [inline]

Compares this string to another string using strcmp().

Parameters:
rhs A string to compare ourself with

Definition at line 217 of file String.h.

References Cstr().

Referenced by CompareToIgnoreCase().

int String::CompareTo ( const char *  rhs  )  const [inline]

Compares this string to a C string using strcmp().

Parameters:
rhs Pointer to a C string to compare to. NULL pointers are considered a synonym for "".

Definition at line 222 of file String.h.

References Cstr().

void String::Clear (  )  [inline]

Clears this string so that it contains no characters.

Equivalent to setting it to "".

Definition at line 231 of file String.h.

status_t String::SetCstr ( const char *  str,
uint32  maxLen = MUSCLE_NO_LIMIT 
)

Sets our state from the given C-style string.

Parameters:
str The new string to copy from. If maxLen is negative, this may be NULL.
maxLen If set, the number of characters to copy (not including the NUL terminator byte). By default, the number of characters is determined automatically by scanning the string.

Referenced by operator=(), and String().

status_t String::SetFromString ( const String str,
uint32  beginIndex = 0,
uint32  endIndex = MUSCLE_NO_LIMIT 
)

Sets our state from the given String.

This is similar to the copy constructor, except that it allows you to optionally specify a maximum length, and it allows you to detect out-of-memory errors.

Parameters:
str The new string to copy from.
beginIndex Index of the first character in (str) to include. Defaults to zero, so that by default the entire string is included.
endIndex Index after the last character in (str) to include. Defaults to a very large number, so that by default the entire remainder of the string is included.
Returns:
B_NO_ERROR on success, or B_ERROR on failure (out of memory?)

Referenced by operator=(), and String().

bool String::EndsWith ( char  c  )  const [inline]

Returns true iff this string starts with (prefix).

Parameters:
c a character to check for at the end of this String.

Definition at line 262 of file String.h.

Referenced by EndsWithIgnoreCase().

bool String::EndsWith ( const String suffix  )  const [inline]

Returns true iff this string ends with (suffix).

Parameters:
suffix a String to check for at the end of this String.

Definition at line 267 of file String.h.

References Cstr(), and Length().

bool String::EndsWith ( const char *  suffix  )  const [inline]

Returns true iff this string ends with (suffix).

Parameters:
suffix a String to check for at the end of this String. NULL pointers are treated as a synonym for "".

Definition at line 272 of file String.h.

References Cstr(), and Length().

bool String::Equals ( const String str  )  const [inline]

Returns true iff this string is equal to (string), as determined by strcmp().

Parameters:
str a String to compare this String with.

Definition at line 282 of file String.h.

Referenced by EqualsIgnoreCase().

bool String::Equals ( const char *  str  )  const [inline]

Returns true iff this string is equal to (str), as determined by strcmp().

Parameters:
str Pointer to a C string to compare to. NULL pointers are considered a synonym for "".

Definition at line 287 of file String.h.

bool String::Equals ( char  c  )  const [inline]

Returns true iff this string contains a single character (c).

Parameters:
c a character to compare this String with.

Definition at line 292 of file String.h.

int String::IndexOf ( char  ch,
uint32  fromIndex = 0 
) const [inline]

Returns the first index of (ch) in this string starting at or after (fromIndex), or -1 if not found.

Parameters:
ch A character to look for in this string.
fromIndex Index of the first character to start searching at in this String. Defaults to zero (i.e. start from the first character)

Definition at line 298 of file String.h.

References Cstr(), and Length().

Referenced by IndexOfIgnoreCase(), and Substring().

int String::IndexOf ( const String str,
uint32  fromIndex = 0 
) const [inline]

Returns the first index of substring (str) in this string starting at or after (fromIndex), or -1 if not found.

Parameters:
str A String to look for in this string.
fromIndex Index of the first character to start searching at in this String. Defaults to zero (i.e. start from the first character)

Definition at line 308 of file String.h.

References Cstr(), and Length().

int String::IndexOf ( const char *  str,
uint32  fromIndex = 0 
) const [inline]

Returns the first index of substring (str) in this string starting at or after (fromIndex), or -1 if not found.

Parameters:
str Pointer to a C string to compare to. NULL pointers are considered a synonym for "".
fromIndex Index of the first character to start searching at in this String. Defaults to zero (i.e. start from the first character)

Definition at line 318 of file String.h.

References Cstr(), and Length().

int String::LastIndexOf ( char  ch,
uint32  fromIndex = 0 
) const [inline]

Returns the last index of (ch) in this string starting at or after (fromIndex), or -1 if not found.

Parameters:
ch A character to look for in this string.
fromIndex Index of the first character to start searching at in this String. Defaults to zero (i.e. start from the first character)

Definition at line 328 of file String.h.

References Cstr(), and Length().

Referenced by LastIndexOf(), LastIndexOfIgnoreCase(), and Substring().

int String::LastIndexOf ( const String str  )  const [inline]

Returns the last index of substring (str) in this string.

Parameters:
str A String to look for in this string.

Definition at line