Wt::WString Class Reference

A unicode string class, with support for localization. More...

#include <Wt/WString>

List of all members.

Public Member Functions

 WString ()
 Creates an empty string.
 WString (const wchar_t *value)
 Creates a WString from a wide C string.
 WString (const WString &other)
 Copy constructor.
 WString (const std::wstring &value)
 Creates a WString from a wide C++ string.
 WString (const char *value, CharEncoding encoding=LocalEncoding)
 Creates a WString from a C string.
 WString (const std::string &value, CharEncoding encoding=LocalEncoding)
 Creates a WString from a C++ string.
 ~WString ()
 Destructor.
WStringoperator= (const WString &rhs)
 Assignment operator.
bool operator== (const WString &rhs) const
 Comparison operator.
bool operator< (const WString &rhs) const
 Comparison operator.
bool operator> (const WString &rhs) const
 Comparison operator.
WStringoperator+= (const WString &rhs)
 Self-concatenation operator.
WStringoperator+= (const std::wstring &rhs)
 Self-concatenation operator.
WStringoperator+= (const wchar_t *rhs)
 Self-concatenation operator.
WStringoperator+= (const std::string &rhs)
 Self-concatenation operator.
WStringoperator+= (const char *rhs)
 Self-concatenation operator.
bool empty () const
 Returns whether the string is empty.
std::string toUTF8 () const
 Returns the value as a UTF8 unicode encoded string.
std::wstring value () const
 Returns the value as a wide C++ string.
std::string narrow () const
 Returns the value as a narrow C++ string.
 operator std::wstring () const
 Returns the value as a wide C++ string.
bool literal () const
 Retuns whether the string is literal or localized.
const std::string key () const
 Returns the key for a localized string.
WStringarg (const std::wstring &value)
 Substitutes the next positional argument with a string value.
WStringarg (const std::string &value, CharEncoding=LocalEncoding)
 Substitutes the next positional argument with a string value.
WStringarg (const WString &value)
 Substitutes the next positional argument with a string value.
WStringarg (int value)
 Substitutes the next positional argument with an integer value.
WStringarg (double value)
 Substitutes the next positional argument with a double value.
const std::vector< std::string > & args () const
 Returns the list of arguments.
bool refresh ()
 Refreshes the string.
std::string jsStringLiteral (char delimiter= '\'') const
 Returns the string as a JavaScript literal.

Static Public Member Functions

static WString fromUTF8 (const std::string &value)
 Creates a WString from a UTF8 unicode encoded string.
static WString fromUTF8 (const char *value)
 Creates a WString from a UTF8 unicode encoded string.
static WString tr (const char *key)
 Creates a localized string from a key.
static WString tr (const std::string &key)
 Creates a localized string with the specified key.

Static Public Attributes

static const WString Empty
 An emtpy string.

Related Functions

(Note that these are not member functions.)

enum  CharEncoding { LocalEncoding, UTF8 }
 Enumeration that indicates a character encoding. More...
typedef WString WMessage
 Alias for WString (deprecated).
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.
WT_API std::wstring widen (const std::string &s)
 Convert a narrow to a wide string.
WT_API std::string narrow (const std::wstring &s)
 Convert a wide to a narrow string.
WT_API std::string toUTF8 (const std::wstring &s)
 Encode a wide string to UTF8.


Detailed Description

A unicode string class, with support for localization.

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() and the WString(..., UTF8) constructor 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.

See also:
WApplication::messageResourceBundle()

WApplication::locale()


Constructor & Destructor Documentation

Wt::WString::WString (  ) 

Creates an empty string.

Create a literal string with empty contents ("").

Wt::WString::WString ( const wchar_t *  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 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 = LocalEncoding 
)

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.

Wt::WString::WString ( const std::string &  value,
CharEncoding  encoding = LocalEncoding 
)

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.


Member Function Documentation

WString & Wt::WString::operator= ( const WString rhs  ) 

Assignment operator.

Copy another string into this string.

bool Wt::WString::operator== ( const WString rhs  )  const

Comparison operator.

Compares to 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::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.

