cppx-core
utf8-from.hpp
Go to the documentation of this file.
1 #pragma once // Source encoding: UTF-8 with BOM (π is a lowercase Greek "pi").
2 #include <cppx-core/meta-template/Enable_if_.hpp> // cppx::Enable_if_
3 #include <cppx-core/meta-type/type-traits.hpp> // cppx::(is_a_byte_char_type_)
4 #include <cppx-core/text/unicode/utf8-Generator.hpp> // cppx::utf8::Generator
5 #include <cppx-core/text/unicode/utf8-n_bytes_for.hpp> // cppx::utf8::n_bytes_for
6 
7 namespace cppx::utf8
8 {
9  CPPX_USE_STD( basic_string, basic_string_view, iterator_traits, move, next, string, string_view );
10 
11  template< class Char >
12  inline auto from_string_view( const basic_string_view<Char>& ws )
13  -> string
14  {
15  if( is_empty( ws ) ) { return ""; }
16 
17  // Assume UTF-8 encoding for byte string, UTF-16 (or UTF-32) for larger units.
18  if constexpr( is_a_byte_char_type_<Char> )
19  {
20  return string( ws.begin(), ws.end() );
21  }
22  else
23  {
24  string result( n_bytes_for( ws ), '\0' );
25  Generator().utf8_from_codes( CPPX_ITEMS_OF( ws ), result.begin() );
26  return result;
27  }
28  }
29 
30  template< class Char >
31  inline auto from( const P_<const Char> s )
32  -> string
33  { return from_string_view<Char>( s ); }
34 
35  template< class Char >
36  inline auto from( const basic_string<Char>& s )
37  -> string
38  { return from_string_view<Char>( s ); }
39 
40  inline auto from( string s )
41  -> string
42  { return move( s ); }
43 
44  template< class Char >
45  inline auto from( const basic_string_view<Char>& s )
46  -> string
47  { return from_string_view<Char>( s ); }
48 
49 } // namespace cppx::utf8
Some_type * P_
Creates a raw pointer type.
CPPX_USE_STD(basic_string, basic_string_view, iterator_traits, move, next, string, string_view)
auto utf8_from_codes(const Span_< In_iterator > range, const Out_iterator destination) -> Out_iterator
auto is_empty(const Collection &c) -> Truth
Definition: is_empty.hpp:33
auto next(P_< const char > p) -> P_< const char >
auto from(const P_< const Char > s) -> string
Definition: utf8-from.hpp:31
#define CPPX_ITEMS_OF(c)
$items_of(c) effectively expands to std::begin(c), std::end(c).
auto from_string_view(const basic_string_view< Char > &ws) -> string
Definition: utf8-from.hpp:12
auto n_bytes_for(const basic_string_view< Char > &sv) noexcept -> Size