| 1 |
pure @safe unittest |
| 2 |
{ |
| 3 |
import std.uni; |
| 4 |
|
| 5 |
import std.algorithm.comparison : equal; |
| 6 |
|
| 7 |
auto set = CodepointSet('a', 'z'+1, 'а', 'я'+1); |
| 8 |
foreach (v; 'a'..'z'+1) |
| 9 |
assert(set[v]); |
| 10 |
// Cyrillic lowercase interval |
| 11 |
foreach (v; 'а'..'я'+1) |
| 12 |
assert(set[v]); |
| 13 |
//specific order is not required, intervals may interesect |
| 14 |
auto set2 = CodepointSet('а', 'я'+1, 'a', 'd', 'b', 'z'+1); |
| 15 |
//the same end result |
| 16 |
assert(set2.byInterval.equal(set.byInterval)); |
| 17 |
// test constructor this(Range)(Range intervals) |
| 18 |
auto chessPiecesWhite = CodepointInterval(9812, 9818); |
| 19 |
auto chessPiecesBlack = CodepointInterval(9818, 9824); |
| 20 |
auto set3 = CodepointSet([chessPiecesWhite, chessPiecesBlack]); |
| 21 |
foreach (v; '♔'..'♟'+1) |
| 22 |
assert(set3[v]); |
| 23 |
|
| 24 |
} |
| 25 |
|
| 26 |
pure @safe unittest |
| 27 |
{ |
| 28 |
import std.uni; |
| 29 |
|
| 30 |
auto gothic = unicode.Gothic; |
| 31 |
// Gothic letter ahsa |
| 32 |
assert(gothic['\U00010330']); |
| 33 |
// no ascii in Gothic obviously |
| 34 |
assert(!gothic['$']); |
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
pure @safe unittest |
| 39 |
{ |
| 40 |
import std.uni; |
| 41 |
|
| 42 |
import std.algorithm.comparison : equal; |
| 43 |
import std.range : iota; |
| 44 |
|
| 45 |
auto lower = unicode.LowerCase; |
| 46 |
auto upper = unicode.UpperCase; |
| 47 |
auto ascii = unicode.ASCII; |
| 48 |
|
| 49 |
assert((lower & upper).empty); // no intersection |
| 50 |
auto lowerASCII = lower & ascii; |
| 51 |
assert(lowerASCII.byCodepoint.equal(iota('a', 'z'+1))); |
| 52 |
// throw away all of the lowercase ASCII |
| 53 |
assert((ascii - lower).length == 128 - 26); |
| 54 |
|
| 55 |
auto onlyOneOf = lower ~ ascii; |
| 56 |
assert(!onlyOneOf['Δ']); // not ASCII and not lowercase |
| 57 |
assert(onlyOneOf['$']); // ASCII and not lowercase |
| 58 |
assert(!onlyOneOf['a']); // ASCII and lowercase |
| 59 |
assert(onlyOneOf['я']); // not ASCII but lowercase |
| 60 |
|
| 61 |
// throw away all cased letters from ASCII |
| 62 |
auto noLetters = ascii - (lower | upper); |
| 63 |
assert(noLetters.length == 128 - 26*2); |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
pure @safe unittest |
| 68 |
{ |
| 69 |
import std.uni; |
| 70 |
|
| 71 |
assert('я' in unicode.Cyrillic); |
| 72 |
assert(!('z' in unicode.Cyrillic)); |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
pure @safe unittest |
| 77 |
{ |
| 78 |
import std.uni; |
| 79 |
|
| 80 |
import std.algorithm.comparison : equal; |
| 81 |
import std.range : iota; |
| 82 |
|
| 83 |
auto set = unicode.ASCII; |
| 84 |
set.byCodepoint.equal(iota(0, 0x80)); |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
pure @safe unittest |
| 89 |
{ |
| 90 |
import std.uni; |
| 91 |
|
| 92 |
import std.conv : to; |
| 93 |
import std.format : format; |
| 94 |
import std.uni : unicode; |
| 95 |
|
| 96 |
// This was originally using Cyrillic script. |
| 97 |
// Unfortunately this is a pretty active range for changes, |
| 98 |
// and hence broke in an update. |
| 99 |
// Therefore the range Basic latin was used instead as it |
| 100 |
// unlikely to ever change. |
| 101 |
|
| 102 |
assert(unicode.InBasic_latin.to!string == "[0..128)"); |
| 103 |
|
| 104 |
// The specs '%s' and '%d' are equivalent to the to!string call above. |
| 105 |
assert(format("%d", unicode.InBasic_latin) == unicode.InBasic_latin.to!string); |
| 106 |
|
| 107 |
assert(format("%#x", unicode.InBasic_latin) == "[0..0x80)"); |
| 108 |
assert(format("%#X", unicode.InBasic_latin) == "[0..0X80)"); |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
pure @safe unittest |
| 113 |
{ |
| 114 |
import std.uni; |
| 115 |
|
| 116 |
CodepointSet someSet; |
| 117 |
someSet.add('0', '5').add('A','Z'+1); |
| 118 |
someSet.add('5', '9'+1); |
| 119 |
assert(someSet['0']); |
| 120 |
assert(someSet['5']); |
| 121 |
assert(someSet['9']); |
| 122 |
assert(someSet['Z']); |
| 123 |
|
| 124 |
} |
| 125 |
|
| 126 |
pure @safe unittest |
| 127 |
{ |
| 128 |
import std.uni; |
| 129 |
|
| 130 |
auto set = unicode.ASCII; |
| 131 |
// union with the inverse gets all of the code points in the Unicode |
| 132 |
assert((set | set.inverted).length == 0x110000); |
| 133 |
// no intersection with the inverse |
| 134 |
assert((set & set.inverted).empty); |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
pure @safe unittest |
| 139 |
{ |
| 140 |
import std.uni; |
| 141 |
|
| 142 |
CodepointSet emptySet; |
| 143 |
assert(emptySet.length == 0); |
| 144 |
assert(emptySet.empty); |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
pure @safe unittest |
| 149 |
{ |
| 150 |
import std.uni; |
| 151 |
|
| 152 |
string truth = "2² = 4"; |
| 153 |
auto m = utfMatcher!char(unicode.Number); |
| 154 |
assert(m.match(truth)); // '2' is a number all right |
| 155 |
assert(truth == "² = 4"); // skips on match |
| 156 |
assert(m.match(truth)); // so is the superscript '2' |
| 157 |
assert(!m.match(truth)); // space is not a number |
| 158 |
assert(truth == " = 4"); // unaffected on no match |
| 159 |
assert(!m.skip(truth)); // same test ... |
| 160 |
assert(truth == "= 4"); // but skips a codepoint regardless |
| 161 |
assert(!m.test(truth)); // '=' is not a number |
| 162 |
assert(truth == "= 4"); // test never affects argument |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
@safe unittest |
| 167 |
{ |
| 168 |
import std.uni; |
| 169 |
|
| 170 |
import std.exception : collectException; |
| 171 |
auto ascii = unicode.ASCII; |
| 172 |
assert(ascii['A']); |
| 173 |
assert(ascii['~']); |
| 174 |
assert(!ascii['\u00e0']); |
| 175 |
// matching is case-insensitive |
| 176 |
assert(ascii == unicode.ascII); |
| 177 |
assert(!ascii['à']); |
| 178 |
// underscores, '-' and whitespace in names are ignored too |
| 179 |
auto latin = unicode.in_latin1_Supplement; |
| 180 |
assert(latin['à']); |
| 181 |
assert(!latin['$']); |
| 182 |
// BTW Latin 1 Supplement is a block, hence "In" prefix |
| 183 |
assert(latin == unicode("In Latin 1 Supplement")); |
| 184 |
// run-time look up throws if no such set is found |
| 185 |
assert(collectException(unicode("InCyrilliac"))); |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
@safe unittest |
| 190 |
{ |
| 191 |
import std.uni; |
| 192 |
|
| 193 |
// use .block for explicitness |
| 194 |
assert(unicode.block.Greek_and_Coptic == unicode.InGreek_and_Coptic); |
| 195 |
|
| 196 |
} |
| 197 |
|
| 198 |
@safe unittest |
| 199 |
{ |
| 200 |
import std.uni; |
| 201 |
|
| 202 |
auto arabicScript = unicode.script.arabic; |
| 203 |
auto arabicBlock = unicode.block.arabic; |
| 204 |
// there is an intersection between script and block |
| 205 |
assert(arabicBlock['']); |
| 206 |
assert(arabicScript['']); |
| 207 |
// but they are different |
| 208 |
assert(arabicBlock != arabicScript); |
| 209 |
assert(arabicBlock == unicode.inArabic); |
| 210 |
assert(arabicScript == unicode.arabic); |
| 211 |
|
| 212 |
} |
| 213 |
|
| 214 |
@safe unittest |
| 215 |
{ |
| 216 |
import std.uni; |
| 217 |
|
| 218 |
// L here is syllable type not Letter as in unicode.L short-cut |
| 219 |
auto leadingVowel = unicode.hangulSyllableType("L"); |
| 220 |
// check that some leading vowels are present |
| 221 |
foreach (vowel; '\u1110'..'\u115F') |
| 222 |
assert(leadingVowel[vowel]); |
| 223 |
assert(leadingVowel == unicode.hangulSyllableType.L); |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
@safe unittest |
| 228 |
{ |
| 229 |
import std.uni; |
| 230 |
|
| 231 |
import std.uni : unicode; |
| 232 |
string pat = "[a-zA-Z0-9]hello"; |
| 233 |
auto set = unicode.parseSet(pat); |
| 234 |
// check some of the codepoints |
| 235 |
assert(set['a'] && set['A'] && set['9']); |
| 236 |
assert(pat == "hello"); |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
@safe unittest |
| 241 |
{ |
| 242 |
import std.uni; |
| 243 |
|
| 244 |
assert(graphemeStride(" ", 1) == 1); |
| 245 |
// A + combing ring above |
| 246 |
string city = "A\u030Arhus"; |
| 247 |
size_t first = graphemeStride(city, 0); |
| 248 |
assert(first == 3); //\u030A has 2 UTF-8 code units |
| 249 |
assert(city[0 .. first] == "A\u030A"); |
| 250 |
assert(city[first..$] == "rhus"); |
| 251 |
} |
| 252 |
|
| 253 |
@safe pure unittest |
| 254 |
{ |
| 255 |
import std.uni; |
| 256 |
|
| 257 |
// Two Union Jacks of the Great Britain in each |
| 258 |
string s = "\U0001F1EC\U0001F1E7\U0001F1EC\U0001F1E7"; |
| 259 |
wstring ws = "\U0001F1EC\U0001F1E7\U0001F1EC\U0001F1E7"; |
| 260 |
dstring ds = "\U0001F1EC\U0001F1E7\U0001F1EC\U0001F1E7"; |
| 261 |
|
| 262 |
// String pop length in code units, not points. |
| 263 |
assert(s.popGrapheme() == 8); |
| 264 |
assert(ws.popGrapheme() == 4); |
| 265 |
assert(ds.popGrapheme() == 2); |
| 266 |
|
| 267 |
assert(s == "\U0001F1EC\U0001F1E7"); |
| 268 |
assert(ws == "\U0001F1EC\U0001F1E7"); |
| 269 |
assert(ds == "\U0001F1EC\U0001F1E7"); |
| 270 |
|
| 271 |
import std.algorithm.comparison : equal; |
| 272 |
import std.algorithm.iteration : filter; |
| 273 |
|
| 274 |
// Also works for non-random access ranges as long as the |
| 275 |
// character type is 32-bit. |
| 276 |
auto testPiece = "\r\nhello!"d.filter!(x => !x.isAlpha); |
| 277 |
// Windows-style line ending is two code points in a single grapheme. |
| 278 |
assert(testPiece.popGrapheme() == 2); |
| 279 |
assert(testPiece.equal("!"d)); |
| 280 |
} |
| 281 |
|
| 282 |
@safe unittest |
| 283 |
{ |
| 284 |
import std.uni; |
| 285 |
|
| 286 |
import std.algorithm.comparison : equal; |
| 287 |
import std.range.primitives : walkLength; |
| 288 |
import std.range : take, drop; |
| 289 |
auto text = "noe\u0308l"; // noël using e + combining diaeresis |
| 290 |
assert(text.walkLength == 5); // 5 code points |
| 291 |
|
| 292 |
auto gText = text.byGrapheme; |
| 293 |
assert(gText.walkLength == 4); // 4 graphemes |
| 294 |
|
| 295 |
assert(gText.take(3).equal("noe\u0308".byGrapheme)); |
| 296 |
assert(gText.drop(3).equal("l".byGrapheme)); |
| 297 |
} |
| 298 |
|
| 299 |
@safe unittest |
| 300 |
{ |
| 301 |
import std.uni; |
| 302 |
|
| 303 |
import std.array : array; |
| 304 |
import std.conv : text; |
| 305 |
import std.range : retro; |
| 306 |
|
| 307 |
string s = "noe\u0308l"; // noël |
| 308 |
|
| 309 |
// reverse it and convert the result to a string |
| 310 |
string reverse = s.byGrapheme |
| 311 |
.array |
| 312 |
.retro |
| 313 |
.byCodePoint |
| 314 |
.text; |
| 315 |
|
| 316 |
assert(reverse == "le\u0308on"); // lëon |
| 317 |
} |
| 318 |
|
| 319 |
@safe unittest |
| 320 |
{ |
| 321 |
import std.uni; |
| 322 |
|
| 323 |
auto g = Grapheme("A\u0302"); |
| 324 |
assert(g[0] == 'A'); |
| 325 |
assert(g.valid); |
| 326 |
g[1] = '~'; // ASCII tilda is not a combining mark |
| 327 |
assert(g[1] == '~'); |
| 328 |
assert(!g.valid); |
| 329 |
|
| 330 |
} |
| 331 |
|
| 332 |
@safe unittest |
| 333 |
{ |
| 334 |
import std.uni; |
| 335 |
|
| 336 |
import std.algorithm.comparison : equal; |
| 337 |
auto g = Grapheme("A"); |
| 338 |
assert(g.valid); |
| 339 |
g ~= '\u0301'; |
| 340 |
assert(g[].equal("A\u0301")); |
| 341 |
assert(g.valid); |
| 342 |
g ~= "B"; |
| 343 |
// not a valid grapheme cluster anymore |
| 344 |
assert(!g.valid); |
| 345 |
// still could be useful though |
| 346 |
assert(g[].equal("A\u0301B")); |
| 347 |
|
| 348 |
} |
| 349 |
|
| 350 |
@safe unittest |
| 351 |
{ |
| 352 |
import std.uni; |
| 353 |
|
| 354 |
import std.algorithm.comparison : equal; |
| 355 |
import std.algorithm.iteration : filter; |
| 356 |
import std.range : isRandomAccessRange; |
| 357 |
|
| 358 |
string bold = "ku\u0308hn"; |
| 359 |
|
| 360 |
// note that decodeGrapheme takes parameter by ref |
| 361 |
auto first = decodeGrapheme(bold); |
| 362 |
|
| 363 |
assert(first.length == 1); |
| 364 |
assert(first[0] == 'k'); |
| 365 |
|
| 366 |
// the next grapheme is 2 characters long |
| 367 |
auto wideOne = decodeGrapheme(bold); |
| 368 |
// slicing a grapheme yields a random-access range of dchar |
| 369 |
assert(wideOne[].equal("u\u0308")); |
| 370 |
assert(wideOne.length == 2); |
| 371 |
static assert(isRandomAccessRange!(typeof(wideOne[]))); |
| 372 |
|
| 373 |
// all of the usual range manipulation is possible |
| 374 |
assert(wideOne[].filter!isMark().equal("\u0308")); |
| 375 |
|
| 376 |
auto g = Grapheme("A"); |
| 377 |
assert(g.valid); |
| 378 |
g ~= '\u0301'; |
| 379 |
assert(g[].equal("A\u0301")); |
| 380 |
assert(g.valid); |
| 381 |
g ~= "B"; |
| 382 |
// not a valid grapheme cluster anymore |
| 383 |
assert(!g.valid); |
| 384 |
// still could be useful though |
| 385 |
assert(g[].equal("A\u0301B")); |
| 386 |
} |
| 387 |
|
| 388 |
@safe @nogc pure nothrow unittest |
| 389 |
{ |
| 390 |
import std.uni; |
| 391 |
|
| 392 |
assert(sicmp("Август", "авгусТ") == 0); |
| 393 |
// Greek also works as long as there is no 1:M mapping in sight |
| 394 |
assert(sicmp("ΌΎ", "όύ") == 0); |
| 395 |
// things like the following won't get matched as equal |
| 396 |
// Greek small letter iota with dialytika and tonos |
| 397 |
assert(sicmp("ΐ", "\u03B9\u0308\u0301") != 0); |
| 398 |
|
| 399 |
// while icmp has no problem with that |
| 400 |
assert(icmp("ΐ", "\u03B9\u0308\u0301") == 0); |
| 401 |
assert(icmp("ΌΎ", "όύ") == 0); |
| 402 |
} |
| 403 |
|
| 404 |
@safe @nogc pure nothrow unittest |
| 405 |
{ |
| 406 |
import std.uni; |
| 407 |
|
| 408 |
assert(icmp("Rußland", "Russland") == 0); |
| 409 |
assert(icmp("ᾩ -> \u1F70\u03B9", "\u1F61\u03B9 -> ᾲ") == 0); |
| 410 |
} |
| 411 |
|
| 412 |
@safe @nogc nothrow pure unittest |
| 413 |
{ |
| 414 |
import std.uni; |
| 415 |
|
| 416 |
import std.utf : byDchar; |
| 417 |
|
| 418 |
assert(icmp("Rußland".byDchar, "Russland".byDchar) == 0); |
| 419 |
assert(icmp("ᾩ -> \u1F70\u03B9".byDchar, "\u1F61\u03B9 -> ᾲ".byDchar) == 0); |
| 420 |
} |
| 421 |
|
| 422 |
@safe unittest |
| 423 |
{ |
| 424 |
import std.uni; |
| 425 |
|
| 426 |
// shorten the code |
| 427 |
alias CC = combiningClass; |
| 428 |
|
| 429 |
// combining tilda |
| 430 |
assert(CC('\u0303') == 230); |
| 431 |
// combining ring below |
| 432 |
assert(CC('\u0325') == 220); |
| 433 |
// the simple consequence is that "tilda" should be |
| 434 |
// placed after a "ring below" in a sequence |
| 435 |
} |
| 436 |
|
| 437 |
@safe unittest |
| 438 |
{ |
| 439 |
import std.uni; |
| 440 |
|
| 441 |
assert(compose('A','\u0308') == '\u00C4'); |
| 442 |
assert(compose('A', 'B') == dchar.init); |
| 443 |
assert(compose('C', '\u0301') == '\u0106'); |
| 444 |
// note that the starter is the first one |
| 445 |
// thus the following doesn't compose |
| 446 |
assert(compose('\u0308', 'A') == dchar.init); |
| 447 |
} |
| 448 |
|
| 449 |
@safe unittest |
| 450 |
{ |
| 451 |
import std.uni; |
| 452 |
|
| 453 |
import std.algorithm.comparison : equal; |
| 454 |
|
| 455 |
assert(compose('A','\u0308') == '\u00C4'); |
| 456 |
assert(compose('A', 'B') == dchar.init); |
| 457 |
assert(compose('C', '\u0301') == '\u0106'); |
| 458 |
// note that the starter is the first one |
| 459 |
// thus the following doesn't compose |
| 460 |
assert(compose('\u0308', 'A') == dchar.init); |
| 461 |
|
| 462 |
assert(decompose('Ĉ')[].equal("C\u0302")); |
| 463 |
assert(decompose('D')[].equal("D")); |
| 464 |
assert(decompose('\uD4DC')[].equal("\u1111\u1171\u11B7")); |
| 465 |
assert(decompose!Compatibility('¹')[].equal("1")); |
| 466 |
} |
| 467 |
|
| 468 |
@safe unittest |
| 469 |
{ |
| 470 |
import std.uni; |
| 471 |
|
| 472 |
import std.algorithm.comparison : equal; |
| 473 |
assert(decomposeHangul('\uD4DB')[].equal("\u1111\u1171\u11B6")); |
| 474 |
} |
| 475 |
|
| 476 |
@safe unittest |
| 477 |
{ |
| 478 |
import std.uni; |
| 479 |
|
| 480 |
assert(composeJamo('\u1111', '\u1171', '\u11B6') == '\uD4DB'); |
| 481 |
// leaving out T-vowel, or passing any codepoint |
| 482 |
// that is not trailing consonant composes an LV-syllable |
| 483 |
assert(composeJamo('\u1111', '\u1171') == '\uD4CC'); |
| 484 |
assert(composeJamo('\u1111', '\u1171', ' ') == '\uD4CC'); |
| 485 |
assert(composeJamo('\u1111', 'A') == dchar.init); |
| 486 |
assert(composeJamo('A', '\u1171') == dchar.init); |
| 487 |
} |
| 488 |
|
| 489 |
@safe pure unittest |
| 490 |
{ |
| 491 |
import std.uni; |
| 492 |
|
| 493 |
// any encoding works |
| 494 |
wstring greet = "Hello world"; |
| 495 |
assert(normalize(greet) is greet); // the same exact slice |
| 496 |
|
| 497 |
// An example of a character with all 4 forms being different: |
| 498 |
// Greek upsilon with acute and hook symbol (code point 0x03D3) |
| 499 |
assert(normalize!NFC("ϓ") == "\u03D3"); |
| 500 |
assert(normalize!NFD("ϓ") == "\u03D2\u0301"); |
| 501 |
assert(normalize!NFKC("ϓ") == "\u038E"); |
| 502 |
assert(normalize!NFKD("ϓ") == "\u03A5\u0301"); |
| 503 |
} |
| 504 |
|
| 505 |
@safe unittest |
| 506 |
{ |
| 507 |
import std.uni; |
| 508 |
|
| 509 |
// e.g. Cyrillic is always allowed, so is ASCII |
| 510 |
assert(allowedIn!NFC('я')); |
| 511 |
assert(allowedIn!NFD('я')); |
| 512 |
assert(allowedIn!NFKC('я')); |
| 513 |
assert(allowedIn!NFKD('я')); |
| 514 |
assert(allowedIn!NFC('Z')); |
| 515 |
} |
| 516 |
|
| 517 |
@safe pure unittest |
| 518 |
{ |
| 519 |
import std.uni; |
| 520 |
|
| 521 |
import std.algorithm.comparison : equal; |
| 522 |
|
| 523 |
assert("hEllo".asUpperCase.equal("HELLO")); |
| 524 |
} |
| 525 |
|
| 526 |
@safe pure unittest |
| 527 |
{ |
| 528 |
import std.uni; |
| 529 |
|
| 530 |
import std.algorithm.comparison : equal; |
| 531 |
|
| 532 |
assert("hEllo".asCapitalized.equal("Hello")); |
| 533 |
} |
| 534 |
|
| 535 |
@safe unittest |
| 536 |
{ |
| 537 |
import std.uni; |
| 538 |
|
| 539 |
import std.algorithm.iteration : map; |
| 540 |
import std.algorithm.mutation : copy; |
| 541 |
import std.array : appender; |
| 542 |
|
| 543 |
auto abuf = appender!(char[])(); |
| 544 |
"hello".map!toUpper.copy(abuf); |
| 545 |
assert(abuf.data == "HELLO"); |
| 546 |
} |
| 547 |
|