Jive reference manual
List of all members | Public Types | Public Member Functions | Public Attributes | Related Functions
jem::String Class Reference

Encapsulates a character string. More...

#include <jem/base/String.h>

Public Types

typedef const char * Iterator
 A random access iterator type. More...
 

Public Member Functions

 String ()
 Creates an empty String. More...
 
 String (bool value)
 Initializes this string to the string representation of the boolean value. More...
 
 String (char value)
 Initializes this string to the character value. More...
 
 String (byte value)
 Initializes this string to the string representation of the byte value. More...
 
 String (short value)
 Initializes this string to the string representation of the short value. More...
 
 String (int value)
 Initializes this string to the string representation of the int value. More...
 
 String (long value)
 Initializes this string to the string representation of the long int value. More...
 
 String (float value)
 Initializes this string to the string representation of the float value. More...
 
 String (double value)
 Initializes this string to the string representation of the double value. More...
 
 String (const char *str)
 Initializes this string to a null-terminated character array. More...
 
 String (InputIterator first, InputIterator last)
 Constructs a string given two input iterators. More...
 
 String (const BasicString &lhs, const BasicString &rhs)
 Creates a string that is the catenation of two BasicString objects. More...
 
 String (const String &rhs)
 Creates a copy of another string. More...
 
 ~String ()
 Frees resources if necessary. More...
 
Stringoperator= (const String &rhs)
 Copies another string into this string. More...
 
Stringoperator= (const char *str)
 Copies a character array into this string. More...
 
void swap (String &rhs)
 Swaps the contents of this string with another string. More...
 
Iterator begin () const
 Returns an iterator pointing to the begin of this string. More...
 
Iterator end () const
 Returns an iterator pointing to the end of this string. More...
 
char front () const
 Returns the first character in this string. More...
 
char back () const
 Returns the last character in this string. More...
 
char operator[] (int i) const
 String subscript operator. More...
 
String operator[] (const Slice &s) const
 Slice operator that creates a slice of this string. More...
 
String operator[] (const SliceFromTo &s) const
 Overloaded slice operator. More...
 
String operator[] (const SliceTo &s) const
 Overloaded slice operator. More...
 
String operator[] (const SliceFrom &s) const
 Overloaded slice operator. More...
 
String operator[] (const SliceAll &) const
 Overloaded slice operator. More...
 
int size () const
 Returns the size of this string. More...
 
const char * addr (int i=0) const
 Returns a pointer to the i-th character in this string. More...
 
long hashValue () const
 Returns a hash value for this string. More...
 
String toUpper () const
 Returns this string with all characters translated to upper case. More...
 
String toLower () const
 Returns this string with all characters translated to lower case. More...
 
String stripWhite () const
 Returns this string stripped of white space. More...
 
bool equals (const String &rhs) const
 Comparison of two strings. More...
 
bool equals (const char *rhs) const
 Comparison of two strings. More...
 
bool equalsIgnoreCase (const BasicString &rhs) const
 Case insensitive comparison of two strings. More...
 
int compareTo (const BasicString &rhs) const
 Lexicographical comparison of two Strings. More...
 
int compareToIgnoreCase (const BasicString &rhs) const
 Case insensitive lexicographical comparison of two Strings. More...
 
int find (const BasicString &needle, int fromIndex) const
 Finds the first occurrence of a specified substring. More...
 
int find (char needle, int fromIndex) const
 Finds the first occurrence of a specified character. More...
 
int rfind (const BasicString &needle) const
 Finds the last occurrence of a specified substring. More...
 
int rfind (char needle) const
 Finds the last occurrence of a specified character. More...
 
int rfind (const BasicString &needle, int fromIndex) const
 Finds the last occurrence of a specified substring, starting at a specified index. More...
 
int rfind (char needle, int fromIndex) const
 Finds the last occurrence of a specified character, starting at a specified index. More...
 
String format (const char *fmt,...)
 Creates a string according to a printf-style format string. More...
 

Public Attributes

const char * ConstIterator
 A random access iterator type. More...
 

Related Functions

(Note that these are not member functions.)

void swap (String &lhs, String &rhs)
 Swaps two strings. More...
 
String catenation operators.

Catenation of strings and characters.

String operator+ (const String &lhs, const String &rhs)
 
