Wt
3.3.8
|
A value class which describes a locale-aware unicode string. More...
#include <Wt/WString>
Public Member Functions | |
WString () | |
Creates an empty string. More... | |
WString (const wchar_t *value) | |
Creates a WString from a wide C strinnarrow(); The wide string is implicitly converted to proper unicode. Note that there are known issues with the portability of wchar_t since its width and encoding are platform dependent. | |
WString (const WString &other) | |
Copy constructor. | |
WString (const std::wstring &value) | |
Creates a WString from a wide C++ string. More... | |
WString (const char *value, CharEncoding encoding=DefaultEncoding) | |
Creates a WString from a C string. More... | |
WString (const char *value, const std::locale &loc) | |
Creates a WString from a C string. More... | |
WString (const std::string &value, CharEncoding encoding=DefaultEncoding) | |
Creates a WString from a C++ string. More... | |
WString (const std::string &value, const std::locale &loc) | |
Creates a WString from a C++ string. More... | |
~WString () | |
Destructor. | |
WString & | operator= (const WString &rhs) |
Assignment operator. More... | |
bool | operator== (const WString &rhs) const |
Comparison operator. More... | |
bool | operator< (const WString &rhs) const |
Comparison operator. More... | |
bool | operator> (const WString &rhs) const |
Comparison operator. More... | |
WString & | operator+= (const WString &rhs) |
Self-concatenation operator. More... | |
WString & | operator+= (const std::wstring &rhs) |
Self-concatenation operator. More... | |
WString & | operator+= (const wchar_t *rhs) |
Self-concatenation operator. More... | |
WString & | operator+= (const std::string &rhs) |
Self-concatenation operator. More... | |
WString & | operator+= (const char *rhs) |
Self-concatenation operator. More... | |
bool | empty () const |
Returns whether the string is empty. | |
std::string | toUTF8 () const |
Returns the value as a UTF8 unicode encoded string. More... | |
std::wstring | value () const |
Returns the value as a wide C++ string. More... | |
std::string | narrow (const std::locale &loc=std::locale()) const |
Returns the value as a narrow C++ string. More... | |
operator std::wstring () const | |
Returns the value as a wide C++ string. More... | |
bool | literal () const |
Returns whether the string is literal or localized. More... | |
const std::string | key () const |
Returns the key for a localized string. More... | |
WString & | arg (const std::wstring &value) |
Substitutes the next positional argument with a string value. More... | |
WString & | arg (const std::string &value, CharEncoding=DefaultEncoding) |
Substitutes the next positional argument with a string value. More... | |
WString & | arg (const WString &value) |
Substitutes the next positional argument with a string value. More... | |
WString & | arg (int value) |
Substitutes the next positional argument with an integer value. More... | |
WString & | arg (unsigned value) |
Substitutes the next positional argument with an unsigned value. More... | |
WString & | arg (::int64_t value) |
Substitutes the next positional argument with an integer value. More... | |
WString & | arg (::uint64_t value) |
Substitutes the next positional argument with an unsigned value. More... | |
WString & | arg (double value) |
Substitutes the next positional argument with a double value. More... | |
const std::vector< WString > & | args () const |
Returns the list of arguments. | |
bool | refresh () |
Refreshes the string. More... | |
std::string | jsStringLiteral (char delimiter= '\'') const |
Returns the string as a JavaScript literal. More... | |
bool | operator!= (const WString &rhs) const |
Comparison operator. More... | |
Static Public Member Functions | |
static void | setDefaultEncoding (Wt::CharEncoding encoding) |
Sets the encoding for DefaultEncoding. More... | |
static WString | fromUTF8 (const std::string &value, bool checkValid=false) |
Creates a WString from a UTF8 unicode encoded string. More... | |
static WString | fromUTF8 (const char *value, bool checkValid=false) |
Creates a WString from a UTF8 unicode encoded string. More... | |
static WString | tr (const char *key) |
Creates a localized string from a key. More... | |
static WString | tr (const std::string &key) |
Creates a localized string with the specified key. More... | |
static WString | trn (const char *key,::uint64_t n) |
Creates a localized string from a key for a number n . More... | |
static WString | trn (const std::string &key,::uint64_t n) |
Creates a localized string with the specified key for a number n . More... | |
Static Public Attributes | |
static const WString | Empty |
An empty string. | |
Related Functions | |
(Note that these are not member functions.) | |
typedef WString | WMessage |
Alias for WString (deprecated). More... | |
WT_API WString | utf8 (const char *value) |
Short hand for WString(const char * value, UTF8) | |
WT_API WString | utf8 (const std::string &value) |
Short hand for WString(const std::string& value, UTF8) | |
WT_API WString | operator+ (const WString &lhs, const WString &rhs) |
Concatenate two WStrings. | |
WT_API WString | operator+ (const WString &lhs, const std::wstring &rhs) |
Conatenate a WString with a C++ wide string. | |
WT_API WString | operator+ (const WString &lhs, const wchar_t *rhs) |
Conatenate a WString with a C wide string. | |
WT_API WString | operator+ (const WString &lhs, const std::string &rhs) |
Conatenate a WStrin with a C++ string. | |
WT_API WString | operator+ (const WString &lhs, const char *rhs) |
Conatenate a WString with a C string. | |
WT_API WString | operator+ (const std::wstring &lhs, const WString &rhs) |
Conatenate a C++ wide string with a WString. | |
WT_API WString | operator+ (const wchar_t *lhs, const WString &rhs) |
Conatenate a C wide string with a WString. | |
WT_API WString | operator+ (const std::string &lhs, const WString &rhs) |
Conatenate a C++ string with a WString. | |
WT_API WString | operator+ (const char *lhs, const WString &rhs) |
Conatenate a C string with a WString. | |
WT_API bool | operator== (const char *lhs, const WString &rhs) |
Compare a C string with a WString. | |
WT_API bool | operator== (const wchar_t *lhs, const WString &rhs) |
Compare a C wide string with a WString. | |
WT_API bool | operator== (const std::string &lhs, const WString &rhs) |
Compare a C++ string with a WString. | |
WT_API bool | operator== (const std::wstring &lhs, const WString &rhs) |
Compare a C++ wide string with a WString. | |
WT_API bool | operator!= (const char *lhs, const WString &rhs) |
Compare a C string with a WString. | |
WT_API bool | operator!= (const wchar_t *lhs, const WString &rhs) |
Compare a C wide string with a WString. | |
WT_API bool | operator!= (const std::string &lhs, const WString &rhs) |
Compare a C++ string with a WString. | |
WT_API bool | operator!= (const std::wstring &lhs, const WString &rhs) |
Compare a C++ wide string with a WString. | |
WT_API std::wostream & | operator<< (std::wostream &lhs, const WString &rhs) |
Output a WString to a C++ wide stream. | |
WT_API std::ostream & | operator<< (std::ostream &lhs, const WString &rhs) |
Output a WString to a C++ stream. More... | |
WT_API std::wstring | widen (const std::string &s, const std::locale &loc=std::locale()) |
Convert a narrow to a wide string. More... | |
WT_API std::string | narrow (const std::wstring &s, const std::locale &loc=std::locale()) |
Convert a wide to a narrow string. More... | |
WT_API std::wstring | fromUTF8 (const std::string &s) |
Decode a UTF8 string a wide string. More... | |
WT_API std::string | fromUTF8 (const std::string &s, const std::locale &loc) |
Decode a UTF8 string into a (narrow) string. More... | |
WT_API std::string | toUTF8 (const std::wstring &s) |
Encode a wide string to UTF8. More... | |
WT_API std::string | toUTF8 (const std::string &s, const std::locale &loc=std::locale()) |
Encode an character string (encoding known) to UTF8. More... | |
A value class which describes a locale-aware unicode string.
Wt offers this string to facilitate handling of unicode text through the user interface, and to offer support for localized text using message resource bundles.
A WString may be constructed from a std::string, std::wstring or c-style strings (const char * and const wchar_t *), and converted to each of these strings taking into account the locale in which the Wt application runs on the web server. Independent of the locale on the web server, you may convert from and to UTF8 unicode encoded std::strings.
By using the static functions WString::tr() (or WWidget::tr()), one may construct a localized string. The key is used to retrieve its current value from the application's message-resource bundles.
Argument place holder in a string, denoted using {n} for the n'th argument, may be substituted by values set using arg(int) and arg(std::string).
WString is used by all built-in widgets for displayed text. By calling WApplication::setLocale() or WApplication::refresh(), the contents of every WString is reevaluated in the new locale, by calling refresh(). In this way, the contents of the whole user interface is adapted to a possibly changed locale.
To benefit from the localization properties of WString, you should design your own widget classes to use WString in their interface for any text that is displayed. In this way, your own widgets will automatically, and without any extra effort, participate in a relocalization triggered by WApplication::setLocale().
This string class does not provide anything more than basic manipulations. Instead, you should convert to a standard library string class to manipulate the string contents and perform string algorithms on them. In particular, we recommend to use the conversion methods toUTF8(), fromUTF8() to convert from and to UTF8 encoded std::strings. In this way, you can support the whole unicode character set, with backward compatible support for the standard 7-bit ASCII set. Since WString internally uses UTF8-encoding, and UTF8-encoding is used by the library for communication with the browser, there is no actual conversion overhead. Only when you need to run string algorithms that require the actual length of the string in number of characters, you would need to convert to a wide string representation such as std::wstring.
Wt::WString::WString | ( | ) |
Creates an empty string.
Create a literal string with empty contents ("").
Wt::WString::WString | ( | const std::wstring & | value | ) |
Creates a WString from a wide C++ string.
The wide string is implicitly converted to proper unicode. Note that there are known issues with the portability of wchar_t since its width and encoding are platform dependent.
Wt::WString::WString | ( | const char * | value, |
CharEncoding | encoding = DefaultEncoding |
||
) |
Creates a WString from a C string.
The C string is implicitly converted to unicode. When encoding
is LocalEncoding, the current locale is used to interpret the C string. When encoding is UTF8, the C string is interpreted as a UTF8 encoded unicode string.
WString::setDefaultEncoding() controls the behaviour of DefaultEncoding. Use it to set a system-wide default format for C style strings (e.g. to UTF-8).
Wt::WString::WString | ( | const char * | value, |
const std::locale & | loc | ||
) |
Creates a WString from a C string.
The C string is implicitly converted to unicode. The string is interpreted within the character set of the given locale.
Wt::WString::WString | ( | const std::string & | value, |
CharEncoding | encoding = DefaultEncoding |
||
) |
Creates a WString from a C++ string.
The C++ string is implicitly converted to unicode. When encoding
is LocalEncoding, the current locale is used to interpret the C++ string. When encoding is UTF8, the C++ string is interpreted as a UTF8 encoded unicode string.
WString::setDefaultEncoding() controls the behaviour of DefaultEncoding. Use it to set a system-wide default format for C style strings (e.g. to UTF-8).
Wt::WString::WString | ( | const std::string & | value, |
const std::locale & | loc | ||
) |
Creates a WString from a C++ string.
The C++ string is implicitly converted to unicode. The string is interpreted within the character set of the given locale.
WString & Wt::WString::arg | ( | const std::wstring & | value | ) |
Substitutes the next positional argument with a string value.
In the string, the n-th
argument is referred to as using {n}
.
For example: the string "<tt>{1} bought {2} apples in the
shop.</tt>" with first argument value "<tt>Bart</tt>" and second argument value 5
becomes: "<tt>Bart bought 5 apples in
the shop.</tt>"
WString & Wt::WString::arg | ( | const std::string & | value, |
CharEncoding | encoding = DefaultEncoding |
||
) |
Substitutes the next positional argument with a string value.
In the string, the n-th
argument is referred to as using {n}
.
For example: the string "<tt>{1} bought {2} apples in the
shop.</tt>" with first argument value "<tt>Bart</tt>" and second argument value 5
becomes: "<tt>Bart bought 5 apples in
the shop.</tt>"
Substitutes the next positional argument with a string value.
In the string, the n-th
argument is referred to as using {n}
.
For example: the string "<tt>{1} bought {2} apples in the
shop.</tt>" with first argument value "<tt>Bart</tt>" and second argument value 5
becomes: "<tt>Bart bought 5 apples in
the shop.</tt>"
WString & Wt::WString::arg | ( | int | value | ) |
Substitutes the next positional argument with an integer value.
In the string, the n-th
argument is reffered to as using {n}
.
For example: the string "<tt>{1} bought {2} apples in the
shop.</tt>" with first argument value "<tt>Bart</tt>" and second argument value 5
becomes: "<tt>Bart bought 5 apples in
the shop.</tt>"
WString & Wt::WString::arg | ( | unsigned | value | ) |
Substitutes the next positional argument with an unsigned value.
WString & Wt::WString::arg | ( | ::int64_t | value | ) |
Substitutes the next positional argument with an integer value.
In the string, the n-th
argument is reffered to as using {n}
.
For example: the string "<tt>{1} bought {2} apples in the
shop.</tt>" with first argument value "<tt>Bart</tt>" and second argument value 5
becomes: "<tt>Bart bought 5 apples in
the shop.</tt>"
WString & Wt::WString::arg | ( | ::uint64_t | value | ) |
Substitutes the next positional argument with an unsigned value.
WString & Wt::WString::arg | ( | double | value | ) |
Substitutes the next positional argument with a double value.
In the string, the n-th
argument is reffered to as using {n}
.
For example: the string "<tt>{1} bought {2} apples in the
shop.</tt>" with first argument value "<tt>Bart</tt>" and second argument value 5
becomes: "<tt>Bart bought 5 apples in
the shop.</tt>"
|
static |
Creates a WString from a UTF8 unicode encoded string.
This is equivalent to using the constructor WString(value
, UTF8).
When checkValid
is true
, the UTF8 encoding is validated. You should enable this only if you cannot trust the origin of the string. The library uses this internally whenever it receives data from the browser (in UTF-8 format).
|
static |
Creates a WString from a UTF8 unicode encoded string.
This is equivalent to using the constructor WString(value
, UTF8).
When checkValid
is true
, the UTF8 encoding is validated. You should enable this only if you cannot trust the origin of the string. The library uses this internally whenever it receives data from the browser (in UTF-8 format).
std::string Wt::WString::jsStringLiteral | ( | char | delimiter = '\'' | ) | const |
Returns the string as a JavaScript literal.
The delimiter
may be a single or double quote.
const std::string Wt::WString::key | ( | ) | const |
Returns the key for a localized string.
When the string is literal, the result is undefined.
bool Wt::WString::literal | ( | ) | const |
Returns whether the string is literal or localized.
std::string Wt::WString::narrow | ( | const std::locale & | loc = std::locale() | ) | const |
Returns the value as a narrow C++ string.
A localized string is resolved using the WApplication::localizedStrings().
Argument place holders are substitued with actual arguments.
Any wide character is narrowed using the provided locale, possibly losing information. If you wish to keep all information, use toUTF8() instead, which encodes wide characters in the string.
Wt::WString::operator std::wstring | ( | ) | const |
Returns the value as a wide C++ string.
A localized string is resolved using the WApplication::localizedStrings().
Argument place holders are substitued with actual arguments.
bool Wt::WString::operator!= | ( | const WString & | rhs | ) | const |
Comparison operator.
Compares two strings and returns true
if the strings are not exactly the same. This may require evaluating a localized string in the current locale.
Self-concatenation operator.
Appends a string to the current value. If the string was localized, this automatically converts it to a literal string, by evaluating the string using the current locale.
WString & Wt::WString::operator+= | ( | const std::wstring & | rhs | ) |
Self-concatenation operator.
Appends a string to the current value. If the string was localized, this automatically converts it to a literal string, by evaluating the string using the current locale.
WString & Wt::WString::operator+= | ( | const wchar_t * | rhs | ) |
Self-concatenation operator.
Appends a string to the current value. If the string was localized, this automatically converts it to a literal string, by evaluating the string using the current locale.
WString & Wt::WString::operator+= | ( | const std::string & | rhs | ) |
Self-concatenation operator.
Appends a string to the current value. The right hand side is interpreted in the server locale and converted to unicode. If the string was localized, this automatically converts it to a literal string, by evaluating the string using the current locale.
WString & Wt::WString::operator+= | ( | const char * | rhs | ) |
Self-concatenation operator.
Appends a string to the current value. The right hand side is interpreted in the server locale and converted to unicode. If the string was localized, this automatically converts it to a literal string, by evaluating the string using the current locale.
bool Wt::WString::operator< | ( | const WString & | rhs | ) | const |
Comparison operator.
Compares to strings lexicographically. This may require evaluating a localized string in the current locale. The unicode representation of the strings are compared.
Assignment operator.
Copy another string into this string.
bool Wt::WString::operator== | ( | const WString & | rhs | ) | const |
Comparison operator.
Compares two strings and returns true
if the strings are exactly the same. This may require evaluating a localized string in the current locale.
bool Wt::WString::operator> | ( | const WString & | rhs | ) | const |
Comparison operator.
Compares to strings lexicographically. This may require evaluating a localized string in the current locale. The unicode representation of the strings are compared.
bool Wt::WString::refresh | ( | ) |
Refreshes the string.
For a localized string, its value is resolved again.
Returns whether the value has (potentially) changed.
|
static |
Sets the encoding for DefaultEncoding.
WString::setDefaultEncoding() controls the behaviour of DefaultEncoding. If not modified, the default encoding will be the encoding dictated by the C++ global locale. Often UTF-8 is desired, in which case you can use this method to change the system-wide how Wt should interpret C style strings.
Since this is a system-wide setting, and not a per-session setting, you should call this function before any session is created, e.g. in main() before calling WRun().
Note: in the future, the default encoding for DefaultEncoded strings may change to UTF-8 instead of the C++ global locale.
std::string Wt::WString::toUTF8 | ( | ) | const |
Returns the value as a UTF8 unicode encoded string.
For a localized string, returns the current localized value.
|
static |
Creates a localized string from a key.
Whenever the value of the string is needed, the key is used for a lookup in the application message resource bundles taking into account the current application locale. If the key cannot be resolved, its value is set to '??key??'.
|
static |
Creates a localized string with the specified key.
|
static |
Creates a localized string from a key for a number n
.
Whenever the value of the string is needed, the key
is used for a lookup in the application message resource bundles taking into account the current application locale. This function fetches the appropriate plural case for the translation corresponding to the quantity n
. Note that usually, your string will have a place-holder for the value of n
, and thus you will also need to bind as
an argument.
For example, consider a string "quantity.cars" with two plural cases:
You would use the following to use the string:
If the key
cannot be resolved, its value is set to '??key??'.
|
static |
Creates a localized string with the specified key for a number n
.
std::wstring Wt::WString::value | ( | ) | const |
Returns the value as a wide C++ string.
A localized string is resolved using the WApplication::localizedStrings().
Argument place holders are substitued with actual arguments.
|
related |
Enumeration that indicates a character encoding.
Character encodings are used to represent characters in a stream of bytes.
|
related |
Decode a UTF8 string a wide string.
Decode a UTF8 string to a wide string. In a UTF8 encoded unicode string, some unicode characters are represented in more than one byte. This method will decode to extract the proper unicode characters from the string. The resulting string may thus be shorter (has less characters) than the original, but does not lead to a loss of information.
|
related |
Decode a UTF8 string into a (narrow) string.
Decode a UTF8 string to a normal string. Not all Unicode characters can be represented in a narrow string, and quite a lot characters will have no equivalent in the target character set, so you may loose information.
To distinguish from the other fromUTF8() function, that returns a wstring, the locale is not an optional argument, as in most other locale-conversing functions. You may choose to use the default-constructed std::locale().
|
related |
Convert a wide to a narrow string.
Convert a wide string to a narrow string. This method will encode the characters in the given locale, if possible.
In general this will lead to a loss of information. If you wish to preserve all information, you should us toUTF8() instead.
|
related |
Output a WString to a C++ stream.
The string is narrowed using the currently global C++ locale, possibly losing information.
|
related |
Encode a wide string to UTF8.
Convert a wide string to UTF8. This method will encode the given wide string in UTF8. This may result in a string that is possibly longer (has more characters), but does not lead to a loss of information.
|
related |
Encode an character string (encoding known) to UTF8.
Convert a char * string to UTF8. This method will encode the given string in UTF8, assuming that the original string was encoded in the given locale. This conversion does not lead to a loss of information.
The reverse operation is in principle narrow(fromUTF8(str), locale).
Do not call this function multiple times: toUTF8(toUTF8(str)) is meaningless.
|
related |
Convert a narrow to a wide string.
Convert a narrow string to a wide string. This method will interpret the input string as being encoded in the given locale (by default the currently configured global C++ locale).
Alias for WString (deprecated).
WString provides all the functionality of the now deprecated WMessage, but is used more consistently through the API.