Debian Codegraph @

flang/runtime/utf.cpp

raw
1 //===-- runtime/utf.cpp ---------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "utf.h"
10
11 namespace Fortran::runtime {
12
13 #ifndef FLANG_RUNTIME_NO_GLOBAL_VAR_DEFS
14 // clang-format off
15 RT_OFFLOAD_VAR_GROUP_BEGIN
16 const RT_CONST_VAR_ATTRS std::uint8_t UTF8FirstByteTable[256]{
17 /* 00 - 7F: 7 bit payload in single byte */
18 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
19 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
20 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
21 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
22 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
23 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
24 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
25 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
26 /* 80 - BF: invalid first byte, valid later byte */
27 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31 /* C0 - DF: 11 bit payload */
32 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
33 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
34 /* E0 - EF: 16 bit payload */
35 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
36 /* F0 - F7: 21 bit payload */ 4, 4, 4, 4, 4, 4, 4, 4,
37 /* F8 - FB: 26 bit payload */ 5, 5, 5, 5,
38 /* FC - FD: 31 bit payload */ 6, 6,
39 /* FE: 32 bit payload */ 7,
40 /* FF: invalid */ 0
41 };
42 RT_OFFLOAD_VAR_GROUP_END
43 // clang-format on
44 #endif // FLANG_RUNTIME_NO_GLOBAL_VAR_DEFS
45
46 RT_OFFLOAD_API_GROUP_BEGIN
47 // Non-minimal encodings are accepted.
48 Fortran::common::optional<char32_t> DecodeUTF8(const char *p0) {
49 const std::uint8_t *p{reinterpret_cast<const std::uint8_t *>(p0)};
50 std::size_t bytes{MeasureUTF8Bytes(*p0)};
51 if (bytes == 1) {
52 return char32_t{*p};
53 } else if (bytes > 1) {
54 std::uint64_t result{char32_t{*p} & (0x7f >> bytes)};
55 for (std::size_t j{1}; j < bytes; ++j) {
56 std::uint8_t next{p[j]};
57 if (next < 0x80 || next > 0xbf) {
58 return Fortran::common::nullopt;
59 }
60 result = (result << 6) | (next & 0x3f);
61 }
62 if (result <= 0xffffffff) {
63 return static_cast<char32_t>(result);
64 }
65 }
66 return Fortran::common::nullopt;
67 }
68
69 std::size_t EncodeUTF8(char *p0, char32_t ucs) {
70 std::uint8_t *p{reinterpret_cast<std::uint8_t *>(p0)};
71 if (ucs <= 0x7f) {
72 p[0] = ucs;
73 return 1;
74 } else if (ucs <= 0x7ff) {
75 p[0] = 0xc0 | (ucs >> 6);
76 p[1] = 0x80 | (ucs & 0x3f);
77 return 2;
78 } else if (ucs <= 0xffff) {
79 p[0] = 0xe0 | (ucs >> 12);
80 p[1] = 0x80 | ((ucs >> 6) & 0x3f);
81 p[2] = 0x80 | (ucs & 0x3f);
82 return 3;
83 } else if (ucs <= 0x1fffff) {
84 p[0] = 0xf0 | (ucs >> 18);
85 p[1] = 0x80 | ((ucs >> 12) & 0x3f);
86 p[2] = 0x80 | ((ucs >> 6) & 0x3f);
87 p[3] = 0x80 | (ucs & 0x3f);
88 return 4;
89 } else if (ucs <= 0x3ffffff) {
90 p[0] = 0xf8 | (ucs >> 24);
91 p[1] = 0x80 | ((ucs >> 18) & 0x3f);
92 p[2] = 0x80 | ((ucs >> 12) & 0x3f);
93 p[3] = 0x80 | ((ucs >> 6) & 0x3f);
94 p[4] = 0x80 | (ucs & 0x3f);
95 return 5;
96 } else if (ucs <= 0x7ffffff) {
97 p[0] = 0xf8 | (ucs >> 30);
98 p[1] = 0x80 | ((ucs >> 24) & 0x3f);
99 p[2] = 0x80 | ((ucs >> 18) & 0x3f);
100 p[3] = 0x80 | ((ucs >> 12) & 0x3f);
101 p[4] = 0x80 | ((ucs >> 6) & 0x3f);
102 p[5] = 0x80 | (ucs & 0x3f);
103 return 6;
104 } else {
105 p[0] = 0xfe;
106 p[1] = 0x80 | ((ucs >> 30) & 0x3f);
107 p[2] = 0x80 | ((ucs >> 24) & 0x3f);
108 p[3] = 0x80 | ((ucs >> 18) & 0x3f);
109 p[4] = 0x80 | ((ucs >> 12) & 0x3f);
110 p[5] = 0x80 | ((ucs >> 6) & 0x3f);
111 p[6] = 0x80 | (ucs & 0x3f);
112 return 7;
113 }
114 }
115 RT_OFFLOAD_API_GROUP_END
116
117 } // namespace Fortran::runtime