String operator+ (const String &lhs, const char *rhs)
 
String operator+ (const char *lhs, const String &rhs)
 
String operator+ (const String &lhs, char rhs)
 
String operator+ (char lhs, const String &rhs)
 
String comparison operators.

Lexicographical comparison of strings.

bool operator== (const String &lhs, const String &rhs)
 
bool operator== (const String &lhs, const char *rhs)
 
bool operator== (const char *lhs, const String &rhs)
 
bool operator!= (const String &lhs, const String &rhs)
 
bool operator!= (const String &lhs, const char *rhs)
 
bool operator!= (const char *lhs, const String &rhs)
 
bool operator< (const String &lhs, const String &rhs)
 
bool operator< (const String &lhs, const char *rhs)
 
bool operator< (const char *lhs, const String &rhs)
 
bool operator<= (const String &lhs, const String &rhs)
 
bool operator<= (const String &lhs, const char *rhs)
 
bool operator<= (const char *lhs, const String &rhs)
 
bool operator> (const String &lhs, const String &rhs)
 
bool operator> (const String &lhs, const char *rhs)
 
bool operator> (const char *lhs, const String &rhs)
 
bool operator>= (const String &lhs, const String &rhs)
 
bool operator>= (const String &lhs, const char *rhs)
 
bool operator>= (const char *lhs, const String &rhs)
 
Input and output operators
io::DataInputoperator>> (io::DataInput &in, String &s)
 String de-serialization operator. More...
 
io::DataOutputoperator<< (io::DataOutput &out, const String &s)
 String serialization operator. More...
 
io::TextInputoperator>> (io::TextInput &in, String &s)
 Reads a string from a text input stream. More...
 
io::TextOutputoperator<< (io::TextOutput &out, const String &s)
 Writes a string to a text output stream. More...
 

Detailed Description

The String class represents character arrays. It provides a set of functions for accessing individual characters, for comparing strings, for finding substrings, for stripping white space from a string, and for translating all characters to uppercase or lowercase. The String class also overloads the + operator so that one can easily concatenate two strings.

Strings are immutable. This means that the individual characters in a string can not be modified. The only way to change the contents of a string is to call one of the assignment operators. Use an Array, or a similar type of object, if you need to modify the

Strings may share their contents through a thread-safe reference counting scheme. Very small strings, however, are stored directly in a String object itself. This implementation avoids the overhead associated with reference counting when the strings are small. Note that users of the String class do not need to be aware that strings may share their contents; they can safely assume that each String stores its own copy of its contents. In particular, there is no danger that modifications to one String will be visible in another String.

Note
Because String instances are used by the exception classes, the class String will never throw an exception itself.
Warning
The character array stored in a String object is not null-terminated. Use the CString class and the makeCString function to convert a String object to a null-terminated character array.
See also
CString.
Examples:
alloc.cpp, DataParser.cpp, GzipFileReader.cpp, GzipFileWriter.cpp, meshgen.cpp, Properties.cpp, String.cpp, and System.cpp.

Member Typedef Documentation

typedef const char* jem::String::Iterator

A random access iterator type pointing to elements of type const char. This iterator fulfills the requirements of the random access iterator category of the standard C++ library. Since a String is immutable, the Iterator type only provides read-only access to a String.

Constructor & Destructor Documentation

jem::String::String ( )

Creates an empty String.

Postcondition
this->size() == 0
jem::String::String ( bool  value)
explicit

Initilizes this String object to the string representation of the boolean value.

Parameters
value- a boolean value.
See also
Boolean::print().
jem::String::String ( char  value)
explicit

Initilizes this String object to the character value.

Parameters
value- a character.
Postcondition
this->size() == 1 &&
this->front() == value
jem::String::String ( byte  value)
explicit

Initilizes this String object to the string representation of the byte value.

Parameters
value- a byte.
See also
Integer::print().
jem::String::String ( short  value)
explicit

Initilizes this String object to the string representation of the short value.

Parameters
value- a short integer.
See also
Integer::print().
jem::String::String ( int  value)
explicit

Initilizes this String object to the string representation of the int value.

Parameters
value- an integer.
See also
Integer::print().
jem::String::String ( long  value)
explicit

Initilizes this String object to the string representation of the long int value.

