HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
ascii.hpp
Go to the documentation of this file.
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
9#pragma once
10
11#include "char_converter.hpp"
12#include "../utility/utility.hpp"
13#include "../macros.hpp"
14#include <cstdint>
15#include <utility>
16#include <bit>
17#include <compare>
18#if defined(HI_HAS_SSE2)
19#include <emmintrin.h>
20#endif
21
22hi_export_module(hikogui.char_maps.ascii);
23
24hi_export namespace hi { inline namespace v1 {
25
29template<>
30struct char_map<"ascii"> {
31 using char_type = char;
32
33 [[nodiscard]] constexpr std::endian guess_endian(void const *ptr, size_t size, std::endian endian) const noexcept
34 {
35 return std::endian::native;
36 }
37
38 template<typename It, typename EndIt>
39 [[nodiscard]] constexpr std::pair<char32_t, bool> read(It& it, EndIt last) const noexcept
40 {
41 hi_axiom(it != last);
42
43 auto const c = char_cast<char32_t>(*it++);
44 if (c < 0x80) {
45 return {c, true};
46 } else {
47 return {0xfffd, false};
48 }
49 }
50
51 [[nodiscard]] constexpr std::pair<uint8_t, bool> size(char32_t code_point) const noexcept
52 {
53 hi_axiom(code_point < 0x11'0000);
54 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
55
56 if (code_point < 0x80) {
57 return {uint8_t{1}, true};
58
59 } else {
60 return {uint8_t{1}, false};
61 }
62 }
63
64 template<typename It>
65 constexpr void write(char32_t code_point, It& dst) const noexcept
66 {
67 hi_axiom(code_point < 0x11'0000);
68 hi_axiom(not(code_point >= 0xd800 and code_point < 0xe000));
69
70 if (code_point < 0x80) {
71 *dst++ = char_cast<char>(code_point);
72
73 } else {
74 *dst++ = '?';
75 }
76 }
77
78#if defined(HI_HAS_SSE2)
79 template<typename It>
80 hi_force_inline __m128i read_ascii_chunk16(It it) const noexcept
81 {
82 return _mm_loadu_si128(reinterpret_cast<__m128i const *>(std::addressof(*it)));
83 }
84
85 template<typename It>
86 hi_force_inline void write_ascii_chunk16(__m128i chunk, It dst) const noexcept
87 {
88 _mm_storeu_si128(reinterpret_cast<__m128i *>(std::addressof(*dst)), chunk);
89 }
90#endif
91};
92
93}} // namespace hi::v1
Definition of the char_converter<From,To> functor.
@ read
Allow read access to a file.
@ write
Allow write access to a file.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Character encoder/decoder template.
Definition char_converter.hpp:89
T addressof(T... args)