newspeoplefor developersdocumentationdownloads

nString Class Reference
[Data Types]

#include <nstring.h>

List of all members.


Detailed Description

A simple Nebula string class.

Very handy when strings must be stored or manipulated. Note that many Nebula interfaces hand strings around as char pointers, not nString objects.

The current implementation does not allocate extra memory if the string has less then 13 characters.

(C) 2001 RadonLabs GmbH

Definition at line 34 of file nstring.h.


Public Member Functions

 nString ()
 constructor
 nString (const char *str)
 constructor 1
 nString (const nString &rhs)
 copy constructor
 ~nString ()
 destructor
nStringoperator= (const nString &rhs)
 = operator
nStringoperator= (const char *rhs)
 = operator with string
nStringoperator+= (const char *rhs)
 += operator with char*
nStringoperator+= (const nString &rhs)
 += operator with string
char operator[] (int i) const
 Subscript operator (read only).
char & operator[] (int i)
 Subscript operator (writable).
void Set (const char *ptr, int length)
 set as char ptr, with explicit length
void Set (const char *str)
 set as char ptr
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 SetVector3 (const vector3 &v)
 set as vector3 value
void SetVector4 (const vector4 &v)
 set as vector4 value
void SetMatrix44 (const matrix44 &m)
 set as matrix44 value
const char * Get () const
 get string as char ptr
int AsInt () const
 return contents as integer
float AsFloat () const
 return contents as float
bool AsBool () const
 return contents as bool
vector3 AsVector3 () const
 return contents as vector3
vector4 AsVector4 () const
 return contents as vector4
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 IsValidVector3 () const
 return true if the content is a valid vector3
bool IsValidVector4 () const
 return true if the content is a valid vector4
bool IsValidMatrix44 () const
 return true if content is a valid matrix44
int 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 char *str)
 append character pointer
void Append (const nString &str)
 append string
void AppendRange (const char *str, uint numChars)
 append a range of characters
void AppendInt (int val)
 append int value
void AppendFloat (float val)
 append float value
void ToLower ()
 convert string to lower case
void ToUpper ()
 convert string to upper case
const char * GetFirstToken (const char *whiteSpace)
 *** OBSOLETE *** get first token (this will destroy the string)
const char * GetNextToken (const char *whiteSpace)
 *** OBSOLETE *** get next token (this will destroy the string)
int Tokenize (const char *whiteSpace, nArray< nString > &tokens) const
 tokenized string into a provided nString array
int Tokenize (const char *whiteSpace, uchar fence, nArray< nString > &tokens) const
 tokenized string, keep strings within fence characters intact
nString ExtractRange (int from, int numChars) const
 extract substring
void Strip (const char *charSet)
 terminate string at first occurrence of character in set
int FindStringIndex (const nString &v, int startIndex) const
 Index of first appearance of `v' starting from index `startIndex'.
int FindCharIndex (unsigned char c, int startIndex) const
 return index of character in string
void TerminateAtIndex (int index)
 terminate string at given index
bool ContainsCharFromSet (const char *charSet) const
 returns true if string contains character from set
void StripTrailingSlash ()
 strip slash at end of path, if exists
nString TrimLeft (const char *charSet) const
 delete characters from charset at left side of string
nString TrimRight (const char *charSet) const
 delete characters from charset at right side of string
nString Trim (const char *charSet) const
 trim characters from charset at both sides of string
nString Substitute (const char *str, const char *substStr) const
 substitute every occurrence of a string with another string
void SubstituteCharacter (char c, char subst)
 substitute every occurrence of a character with another character
void UTF8toANSI ()
 convert string in place from UTF-8 to 8-bit ANSI
void ANSItoUTF8 ()
 convert ANSI to UTF-8 in place
const char * GetExtension () const
 get pointer to extension (without the dot)
bool CheckExtension (const char *ext) const
 check if extension matches (no dot in the extension!)
void ConvertBackslashes ()
 convert backslashes to slashes
void StripExtension ()
 remove extension
nString ExtractFileName () const
 extract the part after the last directory separator
nString ExtractLastDirName () const
 extract the last directory of the path
nString ExtractDirName () const
 extract the part before the last directory separator
nString ExtractToLastSlash () const
 extract path until last slash
bool MatchPattern (const nString &pattern) const
 check if this string matches the given pattern
void Format (const char *fmtString,...)
 format string printf-style
void FormatWithArgs (const char *fmtString, va_list args)
 format string printf-style, taking a va_list
