HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
ascii.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "char_converter.hpp"
8#include "../cast.hpp"
9#include "../required.hpp"
10#include "../architecture.hpp"
11#include <cstdint>
12#include <utility>
13#include <tuple>
14
15namespace hi::inline v1 {
16
17template<>
18struct char_map<"ascii"> {
19 using char_type = char;
20
21 [[nodiscard]] constexpr std::endian guess_endian(void const *ptr, size_t size, std::endian endian) const noexcept
22 {
23 return std::endian::native;
24 }
25
26 template<typename It, typename EndIt>
27 [[nodiscard]] constexpr std::pair<char32_t, bool> read(It& it, EndIt last) const noexcept
28 {
29 hi_axiom(it != last);
30
31 hilet c = char_cast<char32_t>(*it++);
32 if (c < 0x80) {
33 return {c, true};
34 } else {
35 return {0xfffd, false};
36 }
37 }
38
39 [[nodiscard]] constexpr std::pair<uint8_t, bool> size(char32_t code_point) const noexcept
40 {
41 hi_axiom(code_point < 0x11'0000);
42 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
43
44 if (code_point < 0x80) {
45 return {uint8_t{1}, true};
46
47 } else {
48 return {uint8_t{1}, false};
49 }
50 }
51
52 template<typename It>
53 constexpr void write(char32_t code_point, It& dst) const noexcept
54 {
55 hi_axiom(code_point < 0x11'0000);
56 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
57
58 if (code_point < 0x80) {
59 *dst++ = char_cast<char>(code_point);
60
61 } else {
62 *dst++ = '?';
63 }
64 }
65
66#if defined(HI_HAS_SSE2)
67 template<typename It>
68 hi_force_inline __m128i read_ascii_chunk16(It it) const noexcept
69 {
70 return _mm_loadu_si128(reinterpret_cast<__m128i const *>(std::addressof(*it)));
71 }
72
73 template<typename It>
74 hi_force_inline void write_ascii_chunk16(__m128i chunk, It dst) const noexcept
75 {
76 _mm_storeu_si128(reinterpret_cast<__m128i *>(std::addressof(*dst)), chunk);
77 }
78#endif
79};
80
81} // namespace hi::inline v1
This file includes required definitions.
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
Functions and macros for handling architectural difference between compilers, CPUs and operating syst...
Character encoder/decoder template.
Definition char_converter.hpp:34
T addressof(T... args)