Main Page   Class Hierarchy   Compound List   File List   Compound Members  

MagiC::String Class Reference

Generic string and buffer class. More...

#include <mstring.h>

Inheritance diagram for MagiC::String::

Comparable Object List of all members.

Public Types

enum  quoteflags { QUOTE_NORMAL = 0, QUOTE_HTML = 1 }

Public Methods

 String ()
 String (const String &orig)
 String (const char *)
 String (char *str, bool own)
 String (const char *, int n)
 String (char *, uint n, bool own)
 String (int i, int base=10)
 String (uint i, int base=10)
 String (long i, int base=10)
 String (float f, char fmt='g', int prec=6)
 String (double f, char fmt='g', int prec=6)
virtual ~String ()
String& assign (char c)
String& operator= (const String &)
String& operator= (const char *)
String& operator= (char c)
int operator!= (const char *b) const
int operator== (const char *) const
int operator== (const String &b) const
int operator== (const Comparable &other) const
String& operator+= (const String &str)
String& operator+= (char c)
String operator+ (const String &str) const
String operator+ (const char *str) const
const char operator[] (uint n) const
char& operator[] (uint n)
bool isNull () const
void nullify ()
bool isEmpty () const
void empty ()
uint length () const
int maxLength () const
String arg (const String &str, int fieldwidth=0) const
String arg (const char *str, int fieldwidth=0) const
String arg (const char c, int fieldwidth=0) const
String arg (float x, int fieldwidth=0, char fmt='g', int prec=-1) const
String arg (double x, int fieldwidth=0, char fmt='g', int prec=-1) const
String arg (int x, int fieldwidth=0, int base=10) const
String arg (long x, int fieldwidth=0, int base=10) const
int find (const String &, uint start=0) const
int find (const char c, uint start=0) const
int findRev (const String &, int start=-1) const
int regmatch (const char *regexpr) const
int regmatch (const char *regexpr, Array< String > &target) const
int regmatch (RegExp &compiled, Array< String > &target) const
String mid (uint start, int len=-1) const
String left (uint n) const
String right (uint n) const
void upper () const
void lower () const
void split (Array< String > &target, const char delim='') const
void join (const Array< String > &source, const char delim='')
String& dellast (uint n)
void chop ()
String stripWhiteSpace () const
String simplifyWhiteSpace () const
void reserve (int amount)
void ensure (int amount)
void ensure_spontane (int amount)
void grow_spontane ()
char checksum ()
int fast_isequal (const String &other) const
String& hexcode (const String &other)
void quote (char quotechar='%', int flags=0)
void unquote (char quotechar='%', int flags=0)
ostream& dump (ostream &) const
ostream& operator>> (ostream &) const
istream& operator<< (istream &)
CArchive& operator>> (CArchive &) const
IStream& operator<< (IStream &)
int toInt () const
uint toUInt () const
long toLong () const
float toFloat () const
double toDouble () const
 operator const char * () const
char* getbuffer () const
virtual String* clone () const
virtual int hashfunc (int hashsize) const

Friends

class  MagiC::strformat

Detailed Description

Generic string and buffer class.

Supports cached hash-number calculation.

Definition at line 77 of file mstring.h.


Constructor & Destructor Documentation

String ( const String & orig )
 

Copy constructor.

Definition at line 60 of file mstring.cc.

Referenced by arg(), operator+(), and simplifyWhiteSpace().

String ( const char * chrp )
 

Create from a NULL-terminated string.

Definition at line 75 of file mstring.cc.

String ( char * str,
bool own )
 

Create String from a NULL-terminated char buffer.

Parameters:
str   NULL-terminated C-style string.
own   Should the String object take the ownership of the character buffer?

Definition at line 93 of file mstring.cc.

String ( const char * chrp,
int n )
 

Create from a NULL-terminated string with given maximum length.

Definition at line 101 of file mstring.cc.

String ( char * str,
uint maxLen,
bool own )
 

Create String from a NULL-terminated char buffer.

Note:
The buffer must contain the terminating \x00 character.
Parameters:
str   NULL-terminated C-style string.
maxLen   Maximum (allocated) length of the buffer. There must be allocated one byte more than this, to store the end \x00.
own   Should the String object take the ownership of the character buffer?

Definition at line 123 of file mstring.cc.

String ( int i,
int base = 10 ) [explicit]
 

Conversion from an integer.

Definition at line 168 of file mstring.cc.

