<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Recent changes to strcvt</title><link>https://sourceforge.net/p/xprintf/wiki/strcvt/</link><description>Recent changes to strcvt</description><atom:link href="https://sourceforge.net/p/xprintf/wiki/strcvt/feed" rel="self"/><language>en</language><lastBuildDate>Wed, 02 Jul 2014 09:50:54 -0000</lastBuildDate><atom:link href="https://sourceforge.net/p/xprintf/wiki/strcvt/feed" rel="self" type="application/rss+xml"/><item><title>strcvt modified by Ruediger Helsch</title><link>https://sourceforge.net/p/xprintf/wiki/strcvt/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v7
+++ v8
@@ -1,734 +0,0 @@
-Strcvt - Character type converter for C++11
-============================================
-
-
-
-Contents
---------
-- [Introduction](#Introduction)
-- [Installation](#Installation)
-- [Usage](#Usage)
-- [String conversion](#string_conversion)
-- [UTF-8 string type `u8string`](#U8String)
-- [UTF string conversion](#utf_string_conversion)
-- [Locale dependent stream conversion](#locale_dependent)
-- [UTF stream conversion](#utf_stream_conversion)
-- [Character type converting iterator](#converting_iterator)
-- [Operators](#operators)
-- [Creating a Library](#Library)
-- [Compatibility](#Compatibility)
-- [Rationale](#Rationale)
-- [License](#License)
-
-
-
-
-Introduction &lt;a name="Introduction"&gt;&lt;/a&gt;
-------------
-
-C++11 has the new character types `char16_t` and `char32_t`, which use
-the UTF-16 and UTF-32 Unicode encoding. The question is how to convert
-between the four character types of C++.
-
-This library converts between the different character types of C++.
-
-- Conversion between the types `char`, `wchar_t`, `char16_t` and
-  `char32_t` in all combinations.
-
-- Implements the API of the C++ `codecvt` member functions in() and
-  out() for stream conversion, and convenience functions for string
-  conversion.
-
-- Locale-dependent conversion and UTF conversion.
-
-
-
-Installation &lt;a name="Installation"&gt;&lt;/a&gt;
-------------
-
-Unpack the distribution to a directory on your local machine.  You can
-include the headers in subdirectory `include/xprintf` from your
-program.  To make inclusion of the headers easier, it is recommended to
-add the subdirectory `include` of the distribution to the include file
-search path of the compiler.  This is commonly achieved with the
-option `-I/path/to/strcvt/include` (assuming that the xprintf
-distribution has been upacked to directory `/path/to/xprintf`).  Then
-you can include the code converter headers through their standard names
-like "strcvt/strcvt.h".
-
-
-
-Usage &lt;a name="Usage"&gt;&lt;/a&gt;
------
-
-In order to use the code converter, its headers have to be included.
-They are:
-- [UTF-8 string type `u8string`](#U8String)
-~~~cpp
-
-    // The main header defining the strcvt() string conversion functions
-    #include "/path/to/strcvt/include/strcvt/strcvt.h"
-    // The main header defining the u8string type
-    #include "/path/to/strcvt/include/strcvt/u8string.h"
-    // The header defining the strcvt_utf() UTF string conversion functions
-    #include "/path/to/strcvt/include/strcvt/strcvt_utf.h"
-    // The header defining the charcvt() stream conversion functions
-    #include "/path/to/strcvt/include/strcvt/charcvt.h"
-    // The header defining the charcvt_utf() UTF stream conversion functions
-    #include "/path/to/strcvt/include/strcvt/charcvt_utf.h"
-    // The header defining the strcvt_iterator
-    #include "/path/to/strcvt/include/strcvt/strcvt_iterator.h"
-    // The header defining codecvt_utf
-    #include "/path/to/strcvt/include/strcvt/codecvt_utf.h"
-
-    // Optional header defining output operators
-    #include "/path/to/strcvt/include/strcvt/strcvt_operators.h"
-
-    // Import symbols into users namespace
-    using namespace StrCvt;
-~~~
-
-If the xprintf include directory `/path/to/strcvt/include` has been
-added to the include file search path of the compiler, e.g. using the
-compiler option `-I/path/to/strcvt/include`, this reduces to:
-
-~~~{.cpp}
-
-    // The main header defining the strcvt() string conversion functions
-    #include "strcvt/strcvt.h"
-    // The main header defining the u8string type
-    #include "strcvt/u8string.h"
-    // The header defining the strcvt_utf() UTF string conversion functions
-    #include "strcvt/strcvt_utf.h"
-    // The header defining the charcvt() stream conversion functions
-    #include "strcvt/charcvt.h"
-    // The header defining the charcvt_utf() UTF stream conversion functions
-    #include "strcvt/charcvt_utf.h"
-    // The header defining the strcvt_iterator
-    #include "strcvt/strcvt_iterator.h"
-    // The header defining codecvt_utf
-    #include "strcvt/codecvt_utf.h"
-
-    // Optional header defining output operators
-    #include "strcvt/strcvt_operators.h"
-
-    // Import symbols into users namespace
-    using namespace StrCvt;
-~~~
-
-The strcvt() functions are exported through namespace `StrCvt`.  It
-is recommented to make them available via a `using namespace`
-directive like in the example above.  Alternatively it is possible to
-import the functions strcvt() and strcvt_utf() etc. separately
-through `using` declarations:
-
-~~~{.cpp}
-
-    using StrCvt::strcvt;
-    using StrCvt::strcvt_utf;
-    using StrCvt::strcvt_utf_strict;
-    using StrCvt::make_strcvt_iterator;
-~~~
-
-The character type converter is pre-configured for header-only use.
-This means: Just include the header and you are done.  In order to
-reduce space overhead and compilation time, a precompiled library can
-be used.  See section [Creating a Library](#library).
-
-
-
-String conversion &lt;a name="string_conversion"&gt;&lt;/a&gt;
------------------
-
-Function strcvt\(source) converts strings from the
-source character type to the destination string type.  The
-`dest_string_type` can be `std::string`, `std::wstring`,
-`std::u16string` and `std::u32string`. An additional string type
-`StrCvt::u8string` with UTF-8 encoding is also provided; see section
-[UTF-8 string type `u8string`](#U8String). Example usage:
-
-~~~{.cpp}
-
-    #include "strcvt/strcvt.h"
-
-    // ...
-
-    using namespace StrCvt;
-
-    // Convert const char* C string to wide wchar_t string
-    std::wstring w = strcvt("Hello, world!");
-    // Convert wide string to string of different character type
-    std::string s = strcvt(w);
-    // Convert string to char16_t string
-    std::u16string s16 = strcvt(s);
-    // The source buffer can also be specified as (pointer, size):
-    std::cout &lt;&lt; strcvt(&amp;amp;s16[0], 5) &lt;&lt; std::endl;
-~~~
-
-The source string can be specified as:
-- a character pointer of any character type (C-style null-terminated string),
-- a C++ string of arbitrary character type, or
-- a character pointer and a size.
-
-The destination string type is specified as a template argument to
-strcvt().  The function is specialized on all combinations of source
-and destination character types. So the full signatures are:
-
-~~~{.cpp}
-
-    template
-    std::basic_string
-    strcvt&gt;(const std::basic_string&amp; source);
-
-    template
-    std::basic_string
-    strcvt&gt;(const source_charT* source);
-
-    template
-    std::basic_string
-    strcvt&gt;(const source_charT* source, std::size_t size);
-~~~
-
-The function with string argument `dest_string
-strcvt(source_string source)` is overloaded for the case
-`dest_string` == `source_string`: it moves its argument to the result
-(for rvalue arguments) or returns a reference to its argument (for
-lvalue arguments).  This is useful in generic code where the concrete
-types are not known.
-
-It is also possible to omit the template argument determining the
-destination string type.  In this case, strcvt() does no conversion at
-all, but returns an object which can be converted to all string types.
-
-~~~{.cpp}
-
-    // strcvt() returns an object which can be assigned to any string type
-    std::wstring w = strcvt("Hello, world!");
-    std::u16string = strcvt(w);
-~~~
-
-For string arguments, the return value of strcvt() is a subclass of
-the class of its argument.  The subclass adds conversion operators to
-all string types.  For character pointer arguments, the return value
-of strcvt() is an object which can be converted to a character
-pointer (of the character type of the argument) or to any string type.
-In each case, the return value of strcvt() can e.g. be assigned to
-any string type, or can be passed as argument to a function expecting a
-string.
-
-
-
-UTF-8 string type `u8string`&lt;a name="U8String"&gt;&lt;/a&gt;
-----------------------------
-
-In addition to the four standard string types `std::string`,
-`std::wstring`, std::u16string` and `std::u32string` provided by the
-C++ standard library, header strcvt/u8string.h defines an additional
-string type `StrCvt::u8string`. Like the locale dependent string type
-`std::string` it has character type `char`, but it always uses a
-locale-independent UTF-8 encoding.  Example usage:
-
-~~~{.cpp}
-
-    #include "strcvt/u8string.h"
-    #include "strcvt/strcvt.h"
-
-    // ...
-
-    using namespace StrCvt;
-
-    u8string us(u8"Hello, World");
-    std::cout &lt;&lt; strcvt(us) &lt;&lt; std::endl;
-~~~
-
-In the example above, the `u8string` is initialized using the C++11
-UTF-8 character literal `u8"string"`.  For output to the
-locale-dependent standard output stream, it is converted from UTF-8 to
-the locale dependent `char` endoding.
-
-
-
-UTF string conversion &lt;a name="utf_string_conversion"&gt;&lt;/a&gt;
----------------------
-
-In the examples above, strcvt() can be replaced with strcvt_utf(),
-which ignores the locale and always performs a UTF transformation.
-The header is "strcvt/strcvt_utf.h".
-
-It is also possible to configure strcvt() to only use UTF conversions
-instead of the locale dependent character conversions.  Open the header
-`strcvt/strcvt_config.h` with an editor and change the preprocessor
-symbol `STRCVT_IMPL_UTF8_ONLY` from 0 to 1.
-
-
-
-Locale-dependent stream conversion &lt;a name="locale_dependent"&gt;&lt;/a&gt;
-----------------------------------
-
-The character type stream converter function charcvt(state, from,
-from_end, from_next, to, to_end, to_next) is defined in header
-strcvt/charcvt.h and uses the API of the member functions in()
-and out() of the standard code conversion interface `std::codecvt`:
-
-~~~{.cpp}
-
-    #include "strcvt/charcvt.h"
-
-    // ...
-
-    using namespace StrCvt;
-
-    std::mbstate_t state = std::mbstate_t(); // Zero-initialize
-    // Convert between character types:
-    result r = charcvt(state, from, from_end, from_next, to, to_end, to_next);
-
-    // ... More calls of charcvt()
-
-    // Return output to initial shift state
-    r = charcvt_unshift(state, to, to_end, to_next);
-~~~
-
-The `state` must be of type `std::mbstate_t`.  It holds the conversion
-state between successive calls of charcvt() and must be explicitly
-zero-initialized before the first use, like shown above.  The
-arguments `from` and `from_end` delimit the source character buffer,
-`to` and `to_end` delimit the destination character buffer, and on
-exit from the function `from_next` and `to_next` point past the last
-converted character.  The function returns `std::codecvt_base::ok` on
-success and `std::codecvt_base::partial` if the output buffer was too
-small to convert the entire input buffer, or if the input buffer ended
-in a part of a multibyte sequence.  After the input has been
-completely converted, possibly by multiple calls to charcvt(),
-charcvt_unshift() must be called to move the output to the initial
-shift state.  The source character type must be specified as a
-template argument to charcvt_unshift(), since different converters are
-used depending on the source character type.  Charcvt_unshift() should
-even be used if the character encoding is known not to be state
-dependent, like UTF-8.  Charcvt_unshift() checks whether trailing
-incomplete Unicode character input sequences are pending, and appends
-a replacement character to the output buffer if necessary to signal
-the presence of trailing garbage.
-
-Charcvt() is specialized for all combinations of source and
-destination character types `char`, `wchar_t`, `char16_t` and
-`char32_t` and converts between the locale-dependent character type
-`char`, the Unicode UTF-16 and UTF-32 character types `char16_t` and
-`char32_t`, and the implementation defined wide character type
-`wchar_t` which is assumed to be equivalent to either `char16_t` or
-`char32_t`.  It is able to consume or deliver single characters from
-multi-character Unicode characters.
-
-The full signature of charcvt() is:
-
-~~~{.cpp}
-
-    template
-    std::codecvt_base::result
-    charcvt(std::mbstate_t&amp; state,
-            const src_charT* from, const src_charT* from_end,
-            const src_charT*&amp; from_next,
-            dst_charT* to, dst_charT* to_end, dst_charT*&amp; dst_next,
-            int flags = 0);
-~~~
-
-The `flags` can be omitted, or they can be set to the constant
-`strcvt_flags_no_partial_conversions` to prevent partial conversions.
-If the flag is omitted or zero, charcvt() is eager and consumes even
-single partial multibyte characters.
-
-
-
-UTF stream conversion &lt;a name="utf_stream_conversion"&gt;&lt;/a&gt;
----------------------
-
-The function charcvt_utf(state, from, from_end, from_next, to, to_end,
-to_next) is defined in header strcvt/charcvt_utf.h and works like
-charcvt() but treats the type `char` as having UTF-8 encoding instead
-of a locale dependent implementation defined encoding:
-
-~~~{.cpp}
-
-    #include "strcvt/charcvt_utf.h"
-
-    // ...
-
-    using namespace StrCvt;
-
-    std::mbstate_t state = std::mbstate_t(); // Zero-initialize
-    // Convert between character types:
-    charcvt_utf(state, from, from_end, from_next, to, to_end, to_next);
-
-    // ... More calls of charcvt_utf()
-
-    // Return output to initial shift state
-    charcvt_utf_unshift(state, to, to_end, to_next);
-~~~
-
-The `state` must be of type `std::mbstate_t`.  It holds the conversion
-state between successive calls of charcvt_utf() and must be explicitly
-zero-initialized before the first use, like shown above.  The
-arguments `from` and `from_end` delimit the source character buffer,
-`to` and `to_end` delimit the destination character buffer, and on
-exit from the function `from_next` and `to_next` point past the last
-converted character.  The function returns `std::codecvt_base::ok` on
-success and `std::codecvt_base::partial` if the output buffer was too
-small to convert the entire input buffer, or if the input buffer ended
-in a part of a multibyte sequence.  After the input has been
-completely converted, possibly by multiple calls to charcvt_utf(),
-charcvt_utf_unshift() checks whether trailing incomplete Unicode
-character input sequences are pending, and appends a replacement
-character to the output buffer if necessary to signal the presence of
-trailing garbage.
-
-Charcvt_utf() is specialized for all combinations of source and
-destination character types `char`, `wchar_t`, `char16_t` and
-`char32_t` and converts between UTF-coded `char`, the Unicode UTF-16
-and UTF-32 character types `char16_t` and `char32_t`, and the
-implementation defined wide character type `wchar_t` which is assumed
-to be equivalent to either `char16_t` or `char32_t`.  Contrary to
-member functions in() and out() of `codecvt`, charcvt_utf() always
-converts (even if the input character type is the same as the output
-character type, in which case it checks the validity of the encoding).
-It always generates valid UTF-8, UTF-16 or UTF-32 output sequences.
-
-For each invalid encoding in the input buffer, charcvt_utf() inserts
-the replacement character U+0xFFFD into the output buffer.  If this is
-not wanted, function charcvt_utf_strict() can be used, which has the
-same interface as charcvt_utf() but returns `std::codecvt_base::error`
-on encoding errors, with `from_next` pointing to the first element of
-the invalid sequence.
-
-There is also a UTF code conversion facet defined in header
-strcvt/codecvt_utf.h
-
-~~~{.cpp}
-
-    class codecvt_utf;
-~~~
-
-This code conversion facet uses charcvt_utf() to convert between UTF
-coded characters `intern_charT` and `extern_charT`, and is specialized
-for all character type combinations.
-
-
-
-Character type converting character iterator &lt;a name="converting_iterator"&gt;&lt;/a&gt;
---------------------------------------------
-
-Instead of converting the entire buffer, it can be accessed trough a
-converting character iterator.  The iterator is created by function
-make_strcvt_iterator():
-
-~~~{.cpp}
-
-    #include "strcvt/strcvt_iterator.h"
-
-    // ...
-
-    using namespace StrCvt;
-
-    // Create char32_t iterator for access to source C string
-    auto it = make_strcvt_iterator("Hello, world!");
-~~~
-
-The end iterator is returned by make_strcvt_iterator() without
-arguments, or it can be obtained from member function end() of the
-iterator.  Since member function begin() is also implemented, and
-returns the iterator itself, the iterator can be used just like a
-container, for example with the range-based for statement:
-
-~~~{.cpp}
-
-    std::u32string w;
-    // Use range-based for statement: process all Unicode characters
-    for (char32_t c: it)
-        w.push_back(c);
-
-    // Alternatively, use simple iterator interface
-    auto e = it.end();
-    for (auto j = it; j != e; ++j)
-        w.push_back(*j);
-~~~
-
-The converting iterator is (indirectly) derived from base class
-`strcvt_iterator_base` which does not depend on the source
-iterator type or the source character type. It has a virtual
-destructor and can be used polymorphically.  The inheritance hierarchy
-looks like this:
-
-~~~{.cpp}
-
-    template
-    class strcvt_iterator_base {
-        // Operators *(), ++(), ==()
-        // Member functions begin(), end()
-    };
-
-    template
-    class strcvt_iterator_impl : public strcvt_iterator_base {
-        // Implements operator ++() which does the conversion using
-        // virtual member function get_next_source_char()
-    };
-
-    template
-    class strcvt_iterator
-        : public strcvt_iterator_impl::value_type&gt; {
-        // Implements get_next_source_char()
-    };
-~~~
-
-Class template `strcvt_iterator_base` does not know the source
-character type and can be used to handle a `strcvt_iterator`
-polymorphically.  Class template `strcvt_iterator_impl` knows the
-source character type but not the type of the iterator.  If the
-precompiled library is used, it is precompiled for all combinations of
-source and destination character type.  Class template
-`strcvt_iterator` is the return value of function
-make_strcvt_iterator().  It implements the source iterator handling.
-
-
-
-Operators &lt;a name="operators"&gt;&lt;/a&gt;
----------
-
-Some operators for strings are defined in header
-"strcvt/strcvt_operators.h".  These are the output operators `&lt;&lt;` for
-strings and character pointers, and the appending `+=` operators for
-strings.  To use them, include header "strcvt/strcvt_operators.h".
-The operators reside in namespace StrCvt and should be imported into
-the user's namespace through a `using namespace` directive:
-
-~~~{.cpp}
-
-    #include "strcvt/strcvt_operators.h"
-    using namespace StrCvt;
-
-    std::cout &lt;&lt; U"Hello, world!\n";
-~~~
-
-
-It would be nice if we could define conversion operators to enable
-assignment between different string types.  But in C++, conversion and
-assignment operators can only be defined as member functions.
-
-
-
-Creating a Library &lt;a name="Library"&gt;&lt;/a&gt;
-------------------
-
-The character type converter is preconfigured for header-only use.
-This means: Just include the header and you are done.  In order to
-reduce space overhead and compilation time, a precompiled library can
-be used.
-
-The main advantage in using a library is that each time the strcvt
-headers are included, the compiler does not need to look at the
-implementation details.  This can speed up compilation significantly.
-
-To create the library, the C++ source files libstrcvt.cpp,
-libstrcvt_iterator.cpp, libstrcvt_utf.cpp and libcodecvt_utf.cpp in
-directory `lib` of the distribution must be compiled.  Under Linux,
-just run `make`.  Before compiling, you may want to select the
-compiler to use: Uncomment to proper *CXX=* - line in the toplevel
-`Makefile.template`.  Running `make` should create a library
-`lib/libxprintf.a`, which has to be linked to the programs.
-
-In Visual C++, instead of building a library, you may just add the
-library source files to your project.
-
-In order to make the headers use the library, you must open the header
-`strcvt/strcvt_config.h` with an editor and change the preprocessor
-symbol `STRCVT_IMPL_USE_LIBRARY` from 0 to 1.  The next time the
-header is included, the library will be used.  You can check that the
-library is used as intended by omitting the library when linking.
-Linking should fail with missing externals.
-
-In order to run the tests, the headers for the *boost* test framework
-are required.
-
-
-
-Compatibility&lt;a name="Compatibility"&gt;&lt;/a&gt;
--------------------------------------------
-
-The code converter has been tested with:
-
-- GCC 4.7, 4.8 and 4.9 on Linux
-- Visual Studio Express 2013 for Windows Desktop with November 2013 CTP
-- Intel C++ 14.0.2 on Linux
-- Clang 3.5.0 on Linux
-
-
-
-Rationale &lt;a name="Rationale"&gt;&lt;/a&gt;
----------
-
-C++11 has the new character types `char16_t` and `char32_t`, which use
-the UTF-16 and UTF-32 Unicode encoding. The question is how to convert
-between the four character types of C++.
-
-The `codecvt` part of the C++11 library looks like some ruins left
-over at the front line between warring factions.
-
-The `codecvt` class template is part of the header \ and
-described in section 22.4.1.4 of the standard.  The first thing to
-note is that according to this specification `codecvt` transforms
-between an *internal* and an *external* character encoding, so it is
-not intended to transform between internal character encodings like
-`char16_t` and `wchar_t`.
-
-According to the standard, each locale shall have specializations of
-`codecvt` for tranformation between the internal character types
-`char`, `wchar_t`, `char16_t` and `char32_t` and the external
-character type `char`.  The standard first says that these
-specializations "convert the implementation-defined native character
-set", only to continue specifying that
-
-- the specialization with external and internal character type both
-  `char` must not convert at all (effectively saying that the internal
-  `char` must have the same encoding as the external `char`),
-
-- the specializations with internal character types `char16_t` and
-  `char32_t` must treat the external character code as UTF-8, so they
-  are explicitly not allowed to treat it as an "implementation-defined
-  native character set".
-
-This leaves the transformation between internal character type
-`wchar_t` and external character type `char` as the only
-locale-dependent transformation, and it is specified to convert
-"between the native character sets for narrow and wide characters".
-So no luck using `codecvt` to transform between native character sets
-and `char16_t` or `char32_t`.
-
-Section 22.5 of the standard specifies the "standard code conversion
-facets".  It contains:
-
-- Facet `codecvt_utf8`, which converts between a UTF-8 coded `char`
-  buffer and UCS2 or UCS4, so it can be used to transform between
-  UTF-8 and `char32_t`. It is not usable for transformation between
-  UTF-8 and `char16_t` because `char16_t` strings are coded as
-  UTF-16, not UCS2.
-
-- Facet `codecvt_utf8_utf16` converts between UTF-8 and UTF-16, so it
-  can be used to transform between UTF-8-coded `char` and `char16_t`.
-
-- Facet `codecvt_utf16` looks like a hack from the 20th century to
-  adapt UTF-16 wide characters to character byte streams.  The
-  UTF-16-coded buffer is addressed through a `char*`.
-
-So we can use these standard code conversion facets to convert between
-UTF-8-coded `char` and `char16_t` or `char32_t`.  We could even
-transform between `char16_t` and `char32_t` by going through an
-intermediate UTF-8-coded `char` buffer.  But again, no
-transformation between native character sets and `char16_t` or
-`char32_t`.
-
-The standard does provide an interface for transformation between
-native `char` and `char16_t` or `char32_t`.  It is hidden at the very
-end of section 21.7 in the string library and consists of the four
-functions mbrtoc16(), c16rtomb(), mbrtoc32() and c32rtomb().
-Unfortunately, a locale can not be specified for these functions.
-They always work with the currently active global locale.
-
-For the following things I know of no standard-conforming procedure:
-
-- Conversion between `wchar_t` and `char16_t` or `char32_t`.  Clearly,
-  going through native `char` is not an option, unless it happens to
-  use UTF-8 encoding.  I worked around this by assuming that the native
-  `wchar_t` type uses Unicode coding and is equivalent to either
-  `char16_t` or `char32_t`, depending on `sizeof(wchar_t)`.  This
-  assumption holds for the systems I have access to, but there may be
-  other `wchar_t` encodings in use.  Older Windows systems used UCS2,
-  where this assumption would not hold, but modern versions of Windows
-  use UTF-16.
-
-- Locale-dependent conversion: especially for output streams where a
-  locale is known that may not be the global locale, it would be
-  useful if the locale could be passed as an argument to the character
-  transformation.  Unfortunately the standard library does not provide
-  a portable way to do this, so I left it out.
-
-First I thought I would reuse the standard-specified UTF coders, but
-first they are incomplete (going from UTF-16 to UTF-32 via an
-intermediate representation as UTF-8 is not very attractive), second
-they are distributed over several different interfaces, and third GCC
-does not have them.  So as a first step I implemented a transformation
-between UTF-coded `char`, `char16_t`, `char32_t` and `wchar_t`.  The
-function charcvt_utf(state, from, from_end, from_next, to, to_end,
-to_next) uses the API of the member functions in() and out() of
-`codecvt`.  The `state` holds the conversion state, `from` and
-`from_end` delimit the source character buffer, `to` and `to_end`
-delimit the destination character buffer, and on exit from the
-function `from_next` and `to_next` point past the last converted
-character.  Like in() and out(), charcvt_utf() returns
-`std::codecvt_base::ok` on success and `std::codecvt_base::partial` if
-the output buffer was too small to convert the entire input buffer, or
-if the input buffer ended in a part of a multibyte sequence.  For each
-invalid encoding in the input buffer, the replacement character
-U+0xFFFD is inserted into the output stream.  If this is not wanted,
-function charcvt_utf_strict() can be used, which has the same
-interface as charcvt_utf() but returns `std::codecvt_base::error` on
-encoding errors, with `from_next` pointing to the first element of the
-invalid sequence.  Both functions are specialized on all combinations
-of `char`, `wchar_t`, `char16_t` and `char32_t`.  They always convert
-(even if input and output type is the same, in which case they check
-the validity of the encoding).  They are able to consume and produce
-single characters forming part of multibyte Unicode characters.
-
-Based on the UTF coder, a locale-dependent converter has been
-implemented. The function charcvt(state, from, from_end, from_next,
-to, to_end, to_next) also uses the API of the member functions in()
-and out() of `codecvt`:  The `state` holds the conversion state,
-`from` and `from_end` delimit the source character buffer, `to` and
-`to_end` delimit the destination character buffer, and on exit from
-the function `from_next` and `to_next` point past the last converted
-character.  This implementation uses the assumption that `wchar_t` is
-encoded as either UTF-16 or UTF-32.  Transformations between wide
-characters `wchar_t`, `char16_t` and `char32_t` use UTF
-transformations, transformations between `char` and wide characters
-treat `char` as a native locale-defined character code.
-
-Convenience functions for the code transformation are:
-
-- std::basic_string strcvt&gt;(const C*
-  from, std::size_t size) converts the `size` characters at `from`
-  into a string of character type `charT`, which is specified as a
-  template parameter.
-
-- std::basic_string strcvt&gt;(const C*
-  from) converts the characters of the null-terminated string `from`
-  into a string of character type `charT`, which is specified as a
-  template parameter.
-
-- std::basic_string strcvt&gt;(const
-  std::basic_string&amp; from) converts the characters of the string
-  `from` into a string of character type `charT`, which is specified
-  as a template parameter.  Specializations for `charT == C` move the
-  string argument to the return value (if the argument is an rvalue)
-  or return the reference to the argument (if the argument is an
-  lvalue).  This is useful in generic code where the concrete types
-  are not known.
-
-- strcvt(const C* from) (strcvt() without template parameter
-  specifying the destination string type) creates an object which
-  can be converted back to a `const C*` or to any string type.  The
-  result can e.g. be assigned to any string type, or passed to a
-  function expecting any string.
-
-- strcvt(const std::basic_string&amp; from) (strcvt() without template
-  parameter specifying the destination string type) casts the type of
-  the argument to a reference to a class derived from
-  std::basic_string, which adds conversion operators to all string
-  types.  The result can e.g. be assigned to any string type, or
-  passed to a function expecting any string.
-
-
-
-License &lt;a name="License"&gt;&lt;/a&gt;
--------
-
-Copyright (c) 2014 Ruediger Helsch; All rights reserved
-
-Permission to use, copy, modify, and distribute this software for any
-purpose and without fee is hereby granted.  The author disclaims all
-warranties with regard to this software.
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ruediger Helsch</dc:creator><pubDate>Wed, 02 Jul 2014 09:50:54 -0000</pubDate><guid>https://sourceforge.netbd93b917c5e74ba02f815874d6fd465939516dba</guid></item><item><title>strcvt modified by Ruediger Helsch</title><link>https://sourceforge.net/p/xprintf/wiki/strcvt/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v6
+++ v7
@@ -9,6 +9,7 @@
 - [Installation](#Installation)
 - [Usage](#Usage)
 - [String conversion](#string_conversion)
+- [UTF-8 string type `u8string`](#U8String)
 - [UTF string conversion](#utf_string_conversion)
 - [Locale dependent stream conversion](#locale_dependent)
 - [UTF stream conversion](#utf_stream_conversion)
@@ -21,6 +22,7 @@

+
 Introduction &lt;a name="Introduction"&gt;&lt;/a&gt;
 ------------

@@ -61,11 +63,13 @@

 In order to use the code converter, its headers have to be included.
 They are:
-
+- [UTF-8 string type `u8string`](#U8String)
 ~~~cpp

     // The main header defining the strcvt() string conversion functions
     #include "/path/to/strcvt/include/strcvt/strcvt.h"
+    // The main header defining the u8string type
+    #include "/path/to/strcvt/include/strcvt/u8string.h"
     // The header defining the strcvt_utf() UTF string conversion functions
     #include "/path/to/strcvt/include/strcvt/strcvt_utf.h"
     // The header defining the charcvt() stream conversion functions
@@ -77,14 +81,11 @@
     // The header defining codecvt_utf
     #include "/path/to/strcvt/include/strcvt/codecvt_utf.h"

+    // Optional header defining output operators
+    #include "/path/to/strcvt/include/strcvt/strcvt_operators.h"
+
     // Import symbols into users namespace
     using namespace StrCvt;
-
-    // Optional header defining output operators
-    #include "/path/to/strcvt/include/strcvt/strcvt_operators.h"
-
-    // Import operators into users namespace
-    using namespace StrCvtOperators;
 ~~~

 If the xprintf include directory `/path/to/strcvt/include` has been
@@ -95,6 +96,8 @@

     // The main header defining the strcvt() string conversion functions
     #include "strcvt/strcvt.h"
+    // The main header defining the u8string type
+    #include "strcvt/u8string.h"
     // The header defining the strcvt_utf() UTF string conversion functions
     #include "strcvt/strcvt_utf.h"
     // The header defining the charcvt() stream conversion functions
@@ -106,14 +109,11 @@
     // The header defining codecvt_utf
     #include "strcvt/codecvt_utf.h"

+    // Optional header defining output operators
+    #include "strcvt/strcvt_operators.h"
+
     // Import symbols into users namespace
     using namespace StrCvt;
-
-    // Optional header defining output operators
-    #include "strcvt/strcvt_operators.h"
-
-    // Import operators into users namespace
-    using namespace StrCvtOperators;
 ~~~

 The strcvt() functions are exported through namespace `StrCvt`.  It
@@ -141,7 +141,11 @@
 -----------------

 Function strcvt\(source) converts strings from the
-source character type to the destination string type.  Example usage:
+source character type to the destination string type.  The
+`dest_string_type` can be `std::string`, `std::wstring`,
+`std::u16string` and `std::u32string`. An additional string type
+`StrCvt::u8string` with UTF-8 encoding is also provided; see section
+[UTF-8 string type `u8string`](#U8String). Example usage:

 ~~~{.cpp}

@@ -185,9 +189,12 @@
     strcvt&gt;(const source_charT* source, std::size_t size);
 ~~~

-Instead of the destination string type, it is also possible to give
-the destination character type as template argument.  It is
-automatically translated to the string type.
+The function with string argument `dest_string
+strcvt(source_string source)` is overloaded for the case
+`dest_string` == `source_string`: it moves its argument to the result
+(for rvalue arguments) or returns a reference to its argument (for
+lvalue arguments).  This is useful in generic code where the concrete
+types are not known.

 It is also possible to omit the template argument determining the
 destination string type.  In this case, strcvt() does no conversion at
@@ -197,7 +204,7 @@

     // strcvt() returns an object which can be assigned to any string type
     std::wstring w = strcvt("Hello, world!");
-    std::basic_string = strcvt(w);
+    std::u16string = strcvt(w);
 ~~~

 For string arguments, the return value of strcvt() is a subclass of
@@ -208,6 +215,36 @@
 In each case, the return value of strcvt() can e.g. be assigned to
 any string type, or can be passed as argument to a function expecting a
 string.
+
+
+
+UTF-8 string type `u8string`&lt;a name="U8String"&gt;&lt;/a&gt;
+----------------------------
+
+In addition to the four standard string types `std::string`,
+`std::wstring`, std::u16string` and `std::u32string` provided by the
+C++ standard library, header strcvt/u8string.h defines an additional
+string type `StrCvt::u8string`. Like the locale dependent string type
+`std::string` it has character type `char`, but it always uses a
+locale-independent UTF-8 encoding.  Example usage:
+
+~~~{.cpp}
+
+    #include "strcvt/u8string.h"
+    #include "strcvt/strcvt.h"
+
+    // ...
+
+    using namespace StrCvt;
+
+    u8string us(u8"Hello, World");
+    std::cout &lt;&lt; strcvt(us) &lt;&lt; std::endl;
+~~~
+
+In the example above, the `u8string` is initialized using the C++11
+UTF-8 character literal `u8"string"`.  For output to the
+locale-dependent standard output stream, it is converted from UTF-8 to
+the locale dependent `char` endoding.

@@ -402,7 +439,7 @@

 ~~~{.cpp}

-    std::basic_string w;
+    std::u32string w;
     // Use range-based for statement: process all Unicode characters
     for (char32_t c: it)
         w.push_back(c);
@@ -455,17 +492,16 @@
 ---------

 Some operators for strings are defined in header
-"strcvt/strcvt_operators.h".  These are the output operators `&lt;&lt;`
-for strings and character pointers, and the appending `+=` operators
-for strings.  To use them, include header
-"strcvt/strcvt_operators.h".  The operators reside in namespace
-StrCvtOperators and should be imported into the user's namespace
-through a `using namespace` directive:
+"strcvt/strcvt_operators.h".  These are the output operators `&lt;&lt;` for
+strings and character pointers, and the appending `+=` operators for
+strings.  To use them, include header "strcvt/strcvt_operators.h".
+The operators reside in namespace StrCvt and should be imported into
+the user's namespace through a `using namespace` directive:

 ~~~{.cpp}

     #include "strcvt/strcvt_operators.h"
-    using namespace StrCvtOperators;
+    using namespace StrCvt;

     std::cout &lt;&lt; U"Hello, world!\n";
 ~~~
@@ -654,23 +690,24 @@

 Convenience functions for the code transformation are:

-- basic_string strcvt&gt;(const C* from,
-  std::size_t size) converts the `size` characters at `from` into a
-  string of character type `charT`, which is specified as a template
-  parameter.
-
-- basic_string strcvt&gt;(const C* from)
-  converts the characters of the null-terminated string `from` into a
-  string of character type `charT`, which is specified as a template
-  parameter.
-
-- basic_string strcvt&gt;(const
+- std::basic_string strcvt&gt;(const C*
+  from, std::size_t size) converts the `size` characters at `from`
+  into a string of character type `charT`, which is specified as a
+  template parameter.
+
+- std::basic_string strcvt&gt;(const C*
+  from) converts the characters of the null-terminated string `from`
+  into a string of character type `charT`, which is specified as a
+  template parameter.
+
+- std::basic_string strcvt&gt;(const
   std::basic_string&amp; from) converts the characters of the string
   `from` into a string of character type `charT`, which is specified
-  as a template parameter.  A specialization for `charT == C` and
-  rvalue argument is provided, which moves the input string to the
-  return value.  Note that this is the only conversion which does not
-  check the validity of the encoding of the string.
+  as a template parameter.  Specializations for `charT == C` move the
+  string argument to the return value (if the argument is an rvalue)
+  or return the reference to the argument (if the argument is an
+  lvalue).  This is useful in generic code where the concrete types
+  are not known.

 - strcvt(const C* from) (strcvt() without template parameter
   specifying the destination string type) creates an object which
@@ -695,4 +732,3 @@
 Permission to use, copy, modify, and distribute this software for any
 purpose and without fee is hereby granted.  The author disclaims all
 warranties with regard to this software.
-
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ruediger Helsch</dc:creator><pubDate>Tue, 01 Jul 2014 23:35:02 -0000</pubDate><guid>https://sourceforge.netbe1598a90beb9649af81c7e40fd8bba4d85aafaa</guid></item><item><title>strcvt modified by Ruediger Helsch</title><link>https://sourceforge.net/p/xprintf/wiki/strcvt/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v5
+++ v6
@@ -655,7 +655,7 @@
 Convenience functions for the code transformation are:

 - basic_string strcvt&gt;(const C* from,
-  std::size_t size) converts the `size* characters at `from` into a
+  std::size_t size) converts the `size` characters at `from` into a
   string of character type `charT`, which is specified as a template
   parameter.

&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ruediger Helsch</dc:creator><pubDate>Mon, 30 Jun 2014 15:40:42 -0000</pubDate><guid>https://sourceforge.net10b12db4872b69268c8f2f1f747db786096931ac</guid></item><item><title>strcvt modified by Ruediger Helsch</title><link>https://sourceforge.net/p/xprintf/wiki/strcvt/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v4
+++ v5
@@ -21,7 +21,6 @@

-
 Introduction &lt;a name="Introduction"&gt;&lt;/a&gt;
 ------------

@@ -35,7 +34,8 @@
   `char32_t` in all combinations.

 - Implements the API of the C++ `codecvt` member functions in() and
-  out() for stream conversion, and convenience functions for string conversion.
+  out() for stream conversion, and convenience functions for string
+  conversion.

 - Locale-dependent conversion and UTF conversion.

@@ -68,9 +68,9 @@
     #include "/path/to/strcvt/include/strcvt/strcvt.h"
     // The header defining the strcvt_utf() UTF string conversion functions
     #include "/path/to/strcvt/include/strcvt/strcvt_utf.h"
-    // The header defining the strcvt() stream conversion functions
+    // The header defining the charcvt() stream conversion functions
     #include "/path/to/strcvt/include/strcvt/charcvt.h"
-    // The header defining the strcvt_utf() UTF stream conversion functions
+    // The header defining the charcvt_utf() UTF stream conversion functions
     #include "/path/to/strcvt/include/strcvt/charcvt_utf.h"
     // The header defining the strcvt_iterator
     #include "/path/to/strcvt/include/strcvt/strcvt_iterator.h"
@@ -97,9 +97,9 @@
     #include "strcvt/strcvt.h"
     // The header defining the strcvt_utf() UTF string conversion functions
     #include "strcvt/strcvt_utf.h"
-    // The header defining the strcvt() stream conversion functions
+    // The header defining the charcvt() stream conversion functions
     #include "strcvt/charcvt.h"
-    // The header defining the strcvt_utf() UTF stream conversion functions
+    // The header defining the charcvt_utf() UTF stream conversion functions
     #include "strcvt/charcvt_utf.h"
     // The header defining the strcvt_iterator
     #include "strcvt/strcvt_iterator.h"
@@ -174,15 +174,15 @@

     template
     std::basic_string
-    strcvt &gt;(const std::basic_string&amp; source);
+    strcvt&gt;(const std::basic_string&amp; source);

     template
     std::basic_string
-    strcvt &gt;(const source_charT* source);
+    strcvt&gt;(const source_charT* source);

     template
     std::basic_string
-    strcvt &gt;(const source_charT* source, std::size_t size);
+    strcvt&gt;(const source_charT* source, std::size_t size);
 ~~~

 Instead of the destination string type, it is also possible to give
@@ -306,9 +306,8 @@

 The function charcvt_utf(state, from, from_end, from_next, to, to_end,
 to_next) is defined in header strcvt/charcvt_utf.h and works like
-charcvt(state, from, from_end, from_next, to, to_end, to_next) but
-treats the type `char` as having UTF-8 encoding instead of a locale
-dependent implementation defined encoding:
+charcvt() but treats the type `char` as having UTF-8 encoding instead
+of a locale dependent implementation defined encoding:

 ~~~{.cpp}

@@ -521,6 +520,7 @@
 - GCC 4.7, 4.8 and 4.9 on Linux
 - Visual Studio Express 2013 for Windows Desktop with November 2013 CTP
 - Intel C++ 14.0.2 on Linux
+- Clang 3.5.0 on Linux

@@ -618,20 +618,20 @@
 they are distributed over several different interfaces, and third GCC
 does not have them.  So as a first step I implemented a transformation
 between UTF-coded `char`, `char16_t`, `char32_t` and `wchar_t`.  The
-function strcvt_utf(state, from, from_end, from_next, to, to_end,
+function charcvt_utf(state, from, from_end, from_next, to, to_end,
 to_next) uses the API of the member functions in() and out() of
 `codecvt`.  The `state` holds the conversion state, `from` and
 `from_end` delimit the source character buffer, `to` and `to_end`
 delimit the destination character buffer, and on exit from the
 function `from_next` and `to_next` point past the last converted
-character.  Like in() and out(), strcvt_utf() returns
+character.  Like in() and out(), charcvt_utf() returns
 `std::codecvt_base::ok` on success and `std::codecvt_base::partial` if
 the output buffer was too small to convert the entire input buffer, or
 if the input buffer ended in a part of a multibyte sequence.  For each
 invalid encoding in the input buffer, the replacement character
 U+0xFFFD is inserted into the output stream.  If this is not wanted,
-function strcvt_utf_strict() can be used, which has the same
-interface as strcvt_utf() but returns `std::codecvt_base::error` on
+function charcvt_utf_strict() can be used, which has the same
+interface as charcvt_utf() but returns `std::codecvt_base::error` on
 encoding errors, with `from_next` pointing to the first element of the
 invalid sequence.  Both functions are specialized on all combinations
 of `char`, `wchar_t`, `char16_t` and `char32_t`.  They always convert
@@ -640,7 +640,7 @@
 single characters forming part of multibyte Unicode characters.

 Based on the UTF coder, a locale-dependent converter has been
-implemented. The function strcvt(state, from, from_end, from_next,
+implemented. The function charcvt(state, from, from_end, from_next,
 to, to_end, to_next) also uses the API of the member functions in()
 and out() of `codecvt`:  The `state` holds the conversion state,
 `from` and `from_end` delimit the source character buffer, `to` and
@@ -654,36 +654,37 @@

 Convenience functions for the code transformation are:

-- basic_string strcvt(const C* from, const C*
-  from_end) converts the characters between `from` and `from_end` into
-  a string of character type `charT`, which is specified as a template
+- basic_string strcvt&gt;(const C* from,
+  std::size_t size) converts the `size* characters at `from` into a
+  string of character type `charT`, which is specified as a template
   parameter.

-- basic_string strcvt(const C* from) converts the
-  characters of the null-terminated string `from` into a string of
-  character type `charT`, which is specified as a template parameter.
-
-- basic_string strcvt(const std::basic_string&amp;
-  from) converts the characters of the string `from` into a string of
-  character type `charT`, which is specified as a template parameter.
-  A specialization for `charT = C` and rvalue `from` is provided, which
-  moves the input string to the return value.  Note that this is the
-  only conversion which does not check the validity of the encoding of
-  the string.
-
-- strcvt(const std::basic_string&amp; from) (strcvt() without
-  template parameter `charT` specifying the destination character
-  type) casts the type of the argument to a reference to a class
-  derived from std::basic_string, which adds conversion operators
-  to all string types.  The result can e.g. be assigned to any string
-  type, or passed to a function expecting any string.
-
-- strcvt(const C* from) (strcvt() without template parameter `charT`
-  specifying the destination character type) creates an object which
+- basic_string strcvt&gt;(const C* from)
+  converts the characters of the null-terminated string `from` into a
+  string of character type `charT`, which is specified as a template
+  parameter.
+
+- basic_string strcvt&gt;(const
+  std::basic_string&amp; from) converts the characters of the string
+  `from` into a string of character type `charT`, which is specified
+  as a template parameter.  A specialization for `charT == C` and
+  rvalue argument is provided, which moves the input string to the
+  return value.  Note that this is the only conversion which does not
+  check the validity of the encoding of the string.
+
+- strcvt(const C* from) (strcvt() without template parameter
+  specifying the destination string type) creates an object which
   can be converted back to a `const C*` or to any string type.  The
   result can e.g. be assigned to any string type, or passed to a
   function expecting any string.

+- strcvt(const std::basic_string&amp; from) (strcvt() without template
+  parameter specifying the destination string type) casts the type of
+  the argument to a reference to a class derived from
+  std::basic_string, which adds conversion operators to all string
+  types.  The result can e.g. be assigned to any string type, or
+  passed to a function expecting any string.
+

 License &lt;a name="License"&gt;&lt;/a&gt;
@@ -694,3 +695,4 @@
 Permission to use, copy, modify, and distribute this software for any
 purpose and without fee is hereby granted.  The author disclaims all
 warranties with regard to this software.
+
&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ruediger Helsch</dc:creator><pubDate>Mon, 30 Jun 2014 15:37:27 -0000</pubDate><guid>https://sourceforge.netd2aaa73fe9bdef321b4294f05d376f3b843581f5</guid></item><item><title>strcvt modified by Ruediger Helsch</title><link>https://sourceforge.net/p/xprintf/wiki/strcvt/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v3
+++ v4
@@ -214,7 +214,7 @@
 UTF string conversion &lt;a name="utf_string_conversion"&gt;&lt;/a&gt;
 ---------------------

-In the examples above, strcvt() can be replaces with strcvt_utf(),
+In the examples above, strcvt() can be replaced with strcvt_utf(),
 which ignores the locale and always performs a UTF transformation.
 The header is "strcvt/strcvt_utf.h".

&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ruediger Helsch</dc:creator><pubDate>Sun, 29 Jun 2014 22:15:19 -0000</pubDate><guid>https://sourceforge.netb6c7c43824434f27a48903d37c53a3664943aaf0</guid></item><item><title>strcvt modified by Ruediger Helsch</title><link>https://sourceforge.net/p/xprintf/wiki/strcvt/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v2
+++ v3
@@ -140,8 +140,8 @@
 String conversion &lt;a name="string_conversion"&gt;&lt;/a&gt;
 -----------------

-Function strcvt\(source) converts strings from one
-character type to the other.  Example usage:
+Function strcvt\(source) converts strings from the
+source character type to the destination string type.  Example usage:

 ~~~{.cpp}

&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ruediger Helsch</dc:creator><pubDate>Sun, 29 Jun 2014 22:13:17 -0000</pubDate><guid>https://sourceforge.netb52b30322d5e5f2590a86f3a190d3a109d8594d1</guid></item><item><title>strcvt modified by Ruediger Helsch</title><link>https://sourceforge.net/p/xprintf/wiki/strcvt/</link><description>&lt;div class="markdown_content"&gt;&lt;pre&gt;--- v1
+++ v2
@@ -35,7 +35,7 @@
   `char32_t` in all combinations.

 - Implements the API of the C++ `codecvt` member functions in() and
-  out(), and convenience functions for string conversion.
+  out() for stream conversion, and convenience functions for string conversion.

 - Locale-dependent conversion and UTF conversion.

@@ -51,7 +51,7 @@
 search path of the compiler.  This is commonly achieved with the
 option `-I/path/to/strcvt/include` (assuming that the xprintf
 distribution has been upacked to directory `/path/to/xprintf`).  Then
-you can include the code converter header through their standard names
+you can include the code converter headers through their standard names
 like "strcvt/strcvt.h".

&lt;/pre&gt;
&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ruediger Helsch</dc:creator><pubDate>Sun, 29 Jun 2014 22:06:03 -0000</pubDate><guid>https://sourceforge.net37d7129ae673b90e13d3eb26cb0d65df2cd6b275</guid></item><item><title>strcvt modified by Ruediger Helsch</title><link>https://sourceforge.net/p/xprintf/wiki/strcvt/</link><description>&lt;div class="markdown_content"&gt;&lt;h1 id="strcvt-character-type-converter-for-c11"&gt;Strcvt - Character type converter for C++11&lt;/h1&gt;
&lt;h2 id="contents"&gt;Contents&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a class="" href="#Introduction"&gt;Introduction&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#Installation"&gt;Installation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#Usage"&gt;Usage&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#string_conversion"&gt;String conversion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#utf_string_conversion"&gt;UTF string conversion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#locale_dependent"&gt;Locale dependent stream conversion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#utf_stream_conversion"&gt;UTF stream conversion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#converting_iterator"&gt;Character type converting iterator&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#operators"&gt;Operators&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#Library"&gt;Creating a Library&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#Compatibility"&gt;Compatibility&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#Rationale"&gt;Rationale&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class="" href="#License"&gt;License&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="introduction-wzxhzdk14wzxhzdk15"&gt;Introduction &lt;a name="Introduction"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;C++11 has the new character types &lt;code&gt;char16_t&lt;/code&gt; and &lt;code&gt;char32_t&lt;/code&gt;, which use&lt;br /&gt;
the UTF-16 and UTF-32 Unicode encoding. The question is how to convert&lt;br /&gt;
between the four character types of C++.&lt;/p&gt;
&lt;p&gt;This library converts between the different character types of C++.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Conversion between the types &lt;code&gt;char&lt;/code&gt;, &lt;code&gt;wchar_t&lt;/code&gt;, &lt;code&gt;char16_t&lt;/code&gt; and&lt;br /&gt;
&lt;code&gt;char32_t&lt;/code&gt; in all combinations.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Implements the API of the C++ &lt;code&gt;codecvt&lt;/code&gt; member functions in() and&lt;br /&gt;
  out(), and convenience functions for string conversion.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Locale-dependent conversion and UTF conversion.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="installation-wzxhzdk16wzxhzdk17"&gt;Installation &lt;a name="Installation"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Unpack the distribution to a directory on your local machine.  You can&lt;br /&gt;
include the headers in subdirectory &lt;code&gt;include/xprintf&lt;/code&gt; from your&lt;br /&gt;
program.  To make inclusion of the headers easier, it is recommended to&lt;br /&gt;
add the subdirectory &lt;code&gt;include&lt;/code&gt; of the distribution to the include file&lt;br /&gt;
search path of the compiler.  This is commonly achieved with the&lt;br /&gt;
option &lt;code&gt;-I/path/to/strcvt/include&lt;/code&gt; (assuming that the xprintf&lt;br /&gt;
distribution has been upacked to directory &lt;code&gt;/path/to/xprintf&lt;/code&gt;).  Then&lt;br /&gt;
you can include the code converter header through their standard names&lt;br /&gt;
like "strcvt/strcvt.h".&lt;/p&gt;
&lt;h2 id="usage-wzxhzdk18wzxhzdk19"&gt;Usage &lt;a name="Usage"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In order to use the code converter, its headers have to be included.&lt;br /&gt;
They are:&lt;/p&gt;
&lt;p&gt;~~~cpp&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c1"&gt;// The main header defining the strcvt() string conversion functions&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/path/to/strcvt/include/strcvt/strcvt.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining the strcvt_utf() UTF string conversion functions&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/path/to/strcvt/include/strcvt/strcvt_utf.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining the strcvt() stream conversion functions&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/path/to/strcvt/include/strcvt/charcvt.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining the strcvt_utf() UTF stream conversion functions&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/path/to/strcvt/include/strcvt/charcvt_utf.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining the strcvt_iterator&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/path/to/strcvt/include/strcvt/strcvt_iterator.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining codecvt_utf&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/path/to/strcvt/include/strcvt/codecvt_utf.h&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;// Import symbols into users namespace&lt;/span&gt;
&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Optional header defining output operators&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;/path/to/strcvt/include/strcvt/strcvt_operators.h&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;// Import operators into users namespace&lt;/span&gt;
&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;StrCvtOperators&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;If the xprintf include directory &lt;code&gt;/path/to/strcvt/include&lt;/code&gt; has been&lt;br /&gt;
added to the include file search path of the compiler, e.g. using the&lt;br /&gt;
compiler option &lt;code&gt;-I/path/to/strcvt/include&lt;/code&gt;, this reduces to:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c1"&gt;// The main header defining the strcvt() string conversion functions&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;strcvt/strcvt.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining the strcvt_utf() UTF string conversion functions&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;strcvt/strcvt_utf.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining the strcvt() stream conversion functions&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;strcvt/charcvt.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining the strcvt_utf() UTF stream conversion functions&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;strcvt/charcvt_utf.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining the strcvt_iterator&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;strcvt/strcvt_iterator.h&amp;quot;&lt;/span&gt;
&lt;span class="c1"&gt;// The header defining codecvt_utf&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;strcvt/codecvt_utf.h&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;// Import symbols into users namespace&lt;/span&gt;
&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Optional header defining output operators&lt;/span&gt;
&lt;span class="p"&gt;#&lt;/span&gt;&lt;span class="k"&gt;include&lt;/span&gt; &lt;span class="s"&gt;&amp;quot;strcvt/strcvt_operators.h&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;// Import operators into users namespace&lt;/span&gt;
&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;StrCvtOperators&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;The strcvt() functions are exported through namespace &lt;code&gt;StrCvt&lt;/code&gt;.  It&lt;br /&gt;
is recommented to make them available via a &lt;code&gt;using namespace&lt;/code&gt;&lt;br /&gt;
directive like in the example above.  Alternatively it is possible to&lt;br /&gt;
import the functions strcvt() and strcvt_utf() etc. separately&lt;br /&gt;
through &lt;code&gt;using&lt;/code&gt; declarations:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;strcvt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;strcvt_utf&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;strcvt_utf_strict&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;make_strcvt_iterator&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;The character type converter is pre-configured for header-only use.&lt;br /&gt;
This means: Just include the header and you are done.  In order to&lt;br /&gt;
reduce space overhead and compilation time, a precompiled library can&lt;br /&gt;
be used.  See section &lt;a class="" href="#library"&gt;Creating a Library&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id="string-conversion-wzxhzdk20wzxhzdk21"&gt;String conversion &lt;a name="string_conversion"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Function strcvt\&amp;lt;dest_charT&gt;(source) converts strings from one&lt;br /&gt;
character type to the other.  Example usage:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="cp"&gt;#include &amp;quot;strcvt/strcvt.h&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;// ...&lt;/span&gt;

&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Convert const char* C string to wide wchar_t string&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;wstring&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strcvt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;wstring&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Hello, world!&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Convert wide string to string of different character type&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strcvt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// Convert string to char16_t string&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;u16string&lt;/span&gt; &lt;span class="n"&gt;s16&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strcvt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;u16string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// The source buffer can also be specified as (pointer, size):&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;strcvt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;s16&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;endl&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;The source string can be specified as:&lt;br /&gt;
- a character pointer of any character type (C-style null-terminated string),&lt;br /&gt;
- a C++ string of arbitrary character type, or&lt;br /&gt;
- a character pointer and a size.&lt;/p&gt;
&lt;p&gt;The destination string type is specified as a template argument to&lt;br /&gt;
strcvt().  The function is specialized on all combinations of source&lt;br /&gt;
and destination character types. So the full signatures are:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;source_charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;basic_string&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nx"&gt;strcvt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;basic_string&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;basic_string&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;source_charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;source&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;source_charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;basic_string&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nx"&gt;strcvt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;basic_string&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;source_charT&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;source&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;source_charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;basic_string&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nx"&gt;strcvt&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;basic_string&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;source_charT&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;size_t&lt;/span&gt; &lt;span class="nb"&gt;size&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;Instead of the destination string type, it is also possible to give&lt;br /&gt;
the destination character type as template argument.  It is&lt;br /&gt;
automatically translated to the string type.&lt;/p&gt;
&lt;p&gt;It is also possible to omit the template argument determining the&lt;br /&gt;
destination string type.  In this case, strcvt() does no conversion at&lt;br /&gt;
all, but returns an object which can be converted to all string types.&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="c1"&gt;// strcvt() returns an object which can be assigned to any string type&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;wstring&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strcvt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Hello, world!&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;basic_string&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;char16_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strcvt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;For string arguments, the return value of strcvt() is a subclass of&lt;br /&gt;
the class of its argument.  The subclass adds conversion operators to&lt;br /&gt;
all string types.  For character pointer arguments, the return value&lt;br /&gt;
of strcvt() is an object which can be converted to a character&lt;br /&gt;
pointer (of the character type of the argument) or to any string type.&lt;br /&gt;
In each case, the return value of strcvt() can e.g. be assigned to&lt;br /&gt;
any string type, or can be passed as argument to a function expecting a&lt;br /&gt;
string.&lt;/p&gt;
&lt;h2 id="utf-string-conversion-wzxhzdk22wzxhzdk23"&gt;UTF string conversion &lt;a name="utf_string_conversion"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In the examples above, strcvt() can be replaces with strcvt_utf(),&lt;br /&gt;
which ignores the locale and always performs a UTF transformation.&lt;br /&gt;
The header is "strcvt/strcvt_utf.h".&lt;/p&gt;
&lt;p&gt;It is also possible to configure strcvt() to only use UTF conversions&lt;br /&gt;
instead of the locale dependent character conversions.  Open the header&lt;br /&gt;
&lt;code&gt;strcvt/strcvt_config.h&lt;/code&gt; with an editor and change the preprocessor&lt;br /&gt;
symbol &lt;code&gt;STRCVT_IMPL_UTF8_ONLY&lt;/code&gt; from 0 to 1.&lt;/p&gt;
&lt;h2 id="locale-dependent-stream-conversion-wzxhzdk24wzxhzdk25"&gt;Locale-dependent stream conversion &lt;a name="locale_dependent"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The character type stream converter function charcvt(state, from,&lt;br /&gt;
from_end, from_next, to, to_end, to_next) is defined in header&lt;br /&gt;
strcvt/charcvt.h and uses the API of the member functions in()&lt;br /&gt;
and out() of the standard code conversion interface &lt;code&gt;std::codecvt&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="vi"&gt;#include&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;strcvt/charcvt.h&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;// ...&lt;/span&gt;

&lt;span class="nx"&gt;using&lt;/span&gt; &lt;span class="nx"&gt;namespace&lt;/span&gt; &lt;span class="nx"&gt;StrCvt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;mbstate_t&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;mbstate_t&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Zero-initialize&lt;/span&gt;
&lt;span class="c1"&gt;// Convert between character types:&lt;/span&gt;
&lt;span class="nb"&gt;result&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;charcvt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;from_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;from_next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;to_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;to_next&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// ... More calls of charcvt()&lt;/span&gt;

&lt;span class="c1"&gt;// Return output to initial shift state&lt;/span&gt;
&lt;span class="n"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;charcvt_unshift&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;source_charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;to_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;to_next&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;state&lt;/code&gt; must be of type &lt;code&gt;std::mbstate_t&lt;/code&gt;.  It holds the conversion&lt;br /&gt;
state between successive calls of charcvt() and must be explicitly&lt;br /&gt;
zero-initialized before the first use, like shown above.  The&lt;br /&gt;
arguments &lt;code&gt;from&lt;/code&gt; and &lt;code&gt;from_end&lt;/code&gt; delimit the source character buffer,&lt;br /&gt;
&lt;code&gt;to&lt;/code&gt; and &lt;code&gt;to_end&lt;/code&gt; delimit the destination character buffer, and on&lt;br /&gt;
exit from the function &lt;code&gt;from_next&lt;/code&gt; and &lt;code&gt;to_next&lt;/code&gt; point past the last&lt;br /&gt;
converted character.  The function returns &lt;code&gt;std::codecvt_base::ok&lt;/code&gt; on&lt;br /&gt;
success and &lt;code&gt;std::codecvt_base::partial&lt;/code&gt; if the output buffer was too&lt;br /&gt;
small to convert the entire input buffer, or if the input buffer ended&lt;br /&gt;
in a part of a multibyte sequence.  After the input has been&lt;br /&gt;
completely converted, possibly by multiple calls to charcvt(),&lt;br /&gt;
charcvt_unshift() must be called to move the output to the initial&lt;br /&gt;
shift state.  The source character type must be specified as a&lt;br /&gt;
template argument to charcvt_unshift(), since different converters are&lt;br /&gt;
used depending on the source character type.  Charcvt_unshift() should&lt;br /&gt;
even be used if the character encoding is known not to be state&lt;br /&gt;
dependent, like UTF-8.  Charcvt_unshift() checks whether trailing&lt;br /&gt;
incomplete Unicode character input sequences are pending, and appends&lt;br /&gt;
a replacement character to the output buffer if necessary to signal&lt;br /&gt;
the presence of trailing garbage.&lt;/p&gt;
&lt;p&gt;Charcvt() is specialized for all combinations of source and&lt;br /&gt;
destination character types &lt;code&gt;char&lt;/code&gt;, &lt;code&gt;wchar_t&lt;/code&gt;, &lt;code&gt;char16_t&lt;/code&gt; and&lt;br /&gt;
&lt;code&gt;char32_t&lt;/code&gt; and converts between the locale-dependent character type&lt;br /&gt;
&lt;code&gt;char&lt;/code&gt;, the Unicode UTF-16 and UTF-32 character types &lt;code&gt;char16_t&lt;/code&gt; and&lt;br /&gt;
&lt;code&gt;char32_t&lt;/code&gt;, and the implementation defined wide character type&lt;br /&gt;
&lt;code&gt;wchar_t&lt;/code&gt; which is assumed to be equivalent to either &lt;code&gt;char16_t&lt;/code&gt; or&lt;br /&gt;
&lt;code&gt;char32_t&lt;/code&gt;.  It is able to consume or deliver single characters from&lt;br /&gt;
multi-character Unicode characters.&lt;/p&gt;
&lt;p&gt;The full signature of charcvt() is:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt; &lt;span class="n"&gt;src_charT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;class&lt;/span&gt; &lt;span class="n"&gt;dst_charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;codecvt_base&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;
&lt;span class="n"&gt;charcvt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;mbstate_t&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;src_charT&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;src_charT&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;from_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;src_charT&lt;/span&gt;&lt;span class="o"&gt;*&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;from_next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;dst_charT&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dst_charT&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;to_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dst_charT&lt;/span&gt;&lt;span class="o"&gt;*&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;dst_next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;flags&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;flags&lt;/code&gt; can be omitted, or they can be set to the constant&lt;br /&gt;
&lt;code&gt;strcvt_flags_no_partial_conversions&lt;/code&gt; to prevent partial conversions.&lt;br /&gt;
If the flag is omitted or zero, charcvt() is eager and consumes even&lt;br /&gt;
single partial multibyte characters.&lt;/p&gt;
&lt;h2 id="utf-stream-conversion-wzxhzdk26wzxhzdk27"&gt;UTF stream conversion &lt;a name="utf_stream_conversion"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The function charcvt_utf(state, from, from_end, from_next, to, to_end,&lt;br /&gt;
to_next) is defined in header strcvt/charcvt_utf.h and works like&lt;br /&gt;
charcvt(state, from, from_end, from_next, to, to_end, to_next) but&lt;br /&gt;
treats the type &lt;code&gt;char&lt;/code&gt; as having UTF-8 encoding instead of a locale&lt;br /&gt;
dependent implementation defined encoding:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="cp"&gt;#include &amp;quot;strcvt/charcvt_utf.h&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;// ...&lt;/span&gt;

&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;StrCvt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;mbstate_t&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="kt"&gt;mbstate_t&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Zero-initialize&lt;/span&gt;
&lt;span class="c1"&gt;// Convert between character types:&lt;/span&gt;
&lt;span class="n"&gt;charcvt_utf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;from_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;from_next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to_next&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// ... More calls of charcvt_utf()&lt;/span&gt;

&lt;span class="c1"&gt;// Return output to initial shift state&lt;/span&gt;
&lt;span class="n"&gt;charcvt_utf_unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to_end&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;to_next&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;state&lt;/code&gt; must be of type &lt;code&gt;std::mbstate_t&lt;/code&gt;.  It holds the conversion&lt;br /&gt;
state between successive calls of charcvt_utf() and must be explicitly&lt;br /&gt;
zero-initialized before the first use, like shown above.  The&lt;br /&gt;
arguments &lt;code&gt;from&lt;/code&gt; and &lt;code&gt;from_end&lt;/code&gt; delimit the source character buffer,&lt;br /&gt;
&lt;code&gt;to&lt;/code&gt; and &lt;code&gt;to_end&lt;/code&gt; delimit the destination character buffer, and on&lt;br /&gt;
exit from the function &lt;code&gt;from_next&lt;/code&gt; and &lt;code&gt;to_next&lt;/code&gt; point past the last&lt;br /&gt;
converted character.  The function returns &lt;code&gt;std::codecvt_base::ok&lt;/code&gt; on&lt;br /&gt;
success and &lt;code&gt;std::codecvt_base::partial&lt;/code&gt; if the output buffer was too&lt;br /&gt;
small to convert the entire input buffer, or if the input buffer ended&lt;br /&gt;
in a part of a multibyte sequence.  After the input has been&lt;br /&gt;
completely converted, possibly by multiple calls to charcvt_utf(),&lt;br /&gt;
charcvt_utf_unshift() checks whether trailing incomplete Unicode&lt;br /&gt;
character input sequences are pending, and appends a replacement&lt;br /&gt;
character to the output buffer if necessary to signal the presence of&lt;br /&gt;
trailing garbage.&lt;/p&gt;
&lt;p&gt;Charcvt_utf() is specialized for all combinations of source and&lt;br /&gt;
destination character types &lt;code&gt;char&lt;/code&gt;, &lt;code&gt;wchar_t&lt;/code&gt;, &lt;code&gt;char16_t&lt;/code&gt; and&lt;br /&gt;
&lt;code&gt;char32_t&lt;/code&gt; and converts between UTF-coded &lt;code&gt;char&lt;/code&gt;, the Unicode UTF-16&lt;br /&gt;
and UTF-32 character types &lt;code&gt;char16_t&lt;/code&gt; and &lt;code&gt;char32_t&lt;/code&gt;, and the&lt;br /&gt;
implementation defined wide character type &lt;code&gt;wchar_t&lt;/code&gt; which is assumed&lt;br /&gt;
to be equivalent to either &lt;code&gt;char16_t&lt;/code&gt; or &lt;code&gt;char32_t&lt;/code&gt;.  Contrary to&lt;br /&gt;
member functions in() and out() of &lt;code&gt;codecvt&lt;/code&gt;, charcvt_utf() always&lt;br /&gt;
converts (even if the input character type is the same as the output&lt;br /&gt;
character type, in which case it checks the validity of the encoding).&lt;br /&gt;
It always generates valid UTF-8, UTF-16 or UTF-32 output sequences.&lt;/p&gt;
&lt;p&gt;For each invalid encoding in the input buffer, charcvt_utf() inserts&lt;br /&gt;
the replacement character U+0xFFFD into the output buffer.  If this is&lt;br /&gt;
not wanted, function charcvt_utf_strict() can be used, which has the&lt;br /&gt;
same interface as charcvt_utf() but returns &lt;code&gt;std::codecvt_base::error&lt;/code&gt;&lt;br /&gt;
on encoding errors, with &lt;code&gt;from_next&lt;/code&gt; pointing to the first element of&lt;br /&gt;
the invalid sequence.&lt;/p&gt;
&lt;p&gt;There is also a UTF code conversion facet defined in header&lt;br /&gt;
strcvt/codecvt_utf.h&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="n"&gt;class&lt;/span&gt; &lt;span class="n"&gt;codecvt_utf&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;intern_charT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;extern_charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;This code conversion facet uses charcvt_utf() to convert between UTF&lt;br /&gt;
coded characters &lt;code&gt;intern_charT&lt;/code&gt; and &lt;code&gt;extern_charT&lt;/code&gt;, and is specialized&lt;br /&gt;
for all character type combinations.&lt;/p&gt;
&lt;h2 id="character-type-converting-character-iterator-wzxhzdk28wzxhzdk29"&gt;Character type converting character iterator &lt;a name="converting_iterator"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Instead of converting the entire buffer, it can be accessed trough a&lt;br /&gt;
converting character iterator.  The iterator is created by function&lt;br /&gt;
make_strcvt_iterator():&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="vi"&gt;#include&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;strcvt/strcvt_iterator.h&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;// ...&lt;/span&gt;

&lt;span class="nx"&gt;using&lt;/span&gt; &lt;span class="nx"&gt;namespace&lt;/span&gt; &lt;span class="nx"&gt;StrCvt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Create char32_t iterator for access to source C string&lt;/span&gt;
&lt;span class="nx"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;make_strcvt_iterator&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;char32_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Hello, world!&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;The end iterator is returned by make_strcvt_iterator() without&lt;br /&gt;
arguments, or it can be obtained from member function end() of the&lt;br /&gt;
iterator.  Since member function begin() is also implemented, and&lt;br /&gt;
returns the iterator itself, the iterator can be used just like a&lt;br /&gt;
container, for example with the range-based for statement:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="nx"&gt;std&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;basic_string&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;char32_t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;w&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// Use range-based for statement: process all Unicode characters&lt;/span&gt;
&lt;span class="nb"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;char32_t&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;w.push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Alternatively, use simple iterator interface&lt;/span&gt;
&lt;span class="nx"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;it.end&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nb"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;auto&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;it&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;w.push_back&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;The converting iterator is (indirectly) derived from base class&lt;br /&gt;
&lt;code&gt;strcvt_iterator_base&amp;lt;charT&amp;gt;&lt;/code&gt; which does not depend on the source&lt;br /&gt;
iterator type or the source character type. It has a virtual&lt;br /&gt;
destructor and can be used polymorphically.  The inheritance hierarchy&lt;br /&gt;
looks like this:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;strcvt_iterator_base&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Operators *(), ++(), ==()&lt;/span&gt;
    &lt;span class="c1"&gt;// Member functions begin(), end()&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;source_charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;strcvt_iterator_impl&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;strcvt_iterator_base&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Implements operator ++() which does the conversion using&lt;/span&gt;
    &lt;span class="c1"&gt;// virtual member function get_next_source_char()&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;SourceIterator&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;strcvt_iterator&lt;/span&gt;
    &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;strcvt_iterator_impl&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;charT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;iterator_traits&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;SourceIterator&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nl"&gt;value_type&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Implements get_next_source_char()&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;Class template &lt;code&gt;strcvt_iterator_base&lt;/code&gt; does not know the source&lt;br /&gt;
character type and can be used to handle a &lt;code&gt;strcvt_iterator&lt;/code&gt;&lt;br /&gt;
polymorphically.  Class template &lt;code&gt;strcvt_iterator_impl&lt;/code&gt; knows the&lt;br /&gt;
source character type but not the type of the iterator.  If the&lt;br /&gt;
precompiled library is used, it is precompiled for all combinations of&lt;br /&gt;
source and destination character type.  Class template&lt;br /&gt;
&lt;code&gt;strcvt_iterator&lt;/code&gt; is the return value of function&lt;br /&gt;
make_strcvt_iterator().  It implements the source iterator handling.&lt;/p&gt;
&lt;h2 id="operators-wzxhzdk30wzxhzdk31"&gt;Operators &lt;a name="operators"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Some operators for strings are defined in header&lt;br /&gt;
"strcvt/strcvt_operators.h".  These are the output operators &lt;code&gt;&amp;lt;&amp;lt;&lt;/code&gt;&lt;br /&gt;
for strings and character pointers, and the appending &lt;code&gt;+=&lt;/code&gt; operators&lt;br /&gt;
for strings.  To use them, include header&lt;br /&gt;
"strcvt/strcvt_operators.h".  The operators reside in namespace&lt;br /&gt;
StrCvtOperators and should be imported into the user's namespace&lt;br /&gt;
through a &lt;code&gt;using namespace&lt;/code&gt; directive:&lt;/p&gt;
&lt;p&gt;~~~{.cpp}&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span class="cp"&gt;#include &amp;quot;strcvt/strcvt_operators.h&amp;quot;&lt;/span&gt;
&lt;span class="n"&gt;using&lt;/span&gt; &lt;span class="n"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;StrCvtOperators&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;U&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;Hello, world!&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;~~~&lt;/p&gt;
&lt;p&gt;It would be nice if we could define conversion operators to enable&lt;br /&gt;
assignment between different string types.  But in C++, conversion and&lt;br /&gt;
assignment operators can only be defined as member functions.&lt;/p&gt;
&lt;h2 id="creating-a-library-wzxhzdk32wzxhzdk33"&gt;Creating a Library &lt;a name="Library"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The character type converter is preconfigured for header-only use.&lt;br /&gt;
This means: Just include the header and you are done.  In order to&lt;br /&gt;
reduce space overhead and compilation time, a precompiled library can&lt;br /&gt;
be used.&lt;/p&gt;
&lt;p&gt;The main advantage in using a library is that each time the strcvt&lt;br /&gt;
headers are included, the compiler does not need to look at the&lt;br /&gt;
implementation details.  This can speed up compilation significantly.&lt;/p&gt;
&lt;p&gt;To create the library, the C++ source files libstrcvt.cpp,&lt;br /&gt;
libstrcvt_iterator.cpp, libstrcvt_utf.cpp and libcodecvt_utf.cpp in&lt;br /&gt;
directory &lt;code&gt;lib&lt;/code&gt; of the distribution must be compiled.  Under Linux,&lt;br /&gt;
just run &lt;code&gt;make&lt;/code&gt;.  Before compiling, you may want to select the&lt;br /&gt;
compiler to use: Uncomment to proper &lt;em&gt;CXX=&lt;/em&gt; - line in the toplevel&lt;br /&gt;
&lt;code&gt;Makefile.template&lt;/code&gt;.  Running &lt;code&gt;make&lt;/code&gt; should create a library&lt;br /&gt;
&lt;code&gt;lib/libxprintf.a&lt;/code&gt;, which has to be linked to the programs.&lt;/p&gt;
&lt;p&gt;In Visual C++, instead of building a library, you may just add the&lt;br /&gt;
library source files to your project.&lt;/p&gt;
&lt;p&gt;In order to make the headers use the library, you must open the header&lt;br /&gt;
&lt;code&gt;strcvt/strcvt_config.h&lt;/code&gt; with an editor and change the preprocessor&lt;br /&gt;
symbol &lt;code&gt;STRCVT_IMPL_USE_LIBRARY&lt;/code&gt; from 0 to 1.  The next time the&lt;br /&gt;
header is included, the library will be used.  You can check that the&lt;br /&gt;
library is used as intended by omitting the library when linking.&lt;br /&gt;
Linking should fail with missing externals.&lt;/p&gt;
&lt;p&gt;In order to run the tests, the headers for the &lt;em&gt;boost&lt;/em&gt; test framework&lt;br /&gt;
are required.&lt;/p&gt;
&lt;h2 id="compatibilitywzxhzdk34wzxhzdk35"&gt;Compatibility&lt;a name="Compatibility"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The code converter has been tested with:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;GCC 4.7, 4.8 and 4.9 on Linux&lt;/li&gt;
&lt;li&gt;Visual Studio Express 2013 for Windows Desktop with November 2013 CTP&lt;/li&gt;
&lt;li&gt;Intel C++ 14.0.2 on Linux&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="rationale-wzxhzdk36wzxhzdk37"&gt;Rationale &lt;a name="Rationale"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;C++11 has the new character types &lt;code&gt;char16_t&lt;/code&gt; and &lt;code&gt;char32_t&lt;/code&gt;, which use&lt;br /&gt;
the UTF-16 and UTF-32 Unicode encoding. The question is how to convert&lt;br /&gt;
between the four character types of C++.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;codecvt&lt;/code&gt; part of the C++11 library looks like some ruins left&lt;br /&gt;
over at the front line between warring factions.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;codecvt&lt;/code&gt; class template is part of the header \&amp;lt;locale&gt; and&lt;br /&gt;
described in section 22.4.1.4 of the standard.  The first thing to&lt;br /&gt;
note is that according to this specification &lt;code&gt;codecvt&lt;/code&gt; transforms&lt;br /&gt;
between an &lt;em&gt;internal&lt;/em&gt; and an &lt;em&gt;external&lt;/em&gt; character encoding, so it is&lt;br /&gt;
not intended to transform between internal character encodings like&lt;br /&gt;
&lt;code&gt;char16_t&lt;/code&gt; and &lt;code&gt;wchar_t&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;According to the standard, each locale shall have specializations of&lt;br /&gt;
&lt;code&gt;codecvt&lt;/code&gt; for tranformation between the internal character types&lt;br /&gt;
&lt;code&gt;char&lt;/code&gt;, &lt;code&gt;wchar_t&lt;/code&gt;, &lt;code&gt;char16_t&lt;/code&gt; and &lt;code&gt;char32_t&lt;/code&gt; and the external&lt;br /&gt;
character type &lt;code&gt;char&lt;/code&gt;.  The standard first says that these&lt;br /&gt;
specializations "convert the implementation-defined native character&lt;br /&gt;
set", only to continue specifying that&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;the specialization with external and internal character type both&lt;br /&gt;
&lt;code&gt;char&lt;/code&gt; must not convert at all (effectively saying that the internal&lt;br /&gt;
&lt;code&gt;char&lt;/code&gt; must have the same encoding as the external &lt;code&gt;char&lt;/code&gt;),&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;the specializations with internal character types &lt;code&gt;char16_t&lt;/code&gt; and&lt;br /&gt;
&lt;code&gt;char32_t&lt;/code&gt; must treat the external character code as UTF-8, so they&lt;br /&gt;
  are explicitly not allowed to treat it as an "implementation-defined&lt;br /&gt;
  native character set".&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This leaves the transformation between internal character type&lt;br /&gt;
&lt;code&gt;wchar_t&lt;/code&gt; and external character type &lt;code&gt;char&lt;/code&gt; as the only&lt;br /&gt;
locale-dependent transformation, and it is specified to convert&lt;br /&gt;
"between the native character sets for narrow and wide characters".&lt;br /&gt;
So no luck using &lt;code&gt;codecvt&lt;/code&gt; to transform between native character sets&lt;br /&gt;
and &lt;code&gt;char16_t&lt;/code&gt; or &lt;code&gt;char32_t&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Section 22.5 of the standard specifies the "standard code conversion&lt;br /&gt;
facets".  It contains:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Facet &lt;code&gt;codecvt_utf8&lt;/code&gt;, which converts between a UTF-8 coded &lt;code&gt;char&lt;/code&gt;&lt;br /&gt;
  buffer and UCS2 or UCS4, so it can be used to transform between&lt;br /&gt;
  UTF-8 and &lt;code&gt;char32_t&lt;/code&gt;. It is not usable for transformation between&lt;br /&gt;
  UTF-8 and &lt;code&gt;char16_t&lt;/code&gt; because &lt;code&gt;char16_t&lt;/code&gt; strings are coded as&lt;br /&gt;
  UTF-16, not UCS2.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Facet &lt;code&gt;codecvt_utf8_utf16&lt;/code&gt; converts between UTF-8 and UTF-16, so it&lt;br /&gt;
  can be used to transform between UTF-8-coded &lt;code&gt;char&lt;/code&gt; and &lt;code&gt;char16_t&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Facet &lt;code&gt;codecvt_utf16&lt;/code&gt; looks like a hack from the 20th century to&lt;br /&gt;
  adapt UTF-16 wide characters to character byte streams.  The&lt;br /&gt;
  UTF-16-coded buffer is addressed through a &lt;code&gt;char*&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So we can use these standard code conversion facets to convert between&lt;br /&gt;
UTF-8-coded &lt;code&gt;char&lt;/code&gt; and &lt;code&gt;char16_t&lt;/code&gt; or &lt;code&gt;char32_t&lt;/code&gt;.  We could even&lt;br /&gt;
transform between &lt;code&gt;char16_t&lt;/code&gt; and &lt;code&gt;char32_t&lt;/code&gt; by going through an&lt;br /&gt;
intermediate UTF-8-coded &lt;code&gt;char&lt;/code&gt; buffer.  But again, no&lt;br /&gt;
transformation between native character sets and &lt;code&gt;char16_t&lt;/code&gt; or&lt;br /&gt;
&lt;code&gt;char32_t&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The standard does provide an interface for transformation between&lt;br /&gt;
native &lt;code&gt;char&lt;/code&gt; and &lt;code&gt;char16_t&lt;/code&gt; or &lt;code&gt;char32_t&lt;/code&gt;.  It is hidden at the very&lt;br /&gt;
end of section 21.7 in the string library and consists of the four&lt;br /&gt;
functions mbrtoc16(), c16rtomb(), mbrtoc32() and c32rtomb().&lt;br /&gt;
Unfortunately, a locale can not be specified for these functions.&lt;br /&gt;
They always work with the currently active global locale.&lt;/p&gt;
&lt;p&gt;For the following things I know of no standard-conforming procedure:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Conversion between &lt;code&gt;wchar_t&lt;/code&gt; and &lt;code&gt;char16_t&lt;/code&gt; or &lt;code&gt;char32_t&lt;/code&gt;.  Clearly,&lt;br /&gt;
  going through native &lt;code&gt;char&lt;/code&gt; is not an option, unless it happens to&lt;br /&gt;
  use UTF-8 encoding.  I worked around this by assuming that the native&lt;br /&gt;
&lt;code&gt;wchar_t&lt;/code&gt; type uses Unicode coding and is equivalent to either&lt;br /&gt;
&lt;code&gt;char16_t&lt;/code&gt; or &lt;code&gt;char32_t&lt;/code&gt;, depending on &lt;code&gt;sizeof(wchar_t)&lt;/code&gt;.  This&lt;br /&gt;
  assumption holds for the systems I have access to, but there may be&lt;br /&gt;
  other &lt;code&gt;wchar_t&lt;/code&gt; encodings in use.  Older Windows systems used UCS2,&lt;br /&gt;
  where this assumption would not hold, but modern versions of Windows&lt;br /&gt;
  use UTF-16.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Locale-dependent conversion: especially for output streams where a&lt;br /&gt;
  locale is known that may not be the global locale, it would be&lt;br /&gt;
  useful if the locale could be passed as an argument to the character&lt;br /&gt;
  transformation.  Unfortunately the standard library does not provide&lt;br /&gt;
  a portable way to do this, so I left it out.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;First I thought I would reuse the standard-specified UTF coders, but&lt;br /&gt;
first they are incomplete (going from UTF-16 to UTF-32 via an&lt;br /&gt;
intermediate representation as UTF-8 is not very attractive), second&lt;br /&gt;
they are distributed over several different interfaces, and third GCC&lt;br /&gt;
does not have them.  So as a first step I implemented a transformation&lt;br /&gt;
between UTF-coded &lt;code&gt;char&lt;/code&gt;, &lt;code&gt;char16_t&lt;/code&gt;, &lt;code&gt;char32_t&lt;/code&gt; and &lt;code&gt;wchar_t&lt;/code&gt;.  The&lt;br /&gt;
function strcvt_utf(state, from, from_end, from_next, to, to_end,&lt;br /&gt;
to_next) uses the API of the member functions in() and out() of&lt;br /&gt;
&lt;code&gt;codecvt&lt;/code&gt;.  The &lt;code&gt;state&lt;/code&gt; holds the conversion state, &lt;code&gt;from&lt;/code&gt; and&lt;br /&gt;
&lt;code&gt;from_end&lt;/code&gt; delimit the source character buffer, &lt;code&gt;to&lt;/code&gt; and &lt;code&gt;to_end&lt;/code&gt;&lt;br /&gt;
delimit the destination character buffer, and on exit from the&lt;br /&gt;
function &lt;code&gt;from_next&lt;/code&gt; and &lt;code&gt;to_next&lt;/code&gt; point past the last converted&lt;br /&gt;
character.  Like in() and out(), strcvt_utf() returns&lt;br /&gt;
&lt;code&gt;std::codecvt_base::ok&lt;/code&gt; on success and &lt;code&gt;std::codecvt_base::partial&lt;/code&gt; if&lt;br /&gt;
the output buffer was too small to convert the entire input buffer, or&lt;br /&gt;
if the input buffer ended in a part of a multibyte sequence.  For each&lt;br /&gt;
invalid encoding in the input buffer, the replacement character&lt;br /&gt;
U+0xFFFD is inserted into the output stream.  If this is not wanted,&lt;br /&gt;
function strcvt_utf_strict() can be used, which has the same&lt;br /&gt;
interface as strcvt_utf() but returns &lt;code&gt;std::codecvt_base::error&lt;/code&gt; on&lt;br /&gt;
encoding errors, with &lt;code&gt;from_next&lt;/code&gt; pointing to the first element of the&lt;br /&gt;
invalid sequence.  Both functions are specialized on all combinations&lt;br /&gt;
of &lt;code&gt;char&lt;/code&gt;, &lt;code&gt;wchar_t&lt;/code&gt;, &lt;code&gt;char16_t&lt;/code&gt; and &lt;code&gt;char32_t&lt;/code&gt;.  They always convert&lt;br /&gt;
(even if input and output type is the same, in which case they check&lt;br /&gt;
the validity of the encoding).  They are able to consume and produce&lt;br /&gt;
single characters forming part of multibyte Unicode characters.&lt;/p&gt;
&lt;p&gt;Based on the UTF coder, a locale-dependent converter has been&lt;br /&gt;
implemented. The function strcvt(state, from, from_end, from_next,&lt;br /&gt;
to, to_end, to_next) also uses the API of the member functions in()&lt;br /&gt;
and out() of &lt;code&gt;codecvt&lt;/code&gt;:  The &lt;code&gt;state&lt;/code&gt; holds the conversion state,&lt;br /&gt;
&lt;code&gt;from&lt;/code&gt; and &lt;code&gt;from_end&lt;/code&gt; delimit the source character buffer, &lt;code&gt;to&lt;/code&gt; and&lt;br /&gt;
&lt;code&gt;to_end&lt;/code&gt; delimit the destination character buffer, and on exit from&lt;br /&gt;
the function &lt;code&gt;from_next&lt;/code&gt; and &lt;code&gt;to_next&lt;/code&gt; point past the last converted&lt;br /&gt;
character.  This implementation uses the assumption that &lt;code&gt;wchar_t&lt;/code&gt; is&lt;br /&gt;
encoded as either UTF-16 or UTF-32.  Transformations between wide&lt;br /&gt;
characters &lt;code&gt;wchar_t&lt;/code&gt;, &lt;code&gt;char16_t&lt;/code&gt; and &lt;code&gt;char32_t&lt;/code&gt; use UTF&lt;br /&gt;
transformations, transformations between &lt;code&gt;char&lt;/code&gt; and wide characters&lt;br /&gt;
treat &lt;code&gt;char&lt;/code&gt; as a native locale-defined character code.&lt;/p&gt;
&lt;p&gt;Convenience functions for the code transformation are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;basic_string strcvt(const C&lt;em&gt; from, const C&lt;/em&gt;&lt;br /&gt;
  from_end) converts the characters between &lt;code&gt;from&lt;/code&gt; and &lt;code&gt;from_end&lt;/code&gt; into&lt;br /&gt;
  a string of character type &lt;code&gt;charT&lt;/code&gt;, which is specified as a template&lt;br /&gt;
  parameter.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;basic_string strcvt(const C* from) converts the&lt;br /&gt;
  characters of the null-terminated string &lt;code&gt;from&lt;/code&gt; into a string of&lt;br /&gt;
  character type &lt;code&gt;charT&lt;/code&gt;, which is specified as a template parameter.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;basic_string strcvt(const std::basic_string&amp;amp;&lt;br /&gt;
  from) converts the characters of the string &lt;code&gt;from&lt;/code&gt; into a string of&lt;br /&gt;
  character type &lt;code&gt;charT&lt;/code&gt;, which is specified as a template parameter.&lt;br /&gt;
  A specialization for &lt;code&gt;charT = C&lt;/code&gt; and rvalue &lt;code&gt;from&lt;/code&gt; is provided, which&lt;br /&gt;
  moves the input string to the return value.  Note that this is the&lt;br /&gt;
  only conversion which does not check the validity of the encoding of&lt;br /&gt;
  the string.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;strcvt(const std::basic_string&amp;amp; from) (strcvt() without&lt;br /&gt;
  template parameter &lt;code&gt;charT&lt;/code&gt; specifying the destination character&lt;br /&gt;
  type) casts the type of the argument to a reference to a class&lt;br /&gt;
  derived from std::basic_string, which adds conversion operators&lt;br /&gt;
  to all string types.  The result can e.g. be assigned to any string&lt;br /&gt;
  type, or passed to a function expecting any string.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;strcvt(const C* from) (strcvt() without template parameter &lt;code&gt;charT&lt;/code&gt;&lt;br /&gt;
  specifying the destination character type) creates an object which&lt;br /&gt;
  can be converted back to a &lt;code&gt;const C*&lt;/code&gt; or to any string type.  The&lt;br /&gt;
  result can e.g. be assigned to any string type, or passed to a&lt;br /&gt;
  function expecting any string.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="license-wzxhzdk38wzxhzdk39"&gt;License &lt;a name="License"&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Copyright (c) 2014 Ruediger Helsch; All rights reserved&lt;/p&gt;
&lt;p&gt;Permission to use, copy, modify, and distribute this software for any&lt;br /&gt;
purpose and without fee is hereby granted.  The author disclaims all&lt;br /&gt;
warranties with regard to this software.&lt;/p&gt;&lt;/div&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Ruediger Helsch</dc:creator><pubDate>Sun, 29 Jun 2014 21:14:10 -0000</pubDate><guid>https://sourceforge.net7f3b90149fa4b9e0de710f7a7682b837b3709bda</guid></item></channel></rss>