void ReplaceIllegalFilenameChars (char replacement)
 replace illegal filename characters
bool CheckValidCharSet (const nString &charSet) const
 return true if string only contains characters from charSet argument
void ReplaceChars (const char *charSet, char replacement)
 replace characters within a string

Static Public Member Functions

static nString Concatenate (const nArray< nString > &strArray, const nString &whiteSpace)
 concatenate array of strings into new string
static nString FromInt (int i)
 construct a string from an int
static nString FromFloat (float f)
 construct a string from a float
static nString FromBool (bool b)
 construct a string from a bool
static nString FromVector3 (const vector3 &v)
 construct a string from vector3
static nString FromVector4 (const vector4 &v)
 construct a string from vector4
static nString FromMatrix44 (const matrix44 &m)
 construct a string from matrix44

Protected Types

enum  { LOCALSTRINGSIZE = 14 }

Protected Member Functions

void Copy (const nString &src)
 copy contents
void Delete ()
 delete contents
char * GetLastSlash () const
 get pointer to last directory separator
size_t GetFormattedStringLength (const char *format, va_list argList) const
 calculate required length for the result of a printf-style format
void SetLength (int)
 Set the length, accounting for the union.

Protected Attributes

char * string
char localString [LOCALSTRINGSIZE]
ushort localStrLen
uint strLen

Friends

bool operator== (const nString &a, const nString &b)
 Is `a' equal to `b'?
bool operator!= (const nString &a, const nString &b)
 Is `a' unequal to `b'?
bool operator< (const nString &a, const nString &b)
 Is `a' smaller than `b'?
bool operator> (const nString &a, const nString &b)
 Is `a' greater than `b'?
bool operator<= (const nString &a, const nString &b)
 Is `a' smaller than or equal to `b'?
bool operator>= (const nString &a, const nString &b)
 Is `a' greater than or equal to `b'?

Member Enumeration Documentation

anonymous enum [protected]
 

Enumerator:
LOCALSTRINGSIZE 

Definition at line 225 of file nstring.h.


Constructor & Destructor Documentation

nString::nString  )  [inline]
 

constructor

Definition at line 245 of file nstring.h.

nString::nString const char *  str  )  [inline]
 

constructor 1

Definition at line 411 of file nstring.h.

nString::nString const nString rhs  )  [inline]
 

copy constructor

Definition at line 423 of file nstring.h.

nString::~nString  )  [inline]
 

destructor

Definition at line 272 of file nstring.h.


Member Function Documentation

nString & nString::operator= const nString rhs  )  [inline]
 

= operator

Definition at line 454 of file nstring.h.

nString & nString::operator= const char *  rhs  )  [inline]
 

= operator with string

Definition at line 469 of file nstring.h.

nString & nString::operator+= const char *  rhs  )  [inline]
 

+= operator with char*

Definition at line 574 of file nstring.h.

nString & nString::operator+= const nString rhs  )  [inline]
 

+= operator with string

Definition at line 585 of file nstring.h.

char nString::operator[] int  i  )  const [inline]
 

Subscript operator (read only).

  • 21-Sep-04 Johannes the '\0' is a valid part of the string

Definition at line 657 of file nstring.h.

char & nString::operator[] int  i  )  [inline]
 

Subscript operator (writable).

  • 21-Sep-04 Johannes the '\0' is a valid part of the string

Definition at line 673 of file nstring.h.

void nString::Set const char *  ptr,
int  length
[inline]
 

set as char ptr, with explicit length

Definition at line 282 of file nstring.h.

void nString::Set const char *  str  )  [inline]
 

set as char ptr

Definition at line 308 of file nstring.h.

void nString::SetInt int  val  )  [inline]
 

set as int value

Definition at line 323 of file nstring.h.

void nString::SetFloat float  val  )  [inline]
 

set as float value

Definition at line 333 of file nstring.h.

void nString::SetBool bool  val  )  [inline]
 

set as bool value

Definition at line 343 of file nstring.h.

void nString::SetVector3 const vector3 v  )  [inline]
 

set as vector3 value

Definition at line 360 of file nstring.h.

void nString::SetVector4 const vector4 v  )  [inline]
 

set as vector4 value

Definition at line 370 of file nstring.h.

void nString::SetMatrix44 const matrix44 m  )  [inline]
 

set as matrix44 value

Definition at line 380 of file nstring.h.

const char * nString::Get  )  const [inline]
 

get string as char ptr

Definition at line 436 of file nstring.h.

