| 1 |
/** |
| 2 |
* Functions related to UTF encoding. |
| 3 |
* |
| 4 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 5 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 6 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 7 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/root/utf.d, _utf.d) |
| 8 |
* Documentation: https://dlang.org/phobos/dmd_root_utf.html |
| 9 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/root/utf.d |
| 10 |
*/ |
| 11 |
|
| 12 |
module dmd.root.utf; |
| 13 |
|
| 14 |
@nogc nothrow pure @safe: |
| 15 |
|
| 16 |
/// The Unicode code space is the range of code points [0x000000,0x10FFFF] |
| 17 |
/// except the UTF-16 surrogate pairs in the range [0xD800,0xDFFF] |
| 18 |
bool utf_isValidDchar(dchar c) |
| 19 |
{ |
| 20 |
// TODO: Whether non-char code points should be rejected is pending review. |
| 21 |
// 0xFFFE and 0xFFFF are valid for internal use, like Phobos std.utf.isValidDChar |
| 22 |
// See also https://issues.dlang.org/show_bug.cgi?id=1357 |
| 23 |
if (c < 0xD800) // Almost all characters in a typical document. |
| 24 |
return true; |
| 25 |
if (c > 0xDFFF && c <= 0x10FFFF) |
| 26 |
return true; |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Returns the code length of c in code units. |
| 32 |
*/ |
| 33 |
int utf_codeLengthChar(dchar c) |
| 34 |
{ |
| 35 |
if (c <= 0x7F) |
| 36 |
return 1; |
| 37 |
if (c <= 0x7FF) |
| 38 |
return 2; |
| 39 |
if (c <= 0xFFFF) |
| 40 |
return 3; |
| 41 |
if (c <= 0x10FFFF) |
| 42 |
return 4; |
| 43 |
assert(false); |
| 44 |
} |
| 45 |
|
| 46 |
int utf_codeLengthWchar(dchar c) |
| 47 |
{ |
| 48 |
return c <= 0xFFFF ? 1 : 2; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Returns the code length of c in code units for the encoding. |
| 53 |
* sz is the encoding: 1 = utf8, 2 = utf16, 4 = utf32. |
| 54 |
*/ |
| 55 |
int utf_codeLength(int sz, dchar c) |
| 56 |
{ |
| 57 |
if (sz == 1) |
| 58 |
return utf_codeLengthChar(c); |
| 59 |
if (sz == 2) |
| 60 |
return utf_codeLengthWchar(c); |
| 61 |
assert(sz == 4); |
| 62 |
return 1; |
| 63 |
} |
| 64 |
|
| 65 |
void utf_encodeChar(char* s, dchar c) @system |
| 66 |
{ |
| 67 |
assert(s !is null); |
| 68 |
assert(utf_isValidDchar(c)); |
| 69 |
if (c <= 0x7F) |
| 70 |
{ |
| 71 |
s[0] = cast(char)c; |
| 72 |
} |
| 73 |
else if (c <= 0x07FF) |
| 74 |
{ |
| 75 |
s[0] = cast(char)(0xC0 | (c >> 6)); |
| 76 |
s[1] = cast(char)(0x80 | (c & 0x3F)); |
| 77 |
} |
| 78 |
else if (c <= 0xFFFF) |
| 79 |
{ |
| 80 |
s[0] = cast(char)(0xE0 | (c >> 12)); |
| 81 |
s[1] = cast(char)(0x80 | ((c >> 6) & 0x3F)); |
| 82 |
s[2] = cast(char)(0x80 | (c & 0x3F)); |
| 83 |
} |
| 84 |
else if (c <= 0x10FFFF) |
| 85 |
{ |
| 86 |
s[0] = cast(char)(0xF0 | (c >> 18)); |
| 87 |
s[1] = cast(char)(0x80 | ((c >> 12) & 0x3F)); |
| 88 |
s[2] = cast(char)(0x80 | ((c >> 6) & 0x3F)); |
| 89 |
s[3] = cast(char)(0x80 | (c & 0x3F)); |
| 90 |
} |
| 91 |
else |
| 92 |
assert(0); |
| 93 |
} |
| 94 |
|
| 95 |
void utf_encodeWchar(wchar* s, dchar c) @system |
| 96 |
{ |
| 97 |
assert(s !is null); |
| 98 |
assert(utf_isValidDchar(c)); |
| 99 |
if (c <= 0xFFFF) |
| 100 |
{ |
| 101 |
s[0] = cast(wchar)c; |
| 102 |
} |
| 103 |
else |
| 104 |
{ |
| 105 |
s[0] = cast(wchar)((((c - 0x010000) >> 10) & 0x03FF) + 0xD800); |
| 106 |
s[1] = cast(wchar)(((c - 0x010000) & 0x03FF) + 0xDC00); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
void utf_encode(int sz, void* s, dchar c) @system |
| 111 |
{ |
| 112 |
if (sz == 1) |
| 113 |
utf_encodeChar(cast(char*)s, c); |
| 114 |
else if (sz == 2) |
| 115 |
utf_encodeWchar(cast(wchar*)s, c); |
| 116 |
else |
| 117 |
{ |
| 118 |
assert(sz == 4); |
| 119 |
*(cast(dchar*)s) = c; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/******************************************** |
| 124 |
* Checks whether an Unicode code point is a bidirectional |
| 125 |
* control character. |
| 126 |
*/ |
| 127 |
bool isBidiControl(dchar c) |
| 128 |
{ |
| 129 |
// Source: https://www.unicode.org/versions/Unicode15.0.0, table 23-3. |
| 130 |
switch(c) |
| 131 |
{ |
| 132 |
case '\u061C': |
| 133 |
case '\u200E': |
| 134 |
case '\u200F': |
| 135 |
case '\u202A': .. case '\u202E': |
| 136 |
case '\u2066': .. case '\u2069': |
| 137 |
return true; |
| 138 |
default: |
| 139 |
return false; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/******************************************** |
| 144 |
* Decode a UTF-8 sequence as a single UTF-32 code point. |
| 145 |
* Params: |
| 146 |
* s = UTF-8 sequence |
| 147 |
* ridx = starting index in s[], updated to reflect number of code units decoded |
| 148 |
* rresult = set to character decoded |
| 149 |
* Returns: |
| 150 |
* null on success, otherwise error message string |
| 151 |
*/ |
| 152 |
string utf_decodeChar(const(char)[] s, ref size_t ridx, out dchar rresult) |
| 153 |
{ |
| 154 |
// UTF-8 decoding errors |
| 155 |
static immutable string UTF8_DECODE_OK = null; // no error |
| 156 |
static immutable string UTF8_DECODE_OUTSIDE_CODE_SPACE = "Outside Unicode code space"; |
| 157 |
static immutable string UTF8_DECODE_TRUNCATED_SEQUENCE = "Truncated UTF-8 sequence"; |
| 158 |
static immutable string UTF8_DECODE_OVERLONG = "Overlong UTF-8 sequence"; |
| 159 |
static immutable string UTF8_DECODE_INVALID_TRAILER = "Invalid trailing code unit"; |
| 160 |
static immutable string UTF8_DECODE_INVALID_CODE_POINT = "Invalid code point decoded"; |
| 161 |
|
| 162 |
/* The following encodings are valid, except for the 5 and 6 byte |
| 163 |
* combinations: |
| 164 |
* 0xxxxxxx |
| 165 |
* 110xxxxx 10xxxxxx |
| 166 |
* 1110xxxx 10xxxxxx 10xxxxxx |
| 167 |
* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 168 |
* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 169 |
* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 170 |
*/ |
| 171 |
static immutable ubyte[256] UTF8_STRIDE = |
| 172 |
[ |
| 173 |
1,1,1,1, 1,1,1,1, |
| 174 |
1,1,1,1, 1,1,1,1, |
| 175 |
1,1,1,1, 1,1,1,1, |
| 176 |
1,1,1,1, 1,1,1,1, |
| 177 |
1,1,1,1, 1,1,1,1, |
| 178 |
1,1,1,1, 1,1,1,1, |
| 179 |
1,1,1,1, 1,1,1,1, |
| 180 |
1,1,1,1, 1,1,1,1, |
| 181 |
|
| 182 |
1,1,1,1, 1,1,1,1, |
| 183 |
1,1,1,1, 1,1,1,1, |
| 184 |
1,1,1,1, 1,1,1,1, |
| 185 |
1,1,1,1, 1,1,1,1, |
| 186 |
1,1,1,1, 1,1,1,1, |
| 187 |
1,1,1,1, 1,1,1,1, |
| 188 |
1,1,1,1, 1,1,1,1, |
| 189 |
1,1,1,1, 1,1,1,1, |
| 190 |
|
| 191 |
0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, |
| 192 |
0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, |
| 193 |
0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, |
| 194 |
0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, |
| 195 |
0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, |
| 196 |
0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, |
| 197 |
0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, |
| 198 |
0xFF,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF,0xFF, |
| 199 |
|
| 200 |
2,2,2,2, 2,2,2,2, |
| 201 |
2,2,2,2, 2,2,2,2, |
| 202 |
2,2,2,2, 2,2,2,2, |
| 203 |
2,2,2,2, 2,2,2,2, |
| 204 |
|
| 205 |
3,3,3,3, 3,3,3,3, |
| 206 |
3,3,3,3, 3,3,3,3, |
| 207 |
|
| 208 |
4,4,4,4, 4,4,4,4, |
| 209 |
5,5,5,5, 6,6,0xFF,0xFF |
| 210 |
]; |
| 211 |
|
| 212 |
assert(s !is null); |
| 213 |
size_t i = ridx++; |
| 214 |
|
| 215 |
const char u = s[i]; |
| 216 |
// Pre-stage results for ASCII and error cases |
| 217 |
rresult = u; |
| 218 |
//printf("utf_decodeChar(s = %02x, %02x, %02x len = %d)\n", u, s[1], s[2], len); |
| 219 |
// Get expected sequence length |
| 220 |
const size_t n = UTF8_STRIDE[u]; |
| 221 |
switch (n) |
| 222 |
{ |
| 223 |
case 1: |
| 224 |
// ASCII |
| 225 |
return UTF8_DECODE_OK; |
| 226 |
case 2: |
| 227 |
case 3: |
| 228 |
case 4: |
| 229 |
// multi-byte UTF-8 |
| 230 |
break; |
| 231 |
default: |
| 232 |
// 5- or 6-byte sequence |
| 233 |
return UTF8_DECODE_OUTSIDE_CODE_SPACE; |
| 234 |
} |
| 235 |
if (s.length < i + n) // source too short |
| 236 |
return UTF8_DECODE_TRUNCATED_SEQUENCE; |
| 237 |
// Pick off 7 - n low bits from first code unit |
| 238 |
dchar c = u & ((1 << (7 - n)) - 1); |
| 239 |
/* The following combinations are overlong, and illegal: |
| 240 |
* 1100000x (10xxxxxx) |
| 241 |
* 11100000 100xxxxx (10xxxxxx) |
| 242 |
* 11110000 1000xxxx (10xxxxxx 10xxxxxx) |
| 243 |
* 11111000 10000xxx (10xxxxxx 10xxxxxx 10xxxxxx) |
| 244 |
* 11111100 100000xx (10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx) |
| 245 |
*/ |
| 246 |
const char u2 = s[++i]; |
| 247 |
// overlong combination |
| 248 |
if ((u & 0xFE) == 0xC0 || (u == 0xE0 && (u2 & 0xE0) == 0x80) || (u == 0xF0 && (u2 & 0xF0) == 0x80) || (u == 0xF8 && (u2 & 0xF8) == 0x80) || (u == 0xFC && (u2 & 0xFC) == 0x80)) |
| 249 |
return UTF8_DECODE_OVERLONG; |
| 250 |
// Decode remaining bits |
| 251 |
for (const m = n + i - 1; i != m; ++i) |
| 252 |
{ |
| 253 |
const u3 = s[i]; |
| 254 |
if ((u3 & 0xC0) != 0x80) // trailing bytes are 10xxxxxx |
| 255 |
return UTF8_DECODE_INVALID_TRAILER; |
| 256 |
c = (c << 6) | (u3 & 0x3F); |
| 257 |
} |
| 258 |
if (!utf_isValidDchar(c)) |
| 259 |
return UTF8_DECODE_INVALID_CODE_POINT; |
| 260 |
ridx = i; |
| 261 |
rresult = c; |
| 262 |
return UTF8_DECODE_OK; |
| 263 |
} |
| 264 |
|
| 265 |
/******************************************** |
| 266 |
* Decode a UTF-16 sequence as a single UTF-32 code point. |
| 267 |
* Params: |
| 268 |
* s = UTF-16 sequence |
| 269 |
* ridx = starting index in s[], updated to reflect number of code units decoded |
| 270 |
* rresult = set to character decoded |
| 271 |
* Returns: |
| 272 |
* null on success, otherwise error message string |
| 273 |
*/ |
| 274 |
string utf_decodeWchar(const(wchar)[] s, ref size_t ridx, out dchar rresult) |
| 275 |
{ |
| 276 |
// UTF-16 decoding errors |
| 277 |
static immutable string UTF16_DECODE_OK = null; // no error |
| 278 |
static immutable string UTF16_DECODE_TRUNCATED_SEQUENCE = "Truncated UTF-16 sequence"; |
| 279 |
static immutable string UTF16_DECODE_INVALID_SURROGATE = "Invalid low surrogate"; |
| 280 |
static immutable string UTF16_DECODE_UNPAIRED_SURROGATE = "Unpaired surrogate"; |
| 281 |
static immutable string UTF16_DECODE_INVALID_CODE_POINT = "Invalid code point decoded"; |
| 282 |
|
| 283 |
assert(s !is null); |
| 284 |
size_t i = ridx++; |
| 285 |
|
| 286 |
// Pre-stage results for single wchar and error cases |
| 287 |
dchar u = rresult = s[i]; |
| 288 |
if (u < 0xD800) // Single wchar codepoint |
| 289 |
return UTF16_DECODE_OK; |
| 290 |
if (0xD800 <= u && u <= 0xDBFF) // Surrogate pair |
| 291 |
{ |
| 292 |
if (s.length <= i + 1) |
| 293 |
return UTF16_DECODE_TRUNCATED_SEQUENCE; |
| 294 |
wchar u2 = s[i + 1]; |
| 295 |
if (u2 < 0xDC00 || 0xDFFF < u) |
| 296 |
return UTF16_DECODE_INVALID_SURROGATE; |
| 297 |
u = ((u - 0xD7C0) << 10) + (u2 - 0xDC00); |
| 298 |
++ridx; |
| 299 |
} |
| 300 |
else if (0xDC00 <= u && u <= 0xDFFF) |
| 301 |
return UTF16_DECODE_UNPAIRED_SURROGATE; |
| 302 |
if (!utf_isValidDchar(u)) |
| 303 |
return UTF16_DECODE_INVALID_CODE_POINT; |
| 304 |
rresult = u; |
| 305 |
return UTF16_DECODE_OK; |
| 306 |
} |