Parameters
value- a long integer.
See also
Integer::print().
jem::String::String ( float  value)
explicit

Initilizes this String object to the string representation of the float value.

Parameters
value- a float.
See also
Float::print().
jem::String::String ( double  value)
explicit

Initilizes this String object to the string representation of the double value.

Parameters
value- a double.
See also
Float::print().
jem::String::String ( const char *  str)

Initializes this String to the null-terminated character array str. If str is NULL, this String is initialized to an empty String. Note that this contructor makes a copy of the array str.

Parameters
str- a null-terminated character array, or NULL.
Postcondition
this->equals( rhs ) == true
Warning
The character array stored in this String is not null-terminated.
jem::String::String ( InputIterator  first,
InputIterator  last 
)

Constructs a string containing the characters between the input iterators first and last.

Parameters
first- an input iterator pointing to the first character.
last- an input iterator pointing to one position past the last character.
Precondition
The input iterator last is reachable from first,
and the expression *first can be converted to type char.
Postcondition
this->size() == std::distance( first, last )
jem::String::String ( const BasicString lhs,
const BasicString rhs 
)

Creates a String that is the catenation of the BasicString lhs and the BasicString rhs. That is, the first lhs.size() characters of this String are equal to the characters in lhs, while the next rhs.size() characters are equal to the characters in rhs.

Parameters
lhs- the first BasicString to be catenated.
rhs- the second BasicString to be catenated.
Postcondition
(*this)[ slice(BEGIN,lhs.size()) ] == lhs &&
(*this)[ slice(lhs.size(),END) ] == rhs
jem::String::String ( const String rhs)

Creates a copy of the String rhs.

Parameters
rhs- the String to be copied.
Postcondition
this->equals( rhs ) == true
jem::String::~String ( )

Frees resources if necessary.

Member Function Documentation

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

Copies the String rhs into this String.

Parameters
rhs- the String to be copied.
Postcondition
this->equals( rhs ) == true
String& jem::String::operator= ( const char *  str)

Copies the null-terminated character array rhs into this String.

Parameters
stra null-terminated character array, or NULL.
Postcondition
this->equals( rhs ) == true
void jem::String::swap ( String rhs)

Equivalent with:

String tmp( rhs );
lhs = rhs;
rhs = tmp;
Parameters
rhs- the String with which this String should be interchanged.
Iterator jem::String::begin ( ) const

Returns an Iterator pointing to the first character of this string, or end() if this string is empty. The Iterator is valid until one of the assignment operators of this String is called, or until the destructor of this String is invoked.

Returns
An Iterator pointing to the first character of this String.
Iterator jem::String::end ( ) const

Returns an Iterator pointing to one position past the last character of this string. The Iterator is valid until one of the assignment operators of this String is called, or until the destructor of this String is called.

Returns
An Iterator pointing one position past the last character of this String.
char jem::String::front ( ) const

Returns the first character in this String.

Returns
The first character in this String.
Precondition
this->size() > 0
char jem::String::back ( ) const

Return the last character in this String.

Returns
The last character in this String.
Precondition
this->size() > 0
char jem::String::operator[] ( int  i) const

Returns the i-th character in this String.

Parameters
i- character index.
Returns
The i-th character in this String.
Precondition
i >= 0 && i < this->size()
String jem::String::operator[] ( const Slice s) const

Returns a String representing a slice of this String.

Returns
A slice of this String.
See also
Slice, slice().
String jem::String::operator[] ( const SliceFromTo s) const

Returns a String representing a contiguous slice of this String.

Returns
A contiguous slice of this String.
Precondition
s.last() <= this->size()
See also
SliceFromTo, slice().
String jem::String::operator[] ( const SliceTo s) const

Returns a String representing a slice that starts at the first character of this String.

Returns
A slice of this String.
Precondition
s.last() <= this->size()
See also
SliceTo, slice().
String jem::String::operator[] ( const SliceFrom s) const

Returns a String representing a slice that extends to the last character of this String.

Returns
A slice of this String.
See also
SliceFrom, slice().
String jem::String::operator[] ( const SliceAll ) const

Returns this String.

Returns
*this
See also
SliceAll, slice().
int jem::String::size ( ) const

Returns the size of this String.

Returns
The number of characters stored in this String.
Examples:
Properties.cpp, and System.cpp.
const char* jem::String::addr ( int  i = 0) const

