Namespaces
Variants
Views
Actions

std::codecvt::out, std::codecvt::do_out

From cppreference.com
< cpp‎ | locale‎ | codecvt
Revision as of 15:09, 18 March 2013 by Cubbi (Talk | contribs)

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <locale>
</td>

<td></td> <td></td> <tr class="t-dcl ">

<td >
public:

result out( stateT& state,
            const internT* from,
            const internT* from_end,
            const internT*& from_next,
            externT* to,
            externT* to_end,

            externT*& to_next ) const;
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl ">

<td >
protected:

result do_out( stateT& state,
               const internT* from,
               const internT* from_end,
               const internT*& from_next,
               externT* to,
               externT* to_end,

               externT*& to_next ) const;
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

1) public member function, calls the member function do_out of the most derived class.

2) If this codecvt facet defines a conversion, translates the internal characters from the source range [from, from_end) to external characters, placing the results in the subsequent locations starting at to. Converts no more than from_end - from internal characters and writes no more than to_end - to external characters. Leaves from_next and to_next pointing one beyond the last element successfully converted.

If this codecvt facet does not define a conversion, no characters are converted. to_next is set to be equal to to, state is unchanged, and std::codecvt_base::noconv is returned.

Contents

Return value

A value of type std::codecvt_base::result, indicating the success status as follows:

ok conversion completed
partial not enough space in the output buffer or unexpected end of source buffer
error encountered a character that could not be converted
noconv this facet is non-converting, no output written

The non-converting specialization std::codecvt<char, char, std::mbstate_t> always returns std::codecvt_base::noconv

Notes

Requires that from <= from_end && to <= to_end and that state either representing the initial shift state or obtained by converting the preceding characters in the sequence.

While codecvt supports N:M conversions (e.g. UTF-16 to UTF-8, where two internal characters may be necessary to decide what external characters to output), std::basic_filebuf can only use codecvt facets that define a 1:N conversion, that is it must be able to process one internal character at a time when writing to a file.

When performing N:M conversions, this function may return std::codecvt_base::partial after consuming all source characters (from_next == from_end). This means that another internal character is needed to complete the conversion (e.g. when converting UTF-16 to UTF-8, if the last character in the source buffer is a high surrogate).

The effect on state is deliberately unspecified. In standard facets, it is used to maintain shift state like when calling std::wcsrtombs, and is therefore updated to reflect the shift state after the last successfully converted character, but a user-defined facet is free to use it to maintain any other state, e.g. count the number of special characters encountered.

Example

#include <iostream>
#include <string>
#include <locale>
int main()
{
    std::locale::global(std::locale("en_US.utf8"));
    auto& f = std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t>>(std::locale());
    std::wstring internal = L"z\u00df\u6c34\U0001d10b"; // L"zß水𝄋"
 
    // note that the following can be done with wstring_convert
    std::mbstate_t mb = std::mbstate_t(); // initial shift state
    std::string external(internal.size() * f.max_length(), '\0'); 
    const wchar_t* from_next;
    char* to_next;
    f.out(mb, &internal[0], &internal[internal.size()], from_next,
              &external[0], &external[external.size()], to_next);
    // error checking skipped for brevity
    external.resize(to_next - &external[0]);
 
    std::cout << "The string in narrow multibyte encoding: " << external << '\n';
}

Output:

The string in narrow multibyte encoding: zß水𝄋

See also

Template:cpp/io/basic filebuf/dcl list overflowTemplate:cpp/locale/wstring convert/dcl list to bytesTemplate:cpp/string/multibyte/dcl list wcsrtombsTemplate:cpp/locale/codecvt/dcl list do in