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