The Nebula Device 3: Util::String Class Reference

The Nebula Device 3

Util::String Class Reference

#include <string.h>


Detailed Description

Nebula3's universal string class. An empty string object is always 32 bytes big. The string class tries to avoid costly heap allocations with the following tactics:

  • a local embedded buffer is used if the string is short enough
  • if a heap buffer must be allocated, care is taken to reuse an existing buffer instead of allocating a new buffer if possible (usually if an assigned string fits into the existing buffer, the buffer is reused and not re-allocated)

Heap allocations are performed through a local heap which should be faster then going through the process heap.

Besides the usual string manipulation methods, the String class also offers methods to convert basic Nebula3 datatypes from and to string, and a group of methods which manipulate filename strings.

(C) 2006 RadonLabs GmbH

Public Member Functions

 String ()
 constructor
 String (const String &rhs)
 copy constructor
 String (const char *cStr)
 construct from C string
 ~String ()
 destructor
void * operator new (size_t size)
 overloaded operator new
void operator delete (void *p)
 overloaded operator delete
void operator= (const String &rhs)
 assignment operator
void operator= (const char *cStr)
 assign from const char*
void operator+= (const String &rhs)
 += operator
const char operator[] (IndexT i) const
 read-only index operator
char & operator[] (IndexT i)
 read/write index operator
void Reserve (SizeT newSize)
 reserve internal buffer size to prevent heap allocs
SizeT Length () const
 return length of string
void Clear ()
 clear the string
bool IsEmpty () const
 return true if string object is empty
bool IsValid () const
 return true if string object is not empty
void Append (const String &str)
 append string
void Append (const char *str)
 append c-string
void AppendRange (const char *str, SizeT numChars)
 append a range of characters
void ToLower ()
 convert string to lower case
void ToUpper ()
 convert string to upper case
Array< StringTokenize (const String &whiteSpace) const
 tokenize string into a provided String array
Array< StringTokenize (const String &whiteSpace, char fence) const
 tokenize string, keep strings within fence characters intact
String ExtractRange (IndexT fromIndex, SizeT numChars) const
 extract substring
String ExtractToEnd (IndexT fromIndex) const
 extract substring to end of this string
void Strip (const String &charSet)
 terminate string at first occurence of character in set
IndexT FindStringIndex (const String &s, IndexT startIndex=0) const
 return start index of substring, or InvalidIndex if not found
IndexT FindCharIndex (char c, IndexT startIndex=0) const
 return index of character in string, or InvalidIndex if not found
void TerminateAtIndex (IndexT index)
 terminate string at given index
bool ContainsCharFromSet (const String &charSet) const
 returns true if string contains any character from set
void TrimLeft (const String &charSet)
 delete characters from charset at left side of string
void TrimRight (const String &charSet)
 delete characters from charset at right side of string
void Trim (const String &charSet)
 trim characters from charset at both sides of string
void SubstituteString (const String &str, const String &substStr)
 substitute every occurance of a string with another string
void SubstituteChar (char c, char subst)
 substiture every occurance of a character with another character
void __cdecl Format (const char *fmtString,...)
 format string printf-style
void __cdecl FormatArgList (const char *fmtString, va_list argList)
 format string printf-style with varargs list
bool CheckValidCharSet (const String &charSet) const
 return true if string only contains characters from charSet argument
void ReplaceChars (const String &charSet, char replacement)
 replace any char set character within a srtring with the replacement character
IndexT HashCode () const
 return a 32-bit hash code for the string
void UTF8toANSI ()
 convert string inplace from UTF-8 to 8-bit ANSI
void ANSItoUTF8 ()
 convert ANSI to UTF-8 in place
void SetCharPtr (const char *s)
 set content to char ptr
void Set (const char *ptr, SizeT length)
 set as char ptr, with explicit length
void SetInt (int val)
 set as int value
void SetFloat (float val)
 set as float value
void SetBool (bool val)
 set as bool value
void SetFloat4 (const Math::float4 &v)
 set as float4 value
void SetMatrix44 (const Math::matrix44 &v)
 set as matrix44 value
void AppendInt (int val)
 append int value
void AppendFloat (float val)
 append float value
void AppendBool (bool val)
 append bool value
void AppendFloat4 (const Math::float4 &v)
 append float4 value
void AppendMatrix44 (const Math::matrix44 &v)
 append matrix44 value
