v0.1.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Pages
CGUL::String Class Reference

A Utf-8 compliant string. More...

#include <String.hpp>

Public Member Functions

 String ()
 Initializes to "".
 
 String (const String &copy)
 Copies the contents of the string into a new string. More...
 
 String (const char *data)
 Copies raw character array data in a managed string object. More...
 
 String (const std::string &data)
 Copies a raw std::string object. More...
 
Stringoperator= (const char *operand)
 Copies the contents of a c string into this string. More...
 
Stringoperator= (const String &operand)
 Copies the contents of another string into this string. More...
 
char & operator[] (Size operand)
 Gets an individual byte from the string at the specified index.
 
const char operator[] (Size operand) const
 Gets an individual byte from the string at the specified index.
 
bool operator== (const String &operand) const
 Checks if two strings are equal. More...
 
bool operator!= (const String &operand) const
 Checks if two strings are not equal. More...
 
template<typename T >
String operator+ (const T &operand) const
 Attempts to convert a data type to string and concatinate it to this string, returning the result.
 
template<typename T >
Stringoperator+= (const T &operand)
 Attempts to convert a data type to string and concatinate it to this string.
 
bool operator< (const String &operand) const
 Checks if this string is lexicographically less than another. More...
 
Size GetSize () const
 Gets the size of the string in bytes. More...
 
Size GetLength () const
 Counts the number of code points in the string. More...
 
char GetChar (Size position) const
 Gets an individual character from the string. More...
 
Byte GetByte (Size position) const
 Gets an individual byte from the string. More...
 
Size GetCodePoint (Size start, UInt32 *codePoint) const
 Gets an individual code point from the string. More...
 
Boolean IsEmpty () const
 Checks if the string is empty. More...
 
Boolean IsNull () const
 Checks if the string is null. More...
 
std::string GetData () const
 An accessor to the wrapped std::string data. More...
 
const char * GetCString () const
 Accesses the raw character data from the wrapped std::string object. More...
 
UInt32 Count (const String &string) const
 
Size FindFirstOf (const String &string, Size offset=0) const
 Finds the first occurence of a string. More...
 
Size FindFirstOf (const Regex &expression, Size offset=0) const
 
Size FindLastOf (const String &string, Size offset=0) const
 Finds the last occurence of a string. More...
 
Size FindLastOf (const Regex &expression, Size offset=0) const
 
String SubString (Size offset, Size count=none, bool bytes=false) const
 Gets a part of the string. Supports both Utf-8 and individual bytes. More...
 
bool Contains (const String &string) const
 Checks if the string contains another string. More...
 
bool BeginsWith (const String &string) const
 Checks if the string begins with another string. More...
 
bool EndsWith (const String &string) const
 Checks if the string ends with another string. More...
 
StringSet (const char *str)
 Sets the value of the string given a c string. More...
 
StringSet (const String &str)
 Sets the value of the string given another string. More...
 
StringSetFormat (const char *str,...)
 Sets to a printf like format. More...
 
void Trim ()
 Trims whitespace at the beginning and end of the string. More...
 
void TrimStart ()
 Trims whitespace at the beginning of the string. More...
 
void TrimEnd ()
 Trims whitespace at the end of the string. More...
 
void Explode (const String &delimiter, FixedList< String > *debris) const
 
void Explode (const String &delimiter, Size limit, FixedList< String > *debris) const
 
void ToLower ()
 Converts the entire string to uppercase letters.
 
void ToUpper ()
 Converts the entire string to lowercase letters.
 
String GetLower () const
 Gets the string as all lowercase letters.
 
String GetUpper () const
 Gets the string as all uppercase letters.
 
void RemoveWhitespace ()
 Removes all whitespace from within a string. More...
 
void SetNullToEmpty ()
 Changes the string to empty if it is null.
 
void SetEmptyToNull ()
 Changes the string to null if it is empty.
 
template<typename T >
bool Is () const
 Checks if the string can be converted to the given data type. More...
 
template<typename T >
To () const
 Converts the string to the given data type. More...
 
template<typename T >
To (const T &fallback) const
 Converts the string to the given data type. More...
 
template<typename Type >
CGUL::String From (Type value)
 
