Coverage Report

Created: 2025-06-13 06:46

/src/Fast-DDS/thirdparty/taocpp-pegtl/pegtl/internal/peek_utf8.hpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2014-2020 Dr. Colin Hirsch and Daniel Frey
2
// Please see LICENSE for license or visit https://github.com/taocpp/PEGTL/
3
4
#ifndef TAO_PEGTL_INTERNAL_PEEK_UTF8_HPP
5
#define TAO_PEGTL_INTERNAL_PEEK_UTF8_HPP
6
7
#include "../config.hpp"
8
9
#include "input_pair.hpp"
10
11
namespace tao
12
{
13
   namespace TAO_PEGTL_NAMESPACE
14
   {
15
      namespace internal
16
      {
17
         struct peek_utf8
18
         {
19
            using data_t = char32_t;
20
            using pair_t = input_pair< char32_t >;
21
22
            template< typename Input >
23
            static pair_t peek( Input& in ) noexcept( noexcept( in.empty() ) )
24
0
            {
25
0
               if( in.empty() ) {
26
0
                  return { 0, 0 };
27
0
               }
28
0
               char32_t c0 = in.peek_uint8();
29
0
               if( ( c0 & 0x80 ) == 0 ) {
30
0
                  return { c0, 1 };
31
0
               }
32
0
               return peek_impl( in, c0 );
33
0
            }
34
35
         private:
36
            template< typename Input >
37
            static pair_t peek_impl( Input& in, char32_t c0 ) noexcept( noexcept( in.size( 4 ) ) )
38
0
            {
39
0
               if( ( c0 & 0xE0 ) == 0xC0 ) {
40
0
                  if( in.size( 2 ) >= 2 ) {
41
0
                     const char32_t c1 = in.peek_uint8( 1 );
42
0
                     if( ( c1 & 0xC0 ) == 0x80 ) {
43
0
                        c0 &= 0x1F;
44
0
                        c0 <<= 6;
45
0
                        c0 |= ( c1 & 0x3F );
46
0
                        if( c0 >= 0x80 ) {
47
0
                           return { c0, 2 };
48
0
                        }
49
0
                     }
50
0
                  }
51
0
               }
52
0
               else if( ( c0 & 0xF0 ) == 0xE0 ) {
53
0
                  if( in.size( 3 ) >= 3 ) {
54
0
                     const char32_t c1 = in.peek_uint8( 1 );
55
0
                     const char32_t c2 = in.peek_uint8( 2 );
56
0
                     if( ( ( c1 & 0xC0 ) == 0x80 ) && ( ( c2 & 0xC0 ) == 0x80 ) ) {
57
0
                        c0 &= 0x0F;
58
0
                        c0 <<= 6;
59
0
                        c0 |= ( c1 & 0x3F );
60
0
                        c0 <<= 6;
61
0
                        c0 |= ( c2 & 0x3F );
62
0
                        if( c0 >= 0x800 && !( c0 >= 0xD800 && c0 <= 0xDFFF ) ) {
63
0
                           return { c0, 3 };
64
0
                        }
65
0
                     }
66
0
                  }
67
0
               }
68
0
               else if( ( c0 & 0xF8 ) == 0xF0 ) {
69
0
                  if( in.size( 4 ) >= 4 ) {
70
0
                     const char32_t c1 = in.peek_uint8( 1 );
71
0
                     const char32_t c2 = in.peek_uint8( 2 );
72
0
                     const char32_t c3 = in.peek_uint8( 3 );
73
0
                     if( ( ( c1 & 0xC0 ) == 0x80 ) && ( ( c2 & 0xC0 ) == 0x80 ) && ( ( c3 & 0xC0 ) == 0x80 ) ) {
74
0
                        c0 &= 0x07;
75
0
                        c0 <<= 6;
76
0
                        c0 |= ( c1 & 0x3F );
77
0
                        c0 <<= 6;
78
0
                        c0 |= ( c2 & 0x3F );
79
0
                        c0 <<= 6;
80
0
                        c0 |= ( c3 & 0x3F );
81
0
                        if( c0 >= 0x10000 && c0 <= 0x10FFFF ) {
82
0
                           return { c0, 4 };
83
0
                        }
84
0
                     }
85
0
                  }
86
0
               }
87
0
               return { 0, 0 };
88
0
            }
89
         };
90
91
      }  // namespace internal
92
93
   }  // namespace TAO_PEGTL_NAMESPACE
94
95
}  // namespace tao
96
97
#endif