Unicode String. More...

#include <rstring.h>

Inheritance diagram for RString:
[legend]
Collaboration diagram for RString:
[legend]

Public Member Functions

 RString (void)
 
 RString (const char *src)
 
 RString (const RChar *src)
 
 RString (const RChar *src, size_t len)
 
 RString (const std::string &src)
 
 RString (char car)
 
 RString (RChar car)
 
 RString (const RString &src)
 
Manipulation methods
void Clear (void)
 
RString ToUpper (void) const
 
RString ToLower (void) const
 
RString Trim (void) const
 
RString Trim (const RString &str) const
 
int Find (const RChar car, int pos=0, bool CaseSensitive=true) const
 
int FindStr (const RString &str, int pos=0, bool CaseSensitive=true) const
 
int FindAnyStr (const RString &str, int pos=0, bool CaseSensitive=true) const
 
void Replace (const RChar search, const RChar rep, bool first=false, int pos=0)
 
void ReplaceStr (const RString &search, const RString &rep, bool first=false, int pos=0)
 
bool Begins (const RString &begin, bool skip=false) const
 
bool Ends (const RString &end, bool skip=false) const
 
RString Mid (size_t idx, size_t len=(size_t)-1) const
 
bool IsAt (const RString &sub, int pos) const
 
void Insert (const RString &sub, int pos, size_t del=0)
 
void Split (iRContainer< RString > &elements, const RChar car, const RChar del=RChar(0)) const
 
void GuessWords (iRContainer< RString > &elements, bool hyphen=false) const
 
void Concat (const iRContainer< RString > &elements, const RChar car=RChar(0))
 
Operators
RCharoperator() (void)
 
const RCharoperator() (void) const
 
 operator const char * (void) const
 
 operator std::string () const
 
const RCharoperator[] (size_t idx) const
 
RCharoperator[] (size_t idx)
 
bool operator== (const RString &src) const
 
bool operator== (const char *src) const
 
bool operator== (const RChar *src) const
 
bool operator!= (const RString &src) const
 
bool operator!= (const char *src) const
 
bool operator!= (const RChar *src) const
 
RStringoperator= (const RString &src)
 
RStringoperator= (const RChar *src)
 
RStringoperator= (const char *src)
 
RStringoperator= (const std::string &src)
 
RStringoperator+= (const RString &src)
 
RStringoperator+= (const RChar *src)
 
RStringoperator+= (const char *src)
 
RStringoperator+= (const char src)
 
RStringoperator+= (const RChar src)
 
String to number methods
char ToChar (bool &valid) const
 
char ToChar (void) const
 
int ToInt (bool &valid) const
 
int ToInt (void) const
 
unsigned int ToUInt (bool &valid) const
 
unsigned int ToUInt (void) const
 
long ToLong (bool &valid) const
 
long ToLong (void) const
 
unsigned long ToULong (bool &valid) const
 
unsigned long ToULong (void) const
 
size_t ToSizeT (bool &valid) const
 
size_t ToSizeT (void) const
 
off_t ToOffT (bool &valid) const
 
off_t ToOffT (void) const
 
float ToFloat (bool &valid) const
 
float ToFloat (void) const
 
double ToDouble (bool &valid) const
 
double ToDouble (void) const
 
bool ToBool (bool &valid, bool strict) const
 
bool ToBool (bool strict) const
 
- Public Member Functions inherited from BasicString< RChar, RString >
void Copy (const RChar *text, size_t nb)
 
RStringoperator= (const RString &src)
 
RStringoperator= (const RChar *src)
 
size_t GetLen (void) const
 
size_t GetMaxLen (void) const
 
void SetLen (size_t len)
 
void SetLen (size_t len, const RString &str)
 
bool IsEmpty (void) const
 
void Clear (void)
 
RString ToUpper (void) const
 
RString ToLower (void) const
 
RString Trim (void) const
 
RString Trim (const RString &str) const
 
bool ContainOnlySpaces (void) const
 
RStringoperator+= (const RString &src)
 
RStringoperator+= (const RChar *src)
 
int Find (const RCharcar, int pos=0, bool CaseSensitive=true) const
 
int FindStr (const RString &str, int pos=0, bool CaseSensitive=true) const
 
int FindAnyStr (const RString &str, int pos=0, bool CaseSensitive=true) const
 
void Replace (const RCharsearch, const RCharrep, bool first=false, int pos=0)
 
void ReplaceStr (const RString &search, const RString &rep, bool first=false, int pos=0)
 
