22 Localization library [localization]

22.3 Locales [locales]

22.3.3 Convenience interfaces [locale.convenience]

22.3.3.2 Conversions [conversions]

22.3.3.2.1 Character conversions [conversions.character]

template <class charT> charT toupper(charT c, const locale& loc);

Returns: use_facet<ctype<charT> >(loc).toupper(c).

template <class charT> charT tolower(charT c, const locale& loc);

Returns: use_facet<ctype<charT> >(loc).tolower(c).

22.3.3.2.2 string conversions [conversions.string]

Class template wstring_convert performs conversions between a wide string and a byte string. It lets you specify a code conversion facet (like class template codecvt) to perform the conversions, without affecting any streams or locales. [ Example: Say, for example, you have a code conversion facet called codecvt_utf8 that you want to use to output to cout a UTF-8 multibyte sequence corresponding to a wide string, but you don't want to alter the locale for cout. You can write something like:

wstring_convert<codecvt_utf8<wchar_t>> myconv;
std::string mbstring = myconv.to_bytes(L"Hello\n");
std::cout << mbstring;

 — end example ]

Class template wstring_convert synopsis

namespace std {
template<class Codecvt, class Elem = wchar_t,
    class Wide_alloc = std::allocator<Elem>,
    class Byte_alloc = std::allocator<char> > class wstring_convert {
  public:
    typedef std::basic_string<char, char_traits<char>, Byte_alloc> byte_string;
    typedef std::basic_string<Elem, char_traits<Elem>, Wide_alloc> wide_string;
    typedef typename Codecvt::state_type state_type;
    typedef typename wide_string::traits_type::int_type int_type;

    wstring_convert(Codecvt *pcvt = new Codecvt);
    wstring_convert(Codecvt *pcvt, state_type state);
    wstring_convert(const byte_string& byte_err,
                    const wide_string& wide_err = wide_string());
    ~wstring_convert();

    wide_string from_bytes(char byte);
    wide_string from_bytes(const char *ptr);
    wide_string from_bytes(const byte_string& str);
    wide_string from_bytes(const char *first, const char *last);

    byte_string to_bytes(Elem wchar);
    byte_string to_bytes(const Elem *wptr);
    byte_string to_bytes(const wide_string& wstr);
    byte_string to_bytes(const Elem *first, const Elem *last);

    size_t converted() const;
    state_type state() const;
  private:
    byte_string byte_err_string;    // exposition only
    wide_string wide_err_string;    // exposition only
    Codecvt *cvtptr;                // exposition only
    state_type cvtstate;            // exposition only
    size_t cvtcount;                // exposition only
  };
}

The class template describes an object that controls conversions between wide string objects of class std::basic_string<Elem, char_traits<Elem>, Wide_alloc> and byte string objects of class std::
basic_string<char, char_traits<char>, Byte_alloc>
. The class template defines the types wide_string and byte_string as synonyms for these two types. Conversion between a sequence of Elem values (stored in a wide_string object) and multibyte sequences (stored in a byte_string object) is performed by an object of class Codecvt<Elem, char, std::mbstate_t>, which meets the requirements of the standard code-conversion facet std::codecvt<Elem, char, std::mbstate_t>.

An object of this class template stores:

  • byte_err_string — a byte string to display on errors

  • wide_err_string — a wide string to display on errors

  • cvtptr — a pointer to the allocated conversion object (which is freed when the wstring_convert object is destroyed)

  • cvtstate — a conversion state object

  • cvtcount — a conversion count

typedef std::basic_string<char> byte_string;

The type shall be a synonym for std::basic_string<char>

size_t converted() const;

Returns: cvtcount.

wide_string from_bytes(char byte); wide_string from_bytes(const char *ptr); wide_string from_bytes(const byte_string& str); wide_string from_bytes(const char *first, const char *last);

Effects: The first member function shall convert the single-element sequence byte to a wide string. The second member function shall convert the null-terminated sequence beginning at ptr to a wide string. The third member function shall convert the sequence stored in str to a wide string. The fourth member function shall convert the sequence defined by the range [first,last) to a wide string.

In all cases:

  • If the cvtstate object was not constructed with an explicit value, it shall be set to its default value (the initial conversion state) before the conversion begins. Otherwise it shall be left unchanged.

  • The number of input elements successfully converted shall be stored in cvtcount.

Returns: If no conversion error occurs, the member function shall return the converted wide string. Otherwise, if the object was constructed with a wide-error string, the member function shall return the wide-error string. Otherwise, the member function throws an object of class std::range_error.