Returns a pointer to the i-th character in this String. The pointer is valid until one of the assignment operators of this String is called, or until the destructor of this String is invoked.

Parameters
i- a character index.
Returns
The address of the i-th character in this String.
Precondition
i >= 0 && i <= this->size()
long jem::String::hashValue ( ) const

Computes a hash value for this String.

Returns
A hash value for this String.
String jem::String::toUpper ( ) const

Translates all characters in this String to upper case.

Returns
A copy of this String with all characters translated to upper case.
Examples:
String.cpp, and System.cpp.
String jem::String::toLower ( ) const

Translates all characters in this String to lower case.

Returns
A copy of this String with all characters translated to lower case.
String jem::String::stripWhite ( ) const

Strips this String of white space.

Returns
A copy of this String that has been stripped of leading and trailing white space.
Examples:
String.cpp.
bool jem::String::equals ( const String rhs) const

Compares this String to the string rhs. The result is true if and only if both strings represent the same character sequence.

Parameters
rhs- a String to which this String is compared.
Returns
true if both strings are equal, false otherwise.
Note
For performance reasons, the argument rhs has type String instead of type BasicString.
bool jem::String::equals ( const char *  rhs) const

Compares this String to the character array rhs. The result is true if and only if both strings represent the same character sequence. If the character array rhs equals NULL, it is viewed as a string with zero length.

Parameters
rhs- a null-terminated character array to which this String is compared.
Returns
true if both strings are equal, false otherwise.
Note
For performance reasons, the argument rhs has type const char* instead of type BasicString.
bool jem::String::equalsIgnoreCase ( const BasicString rhs) const

Compares this String to the argument string rhs, ignoring case considerations. The result is true if and only if both strings have the same size and the corresponding characters in both strings are equal ignoring case.

Parameters
rhs- a BasicString to which this String is compared.
Returns
true if both strings are equal ignoring case, false otherwise.
int jem::String::compareTo ( const BasicString rhs) const

Compares this String lexicographically to the argument string rhs. This function is similar to the standard C function strcmp().

Parameters
rhs- a BasicString to which this String is compared.
Returns
  • zero if this String equals the argument string;
  • a positive integer if this String lexicographically precedes the argument string;
  • a negative integer if the argument string lexicographically precedes this String.
int jem::String::compareToIgnoreCase ( const BasicString rhs) const

This function is similar to the function compareTo(), except that case considerations are ignored.

Parameters
rhs- a BasicString to which this String is compared.
Returns
An integer indicating whether this String is larger than, equal to, or smaller than the argument string.
int jem::String::find ( const BasicString needle,
int  fromIndex 
) const

Determines the smallest integer k such that

k >= fromIndex &&
(*this)[ slice(k,k+needle.size()) ].equals( needle ) == true

Parameters
needle- the substring to be found.
fromIndex- the character index from which the search is started. fromIndex may have any value.
Returns
k, if such an integer can be found. Otherwise a negative number is returned.
Examples:
String.cpp.
int jem::String::find ( char  needle,
int  fromIndex 
) const

Determines the smallest integer k such that

k >= fromIndex && k < this->size() &&
(*this)[k] == needle

Parameters
needle- the character to be found.
fromIndex- the character index from which the search is started. fromIndex may have any value.
Returns
k, if such an integer can be found. Otherwise a negative number is returned.
int jem::String::rfind ( const BasicString needle) const

Finds the last occurrence of a specified substring.

Parameters
needle- the substring to be found.
Returns
this->rfind( needle, this->size() )
Examples:
DataParser.cpp, and String.cpp.
int jem::String::rfind ( char  needle) const

Finds the last occurrence of a specified character.

Parameters
needle- the character to be found.
Returns
this->rfind( needle, this->size() )
int jem::String::rfind ( const BasicString needle,
int  fromIndex 
) const

Determines the largest integer k such that

k < fromIndex &&
(*this)[ slice(k,k+needle.size()) ].equals( needle )

Parameters
needle- the substring to be found.
fromIndex- the character index from which the backward search is started. fromIndex may have any value.
Returns
k, if such a number can be found. Otherwise a negative number is returned.
int jem::String::rfind ( char  needle,
int  fromIndex 
) const

