cppx-core
utf16-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_same_type_)
4 #include <cppx-core/text/unicode/utf16-Generator.hpp> // cppx::utf16::Generator
5 #include <cppx-core/text/unicode/utf16-n_units_for.hpp> // cppx::utf16::n_units_for
6 
7 namespace cppx::utf16
8 {
10  basic_string, basic_string_view, string, string_view, u16string, wstring,
11  iterator_traits, move, next
12  );
13 
14  template<
15  class Char,
16  class Result_char = char16_t,
18  >
19  inline auto from_string_view( const basic_string_view<Char>& sv )
20  -> basic_string<Result_char>
21  {
22  if( is_empty( sv ) ) { return {}; }
23 
24  // Assume UTF-8 encoding for byte string, UTF-16 (or UTF-32) for larger units.
25  if constexpr( sizeof( Char ) > 1 )
26  {
27  static_assert( magnitude_bits_per_<Char> == 16 ); // TODO: Support UTF-32 input.
28  return string( sv.begin(), sv.end() );
29  }
30  else
31  {
32  basic_string<Result_char> result( n_units_for( sv ), Result_char() );
33  Generator().utf16_from_bytes( CPPX_ITEMS_OF( sv ), result.begin() );
34  return result;
35  }
36  }
37 
38  template< class Char >
39  inline auto from( const P_<const Char> s )
40  -> u16string
41  { return from_string_view<Char>( s ); }
42 
43  template< class Char >
44  inline auto from( const basic_string<Char>& s )
45  -> u16string
46  { return from_string_view<Char>( s ); }
47 
48  inline auto from( u16string s )
49  -> u16string
50  { return move( s ); }
51 
52  template< class Char >
53  inline auto from( const basic_string_view<Char>& s )
54  -> u16string
55  { return from_string_view<Char>( s ); }
56 
57  template< class Char >
58  inline auto wide_from( const P_<const Char> s )
59  -> wstring
60  { return from_string_view<Char, wchar_t>( s ); }
61 
62  template< class Char >
63  inline auto wide_from( const basic_string<Char>& s )
64  -> wstring
65  { return from_string_view<Char, wchar_t>( s ); }
66 
67  //inline auto wide_from( wstring s )
68  // -> wstring
69  //{ return move( s ); }
70 
71  template< class Char >
72  inline auto wide_from( const basic_string_view<Char>& s )
73  -> wstring
74  { return from_string_view<Char, wchar_t>( s ); }
75 } // namespace cppx::utf16
auto n_units_for(const basic_string_view< Char > &sv) noexcept -> Size
Some_type * P_
Creates a raw pointer type.
auto wide_from(const P_< const Char > s) -> wstring
Definition: utf16-from.hpp:58
auto from_string_view(const basic_string_view< Char > &sv) -> basic_string< Result_char >
Definition: utf16-from.hpp:19
auto is_empty(const Collection &c) -> Truth
Definition: is_empty.hpp:33
auto next(P_< const char > p) -> P_< const char >
CPPX_USE_STD(basic_string, basic_string_view, string, string_view, u16string, wstring, iterator_traits, move, next)
#define CPPX_ITEMS_OF(c)
$items_of(c) effectively expands to std::begin(c), std::end(c).
auto utf16_from_bytes(const Span_< In_iterator > bytes_range, const Out_iterator destination) -> Out_iterator
std::enable_if_t< condition, Result > Enable_if_
Just more readable than enable_if_t.
auto from(const P_< const Char > s) -> u16string
Definition: utf16-from.hpp:39