int nString::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!

Definition at line 1638 of file nstring.h.

float nString::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!

Definition at line 1651 of file nstring.h.

bool nString::AsBool  )  const [inline]
 

return contents as bool

Definition at line 1661 of file nstring.h.

vector3 nString::AsVector3  )  const [inline]
 

return contents as vector3

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

Definition at line 1687 of file nstring.h.

vector4 nString::AsVector4  )  const [inline]
 

return contents as vector4

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

Definition at line 1724 of file nstring.h.

matrix44 nString::AsMatrix44  )  const [inline]
 

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!

Definition at line 1704 of file nstring.h.

bool nString::IsValidInt  )  const [inline]
 

return true if the content is a valid integer

Definition at line 1559 of file nstring.h.

bool nString::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.

Definition at line 1570 of file nstring.h.

bool nString::IsValidBool  )  const [inline]
 

return true if the content is a valid bool

Definition at line 1580 of file nstring.h.

bool nString::IsValidVector3  )  const [inline]
 

return true if the content is a valid vector3

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

Definition at line 1603 of file nstring.h.

bool nString::IsValidVector4  )  const [inline]
 

return true if the content is a valid vector4

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

Definition at line 1614 of file nstring.h.

bool nString::IsValidMatrix44  )  const [inline]
 

return true if content is a valid matrix44

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

Definition at line 1625 of file nstring.h.

int nString::Length  )  const [inline]
 

return length of string

Definition at line 691 of file nstring.h.

void nString::Clear  )  [inline]
 

clear the string

Definition at line 705 of file nstring.h.

bool nString::IsEmpty  )  const [inline]
 

return true if string object is empty

Definition at line 715 of file nstring.h.

bool nString::IsValid  )  const [inline]
 

return true if string object is not empty

Definition at line 733 of file nstring.h.

void nString::Append const char *  str  )  [inline]
 

append character pointer

Definition at line 528 of file nstring.h.

void nString::Append const nString str  )  [inline]
 

append string

Definition at line 540 of file nstring.h.

void nString::AppendRange const char *  str,
uint  numChars
[inline]
 

append a range of characters

Definition at line 483 of file nstring.h.

void nString::AppendInt int  val  )  [inline]
 

append int value

Definition at line 550 of file nstring.h.

void nString::AppendFloat float  val  )  [inline]
 

append float value

Definition at line 562 of file nstring.h.

void nString::ToLower  )  [inline]
 

convert string to lower case

Definition at line 743 of file nstring.h.

void nString::ToUpper  )  [inline]
 

convert string to upper case

Definition at line 762 of file nstring.h.

const char * nString::GetFirstToken const char *  whiteSpace  )  [inline]
 

*** OBSOLETE *** get first token (this will destroy the string)

Get the first token in the string, delimited by one or more of the character in 'whiteSpace'. This simply invokes the strtok() function internally, and will destroy the contained string. After calling GetFirstToken(), invoke GetNextToken() until 0 returns.

ATTENTION: if somebody else calls strtok() while GetFirstToken()/ GetNextToken() is underway, everything will break apart! Check out the Tokenize() method for a better alternative.

Parameters:
whiteSpace string containing white space characters

Definition at line 804 of file nstring.h.

const char * nString::GetNextToken const char *  whiteSpace  )  [inline]
 

*** OBSOLETE *** get next token (this will destroy the string)

Get the next token in the string. Call this after a GetFirstToken() or GetNextToken(). Returns 0 if no more tokens in string. This method will destroy the original string.

ATTENTION: if somebody else calls strtok() while GetFirstToken()/ GetNextToken() is underway, everything will break apart! Check out the Tokenize() method for a better alternative.

Parameters:
whiteSpace string containing whitespace characters

Definition at line 824 of file nstring.h.

int nString::Tokenize const char *  whiteSpace,
nArray< nString > &  tokens
const [inline]
 

tokenized string into a provided nString array

Tokenize the string into a provided nString array. Returns the number of tokens. This method is recommended over GetFirstToken()/GetNextToken(), since it is atomic. This nString object will not be destroyed (as is the case with GetFirstToken()/GetNextToken().

Parameters:
whiteSpace [in] a string containing the whitespace characters
tokens [out] nArray<nString> where tokens will be appended
Returns:
number of tokens found

Definition at line 843 of file nstring.h.

int nString::Tokenize const char *  whiteSpace,
uchar  fence,
nArray< nString > &  tokens
const [inline]
 

tokenized 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.

Definition at line 875 of file nstring.h.

nString nString::ExtractRange int  from,
int  numChars
const [inline]
 

extract substring

Extract sub string.

Definition at line 921 of file nstring.h.

void nString::Strip const char *  charSet  )  [inline]
 

