cppx-core
utf8-n_bytes_for.hpp
Go to the documentation of this file.
1 #pragma once // Source encoding: UTF-8 with BOM (π is a lowercase Greek "pi").
2 
5 
6 namespace cppx::utf8
7 {
8  CPPX_USE_STD( basic_string, basic_string_view, iterator_traits, move, next, string );
9 
11  namespace impl
12  {
13  using Bytes_count_iterator = Count_iterator_<char>;
14 
15  // This overload is an optimization and can be removed with no ill effects
16  // except execution time.
17  template< class Value >
18  auto output_utf8( const uint32_t code, const Bytes_count_iterator initial_count )
19  -> Bytes_count_iterator
20  {
21  Bytes_count_iterator counter = initial_count;
22  if( code <= 0x7F ) { ++counter; }
23  else if( code <= 0x7FF ) { counter += 2; }
24  else if( code <= 0xFFFF ) { counter += 3; }
25  else { counter += 4; }
26 
27  return counter;
28  }
29  } // namespace impl
31 
32  template< class Char >
33  inline auto n_bytes_for( const basic_string_view<Char>& sv ) noexcept
34  -> Size
35  {
36  impl::Bytes_count_iterator it;
37  it = Generator().utf8_from_codes( CPPX_ITEMS_OF( sv ), it );
38  return it.count();
39  }
40 
41  // Optimization.
42  inline auto n_bytes_for( const basic_string_view<char>& sv ) noexcept
43  -> Size
44  { return sv.size(); }
45 
46  // Callability.
47  template< class Char >
48  inline auto n_bytes_for( const P_<Char> s ) noexcept
49  -> Size
50  { return n_bytes_for( basic_string_view<Char>( s ) ); }
51 
52  template< class Char >
53  inline auto n_bytes_for( const basic_string<Char>& s ) noexcept
54  -> Size
55  { return n_bytes_for( basic_string_view<Char>( s ) ); }
56 } // 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 next(P_< const char > p) -> P_< const char >
Signed_< size_t > Size
A Signed_ equivalent of size_t.
#define CPPX_ITEMS_OF(c)
$items_of(c) effectively expands to std::begin(c), std::end(c).
auto n_bytes_for(const basic_string_view< Char > &sv) noexcept -> Size