typedef typename wide_string::traits_type::int_type int_type;

The type shall be a synonym for wide_string::traits_type::int_type.

state_type state() const;

returns cvtstate.

typedef typename Codecvt::state_type state_type;

The type shall be a synonym for Codecvt::state_type.

byte_string to_bytes(Elem wchar); byte_string to_bytes(const Elem *wptr); byte_string to_bytes(const wide_string& wstr); byte_string to_bytes(const Elem *first, const Elem *last);

Effects: The first member function shall convert the single-element sequence wchar to a byte string. The second member function shall convert the null-terminated sequence beginning at wptr to a byte string. The third member function shall convert the sequence stored in wstr to a byte string. The fourth member function shall convert the sequence defined by the range [first,last) to a byte string.

In all cases:

  • If the cvtstate object was not constructed with an explicit value, it shall be set to its default value (the initial conversion state) before the conversion begins. Otherwise it shall be left unchanged.

  • The number of input elements successfully converted shall be stored in cvtcount.

Returns: If no conversion error occurs, the member function shall return the converted byte string. Otherwise, if the object was constructed with a byte-error string, the member function shall return the byte-error string. Otherwise, the member function shall throw an object of class std::range_error.

typedef std::basic_string<Elem> wide_string;

The type shall be a synonym for std::basic_string<Elem>.

wstring_convert(Codecvt *pcvt = new Codecvt); wstring_convert(Codecvt *pcvt, state_type state); wstring_convert(const byte_string& byte_err, const wide_string& wide_err = wide_string());

Effects: The first constructor shall store pcvt in cvtptr and default values in cvtstate, byte_err_string, and wide_err_string. The second constructor shall store pcvt in cvtptr, state in cvtstate, and default values in byte_err_string and wide_err_string; moreover the stored state shall be retained between calls to from_bytes and to_bytes. The third constructor shall store new Codecvt in cvtptr, state_type() in cvtstate, byte_err in byte_err_string, and wide_err in wide_err_string.

~wstring_convert();

Effects: The destructor shall delete cvtptr.

22.3.3.2.3 Buffer conversions [conversions.buffer]

Class template wbuffer_convert looks like a wide stream buffer, but performs all its I/O through an underlying byte stream buffer that you specify when you construct it. Like class template wstring_convert, it lets you specify a code conversion facet to perform the conversions, without affecting any streams or locales.

Class template wbuffer_convert synopsis

namespace std {
template<class Codecvt,
    class Elem = wchar_t,
    class Tr = std::char_traits<Elem> >
  class wbuffer_convert
    : public std::basic_streambuf<Elem, Tr> {
  public:
    typedef typename Codecvt::state_type state_type;

    wbuffer_convert(std::streambuf *bytebuf = 0,
                    Codecvt *pcvt = new Codecvt,
                    state_type state = state_type());

    std::streambuf *rdbuf() const;
    std::streambuf *rdbuf(std::streambuf *bytebuf);

    state_type state() const;

  private:
    std::streambuf *bufptr;         // exposition only
    Codecvt *cvtptr;                // exposition only
    state_type cvtstate;            // exposition only
    };
}

The class template describes a stream buffer that controls the transmission of elements of type Elem, whose character traits are described by the class Tr, to and from a byte stream buffer of type std::streambuf. Conversion between a sequence of Elem values and multibyte sequences is performed by an object of class Codecvt<Elem, char, std::mbstate_t>, which shall meet the requirements of the standard code-conversion facet std::codecvt<Elem, char, std::mbstate_t>.

An object of this class template stores:

  • bufptr — a pointer to its underlying byte stream buffer

  • cvtptr — a pointer to the allocated conversion object (which is freed when the wbuffer_convert object is destroyed)

  • cvtstate — a conversion state object

state_type state() const;

Returns: cvtstate.

std::streambuf *rdbuf() const;

Returns: bufptr.

std::streambuf *rdbuf(std::streambuf *bytebuf);

Effects: stores bytebuf in bufptr.

Returns: The previous value of bufptr.

typedef typename Codecvt::state_type state_type;

The type shall be a synonym for Codecvt::state_type.

wbuffer_convert(std::streambuf *bytebuf = 0, Codecvt *pcvt = new Codecvt, state_type state = state_type());

Effects: The constructor constructs a stream buffer object, initializes bufptr to bytebuf, initializes cvtptr to pcvt, and initializes cvtstate to state.

~wbuffer_convert();

Effects: The destructor shall delete cvtptr.