| 1 |
/** |
| 2 |
* This code handles decoding UTF strings for `foreach_reverse` loops. |
| 3 |
* |
| 4 |
* Copyright: Copyright Digital Mars 2004 - 2010. |
| 5 |
* License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0). |
| 6 |
* Authors: Walter Bright, Sean Kelly |
| 7 |
* Source: $(DRUNTIMESRC rt/_aApplyR.d) |
| 8 |
*/ |
| 9 |
module rt.aApplyR; |
| 10 |
|
| 11 |
import core.internal.utf; |
| 12 |
|
| 13 |
debug (apply) import core.stdc.stdio : printf; |
| 14 |
|
| 15 |
/**********************************************/ |
| 16 |
/* 1 argument versions */ |
| 17 |
|
| 18 |
// Note: dg is extern(D), but _aApplyRcd() is extern(C) |
| 19 |
|
| 20 |
/** |
| 21 |
Delegate type corresponding to transformed loop body |
| 22 |
|
| 23 |
The parameter is a pointer to the current `char`, `wchar` or `dchar` |
| 24 |
|
| 25 |
Returns: non-zero when a `break` statement is hit |
| 26 |
*/ |
| 27 |
extern (D) alias dg_t = int delegate(void* c); |
| 28 |
|
| 29 |
/** |
| 30 |
Same as `_aApplyXXX` functions, but for `foreach_reverse` |
| 31 |
|
| 32 |
Params: |
| 33 |
aa = input string |
| 34 |
dg = foreach body transformed into a delegate, similar to `opApply` |
| 35 |
|
| 36 |
Returns: |
| 37 |
non-zero when the loop was exited through a `break` |
| 38 |
*/ |
| 39 |
extern (C) int _aApplyRcd1(scope const(char)[] aa, dg_t dg) |
| 40 |
{ int result; |
| 41 |
|
| 42 |
debug(apply) printf("_aApplyRcd1(), len = %zd\n", aa.length); |
| 43 |
for (size_t i = aa.length; i != 0; ) |
| 44 |
{ dchar d; |
| 45 |
|
| 46 |
i--; |
| 47 |
d = aa[i]; |
| 48 |
if (d & 0x80) |
| 49 |
{ char c = cast(char)d; |
| 50 |
uint j; |
| 51 |
uint m = 0x3F; |
| 52 |
d = 0; |
| 53 |
while ((c & 0xC0) != 0xC0) |
| 54 |
{ if (i == 0) |
| 55 |
onUnicodeError("Invalid UTF-8 sequence", 0); |
| 56 |
i--; |
| 57 |
d |= (c & 0x3F) << j; |
| 58 |
j += 6; |
| 59 |
m >>= 1; |
| 60 |
c = aa[i]; |
| 61 |
} |
| 62 |
d |= (c & m) << j; |
| 63 |
} |
| 64 |
result = dg(cast(void *)&d); |
| 65 |
if (result) |
| 66 |
break; |
| 67 |
} |
| 68 |
return result; |
| 69 |
} |
| 70 |
|
| 71 |
unittest |
| 72 |
{ |
| 73 |
debug(apply) printf("_aApplyRcd1.unittest\n"); |
| 74 |
|
| 75 |
auto s = "hello"c[]; |
| 76 |
int i; |
| 77 |
|
| 78 |
foreach_reverse (dchar d; s) |
| 79 |
{ |
| 80 |
switch (i) |
| 81 |
{ |
| 82 |
case 0: assert(d == 'o'); break; |
| 83 |
case 1: assert(d == 'l'); break; |
| 84 |
case 2: assert(d == 'l'); break; |
| 85 |
case 3: assert(d == 'e'); break; |
| 86 |
case 4: assert(d == 'h'); break; |
| 87 |
default: assert(0); |
| 88 |
} |
| 89 |
i++; |
| 90 |
} |
| 91 |
assert(i == 5); |
| 92 |
|
| 93 |
s = "a\u1234\U000A0456b"; |
| 94 |
i = 0; |
| 95 |
foreach_reverse (dchar d; s) |
| 96 |
{ |
| 97 |
//printf("i = %d, d = %x\n", i, d); |
| 98 |
switch (i) |
| 99 |
{ |
| 100 |
case 0: assert(d == 'b'); break; |
| 101 |
case 1: assert(d == '\U000A0456'); break; |
| 102 |
case 2: assert(d == '\u1234'); break; |
| 103 |
case 3: assert(d == 'a'); break; |
| 104 |
default: assert(0); |
| 105 |
} |
| 106 |
i++; |
| 107 |
} |
| 108 |
assert(i == 4); |
| 109 |
} |
| 110 |
|
| 111 |
/// ditto |
| 112 |
extern (C) int _aApplyRwd1(scope const(wchar)[] aa, dg_t dg) |
| 113 |
{ int result; |
| 114 |
|
| 115 |
debug(apply) printf("_aApplyRwd1(), len = %zd\n", aa.length); |
| 116 |
for (size_t i = aa.length; i != 0; ) |
| 117 |
{ dchar d; |
| 118 |
|
| 119 |
i--; |
| 120 |
d = aa[i]; |
| 121 |
if (d >= 0xDC00 && d <= 0xDFFF) |
| 122 |
{ if (i == 0) |
| 123 |
onUnicodeError("Invalid UTF-16 sequence", 0); |
| 124 |
i--; |
| 125 |
d = ((aa[i] - 0xD7C0) << 10) + (d - 0xDC00); |
| 126 |
} |
| 127 |
result = dg(cast(void *)&d); |
| 128 |
if (result) |
| 129 |
break; |
| 130 |
} |
| 131 |
return result; |
| 132 |
} |
| 133 |
|
| 134 |
unittest |
| 135 |
{ |
| 136 |
debug(apply) printf("_aApplyRwd1.unittest\n"); |
| 137 |
|
| 138 |
auto s = "hello"w[]; |
| 139 |
int i; |
| 140 |
|
| 141 |
foreach_reverse (dchar d; s) |
| 142 |
{ |
| 143 |
switch (i) |
| 144 |
{ |
| 145 |
case 0: assert(d == 'o'); break; |
| 146 |
case 1: assert(d == 'l'); break; |
| 147 |
case 2: assert(d == 'l'); break; |
| 148 |
case 3: assert(d == 'e'); break; |
| 149 |
case 4: assert(d == 'h'); break; |
| 150 |
default: assert(0); |
| 151 |
} |
| 152 |
i++; |
| 153 |
} |
| 154 |
assert(i == 5); |
| 155 |
|
| 156 |
s = "a\u1234\U000A0456b"; |
| 157 |
i = 0; |
| 158 |
foreach_reverse (dchar d; s) |
| 159 |
{ |
| 160 |
//printf("i = %d, d = %x\n", i, d); |
| 161 |
switch (i) |
| 162 |
{ |
| 163 |
case 0: assert(d == 'b'); break; |
| 164 |
case 1: assert(d == '\U000A0456'); break; |
| 165 |
case 2: assert(d == '\u1234'); break; |
| 166 |
case 3: assert(d == 'a'); break; |
| 167 |
default: assert(0); |
| 168 |
} |
| 169 |
i++; |
| 170 |
} |
| 171 |
assert(i == 4); |
| 172 |
} |
| 173 |
|
| 174 |
/// ditto |
| 175 |
extern (C) int _aApplyRcw1(scope const(char)[] aa, dg_t dg) |
| 176 |
{ int result; |
| 177 |
|
| 178 |
debug(apply) printf("_aApplyRcw1(), len = %zd\n", aa.length); |
| 179 |
for (size_t i = aa.length; i != 0; ) |
| 180 |
{ dchar d; |
| 181 |
wchar w; |
| 182 |
|
| 183 |
i--; |
| 184 |
w = aa[i]; |
| 185 |
if (w & 0x80) |
| 186 |
{ char c = cast(char)w; |
| 187 |
uint j; |
| 188 |
uint m = 0x3F; |
| 189 |
d = 0; |
| 190 |
while ((c & 0xC0) != 0xC0) |
| 191 |
{ if (i == 0) |
| 192 |
onUnicodeError("Invalid UTF-8 sequence", 0); |
| 193 |
i--; |
| 194 |
d |= (c & 0x3F) << j; |
| 195 |
j += 6; |
| 196 |
m >>= 1; |
| 197 |
c = aa[i]; |
| 198 |
} |
| 199 |
d |= (c & m) << j; |
| 200 |
|
| 201 |
if (d <= 0xFFFF) |
| 202 |
w = cast(wchar) d; |
| 203 |
else |
| 204 |
{ |
| 205 |
w = cast(wchar) ((((d - 0x10000) >> 10) & 0x3FF) + 0xD800); |
| 206 |
result = dg(cast(void *)&w); |
| 207 |
if (result) |
| 208 |
break; |
| 209 |
w = cast(wchar) (((d - 0x10000) & 0x3FF) + 0xDC00); |
| 210 |
} |
| 211 |
} |
| 212 |
result = dg(cast(void *)&w); |
| 213 |
if (result) |
| 214 |
break; |
| 215 |
} |
| 216 |
return result; |
| 217 |
} |
| 218 |
|
| 219 |
unittest |
| 220 |
{ |
| 221 |
debug(apply) printf("_aApplyRcw1.unittest\n"); |
| 222 |
|
| 223 |
auto s = "hello"c[]; |
| 224 |
int i; |
| 225 |
|
| 226 |
foreach_reverse (wchar d; s) |
| 227 |
{ |
| 228 |
switch (i) |
| 229 |
{ |
| 230 |
case 0: assert(d == 'o'); break; |
| 231 |
case 1: assert(d == 'l'); break; |
| 232 |
case 2: assert(d == 'l'); break; |
| 233 |
case 3: assert(d == 'e'); break; |
| 234 |
case 4: assert(d == 'h'); break; |
| 235 |
default: assert(0); |
| 236 |
} |
| 237 |
i++; |
| 238 |
} |
| 239 |
assert(i == 5); |
| 240 |
|
| 241 |
s = "a\u1234\U000A0456b"; |
| 242 |
i = 0; |
| 243 |
foreach_reverse (wchar d; s) |
| 244 |
{ |
| 245 |
//printf("i = %d, d = %x\n", i, d); |
| 246 |
switch (i) |
| 247 |
{ |
| 248 |
case 0: assert(d == 'b'); break; |
| 249 |
case 1: assert(d == 0xDA41); break; |
| 250 |
case 2: assert(d == 0xDC56); break; |
| 251 |
case 3: assert(d == 0x1234); break; |
| 252 |
case 4: assert(d == 'a'); break; |
| 253 |
default: assert(0); |
| 254 |
} |
| 255 |
i++; |
| 256 |
} |
| 257 |
assert(i == 5); |
| 258 |
} |
| 259 |
|
| 260 |
/// ditto |
| 261 |
extern (C) int _aApplyRwc1(scope const(wchar)[] aa, dg_t dg) |
| 262 |
{ int result; |
| 263 |
|
| 264 |
debug(apply) printf("_aApplyRwc1(), len = %zd\n", aa.length); |
| 265 |
for (size_t i = aa.length; i != 0; ) |
| 266 |
{ dchar d; |
| 267 |
char c; |
| 268 |
|
| 269 |
i--; |
| 270 |
d = aa[i]; |
| 271 |
if (d >= 0xDC00 && d <= 0xDFFF) |
| 272 |
{ if (i == 0) |
| 273 |
onUnicodeError("Invalid UTF-16 sequence", 0); |
| 274 |
i--; |
| 275 |
d = ((aa[i] - 0xD7C0) << 10) + (d - 0xDC00); |
| 276 |
} |
| 277 |
|
| 278 |
if (d & ~0x7F) |
| 279 |
{ |
| 280 |
char[4] buf = void; |
| 281 |
|
| 282 |
auto b = toUTF8(buf, d); |
| 283 |
foreach (char c2; b) |
| 284 |
{ |
| 285 |
result = dg(cast(void *)&c2); |
| 286 |
if (result) |
| 287 |
return result; |
| 288 |
} |
| 289 |
continue; |
| 290 |
} |
| 291 |
c = cast(char)d; |
| 292 |
result = dg(cast(void *)&c); |
| 293 |
if (result) |
| 294 |
break; |
| 295 |
} |
| 296 |
return result; |
| 297 |
} |
| 298 |
|
| 299 |
unittest |
| 300 |
{ |
| 301 |
debug(apply) printf("_aApplyRwc1.unittest\n"); |
| 302 |
|
| 303 |
auto s = "hello"w[]; |
| 304 |
int i; |
| 305 |
|
| 306 |
foreach_reverse (char d; s) |
| 307 |
{ |
| 308 |
switch (i) |
| 309 |
{ |
| 310 |
case 0: assert(d == 'o'); break; |
| 311 |
case 1: assert(d == 'l'); break; |
| 312 |
case 2: assert(d == 'l'); break; |
| 313 |
case 3: assert(d == 'e'); break; |
| 314 |
case 4: assert(d == 'h'); break; |
| 315 |
default: assert(0); |
| 316 |
} |
| 317 |
i++; |
| 318 |
} |
| 319 |
assert(i == 5); |
| 320 |
|
| 321 |
s = "a\u1234\U000A0456b"; |
| 322 |
i = 0; |
| 323 |
foreach_reverse (char d; s) |
| 324 |
{ |
| 325 |
//printf("i = %d, d = %x\n", i, d); |
| 326 |
switch (i) |
| 327 |
{ |
| 328 |
case 0: assert(d == 'b'); break; |
| 329 |
case 1: assert(d == 0xF2); break; |
| 330 |
case 2: assert(d == 0xA0); break; |
| 331 |
case 3: assert(d == 0x91); break; |
| 332 |
case 4: assert(d == 0x96); break; |
| 333 |
case 5: assert(d == 0xE1); break; |
| 334 |
case 6: assert(d == 0x88); break; |
| 335 |
case 7: assert(d == 0xB4); break; |
| 336 |
case 8: assert(d == 'a'); break; |
| 337 |
default: assert(0); |
| 338 |
} |
| 339 |
i++; |
| 340 |
} |
| 341 |
assert(i == 9); |
| 342 |
} |
| 343 |
|
| 344 |
/// ditto |
| 345 |
extern (C) int _aApplyRdc1(scope const(dchar)[] aa, dg_t dg) |
| 346 |
{ int result; |
| 347 |
|
| 348 |
debug(apply) printf("_aApplyRdc1(), len = %zd\n", aa.length); |
| 349 |
for (size_t i = aa.length; i != 0;) |
| 350 |
{ dchar d = aa[--i]; |
| 351 |
char c; |
| 352 |
|
| 353 |
if (d & ~0x7F) |
| 354 |
{ |
| 355 |
char[4] buf = void; |
| 356 |
|
| 357 |
auto b = toUTF8(buf, d); |
| 358 |
foreach (char c2; b) |
| 359 |
{ |
| 360 |
result = dg(cast(void *)&c2); |
| 361 |
if (result) |
| 362 |
return result; |
| 363 |
} |
| 364 |
continue; |
| 365 |
} |
| 366 |
else |
| 367 |
{ |
| 368 |
c = cast(char)d; |
| 369 |
} |
| 370 |
result = dg(cast(void *)&c); |
| 371 |
if (result) |
| 372 |
break; |
| 373 |
} |
| 374 |
return result; |
| 375 |
} |
| 376 |
|
| 377 |
unittest |
| 378 |
{ |
| 379 |
debug(apply) printf("_aApplyRdc1.unittest\n"); |
| 380 |
|
| 381 |
auto s = "hello"d[]; |
| 382 |
int i; |
| 383 |
|
| 384 |
foreach_reverse (char d; s) |
| 385 |
{ |
| 386 |
switch (i) |
| 387 |
{ |
| 388 |
case 0: assert(d == 'o'); break; |
| 389 |
case 1: assert(d == 'l'); break; |
| 390 |
case 2: assert(d == 'l'); break; |
| 391 |
case 3: assert(d == 'e'); break; |
| 392 |
case 4: assert(d == 'h'); break; |
| 393 |
default: assert(0); |
| 394 |
} |
| 395 |
i++; |
| 396 |
} |
| 397 |
assert(i == 5); |
| 398 |
|
| 399 |
s = "a\u1234\U000A0456b"; |
| 400 |
i = 0; |
| 401 |
foreach_reverse (char d; s) |
| 402 |
{ |
| 403 |
//printf("i = %d, d = %x\n", i, d); |
| 404 |
switch (i) |
| 405 |
{ |
| 406 |
case 0: assert(d == 'b'); break; |
| 407 |
case 1: assert(d == 0xF2); break; |
| 408 |
case 2: assert(d == 0xA0); break; |
| 409 |
case 3: assert(d == 0x91); break; |
| 410 |
case 4: assert(d == 0x96); break; |
| 411 |
case 5: assert(d == 0xE1); break; |
| 412 |
case 6: assert(d == 0x88); break; |
| 413 |
case 7: assert(d == 0xB4); break; |
| 414 |
case 8: assert(d == 'a'); break; |
| 415 |
default: assert(0); |
| 416 |
} |
| 417 |
i++; |
| 418 |
} |
| 419 |
assert(i == 9); |
| 420 |
} |
| 421 |
|
| 422 |
/// ditto |
| 423 |
extern (C) int _aApplyRdw1(scope const(dchar)[] aa, dg_t dg) |
| 424 |
{ int result; |
| 425 |
|
| 426 |
debug(apply) printf("_aApplyRdw1(), len = %zd\n", aa.length); |
| 427 |
for (size_t i = aa.length; i != 0; ) |
| 428 |
{ dchar d = aa[--i]; |
| 429 |
wchar w; |
| 430 |
|
| 431 |
if (d <= 0xFFFF) |
| 432 |
w = cast(wchar) d; |
| 433 |
else |
| 434 |
{ |
| 435 |
w = cast(wchar) ((((d - 0x10000) >> 10) & 0x3FF) + 0xD800); |
| 436 |
result = dg(cast(void *)&w); |
| 437 |
if (result) |
| 438 |
break; |
| 439 |
w = cast(wchar) (((d - 0x10000) & 0x3FF) + 0xDC00); |
| 440 |
} |
| 441 |
result = dg(cast(void *)&w); |
| 442 |
if (result) |
| 443 |
break; |
| 444 |
} |
| 445 |
return result; |
| 446 |
} |
| 447 |
|
| 448 |
unittest |
| 449 |
{ |
| 450 |
debug(apply) printf("_aApplyRdw1.unittest\n"); |
| 451 |
|
| 452 |
auto s = "hello"d[]; |
| 453 |
int i; |
| 454 |
|
| 455 |
foreach_reverse (wchar d; s) |
| 456 |
{ |
| 457 |
switch (i) |
| 458 |
{ |
| 459 |
case 0: assert(d == 'o'); break; |
| 460 |
case 1: assert(d == 'l'); break; |
| 461 |
case 2: assert(d == 'l'); break; |
| 462 |
case 3: assert(d == 'e'); break; |
| 463 |
case 4: assert(d == 'h'); break; |
| 464 |
default: assert(0); |
| 465 |
} |
| 466 |
i++; |
| 467 |
} |
| 468 |
assert(i == 5); |
| 469 |
|
| 470 |
s = "a\u1234\U000A0456b"; |
| 471 |
i = 0; |
| 472 |
foreach_reverse (wchar d; s) |
| 473 |
{ |
| 474 |
//printf("i = %d, d = %x\n", i, d); |
| 475 |
switch (i) |
| 476 |
{ |
| 477 |
case 0: assert(d == 'b'); break; |
| 478 |
case 1: assert(d == 0xDA41); break; |
| 479 |
case 2: assert(d == 0xDC56); break; |
| 480 |
case 3: assert(d == 0x1234); break; |
| 481 |
case 4: assert(d == 'a'); break; |
| 482 |
default: assert(0); |
| 483 |
} |
| 484 |
i++; |
| 485 |
} |
| 486 |
assert(i == 5); |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
/****************************************************************************/ |
| 491 |
/* 2 argument versions */ |
| 492 |
|
| 493 |
/** |
| 494 |
Delegate type corresponding to transformed loop body |
| 495 |
|
| 496 |
Parameters are pointers to a `size_t` loop index, and the current `char`, `wchar` or `dchar`. |
| 497 |
|
| 498 |
Returns: non-zero when a `break` statement is hit |
| 499 |
*/ |
| 500 |
extern (D) alias dg2_t = int delegate(void* i, void* c); |
| 501 |
|
| 502 |
// Note: dg is extern(D), but _aApplyRcd2() is extern(C) |
| 503 |
|
| 504 |
/** |
| 505 |
Variants of _aApplyRXXX that include a loop index. |
| 506 |
*/ |
| 507 |
extern (C) int _aApplyRcd2(scope const(char)[] aa, dg2_t dg) |
| 508 |
{ int result; |
| 509 |
size_t i; |
| 510 |
size_t len = aa.length; |
| 511 |
|
| 512 |
debug(apply) printf("_aApplyRcd2(), len = %zd\n", len); |
| 513 |
for (i = len; i != 0; ) |
| 514 |
{ dchar d; |
| 515 |
|
| 516 |
i--; |
| 517 |
d = aa[i]; |
| 518 |
if (d & 0x80) |
| 519 |
{ char c = cast(char)d; |
| 520 |
uint j; |
| 521 |
uint m = 0x3F; |
| 522 |
d = 0; |
| 523 |
while ((c & 0xC0) != 0xC0) |
| 524 |
{ if (i == 0) |
| 525 |
onUnicodeError("Invalid UTF-8 sequence", 0); |
| 526 |
i--; |
| 527 |
d |= (c & 0x3F) << j; |
| 528 |
j += 6; |
| 529 |
m >>= 1; |
| 530 |
c = aa[i]; |
| 531 |
} |
| 532 |
d |= (c & m) << j; |
| 533 |
} |
| 534 |
result = dg(&i, cast(void *)&d); |
| 535 |
if (result) |
| 536 |
break; |
| 537 |
} |
| 538 |
return result; |
| 539 |
} |
| 540 |
|
| 541 |
unittest |
| 542 |
{ |
| 543 |
debug(apply) printf("_aApplyRcd2.unittest\n"); |
| 544 |
|
| 545 |
auto s = "hello"c[]; |
| 546 |
int i; |
| 547 |
|
| 548 |
foreach_reverse (k, dchar d; s) |
| 549 |
{ |
| 550 |
assert(k == 4 - i); |
| 551 |
switch (i) |
| 552 |
{ |
| 553 |
case 0: assert(d == 'o'); break; |
| 554 |
case 1: assert(d == 'l'); break; |
| 555 |
case 2: assert(d == 'l'); break; |
| 556 |
case 3: assert(d == 'e'); break; |
| 557 |
case 4: assert(d == 'h'); break; |
| 558 |
default: assert(0); |
| 559 |
} |
| 560 |
i++; |
| 561 |
} |
| 562 |
assert(i == 5); |
| 563 |
|
| 564 |
s = "a\u1234\U000A0456b"; |
| 565 |
i = 0; |
| 566 |
foreach_reverse (k, dchar d; s) |
| 567 |
{ |
| 568 |
//printf("i = %d, k = %d, d = %x\n", i, k, d); |
| 569 |
switch (i) |
| 570 |
{ |
| 571 |
case 0: assert(d == 'b'); assert(k == 8); break; |
| 572 |
case 1: assert(d == '\U000A0456'); assert(k == 4); break; |
| 573 |
case 2: assert(d == '\u1234'); assert(k == 1); break; |
| 574 |
case 3: assert(d == 'a'); assert(k == 0); break; |
| 575 |
default: assert(0); |
| 576 |
} |
| 577 |
i++; |
| 578 |
} |
| 579 |
assert(i == 4); |
| 580 |
} |
| 581 |
|
| 582 |
/// ditto |
| 583 |
extern (C) int _aApplyRwd2(scope const(wchar)[] aa, dg2_t dg) |
| 584 |
{ int result; |
| 585 |
|
| 586 |
debug(apply) printf("_aApplyRwd2(), len = %zd\n", aa.length); |
| 587 |
for (size_t i = aa.length; i != 0; ) |
| 588 |
{ dchar d; |
| 589 |
|
| 590 |
i--; |
| 591 |
d = aa[i]; |
| 592 |
if (d >= 0xDC00 && d <= 0xDFFF) |
| 593 |
{ if (i == 0) |
| 594 |
onUnicodeError("Invalid UTF-16 sequence", 0); |
| 595 |
i--; |
| 596 |
d = ((aa[i] - 0xD7C0) << 10) + (d - 0xDC00); |
| 597 |
} |
| 598 |
result = dg(&i, cast(void *)&d); |
| 599 |
if (result) |
| 600 |
break; |
| 601 |
} |
| 602 |
return result; |
| 603 |
} |
| 604 |
|
| 605 |
unittest |
| 606 |
{ |
| 607 |
debug(apply) printf("_aApplyRwd2.unittest\n"); |
| 608 |
|
| 609 |
auto s = "hello"w[]; |
| 610 |
int i; |
| 611 |
|
| 612 |
foreach_reverse (k, dchar d; s) |
| 613 |
{ |
| 614 |
//printf("i = %d, k = %d, d = %x\n", i, k, d); |
| 615 |
assert(k == 4 - i); |
| 616 |
switch (i) |
| 617 |
{ |
| 618 |
case 0: assert(d == 'o'); break; |
| 619 |
case 1: assert(d == 'l'); break; |
| 620 |
case 2: assert(d == 'l'); break; |
| 621 |
case 3: assert(d == 'e'); break; |
| 622 |
case 4: assert(d == 'h'); break; |
| 623 |
default: assert(0); |
| 624 |
} |
| 625 |
i++; |
| 626 |
} |
| 627 |
assert(i == 5); |
| 628 |
|
| 629 |
s = "a\u1234\U000A0456b"; |
| 630 |
i = 0; |
| 631 |
foreach_reverse (k, dchar d; s) |
| 632 |
{ |
| 633 |
//printf("i = %d, k = %d, d = %x\n", i, k, d); |
| 634 |
switch (i) |
| 635 |
{ |
| 636 |
case 0: assert(k == 4); assert(d == 'b'); break; |
| 637 |
case 1: assert(k == 2); assert(d == '\U000A0456'); break; |
| 638 |
case 2: assert(k == 1); assert(d == '\u1234'); break; |
| 639 |
case 3: assert(k == 0); assert(d == 'a'); break; |
| 640 |
default: assert(0); |
| 641 |
} |
| 642 |
i++; |
| 643 |
} |
| 644 |
assert(i == 4); |
| 645 |
} |
| 646 |
|
| 647 |
/// ditto |
| 648 |
extern (C) int _aApplyRcw2(scope const(char)[] aa, dg2_t dg) |
| 649 |
{ int result; |
| 650 |
|
| 651 |
debug(apply) printf("_aApplyRcw2(), len = %zd\n", aa.length); |
| 652 |
for (size_t i = aa.length; i != 0; ) |
| 653 |
{ dchar d; |
| 654 |
wchar w; |
| 655 |
|
| 656 |
i--; |
| 657 |
w = aa[i]; |
| 658 |
if (w & 0x80) |
| 659 |
{ char c = cast(char)w; |
| 660 |
uint j; |
| 661 |
uint m = 0x3F; |
| 662 |
d = 0; |
| 663 |
while ((c & 0xC0) != 0xC0) |
| 664 |
{ if (i == 0) |
| 665 |
onUnicodeError("Invalid UTF-8 sequence", 0); |
| 666 |
i--; |
| 667 |
d |= (c & 0x3F) << j; |
| 668 |
j += 6; |
| 669 |
m >>= 1; |
| 670 |
c = aa[i]; |
| 671 |
} |
| 672 |
d |= (c & m) << j; |
| 673 |
|
| 674 |
if (d <= 0xFFFF) |
| 675 |
w = cast(wchar) d; |
| 676 |
else |
| 677 |
{ |
| 678 |
w = cast(wchar) ((((d - 0x10000) >> 10) & 0x3FF) + 0xD800); |
| 679 |
result = dg(&i, cast(void *)&w); |
| 680 |
if (result) |
| 681 |
break; |
| 682 |
w = cast(wchar) (((d - 0x10000) & 0x3FF) + 0xDC00); |
| 683 |
} |
| 684 |
} |
| 685 |
result = dg(&i, cast(void *)&w); |
| 686 |
if (result) |
| 687 |
break; |
| 688 |
} |
| 689 |
return result; |
| 690 |
} |
| 691 |
|
| 692 |
unittest |
| 693 |
{ |
| 694 |
debug(apply) printf("_aApplyRcw2.unittest\n"); |
| 695 |
|
| 696 |
auto s = "hello"c[]; |
| 697 |
int i; |
| 698 |
|
| 699 |
foreach_reverse (k, wchar d; s) |
| 700 |
{ |
| 701 |
//printf("i = %d, k = %d, d = %x\n", i, k, d); |
| 702 |
assert(k == 4 - i); |
| 703 |
switch (i) |
| 704 |
{ |
| 705 |
case 0: assert(d == 'o'); break; |
| 706 |
case 1: assert(d == 'l'); break; |
| 707 |
case 2: assert(d == 'l'); break; |
| 708 |
case 3: assert(d == 'e'); break; |
| 709 |
case 4: assert(d == 'h'); break; |
| 710 |
default: assert(0); |
| 711 |
} |
| 712 |
i++; |
| 713 |
} |
| 714 |
assert(i == 5); |
| 715 |
|
| 716 |
s = "a\u1234\U000A0456b"; |
| 717 |
i = 0; |
| 718 |
foreach_reverse (k, wchar d; s) |
| 719 |
{ |
| 720 |
//printf("i = %d, k = %d, d = %x\n", i, k, d); |
| 721 |
switch (i) |
| 722 |
{ |
| 723 |
case 0: assert(k == 8); assert(d == 'b'); break; |
| 724 |
case 1: assert(k == 4); assert(d == 0xDA41); break; |
| 725 |
case 2: assert(k == 4); assert(d == 0xDC56); break; |
| 726 |
case 3: assert(k == 1); assert(d == 0x1234); break; |
| 727 |
case 4: assert(k == 0); assert(d == 'a'); break; |
| 728 |
default: assert(0); |
| 729 |
} |
| 730 |
i++; |
| 731 |
} |
| 732 |
assert(i == 5); |
| 733 |
} |
| 734 |
|
| 735 |
/// ditto |
| 736 |
extern (C) int _aApplyRwc2(scope const(wchar)[] aa, dg2_t dg) |
| 737 |
{ int result; |
| 738 |
|
| 739 |
debug(apply) printf("_aApplyRwc2(), len = %zd\n", aa.length); |
| 740 |
for (size_t i = aa.length; i != 0; ) |
| 741 |
{ dchar d; |
| 742 |
char c; |
| 743 |
|
| 744 |
i--; |
| 745 |
d = aa[i]; |
| 746 |
if (d >= 0xDC00 && d <= 0xDFFF) |
| 747 |
{ if (i == 0) |
| 748 |
onUnicodeError("Invalid UTF-16 sequence", 0); |
| 749 |
i--; |
| 750 |
d = ((aa[i] - 0xD7C0) << 10) + (d - 0xDC00); |
| 751 |
} |
| 752 |
|
| 753 |
if (d & ~0x7F) |
| 754 |
{ |
| 755 |
char[4] buf = void; |
| 756 |
|
| 757 |
auto b = toUTF8(buf, d); |
| 758 |
foreach (char c2; b) |
| 759 |
{ |
| 760 |
result = dg(&i, cast(void *)&c2); |
| 761 |
if (result) |
| 762 |
return result; |
| 763 |
} |
| 764 |
continue; |
| 765 |
} |
| 766 |
c = cast(char)d; |
| 767 |
result = dg(&i, cast(void *)&c); |
| 768 |
if (result) |
| 769 |
break; |
| 770 |
} |
| 771 |
return result; |
| 772 |
} |
| 773 |
|
| 774 |
unittest |
| 775 |
{ |
| 776 |
debug(apply) printf("_aApplyRwc2.unittest\n"); |
| 777 |
|
| 778 |
auto s = "hello"w[]; |
| 779 |
int i; |
| 780 |
|
| 781 |
foreach_reverse (k, char d; s) |
| 782 |
{ |
| 783 |
//printf("i = %d, k = %d, d = %x\n", i, k, d); |
| 784 |
assert(k == 4 - i); |
| 785 |
switch (i) |
| 786 |
{ |
| 787 |
case 0: assert(d == 'o'); break; |
| 788 |
case 1: assert(d == 'l'); break; |
| 789 |
case 2: assert(d == 'l'); break; |
| 790 |
case 3: assert(d == 'e'); break; |
| 791 |
case 4: assert(d == 'h'); break; |
| 792 |
default: assert(0); |
| 793 |
} |
| 794 |
i++; |
| 795 |
} |
| 796 |
assert(i == 5); |
| 797 |
|
| 798 |
s = "a\u1234\U000A0456b"; |
| 799 |
i = 0; |
| 800 |
foreach_reverse (k, char d; s) |
| 801 |
{ |
| 802 |
//printf("i = %d, k = %d, d = %x\n", i, k, d); |
| 803 |
switch (i) |
| 804 |
{ |
| 805 |
case 0: assert(k == 4); assert(d == 'b'); break; |
| 806 |
case 1: assert(k == 2); assert(d == 0xF2); break; |
| 807 |
case 2: assert(k == 2); assert(d == 0xA0); break; |
| 808 |
case 3: assert(k == 2); assert(d == 0x91); break; |
| 809 |
case 4: assert(k == 2); assert(d == 0x96); break; |
| 810 |
case 5: assert(k == 1); assert(d == 0xE1); break; |
| 811 |
case 6: assert(k == 1); assert(d == 0x88); break; |
| 812 |
case 7: assert(k == 1); assert(d == 0xB4); break; |
| 813 |
case 8: assert(k == 0); assert(d == 'a'); break; |
| 814 |
default: assert(0); |
| 815 |
} |
| 816 |
i++; |
| 817 |
} |
| 818 |
assert(i == 9); |
| 819 |
} |
| 820 |
|
| 821 |
/// ditto |
| 822 |
extern (C) int _aApplyRdc2(scope const(dchar)[] aa, dg2_t dg) |
| 823 |
{ int result; |
| 824 |
|
| 825 |
debug(apply) printf("_aApplyRdc2(), len = %zd\n", aa.length); |
| 826 |
for (size_t i = aa.length; i != 0; ) |
| 827 |
{ dchar d = aa[--i]; |
| 828 |
char c; |
| 829 |
|
| 830 |
if (d & ~0x7F) |
| 831 |
{ |
| 832 |
char[4] buf = void; |
| 833 |
|
| 834 |
auto b = toUTF8(buf, d); |
| 835 |
foreach (char c2; b) |
| 836 |
{ |
| 837 |
result = dg(&i, cast(void *)&c2); |
| 838 |
if (result) |
| 839 |
return result; |
| 840 |
} |
| 841 |
continue; |
| 842 |
} |
| 843 |
else |
| 844 |
{ c = cast(char)d; |
| 845 |
} |
| 846 |
result = dg(&i, cast(void *)&c); |
| 847 |
if (result) |
| 848 |
break; |
| 849 |
} |
| 850 |
return result; |
| 851 |
} |
| 852 |
|
| 853 |
unittest |
| 854 |
{ |
| 855 |
debug(apply) printf("_aApplyRdc2.unittest\n"); |
| 856 |
|
| 857 |
auto s = "hello"d[]; |
| 858 |
int i; |
| 859 |
|
| 860 |
foreach_reverse (k, char d; s) |
| 861 |
{ |
| 862 |
//printf("i = %d, k = %d, d = %x\n", i, k, d); |
| 863 |
assert(k == 4 - i); |
| 864 |
switch (i) |
| 865 |
{ |
| 866 |
case 0: assert(d == 'o'); break; |
| 867 |
case 1: assert(d == 'l'); break; |
| 868 |
case 2: assert(d == 'l'); break; |
| 869 |
case 3: assert(d == 'e'); break; |
| 870 |
case 4: assert(d == 'h'); break; |
| 871 |
default: assert(0); |
| 872 |
} |
| 873 |
i++; |
| 874 |
} |
| 875 |
assert(i == 5); |
| 876 |
|
| 877 |
s = "a\u1234\U000A0456b"; |
| 878 |
i = 0; |
| 879 |
foreach_reverse (k, char d; s) |
| 880 |
{ |
| 881 |
//printf("i = %d, k = %d, d = %x\n", i, k, d); |
| 882 |
switch (i) |
| 883 |
{ |
| 884 |
case 0: assert(k == 3); assert(d == 'b'); break; |
| 885 |
case 1: assert(k == 2); assert(d == 0xF2); break; |
| 886 |
case 2: assert(k == 2); assert(d == 0xA0); break; |
| 887 |
case 3: assert(k == 2); assert(d == 0x91); break; |
| 888 |
case 4: assert(k == 2); assert(d == 0x96); break; |
| 889 |
case 5: assert(k == 1); assert(d == 0xE1); break; |
| 890 |
case 6: assert(k == 1); assert(d == 0x88); break; |
| 891 |
case 7: assert(k == 1); assert(d == 0xB4); break; |
| 892 |
case 8: assert(k == 0); assert(d == 'a'); break; |
| 893 |
default: assert(0); |
| 894 |
} |
| 895 |
i++; |
| 896 |
} |
| 897 |
assert(i == 9); |
| 898 |
} |
| 899 |
|
| 900 |
/// ditto |
| 901 |
extern (C) int _aApplyRdw2(scope const(dchar)[] aa, dg2_t dg) |
| 902 |
{ int result; |
| 903 |
|
| 904 |
debug(apply) printf("_aApplyRdw2(), len = %zd\n", aa.length); |
| 905 |
for (size_t i = aa.length; i != 0; ) |
| 906 |
{ dchar d = aa[--i]; |
| 907 |
wchar w; |
| 908 |
|
| 909 |
if (d <= 0xFFFF) |
| 910 |
w = cast(wchar) d; |
| 911 |
else |
| 912 |
{ |
| 913 |
w = cast(wchar) ((((d - 0x10000) >> 10) & 0x3FF) + 0xD800); |
| 914 |
result = dg(&i, cast(void *)&w); |
| 915 |
if (result) |
| 916 |
break; |
| 917 |
w = cast(wchar) (((d - 0x10000) & 0x3FF) + 0xDC00); |
| 918 |
} |
| 919 |
result = dg(&i, cast(void *)&w); |
| 920 |
if (result) |
| 921 |
break; |
| 922 |
} |
| 923 |
return result; |
| 924 |
} |
| 925 |
|
| 926 |
unittest |
| 927 |
{ |
| 928 |
debug(apply) printf("_aApplyRdw2.unittest\n"); |
| 929 |
|
| 930 |
auto s = "hello"d[]; |
| 931 |
int i; |
| 932 |
|
| 933 |
foreach_reverse (k, wchar d; s) |
| 934 |
{ |
| 935 |
//printf("i = %d, k = %d, d = %x\n", i, k, d); |
| 936 |
assert(k == 4 - i); |
| 937 |
switch (i) |
| 938 |
{ |
| 939 |
case 0: assert(d == 'o'); break; |
| 940 |
case 1: assert(d == 'l'); break; |
| 941 |
case 2: assert(d == 'l'); break; |
| 942 |
case 3: assert(d == 'e'); break; |
| 943 |
case 4: assert(d == 'h'); break; |
| 944 |
default: assert(0); |
| 945 |
} |
| 946 |
i++; |
| 947 |
} |
| 948 |
assert(i == 5); |
| 949 |
|
| 950 |
s = "a\u1234\U000A0456b"; |
| 951 |
i = 0; |
| 952 |
foreach_reverse (k, wchar d; s) |
| 953 |
{ |
| 954 |
//printf("i = %d, k = %d, d = %x\n", i, k, d); |
| 955 |
switch (i) |
| 956 |
{ |
| 957 |
case 0: assert(k == 3); assert(d == 'b'); break; |
| 958 |
case 1: assert(k == 2); assert(d == 0xDA41); break; |
| 959 |
case 2: assert(k == 2); assert(d == 0xDC56); break; |
| 960 |
case 3: assert(k == 1); assert(d == 0x1234); break; |
| 961 |
case 4: assert(k == 0); assert(d == 'a'); break; |
| 962 |
default: assert(0); |
| 963 |
} |
| 964 |
i++; |
| 965 |
} |
| 966 |
assert(i == 5); |
| 967 |
} |