template<typename T >
CGUL::String operator+ (const T &operand) const
 
template<typename T >
CGUL::Stringoperator+= (const T &operand)
 

Static Public Member Functions

static String FromCodePoint (UInt32 codePoint)
 
template<typename Type >
static String From (Type value)
 
static String Format (const char *str,...)
 

Static Public Attributes

static const Size none = (Size)-1
 

Friends

std::ostream & operator<< (std::ostream &stream, const String &string)
 

Detailed Description

A Utf-8 compliant string.

Constructor & Destructor Documentation

CGUL::String::String ( const String copy)

Copies the contents of the string into a new string.

Parameters
copyThe string to copy.
CGUL::String::String ( const char *  data)

Copies raw character array data in a managed string object.

Parameters
dataA character pointer to a null-terminated string.
CGUL::String::String ( const std::string &  data)

Copies a raw std::string object.

Parameters
Anstd::string value.

Member Function Documentation

bool CGUL::String::BeginsWith ( const String string) const

Checks if the string begins with another string.

Parameters
stringString to check for.
Returns
True if the string was found at the beginning.
bool CGUL::String::Contains ( const String string) const

Checks if the string contains another string.

Parameters
stringString to check for.
Returns
True if the string was found.
bool CGUL::String::EndsWith ( const String string) const

Checks if the string ends with another string.

Parameters
stringString to check for.
Returns
True if the string was found at the end.
CGUL::Size CGUL::String::FindFirstOf ( const String string,
Size  offset = 0 
) const

Finds the first occurence of a string.

Parameters
stringThe string to look for.
offsetAn offset to start looking for the string.
Returns
The index of the first character of the string.
CGUL::Size CGUL::String::FindLastOf ( const String string,
Size  offset = 0 
) const

Finds the last occurence of a string.

Parameters
stringThe string to look for.
offsetA backwards offset to start looking for the string.
Returns
The index of the first character of the string.
CGUL::Byte CGUL::String::GetByte ( Size  position) const

Gets an individual byte from the string.

Parameters
positionThe index of the byte.
Returns
The byte in the string.
char CGUL::String::GetChar ( Size  position) const

Gets an individual character from the string.

Parameters
positionThe index of the character.
Returns
The character in the string.
CGUL::Size CGUL::String::GetCodePoint ( Size  start,
UInt32 codePoint 
) const

Gets an individual code point from the string.

Parameters
startThe first byte of the code point.
codePointA pointer to the UInt32 variable to store the code point.

Example

#include <CGUL.hpp>
#include <iostream>
using namespace CGUL;
int main()
{
String str = "\u3053\u3093\u306b\u3061\u306f";
// ko n ni chi ha
UInt32 codePoint;
Size i = 0;
while (i < str.GetSize())
{
i += str.GetCodePoint(i, &codePoint);
std::cout << std::hex << codePoint << std::endl;
}
// outputs:
// 3053
// 3093
// 306b
// 3061
// 306f
}
const char * CGUL::String::GetCString ( ) const

Accesses the raw character data from the wrapped std::string object.

Returns
A constant character pointer of the data.
std::string CGUL::String::GetData ( ) const

An accessor to the wrapped std::string data.

Returns
The std::string variable.
CGUL::Size CGUL::String::GetLength ( ) const

Counts the number of code points in the string.

Returns
Number of code points.
CGUL::Size CGUL::String::GetSize ( ) const

Gets the size of the string in bytes.

Returns
Number of bytes.
template<typename T >
bool CGUL::String::Is ( ) const

Checks if the string can be converted to the given data type.

Returns
True if the string can be converted, false otherwise.
CGUL::Boolean CGUL::String::IsEmpty ( ) const

Checks if the string is empty.

Returns
True if the string is empty, false otherwise.
CGUL::Boolean CGUL::String::IsNull ( ) const

Checks if the string is null.

Returns
True if the string is null, false otherwise.
bool CGUL::String::operator!= ( const String operand) const

Checks if two strings are not equal.

Parameters
operandThe other string.
Returns
False if they're equal, true otherwise.
template<typename T >
CGUL::String CGUL::String::operator+ ( const T &  operand) const
Parameters
operandThe value to concatenate.
Returns
The new string.