Determines the largest integer k such that

k >= 0 && k < fromIndex && (*this)[k] == needle

Parameters
needle- the character to be found.
fromIndex- the character index from which the backward search is started. fromIndex may have any value.
Returns
k, if such a number can be found. Otherwise a negative number is returned.
String jem::String::format ( const char *  fmt,
  ... 
)

Creates a String according to the format string fmt. This function is similar to the standard C function printf, except that it recognises only the following conversion specifiers: %%, %d, %i %o, %u, %x, %X, %f, %g, %e, %G, %E, %c, %s and %p (see the manual pages for printf for their meaning). Any special formatting flags, such as the field width, are not supported.

This format string fmt may also contain the special format specifier %S. The argument for this specifier must be a pointer to a String object.

Parameters
fmt- a null-terminated format string.
Returns
A String formatted according to the format string fmt.
Note
If the format string contains an illegal conversion specifier the program will be aborted.
Examples:
String.cpp.

Friends And Related Function Documentation

String operator+ ( const String lhs,
const String rhs 
)
related

Returns the catenation of two strings.

Parameters
lhs- a String.
rhs- another String.
Returns
String( lhs, rhs )
String operator+ ( const String lhs,
const char *  rhs 
)
related

Returns the catenation of two strings.

Parameters
lhs- a String.
rhs- a null-terminated character array, or NULL.
Returns
String( lhs, rhs )
String operator+ ( const char *  lhs,
const String rhs 
)
related

Returns the catenation of two strings.

Parameters
lhs- a null-terminated character array, or NULL.
rhs- a String.
Returns
String( lhs, rhs )
String operator+ ( const String lhs,
char  rhs 
)
related

Returns the catenation of a string and a character.

Parameters
lhs- a String.
rhs- a character.
Returns
String( lhs, BasicString( &rhs, 1 ) )
String operator+ ( char  lhs,
const String rhs 
)
related

Returns the catenation of a string and a character.

Parameters
lhs- a character.
rhs- a String.
Returns
String( BasicString( &rhs, 1 ), rhs )
bool operator== ( const String lhs,
const String rhs 
)
related

Tests whether two strings are equal.

Parameters
lhs- a String.
rhs- a String.
Returns
lhs.equals( rhs )
See also
equals.
bool operator== ( const String lhs,
const char *  rhs 
)
related

Tests whether two strings are equal.

Parameters
lhs- a String.
rhs- a null-terminated character array, or NULL.
Returns
lhs.equals( rhs )
See also
equals.
bool operator== ( const char *  lhs,
const String rhs 
)
related

Tests whether two strings are equal.

Parameters
lhs- a null-terminated character array, or NULL.
rhs- a String.
Returns
lhs.equals( rhs )
See also
equals.
bool operator!= ( const String lhs,
const String rhs 
)
related

Tests whether two strings are unequal.

Parameters
lhs- a String.
rhs- a String.
Returns
! lhs.equals( rhs )
See also
equals.
bool operator!= ( const String lhs,
const char *  rhs 
)
related

Tests whether two strings are unequal.

Parameters
lhs- a String.
rhs- a null-terminated character array, or NULL.
Returns
! lhs.equals( rhs )
See also
equals.
bool operator!= ( const char *  lhs,
const String rhs 
)
related

Tests whether two strings are unequal.

Parameters
lhs- a null-terminated character array, or NULL.
rhs- a String.
Returns
! lhs.equals( rhs )
See also
equals.
bool operator< ( const String lhs,
const String rhs 
)
related

Tests whether one string is lexicographically smaller than another string.

Parameters
lhs- a String.
rhs- a String.
Returns
lhs.compareTo( rhs ) < 0
See also
compareTo.
bool operator< ( const String lhs,
const char *  rhs 
)
related

Tests whether one string is lexicographically smaller than another string.

Parameters
lhs- a String.
rhs- a null-terminated character array, or NULL.
Returns
lhs.compareTo( rhs ) < 0
See also
compareTo.
bool operator< ( const char *  lhs,
const String rhs 
)
related

Tests whether one string is lexicographically smaller than another string.

Parameters
lhs- a null-terminated character array, or NULL.
rhs- a String.
Returns
lhs.compareTo( rhs ) < 0
See also
compareTo.
bool operator<= ( const String lhs,
const String rhs 
)
related