String ( uint i,
int base = 10 ) [explicit]
 

Conversion from an integer.

Definition at line 175 of file mstring.cc.

String ( long i,
int base = 10 ) [explicit]
 

Conversion from an integer.

Definition at line 182 of file mstring.cc.

String ( float f,
char fmt = 'g',
int prec = 6 ) [explicit]
 

Conversion from a float.

Definition at line 205 of file mstring.cc.

String ( double f,
char fmt = 'g',
int prec = 6 ) [explicit]
 

Conversion from a double.

Definition at line 212 of file mstring.cc.


Member Function Documentation

String arg ( const char * str,
int fieldwidth = 0 ) const [inline]
 

Replaces the lowest occurrence of n in the string.

Definition at line 121 of file mstring.h.

String arg ( const String & replacement,
int fieldwidth = 0 ) const
 

Replaces the lowest occurrence of n in the string.

Definition at line 423 of file mstring.cc.

String & assign ( char c )
 

Assignment from a single character.

Definition at line 219 of file mstring.cc.

char checksum ( )
 

Calculates an 8-bit checksum for the string.

Definition at line 973 of file mstring.cc.

void chop ( )
 

Removes all trailing newline (\r,
) characters.

Definition at line 484 of file mstring.cc.

virtual String* clone ( ) const [virtual]
 

Clone should perform a full recursive cloning of the object.

The return type should vary by implementor.

Reimplemented from Object.

String & dellast ( uint n )
 

Deletes the last n characters from the string.

Definition at line 470 of file mstring.cc.

Referenced by chop().

void ensure ( int amount ) [inline]
 

Ensures that at least the given length is reserved for the string.

Definition at line 152 of file mstring.h.

Referenced by hexcode(), and operator+=().

void ensure_spontane ( int amount ) [inline]
 

Ensures that at least the given amount is reserved for the string, preferably a little (50%+4 characters) more.

Definition at line 153 of file mstring.h.

Referenced by operator+=(), and operator<<().

int fast_isequal ( const String & other ) const
 

Fast comparison operator, based on cached checksum (hash value).

Very quick if the hash values differ. OBSERVE! This method doesn't calculate the checksums for the strings; you must calculate them manually!

Definition at line 495 of file mstring.cc.

int find ( const char c,
uint n = 0 ) const
 

Searches for a single character.

Returns:
Position of the character, or -1 of not found.

Definition at line 677 of file mstring.cc.

int find ( const String & subs,
uint n = 0 ) const
 

Searches for a substring.

Returns:
Position of the substring, or -1 of not found.

Definition at line 666 of file mstring.cc.

int findRev ( const String & subs,
int n = -1 ) const
 

Reverse substring search.

If start<0, it is calculated from the end of the string.

Returns:
Position of the character, or -1 of not found.

Definition at line 689 of file mstring.cc.

char * getbuffer ( ) const [inline]
 

Returns a non-const pointer to the string buffer.

Dangerous.

Definition at line 180 of file mstring.h.

void grow_spontane ( ) [inline]
 

Grows the string somewhat (50%+4 characters).

Definition at line 154 of file mstring.h.

int hashfunc ( int hashsize ) const [virtual]
 

Computes hash value for the string.

Defined for the use of Comparable::hashfunc.

The hash function is simply h(c)=chashsize. Once the hash number has been calculated for the string, it is cached. This gives a considerable speedup for evaluating this method.

Reimplemented from Comparable.

Definition at line 992 of file mstring.cc.

Referenced by checksum().

String & hexcode ( const String & other )
 

Encodes the given string in two-character hex code.

Definition at line 508 of file mstring.cc.

bool isEmpty ( ) const [inline]
 

Is the string empty (can be empty without being NULL)?

Definition at line 112 of file mstring.h.

bool isNull ( ) const [inline]
 

Is the string NULL?

Definition at line 110 of file mstring.h.

void join ( const Array< String > & src,
const char delim = ' ' )
 

Forms the string by joining the substrings in the given array with the given delimiter.

Definition at line 836 of file mstring.cc.

String left ( uint n ) const
 

Returns new string that contains n leftmost letters of the string.

Definition at line 646 of file mstring.cc.

uint length ( ) const [inline]
 

Returns:
Length of the string.

Definition at line 114 of file mstring.h.

Referenced by arg().

int maxLength ( ) const [inline]
 

Returns the current maximum length of the buffer.

Definition at line 115 of file mstring.h.

String mid ( uint from,
int n = -1 ) const
 

Returns a substring (0-based indexing).

Definition at line 618 of file mstring.cc.

Referenced by left(), right(), and stripWhiteSpace().

String operator+ ( const char * str ) const
 

Concatenates a C-string to the string and returns the result.

Definition at line 369 of file mstring.cc.

String operator+ ( const String & other ) const
 

Concatenates a String to the string and returns the result.

Definition at line 362 of file mstring.cc.

String & operator+= ( char c )
 

Concatenates a single character to the string.

This operation is O(1), as it uses the ensure_spontane() to grow the allocated the length of the string buffer.

Definition at line 746 of file mstring.cc.

String & operator+= ( const String & other )
 

Concatenates another String to the string.

Definition at line 703 of file mstring.cc.

IStream& operator<< ( IStream & ) [virtual]
 

Loads the object from an archive.

Overload this to make the class readable.

Reimplemented from Object.

istream & operator<< ( istream & is ) [virtual]
 

Standard input operation from stream.

Note:
The string will be readoutput as null-terminated. If the string was used as a binary buffer, its contents may be truncated.

Reimplemented from Object.

Definition at line 307 of file mstring.cc.

String & operator= ( const char * chrp )
 

Assignment from a null-terminated C-style string.

Returns:
Self.

Definition at line 260 of file mstring.cc.

String & operator= ( const String & other )
 

Assignment from another String.

Returns:
Self.

Definition at line 233 of file mstring.cc.

int operator== ( const Comparable & other ) const [virtual]
 

Checks if the String equals another Comparable, which *MUST* be a string.

This implements the Comparable::operator== that is used for sorting

Reimplemented from Comparable.

Definition at line 734 of file mstring.cc.

int operator== ( const char * other ) const
 

Returns true if the string equals with the given char string.

Definition at line 720 of file mstring.cc.

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

ostream & operator>> ( ostream & out ) const [virtual]
 

Standard output operation to stream.

Note:
The string will be output as null-terminated. If the string was used as a binary buffer, its contents may be truncated.

Reimplemented from Object.

Definition at line 296 of file mstring.cc.

void quote ( char quotechar = '%',
int flags = 0 )
 

Quote non-printable characters with given escape character.

Parameters:
quotechar   Escape character for quotation, % by default.
flags   The default is QUOTE_NORMAL. QUOTE_HTML-flag quotes the spaces into +-characters.

Definition at line 526 of file mstring.cc.

int regmatch ( RegExp & compiled,
Array< String > & target ) const
 

As above, but uses a precompiled regular expression.

Definition at line 955 of file mstring.cc.

int regmatch ( const char * expr,
Array< String > & target ) const
 

Tries to match the given regular expression to the string.

The second version supports parameters in the regular expression.

Parameters:
regexpr   Regular expression.

Returns:
!= 0 if succesful and places the return values into the array.

Definition at line 946 of file mstring.cc.

int regmatch ( const char * expr ) const
 

Tries to match the given regular expression to the string.

Returns:
true if the string matches.

Definition at line 929 of file mstring.cc.

void reserve ( int amount )
 

Reserves exactly the given length for the string (shortens the string if necessary).

Definition at line 757 of file mstring.cc.

Referenced by join().

String right ( uint n ) const
 

Returns new string that contains n rightmost letters of the string.

Definition at line 651 of file mstring.cc.

String simplifyWhiteSpace ( ) const
 

Returns the string without any whitespace (space, tab, newline cr, ff, vt) characters in the beginning or the end, or any duplicates within the string.

Definition at line 873 of file mstring.cc.

void split ( Array< String > & trg,
const char delim = ' ' ) const
 

Splits the string into an array according to the given delimiter.

Definition at line 816 of file mstring.cc.

String stripWhiteSpace ( ) const
 

Returns the string without any whitespace (space, tab, newline, cr, ff, vt) characters in the beginning or the end.

Definition at line 860 of file mstring.cc.

void unquote ( char quotechar = '%',
int flags = 0 )
 

Unquote non-printable characters with given escape character.

Parameters:
quotechar   Escape character for quotation, % by default.
flags   The default is QUOTE_NORMAL. QUOTE_HTML-flag quotes the spaces into +-characters.

Definition at line 537 of file mstring.cc.


The documentation for this class was generated from the following files:
Generated at Tue Dec 4 19:53:32 2001 for MagiC++ by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001