| 1 |
@safe unittest |
| 2 |
{ |
| 3 |
import std.algorithm.searching; |
| 4 |
|
| 5 |
assert( all!"a & 1"([1, 3, 5, 7, 9])); |
| 6 |
assert(!all!"a & 1"([1, 2, 3, 5, 7, 9])); |
| 7 |
} |
| 8 |
|
| 9 |
@safe unittest |
| 10 |
{ |
| 11 |
import std.algorithm.searching; |
| 12 |
|
| 13 |
int[3] vals = [5, 3, 18]; |
| 14 |
assert( all(vals[])); |
| 15 |
} |
| 16 |
|
| 17 |
@safe unittest |
| 18 |
{ |
| 19 |
import std.algorithm.searching; |
| 20 |
|
| 21 |
import std.ascii : isWhite; |
| 22 |
assert( all!(any!isWhite)(["a a", "b b"])); |
| 23 |
assert(!any!(all!isWhite)(["a a", "b b"])); |
| 24 |
} |
| 25 |
|
| 26 |
@safe unittest |
| 27 |
{ |
| 28 |
import std.algorithm.searching; |
| 29 |
|
| 30 |
int[3] vals1 = [0, 0, 0]; |
| 31 |
assert(!any(vals1[])); //none of vals1 evaluate to true |
| 32 |
|
| 33 |
int[3] vals2 = [2, 0, 2]; |
| 34 |
assert( any(vals2[])); |
| 35 |
assert(!all(vals2[])); |
| 36 |
|
| 37 |
int[3] vals3 = [3, 3, 3]; |
| 38 |
assert( any(vals3[])); |
| 39 |
assert( all(vals3[])); |
| 40 |
} |
| 41 |
|
| 42 |
@safe pure unittest |
| 43 |
{ |
| 44 |
import std.algorithm.searching; |
| 45 |
|
| 46 |
auto s = "1 + (2 * (3 + 1 / 2)"; |
| 47 |
assert(!balancedParens(s, '(', ')')); |
| 48 |
s = "1 + (2 * (3 + 1) / 2)"; |
| 49 |
assert(balancedParens(s, '(', ')')); |
| 50 |
s = "1 + (2 * (3 + 1) / 2)"; |
| 51 |
assert(!balancedParens(s, '(', ')', 0)); |
| 52 |
s = "1 + (2 * 3 + 1) / (2 - 5)"; |
| 53 |
assert(balancedParens(s, '(', ')', 0)); |
| 54 |
s = "f(x) = ⌈x⌉"; |
| 55 |
assert(balancedParens(s, '⌈', '⌉')); |
| 56 |
} |
| 57 |
|
| 58 |
@safe pure nothrow unittest |
| 59 |
{ |
| 60 |
import std.algorithm.searching; |
| 61 |
|
| 62 |
auto bmFinder = boyerMooreFinder("TG"); |
| 63 |
|
| 64 |
string r = "TAGTGCCTGA"; |
| 65 |
// search for the first match in the haystack r |
| 66 |
r = bmFinder.beFound(r); |
| 67 |
assert(r == "TGCCTGA"); |
| 68 |
|
| 69 |
// continue search in haystack |
| 70 |
r = bmFinder.beFound(r[2 .. $]); |
| 71 |
assert(r == "TGA"); |
| 72 |
} |
| 73 |
|
| 74 |
@safe unittest |
| 75 |
{ |
| 76 |
import std.algorithm.searching; |
| 77 |
|
| 78 |
assert(commonPrefix("hello, world", "hello, there") == "hello, "); |
| 79 |
} |
| 80 |
|
| 81 |
@safe unittest |
| 82 |
{ |
| 83 |
import std.algorithm.searching; |
| 84 |
|
| 85 |
// count elements in range |
| 86 |
int[] a = [ 1, 2, 4, 3, 2, 5, 3, 2, 4 ]; |
| 87 |
assert(count(a, 2) == 3); |
| 88 |
assert(count!("a > b")(a, 2) == 5); |
| 89 |
} |
| 90 |
|
| 91 |
@safe unittest |
| 92 |
{ |
| 93 |
import std.algorithm.searching; |
| 94 |
|
| 95 |
import std.uni : toLower; |
| 96 |
// count range in range |
| 97 |
assert(count("abcadfabf", "ab") == 2); |
| 98 |
assert(count("ababab", "abab") == 1); |
| 99 |
assert(count("ababab", "abx") == 0); |
| 100 |
// fuzzy count range in range |
| 101 |
assert(count!((a, b) => toLower(a) == toLower(b))("AbcAdFaBf", "ab") == 2); |
| 102 |
} |
| 103 |
|
| 104 |
@safe unittest |
| 105 |
{ |
| 106 |
import std.algorithm.searching; |
| 107 |
|
| 108 |
// count elements in range |
| 109 |
int[] a = [ 1, 2, 4, 3, 2, 5, 3, 2, 4 ]; |
| 110 |
assert(count(a) == 9); |
| 111 |
// count predicate in range |
| 112 |
assert(count!("a > 2")(a) == 5); |
| 113 |
} |
| 114 |
|
| 115 |
@safe unittest |
| 116 |
{ |
| 117 |
import std.algorithm.searching; |
| 118 |
|
| 119 |
assert(countUntil("hello world", "world") == 6); |
| 120 |
assert(countUntil("hello world", 'r') == 8); |
| 121 |
assert(countUntil("hello world", "programming") == -1); |
| 122 |
assert(countUntil("日本語", "本語") == 1); |
| 123 |
assert(countUntil("日本語", '語') == 2); |
| 124 |
assert(countUntil("日本語", "五") == -1); |
| 125 |
assert(countUntil("日本語", '五') == -1); |
| 126 |
assert(countUntil([0, 7, 12, 22, 9], [12, 22]) == 2); |
| 127 |
assert(countUntil([0, 7, 12, 22, 9], 9) == 4); |
| 128 |
assert(countUntil!"a > b"([0, 7, 12, 22, 9], 20) == 3); |
| 129 |
|
| 130 |
// supports multiple needles |
| 131 |
auto res = "...hello".countUntil("ha", "he"); |
| 132 |
assert(res.steps == 3); |
| 133 |
assert(res.needle == 1); |
| 134 |
|
| 135 |
// returns -1 if no needle was found |
| 136 |
res = "hello".countUntil("ha", "hu"); |
| 137 |
assert(res.steps == -1); |
| 138 |
assert(res.needle == -1); |
| 139 |
} |
| 140 |
|
| 141 |
@safe unittest |
| 142 |
{ |
| 143 |
import std.algorithm.searching; |
| 144 |
|
| 145 |
import std.ascii : isDigit; |
| 146 |
import std.uni : isWhite; |
| 147 |
|
| 148 |
assert(countUntil!(isWhite)("hello world") == 5); |
| 149 |
assert(countUntil!(isDigit)("hello world") == -1); |
| 150 |
assert(countUntil!"a > 20"([0, 7, 12, 22, 9]) == 3); |
| 151 |
} |
| 152 |
|
| 153 |
@safe unittest |
| 154 |
{ |
| 155 |
import std.algorithm.searching; |
| 156 |
|
| 157 |
import std.ascii : isAlpha; |
| 158 |
assert("abc".endsWith!(a => a.isAlpha)); |
| 159 |
assert("abc".endsWith!isAlpha); |
| 160 |
|
| 161 |
assert(!"ab1".endsWith!(a => a.isAlpha)); |
| 162 |
|
| 163 |
assert(!"ab1".endsWith!isAlpha); |
| 164 |
assert(!"".endsWith!(a => a.isAlpha)); |
| 165 |
|
| 166 |
import std.algorithm.comparison : among; |
| 167 |
assert("abc".endsWith!(a => a.among('c', 'd') != 0)); |
| 168 |
assert(!"abc".endsWith!(a => a.among('a', 'b') != 0)); |
| 169 |
|
| 170 |
assert(endsWith("abc", "")); |
| 171 |
assert(!endsWith("abc", "b")); |
| 172 |
assert(endsWith("abc", "a", 'c') == 2); |
| 173 |
assert(endsWith("abc", "c", "a") == 1); |
| 174 |
assert(endsWith("abc", "c", "c") == 1); |
| 175 |
assert(endsWith("abc", "bc", "c") == 2); |
| 176 |
assert(endsWith("abc", "x", "c", "b") == 2); |
| 177 |
assert(endsWith("abc", "x", "aa", "bc") == 3); |
| 178 |
assert(endsWith("abc", "x", "aaa", "sab") == 0); |
| 179 |
assert(endsWith("abc", "x", "aaa", 'c', "sab") == 3); |
| 180 |
} |
| 181 |
|
| 182 |
@safe unittest |
| 183 |
{ |
| 184 |
import std.algorithm.searching; |
| 185 |
|
| 186 |
auto arr = [ 1, 2, 3, 4, 1 ]; |
| 187 |
assert(find!("a > 2")(arr) == [ 3, 4, 1 ]); |
| 188 |
|
| 189 |
// with predicate alias |
| 190 |
bool pred(int e) => e + 1 > 1.5; |
| 191 |
assert(find!(pred)(arr) == arr); |
| 192 |
} |
| 193 |
|
| 194 |
@safe unittest |
| 195 |
{ |
| 196 |
import std.algorithm.searching; |
| 197 |
|
| 198 |
import std.range.primitives; |
| 199 |
|
| 200 |
auto arr = [1, 2, 4, 4, 4, 4, 5, 6, 9]; |
| 201 |
assert(arr.find(4) == [4, 4, 4, 4, 5, 6, 9]); |
| 202 |
assert(arr.find(1) == arr); |
| 203 |
assert(arr.find(9) == [9]); |
| 204 |
assert(arr.find!((e, n) => e > n)(4) == [5, 6, 9]); |
| 205 |
assert(arr.find!((e, n) => e < n)(4) == arr); |
| 206 |
assert(arr.find(0).empty); |
| 207 |
assert(arr.find(10).empty); |
| 208 |
assert(arr.find(8).empty); |
| 209 |
|
| 210 |
assert(find("hello, world", ',') == ", world"); |
| 211 |
} |
| 212 |
|
| 213 |
@safe unittest |
| 214 |
{ |
| 215 |
import std.algorithm.searching; |
| 216 |
|
| 217 |
import std.range.primitives; |
| 218 |
import std.uni : toLower; |
| 219 |
|
| 220 |
string[] s = ["Hello", "world", "!"]; |
| 221 |
assert(s.find!((e, n) => toLower(e) == n)("hello") == s); |
| 222 |
} |
| 223 |
|
| 224 |
@safe unittest |
| 225 |
{ |
| 226 |
import std.algorithm.searching; |
| 227 |
|
| 228 |
import std.container : SList; |
| 229 |
import std.range.primitives : empty; |
| 230 |
import std.typecons : Tuple; |
| 231 |
|
| 232 |
assert(find("hello, world", "World").empty); |
| 233 |
assert(find("hello, world", "wo") == "world"); |
| 234 |
assert([1, 2, 3, 4].find(SList!int(2, 3)[]) == [2, 3, 4]); |
| 235 |
alias C = Tuple!(int, "x", int, "y"); |
| 236 |
auto a = [C(1,0), C(2,0), C(3,1), C(4,0)]; |
| 237 |
assert(a.find!"a.x == b"([2, 3]) == [C(2,0), C(3,1), C(4,0)]); |
| 238 |
assert(a[1 .. $].find!"a.x == b"([2, 3]) == [C(2,0), C(3,1), C(4,0)]); |
| 239 |
} |
| 240 |
|
| 241 |
@safe unittest |
| 242 |
{ |
| 243 |
import std.algorithm.searching; |
| 244 |
|
| 245 |
import std.typecons : tuple; |
| 246 |
int[] a = [ 1, 4, 2, 3 ]; |
| 247 |
assert(find(a, 4) == [ 4, 2, 3 ]); |
| 248 |
assert(find(a, [ 1, 4 ]) == [ 1, 4, 2, 3 ]); |
| 249 |
assert(find(a, [ 1, 3 ], 4) == tuple([ 4, 2, 3 ], 2)); |
| 250 |
// Mixed types allowed if comparable |
| 251 |
assert(find(a, 5, [ 1.2, 3.5 ], 2.0) == tuple([ 2, 3 ], 3)); |
| 252 |
} |
| 253 |
|
| 254 |
@safe unittest |
| 255 |
{ |
| 256 |
import std.algorithm.searching; |
| 257 |
|
| 258 |
import std.range.primitives : empty; |
| 259 |
int[] a = [ -1, 0, 1, 2, 3, 4, 5 ]; |
| 260 |
int[] b = [ 1, 2, 3 ]; |
| 261 |
|
| 262 |
assert(find(a, boyerMooreFinder(b)) == [ 1, 2, 3, 4, 5 ]); |
| 263 |
assert(find(b, boyerMooreFinder(a)).empty); |
| 264 |
} |
| 265 |
|
| 266 |
@safe unittest |
| 267 |
{ |
| 268 |
import std.algorithm.searching; |
| 269 |
|
| 270 |
const arr = [0, 1, 2, 3]; |
| 271 |
assert(canFind(arr, 2)); |
| 272 |
assert(!canFind(arr, 4)); |
| 273 |
|
| 274 |
// find one of several needles |
| 275 |
assert(arr.canFind(3, 2)); |
| 276 |
assert(arr.canFind(3, 2) == 2); // second needle found |
| 277 |
assert(arr.canFind([1, 3], 2) == 2); |
| 278 |
|
| 279 |
assert(canFind(arr, [1, 2], [2, 3])); |
| 280 |
assert(canFind(arr, [1, 2], [2, 3]) == 1); |
| 281 |
assert(canFind(arr, [1, 7], [2, 3])); |
| 282 |
assert(canFind(arr, [1, 7], [2, 3]) == 2); |
| 283 |
assert(!canFind(arr, [1, 3], [2, 4])); |
| 284 |
assert(canFind(arr, [1, 3], [2, 4]) == 0); |
| 285 |
} |
| 286 |
|
| 287 |
@safe unittest |
| 288 |
{ |
| 289 |
import std.algorithm.searching; |
| 290 |
|
| 291 |
auto words = [ |
| 292 |
"apple", |
| 293 |
"beeswax", |
| 294 |
"cardboard" |
| 295 |
]; |
| 296 |
assert(!canFind(words, "bees")); |
| 297 |
assert( canFind!((string elem, string needle) => elem.startsWith(needle))(words, "bees")); |
| 298 |
} |
| 299 |
|
| 300 |
@safe unittest |
| 301 |
{ |
| 302 |
import std.algorithm.searching; |
| 303 |
|
| 304 |
string s1 = "aaa111aaa"; |
| 305 |
string s2 = "aaa222aaa"; |
| 306 |
string s3 = "aaa333aaa"; |
| 307 |
string s4 = "aaa444aaa"; |
| 308 |
const hay = [s1, s2, s3, s4]; |
| 309 |
assert(hay.canFind!(e => e.canFind("111", "222"))); |
| 310 |
} |
| 311 |
|
| 312 |
@safe unittest |
| 313 |
{ |
| 314 |
import std.algorithm.searching; |
| 315 |
|
| 316 |
int[] a = [ 11, 10, 10, 9, 8, 8, 7, 8, 9 ]; |
| 317 |
auto r = findAdjacent(a); |
| 318 |
assert(r == [ 10, 10, 9, 8, 8, 7, 8, 9 ]); |
| 319 |
auto p = findAdjacent!("a < b")(a); |
| 320 |
assert(p == [ 7, 8, 9 ]); |
| 321 |
|
| 322 |
} |
| 323 |
|
| 324 |
@safe unittest |
| 325 |
{ |
| 326 |
import std.algorithm.searching; |
| 327 |
|
| 328 |
int[] a = [ -1, 0, 1, 2, 3, 4, 5 ]; |
| 329 |
int[] b = [ 3, 1, 2 ]; |
| 330 |
assert(findAmong(a, b) == a[2 .. $]); |
| 331 |
} |
| 332 |
|
| 333 |
@safe unittest |
| 334 |
{ |
| 335 |
import std.algorithm.searching; |
| 336 |
|
| 337 |
import std.range.primitives : empty; |
| 338 |
// Needle is found; s is replaced by the substring following the first |
| 339 |
// occurrence of the needle. |
| 340 |
string s = "abcdef"; |
| 341 |
assert(findSkip(s, "cd") && s == "ef"); |
| 342 |
|
| 343 |
// Needle is not found; s is left untouched. |
| 344 |
s = "abcdef"; |
| 345 |
assert(!findSkip(s, "cxd") && s == "abcdef"); |
| 346 |
|
| 347 |
// If the needle occurs at the end of the range, the range is left empty. |
| 348 |
s = "abcdef"; |
| 349 |
assert(findSkip(s, "def") && s.empty); |
| 350 |
} |
| 351 |
|
| 352 |
@safe unittest |
| 353 |
{ |
| 354 |
import std.algorithm.searching; |
| 355 |
|
| 356 |
import std.ascii : isWhite; |
| 357 |
string s = " abc"; |
| 358 |
assert(findSkip!isWhite(s) && s == "abc"); |
| 359 |
assert(!findSkip!isWhite(s) && s == "abc"); |
| 360 |
|
| 361 |
s = " "; |
| 362 |
assert(findSkip!isWhite(s) == 2); |
| 363 |
} |
| 364 |
|
| 365 |
@safe pure nothrow unittest |
| 366 |
{ |
| 367 |
import std.algorithm.searching; |
| 368 |
|
| 369 |
// findSplit returns a triplet |
| 370 |
if (auto split = "dlang-rocks".findSplit("-")) |
| 371 |
{ |
| 372 |
assert(split[0] == "dlang"); |
| 373 |
assert(split[1] == "-"); |
| 374 |
assert(split[2] == "rocks"); |
| 375 |
} |
| 376 |
else assert(0); |
| 377 |
|
| 378 |
// findSplitBefore returns 2 ranges |
| 379 |
if (const split = [2, 3, 2, 3, 4, 1].findSplitBefore!"a > b"([2, 2])) |
| 380 |
{ |
| 381 |
assert(split[0] == [2, 3, 2]); |
| 382 |
// [3, 4] each greater than [2, 2] |
| 383 |
assert(split[1] == [3, 4, 1]); |
| 384 |
} |
| 385 |
else assert(0); |
| 386 |
} |
| 387 |
|
| 388 |
@safe pure nothrow unittest |
| 389 |
{ |
| 390 |
import std.algorithm.searching; |
| 391 |
|
| 392 |
import std.range.primitives : empty; |
| 393 |
|
| 394 |
auto a = "Carl Sagan Memorial Station"; |
| 395 |
auto r = findSplit(a, "Velikovsky"); |
| 396 |
import std.typecons : isTuple; |
| 397 |
static assert(isTuple!(typeof(r.asTuple))); |
| 398 |
static assert(isTuple!(typeof(r))); |
| 399 |
assert(!r); |
| 400 |
assert(r[0] == a); |
| 401 |
assert(r[1].empty); |
| 402 |
assert(r[2].empty); |
| 403 |
r = findSplit(a, " "); |
| 404 |
assert(r[0] == "Carl"); |
| 405 |
assert(r[1] == " "); |
| 406 |
assert(r[2] == "Sagan Memorial Station"); |
| 407 |
if (const r1 = findSplitBefore(a, "Sagan")) |
| 408 |
{ |
| 409 |
assert(r1); |
| 410 |
assert(r1[0] == "Carl "); |
| 411 |
assert(r1[1] == "Sagan Memorial Station"); |
| 412 |
} |
| 413 |
if (const r2 = findSplitAfter(a, "Sagan")) |
| 414 |
{ |
| 415 |
assert(r2); |
| 416 |
assert(r2[0] == "Carl Sagan"); |
| 417 |
assert(r2[1] == " Memorial Station"); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
@safe pure nothrow unittest |
| 422 |
{ |
| 423 |
import std.algorithm.searching; |
| 424 |
|
| 425 |
import std.range : only; |
| 426 |
assert([1, 2, 3, 4].findSplitBefore(only(3))[0] == [1, 2]); |
| 427 |
} |
| 428 |
|
| 429 |
@safe unittest |
| 430 |
{ |
| 431 |
import std.algorithm.searching; |
| 432 |
|
| 433 |
import std.conv : text; |
| 434 |
import std.typecons : tuple; |
| 435 |
|
| 436 |
int[] a = [ 2, 3, 4, 1, 2, 4, 1, 1, 2 ]; |
| 437 |
// Minimum is 1 and occurs 3 times |
| 438 |
assert(a.minCount == tuple(1, 3)); |
| 439 |
// Maximum is 4 and occurs 2 times |
| 440 |
assert(a.maxCount == tuple(4, 2)); |
| 441 |
} |
| 442 |
|
| 443 |
@safe pure unittest |
| 444 |
{ |
| 445 |
import std.algorithm.searching; |
| 446 |
|
| 447 |
import std.range : enumerate; |
| 448 |
import std.typecons : tuple; |
| 449 |
|
| 450 |
assert([2, 7, 1, 3].minElement == 1); |
| 451 |
|
| 452 |
// allows to get the index of an element too |
| 453 |
assert([5, 3, 7, 9].enumerate.minElement!"a.value" == tuple(1, 3)); |
| 454 |
|
| 455 |
// any custom accessor can be passed |
| 456 |
assert([[0, 4], [1, 2]].minElement!"a[1]" == [1, 2]); |
| 457 |
|
| 458 |
// can be seeded |
| 459 |
int[] arr; |
| 460 |
assert(arr.minElement(1) == 1); |
| 461 |
} |
| 462 |
|
| 463 |
@safe pure unittest |
| 464 |
{ |
| 465 |
import std.algorithm.searching; |
| 466 |
|
| 467 |
import std.range : enumerate; |
| 468 |
import std.typecons : tuple; |
| 469 |
assert([2, 1, 4, 3].maxElement == 4); |
| 470 |
|
| 471 |
// allows to get the index of an element too |
| 472 |
assert([2, 1, 4, 3].enumerate.maxElement!"a.value" == tuple(2, 4)); |
| 473 |
|
| 474 |
// any custom accessor can be passed |
| 475 |
assert([[0, 4], [1, 2]].maxElement!"a[1]" == [0, 4]); |
| 476 |
|
| 477 |
// can be seeded |
| 478 |
int[] arr; |
| 479 |
assert(arr.minElement(1) == 1); |
| 480 |
} |
| 481 |
|
| 482 |
@safe unittest |
| 483 |
{ |
| 484 |
import std.algorithm.searching; |
| 485 |
|
| 486 |
assert(extrema([5,2,9,4,1]) == [1, 9]); |
| 487 |
} |
| 488 |
|
| 489 |
@safe unittest |
| 490 |
{ |
| 491 |
import std.algorithm.searching; |
| 492 |
|
| 493 |
int[] a = [ 2, 3, 4, 1, 2, 4, 1, 1, 2 ]; |
| 494 |
// Minimum is 1 and first occurs in position 3 |
| 495 |
assert(a.minPos == [ 1, 2, 4, 1, 1, 2 ]); |
| 496 |
// Maximum is 4 and first occurs in position 2 |
| 497 |
assert(a.maxPos == [ 4, 1, 2, 4, 1, 1, 2 ]); |
| 498 |
} |
| 499 |
|
| 500 |
@safe pure nothrow unittest |
| 501 |
{ |
| 502 |
import std.algorithm.searching; |
| 503 |
|
| 504 |
int[] a = [2, 3, 4, 1, 2, 4, 1, 1, 2]; |
| 505 |
|
| 506 |
// Minimum is 1 and first occurs in position 3 |
| 507 |
assert(a.minIndex == 3); |
| 508 |
// Get maximum index with minIndex |
| 509 |
assert(a.minIndex!"a > b" == 2); |
| 510 |
|
| 511 |
// Range is empty, so return value is -1 |
| 512 |
int[] b; |
| 513 |
assert(b.minIndex == -1); |
| 514 |
|
| 515 |
// Works with more custom types |
| 516 |
struct Dog { int age; } |
| 517 |
Dog[] dogs = [Dog(10), Dog(5), Dog(15)]; |
| 518 |
assert(dogs.minIndex!"a.age < b.age" == 1); |
| 519 |
} |
| 520 |
|
| 521 |
@safe pure nothrow unittest |
| 522 |
{ |
| 523 |
import std.algorithm.searching; |
| 524 |
|
| 525 |
// Maximum is 4 and first occurs in position 2 |
| 526 |
int[] a = [2, 3, 4, 1, 2, 4, 1, 1, 2]; |
| 527 |
assert(a.maxIndex == 2); |
| 528 |
|
| 529 |
// Empty range |
| 530 |
int[] b; |
| 531 |
assert(b.maxIndex == -1); |
| 532 |
|
| 533 |
// Works with more custom types |
| 534 |
struct Dog { int age; } |
| 535 |
Dog[] dogs = [Dog(10), Dog(15), Dog(5)]; |
| 536 |
assert(dogs.maxIndex!"a.age < b.age" == 1); |
| 537 |
} |
| 538 |
|
| 539 |
@safe unittest |
| 540 |
{ |
| 541 |
import std.algorithm.searching; |
| 542 |
|
| 543 |
import std.algorithm.comparison : equal; |
| 544 |
|
| 545 |
auto s1 = "Hello world"; |
| 546 |
assert(!skipOver(s1, "Ha")); |
| 547 |
assert(s1 == "Hello world"); |
| 548 |
assert(skipOver(s1, "Hell") && s1 == "o world", s1); |
| 549 |
|
| 550 |
string[] r1 = ["abc", "def", "hij"]; |
| 551 |
dstring[] r2 = ["abc"d]; |
| 552 |
assert(!skipOver!((a, b) => a.equal(b))(r1, ["def"d]), r1[0]); |
| 553 |
assert(r1 == ["abc", "def", "hij"]); |
| 554 |
assert(skipOver!((a, b) => a.equal(b))(r1, r2)); |
| 555 |
assert(r1 == ["def", "hij"]); |
| 556 |
} |
| 557 |
|
| 558 |
@safe unittest |
| 559 |
{ |
| 560 |
import std.algorithm.searching; |
| 561 |
|
| 562 |
import std.ascii : isWhite; |
| 563 |
import std.range.primitives : empty; |
| 564 |
|
| 565 |
auto s2 = "\t\tvalue"; |
| 566 |
auto s3 = ""; |
| 567 |
auto s4 = "\t\t\t"; |
| 568 |
assert(s2.skipOver!isWhite && s2 == "value"); |
| 569 |
assert(!s3.skipOver!isWhite); |
| 570 |
assert(s4.skipOver!isWhite && s3.empty); |
| 571 |
} |
| 572 |
|
| 573 |
@safe unittest |
| 574 |
{ |
| 575 |
import std.algorithm.searching; |
| 576 |
|
| 577 |
auto s = "Hello world"; |
| 578 |
assert(!skipOver(s, "hello", "HellO")); |
| 579 |
assert(s == "Hello world"); |
| 580 |
|
| 581 |
// the range is skipped over the longest matching needle is skipped |
| 582 |
assert(skipOver(s, "foo", "hell", "Hello ")); |
| 583 |
assert(s == "world"); |
| 584 |
} |
| 585 |
|
| 586 |
@safe unittest |
| 587 |
{ |
| 588 |
import std.algorithm.searching; |
| 589 |
|
| 590 |
import std.algorithm.comparison : equal; |
| 591 |
|
| 592 |
auto s1 = "Hello world"; |
| 593 |
assert(!skipOver(s1, 'a')); |
| 594 |
assert(s1 == "Hello world"); |
| 595 |
assert(skipOver(s1, 'H') && s1 == "ello world"); |
| 596 |
|
| 597 |
string[] r = ["abc", "def", "hij"]; |
| 598 |
dstring e = "abc"d; |
| 599 |
assert(!skipOver!((a, b) => a.equal(b))(r, "def"d)); |
| 600 |
assert(r == ["abc", "def", "hij"]); |
| 601 |
assert(skipOver!((a, b) => a.equal(b))(r, e)); |
| 602 |
assert(r == ["def", "hij"]); |
| 603 |
|
| 604 |
auto s2 = ""; |
| 605 |
assert(!s2.skipOver('a')); |
| 606 |
} |
| 607 |
|
| 608 |
@safe unittest |
| 609 |
{ |
| 610 |
import std.algorithm.searching; |
| 611 |
|
| 612 |
import std.ascii : isWhite; |
| 613 |
import std.range.primitives : empty; |
| 614 |
|
| 615 |
alias whitespaceSkiper = skipOver!isWhite; |
| 616 |
|
| 617 |
auto s2 = "\t\tvalue"; |
| 618 |
auto s3 = ""; |
| 619 |
auto s4 = "\t\t\t"; |
| 620 |
assert(whitespaceSkiper(s2) && s2 == "value"); |
| 621 |
assert(!whitespaceSkiper(s2)); |
| 622 |
assert(whitespaceSkiper(s4) && s3.empty); |
| 623 |
} |
| 624 |
|
| 625 |
@safe unittest |
| 626 |
{ |
| 627 |
import std.algorithm.searching; |
| 628 |
|
| 629 |
import std.ascii : isAlpha; |
| 630 |
|
| 631 |
assert("abc".startsWith!(a => a.isAlpha)); |
| 632 |
assert("abc".startsWith!isAlpha); |
| 633 |
assert(!"1ab".startsWith!(a => a.isAlpha)); |
| 634 |
assert(!"".startsWith!(a => a.isAlpha)); |
| 635 |
|
| 636 |
import std.algorithm.comparison : among; |
| 637 |
assert("abc".startsWith!(a => a.among('a', 'b') != 0)); |
| 638 |
assert(!"abc".startsWith!(a => a.among('b', 'c') != 0)); |
| 639 |
|
| 640 |
assert(startsWith("abc", "")); |
| 641 |
assert(startsWith("abc", "a")); |
| 642 |
assert(!startsWith("abc", "b")); |
| 643 |
assert(startsWith("abc", 'a', "b") == 1); |
| 644 |
assert(startsWith("abc", "b", "a") == 2); |
| 645 |
assert(startsWith("abc", "a", "a") == 1); |
| 646 |
assert(startsWith("abc", "ab", "a") == 2); |
| 647 |
assert(startsWith("abc", "x", "a", "b") == 2); |
| 648 |
assert(startsWith("abc", "x", "aa", "ab") == 3); |
| 649 |
assert(startsWith("abc", "x", "aaa", "sab") == 0); |
| 650 |
assert(startsWith("abc", "x", "aaa", "a", "sab") == 3); |
| 651 |
|
| 652 |
import std.typecons : Tuple; |
| 653 |
alias C = Tuple!(int, "x", int, "y"); |
| 654 |
assert(startsWith!"a.x == b"([ C(1,1), C(1,2), C(2,2) ], [1, 1])); |
| 655 |
assert(startsWith!"a.x == b"([ C(1,1), C(2,1), C(2,2) ], [1, 1], [1, 2], [1, 3]) == 2); |
| 656 |
} |
| 657 |
|
| 658 |
@safe unittest |
| 659 |
{ |
| 660 |
import std.algorithm.searching; |
| 661 |
|
| 662 |
import std.algorithm.comparison : equal; |
| 663 |
import std.typecons : No; |
| 664 |
int[] a = [ 1, 2, 4, 7, 7, 2, 4, 7, 3, 5]; |
| 665 |
assert(equal(a.until(7), [1, 2, 4])); |
| 666 |
assert(equal(a.until(7, No.openRight), [1, 2, 4, 7])); |
| 667 |
} |
| 668 |
|