Attempts to convert the value to a string using standard template library streams.

template<typename T >
CGUL::String& CGUL::String::operator+= ( const T &  operand)
Parameters
operandThe value to concatenate.
Returns
A reference to this object.

Attempts to convert the value to a string using standard template library streams.

bool CGUL::String::operator< ( const String operand) const

Checks if this string is lexicographically less than another.

Parameters
operandThe other string.
Returns
True if this string is lexicographically less than the other, false otherwise.
CGUL::String & CGUL::String::operator= ( const char *  operand)

Copies the contents of a c string into this string.

Parameters
operandThe c string or null.
Returns
A reference to this object.
CGUL::String & CGUL::String::operator= ( const String operand)

Copies the contents of another string into this string.

Parameters
operandThe other string.
Returns
A reference to this object.
bool CGUL::String::operator== ( const String operand) const

Checks if two strings are equal.

Parameters
operandThe other string.
Returns
True if they're equal, false otherwise.
void CGUL::String::RemoveWhitespace ( )

Removes all whitespace from within a string.

Removes spaces, tabs and the following whitespaces: \n \v \f \r

CGUL::String & CGUL::String::Set ( const char *  str)

Sets the value of the string given a c string.

Parameters
strThe c string.
Returns
A reference to this object.
CGUL::String & CGUL::String::Set ( const String str)

Sets the value of the string given another string.

Parameters
strThe c string.
Returns
A reference to this object.
CGUL::String & CGUL::String::SetFormat ( const char *  str,
  ... 
)

Sets to a printf like format.

Parameters
strPrintf-like string.
...Values.
Returns
A reference to this object.
CGUL::String CGUL::String::SubString ( Size  start,
Size  count = none,
bool  bytes = false 
) const

Gets a part of the string. Supports both Utf-8 and individual bytes.

Parameters
startThe first character to grab.
countThe number of characters to grab.
bytesIf true, this method will grab count number of bytes. If false, this method will grab count number of codepoints.

This method does not support backwards counting (supplying a negative start) like php's substr() and others.

Example

#include <CGUL.hpp>
#include <iostream>
using namespace CGUL;
// Note: this example only works in terminals with unicode support
int main()
{
String str = "\u3053\u3093\u306b\u3061\u306f";
// ko n ni chi ha
std::cout << str.SubString(2, 2) << std::endl;
// outputs: \u306b\u3061
// ni chi
std::cout << str.SubString(3, 3, true) << std::endl;
// outputs: \u3093
// n
}
template<typename T >
T CGUL::String::To ( ) const

Converts the string to the given data type.

Returns
The string as the given type.
template<typename T >
T CGUL::String::To ( const T &  fallback) const

Converts the string to the given data type.

Parameters
fallbackThe value to set to if the conversion failed.
Returns
The string as the given type.
void CGUL::String::Trim ( )

Trims whitespace at the beginning and end of the string.

Removes spaces, tabs and the following whitespaces: \n \v \f \r

Example

#include <CGUL.hpp>
#include <iostream>
using namespace CGUL;
int main()
{
String str;
str = " \t this is a test \t ";
str.Trim();
std::cout << "|" << str << "|" << std::endl;
// outputs: |this is a test|
}
void CGUL::String::TrimEnd ( )

Trims whitespace at the end of the string.

Removes spaces, tabs and the following whitespaces: \n \v \f \r

Example

#include <CGUL.hpp>
#include <iostream>
using namespace CGUL;
int main()
{
String str;
str = " \t this is a test \t ";
str.TrimEnd();
std::cout << "|" << str << "|" << std::endl;
// outputs: | this is a test|
}
void CGUL::String::TrimStart ( )

Trims whitespace at the beginning of the string.

Removes spaces, tabs and the following whitespaces: \n \v \f \r

Example

#include <CGUL.hpp>
#include <iostream>
using namespace CGUL;
int main()
{
String str;
str = " \t this is a test \t ";
str.TrimStart();
std::cout << "|" << str << "|" << std::endl;
// outputs: |this is a test |
}

The documentation for this class was generated from the following files: