| 1 |
@safe pure unittest |
| 2 |
{ |
| 3 |
import std.string; |
| 4 |
|
| 5 |
import std.exception : assertThrown; |
| 6 |
auto bad = " a\n\tb\n c"; |
| 7 |
assertThrown!StringException(bad.outdent); |
| 8 |
} |
| 9 |
|
| 10 |
@system pure unittest |
| 11 |
{ |
| 12 |
import std.string; |
| 13 |
|
| 14 |
assert(fromStringz("foo\0"c.ptr) == "foo"c); |
| 15 |
assert(fromStringz("foo\0"w.ptr) == "foo"w); |
| 16 |
assert(fromStringz("foo\0"d.ptr) == "foo"d); |
| 17 |
|
| 18 |
assert(fromStringz("福\0"c.ptr) == "福"c); |
| 19 |
assert(fromStringz("福\0"w.ptr) == "福"w); |
| 20 |
assert(fromStringz("福\0"d.ptr) == "福"d); |
| 21 |
} |
| 22 |
|
| 23 |
@nogc @safe pure nothrow unittest |
| 24 |
{ |
| 25 |
import std.string; |
| 26 |
|
| 27 |
struct C |
| 28 |
{ |
| 29 |
char[32] name; |
| 30 |
} |
| 31 |
assert(C("foo\0"c).name.fromStringz() == "foo"c); |
| 32 |
|
| 33 |
struct W |
| 34 |
{ |
| 35 |
wchar[32] name; |
| 36 |
} |
| 37 |
assert(W("foo\0"w).name.fromStringz() == "foo"w); |
| 38 |
|
| 39 |
struct D |
| 40 |
{ |
| 41 |
dchar[32] name; |
| 42 |
} |
| 43 |
assert(D("foo\0"d).name.fromStringz() == "foo"d); |
| 44 |
} |
| 45 |
|
| 46 |
pure nothrow @system unittest |
| 47 |
{ |
| 48 |
import std.string; |
| 49 |
|
| 50 |
import core.stdc.string : strlen; |
| 51 |
import std.conv : to; |
| 52 |
|
| 53 |
auto p = toStringz("foo"); |
| 54 |
assert(strlen(p) == 3); |
| 55 |
const(char)[] foo = "abbzxyzzy"; |
| 56 |
p = toStringz(foo[3 .. 5]); |
| 57 |
assert(strlen(p) == 2); |
| 58 |
|
| 59 |
string test = ""; |
| 60 |
p = toStringz(test); |
| 61 |
assert(*p == 0); |
| 62 |
|
| 63 |
test = "\0"; |
| 64 |
p = toStringz(test); |
| 65 |
assert(*p == 0); |
| 66 |
|
| 67 |
test = "foo\0"; |
| 68 |
p = toStringz(test); |
| 69 |
assert(p[0] == 'f' && p[1] == 'o' && p[2] == 'o' && p[3] == 0); |
| 70 |
|
| 71 |
const string test2 = ""; |
| 72 |
p = toStringz(test2); |
| 73 |
assert(*p == 0); |
| 74 |
|
| 75 |
assert(toStringz([]) is toStringz("")); |
| 76 |
} |
| 77 |
|
| 78 |
@safe pure unittest |
| 79 |
{ |
| 80 |
import std.string; |
| 81 |
|
| 82 |
import std.typecons : No; |
| 83 |
|
| 84 |
string s = "Hello World"; |
| 85 |
assert(indexOf(s, 'W') == 6); |
| 86 |
assert(indexOf(s, 'Z') == -1); |
| 87 |
assert(indexOf(s, 'w', No.caseSensitive) == 6); |
| 88 |
} |
| 89 |
|
| 90 |
@safe pure unittest |
| 91 |
{ |
| 92 |
import std.string; |
| 93 |
|
| 94 |
import std.typecons : No; |
| 95 |
|
| 96 |
string s = "Hello World"; |
| 97 |
assert(indexOf(s, 'W', 4) == 6); |
| 98 |
assert(indexOf(s, 'Z', 100) == -1); |
| 99 |
assert(indexOf(s, 'w', 3, No.caseSensitive) == 6); |
| 100 |
} |
| 101 |
|
| 102 |
@safe pure unittest |
| 103 |
{ |
| 104 |
import std.string; |
| 105 |
|
| 106 |
import std.typecons : No; |
| 107 |
|
| 108 |
string s = "Hello World"; |
| 109 |
assert(indexOf(s, "Wo", 4) == 6); |
| 110 |
assert(indexOf(s, "Zo", 100) == -1); |
| 111 |
assert(indexOf(s, "wo", 3, No.caseSensitive) == 6); |
| 112 |
} |
| 113 |
|
| 114 |
@safe pure unittest |
| 115 |
{ |
| 116 |
import std.string; |
| 117 |
|
| 118 |
import std.typecons : No; |
| 119 |
|
| 120 |
string s = "Hello World"; |
| 121 |
assert(indexOf(s, "Wo") == 6); |
| 122 |
assert(indexOf(s, "Zo") == -1); |
| 123 |
assert(indexOf(s, "wO", No.caseSensitive) == 6); |
| 124 |
} |
| 125 |
|
| 126 |
@safe pure unittest |
| 127 |
{ |
| 128 |
import std.string; |
| 129 |
|
| 130 |
import std.typecons : No; |
| 131 |
|
| 132 |
string s = "Hello World"; |
| 133 |
assert(lastIndexOf(s, 'l') == 9); |
| 134 |
assert(lastIndexOf(s, 'Z') == -1); |
| 135 |
assert(lastIndexOf(s, 'L', No.caseSensitive) == 9); |
| 136 |
} |
| 137 |
|
| 138 |
@safe pure unittest |
| 139 |
{ |
| 140 |
import std.string; |
| 141 |
|
| 142 |
import std.typecons : No; |
| 143 |
|
| 144 |
string s = "Hello World"; |
| 145 |
assert(lastIndexOf(s, 'l', 4) == 3); |
| 146 |
assert(lastIndexOf(s, 'Z', 1337) == -1); |
| 147 |
assert(lastIndexOf(s, 'L', 7, No.caseSensitive) == 3); |
| 148 |
} |
| 149 |
|
| 150 |
@safe pure unittest |
| 151 |
{ |
| 152 |
import std.string; |
| 153 |
|
| 154 |
import std.typecons : No; |
| 155 |
|
| 156 |
string s = "Hello World"; |
| 157 |
assert(lastIndexOf(s, "ll") == 2); |
| 158 |
assert(lastIndexOf(s, "Zo") == -1); |
| 159 |
assert(lastIndexOf(s, "lL", No.caseSensitive) == 2); |
| 160 |
} |
| 161 |
|
| 162 |
@safe pure unittest |
| 163 |
{ |
| 164 |
import std.string; |
| 165 |
|
| 166 |
import std.typecons : No; |
| 167 |
|
| 168 |
string s = "Hello World"; |
| 169 |
assert(lastIndexOf(s, "ll", 4) == 2); |
| 170 |
assert(lastIndexOf(s, "Zo", 128) == -1); |
| 171 |
assert(lastIndexOf(s, "lL", 3, No.caseSensitive) == -1); |
| 172 |
} |
| 173 |
|
| 174 |
@safe pure unittest |
| 175 |
{ |
| 176 |
import std.string; |
| 177 |
|
| 178 |
import std.conv : to; |
| 179 |
|
| 180 |
ptrdiff_t i = "helloWorld".indexOfAny("Wr"); |
| 181 |
assert(i == 5); |
| 182 |
i = "öällo world".indexOfAny("lo "); |
| 183 |
assert(i == 4, to!string(i)); |
| 184 |
} |
| 185 |
|
| 186 |
@safe pure unittest |
| 187 |
{ |
| 188 |
import std.string; |
| 189 |
|
| 190 |
import std.conv : to; |
| 191 |
|
| 192 |
ptrdiff_t i = "helloWorld".indexOfAny("Wr", 4); |
| 193 |
assert(i == 5); |
| 194 |
|
| 195 |
i = "Foo öällo world".indexOfAny("lh", 3); |
| 196 |
assert(i == 8, to!string(i)); |
| 197 |
} |
| 198 |
|
| 199 |
@safe pure unittest |
| 200 |
{ |
| 201 |
import std.string; |
| 202 |
|
| 203 |
ptrdiff_t i = "helloWorld".lastIndexOfAny("Wlo"); |
| 204 |
assert(i == 8); |
| 205 |
|
| 206 |
i = "Foo öäöllo world".lastIndexOfAny("öF"); |
| 207 |
assert(i == 8); |
| 208 |
} |
| 209 |
|
| 210 |
@safe pure unittest |
| 211 |
{ |
| 212 |
import std.string; |
| 213 |
|
| 214 |
import std.conv : to; |
| 215 |
|
| 216 |
ptrdiff_t i = "helloWorld".lastIndexOfAny("Wlo", 4); |
| 217 |
assert(i == 3); |
| 218 |
|
| 219 |
i = "Foo öäöllo world".lastIndexOfAny("öF", 3); |
| 220 |
assert(i == 0); |
| 221 |
} |
| 222 |
|
| 223 |
@safe pure unittest |
| 224 |
{ |
| 225 |
import std.string; |
| 226 |
|
| 227 |
assert(indexOfNeither("abba", "a", 2) == 2); |
| 228 |
assert(indexOfNeither("def", "de", 1) == 2); |
| 229 |
assert(indexOfNeither("dfefffg", "dfe", 4) == 6); |
| 230 |
} |
| 231 |
|
| 232 |
@safe pure unittest |
| 233 |
{ |
| 234 |
import std.string; |
| 235 |
|
| 236 |
assert(indexOfNeither("def", "a") == 0); |
| 237 |
assert(indexOfNeither("def", "de") == 2); |
| 238 |
assert(indexOfNeither("dfefffg", "dfe") == 6); |
| 239 |
} |
| 240 |
|
| 241 |
@safe pure unittest |
| 242 |
{ |
| 243 |
import std.string; |
| 244 |
|
| 245 |
assert(lastIndexOfNeither("abba", "a") == 2); |
| 246 |
assert(lastIndexOfNeither("def", "f") == 1); |
| 247 |
} |
| 248 |
|
| 249 |
@safe pure unittest |
| 250 |
{ |
| 251 |
import std.string; |
| 252 |
|
| 253 |
assert(lastIndexOfNeither("def", "rsa", 3) == -1); |
| 254 |
assert(lastIndexOfNeither("abba", "a", 2) == 1); |
| 255 |
} |
| 256 |
|
| 257 |
@safe pure unittest |
| 258 |
{ |
| 259 |
import std.string; |
| 260 |
|
| 261 |
string s = "hello"; |
| 262 |
static assert(is(typeof(representation(s)) == immutable(ubyte)[])); |
| 263 |
assert(representation(s) is cast(immutable(ubyte)[]) s); |
| 264 |
assert(representation(s) == [0x68, 0x65, 0x6c, 0x6c, 0x6f]); |
| 265 |
} |
| 266 |
|
| 267 |
pure @safe unittest |
| 268 |
{ |
| 269 |
import std.string; |
| 270 |
|
| 271 |
assert(capitalize("hello") == "Hello"); |
| 272 |
assert(capitalize("World") == "World"); |
| 273 |
} |
| 274 |
|
| 275 |
@safe pure nothrow unittest |
| 276 |
{ |
| 277 |
import std.string; |
| 278 |
|
| 279 |
string s = "Hello\nmy\rname\nis"; |
| 280 |
assert(splitLines(s) == ["Hello", "my", "name", "is"]); |
| 281 |
} |
| 282 |
|
| 283 |
@safe pure unittest |
| 284 |
{ |
| 285 |
import std.string; |
| 286 |
|
| 287 |
import std.array : array; |
| 288 |
|
| 289 |
string s = "Hello\nmy\rname\nis"; |
| 290 |
|
| 291 |
/* notice the call to 'array' to turn the lazy range created by |
| 292 |
lineSplitter comparable to the string[] created by splitLines. |
| 293 |
*/ |
| 294 |
assert(lineSplitter(s).array == splitLines(s)); |
| 295 |
} |
| 296 |
|
| 297 |
@nogc @safe pure unittest |
| 298 |
{ |
| 299 |
import std.string; |
| 300 |
|
| 301 |
auto s = "\rpeter\n\rpaul\r\njerry\u2028ice\u2029cream\n\nsunday\nmon\u2030day\n"; |
| 302 |
auto lines = s.lineSplitter(); |
| 303 |
static immutable witness = ["", "peter", "", "paul", "jerry", "ice", "cream", "", "sunday", "mon\u2030day"]; |
| 304 |
uint i; |
| 305 |
foreach (line; lines) |
| 306 |
{ |
| 307 |
assert(line == witness[i++]); |
| 308 |
} |
| 309 |
assert(i == witness.length); |
| 310 |
} |
| 311 |
|
| 312 |
nothrow @safe pure unittest |
| 313 |
{ |
| 314 |
import std.string; |
| 315 |
|
| 316 |
import std.uni : lineSep, paraSep; |
| 317 |
assert(stripLeft(" hello world ") == |
| 318 |
"hello world "); |
| 319 |
assert(stripLeft("\n\t\v\rhello world\n\t\v\r") == |
| 320 |
"hello world\n\t\v\r"); |
| 321 |
assert(stripLeft(" \u2028hello world") == |
| 322 |
"hello world"); |
| 323 |
assert(stripLeft("hello world") == |
| 324 |
"hello world"); |
| 325 |
assert(stripLeft([lineSep] ~ "hello world" ~ lineSep) == |
| 326 |
"hello world" ~ [lineSep]); |
| 327 |
assert(stripLeft([paraSep] ~ "hello world" ~ paraSep) == |
| 328 |
"hello world" ~ [paraSep]); |
| 329 |
|
| 330 |
import std.array : array; |
| 331 |
import std.utf : byChar; |
| 332 |
assert(stripLeft(" hello world "w.byChar).array == |
| 333 |
"hello world "); |
| 334 |
assert(stripLeft(" \u2022hello world ".byChar).array == |
| 335 |
"\u2022hello world "); |
| 336 |
} |
| 337 |
|
| 338 |
@safe pure unittest |
| 339 |
{ |
| 340 |
import std.string; |
| 341 |
|
| 342 |
assert(stripLeft(" hello world ", " ") == |
| 343 |
"hello world "); |
| 344 |
assert(stripLeft("xxxxxhello world ", "x") == |
| 345 |
"hello world "); |
| 346 |
assert(stripLeft("xxxyy hello world ", "xy ") == |
| 347 |
"hello world "); |
| 348 |
} |
| 349 |
|
| 350 |
@safe pure unittest |
| 351 |
{ |
| 352 |
import std.string; |
| 353 |
|
| 354 |
import std.array : array; |
| 355 |
import std.utf : byChar, byWchar, byDchar; |
| 356 |
|
| 357 |
assert(stripLeft(" xxxyy hello world "w.byChar, "xy ").array == |
| 358 |
"hello world "); |
| 359 |
|
| 360 |
assert(stripLeft("\u2028\u2020hello world\u2028"w.byWchar, |
| 361 |
"\u2028").array == "\u2020hello world\u2028"); |
| 362 |
assert(stripLeft("\U00010001hello world"w.byWchar, " ").array == |
| 363 |
"\U00010001hello world"w); |
| 364 |
assert(stripLeft("\U00010001 xyhello world"d.byDchar, |
| 365 |
"\U00010001 xy").array == "hello world"d); |
| 366 |
|
| 367 |
assert(stripLeft("\u2020hello"w, "\u2020"w) == "hello"w); |
| 368 |
assert(stripLeft("\U00010001hello"d, "\U00010001"d) == "hello"d); |
| 369 |
assert(stripLeft(" hello ", "") == " hello "); |
| 370 |
} |
| 371 |
|
| 372 |
nothrow @safe pure unittest |
| 373 |
{ |
| 374 |
import std.string; |
| 375 |
|
| 376 |
import std.uni : lineSep, paraSep; |
| 377 |
assert(stripRight(" hello world ") == |
| 378 |
" hello world"); |
| 379 |
assert(stripRight("\n\t\v\rhello world\n\t\v\r") == |
| 380 |
"\n\t\v\rhello world"); |
| 381 |
assert(stripRight("hello world") == |
| 382 |
"hello world"); |
| 383 |
assert(stripRight([lineSep] ~ "hello world" ~ lineSep) == |
| 384 |
[lineSep] ~ "hello world"); |
| 385 |
assert(stripRight([paraSep] ~ "hello world" ~ paraSep) == |
| 386 |
[paraSep] ~ "hello world"); |
| 387 |
} |
| 388 |
|
| 389 |
@safe pure unittest |
| 390 |
{ |
| 391 |
import std.string; |
| 392 |
|
| 393 |
assert(stripRight(" hello world ", "x") == |
| 394 |
" hello world "); |
| 395 |
assert(stripRight(" hello world ", " ") == |
| 396 |
" hello world"); |
| 397 |
assert(stripRight(" hello worldxy ", "xy ") == |
| 398 |
" hello world"); |
| 399 |
} |
| 400 |
|
| 401 |
@safe pure unittest |
| 402 |
{ |
| 403 |
import std.string; |
| 404 |
|
| 405 |
import std.uni : lineSep, paraSep; |
| 406 |
assert(strip(" hello world ") == |
| 407 |
"hello world"); |
| 408 |
assert(strip("\n\t\v\rhello world\n\t\v\r") == |
| 409 |
"hello world"); |
| 410 |
assert(strip("hello world") == |
| 411 |
"hello world"); |
| 412 |
assert(strip([lineSep] ~ "hello world" ~ [lineSep]) == |
| 413 |
"hello world"); |
| 414 |
assert(strip([paraSep] ~ "hello world" ~ [paraSep]) == |
| 415 |
"hello world"); |
| 416 |
} |
| 417 |
|
| 418 |
@safe pure unittest |
| 419 |
{ |
| 420 |
import std.string; |
| 421 |
|
| 422 |
assert(strip(" hello world ", "x") == |
| 423 |
" hello world "); |
| 424 |
assert(strip(" hello world ", " ") == |
| 425 |
"hello world"); |
| 426 |
assert(strip(" xyxyhello worldxyxy ", "xy ") == |
| 427 |
"hello world"); |
| 428 |
assert(strip("\u2020hello\u2020"w, "\u2020"w) == "hello"w); |
| 429 |
assert(strip("\U00010001hello\U00010001"d, "\U00010001"d) == "hello"d); |
| 430 |
assert(strip(" hello ", "") == " hello "); |
| 431 |
} |
| 432 |
|
| 433 |
@safe pure unittest |
| 434 |
{ |
| 435 |
import std.string; |
| 436 |
|
| 437 |
assert(strip("xxhelloyy", "x", "y") == "hello"); |
| 438 |
assert(strip(" xyxyhello worldxyxyzz ", "xy ", "xyz ") == |
| 439 |
"hello world"); |
| 440 |
assert(strip("\u2020hello\u2028"w, "\u2020"w, "\u2028"w) == "hello"w); |
| 441 |
assert(strip("\U00010001hello\U00010002"d, "\U00010001"d, "\U00010002"d) == |
| 442 |
"hello"d); |
| 443 |
assert(strip(" hello ", "", "") == " hello "); |
| 444 |
} |
| 445 |
|
| 446 |
@safe pure unittest |
| 447 |
{ |
| 448 |
import std.string; |
| 449 |
|
| 450 |
import std.uni : lineSep, paraSep, nelSep; |
| 451 |
import std.utf : decode; |
| 452 |
assert(chomp(" hello world \n\r") == " hello world \n"); |
| 453 |
assert(chomp(" hello world \r\n") == " hello world "); |
| 454 |
assert(chomp(" hello world \f") == " hello world "); |
| 455 |
assert(chomp(" hello world \v") == " hello world "); |
| 456 |
assert(chomp(" hello world \n\n") == " hello world \n"); |
| 457 |
assert(chomp(" hello world \n\n ") == " hello world \n\n "); |
| 458 |
assert(chomp(" hello world \n\n" ~ [lineSep]) == " hello world \n\n"); |
| 459 |
assert(chomp(" hello world \n\n" ~ [paraSep]) == " hello world \n\n"); |
| 460 |
assert(chomp(" hello world \n\n" ~ [ nelSep]) == " hello world \n\n"); |
| 461 |
assert(chomp(" hello world ") == " hello world "); |
| 462 |
assert(chomp(" hello world") == " hello world"); |
| 463 |
assert(chomp("") == ""); |
| 464 |
|
| 465 |
assert(chomp(" hello world", "orld") == " hello w"); |
| 466 |
assert(chomp(" hello world", " he") == " hello world"); |
| 467 |
assert(chomp("", "hello") == ""); |
| 468 |
|
| 469 |
// Don't decode pointlessly |
| 470 |
assert(chomp("hello\xFE", "\r") == "hello\xFE"); |
| 471 |
} |
| 472 |
|
| 473 |
@safe pure unittest |
| 474 |
{ |
| 475 |
import std.string; |
| 476 |
|
| 477 |
assert(chompPrefix("hello world", "he") == "llo world"); |
| 478 |
assert(chompPrefix("hello world", "hello w") == "orld"); |
| 479 |
assert(chompPrefix("hello world", " world") == "hello world"); |
| 480 |
assert(chompPrefix("", "hello") == ""); |
| 481 |
} |
| 482 |
|
| 483 |
@safe pure unittest |
| 484 |
{ |
| 485 |
import std.string; |
| 486 |
|
| 487 |
assert(chop("hello world") == "hello worl"); |
| 488 |
assert(chop("hello world\n") == "hello world"); |
| 489 |
assert(chop("hello world\r") == "hello world"); |
| 490 |
assert(chop("hello world\n\r") == "hello world\n"); |
| 491 |
assert(chop("hello world\r\n") == "hello world"); |
| 492 |
assert(chop("Walter Bright") == "Walter Brigh"); |
| 493 |
assert(chop("") == ""); |
| 494 |
} |
| 495 |
|
| 496 |
@safe pure unittest |
| 497 |
{ |
| 498 |
import std.string; |
| 499 |
|
| 500 |
assert(leftJustify("hello", 7, 'X') == "helloXX"); |
| 501 |
assert(leftJustify("hello", 2, 'X') == "hello"); |
| 502 |
assert(leftJustify("hello", 9, 'X') == "helloXXXX"); |
| 503 |
} |
| 504 |
|
| 505 |
@safe pure @nogc nothrow unittest |
| 506 |
{ |
| 507 |
import std.string; |
| 508 |
|
| 509 |
import std.algorithm.comparison : equal; |
| 510 |
import std.utf : byChar; |
| 511 |
assert(leftJustifier("hello", 2).equal("hello".byChar)); |
| 512 |
assert(leftJustifier("hello", 7).equal("hello ".byChar)); |
| 513 |
assert(leftJustifier("hello", 7, 'x').equal("helloxx".byChar)); |
| 514 |
} |
| 515 |
|
| 516 |
@safe pure unittest |
| 517 |
{ |
| 518 |
import std.string; |
| 519 |
|
| 520 |
assert(rightJustify("hello", 7, 'X') == "XXhello"); |
| 521 |
assert(rightJustify("hello", 2, 'X') == "hello"); |
| 522 |
assert(rightJustify("hello", 9, 'X') == "XXXXhello"); |
| 523 |
} |
| 524 |
|
| 525 |
@safe pure @nogc nothrow unittest |
| 526 |
{ |
| 527 |
import std.string; |
| 528 |
|
| 529 |
import std.algorithm.comparison : equal; |
| 530 |
import std.utf : byChar; |
| 531 |
assert(rightJustifier("hello", 2).equal("hello".byChar)); |
| 532 |
assert(rightJustifier("hello", 7).equal(" hello".byChar)); |
| 533 |
assert(rightJustifier("hello", 7, 'x').equal("xxhello".byChar)); |
| 534 |
} |
| 535 |
|
| 536 |
@safe pure unittest |
| 537 |
{ |
| 538 |
import std.string; |
| 539 |
|
| 540 |
assert(center("hello", 7, 'X') == "XhelloX"); |
| 541 |
assert(center("hello", 2, 'X') == "hello"); |
| 542 |
assert(center("hello", 9, 'X') == "XXhelloXX"); |
| 543 |
} |
| 544 |
|
| 545 |
@safe pure @nogc nothrow unittest |
| 546 |
{ |
| 547 |
import std.string; |
| 548 |
|
| 549 |
import std.algorithm.comparison : equal; |
| 550 |
import std.utf : byChar; |
| 551 |
assert(centerJustifier("hello", 2).equal("hello".byChar)); |
| 552 |
assert(centerJustifier("hello", 8).equal(" hello ".byChar)); |
| 553 |
assert(centerJustifier("hello", 7, 'x').equal("xhellox".byChar)); |
| 554 |
} |
| 555 |
|
| 556 |
@safe pure unittest |
| 557 |
{ |
| 558 |
import std.string; |
| 559 |
|
| 560 |
assert(detab(" \n\tx", 9) == " \n x"); |
| 561 |
} |
| 562 |
|
| 563 |
@safe pure unittest |
| 564 |
{ |
| 565 |
import std.string; |
| 566 |
|
| 567 |
import std.array : array; |
| 568 |
|
| 569 |
assert(detabber(" \n\tx", 9).array == " \n x"); |
| 570 |
} |
| 571 |
|
| 572 |
@safe pure unittest |
| 573 |
{ |
| 574 |
import std.string; |
| 575 |
|
| 576 |
import std.array : array; |
| 577 |
import std.utf : byChar, byWchar; |
| 578 |
|
| 579 |
assert(detabber(" \u2029\t".byChar, 9).array == " \u2029 "); |
| 580 |
auto r = "hel\tx".byWchar.detabber(); |
| 581 |
assert(r.front == 'h'); |
| 582 |
auto s = r.save; |
| 583 |
r.popFront(); |
| 584 |
r.popFront(); |
| 585 |
assert(r.front == 'l'); |
| 586 |
assert(s.front == 'h'); |
| 587 |
} |
| 588 |
|
| 589 |
@safe pure unittest |
| 590 |
{ |
| 591 |
import std.string; |
| 592 |
|
| 593 |
assert(entab(" x \n") == "\tx\n"); |
| 594 |
} |
| 595 |
|
| 596 |
@safe pure unittest |
| 597 |
{ |
| 598 |
import std.string; |
| 599 |
|
| 600 |
import std.array : array; |
| 601 |
assert(entabber(" x \n").array == "\tx\n"); |
| 602 |
} |
| 603 |
|
| 604 |
@safe pure unittest |
| 605 |
{ |
| 606 |
import std.string; |
| 607 |
|
| 608 |
dchar[dchar] transTable1 = ['e' : '5', 'o' : '7', '5': 'q']; |
| 609 |
assert(translate("hello world", transTable1) == "h5ll7 w7rld"); |
| 610 |
|
| 611 |
assert(translate("hello world", transTable1, "low") == "h5 rd"); |
| 612 |
|
| 613 |
string[dchar] transTable2 = ['e' : "5", 'o' : "orange"]; |
| 614 |
assert(translate("hello world", transTable2) == "h5llorange worangerld"); |
| 615 |
} |
| 616 |
|
| 617 |
@safe pure unittest |
| 618 |
{ |
| 619 |
import std.string; |
| 620 |
|
| 621 |
import std.array : appender; |
| 622 |
dchar[dchar] transTable1 = ['e' : '5', 'o' : '7', '5': 'q']; |
| 623 |
auto buffer = appender!(dchar[])(); |
| 624 |
translate("hello world", transTable1, null, buffer); |
| 625 |
assert(buffer.data == "h5ll7 w7rld"); |
| 626 |
|
| 627 |
buffer.clear(); |
| 628 |
translate("hello world", transTable1, "low", buffer); |
| 629 |
assert(buffer.data == "h5 rd"); |
| 630 |
|
| 631 |
buffer.clear(); |
| 632 |
string[dchar] transTable2 = ['e' : "5", 'o' : "orange"]; |
| 633 |
translate("hello world", transTable2, null, buffer); |
| 634 |
assert(buffer.data == "h5llorange worangerld"); |
| 635 |
} |
| 636 |
|
| 637 |
@safe pure nothrow unittest |
| 638 |
{ |
| 639 |
import std.string; |
| 640 |
|
| 641 |
auto transTable1 = makeTrans("eo5", "57q"); |
| 642 |
assert(translate("hello world", transTable1) == "h5ll7 w7rld"); |
| 643 |
|
| 644 |
assert(translate("hello world", transTable1, "low") == "h5 rd"); |
| 645 |
} |
| 646 |
|
| 647 |
@safe pure nothrow unittest |
| 648 |
{ |
| 649 |
import std.string; |
| 650 |
|
| 651 |
auto transTable1 = makeTrans("eo5", "57q"); |
| 652 |
assert(translate("hello world", transTable1) == "h5ll7 w7rld"); |
| 653 |
|
| 654 |
assert(translate("hello world", transTable1, "low") == "h5 rd"); |
| 655 |
} |
| 656 |
|
| 657 |
@safe pure unittest |
| 658 |
{ |
| 659 |
import std.string; |
| 660 |
|
| 661 |
assert(translate("hello world", makeTransTable("hl", "q5")) == "qe55o wor5d"); |
| 662 |
assert(translate("hello world", makeTransTable("12345", "67890")) == "hello world"); |
| 663 |
} |
| 664 |
|
| 665 |
@safe pure unittest |
| 666 |
{ |
| 667 |
import std.string; |
| 668 |
|
| 669 |
import std.array : appender; |
| 670 |
auto buffer = appender!(char[])(); |
| 671 |
auto transTable1 = makeTransTable("eo5", "57q"); |
| 672 |
translate("hello world", transTable1, null, buffer); |
| 673 |
assert(buffer.data == "h5ll7 w7rld"); |
| 674 |
|
| 675 |
buffer.clear(); |
| 676 |
translate("hello world", transTable1, "low", buffer); |
| 677 |
assert(buffer.data == "h5 rd"); |
| 678 |
} |
| 679 |
|
| 680 |
@safe pure unittest |
| 681 |
{ |
| 682 |
import std.string; |
| 683 |
|
| 684 |
assert(succ("1") == "2"); |
| 685 |
assert(succ("9") == "10"); |
| 686 |
assert(succ("999") == "1000"); |
| 687 |
assert(succ("zz99") == "aaa00"); |
| 688 |
} |
| 689 |
|
| 690 |
@safe pure unittest |
| 691 |
{ |
| 692 |
import std.string; |
| 693 |
|
| 694 |
assert(tr("abcdef", "cd", "CD") == "abCDef"); |
| 695 |
assert(tr("1st March, 2018", "March", "MAR", "s") == "1st MAR, 2018"); |
| 696 |
assert(tr("abcdef", "ef", "", "d") == "abcd"); |
| 697 |
assert(tr("14-Jul-87", "a-zA-Z", " ", "cs") == " Jul "); |
| 698 |
} |
| 699 |
|
| 700 |
@safe @nogc pure nothrow unittest |
| 701 |
{ |
| 702 |
import std.string; |
| 703 |
|
| 704 |
assert(isNumeric("123")); |
| 705 |
assert(isNumeric("123UL")); |
| 706 |
assert(isNumeric("123L")); |
| 707 |
assert(isNumeric("+123U")); |
| 708 |
assert(isNumeric("-123L")); |
| 709 |
} |
| 710 |
|
| 711 |
@safe @nogc pure nothrow unittest |
| 712 |
{ |
| 713 |
import std.string; |
| 714 |
|
| 715 |
assert(isNumeric("+123")); |
| 716 |
assert(isNumeric("-123.01")); |
| 717 |
assert(isNumeric("123.3e-10f")); |
| 718 |
assert(isNumeric("123.3e-10fi")); |
| 719 |
assert(isNumeric("123.3e-10L")); |
| 720 |
|
| 721 |
assert(isNumeric("nan")); |
| 722 |
assert(isNumeric("nani")); |
| 723 |
assert(isNumeric("-inf")); |
| 724 |
} |
| 725 |
|
| 726 |
@safe @nogc pure nothrow unittest |
| 727 |
{ |
| 728 |
import std.string; |
| 729 |
|
| 730 |
assert(isNumeric("-123e-1+456.9e-10Li")); |
| 731 |
assert(isNumeric("+123e+10+456i")); |
| 732 |
assert(isNumeric("123+456")); |
| 733 |
} |
| 734 |
|
| 735 |
@safe pure unittest |
| 736 |
{ |
| 737 |
import std.string; |
| 738 |
|
| 739 |
enum a = isNumeric("123.00E-5+1234.45E-12Li"); |
| 740 |
enum b = isNumeric("12345xxxx890"); |
| 741 |
|
| 742 |
static assert( a); |
| 743 |
static assert(!b); |
| 744 |
} |
| 745 |
|
| 746 |
@safe unittest |
| 747 |
{ |
| 748 |
import std.string; |
| 749 |
|
| 750 |
assert(soundexer("Gauss") == "G200"); |
| 751 |
assert(soundexer("Ghosh") == "G200"); |
| 752 |
|
| 753 |
assert(soundexer("Robert") == "R163"); |
| 754 |
assert(soundexer("Rupert") == "R163"); |
| 755 |
|
| 756 |
assert(soundexer("0123^&^^**&^") == ['\0', '\0', '\0', '\0']); |
| 757 |
} |
| 758 |
|
| 759 |
@safe unittest |
| 760 |
{ |
| 761 |
import std.string; |
| 762 |
|
| 763 |
assert(soundex("Gauss") == "G200"); |
| 764 |
assert(soundex("Ghosh") == "G200"); |
| 765 |
|
| 766 |
assert(soundex("Robert") == "R163"); |
| 767 |
assert(soundex("Rupert") == "R163"); |
| 768 |
|
| 769 |
assert(soundex("0123^&^^**&^") == null); |
| 770 |
} |
| 771 |
|
| 772 |
@safe unittest |
| 773 |
{ |
| 774 |
import std.string; |
| 775 |
|
| 776 |
import std.string; |
| 777 |
|
| 778 |
static string[] list = [ "food", "foxy" ]; |
| 779 |
auto abbrevs = abbrev(list); |
| 780 |
assert(abbrevs == ["fox": "foxy", "food": "food", |
| 781 |
"foxy": "foxy", "foo": "food"]); |
| 782 |
} |
| 783 |
|
| 784 |
@safe pure unittest |
| 785 |
{ |
| 786 |
import std.string; |
| 787 |
|
| 788 |
import std.utf : byChar, byWchar, byDchar; |
| 789 |
|
| 790 |
assert(column("1234 ") == 5); |
| 791 |
assert(column("1234 "w) == 5); |
| 792 |
assert(column("1234 "d) == 5); |
| 793 |
|
| 794 |
assert(column("1234 ".byChar()) == 5); |
| 795 |
assert(column("1234 "w.byWchar()) == 5); |
| 796 |
assert(column("1234 "d.byDchar()) == 5); |
| 797 |
|
| 798 |
// Tab stops are set at 8 spaces by default; tab characters insert enough |
| 799 |
// spaces to bring the column position to the next multiple of 8. |
| 800 |
assert(column("\t") == 8); |
| 801 |
assert(column("1\t") == 8); |
| 802 |
assert(column("\t1") == 9); |
| 803 |
assert(column("123\t") == 8); |
| 804 |
|
| 805 |
// Other tab widths are possible by specifying it explicitly: |
| 806 |
assert(column("\t", 4) == 4); |
| 807 |
assert(column("1\t", 4) == 4); |
| 808 |
assert(column("\t1", 4) == 5); |
| 809 |
assert(column("123\t", 4) == 4); |
| 810 |
|
| 811 |
// New lines reset the column number. |
| 812 |
assert(column("abc\n") == 0); |
| 813 |
assert(column("abc\n1") == 1); |
| 814 |
assert(column("abcdefg\r1234") == 4); |
| 815 |
assert(column("abc\u20281") == 1); |
| 816 |
assert(column("abc\u20291") == 1); |
| 817 |
assert(column("abc\u00851") == 1); |
| 818 |
assert(column("abc\u00861") == 5); |
| 819 |
} |
| 820 |
|
| 821 |
@safe pure unittest |
| 822 |
{ |
| 823 |
import std.string; |
| 824 |
|
| 825 |
assert(wrap("a short string", 7) == "a short\nstring\n"); |
| 826 |
|
| 827 |
// wrap will not break inside of a word, but at the next space |
| 828 |
assert(wrap("a short string", 4) == "a\nshort\nstring\n"); |
| 829 |
|
| 830 |
assert(wrap("a short string", 7, "\t") == "\ta\nshort\nstring\n"); |
| 831 |
assert(wrap("a short string", 7, "\t", " ") == "\ta\n short\n string\n"); |
| 832 |
} |
| 833 |
|
| 834 |
@safe pure unittest |
| 835 |
{ |
| 836 |
import std.string; |
| 837 |
|
| 838 |
enum pretty = q{ |
| 839 |
import std.stdio; |
| 840 |
void main() { |
| 841 |
writeln("Hello"); |
| 842 |
} |
| 843 |
}.outdent(); |
| 844 |
|
| 845 |
enum ugly = q{ |
| 846 |
import std.stdio; |
| 847 |
void main() { |
| 848 |
writeln("Hello"); |
| 849 |
} |
| 850 |
}; |
| 851 |
|
| 852 |
assert(pretty == ugly); |
| 853 |
} |
| 854 |
|
| 855 |
@safe pure unittest |
| 856 |
{ |
| 857 |
import std.string; |
| 858 |
|
| 859 |
auto str1 = [ |
| 860 |
" void main()\n", |
| 861 |
" {\n", |
| 862 |
" test();\n", |
| 863 |
" }\n" |
| 864 |
]; |
| 865 |
auto str1Expected = [ |
| 866 |
"void main()\n", |
| 867 |
"{\n", |
| 868 |
" test();\n", |
| 869 |
"}\n" |
| 870 |
]; |
| 871 |
assert(str1.outdent == str1Expected); |
| 872 |
|
| 873 |
auto str2 = [ |
| 874 |
"void main()\n", |
| 875 |
" {\n", |
| 876 |
" test();\n", |
| 877 |
" }\n" |
| 878 |
]; |
| 879 |
assert(str2.outdent == str2); |
| 880 |
} |
| 881 |
|
| 882 |
@safe pure unittest |
| 883 |
{ |
| 884 |
import std.string; |
| 885 |
|
| 886 |
string a = "Hölo World"; |
| 887 |
immutable(ubyte)[] b = a.representation; |
| 888 |
string c = b.assumeUTF; |
| 889 |
|
| 890 |
assert(c == "Hölo World"); |
| 891 |
} |
| 892 |
|