const RCharoperator[] (size_t idx) const
 
RCharoperator[] (size_t idx)
 
bool Begins (const RString &begin, bool skip=false) const
 
bool Ends (const RString &end, bool skip=false) const
 
RString Mid (size_t idx, size_t len=(size_t)-1) const
 
bool IsAt (const RString &sub, int pos) const
 
void Insert (const RString &sub, int pos, size_t del=0)
 
void Split (iRContainer< RString > &elements, const RCharcar, const RChardel) const
 
void GuessWords (iRContainer< RString > &elements, bool hyphen) const
 
void Concat (const iRContainer< RString > &elements, const RCharcar)
 
size_t HashIndex (size_t idx) const
 
 ~BasicString (void)
 

Static Public Member Functions

Number to string methods
static RString Number (const int nb)
 
static RString Number (const unsigned int nb)
 
static RString Number (const long nb)
 
static RString Number (const unsigned long nb)
 
static RString Number (const long long nb)
 
static RString Number (const unsigned long long nb)
 
static RString Number (const float nb, const char *format="%E")
 
static RString Number (const double nb, const char *format="%E")
 
static RString Number (const long double nb, const char *format="%LE")
 

Static Public Attributes

static const RString Null
 

Internal methods

void Copy (const RChar *text, size_t nb)
 
void Copy (const char *text, size_t nb)
 
const char * ToLatin1 (void) const
 
const RCharToUTF16 (void) const
 
std::string ToString (void) const
 
size_t GetLen (void) const
 
size_t GetMaxLen (void) const
 
void SetLen (size_t len)
 
void SetLen (size_t len, const RString &str)
 
bool IsEmpty (void) const
 
bool ContainOnlySpaces (void) const
 
static RCharLatin1ToUnicode (const char *src, size_t &len, size_t &maxlen)
 
char * UnicodeToLatin1 (bool escape=false) const
 
void GenerateException (const RString &msg) const
 

Methods related to containers

int Compare (const RString &src) const
 
int Compare (const char *src) const
 
int Compare (const RChar *src) const
 
size_t HashCode (size_t max) const
 
static int SortOrder (const void *a, const void *b)
 
static int SortDesOrder (const void *a, const void *b)
 

Additional Inherited Members

- Protected Member Functions inherited from BasicString< RChar, RString >
 BasicString (void)
 
 BasicString (const BasicString &str)
 
 BasicString (const RCharcar)
 
 BasicString (const RChar *src)
 
 BasicString (const RChar *src, size_t len)
 
void Copy (void)
 
- Static Protected Member Functions inherited from BasicString< RChar, RString >
static CharBuffer * GetDataNull (void)
 
- Protected Attributes inherited from BasicString< RChar, RString >
CharBuffer * Data
 
- Static Protected Attributes inherited from BasicString< RChar, RString >
static CharBuffer * DataNull
 

Detailed Description

Unicode String.

This class implements a unicode string (RChar*) as a class. The class uses a shared approach. For example in the following code:

RString str1("Hello Wold");
RString str2;
str2=str1;

The strings str1 and str2 use (as long as none of them is modified) the same copy of the characters string.

The RStringBuilder class is an efficient implementation to build string by adding characters.

Constructor & Destructor Documentation

RString ( void  )

Construct a empty string.

RString ( const char *  src)

Construct a string from a C-style string.

Parameters
srcC-style string used as reference.
RString ( const RChar src)

Construct a string from a Unicode array.

Parameters
srcArray used as reference.
RString ( const RChar src,
size_t  len 
)

Construct a string by doing a deep copy of the first characters of a C-style string.

Parameters
srcC-style string used as reference.
lenLength.
RString ( const std::string &  src)

Construct a string from a string.

Parameters
srcString used as reference.
RString ( char  car)
explicit

Construct a string from a character.

Parameters
carCharacter.
RString ( RChar  car)
explicit

Construct a string from a character.

Parameters
carCharacter.
RString ( const RString src)

Construct a string from another string.

Parameters
srcSource string.

Member Function Documentation

void Copy ( const RChar text,
size_t  nb 
)

Copy a certain number of characters in the string.

Parameters
textText to copy.
nbNumber of characters to copy.
void Copy ( const char *  text,
size_t  nb 
)

Copy a certain number of characters in the string.

Parameters
textText to copy.
nbNumber of characters to copy.
const char* ToLatin1 ( void  ) const

Transform the string into a "C String" in Latin1 encoding. The resulting array should be copied (and not destroyed) since it is an internal structure.

