- cpp3ds
- String
Utility string class that automatically handles conversions between types and encodings. More...
#include <String.hpp>
Public Types | |
typedef std::basic_string< Uint32 >::iterator | Iterator |
Iterator type. More... | |
typedef std::basic_string< Uint32 >::const_iterator | ConstIterator |
Constant iterator type. More... | |
Public Member Functions | |
String () | |
Default constructor. More... | |
String (char ansiChar, const std::locale &locale=std::locale()) | |
Construct from a single ANSI character and a locale. More... | |
String (wchar_t wideChar) | |
Construct from single wide character. More... | |
String (Uint32 utf32Char) | |
Construct from single UTF-32 character. More... | |
String (const char *ansiString, const std::locale &locale=std::locale()) | |
Construct from a null-terminated C-style ANSI string and a locale. More... | |
String (const std::string &ansiString, const std::locale &locale=std::locale()) | |
Construct from an ANSI string and a locale. More... | |
String (const wchar_t *wideString) | |
Construct from null-terminated C-style wide string. More... | |
String (const std::wstring &wideString) | |
Construct from a wide string. More... | |
String (const Uint32 *utf32String) | |
Construct from a null-terminated C-style UTF-32 string. More... | |
String (const std::basic_string< Uint32 > &utf32String) | |
Construct from an UTF-32 string. More... | |
String (const String ©) | |
Copy constructor. More... | |
operator std::string () const | |
Implicit cast operator to std::string (ANSI string) More... | |
operator std::wstring () const | |
Implicit cast operator to std::wstring (wide string) More... | |
std::string | toAnsiString (const std::locale &locale=std::locale()) const |
Convert the unicode string to an ANSI string. More... | |
std::wstring | toWideString () const |
Convert the unicode string to a wide string. More... | |
std::basic_string< Uint8 > | toUtf8 () const |
Convert the unicode string to a UTF-8 string. More... | |
std::basic_string< Uint16 > | toUtf16 () const |
Convert the unicode string to a UTF-16 string. More... | |
std::basic_string< Uint32 > | toUtf32 () const |
Convert the unicode string to a UTF-32 string. More... | |
String & | operator= (const String &right) |
Overload of assignment operator. More... | |
String & | operator+= (const String &right) |
Overload of += operator to append an UTF-32 string. More... | |
Uint32 | operator[] (std::size_t index) const |
Overload of [] operator to access a character by its position. More... | |
Uint32 & | operator[] (std::size_t index) |
Overload of [] operator to access a character by its position. More... | |
void | clear () |
Clear the string. More... | |
std::size_t | getSize () const |
Get the size of the string. More... | |
bool | isEmpty () const |
Check whether the string is empty or not. More... | |
void | erase (std::size_t position, std::size_t count=1) |
Erase one or more characters from the string. More... | |
void | insert (std::size_t position, const String &str) |
Insert one or more characters into the string. More... | |
std::size_t | find (const String &str, std::size_t start=0) const |
Find a sequence of one or more characters in the string. More... | |
void | replace (std::size_t position, std::size_t length, const String &replaceWith) |
Replace a substring with another string. More... | |
void | replace (const String &searchFor, const String &replaceWith) |
Replace all occurrences of a substring with a replacement string. More... | |
String | substring (std::size_t position, std::size_t length=InvalidPos) const |
Return a part of the string. More... | |
const Uint32 * | getData () const |
Get a pointer to the C-style array of characters. More... | |
Iterator | begin () |
Return an iterator to the beginning of the string. More... | |
ConstIterator | begin () const |
Return an iterator to the beginning of the string. More... | |
Iterator | end () |
Return an iterator to the beginning of the string. More... | |
ConstIterator | end () const |
Return an iterator to the beginning of the string. More... | |
Static Public Member Functions | |
template<typename T > | |
static String | fromUtf8 (T begin, T end) |
Create a new cpp3ds::String from a UTF-8 encoded string. More... | |
template<typename T > | |
static String | fromUtf16 (T begin, T end) |
Create a new cpp3ds::String from a UTF-16 encoded string. More... | |
template<typename T > | |
static String | fromUtf32 (T begin, T end) |
Create a new cpp3ds::String from a UTF-32 encoded string. More... | |
Static Public Attributes | |
static const std::size_t | InvalidPos |
Represents an invalid position in the string. More... | |
Friends | |
bool | operator== (const String &left, const String &right) |
bool | operator< (const String &left, const String &right) |
Related Functions | |
(Note that these are not member functions.) | |
bool | operator== (const String &left, const String &right) |
Overload of == operator to compare two UTF-32 strings. More... | |
bool | operator!= (const String &left, const String &right) |
Overload of != operator to compare two UTF-32 strings. More... | |
bool | operator< (const String &left, const String &right) |
Overload of < operator to compare two UTF-32 strings. More... | |
bool | operator> (const String &left, const String &right) |
Overload of > operator to compare two UTF-32 strings. More... | |
bool | operator<= (const String &left, const String &right) |
Overload of <= operator to compare two UTF-32 strings. More... | |
bool | operator>= (const String &left, const String &right) |
Overload of >= operator to compare two UTF-32 strings. More... | |
String | operator+ (const String &left, const String &right) |
Overload of binary + operator to concatenate two strings. More... | |
Utility string class that automatically handles conversions between types and encodings.
cpp3ds::String is a utility string class defined mainly for convenience.
It is a Unicode string (implemented using UTF-32), thus it can store any character in the world (european, chinese, arabic, hebrew, etc.).
It automatically handles conversions from/to ANSI and wide strings, so that you can work with standard string classes and still be compatible with functions taking a cpp3ds::String.
Conversions involving ANSI strings use the default user locale. However it is possible to use a custom locale if necessary:
cpp3ds::String defines the most important functions of the standard std::string class: removing, random access, iterating, appending, comparing, etc. However it is a simple class provided for convenience, and you may have to consider using a more optimized class if your program requires complex string handling. The automatic conversion functions will then take care of converting your string to cpp3ds::String whenever SFML requires it.
Please note that SFML also defines a low-level, generic interface for Unicode handling, see the cpp3ds::Utf classes.
Definition at line 42 of file String.hpp.
typedef std::basic_string<Uint32>::const_iterator cpp3ds::String::ConstIterator |
Constant iterator type.
Definition at line 50 of file String.hpp.
typedef std::basic_string<Uint32>::iterator cpp3ds::String::Iterator |
Iterator type.
Definition at line 49 of file String.hpp.
cpp3ds::String::String | ( | ) |
Default constructor.
This constructor creates an empty string.
cpp3ds::String::String | ( | char | ansiChar, |
const std::locale & | locale = std::locale() |
||
) |
Construct from a single ANSI character and a locale.
The source character is converted to UTF-32 according to the given locale.
ansiChar | ANSI character to convert |
locale | Locale to use for conversion |
cpp3ds::String::String | ( | wchar_t | wideChar | ) |
Construct from single wide character.
wideChar | Wide character to convert |
cpp3ds::String::String | ( | Uint32 | utf32Char | ) |
Construct from single UTF-32 character.
utf32Char | UTF-32 character to convert |
cpp3ds::String::String | ( | const char * | ansiString, |
const std::locale & | locale = std::locale() |
||
) |
Construct from a null-terminated C-style ANSI string and a locale.
The source string is converted to UTF-32 according to the given locale.
ansiString | ANSI string to convert |
locale | Locale to use for conversion |
cpp3ds::String::String | ( | const std::string & | ansiString, |
const std::locale & | locale = std::locale() |
||
) |
Construct from an ANSI string and a locale.
The source string is converted to UTF-32 according to the given locale.
ansiString | ANSI string to convert |
locale | Locale to use for conversion |
cpp3ds::String::String | ( | const wchar_t * | wideString | ) |
Construct from null-terminated C-style wide string.
wideString | Wide string to convert |
cpp3ds::String::String | ( | const std::wstring & | wideString | ) |
Construct from a wide string.
wideString | Wide string to convert |
cpp3ds::String::String | ( | const Uint32 * | utf32String | ) |
Construct from a null-terminated C-style UTF-32 string.
utf32String | UTF-32 string to assign |
cpp3ds::String::String | ( | const std::basic_string< Uint32 > & | utf32String | ) |
Construct from an UTF-32 string.
utf32String | UTF-32 string to assign |
cpp3ds::String::String | ( | const String & | copy | ) |
Copy constructor.
copy | Instance to copy |
Iterator cpp3ds::String::begin | ( | ) |
Return an iterator to the beginning of the string.
ConstIterator cpp3ds::String::begin | ( | ) | const |
Return an iterator to the beginning of the string.
void cpp3ds::String::clear | ( | ) |
Iterator cpp3ds::String::end | ( | ) |
Return an iterator to the beginning of the string.
The end iterator refers to 1 position past the last character; thus it represents an invalid character and should never be accessed.
ConstIterator cpp3ds::String::end | ( | ) | const |
Return an iterator to the beginning of the string.
The end iterator refers to 1 position past the last character; thus it represents an invalid character and should never be accessed.
void cpp3ds::String::erase | ( | std::size_t | position, |
std::size_t | count = 1 |
||
) |
Erase one or more characters from the string.
This function removes a sequence of count characters starting from position.
position | Position of the first character to erase |
count | Number of characters to erase |
std::size_t cpp3ds::String::find | ( | const String & | str, |
std::size_t | start = 0 |
||
) | const |
Find a sequence of one or more characters in the string.
This function searches for the characters of str into the string, starting from start.
str | Characters to find |
start | Where to begin searching |
|
static |
Create a new cpp3ds::String from a UTF-16 encoded string.
begin | Forward iterator to the begining of the UTF-16 sequence |
end | Forward iterator to the end of the UTF-16 sequence |
|
static |
Create a new cpp3ds::String from a UTF-32 encoded string.
This function is provided for consistency, it is equivalent to using the constructors that takes a const cpp3ds::Uint32* or a std::basic_string<cpp3ds::Uint32>.
begin | Forward iterator to the begining of the UTF-32 sequence |
end | Forward iterator to the end of the UTF-32 sequence |
|
static |
Create a new cpp3ds::String from a UTF-8 encoded string.
begin | Forward iterator to the begining of the UTF-8 sequence |
end | Forward iterator to the end of the UTF-8 sequence |
const Uint32* cpp3ds::String::getData | ( | ) | const |
Get a pointer to the C-style array of characters.
This functions provides a read-only access to a null-terminated C-style representation of the string. The returned pointer is temporary and is meant only for immediate use, thus it is not recommended to store it.
std::size_t cpp3ds::String::getSize | ( | ) | const |
void cpp3ds::String::insert | ( | std::size_t | position, |
const String & | str | ||
) |
Insert one or more characters into the string.
This function inserts the characters of str into the string, starting from position.
position | Position of insertion |
str | Characters to insert |
bool cpp3ds::String::isEmpty | ( | ) | const |
cpp3ds::String::operator std::string | ( | ) | const |
Implicit cast operator to std::string (ANSI string)
The current global locale is used for conversion. If you want to explicitely specify a locale, see toAnsiString. Characters that do not fit in the target encoding are discarded from the returned string. This operator is defined for convenience, and is equivalent to calling toAnsiString().
cpp3ds::String::operator std::wstring | ( | ) | const |
Implicit cast operator to std::wstring (wide string)
Characters that do not fit in the target encoding are discarded from the returned string. This operator is defined for convenience, and is equivalent to calling toWideString().
Overload of += operator to append an UTF-32 string.
right | String to append |
Overload of assignment operator.
right | Instance to assign |
Uint32 cpp3ds::String::operator[] | ( | std::size_t | index | ) | const |
Overload of [] operator to access a character by its position.
This function provides read-only access to characters. Note: this function doesn't throw if index is out of range.
index | Index of the character to get |
Uint32& cpp3ds::String::operator[] | ( | std::size_t | index | ) |
Overload of [] operator to access a character by its position.
This function provides read and write access to characters. Note: this function doesn't throw if index is out of range.
index | Index of the character to get |
void cpp3ds::String::replace | ( | std::size_t | position, |
std::size_t | length, | ||
const String & | replaceWith | ||
) |
Replace a substring with another string.
This function replaces the substring that starts at index position and spans length characters with the string replaceWith.
position | Index of the first character to be replaced |
length | Number of characters to replace. You can pass InvalidPos to replace all characters until the end of the string. |
replaceWith | String that replaces the given substring. |
Replace all occurrences of a substring with a replacement string.
This function replaces all occurences of searchFor in this string with the string replaceWith.
searchFor | The value being searched for |
replaceWith | The value that replaces found searchFor values |
String cpp3ds::String::substring | ( | std::size_t | position, |
std::size_t | length = InvalidPos |
||
) | const |
Return a part of the string.
This function returns the substring that starts at index position and spans length characters.
position | Index of the first character |
length | Number of characters to include in the substring (if the string is shorter, as many characters as possible are included). InvalidPos can be used to include all characters until the end of the string. |
std::string cpp3ds::String::toAnsiString | ( | const std::locale & | locale = std::locale() | ) | const |
Convert the unicode string to an ANSI string.
The UTF-32 string is converted to an ANSI string in the encoding defined by locale. Characters that do not fit in the target encoding are discarded from the returned string.
locale | Locale to use for conversion |
std::basic_string<Uint16> cpp3ds::String::toUtf16 | ( | ) | const |
std::basic_string<Uint32> cpp3ds::String::toUtf32 | ( | ) | const |
std::basic_string<Uint8> cpp3ds::String::toUtf8 | ( | ) | const |
std::wstring cpp3ds::String::toWideString | ( | ) | const |
Convert the unicode string to a wide string.
Characters that do not fit in the target encoding are discarded from the returned string.
Overload of != operator to compare two UTF-32 strings.
left | Left operand (a string) |
right | Right operand (a string) |
Overload of binary + operator to concatenate two strings.
left | Left operand (a string) |
right | Right operand (a string) |
Overload of < operator to compare two UTF-32 strings.
left | Left operand (a string) |
right | Right operand (a string) |
Overload of <= operator to compare two UTF-32 strings.
left | Left operand (a string) |
right | Right operand (a string) |
Overload of == operator to compare two UTF-32 strings.
left | Left operand (a string) |
right | Right operand (a string) |
Overload of > operator to compare two UTF-32 strings.
left | Left operand (a string) |
right | Right operand (a string) |
Overload of >= operator to compare two UTF-32 strings.
left | Left operand (a string) |
right | Right operand (a string) |
|
static |
Represents an invalid position in the string.
Definition at line 55 of file String.hpp.