const char * AsCharPtr () const
 return contents as character pointer
int AsInt () const
 return contents as integer
float AsFloat () const
 return contents as float
bool AsBool () const
 return contents as bool
Math::float4 AsFloat4 () const
 return contents as float4
Math::matrix44 AsMatrix44 () const
 return contents as matrix44
bool IsValidInt () const
 return true if the content is a valid integer
bool IsValidFloat () const
 return true if the content is a valid float
bool IsValidBool () const
 return true if the content is a valid bool
bool IsValidFloat4 () const
 return true if the content is a valid float4
bool IsValidMatrix44 () const
 return true if content is a valid matrix44
String GetFileExtension () const
 get filename extension without dot
bool CheckFileExtension (const String &ext) const
 check file extension
void ConvertBackslashes ()
 convert backslashes to slashes
void StripFileExtension ()
 remove file extension
String ExtractFileName () const
 extract the part after the last directory separator
String ExtractLastDirName () const
 extract the last directory of the path
String ExtractDirName () const
 extract the part before the last directory separator
String ExtractToLastSlash () const
 extract path until last slash
void ReplaceIllegalFilenameChars (char replacement)
 replace illegal filename characters

Static Public Member Functions

static void Setup ()
 static Setup method called by Util::Setup
static String Concatenate (const Array< String > &strArray, const String &whiteSpace)
 concatenate array of strings into new string
static bool MatchPattern (const String &str, const String &pattern)
 pattern matching
static String FromInt (int i)
 construct a string from an int
static String FromFloat (float f)
 construct a string from a float
static String FromBool (bool b)
 construct a string from a bool
static String FromFloat4 (const Math::float4 &v)
 construct a string from float4
static String FromMatrix44 (const Math::matrix44 &m)
 construct a string from matrix44

Friends

bool operator== (const String &a, const String &b)
 equality operator
bool operator== (const String &a, const char *cStr)
 shortcut equality operator
bool operator== (const char *cStr, const String &a)
 shortcut equality operator
bool operator!= (const String &a, const String &b)
 inequality operator
bool operator< (const String &a, const String &b)
 less-then operator
bool operator> (const String &a, const String &b)
 greater-then operator
bool operator<= (const String &a, const String &b)
 less-or-equal operator
bool operator>= (const String &a, const String &b)
 greater-then operator

Member Function Documentation

char & Util::String::operator[] ( IndexT  i  )  [inline]

read/write index operator

NOTE: unlike the read-only indexer, the terminating 0 is NOT a valid part of the string because it may not be overwritten!!!

void Util::String::Reserve ( SizeT  newSize  )  [inline]

reserve internal buffer size to prevent heap allocs

Reserves internal space to prevent excessive heap re-allocations. If you plan to do many Append() operations this may help alot.

Array< String > Util::String::Tokenize ( const String whiteSpace  )  const

tokenize string into a provided String array

Tokenize the string into a String array.

Parameters:
whiteSpace a string containing the whitespace characters
Returns:
a string array of tokens

Array< String > Util::String::Tokenize ( const String whiteSpace,
char  fence 
) const

tokenize string, keep strings within fence characters intact

Tokenize a string, but keeps the string within the fence-character intact. For instance for the sentence:

He said: "I don't know."

A Tokenize(" ", '"', tokens) would return:

token 0: He token 1: said: token 2: I don't know.

String Util::String::ExtractRange ( IndexT  from,
SizeT  numChars 
) const

extract substring

Extract a substring range.

String Util::String::ExtractToEnd ( IndexT  fromIndex  )  const

extract substring to end of this string

Extract a substring until the end of the original string.

void Util::String::Strip ( const String charSet  ) 

terminate string at first occurence of character in set

Terminates the string at the first occurance of one of the characters in charSet.

IndexT Util::String::FindStringIndex ( const String s,
IndexT  startIndex = 0 
) const

return start index of substring, or InvalidIndex if not found

Return the index of a substring, or InvalidIndex if not found.

IndexT Util::String::FindCharIndex ( char  c,
IndexT  startIndex = 0 
) const

return index of character in string, or InvalidIndex if not found

Return index of character in string, or InvalidIndex if not found.

void Util::String::TerminateAtIndex ( IndexT  index  )  [inline]

terminate string at given index

Terminates the string at the given index.

bool Util::String::ContainsCharFromSet ( const String charSet  )  const [inline]

returns true if string contains any character from set

Returns true if string contains one of the characters from charset.

void Util::String::TrimLeft ( const String charSet  ) 

delete characters from charset at left side of string

Removes all characters in charSet from the left side of the string.

void Util::String::TrimRight ( const String charSet  ) 

delete characters from charset at right side of string

Removes all characters in charSet from the right side of the string.

void Util::String::Trim ( const String charSet  ) 

trim characters from charset at both sides of string

Trim both sides of a string.

void Util::String::SubstituteString ( const String matchStr,
const String substStr 
)

substitute every occurance of a string with another string

Substitute every occurance of origStr with substStr.

void Util::String::SubstituteChar ( char  c,
char  subst 
) [inline]

substiture every occurance of a character with another character

Replace character with another.

bool Util::String::CheckValidCharSet ( const String charSet  )  const [inline]

return true if string only contains characters from charSet argument

Return true if the string only contains characters which are in the defined character set.

bool Util::String::MatchPattern ( const String string,
const String pattern 
) [static]

pattern matching

Pattern-matching, TCL-style.

IndexT Util::String::HashCode (  )  const [inline]

return a 32-bit hash code for the string

This method computes a hash code for the string. The method is compatible with the Util::HashTable class.

void Util::String::UTF8toANSI (  ) 

convert string inplace from UTF-8 to 8-bit ANSI

This converts an UTF-8 string to 8-bit-ANSI. Note that only characters in the range 0 .. 255 are converted, all other characters will be converted to a question mark.

For conversion rules see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8

void Util::String::ANSItoUTF8 (  ) 

convert ANSI to UTF-8 in place

Convert contained ANSI string to UTF-8 in place.

void Util::String::Set ( const char *  str,
SizeT  length 
)

set as char ptr, with explicit length

Sets a new string content. This will handle all special cases and try to minimize heap allocations as much as possible.

int Util::String::AsInt (  )  const [inline]

return contents as integer

Returns content as integer. Note: this method doesn't check whether the contents is actually a valid integer. Use the IsValidInteger() method for this!

float Util::String::AsFloat (  )  const [inline]

return contents as float

Returns content as float. Note: this method doesn't check whether the contents is actually a valid float. Use the IsValidInt() method for this!

Math::float4 Util::String::AsFloat4 (  )  const [inline]

return contents as float4

Returns content as float4. Note: this method doesn't check whether the contents is actually a valid float4. Use the IsValidFloat4() method for this!

Math::matrix44 Util::String::AsMatrix44 (  )  const

return contents as matrix44

Returns content as matrix44. Note: this method doesn't check whether the contents is actually a valid matrix44. Use the IsValidMatrix44() method for this!

bool Util::String::IsValidFloat (  )  const [inline]

return true if the content is a valid float

Note: this method is not 100% correct, it just checks for invalid characters.

bool Util::String::IsValidFloat4 (  )  const [inline]

return true if the content is a valid float4

Note: this method is not 100% correct, it just checks for invalid characters.

bool Util::String::IsValidMatrix44 (  )  const [inline]

return true if content is a valid matrix44

Note: this method is not 100% correct, it just checks for invalid characters.

String Util::String::GetFileExtension (  )  const [inline]

get filename extension without dot

Returns:
string representing the filename extension (maybe empty)

void Util::String::ConvertBackslashes (  )  [inline]

convert backslashes to slashes

Converts backslashes to slashes.

void Util::String::StripFileExtension (  )  [inline]

remove file extension

Remove the file extension.

String Util::String::ExtractFileName (  )  const [inline]

extract the part after the last directory separator

Return a String object containing the part after the last path separator.

String Util::String::ExtractLastDirName (  )  const

extract the last directory of the path

Return a String object containing the last directory of the path, i.e. a category.

  • 17-Feb-04 floh fixed a bug when the path ended with a slash

String Util::String::ExtractDirName (  )  const

extract the part before the last directory separator

Return a String object containing the part before the last directory separator.

NOTE: I left my fix in that returns the last slash (or colon), this was necessary to tell if a dirname is a normal directory or an assign.

  • 17-Feb-04 floh fixed a bug when the path ended with a slash

String Util::String::ExtractToLastSlash (  )  const [inline]

extract path until last slash

Return a path string object which contains of the complete path up to the last slash. Returns an empty string if there is no slash in the path.