Returns
C String
const RChar* ToUTF16 ( void  ) const

Return the string in UTF16.

std::string ToString ( void  ) const

Get a normal C++ string representing the current string.

Returns
std::string.
static RChar* Latin1ToUnicode ( const char *  src,
size_t &  len,
size_t &  maxlen 
)
staticprivate

Transform a C string into an array of RChar. The resulting array should be destroyed by the caller of the function.

Parameters
srcC-style string.
lenLength of the string (computed by the function).
maxlenMaximum length (may be updated by the function).
char* UnicodeToLatin1 ( bool  escape = false) const
private

Transform the RString into C string. The resulting C string should be destroyed by the caller of the function.

Parameters
escapeEscape invalid sequences of bytes?
void GenerateException ( const RString msg) const
private

Generate an exception related to this string. Its contains:

"'string': msg".

Parameters
msgMessage to add.
size_t GetLen ( void  ) const
size_t GetMaxLen ( void  ) const
void SetLen ( size_t  len)

Set the length of the string. If the length is shorter that the current one, the string is truncated. If the length is greater than the current one, the internal buffer is updated. Any new space allocated contains arbitrary data.

Parameters
lenLength of the string.
void SetLen ( size_t  len,
const RString str 
)

Set the length of the string. If the length is shorter that the current one, the string is truncated. If the length is greater than the current one, the second string is used to fill the first string (eventually it is copied several times).

Parameters
lenLength of the string.
strString used to fill.
bool IsEmpty ( void  ) const
bool ContainOnlySpaces ( void  ) const
void Clear ( void  )

Clear the content of the string. The string is deallocated and set to RSting::Null. If the memory must not be deallocated (for example if the string holds multiple temporary values), it is better to use RString::SetLen.

RString ToUpper ( void  ) const

Get a uppercase version of the string.

Returns
String.
RString ToLower ( void  ) const

Get a lowercase version of the string.

Returns
String.
RString Trim ( void  ) const

This function return a string by stripping whitespace (or other characters) from the beginning and end of the string.

Returns
String.
RString Trim ( const RString str) const

This function return a string by stripping a given sub-string from the beginning and end of the string.

Parameters
strSub-string to strip.
Returns
String.
int Find ( const RChar  car,
int  pos = 0,
bool  CaseSensitive = true 
) const

Find the position of a given character in the string.

Parameters
carCharacter to find.
posPosition to start the search. Negative values start the search from the end.
CaseSensitiveIs the search case sensitive.
Returns
The position of the first occurrence or -1 if the character was not found.
int FindStr ( const RString str,
int  pos = 0,
bool  CaseSensitive = true 
) const

Find the position of a given string in the string.

Parameters
strString to find.
posPosition to start the search. Negative values start the search from the end.
CaseSensitiveIs the search case sensitive.
Returns
The position of the first occurrence or -1 if the character was not found.
int FindAnyStr ( const RString str,
int  pos = 0,
bool  CaseSensitive = true 
) const

Find the position of a character of a given set in the string.

Parameters
strString to find.
posPosition to start the search. Negative values start the search from the end.
CaseSensitiveIs the search case sensitive.
Returns
The position of the first occurrence or -1 if the character was not found.
void Replace ( const RChar  search,
const RChar  rep,
bool  first = false,
int  pos = 0 
)

Replace a given character in the string.

Parameters
searchCharacter to search.
repCharacter that will put in.
firstMust it stops after the first occurrence.
posPosition to start. Negative values start the search from the end.
void ReplaceStr ( const RString search,
const RString rep,
bool  first = false,
int  pos = 0 
)

Replace a given sub-string in the string.

Parameters
searchString to search.
repString that will put in.
firstMust it stops after the first occurrence.
posPosition to start. Negative values start the search from the end.
bool Begins ( const RString begin,
bool  skip = false 
) const

Look if a string begins with a given sub-string.

Parameters
beginSub-string to search for.
skipIf true, leading spaces are skipped for the comparison.
Returns
true if the string begins with the sub-string.
bool Ends ( const RString end,
bool  skip = false 
) const

Look if a string ends with a given sub-string.

Parameters
endSub-string to search for.
skipIf true, ending spaces are skipped for the comparison.
Returns
true if the string ends with the sub-string.
RString Mid ( size_t  idx,
size_t  len = (size_t)-1 
) const

Get a sub-string of a given string.

Parameters
idxIndex of the first character.
lenLength of the sub-string. If the length is not specified, the end of the string is copied.
Returns
A string containing the substring.
bool IsAt ( const RString sub,
int  pos 
) const

