Coverage Report

Created: 2026-09-14 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/tomlplusplus/include/toml++/impl/unicode.hpp
Line
Count
Source
1
//# This file is a part of toml++ and is subject to the the terms of the MIT license.
2
//# Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
3
//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
4
// SPDX-License-Identifier: MIT
5
#pragma once
6
7
#include "unicode_autogenerated.hpp"
8
#include "header_start.hpp"
9
/// \cond
10
11
TOML_IMPL_NAMESPACE_START
12
{
13
  TOML_CONST_GETTER
14
  constexpr bool is_string_delimiter(char32_t c) noexcept
15
64.0k
  {
16
64.0k
    return c == U'"' || c == U'\'';
17
64.0k
  }
18
19
  TOML_CONST_GETTER
20
  constexpr bool is_ascii_letter(char32_t c) noexcept
21
67.5k
  {
22
67.5k
    return (c >= U'a' && c <= U'z') || (c >= U'A' && c <= U'Z');
23
67.5k
  }
24
25
  TOML_CONST_GETTER
26
  constexpr bool is_binary_digit(char32_t c) noexcept
27
9.71k
  {
28
9.71k
    return c == U'0' || c == U'1';
29
9.71k
  }
30
31
  TOML_CONST_GETTER
32
  constexpr bool is_octal_digit(char32_t c) noexcept
33
34.8k
  {
34
34.8k
    return (c >= U'0' && c <= U'7');
35
34.8k
  }
36
37
  TOML_CONST_GETTER
38
  constexpr bool is_decimal_digit(char32_t c) noexcept
39
2.14M
  {
40
2.14M
    return (c >= U'0' && c <= U'9');
41
2.14M
  }
42
43
  TOML_CONST_GETTER
44
  constexpr bool is_hexadecimal_digit(char32_t c) noexcept
45
27.5k
  {
46
27.5k
    return U'0' <= c && c <= U'f' && (1ull << (static_cast<uint_least64_t>(c) - 0x30u)) & 0x7E0000007E03FFull;
47
27.5k
  }
48
49
  template <typename T>
50
  TOML_CONST_GETTER
51
  constexpr uint_least32_t hex_to_dec(const T c) noexcept
52
22.2k
  {
53
    if constexpr (std::is_same_v<remove_cvref<T>, uint_least32_t>)
54
11.1k
      return c >= 0x41u           // >= 'A'
55
11.1k
           ? 10u + (c | 0x20u) - 0x61u // - 'a'
56
11.1k
           : c - 0x30u         // - '0'
57
        ;
58
    else
59
11.1k
      return hex_to_dec(static_cast<uint_least32_t>(c));
60
22.2k
  }
unsigned int toml::v3::impl::hex_to_dec<toml::v3::impl::utf8_codepoint>(toml::v3::impl::utf8_codepoint)
Line
Count
Source
52
6.30k
  {
53
    if constexpr (std::is_same_v<remove_cvref<T>, uint_least32_t>)
54
      return c >= 0x41u          // >= 'A'
55
           ? 10u + (c | 0x20u) - 0x61u // - 'a'
56
           : c - 0x30u         // - '0'
57
        ;
58
    else
59
6.30k
      return hex_to_dec(static_cast<uint_least32_t>(c));
60
6.30k
  }
unsigned int toml::v3::impl::hex_to_dec<unsigned int>(unsigned int)
Line
Count
Source
52
11.1k
  {
53
    if constexpr (std::is_same_v<remove_cvref<T>, uint_least32_t>)
54
11.1k
      return c >= 0x41u           // >= 'A'
55
11.1k
           ? 10u + (c | 0x20u) - 0x61u // - 'a'
56
11.1k
           : c - 0x30u         // - '0'
57
        ;
58
    else
59
      return hex_to_dec(static_cast<uint_least32_t>(c));
60
11.1k
  }
unsigned int toml::v3::impl::hex_to_dec<char>(char)
Line
Count
Source
52
4.83k
  {
53
    if constexpr (std::is_same_v<remove_cvref<T>, uint_least32_t>)
54
      return c >= 0x41u          // >= 'A'
55
           ? 10u + (c | 0x20u) - 0x61u // - 'a'
56
           : c - 0x30u         // - '0'
57
        ;
58
    else
59
4.83k
      return hex_to_dec(static_cast<uint_least32_t>(c));
60
4.83k
  }
61
62
  TOML_CONST_GETTER
63
  constexpr bool is_horizontal_whitespace(char32_t c) noexcept
64
23.0M
  {
65
23.0M
    return is_ascii_horizontal_whitespace(c) || is_non_ascii_horizontal_whitespace(c);
66
23.0M
  }
67
68
  TOML_CONST_GETTER
69
  constexpr bool is_vertical_whitespace(char32_t c) noexcept
70
17.9M
  {
71
17.9M
    return is_ascii_vertical_whitespace(c) || is_non_ascii_vertical_whitespace(c);
72
17.9M
  }
73
74
  TOML_CONST_GETTER
75
  constexpr bool is_whitespace(char32_t c) noexcept
76
17.9M
  {
77
17.9M
    return is_horizontal_whitespace(c) || is_vertical_whitespace(c);
78
17.9M
  }
79
80
  TOML_CONST_GETTER
81
  constexpr bool is_bare_key_character(char32_t c) noexcept
82
20.1M
  {
83
20.1M
    return is_ascii_bare_key_character(c)
84
#if TOML_LANG_UNRELEASED // toml/pull/891 (unicode bare keys)
85
      || is_non_ascii_bare_key_character(c)
86
#endif
87
20.1M
      ;
88
20.1M
  }
89
90
  TOML_CONST_GETTER
91
  constexpr bool is_value_terminator(char32_t c) noexcept
92
2.70M
  {
93
2.70M
    return is_whitespace(c) || c == U']' || c == U'}' || c == U',' || c == U'#';
94
2.70M
  }
95
96
  TOML_CONST_GETTER
97
  constexpr bool is_control_character(char c) noexcept
98
0
  {
99
0
    return c <= '\u001F' || c == '\u007F';
100
0
  }
101
102
  TOML_CONST_GETTER
103
  constexpr bool is_control_character(char32_t c) noexcept
104
1.85M
  {
105
1.85M
    return c <= U'\u001F' || c == U'\u007F';
106
1.85M
  }
107
108
  TOML_CONST_GETTER
109
  constexpr bool is_nontab_control_character(char32_t c) noexcept
110
32.6M
  {
111
32.6M
    return c <= U'\u0008' || (c >= U'\u000A' && c <= U'\u001F') || c == U'\u007F';
112
32.6M
  }
113
114
  TOML_CONST_GETTER
115
  constexpr bool is_unicode_surrogate(char32_t c) noexcept
116
32.6M
  {
117
32.6M
    return c >= 0xD800u && c <= 0xDFFF;
118
32.6M
  }
119
120
  struct utf8_decoder
121
  {
122
    // utf8_decoder based on this: https://bjoern.hoehrmann.de/utf-8/decoder/dfa/
123
    // Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
124
125
    uint_least32_t state{};
126
    char32_t codepoint{};
127
128
    static constexpr uint8_t state_table[]{
129
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
130
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
131
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
132
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
133
      0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  1,  1,  1,
134
      1,  1,  1,  1,  1,  1,  1,  1,  1,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  9,  7,  7,
135
      7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,  7,
136
      7,  7,  7,  8,  8,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,  2,
137
      2,  2,  2,  2,  2,  2,  2,  2,  10, 3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  4,  3,  3,  11, 6,  6,
138
      6,  5,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,
139
140
      0,  12, 24, 36, 60, 96, 84, 12, 12, 12, 48, 72, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 0,  12,
141
      12, 12, 12, 12, 0,  12, 0,  12, 12, 12, 24, 12, 12, 12, 12, 12, 24, 12, 24, 12, 12, 12, 12, 12, 12, 12, 12,
142
      12, 24, 12, 12, 12, 12, 12, 24, 12, 12, 12, 12, 12, 12, 12, 24, 12, 12, 12, 12, 12, 12, 12, 12, 12, 36, 12,
143
      36, 12, 12, 12, 36, 12, 12, 12, 12, 12, 36, 12, 36, 12, 12, 12, 36, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12
144
    };
145
146
    TOML_PURE_INLINE_GETTER
147
    constexpr bool error() const noexcept
148
771k
    {
149
771k
      return state == uint_least32_t{ 12u };
150
771k
    }
151
152
    TOML_PURE_INLINE_GETTER
153
    constexpr bool has_code_point() const noexcept
154
2.44M
    {
155
2.44M
      return state == uint_least32_t{};
156
2.44M
    }
157
158
    TOML_PURE_INLINE_GETTER
159
    constexpr bool needs_more_input() const noexcept
160
1.67M
    {
161
1.67M
      return !has_code_point() && !error();
162
1.67M
    }
163
164
    constexpr void operator()(uint8_t byte) noexcept
165
384k
    {
166
384k
      TOML_ASSERT_ASSUME(!error());
167
168
384k
      const auto type = state_table[byte];
169
170
384k
      codepoint = static_cast<char32_t>(has_code_point() ? (uint_least32_t{ 255u } >> type) & byte
171
384k
                                 : (byte & uint_least32_t{ 63u })
172
40.1k
                                   | (static_cast<uint_least32_t>(codepoint) << 6));
173
174
384k
      state = state_table[state + uint_least32_t{ 256u } + type];
175
384k
    }
176
177
    TOML_ALWAYS_INLINE
178
    constexpr void operator()(char c) noexcept
179
0
    {
180
0
      operator()(static_cast<uint8_t>(c));
181
0
    }
182
183
    TOML_ALWAYS_INLINE
184
    constexpr void reset() noexcept
185
1.64M
    {
186
1.64M
      state = {};
187
1.64M
    }
188
  };
189
190
  TOML_PURE_GETTER
191
  TOML_ATTR(nonnull)
192
  bool is_ascii(const char* str, size_t len) noexcept;
193
}
194
TOML_IMPL_NAMESPACE_END;
195
196
/// \endcond
197
#include "header_end.hpp"