WString & Wt::WString::operator+= ( const 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 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.

WT_API std::wstring fromUTF8 ( const std::string &  value  )  [static]

Creates a WString from a UTF8 unicode encoded string.

Decode a UTF8 string a wide string.

This is equivalent to using the constructor WString(value, UTF8).

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.

See also:
toUTF8(const std::string& s), narrow(const std::wstring&)

WString Wt::WString::fromUTF8 ( const char *  value  )  [static]

Creates a WString from a UTF8 unicode encoded string.

This is equivalent to using the constructor WString(value, UTF8).

std::string Wt::WString::toUTF8 (  )  const

Returns the value as a UTF8 unicode encoded string.

For a localized string, returns the current localized value.

See also:
fromUTF8()

WString Wt::WString::tr ( const char *  key  )  [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??'.

See also:
WApplication::messageResourceBundle(), WApplication::locale(), WApplication::localizedStrings()

WString Wt::WString::tr ( const std::string &  key  )  [static]

Creates a localized string with the specified key.

See also:
tr(const char *)

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.

std::string Wt::WString::narrow (  )  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, possibly losing information. If you wish to keep all information, use toUTF8() instead, which encodes wide characters in the string.

See also:
toUTF8()

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::literal (  )  const [inline]

Retuns whether the string is literal or localized.

See also:
tr()

const std::string Wt::WString::key (  )  const

Returns the key for a localized string.

When the string is literal, the result is undefined.

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 "{1} bought {2} apples in the shop." with first argument value "Bart" and second argument value 5 becomes: "Bart bought 5 apples in the shop."

WString & Wt::WString::arg ( const std::string &  value,
CharEncoding  encoding = LocalEncoding 
)

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 "{1} bought {2} apples in the shop." with first argument value "Bart" and second argument value 5 becomes: "Bart bought 5 apples in the shop."

WString & Wt::WString::arg ( const 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 "{1} bought {2} apples in the shop." with first argument value "Bart" and second argument value 5 becomes: "Bart bought 5 apples in the shop."

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 "{1} bought {2} apples in the shop." with first argument value "Bart" and second argument value 5 becomes: "Bart bought 5 apples in the shop."

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 "{1} bought {2} apples in the shop." with first argument value "Bart" and second argument value 5 becomes: "Bart bought 5 apples in the shop."

const std::vector< std::string > & Wt::WString::args (  )  const

Returns the list of arguments.

Each argument is UTF8 encoded.

bool Wt::WString::refresh (  ) 

Refreshes the string.

For a localized string, its value is resolved again.

Returns whether the value has (potentially) changed.

std::string Wt::WString::jsStringLiteral ( char  delimiter = '\''  )  const

Returns the string as a JavaScript literal.

The delimiter may be a single or double quote.

See also:
WWebWidget::jsStringLiteral()


Friends And Related Function Documentation

enum CharEncoding [related]

Enumeration that indicates a character encoding.

Character encodings are used to represent characters in a stream of bytes.

Enumerator:
LocalEncoding  Each byte represents a character, using the local server-side system locale.
UTF8  The byte stream represents unicode characters encoded using UTF-8.

typedef WString WMessage [related]

Alias for WString (deprecated).

Deprecated:
use WString instead.
WString provides all the functionality of the now deprecated WMessage, but is used more consistently through the API.

WT_API std::wstring widen ( const std::string &  s  )  [related]

Convert a narrow to a wide string.

Convert a narrow string to a wide string. This method will not perform any code conversion, it simply represents the same string with a same number of wide characters.

See also:
narrow(const std::wstring&), fromUTF8(const std::string& s)

WT_API std::string narrow ( const std::wstring &  s  )  [related]

Convert a wide to a narrow string.

Convert a wide string to a narrow string. This method will not perform any code conversion, it simply represents (as far as possible) the same string with a same number of narrow characters.

In general this will lead to a loss of information. If you wish to preserve all information, you should us toUTF8() instead.

See also:
widen(const std::string&), toUTF8(const std::wstring& s)

WT_API std::string toUTF8 ( const std::wstring &  s  )  [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.

See also:
fromUTF8(const std::string& s), narrow(const std::wstring&)


Generated on Tue Nov 3 15:34:33 2009 for Wt by doxygen 1.5.6