Verify if a given sub-string is at a given position.

Parameters
subSub-string.
posPosition. If negative, the sub-string is searched at a given position of the end.
Returns
true if the string at a given position.
void Insert ( const RString sub,
int  pos,
size_t  del = 0 
)

Insert a sub-string at a given position of the string.

Parameters
subSub-string to insert.
posPosition. If negative, the sub-string is searched at a given position of the end.
delNumber of characters that will be replaced by the sub-string. If del=(size_t)-1, all the characters after pos are deleted.
void Split ( iRContainer< RString > &  elements,
const RChar  car,
const RChar  del = RChar(0) 
) const

Split the string to find all the elements separated by a given character.

Parameters
elementsContainer that will hold the results. It is not emptied by the method.
carCharacter used as separator.
delDelimiter of an element.
void GuessWords ( iRContainer< RString > &  elements,
bool  hyphen = false 
) const

Try to guess the different words in a given string. In practice, it supposes that certain characters separates words (spaces, ponctuations, uppercase, etc.).

Parameters
elementsContainer that will hold the results. It is not emptied by the method.
hyphenShould a hyphen be considered as a separator?
void Concat ( const iRContainer< RString > &  elements,
const RChar  car = RChar(0) 
)

Concatenate a series of elements and, eventually, separated them by a given character.

Parameters
elementsContainer of elements to concatenate.
carCharacter used as separator.
int Compare ( const RString src) const

Lexically compares two strings and returns an integer less than, equal to, or greater than zero if this is less than, equal to, or greater than src.

Parameters
srcString to compare with.
See also
R::RContainer.
int Compare ( const char *  src) const

Lexically compares two strings and returns an integer less than, equal to, or greater than zero if this is less than, equal to, or greater than src.

Parameters
srcString to compare with.
See also
R::RContainer.
int Compare ( const RChar src) const

Lexically compares two strings and returns an integer less than, equal to, or greater than zero if this is less than, equal to, or greater than src.

Parameters
srcString to compare with.
See also
R::RContainer.
size_t HashCode ( size_t  max) const

Compute a hash code that in the range [0,max-1]. In practice, in computes a sum based on the Unicode values the characters.

This method is R::RHashContainer to manage string.

Parameters
maxMaximal value.
Returns
a number in the range [0,max-1].
static int SortOrder ( const void *  a,
const void *  b 
)
static

Static function used to order a container of strings in ascending order.

Parameters
aFirst string.
bSecond string.
See also
R::RContainer.
static int SortDesOrder ( const void *  a,
const void *  b 
)
static

Static function used to order a container of strings in descending order.

Parameters
aFirst string.
bSecond string.
See also
R::RContainer.
RChar* operator() ( void  )

Return the string. The resulting array should be copied (and not destroyed) since it is an internal structure.

const RChar* operator() ( void  ) const

Return the string. The resulting array should be copied (and not destroyed) since it is an internal structure.

operator const char * ( void  ) const

Return the string. The resulting array should be copied (and not destroyed) since it is an internal structure.

operator std::string ( ) const

Get a normal C++ string representing the current string.

Returns
std::string.
const RChar& operator[] ( size_t  idx) const

This function returns the character at a given position in the string. (Read only).

Parameters
idxPosition of the character.
RChar& operator[] ( size_t  idx)

This function returns the character at a given position in the string.

Parameters
idxPosition of the character.
bool operator== ( const RString src) const

Equal operator.

Parameters
srcString to compare with.
bool operator== ( const char *  src) const

Equal operator.

Parameters
srcString to compare with.
bool operator== ( const RChar src) const

Equal operator.

Parameters
srcString to compare with.
bool operator!= ( const RString src) const

Non-equal operator.

Parameters
srcString to compare with.
bool operator!= ( const char *  src) const

Non-equal operator.

Parameters
srcString to compare with.
bool operator!= ( const RChar src) const

Non-equal operator.

Parameters
srcString to compare with.
RString& operator= ( const RString src)

Assignment operator using another string.

Parameters
srcSource string.
RString& operator= ( const RChar src)

Assignment operator using another string.

Parameters
srcSource string.
RString& operator= ( const char *  src)

Assignment operator using a "C string".

Parameters
srcSource string.
RString& operator= ( const std::string &  src)

Assignment operator using a string.

Parameters
srcSource string.
RString& operator+= ( const RString src)

Add another string.

Parameters
srcSource string.
RString& operator+= ( const RChar src)

Add another string.

Parameters
srcSource string.
RString& operator+= ( const char *  src)

