| 1 |
/******************************************** |
| 2 |
* Encode and decode UTF-8, UTF-16 and UTF-32 strings. |
| 3 |
* |
| 4 |
* For Win32 systems, the C wchar_t type is UTF-16 and corresponds to the D |
| 5 |
* wchar type. |
| 6 |
* For Posix systems, the C wchar_t type is UTF-32 and corresponds to |
| 7 |
* the D utf.dchar type. |
| 8 |
* |
| 9 |
* UTF character support is restricted to (\u0000 <= character <= \U0010FFFF). |
| 10 |
* |
| 11 |
* See_Also: |
| 12 |
* $(LINK2 http://en.wikipedia.org/wiki/Unicode, Wikipedia)<br> |
| 13 |
* $(LINK http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8)<br> |
| 14 |
* $(LINK http://anubis.dkuug.dk/JTC1/SC2/WG2/docs/n1335) |
| 15 |
* |
| 16 |
* Copyright: Copyright Digital Mars 2003 - 2016. |
| 17 |
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0). |
| 18 |
* Authors: Walter Bright, Sean Kelly |
| 19 |
* Source: $(DRUNTIMESRC core/internal/_utf.d) |
| 20 |
*/ |
| 21 |
|
| 22 |
module core.internal.utf; |
| 23 |
|
| 24 |
debug (utf) import core.stdc.stdio : printf; |
| 25 |
|
| 26 |
extern (C) void onUnicodeError( string msg, size_t idx, string file = __FILE__, size_t line = __LINE__ ) @safe pure; |
| 27 |
|
| 28 |
/******************************* |
| 29 |
* Test if c is a valid UTF-32 character. |
| 30 |
* |
| 31 |
* \uFFFE and \uFFFF are considered valid by this function, |
| 32 |
* as they are permitted for internal use by an application, |
| 33 |
* but they are not allowed for interchange by the Unicode standard. |
| 34 |
* |
| 35 |
* Returns: true if it is, false if not. |
| 36 |
*/ |
| 37 |
|
| 38 |
@safe @nogc pure nothrow |
| 39 |
bool isValidDchar(dchar c) |
| 40 |
{ |
| 41 |
/* Note: FFFE and FFFF are specifically permitted by the |
| 42 |
* Unicode standard for application internal use, but are not |
| 43 |
* allowed for interchange. |
| 44 |
* (thanks to Arcane Jill) |
| 45 |
*/ |
| 46 |
|
| 47 |
return c < 0xD800 || |
| 48 |
(c > 0xDFFF && c <= 0x10FFFF /*&& c != 0xFFFE && c != 0xFFFF*/); |
| 49 |
} |
| 50 |
|
| 51 |
unittest |
| 52 |
{ |
| 53 |
debug(utf) printf("utf.isValidDchar.unittest\n"); |
| 54 |
assert(isValidDchar(cast(dchar)'a') == true); |
| 55 |
assert(isValidDchar(cast(dchar)0x1FFFFF) == false); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
static immutable UTF8stride = |
| 61 |
[ |
| 62 |
cast(ubyte) |
| 63 |
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 64 |
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 65 |
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 66 |
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 67 |
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 68 |
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 69 |
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 70 |
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, |
| 71 |
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, |
| 72 |
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, |
| 73 |
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, |
| 74 |
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, |
| 75 |
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, |
| 76 |
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, |
| 77 |
3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, |
| 78 |
4,4,4,4,4,4,4,4,5,5,5,5,6,6,0xFF,0xFF, |
| 79 |
]; |
| 80 |
|
| 81 |
/** |
| 82 |
* stride() returns the length of a UTF-8 sequence starting at index i |
| 83 |
* in string s. |
| 84 |
* Returns: |
| 85 |
* The number of bytes in the UTF-8 sequence or |
| 86 |
* 0xFF meaning s[i] is not the start of of UTF-8 sequence. |
| 87 |
*/ |
| 88 |
@safe @nogc pure nothrow |
| 89 |
uint stride(const scope char[] s, size_t i) |
| 90 |
{ |
| 91 |
return UTF8stride[s[i]]; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* stride() returns the length of a UTF-16 sequence starting at index i |
| 96 |
* in string s. |
| 97 |
*/ |
| 98 |
@safe @nogc pure nothrow |
| 99 |
uint stride(const scope wchar[] s, size_t i) |
| 100 |
{ uint u = s[i]; |
| 101 |
return 1 + (u >= 0xD800 && u <= 0xDBFF); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* stride() returns the length of a UTF-32 sequence starting at index i |
| 106 |
* in string s. |
| 107 |
* Returns: The return value will always be 1. |
| 108 |
*/ |
| 109 |
@safe @nogc pure nothrow |
| 110 |
uint stride(const scope dchar[] s, size_t i) |
| 111 |
{ |
| 112 |
return 1; |
| 113 |
} |
| 114 |
|
| 115 |
/******************************************* |
| 116 |
* Given an index i into an array of characters s[], |
| 117 |
* and assuming that index i is at the start of a UTF character, |
| 118 |
* determine the number of UCS characters up to that index i. |
| 119 |
*/ |
| 120 |
@safe pure |
| 121 |
size_t toUCSindex(const scope char[] s, size_t i) |
| 122 |
{ |
| 123 |
size_t n; |
| 124 |
size_t j; |
| 125 |
|
| 126 |
for (j = 0; j < i; ) |
| 127 |
{ |
| 128 |
j += stride(s, j); |
| 129 |
n++; |
| 130 |
} |
| 131 |
if (j > i) |
| 132 |
{ |
| 133 |
onUnicodeError("invalid UTF-8 sequence", j); |
| 134 |
} |
| 135 |
return n; |
| 136 |
} |
| 137 |
|
| 138 |
/** ditto */ |
| 139 |
@safe pure |
| 140 |
size_t toUCSindex(const scope wchar[] s, size_t i) |
| 141 |
{ |
| 142 |
size_t n; |
| 143 |
size_t j; |
| 144 |
|
| 145 |
for (j = 0; j < i; ) |
| 146 |
{ |
| 147 |
j += stride(s, j); |
| 148 |
n++; |
| 149 |
} |
| 150 |
if (j > i) |
| 151 |
{ |
| 152 |
onUnicodeError("invalid UTF-16 sequence", j); |
| 153 |
} |
| 154 |
return n; |
| 155 |
} |
| 156 |
|
| 157 |
/** ditto */ |
| 158 |
@safe @nogc pure nothrow |
| 159 |
size_t toUCSindex(const scope dchar[] s, size_t i) |
| 160 |
{ |
| 161 |
return i; |
| 162 |
} |
| 163 |
|
| 164 |
/****************************************** |
| 165 |
* Given a UCS index n into an array of characters s[], return the UTF index. |
| 166 |
*/ |
| 167 |
@safe pure |
| 168 |
size_t toUTFindex(const scope char[] s, size_t n) |
| 169 |
{ |
| 170 |
size_t i; |
| 171 |
|
| 172 |
while (n--) |
| 173 |
{ |
| 174 |
uint j = UTF8stride[s[i]]; |
| 175 |
if (j == 0xFF) |
| 176 |
onUnicodeError("invalid UTF-8 sequence", i); |
| 177 |
i += j; |
| 178 |
} |
| 179 |
return i; |
| 180 |
} |
| 181 |
|
| 182 |
/** ditto */ |
| 183 |
@safe @nogc pure nothrow |
| 184 |
size_t toUTFindex(const scope wchar[] s, size_t n) |
| 185 |
{ |
| 186 |
size_t i; |
| 187 |
|
| 188 |
while (n--) |
| 189 |
{ wchar u = s[i]; |
| 190 |
|
| 191 |
i += 1 + (u >= 0xD800 && u <= 0xDBFF); |
| 192 |
} |
| 193 |
return i; |
| 194 |
} |
| 195 |
|
| 196 |
/** ditto */ |
| 197 |
@safe @nogc pure nothrow |
| 198 |
size_t toUTFindex(const scope dchar[] s, size_t n) |
| 199 |
{ |
| 200 |
return n; |
| 201 |
} |
| 202 |
|
| 203 |
/* =================== Decode ======================= */ |
| 204 |
|
| 205 |
/*************** |
| 206 |
* Decodes and returns character starting at s[idx]. idx is advanced past the |
| 207 |
* decoded character. If the character is not well formed, a UtfException is |
| 208 |
* thrown and idx remains unchanged. |
| 209 |
*/ |
| 210 |
@safe pure |
| 211 |
dchar decode(const scope char[] s, ref size_t idx) |
| 212 |
in |
| 213 |
{ |
| 214 |
assert(idx >= 0 && idx < s.length); |
| 215 |
} |
| 216 |
out (result) |
| 217 |
{ |
| 218 |
assert(isValidDchar(result)); |
| 219 |
} |
| 220 |
do |
| 221 |
{ |
| 222 |
size_t len = s.length; |
| 223 |
dchar V; |
| 224 |
size_t i = idx; |
| 225 |
char u = s[i]; |
| 226 |
|
| 227 |
if (u & 0x80) |
| 228 |
{ uint n; |
| 229 |
char u2; |
| 230 |
|
| 231 |
/* The following encodings are valid, except for the 5 and 6 byte |
| 232 |
* combinations: |
| 233 |
* 0xxxxxxx |
| 234 |
* 110xxxxx 10xxxxxx |
| 235 |
* 1110xxxx 10xxxxxx 10xxxxxx |
| 236 |
* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 237 |
* 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 238 |
* 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx |
| 239 |
*/ |
| 240 |
for (n = 1; ; n++) |
| 241 |
{ |
| 242 |
if (n > 4) |
| 243 |
goto Lerr; // only do the first 4 of 6 encodings |
| 244 |
if (((u << n) & 0x80) == 0) |
| 245 |
{ |
| 246 |
if (n == 1) |
| 247 |
goto Lerr; |
| 248 |
break; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
// Pick off (7 - n) significant bits of B from first byte of octet |
| 253 |
V = cast(dchar)(u & ((1 << (7 - n)) - 1)); |
| 254 |
|
| 255 |
if (i + (n - 1) >= len) |
| 256 |
goto Lerr; // off end of string |
| 257 |
|
| 258 |
/* The following combinations are overlong, and illegal: |
| 259 |
* 1100000x (10xxxxxx) |
| 260 |
* 11100000 100xxxxx (10xxxxxx) |
| 261 |
* 11110000 1000xxxx (10xxxxxx 10xxxxxx) |
| 262 |
* 11111000 10000xxx (10xxxxxx 10xxxxxx 10xxxxxx) |
| 263 |
* 11111100 100000xx (10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx) |
| 264 |
*/ |
| 265 |
u2 = s[i + 1]; |
| 266 |
if ((u & 0xFE) == 0xC0 || |
| 267 |
(u == 0xE0 && (u2 & 0xE0) == 0x80) || |
| 268 |
(u == 0xF0 && (u2 & 0xF0) == 0x80) || |
| 269 |
(u == 0xF8 && (u2 & 0xF8) == 0x80) || |
| 270 |
(u == 0xFC && (u2 & 0xFC) == 0x80)) |
| 271 |
goto Lerr; // overlong combination |
| 272 |
|
| 273 |
for (uint j = 1; j != n; j++) |
| 274 |
{ |
| 275 |
u = s[i + j]; |
| 276 |
if ((u & 0xC0) != 0x80) |
| 277 |
goto Lerr; // trailing bytes are 10xxxxxx |
| 278 |
V = (V << 6) | (u & 0x3F); |
| 279 |
} |
| 280 |
if (!isValidDchar(V)) |
| 281 |
goto Lerr; |
| 282 |
i += n; |
| 283 |
} |
| 284 |
else |
| 285 |
{ |
| 286 |
V = cast(dchar) u; |
| 287 |
i++; |
| 288 |
} |
| 289 |
|
| 290 |
idx = i; |
| 291 |
return V; |
| 292 |
|
| 293 |
Lerr: |
| 294 |
onUnicodeError("invalid UTF-8 sequence", i); |
| 295 |
return V; // dummy return |
| 296 |
} |
| 297 |
|
| 298 |
unittest |
| 299 |
{ size_t i; |
| 300 |
dchar c; |
| 301 |
|
| 302 |
debug(utf) printf("utf.decode.unittest\n"); |
| 303 |
|
| 304 |
static s1 = "abcd"c; |
| 305 |
i = 0; |
| 306 |
c = decode(s1, i); |
| 307 |
assert(c == cast(dchar)'a'); |
| 308 |
assert(i == 1); |
| 309 |
c = decode(s1, i); |
| 310 |
assert(c == cast(dchar)'b'); |
| 311 |
assert(i == 2); |
| 312 |
|
| 313 |
static s2 = "\xC2\xA9"c; |
| 314 |
i = 0; |
| 315 |
c = decode(s2, i); |
| 316 |
assert(c == cast(dchar)'\u00A9'); |
| 317 |
assert(i == 2); |
| 318 |
|
| 319 |
static s3 = "\xE2\x89\xA0"c; |
| 320 |
i = 0; |
| 321 |
c = decode(s3, i); |
| 322 |
assert(c == cast(dchar)'\u2260'); |
| 323 |
assert(i == 3); |
| 324 |
|
| 325 |
static s4 = |
| 326 |
[ "\xE2\x89"c[], // too short |
| 327 |
"\xC0\x8A", |
| 328 |
"\xE0\x80\x8A", |
| 329 |
"\xF0\x80\x80\x8A", |
| 330 |
"\xF8\x80\x80\x80\x8A", |
| 331 |
"\xFC\x80\x80\x80\x80\x8A", |
| 332 |
]; |
| 333 |
|
| 334 |
for (int j = 0; j < s4.length; j++) |
| 335 |
{ |
| 336 |
try |
| 337 |
{ |
| 338 |
i = 0; |
| 339 |
c = decode(s4[j], i); |
| 340 |
assert(0); |
| 341 |
} |
| 342 |
catch (Throwable o) |
| 343 |
{ |
| 344 |
i = 23; |
| 345 |
} |
| 346 |
assert(i == 23); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
/** ditto */ |
| 351 |
@safe pure |
| 352 |
dchar decode(const scope wchar[] s, ref size_t idx) |
| 353 |
in |
| 354 |
{ |
| 355 |
assert(idx >= 0 && idx < s.length); |
| 356 |
} |
| 357 |
out (result) |
| 358 |
{ |
| 359 |
assert(isValidDchar(result)); |
| 360 |
} |
| 361 |
do |
| 362 |
{ |
| 363 |
string msg; |
| 364 |
dchar V; |
| 365 |
size_t i = idx; |
| 366 |
uint u = s[i]; |
| 367 |
|
| 368 |
if (u & ~0x7F) |
| 369 |
{ if (u >= 0xD800 && u <= 0xDBFF) |
| 370 |
{ uint u2; |
| 371 |
|
| 372 |
if (i + 1 == s.length) |
| 373 |
{ msg = "surrogate UTF-16 high value past end of string"; |
| 374 |
goto Lerr; |
| 375 |
} |
| 376 |
u2 = s[i + 1]; |
| 377 |
if (u2 < 0xDC00 || u2 > 0xDFFF) |
| 378 |
{ msg = "surrogate UTF-16 low value out of range"; |
| 379 |
goto Lerr; |
| 380 |
} |
| 381 |
u = ((u - 0xD7C0) << 10) + (u2 - 0xDC00); |
| 382 |
i += 2; |
| 383 |
} |
| 384 |
else if (u >= 0xDC00 && u <= 0xDFFF) |
| 385 |
{ msg = "unpaired surrogate UTF-16 value"; |
| 386 |
goto Lerr; |
| 387 |
} |
| 388 |
else if (u == 0xFFFE || u == 0xFFFF) |
| 389 |
{ msg = "illegal UTF-16 value"; |
| 390 |
goto Lerr; |
| 391 |
} |
| 392 |
else |
| 393 |
i++; |
| 394 |
} |
| 395 |
else |
| 396 |
{ |
| 397 |
i++; |
| 398 |
} |
| 399 |
|
| 400 |
idx = i; |
| 401 |
return cast(dchar)u; |
| 402 |
|
| 403 |
Lerr: |
| 404 |
onUnicodeError(msg, i); |
| 405 |
return cast(dchar)u; // dummy return |
| 406 |
} |
| 407 |
|
| 408 |
/** ditto */ |
| 409 |
@safe pure |
| 410 |
dchar decode(const scope dchar[] s, ref size_t idx) |
| 411 |
in |
| 412 |
{ |
| 413 |
assert(idx >= 0 && idx < s.length); |
| 414 |
} |
| 415 |
do |
| 416 |
{ |
| 417 |
size_t i = idx; |
| 418 |
dchar c = s[i]; |
| 419 |
|
| 420 |
if (!isValidDchar(c)) |
| 421 |
goto Lerr; |
| 422 |
idx = i + 1; |
| 423 |
return c; |
| 424 |
|
| 425 |
Lerr: |
| 426 |
onUnicodeError("invalid UTF-32 value", i); |
| 427 |
return c; // dummy return |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
/* =================== Encode ======================= */ |
| 432 |
|
| 433 |
/******************************* |
| 434 |
* Encodes character c and appends it to array s[]. |
| 435 |
*/ |
| 436 |
@safe pure nothrow |
| 437 |
void encode(ref char[] s, dchar c) |
| 438 |
in |
| 439 |
{ |
| 440 |
assert(isValidDchar(c)); |
| 441 |
} |
| 442 |
do |
| 443 |
{ |
| 444 |
char[] r = s; |
| 445 |
|
| 446 |
if (c <= 0x7F) |
| 447 |
{ |
| 448 |
r ~= cast(char) c; |
| 449 |
} |
| 450 |
else |
| 451 |
{ |
| 452 |
char[4] buf = void; |
| 453 |
uint L; |
| 454 |
|
| 455 |
if (c <= 0x7FF) |
| 456 |
{ |
| 457 |
buf[0] = cast(char)(0xC0 | (c >> 6)); |
| 458 |
buf[1] = cast(char)(0x80 | (c & 0x3F)); |
| 459 |
L = 2; |
| 460 |
} |
| 461 |
else if (c <= 0xFFFF) |
| 462 |
{ |
| 463 |
buf[0] = cast(char)(0xE0 | (c >> 12)); |
| 464 |
buf[1] = cast(char)(0x80 | ((c >> 6) & 0x3F)); |
| 465 |
buf[2] = cast(char)(0x80 | (c & 0x3F)); |
| 466 |
L = 3; |
| 467 |
} |
| 468 |
else if (c <= 0x10FFFF) |
| 469 |
{ |
| 470 |
buf[0] = cast(char)(0xF0 | (c >> 18)); |
| 471 |
buf[1] = cast(char)(0x80 | ((c >> 12) & 0x3F)); |
| 472 |
buf[2] = cast(char)(0x80 | ((c >> 6) & 0x3F)); |
| 473 |
buf[3] = cast(char)(0x80 | (c & 0x3F)); |
| 474 |
L = 4; |
| 475 |
} |
| 476 |
else |
| 477 |
{ |
| 478 |
assert(0); |
| 479 |
} |
| 480 |
r ~= buf[0 .. L]; |
| 481 |
} |
| 482 |
s = r; |
| 483 |
} |
| 484 |
|
| 485 |
unittest |
| 486 |
{ |
| 487 |
debug(utf) printf("utf.encode.unittest\n"); |
| 488 |
|
| 489 |
char[] s = "abcd".dup; |
| 490 |
encode(s, cast(dchar)'a'); |
| 491 |
assert(s.length == 5); |
| 492 |
assert(s == "abcda"); |
| 493 |
|
| 494 |
encode(s, cast(dchar)'\u00A9'); |
| 495 |
assert(s.length == 7); |
| 496 |
assert(s == "abcda\xC2\xA9"); |
| 497 |
//assert(s == "abcda\u00A9"); // BUG: fix compiler |
| 498 |
|
| 499 |
encode(s, cast(dchar)'\u2260'); |
| 500 |
assert(s.length == 10); |
| 501 |
assert(s == "abcda\xC2\xA9\xE2\x89\xA0"); |
| 502 |
} |
| 503 |
|
| 504 |
/** ditto */ |
| 505 |
@safe pure nothrow |
| 506 |
void encode(ref wchar[] s, dchar c) |
| 507 |
in |
| 508 |
{ |
| 509 |
assert(isValidDchar(c)); |
| 510 |
} |
| 511 |
do |
| 512 |
{ |
| 513 |
wchar[] r = s; |
| 514 |
|
| 515 |
if (c <= 0xFFFF) |
| 516 |
{ |
| 517 |
r ~= cast(wchar) c; |
| 518 |
} |
| 519 |
else |
| 520 |
{ |
| 521 |
wchar[2] buf = void; |
| 522 |
|
| 523 |
buf[0] = cast(wchar) ((((c - 0x10000) >> 10) & 0x3FF) + 0xD800); |
| 524 |
buf[1] = cast(wchar) (((c - 0x10000) & 0x3FF) + 0xDC00); |
| 525 |
r ~= buf; |
| 526 |
} |
| 527 |
s = r; |
| 528 |
} |
| 529 |
|
| 530 |
/** ditto */ |
| 531 |
@safe pure nothrow |
| 532 |
void encode(ref dchar[] s, dchar c) |
| 533 |
in |
| 534 |
{ |
| 535 |
assert(isValidDchar(c)); |
| 536 |
} |
| 537 |
do |
| 538 |
{ |
| 539 |
s ~= c; |
| 540 |
} |
| 541 |
|
| 542 |
/** |
| 543 |
Returns the code length of $(D c) in the encoding using $(D C) as a |
| 544 |
code point. The code is returned in character count, not in bytes. |
| 545 |
*/ |
| 546 |
@safe pure nothrow @nogc |
| 547 |
ubyte codeLength(C)(dchar c) |
| 548 |
{ |
| 549 |
static if (C.sizeof == 1) |
| 550 |
{ |
| 551 |
if (c <= 0x7F) return 1; |
| 552 |
if (c <= 0x7FF) return 2; |
| 553 |
if (c <= 0xFFFF) return 3; |
| 554 |
if (c <= 0x10FFFF) return 4; |
| 555 |
assert(false); |
| 556 |
} |
| 557 |
else static if (C.sizeof == 2) |
| 558 |
{ |
| 559 |
return c <= 0xFFFF ? 1 : 2; |
| 560 |
} |
| 561 |
else |
| 562 |
{ |
| 563 |
static assert(C.sizeof == 4); |
| 564 |
return 1; |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
/* =================== Validation ======================= */ |
| 569 |
|
| 570 |
/*********************************** |
| 571 |
Checks to see if string is well formed or not. $(D S) can be an array |
| 572 |
of $(D char), $(D wchar), or $(D dchar). Returns $(D false) if it is not. |
| 573 |
Use to check all untrusted input for correctness. |
| 574 |
*/ |
| 575 |
@safe pure nothrow |
| 576 |
bool isValidString(S)(const scope S s) |
| 577 |
{ |
| 578 |
auto len = s.length; |
| 579 |
for (size_t i = 0; i < len; ) |
| 580 |
{ |
| 581 |
try |
| 582 |
decode(s, i); |
| 583 |
catch (Exception e) |
| 584 |
return false; |
| 585 |
} |
| 586 |
|
| 587 |
return true; |
| 588 |
} |
| 589 |
|
| 590 |
/* =================== Conversion to UTF8 ======================= */ |
| 591 |
|
| 592 |
@safe pure nothrow @nogc |
| 593 |
char[] toUTF8(return scope char[] buf, dchar c) |
| 594 |
in |
| 595 |
{ |
| 596 |
assert(isValidDchar(c)); |
| 597 |
} |
| 598 |
do |
| 599 |
{ |
| 600 |
if (c <= 0x7F) |
| 601 |
{ |
| 602 |
buf[0] = cast(char) c; |
| 603 |
return buf[0 .. 1]; |
| 604 |
} |
| 605 |
else if (c <= 0x7FF) |
| 606 |
{ |
| 607 |
buf[0] = cast(char)(0xC0 | (c >> 6)); |
| 608 |
buf[1] = cast(char)(0x80 | (c & 0x3F)); |
| 609 |
return buf[0 .. 2]; |
| 610 |
} |
| 611 |
else if (c <= 0xFFFF) |
| 612 |
{ |
| 613 |
buf[0] = cast(char)(0xE0 | (c >> 12)); |
| 614 |
buf[1] = cast(char)(0x80 | ((c >> 6) & 0x3F)); |
| 615 |
buf[2] = cast(char)(0x80 | (c & 0x3F)); |
| 616 |
return buf[0 .. 3]; |
| 617 |
} |
| 618 |
else if (c <= 0x10FFFF) |
| 619 |
{ |
| 620 |
buf[0] = cast(char)(0xF0 | (c >> 18)); |
| 621 |
buf[1] = cast(char)(0x80 | ((c >> 12) & 0x3F)); |
| 622 |
buf[2] = cast(char)(0x80 | ((c >> 6) & 0x3F)); |
| 623 |
buf[3] = cast(char)(0x80 | (c & 0x3F)); |
| 624 |
return buf[0 .. 4]; |
| 625 |
} |
| 626 |
assert(0); |
| 627 |
} |
| 628 |
|
| 629 |
/******************* |
| 630 |
* Encodes string s into UTF-8 and returns the encoded string. |
| 631 |
*/ |
| 632 |
@safe pure nothrow |
| 633 |
string toUTF8(return scope string s) |
| 634 |
in |
| 635 |
{ |
| 636 |
assert(isValidString(s)); |
| 637 |
} |
| 638 |
do |
| 639 |
{ |
| 640 |
return s; |
| 641 |
} |
| 642 |
|
| 643 |
/** ditto */ |
| 644 |
@trusted pure |
| 645 |
string toUTF8(const scope wchar[] s) |
| 646 |
{ |
| 647 |
char[] r; |
| 648 |
size_t i; |
| 649 |
size_t slen = s.length; |
| 650 |
|
| 651 |
r.length = slen; |
| 652 |
|
| 653 |
for (i = 0; i < slen; i++) |
| 654 |
{ wchar c = s[i]; |
| 655 |
|
| 656 |
if (c <= 0x7F) |
| 657 |
r[i] = cast(char)c; // fast path for ascii |
| 658 |
else |
| 659 |
{ |
| 660 |
r.length = i; |
| 661 |
foreach (dchar ch; s[i .. slen]) |
| 662 |
{ |
| 663 |
encode(r, ch); |
| 664 |
} |
| 665 |
break; |
| 666 |
} |
| 667 |
} |
| 668 |
return cast(string)r; |
| 669 |
} |
| 670 |
|
| 671 |
/** ditto */ |
| 672 |
@trusted pure |
| 673 |
string toUTF8(const scope dchar[] s) |
| 674 |
{ |
| 675 |
char[] r; |
| 676 |
size_t i; |
| 677 |
size_t slen = s.length; |
| 678 |
|
| 679 |
r.length = slen; |
| 680 |
|
| 681 |
for (i = 0; i < slen; i++) |
| 682 |
{ dchar c = s[i]; |
| 683 |
|
| 684 |
if (c <= 0x7F) |
| 685 |
r[i] = cast(char)c; // fast path for ascii |
| 686 |
else |
| 687 |
{ |
| 688 |
r.length = i; |
| 689 |
foreach (dchar d; s[i .. slen]) |
| 690 |
{ |
| 691 |
encode(r, d); |
| 692 |
} |
| 693 |
break; |
| 694 |
} |
| 695 |
} |
| 696 |
return cast(string)r; |
| 697 |
} |
| 698 |
|
| 699 |
/* =================== Conversion to UTF16 ======================= */ |
| 700 |
|
| 701 |
@safe pure nothrow @nogc |
| 702 |
wchar[] toUTF16(return scope wchar[] buf, dchar c) |
| 703 |
in |
| 704 |
{ |
| 705 |
assert(isValidDchar(c)); |
| 706 |
} |
| 707 |
do |
| 708 |
{ |
| 709 |
if (c <= 0xFFFF) |
| 710 |
{ |
| 711 |
buf[0] = cast(wchar) c; |
| 712 |
return buf[0 .. 1]; |
| 713 |
} |
| 714 |
else |
| 715 |
{ |
| 716 |
buf[0] = cast(wchar) ((((c - 0x10000) >> 10) & 0x3FF) + 0xD800); |
| 717 |
buf[1] = cast(wchar) (((c - 0x10000) & 0x3FF) + 0xDC00); |
| 718 |
return buf[0 .. 2]; |
| 719 |
} |
| 720 |
} |
| 721 |
|
| 722 |
/**************** |
| 723 |
* Encodes string s into UTF-16 and returns the encoded string. |
| 724 |
* toUTF16z() is suitable for calling the 'W' functions in the Win32 API that take |
| 725 |
* an LPWSTR or LPCWSTR argument. |
| 726 |
*/ |
| 727 |
@trusted pure |
| 728 |
wstring toUTF16(const scope char[] s) |
| 729 |
{ |
| 730 |
wchar[] r; |
| 731 |
size_t slen = s.length; |
| 732 |
|
| 733 |
if (!__ctfe) |
| 734 |
{ |
| 735 |
// Reserve still does a lot if slen is zero. |
| 736 |
// Return early for that case. |
| 737 |
if (0 == slen) |
| 738 |
return ""w; |
| 739 |
r.reserve(slen); |
| 740 |
} |
| 741 |
for (size_t i = 0; i < slen; ) |
| 742 |
{ |
| 743 |
dchar c = s[i]; |
| 744 |
if (c <= 0x7F) |
| 745 |
{ |
| 746 |
i++; |
| 747 |
r ~= cast(wchar)c; |
| 748 |
} |
| 749 |
else |
| 750 |
{ |
| 751 |
c = decode(s, i); |
| 752 |
encode(r, c); |
| 753 |
} |
| 754 |
} |
| 755 |
return cast(wstring)r; |
| 756 |
} |
| 757 |
|
| 758 |
alias const(wchar)* wptr; |
| 759 |
/** ditto */ |
| 760 |
@safe pure |
| 761 |
wptr toUTF16z(const scope char[] s) |
| 762 |
{ |
| 763 |
wchar[] r; |
| 764 |
size_t slen = s.length; |
| 765 |
|
| 766 |
if (!__ctfe) |
| 767 |
{ |
| 768 |
// Reserve still does a lot if slen is zero. |
| 769 |
// Return early for that case. |
| 770 |
if (0 == slen) |
| 771 |
return &"\0"w[0]; |
| 772 |
r.reserve(slen + 1); |
| 773 |
} |
| 774 |
for (size_t i = 0; i < slen; ) |
| 775 |
{ |
| 776 |
dchar c = s[i]; |
| 777 |
if (c <= 0x7F) |
| 778 |
{ |
| 779 |
i++; |
| 780 |
r ~= cast(wchar)c; |
| 781 |
} |
| 782 |
else |
| 783 |
{ |
| 784 |
c = decode(s, i); |
| 785 |
encode(r, c); |
| 786 |
} |
| 787 |
} |
| 788 |
r ~= '\000'; |
| 789 |
return &r[0]; |
| 790 |
} |
| 791 |
|
| 792 |
/** ditto */ |
| 793 |
@safe pure nothrow |
| 794 |
wstring toUTF16(return scope wstring s) |
| 795 |
in |
| 796 |
{ |
| 797 |
assert(isValidString(s)); |
| 798 |
} |
| 799 |
do |
| 800 |
{ |
| 801 |
return s; |
| 802 |
} |
| 803 |
|
| 804 |
/** ditto */ |
| 805 |
@trusted pure nothrow |
| 806 |
wstring toUTF16(const scope dchar[] s) |
| 807 |
{ |
| 808 |
wchar[] r; |
| 809 |
size_t slen = s.length; |
| 810 |
|
| 811 |
if (!__ctfe) |
| 812 |
{ |
| 813 |
// Reserve still does a lot if slen is zero. |
| 814 |
// Return early for that case. |
| 815 |
if (0 == slen) |
| 816 |
return ""w; |
| 817 |
r.reserve(slen); |
| 818 |
} |
| 819 |
for (size_t i = 0; i < slen; i++) |
| 820 |
{ |
| 821 |
encode(r, s[i]); |
| 822 |
} |
| 823 |
return cast(wstring)r; |
| 824 |
} |
| 825 |
|
| 826 |
/* =================== Conversion to UTF32 ======================= */ |
| 827 |
|
| 828 |
/***** |
| 829 |
* Encodes string s into UTF-32 and returns the encoded string. |
| 830 |
*/ |
| 831 |
@trusted pure |
| 832 |
dstring toUTF32(const scope char[] s) |
| 833 |
{ |
| 834 |
dchar[] r; |
| 835 |
size_t slen = s.length; |
| 836 |
size_t j = 0; |
| 837 |
|
| 838 |
r.length = slen; // r[] will never be longer than s[] |
| 839 |
for (size_t i = 0; i < slen; ) |
| 840 |
{ |
| 841 |
dchar c = s[i]; |
| 842 |
if (c >= 0x80) |
| 843 |
c = decode(s, i); |
| 844 |
else |
| 845 |
i++; // c is ascii, no need for decode |
| 846 |
r[j++] = c; |
| 847 |
} |
| 848 |
return cast(dstring)r[0 .. j]; |
| 849 |
} |
| 850 |
|
| 851 |
/** ditto */ |
| 852 |
@trusted pure |
| 853 |
dstring toUTF32(const scope wchar[] s) |
| 854 |
{ |
| 855 |
dchar[] r; |
| 856 |
size_t slen = s.length; |
| 857 |
size_t j = 0; |
| 858 |
|
| 859 |
r.length = slen; // r[] will never be longer than s[] |
| 860 |
for (size_t i = 0; i < slen; ) |
| 861 |
{ |
| 862 |
dchar c = s[i]; |
| 863 |
if (c >= 0x80) |
| 864 |
c = decode(s, i); |
| 865 |
else |
| 866 |
i++; // c is ascii, no need for decode |
| 867 |
r[j++] = c; |
| 868 |
} |
| 869 |
return cast(dstring)r[0 .. j]; |
| 870 |
} |
| 871 |
|
| 872 |
/** ditto */ |
| 873 |
@safe pure nothrow |
| 874 |
dstring toUTF32(return scope dstring s) |
| 875 |
in |
| 876 |
{ |
| 877 |
assert(isValidString(s)); |
| 878 |
} |
| 879 |
do |
| 880 |
{ |
| 881 |
return s; |
| 882 |
} |
| 883 |
|
| 884 |
/* ================================ tests ================================== */ |
| 885 |
|
| 886 |
unittest |
| 887 |
{ |
| 888 |
debug(utf) printf("utf.toUTF.unittest\n"); |
| 889 |
|
| 890 |
auto c = "hello"c[]; |
| 891 |
auto w = toUTF16(c); |
| 892 |
assert(w == "hello"); |
| 893 |
auto d = toUTF32(c); |
| 894 |
assert(d == "hello"); |
| 895 |
|
| 896 |
c = toUTF8(w); |
| 897 |
assert(c == "hello"); |
| 898 |
d = toUTF32(w); |
| 899 |
assert(d == "hello"); |
| 900 |
|
| 901 |
c = toUTF8(d); |
| 902 |
assert(c == "hello"); |
| 903 |
w = toUTF16(d); |
| 904 |
assert(w == "hello"); |
| 905 |
|
| 906 |
|
| 907 |
c = "hel\u1234o"; |
| 908 |
w = toUTF16(c); |
| 909 |
assert(w == "hel\u1234o"); |
| 910 |
d = toUTF32(c); |
| 911 |
assert(d == "hel\u1234o"); |
| 912 |
|
| 913 |
c = toUTF8(w); |
| 914 |
assert(c == "hel\u1234o"); |
| 915 |
d = toUTF32(w); |
| 916 |
assert(d == "hel\u1234o"); |
| 917 |
|
| 918 |
c = toUTF8(d); |
| 919 |
assert(c == "hel\u1234o"); |
| 920 |
w = toUTF16(d); |
| 921 |
assert(w == "hel\u1234o"); |
| 922 |
|
| 923 |
|
| 924 |
c = "he\U000BAAAAllo"; |
| 925 |
w = toUTF16(c); |
| 926 |
//foreach (wchar c; w) printf("c = x%x\n", c); |
| 927 |
//foreach (wchar c; cast(wstring)"he\U000BAAAAllo") printf("c = x%x\n", c); |
| 928 |
assert(w == "he\U000BAAAAllo"); |
| 929 |
d = toUTF32(c); |
| 930 |
assert(d == "he\U000BAAAAllo"); |
| 931 |
|
| 932 |
c = toUTF8(w); |
| 933 |
assert(c == "he\U000BAAAAllo"); |
| 934 |
d = toUTF32(w); |
| 935 |
assert(d == "he\U000BAAAAllo"); |
| 936 |
|
| 937 |
c = toUTF8(d); |
| 938 |
assert(c == "he\U000BAAAAllo"); |
| 939 |
w = toUTF16(d); |
| 940 |
assert(w == "he\U000BAAAAllo"); |
| 941 |
|
| 942 |
wchar[2] buf; |
| 943 |
auto ret = toUTF16(buf, '\U000BAAAA'); |
| 944 |
assert(ret == "\U000BAAAA"); |
| 945 |
} |