Tests whether one string is lexicographically smaller than or equal to another string.

Parameters
lhs- a String.
rhs- a String.
Returns
lhs.compareTo( rhs ) <= 0
See also
compareTo.
bool operator<= ( const String lhs,
const char *  rhs 
)
related

Tests whether one string is lexicographically smaller than or equal to another string.

Parameters
lhs- a String.
rhs- a a null-terminated character array, or NULL.
Returns
lhs.compareTo( rhs ) <= 0
See also
compareTo.
bool operator<= ( const char *  lhs,
const String rhs 
)
related

Tests whether one string is lexicographically smaller than or equal to another string.

Parameters
lhs- a a null-terminated character array, or NULL.
rhs- a String.
Returns
lhs.compareTo( rhs ) <= 0
See also
compareTo.
bool operator> ( const String lhs,
const String rhs 
)
related

Tests whether one string is lexicographically larger than another string.

Parameters
lhs- a String.
rhs- a String.
Returns
lhs.compareTo( rhs ) > 0
See also
compareTo.
bool operator> ( const String lhs,
const char *  rhs 
)
related

Tests whether one string is lexicographically larger than another string.

Parameters
lhs- a String.
rhs- a null-terminated character array, or NULL.
Returns
lhs.compareTo( rhs ) > 0
See also
compareTo.
bool operator> ( const char *  lhs,
const String rhs 
)
related

Tests whether one string is lexicographically larger than another string.

Parameters
lhs- a null-terminated character array, or NULL.
rhs- a String.
Returns
lhs.compareTo( rhs ) > 0
See also
compareTo.
bool operator>= ( const String lhs,
const String rhs 
)
related

Tests whether one string is lexicographically larger than or equal to another string.

Parameters
lhs- a String.
rhs- a String.
Returns
lhs.compareTo( rhs ) >= 0
See also
compareTo.
bool operator>= ( const String lhs,
const char *  rhs 
)
related

Tests whether one string is lexicographically larger than or equal to another string.

Parameters
lhs- a String.
rhs- a a null-terminated character array, or NULL.
Returns
lhs.compareTo( rhs ) >= 0
See also
compareTo.
bool operator>= ( const char *  lhs,
const String rhs 
)
related

Tests whether one string is lexicographically larger than or equal to another string.

Parameters
lhs- a a null-terminated character array, or NULL.
rhs- a String.
Returns
lhs.compareTo( rhs ) >= 0
See also
compareTo.
io::DataInput & operator>> ( io::DataInput in,
String s 
)
related

Reads a String from the data input stream in and assigns it to s.

Parameters
in- the DataInputStream from which the string is to be read.
s- the String to be read.
Returns
in
Exceptions
io::IOException- if an IO error occurs.
io::SerializationException- if the data input stream is corrupt.
io::DataOutput & operator<< ( io::DataOutput out,
const String s 
)
related

Writes the String s to the data output stream out. The de-serialization operator can be used to restore the String object.

Parameters
out- the data output stream to which the string is to be written.
s- the String to be written.
Returns
out
Exceptions
io::IOException- if an IO error occurs.
io::TextInput & operator>> ( io::TextInput in,
String s 
)
related

Skips white space and then reads a String from the text input stream in. This function is equivalent with:

s = String();
in.skipWhite();
for ( int c = in.read(); c >= 0 && ! isspace(c); c = in.read() ) {
s += (char) c;
}
in.pushBack( c );
return in;
Parameters
in- the text input stream from which the string is te be read.
s- the string to be read.
Returns
in
Exceptions
io::IOException- if an IO error occurs.
io::TextOutput & operator<< ( io::TextOutput out,
const String s 
)
related

Writes the contents of the String s to the text output stream out. This function is equivalent with:

out.write( s.addr(), s.size() ).

Parameters
out- the text output stream to which the string is to be written.
s- the string to be writtem.
Returns
out
Exceptions
io::IOException- if an IO error occurs.
void swap ( String lhs,
String rhs 
)
related

Swaps two strings. Equivalent with: lhs.swap( rhs ).

Parameters
lhs- a String.
rhs- another String.

Member Data Documentation

const char* jem::String::ConstIterator

A random access iterator type pointing to elements of type const char. This is an alias for the Iterator type.