terminate string at first occurrence of character in set

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

Definition at line 938 of file nstring.h.

int nString::FindStringIndex const nString v,
int  startIndex
const [inline]
 

Index of first appearance of `v' starting from index `startIndex'.

Returns:
Index or -1 if not found.

Definition at line 971 of file nstring.h.

int nString::FindCharIndex unsigned char  c,
int  startIndex
const [inline]
 

return index of character in string

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

Definition at line 998 of file nstring.h.

void nString::TerminateAtIndex int  index  )  [inline]
 

terminate string at given index

Terminates the string at the given index.

Definition at line 1018 of file nstring.h.

bool nString::ContainsCharFromSet const char *  charSet  )  const [inline]
 

returns true if string contains character from set

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

Definition at line 957 of file nstring.h.

void nString::StripTrailingSlash  )  [inline]
 

strip slash at end of path, if exists

Strips last slash, if the path name ends on a slash.

Definition at line 1032 of file nstring.h.

nString nString::TrimLeft const char *  charSet  )  const [inline]
 

delete characters from charset at left side of string

Returns a new string which is this string, stripped on the left side by all characters in the char set.

Definition at line 1061 of file nstring.h.

nString nString::TrimRight const char *  charSet  )  const [inline]
 

delete characters from charset at right side of string

Returns a new string, which is this string, stripped on the right side by all characters in the char set.

Definition at line 1107 of file nstring.h.

nString nString::Trim const char *  charSet  )  const [inline]
 

trim characters from charset at both sides of string

Trim both sides of a string.

Definition at line 1153 of file nstring.h.

nString nString::Substitute const char *  matchStr,
const char *  substStr
const [inline]
 

substitute every occurrence of a string with another string

Substitute every occurrence of origStr with substStr.

Definition at line 1164 of file nstring.h.

void nString::SubstituteCharacter char  c,
char  subst
[inline]
 

substitute every occurrence of a character with another character

Replace character with another.

Definition at line 1290 of file nstring.h.

void nString::UTF8toANSI  )  [inline]
 

convert string in place 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

Definition at line 1199 of file nstring.h.

void nString::ANSItoUTF8  )  [inline]
 

convert ANSI to UTF-8 in place

Convert contained ANSI string to UTF-8 in place.

Definition at line 1257 of file nstring.h.

const char * nString::GetExtension  )  const [inline]
 

get pointer to extension (without the dot)

Returns:
pointer to extension (without the dot), or 0

Definition at line 1320 of file nstring.h.

bool nString::CheckExtension const char *  ext  )  const [inline]
 

check if extension matches (no dot in the extension!)

Returns true if file extension matches.

Parameters:
ext extension string (without the dot)
Returns:
true if extension matches

Definition at line 1344 of file nstring.h.

void nString::ConvertBackslashes  )  [inline]
 

convert backslashes to slashes

Converts backslashes to slashes.

Definition at line 1309 of file nstring.h.

void nString::StripExtension  )  [inline]
 

remove extension

Remove the file extension.

Definition at line 1361 of file nstring.h.

nString nString::ExtractFileName  )  const [inline]
 

extract the part after the last directory separator

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

Definition at line 1393 of file nstring.h.

nString nString::ExtractLastDirName  )  const [inline]
 

extract the last directory of the path

Return a nString 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

Definition at line 1417 of file nstring.h.

nString nString::ExtractDirName  )  const [inline]
 

extract the part before the last directory separator

Return a nString 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

Definition at line 1458 of file nstring.h.

nString nString::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.

Definition at line 1488 of file nstring.h.

bool nString::MatchPattern const nString pattern  )  const [inline]
 

check if this string matches the given pattern

Definition at line 1508 of file nstring.h.

void nString::Format const char *  fmtString,
  ...
[inline]
 

format string printf-style

Definition at line 1738 of file nstring.h.

void nString::FormatWithArgs const char *  fmtString,
va_list  args
[inline]
 

format string printf-style, taking a va_list

Definition at line 1751 of file nstring.h.

void nString::ReplaceIllegalFilenameChars char  replacement  )  [inline]
 

replace illegal filename characters

Definition at line 1805 of file nstring.h.

bool nString::CheckValidCharSet const nString charSet  )  const [inline]