summaryrefslogtreecommitdiffhomepage
path: root/vendor/toml++/impl/unicode.inl
blob: 93a7d5258de26f6ab5f3df800241b3b3a92490a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// # This file is a part of toml++ and is subject to the the terms of the MIT license.
// # Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
// # See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
//  SPDX-License-Identifier: MIT
#pragma once

// # {{
#include "preprocessor.hpp"
#if !TOML_IMPLEMENTATION
#error This is an implementation-only header.
#endif
// # }}

#include "header_start.hpp"
#include "simd.hpp"
#include "unicode.hpp"

TOML_IMPL_NAMESPACE_START {
  TOML_PURE_GETTER
  TOML_EXTERNAL_LINKAGE
  bool is_ascii(const char* str, size_t len) noexcept {
    const char* const end = str + len;

#if TOML_HAS_SSE2 && (128 % CHAR_BIT) == 0
    {
      constexpr size_t chars_per_vector = 128u / CHAR_BIT;

      if (const size_t simdable = len - (len % chars_per_vector)) {
        __m128i mask = _mm_setzero_si128();
        for (const char* const e = str + simdable; str < e; str += chars_per_vector) {
          const __m128i current_bytes = _mm_loadu_si128(reinterpret_cast<const __m128i*>(str));
          mask                        = _mm_or_si128(mask, current_bytes);
        }
        const __m128i has_error = _mm_cmpgt_epi8(_mm_setzero_si128(), mask);

#if TOML_HAS_SSE4_1
        if (!_mm_testz_si128(has_error, has_error)) return false;
#else
        if (_mm_movemask_epi8(_mm_cmpeq_epi8(has_error, _mm_setzero_si128())) != 0xFFFF)
          return false;
#endif
      }
    }
#endif

    for (; str < end; str++)
      if (static_cast<unsigned char>(*str) > 127u) return false;

    return true;
  }
}
TOML_IMPL_NAMESPACE_END;

#include "header_end.hpp"