| 1 |
@safe unittest |
| 2 |
{ |
| 3 |
import std.utf; |
| 4 |
|
| 5 |
import std.exception : assertThrown; |
| 6 |
|
| 7 |
char[4] buf; |
| 8 |
assertThrown!UTFException(encode(buf, cast(dchar) 0xD800)); |
| 9 |
assertThrown!UTFException(encode(buf, cast(dchar) 0xDBFF)); |
| 10 |
assertThrown!UTFException(encode(buf, cast(dchar) 0xDC00)); |
| 11 |
assertThrown!UTFException(encode(buf, cast(dchar) 0xDFFF)); |
| 12 |
assertThrown!UTFException(encode(buf, cast(dchar) 0x110000)); |
| 13 |
} |
| 14 |
|
| 15 |
@safe @nogc pure nothrow unittest |
| 16 |
{ |
| 17 |
import std.utf; |
| 18 |
|
| 19 |
assert( isValidDchar(cast(dchar) 0x41)); |
| 20 |
assert( isValidDchar(cast(dchar) 0x00)); |
| 21 |
assert(!isValidDchar(cast(dchar) 0xD800)); |
| 22 |
assert(!isValidDchar(cast(dchar) 0x11FFFF)); |
| 23 |
} |
| 24 |
|
| 25 |
@safe pure nothrow unittest |
| 26 |
{ |
| 27 |
import std.utf; |
| 28 |
|
| 29 |
assert( isValidCodepoint(cast(char) 0x40)); |
| 30 |
assert(!isValidCodepoint(cast(char) 0x80)); |
| 31 |
assert( isValidCodepoint(cast(wchar) 0x1234)); |
| 32 |
assert(!isValidCodepoint(cast(wchar) 0xD800)); |
| 33 |
assert( isValidCodepoint(cast(dchar) 0x0010FFFF)); |
| 34 |
assert(!isValidCodepoint(cast(dchar) 0x12345678)); |
| 35 |
} |
| 36 |
|
| 37 |
@safe unittest |
| 38 |
{ |
| 39 |
import std.utf; |
| 40 |
|
| 41 |
assert("a".stride == 1); |
| 42 |
assert("λ".stride == 2); |
| 43 |
assert("aλ".stride == 1); |
| 44 |
assert("aλ".stride(1) == 2); |
| 45 |
assert("𐐷".stride == 4); |
| 46 |
} |
| 47 |
|
| 48 |
@safe unittest |
| 49 |
{ |
| 50 |
import std.utf; |
| 51 |
|
| 52 |
assert("a".strideBack == 1); |
| 53 |
assert("λ".strideBack == 2); |
| 54 |
assert("aλ".strideBack == 2); |
| 55 |
assert("aλ".strideBack(1) == 1); |
| 56 |
assert("𐐷".strideBack == 4); |
| 57 |
} |
| 58 |
|
| 59 |
@safe unittest |
| 60 |
{ |
| 61 |
import std.utf; |
| 62 |
|
| 63 |
assert(toUCSindex(`hello world`, 7) == 7); |
| 64 |
assert(toUCSindex(`hello world`w, 7) == 7); |
| 65 |
assert(toUCSindex(`hello world`d, 7) == 7); |
| 66 |
|
| 67 |
assert(toUCSindex(`Ma Chérie`, 7) == 6); |
| 68 |
assert(toUCSindex(`Ma Chérie`w, 7) == 7); |
| 69 |
assert(toUCSindex(`Ma Chérie`d, 7) == 7); |
| 70 |
|
| 71 |
assert(toUCSindex(`さいごの果実 / ミツバチと科学者`, 9) == 3); |
| 72 |
assert(toUCSindex(`さいごの果実 / ミツバチと科学者`w, 9) == 9); |
| 73 |
assert(toUCSindex(`さいごの果実 / ミツバチと科学者`d, 9) == 9); |
| 74 |
} |
| 75 |
|
| 76 |
@safe unittest |
| 77 |
{ |
| 78 |
import std.utf; |
| 79 |
|
| 80 |
assert(toUTFindex(`hello world`, 7) == 7); |
| 81 |
assert(toUTFindex(`hello world`w, 7) == 7); |
| 82 |
assert(toUTFindex(`hello world`d, 7) == 7); |
| 83 |
|
| 84 |
assert(toUTFindex(`Ma Chérie`, 6) == 7); |
| 85 |
assert(toUTFindex(`Ma Chérie`w, 7) == 7); |
| 86 |
assert(toUTFindex(`Ma Chérie`d, 7) == 7); |
| 87 |
|
| 88 |
assert(toUTFindex(`さいごの果実 / ミツバチと科学者`, 3) == 9); |
| 89 |
assert(toUTFindex(`さいごの果実 / ミツバチと科学者`w, 9) == 9); |
| 90 |
assert(toUTFindex(`さいごの果実 / ミツバチと科学者`d, 9) == 9); |
| 91 |
} |
| 92 |
|
| 93 |
@safe pure unittest |
| 94 |
{ |
| 95 |
import std.utf; |
| 96 |
|
| 97 |
size_t i; |
| 98 |
|
| 99 |
assert("a".decode(i) == 'a' && i == 1); |
| 100 |
i = 0; |
| 101 |
assert("å".decode(i) == 'å' && i == 2); |
| 102 |
i = 1; |
| 103 |
assert("aå".decode(i) == 'å' && i == 3); |
| 104 |
i = 0; |
| 105 |
assert("å"w.decode(i) == 'å' && i == 1); |
| 106 |
|
| 107 |
// ë as a multi-code point grapheme |
| 108 |
i = 0; |
| 109 |
assert("e\u0308".decode(i) == 'e' && i == 1); |
| 110 |
// ë as a single code point grapheme |
| 111 |
i = 0; |
| 112 |
assert("ë".decode(i) == 'ë' && i == 2); |
| 113 |
i = 0; |
| 114 |
assert("ë"w.decode(i) == 'ë' && i == 1); |
| 115 |
} |
| 116 |
|
| 117 |
@safe pure unittest |
| 118 |
{ |
| 119 |
import std.utf; |
| 120 |
|
| 121 |
import std.range.primitives; |
| 122 |
string str = "Hello, World!"; |
| 123 |
|
| 124 |
assert(str.decodeFront == 'H' && str == "ello, World!"); |
| 125 |
str = "å"; |
| 126 |
assert(str.decodeFront == 'å' && str.empty); |
| 127 |
str = "å"; |
| 128 |
size_t i; |
| 129 |
assert(str.decodeFront(i) == 'å' && i == 2 && str.empty); |
| 130 |
} |
| 131 |
|
| 132 |
@system pure unittest |
| 133 |
{ |
| 134 |
import std.utf; |
| 135 |
|
| 136 |
import std.range.primitives; |
| 137 |
string str = "Hello, World!"; |
| 138 |
|
| 139 |
assert(str.decodeBack == '!' && str == "Hello, World"); |
| 140 |
str = "å"; |
| 141 |
assert(str.decodeBack == 'å' && str.empty); |
| 142 |
str = "å"; |
| 143 |
size_t i; |
| 144 |
assert(str.decodeBack(i) == 'å' && i == 2 && str.empty); |
| 145 |
} |
| 146 |
|
| 147 |
@safe unittest |
| 148 |
{ |
| 149 |
import std.utf; |
| 150 |
|
| 151 |
import std.exception : assertThrown; |
| 152 |
import std.typecons : Yes; |
| 153 |
|
| 154 |
char[4] buf; |
| 155 |
|
| 156 |
assert(encode(buf, '\u0000') == 1 && buf[0 .. 1] == "\u0000"); |
| 157 |
assert(encode(buf, '\u007F') == 1 && buf[0 .. 1] == "\u007F"); |
| 158 |
assert(encode(buf, '\u0080') == 2 && buf[0 .. 2] == "\u0080"); |
| 159 |
assert(encode(buf, '\uE000') == 3 && buf[0 .. 3] == "\uE000"); |
| 160 |
assert(encode(buf, 0xFFFE) == 3 && buf[0 .. 3] == "\xEF\xBF\xBE"); |
| 161 |
assertThrown!UTFException(encode(buf, cast(dchar) 0x110000)); |
| 162 |
|
| 163 |
encode!(Yes.useReplacementDchar)(buf, cast(dchar) 0x110000); |
| 164 |
auto slice = buf[]; |
| 165 |
assert(slice.decodeFront == replacementDchar); |
| 166 |
} |
| 167 |
|
| 168 |
@safe unittest |
| 169 |
{ |
| 170 |
import std.utf; |
| 171 |
|
| 172 |
import std.exception : assertThrown; |
| 173 |
import std.typecons : Yes; |
| 174 |
|
| 175 |
wchar[2] buf; |
| 176 |
|
| 177 |
assert(encode(buf, '\u0000') == 1 && buf[0 .. 1] == "\u0000"); |
| 178 |
assert(encode(buf, '\uD7FF') == 1 && buf[0 .. 1] == "\uD7FF"); |
| 179 |
assert(encode(buf, '\uE000') == 1 && buf[0 .. 1] == "\uE000"); |
| 180 |
assert(encode(buf, '\U00010000') == 2 && buf[0 .. 2] == "\U00010000"); |
| 181 |
assert(encode(buf, '\U0010FFFF') == 2 && buf[0 .. 2] == "\U0010FFFF"); |
| 182 |
assertThrown!UTFException(encode(buf, cast(dchar) 0xD800)); |
| 183 |
|
| 184 |
encode!(Yes.useReplacementDchar)(buf, cast(dchar) 0x110000); |
| 185 |
auto slice = buf[]; |
| 186 |
assert(slice.decodeFront == replacementDchar); |
| 187 |
} |
| 188 |
|
| 189 |
@safe unittest |
| 190 |
{ |
| 191 |
import std.utf; |
| 192 |
|
| 193 |
import std.exception : assertThrown; |
| 194 |
import std.typecons : Yes; |
| 195 |
|
| 196 |
dchar[1] buf; |
| 197 |
|
| 198 |
assert(encode(buf, '\u0000') == 1 && buf[0] == '\u0000'); |
| 199 |
assert(encode(buf, '\uD7FF') == 1 && buf[0] == '\uD7FF'); |
| 200 |
assert(encode(buf, '\uE000') == 1 && buf[0] == '\uE000'); |
| 201 |
assert(encode(buf, '\U0010FFFF') == 1 && buf[0] == '\U0010FFFF'); |
| 202 |
assertThrown!UTFException(encode(buf, cast(dchar) 0xD800)); |
| 203 |
|
| 204 |
encode!(Yes.useReplacementDchar)(buf, cast(dchar) 0x110000); |
| 205 |
assert(buf[0] == replacementDchar); |
| 206 |
} |
| 207 |
|
| 208 |
@safe unittest |
| 209 |
{ |
| 210 |
import std.utf; |
| 211 |
|
| 212 |
char[] s = "abcd".dup; |
| 213 |
dchar d1 = 'a'; |
| 214 |
dchar d2 = 'ø'; |
| 215 |
|
| 216 |
encode(s, d1); |
| 217 |
assert(s.length == 5); |
| 218 |
assert(s == "abcda"); |
| 219 |
encode(s, d2); |
| 220 |
assert(s.length == 7); |
| 221 |
assert(s == "abcdaø"); |
| 222 |
} |
| 223 |
|
| 224 |
@safe pure nothrow @nogc unittest |
| 225 |
{ |
| 226 |
import std.utf; |
| 227 |
|
| 228 |
assert(codeLength!char('a') == 1); |
| 229 |
assert(codeLength!wchar('a') == 1); |
| 230 |
assert(codeLength!dchar('a') == 1); |
| 231 |
|
| 232 |
assert(codeLength!char('\U0010FFFF') == 4); |
| 233 |
assert(codeLength!wchar('\U0010FFFF') == 2); |
| 234 |
assert(codeLength!dchar('\U0010FFFF') == 1); |
| 235 |
} |
| 236 |
|
| 237 |
@safe unittest |
| 238 |
{ |
| 239 |
import std.utf; |
| 240 |
|
| 241 |
assert(codeLength!char("hello world") == |
| 242 |
"hello world".length); |
| 243 |
assert(codeLength!wchar("hello world") == |
| 244 |
"hello world"w.length); |
| 245 |
assert(codeLength!dchar("hello world") == |
| 246 |
"hello world"d.length); |
| 247 |
|
| 248 |
assert(codeLength!char(`プログラミング`) == |
| 249 |
`プログラミング`.length); |
| 250 |
assert(codeLength!wchar(`プログラミング`) == |
| 251 |
`プログラミング`w.length); |
| 252 |
assert(codeLength!dchar(`プログラミング`) == |
| 253 |
`プログラミング`d.length); |
| 254 |
|
| 255 |
string haystack = `Être sans la verité, ça, ce ne serait pas bien.`; |
| 256 |
wstring needle = `Être sans la verité`; |
| 257 |
assert(haystack[codeLength!char(needle) .. $] == |
| 258 |
`, ça, ce ne serait pas bien.`); |
| 259 |
} |
| 260 |
|
| 261 |
@safe unittest |
| 262 |
{ |
| 263 |
import std.utf; |
| 264 |
|
| 265 |
import std.exception : assertThrown; |
| 266 |
char[] a = [167, 133, 175]; |
| 267 |
assertThrown!UTFException(validate(a)); |
| 268 |
} |
| 269 |
|
| 270 |
@safe pure unittest |
| 271 |
{ |
| 272 |
import std.utf; |
| 273 |
|
| 274 |
import std.algorithm.comparison : equal; |
| 275 |
|
| 276 |
// The ö is represented by two UTF-8 code units |
| 277 |
assert("Hellø"w.toUTF8.equal(['H', 'e', 'l', 'l', 0xC3, 0xB8])); |
| 278 |
|
| 279 |
// 𐐷 is four code units in UTF-8 |
| 280 |
assert("𐐷"d.toUTF8.equal([0xF0, 0x90, 0x90, 0xB7])); |
| 281 |
} |
| 282 |
|
| 283 |
@safe pure unittest |
| 284 |
{ |
| 285 |
import std.utf; |
| 286 |
|
| 287 |
import std.algorithm.comparison : equal; |
| 288 |
|
| 289 |
// these graphemes are two code units in UTF-16 and one in UTF-32 |
| 290 |
assert("𤭢"d.length == 1); |
| 291 |
assert("𐐷"d.length == 1); |
| 292 |
|
| 293 |
assert("𤭢"d.toUTF16.equal([0xD852, 0xDF62])); |
| 294 |
assert("𐐷"d.toUTF16.equal([0xD801, 0xDC37])); |
| 295 |
} |
| 296 |
|
| 297 |
@safe pure unittest |
| 298 |
{ |
| 299 |
import std.utf; |
| 300 |
|
| 301 |
import std.algorithm.comparison : equal; |
| 302 |
|
| 303 |
// these graphemes are two code units in UTF-16 and one in UTF-32 |
| 304 |
assert("𤭢"w.length == 2); |
| 305 |
assert("𐐷"w.length == 2); |
| 306 |
|
| 307 |
assert("𤭢"w.toUTF32.equal([0x00024B62])); |
| 308 |
assert("𐐷"w.toUTF32.equal([0x00010437])); |
| 309 |
} |
| 310 |
|
| 311 |
@safe pure unittest |
| 312 |
{ |
| 313 |
import std.utf; |
| 314 |
|
| 315 |
auto p1 = toUTFz!(char*)("hello world"); |
| 316 |
auto p2 = toUTFz!(const(char)*)("hello world"); |
| 317 |
auto p3 = toUTFz!(immutable(char)*)("hello world"); |
| 318 |
auto p4 = toUTFz!(char*)("hello world"d); |
| 319 |
auto p5 = toUTFz!(const(wchar)*)("hello world"); |
| 320 |
auto p6 = toUTFz!(immutable(dchar)*)("hello world"w); |
| 321 |
} |
| 322 |
|
| 323 |
@system unittest |
| 324 |
{ |
| 325 |
import std.utf; |
| 326 |
|
| 327 |
string str = "Hello, World!"; |
| 328 |
const(wchar)* p = str.toUTF16z; |
| 329 |
assert(p[str.length] == '\0'); |
| 330 |
} |
| 331 |
|
| 332 |
@safe pure nothrow @nogc unittest |
| 333 |
{ |
| 334 |
import std.utf; |
| 335 |
|
| 336 |
assert(count("") == 0); |
| 337 |
assert(count("a") == 1); |
| 338 |
assert(count("abc") == 3); |
| 339 |
assert(count("\u20AC100") == 4); |
| 340 |
} |
| 341 |
|
| 342 |
@safe unittest |
| 343 |
{ |
| 344 |
import std.utf; |
| 345 |
|
| 346 |
import std.range.primitives; |
| 347 |
import std.traits : isAutodecodableString; |
| 348 |
|
| 349 |
auto r = "Hello, World!".byCodeUnit(); |
| 350 |
static assert(hasLength!(typeof(r))); |
| 351 |
static assert(hasSlicing!(typeof(r))); |
| 352 |
static assert(isRandomAccessRange!(typeof(r))); |
| 353 |
static assert(is(ElementType!(typeof(r)) == immutable char)); |
| 354 |
|
| 355 |
// contrast with the range capabilities of standard strings (with or |
| 356 |
// without autodecoding enabled). |
| 357 |
auto s = "Hello, World!"; |
| 358 |
static assert(isBidirectionalRange!(typeof(r))); |
| 359 |
static if (isAutodecodableString!(typeof(s))) |
| 360 |
{ |
| 361 |
// with autodecoding enabled, strings are non-random-access ranges of |
| 362 |
// dchar. |
| 363 |
static assert(is(ElementType!(typeof(s)) == dchar)); |
| 364 |
static assert(!isRandomAccessRange!(typeof(s))); |
| 365 |
static assert(!hasSlicing!(typeof(s))); |
| 366 |
static assert(!hasLength!(typeof(s))); |
| 367 |
} |
| 368 |
else |
| 369 |
{ |
| 370 |
// without autodecoding, strings are normal arrays. |
| 371 |
static assert(is(ElementType!(typeof(s)) == immutable char)); |
| 372 |
static assert(isRandomAccessRange!(typeof(s))); |
| 373 |
static assert(hasSlicing!(typeof(s))); |
| 374 |
static assert(hasLength!(typeof(s))); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
@safe unittest |
| 379 |
{ |
| 380 |
import std.utf; |
| 381 |
|
| 382 |
string noel1 = "noe\u0308l"; // noël using e + combining diaeresis |
| 383 |
assert(noel1.byCodeUnit[2] != 'ë'); |
| 384 |
assert(noel1.byCodeUnit[2] == 'e'); |
| 385 |
|
| 386 |
string noel2 = "no\u00EBl"; // noël using a precomposed ë character |
| 387 |
// Because string is UTF-8, the code unit at index 2 is just |
| 388 |
// the first of a sequence that encodes 'ë' |
| 389 |
assert(noel2.byCodeUnit[2] != 'ë'); |
| 390 |
} |
| 391 |
|
| 392 |
@safe unittest |
| 393 |
{ |
| 394 |
import std.utf; |
| 395 |
|
| 396 |
import std.algorithm.comparison : equal; |
| 397 |
import std.range : popFrontN; |
| 398 |
import std.traits : isAutodecodableString; |
| 399 |
{ |
| 400 |
auto range = byCodeUnit("hello world"); |
| 401 |
range.popFrontN(3); |
| 402 |
assert(equal(range.save, "lo world")); |
| 403 |
static if (isAutodecodableString!string) // only enabled with autodecoding |
| 404 |
{ |
| 405 |
string str = range.source; |
| 406 |
assert(str == "lo world"); |
| 407 |
} |
| 408 |
} |
| 409 |
// source only exists if the range was wrapped |
| 410 |
{ |
| 411 |
auto range = byCodeUnit("hello world"d); |
| 412 |
static assert(!__traits(compiles, range.source)); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
@safe pure nothrow unittest |
| 417 |
{ |
| 418 |
import std.utf; |
| 419 |
|
| 420 |
import std.algorithm.comparison : equal; |
| 421 |
|
| 422 |
// hellö as a range of `char`s, which are UTF-8 |
| 423 |
assert("hell\u00F6".byUTF!char().equal(['h', 'e', 'l', 'l', 0xC3, 0xB6])); |
| 424 |
|
| 425 |
// `wchar`s are able to hold the ö in a single element (UTF-16 code unit) |
| 426 |
assert("hell\u00F6".byUTF!wchar().equal(['h', 'e', 'l', 'l', 'ö'])); |
| 427 |
|
| 428 |
// 𐐷 is four code units in UTF-8, two in UTF-16, and one in UTF-32 |
| 429 |
assert("𐐷".byUTF!char().equal([0xF0, 0x90, 0x90, 0xB7])); |
| 430 |
assert("𐐷".byUTF!wchar().equal([0xD801, 0xDC37])); |
| 431 |
assert("𐐷".byUTF!dchar().equal([0x00010437])); |
| 432 |
} |
| 433 |
|
| 434 |
@safe unittest |
| 435 |
{ |
| 436 |
import std.utf; |
| 437 |
|
| 438 |
import std.algorithm.comparison : equal; |
| 439 |
import std.exception : assertThrown; |
| 440 |
|
| 441 |
assert("hello\xF0betty".byChar.byUTF!(dchar, UseReplacementDchar.yes).equal("hello\uFFFDetty")); |
| 442 |
assertThrown!UTFException("hello\xF0betty".byChar.byUTF!(dchar, UseReplacementDchar.no).equal("hello betty")); |
| 443 |
} |
| 444 |
|
| 445 |
@safe pure nothrow unittest |
| 446 |
{ |
| 447 |
import std.utf; |
| 448 |
|
| 449 |
import std.range.primitives; |
| 450 |
wchar[] s = ['ă', 'î']; |
| 451 |
|
| 452 |
auto rc = s.byUTF!char; |
| 453 |
static assert(isBidirectionalRange!(typeof(rc))); |
| 454 |
assert(rc.back == 0xae); |
| 455 |
rc.popBack; |
| 456 |
assert(rc.back == 0xc3); |
| 457 |
rc.popBack; |
| 458 |
assert(rc.back == 0x83); |
| 459 |
rc.popBack; |
| 460 |
assert(rc.back == 0xc4); |
| 461 |
|
| 462 |
auto rw = s.byUTF!wchar; |
| 463 |
static assert(isBidirectionalRange!(typeof(rw))); |
| 464 |
assert(rw.back == 'î'); |
| 465 |
rw.popBack; |
| 466 |
assert(rw.back == 'ă'); |
| 467 |
|
| 468 |
auto rd = s.byUTF!dchar; |
| 469 |
static assert(isBidirectionalRange!(typeof(rd))); |
| 470 |
assert(rd.back == 'î'); |
| 471 |
rd.popBack; |
| 472 |
assert(rd.back == 'ă'); |
| 473 |
} |
| 474 |
|