Coverage Report

Created: 2025-07-11 06:31

/src/tomlplusplus/include/toml++/impl/unicode.inl
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
//# {{
8
#include "preprocessor.hpp"
9
#if !TOML_IMPLEMENTATION
10
#error This is an implementation-only header.
11
#endif
12
//# }}
13
14
#include "unicode.hpp"
15
#include "simd.hpp"
16
#include "header_start.hpp"
17
18
TOML_IMPL_NAMESPACE_START
19
{
20
  TOML_PURE_GETTER
21
  TOML_EXTERNAL_LINKAGE
22
  bool is_ascii(const char* str, size_t len) noexcept
23
1.53M
  {
24
1.53M
    const char* const end = str + len;
25
26
1.53M
#if TOML_HAS_SSE2 && (128 % CHAR_BIT) == 0
27
1.53M
    {
28
1.53M
      constexpr size_t chars_per_vector = 128u / CHAR_BIT;
29
30
1.53M
      if (const size_t simdable = len - (len % chars_per_vector))
31
1.52M
      {
32
1.52M
        __m128i mask = _mm_setzero_si128();
33
4.58M
        for (const char* const e = str + simdable; str < e; str += chars_per_vector)
34
3.05M
        {
35
3.05M
          const __m128i current_bytes = _mm_loadu_si128(reinterpret_cast<const __m128i*>(str));
36
3.05M
          mask            = _mm_or_si128(mask, current_bytes);
37
3.05M
        }
38
1.52M
        const __m128i has_error = _mm_cmpgt_epi8(_mm_setzero_si128(), mask);
39
40
#if TOML_HAS_SSE4_1
41
        if (!_mm_testz_si128(has_error, has_error))
42
          return false;
43
#else
44
1.52M
        if (_mm_movemask_epi8(_mm_cmpeq_epi8(has_error, _mm_setzero_si128())) != 0xFFFF)
45
19.6k
          return false;
46
1.52M
#endif
47
1.52M
      }
48
1.53M
    }
49
1.51M
#endif
50
51
1.55M
    for (; str < end; str++)
52
39.5k
      if (static_cast<unsigned char>(*str) > 127u)
53
1.24k
        return false;
54
55
1.51M
    return true;
56
1.51M
  }
57
}
58
TOML_IMPL_NAMESPACE_END;
59
60
#include "header_end.hpp"