Add a "C string" to the string.

Parameters
srcSource string.
RString& operator+= ( const char  src)

Add a character to the string. If mutliple characters must be added, it is more efficient to use RStringBuilder.

Parameters
srcCharacter.
RString& operator+= ( const RChar  src)

Add a character to the string. If mutliple characters must be added, it is more efficient to use RStringBuilder.

Parameters
srcCharacter.
static RString Number ( const int  nb)
static

Transform an int to a string.

Parameters
nbNumber.
static RString Number ( const unsigned int  nb)
static

Transform an size_t to a string.

Parameters
nbNumber.
static RString Number ( const long  nb)
static

Transform a long to a string.

Parameters
nbNumber.
static RString Number ( const unsigned long  nb)
static

Transform an unsigned long to a string.

Parameters
nbNumber.
static RString Number ( const long long  nb)
static

Transform an unsigned long long to a string.

Parameters
nbNumber.
static RString Number ( const unsigned long long  nb)
static

Transform an unsigned long long to a string.

Parameters
nbNumber.
static RString Number ( const float  nb,
const char *  format = "%E" 
)
static

Transform a float to a string.

Parameters
nbNumber.
formatString representing the format.
static RString Number ( const double  nb,
const char *  format = "%E" 
)
static

Transform a double to a string.

Parameters
nbNumber.
formatString representing the format.
static RString Number ( const long double  nb,
const char *  format = "%LE" 
)
static

Transform a long double to a string.

Parameters
nbNumber.
formatString representing the format.
char ToChar ( bool &  valid) const

Try to transform a string into a char (as a number).

Parameters
validVariable becomes true if the conversion was done.
char ToChar ( void  ) const

Try to transform a string into a char (as a number). This version generates an exception if the string does not contain a valid number.

int ToInt ( bool &  valid) const

Try to transform a string into an integer.

Parameters
validVariable becomes true if the conversion was done.
int ToInt ( void  ) const

Try to transform a string into an integer. This version generates an exception if the string does not contain a valid number.

unsigned int ToUInt ( bool &  valid) const

Try to transform a string into an unsigned integer.

Parameters
validVariable becomes true if the conversion was done.
unsigned int ToUInt ( void  ) const

Try to transform a string into an unsigned integer. This version generates an exception if the string does not contain a valid number.

long ToLong ( bool &  valid) const

Try to transform a string into a long.

Parameters
validVariable becomes true if the conversion was done.
long ToLong ( void  ) const

Try to transform a string into a long. This version generates an exception if the string does not contain a valid number.

unsigned long ToULong ( bool &  valid) const

Try to transform a string into an unsigned long.

Parameters
validVariable becomes true if the conversion was done.
unsigned long ToULong ( void  ) const

Try to transform a string into an unsigned long. This version generates an exception if the string does not contain a valid number.

size_t ToSizeT ( bool &  valid) const

Try to transform a string into a size_t.

Parameters
validVariable becomes true if the conversion was done.
size_t ToSizeT ( void  ) const

Try to transform a string into a size_t. This version generates an exception if the string does not contain a valid number.

off_t ToOffT ( bool &  valid) const

Try to transform a string into a off_t.

Parameters
validVariable becomes true if the conversion was done.
off_t ToOffT ( void  ) const

Try to transform a string into a off_t. This version generates an exception if the string does not contain a valid number.

float ToFloat ( bool &  valid) const
  • Try to transform a string into a float.
    Parameters
    validVariable becomes true if the conversion was done.
float ToFloat ( void  ) const
  • Try to transform a string into a float. This version generates an exception if the string does not contain a valid number.
double ToDouble ( bool &  valid) const
  • Try to transform a string into a double.
    Parameters
    validVariable becomes true if the conversion was done.
double ToDouble ( void  ) const
  • Try to transform a string into a double. This version generates an exception if the string does not contain a valid number.
bool ToBool ( bool &  valid,
bool  strict 
) const

Try to transform a string into a boolean value. It recognizes the strings "0", "1", "true" and "false".

Parameters
validVariable becomes true if the conversion was done.
strictDefine if the string recognition is strict ("true" and "false") or if upper case letters are allowed.
bool ToBool ( bool  strict) const

Try to transform a string into a boolean value. It recognizes the strings "0", "1", "true" and "false".

Parameters
strictDefine if the string recognition is strict ("true" and "false") or if upper case letters are allowed.

This version generates an exception if the string does not contain a valid boolean value.

Field Documentation

const RString Null
static

Null string.