1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
|
(*---------------------------------------------------------------------------
Copyright (c) 2015 The astring programmers. All rights reserved.
Distributed under the ISC license, see terms at the end of the file.
---------------------------------------------------------------------------*)
open Testing
open Astring
let pp_str_pair ppf (a, b) =
Format.fprintf ppf "@[<1>(%a,%a)@]" String.dump a String.dump b
let pp_pair ppf (a, b) =
Format.fprintf ppf "@[<1>(%a,%a)@]" String.Sub.dump a String.Sub.dump b
let eq_pair (l0, r0) (l1, r1) =
String.Sub.equal l0 l1 && String.Sub.equal r0 r1
let eq_sub = eq ~eq:String.Sub.equal ~pp:String.Sub.dump
let eq_sub_raw = eq ~eq:String.Sub.equal ~pp:String.Sub.dump_raw
let eqs sub s = eq_str (String.Sub.to_string sub) s
let eqb sub s = eq_str (String.Sub.base_string sub) s
let eqs_opt sub os =
let sub_to_str = function
| Some sub -> Some (String.Sub.to_string sub)
| None -> None
in
eq_option ~eq:(=) ~pp:pp_str (sub_to_str sub) os
let eqs_pair_opt subs_pair pair =
let subs_to_str = function
| None -> None
| Some (sub, sub') ->
Some (String.Sub.to_string sub, String.Sub.to_string sub')
in
eq_option ~eq:(=) ~pp:pp_str_pair (subs_to_str subs_pair) pair
let empty_pos s pos =
eq_int (String.Sub.length s) 0;
eq_int (String.Sub.start_pos s) pos
(* Base functions *)
let misc = test "String.Sub misc. base functions" @@ fun () ->
eqs String.Sub.empty "";
eqs (String.Sub.v "abc") "abc";
eqs (String.Sub.v ~start:0 ~stop:1 "abc") "a";
eqs (String.Sub.v ~start:1 ~stop:2 "abc") "b";
eqs (String.Sub.v ~start:1 ~stop:3 "abc") "bc";
eqs (String.Sub.v ~start:2 ~stop:3 "abc") "c";
eqs (String.Sub.v ~start:3 ~stop:3 "abc") "";
let sub = String.Sub.v ~start:2 ~stop:3 "abc" in
eq_int (String.Sub.start_pos sub) 2;
eq_int (String.Sub.stop_pos sub) 3;
eq_int (String.Sub.length sub) 1;
app_invalid ~pp:pp_char (String.Sub.get sub) 3;
app_invalid ~pp:pp_char (String.Sub.get sub) 2;
app_invalid ~pp:pp_char (String.Sub.get sub) 1;
eq_char (String.Sub.get sub 0) 'c';
eq_int (String.Sub.get_byte sub 0) 0x63;
eqb (String.Sub.(rebase (v ~stop:2 "abc"))) "ab";
()
let head = test "String.[get_]head" @@ fun () ->
let empty = String.Sub.v ~start:2 ~stop:2 "abc" in
let bc = String.Sub.v ~start:1 ~stop:3 "abc" in
let eq_ochar = eq_option ~eq:(=) ~pp:pp_char in
eq_ochar (String.Sub.head empty) None;
eq_ochar (String.Sub.head ~rev:true empty) None;
eq_ochar (String.Sub.head bc) (Some 'b');
eq_ochar (String.Sub.head ~rev:true bc) (Some 'c');
eq_char (String.Sub.get_head bc) 'b';
eq_char (String.Sub.get_head ~rev:true bc) 'c';
app_invalid ~pp:pp_char String.Sub.get_head empty;
()
let to_string = test "String.Sub.to_string" @@ fun () ->
let no_alloc s =
let r = String.Sub.to_string s in
eq_bool (r == (String.Sub.base_string s) || r == String.empty) true
in
no_alloc (String.Sub.v ~start:0 ~stop:0 "abc");
eq_str (String.Sub.(to_string (v ~start:0 ~stop:1 "abc"))) "a";
eq_str (String.Sub.(to_string (v ~start:0 ~stop:2 "abc"))) "ab";
no_alloc (String.Sub.v ~start:0 ~stop:3 "abc");
no_alloc (String.Sub.v ~start:1 ~stop:1 "abc");
eq_str (String.Sub.(to_string (v ~start:1 ~stop:2 "abc"))) "b";
eq_str (String.Sub.(to_string (v ~start:1 ~stop:3 "abc"))) "bc";
no_alloc (String.Sub.v ~start:2 ~stop:2 "abc");
eq_str (String.Sub.(to_string (v ~start:2 ~stop:3 "abc"))) "c";
no_alloc (String.Sub.v ~start:3 ~stop:3 "abc");
()
(* Stretching substrings *)
let start = test "String.Sub.start" @@ fun () ->
empty_pos String.Sub.(start @@ v "") 0;
empty_pos String.Sub.(start @@ v ~start:0 ~stop:0 "abc") 0;
empty_pos String.Sub.(start @@ v ~start:0 ~stop:1 "abc") 0;
empty_pos String.Sub.(start @@ v ~start:0 ~stop:2 "abc") 0;
empty_pos String.Sub.(start @@ v ~start:0 ~stop:3 "abc") 0;
empty_pos String.Sub.(start @@ v ~start:1 ~stop:1 "abc") 1;
empty_pos String.Sub.(start @@ v ~start:1 ~stop:2 "abc") 1;
empty_pos String.Sub.(start @@ v ~start:1 ~stop:3 "abc") 1;
empty_pos String.Sub.(start @@ v ~start:2 ~stop:2 "abc") 2;
empty_pos String.Sub.(start @@ v ~start:2 ~stop:3 "abc") 2;
empty_pos String.Sub.(start @@ v ~start:3 ~stop:3 "abc") 3;
()
let stop = test "String.Sub.stop" @@ fun () ->
empty_pos String.Sub.(stop @@ v "") 0;
empty_pos String.Sub.(stop @@ v ~start:0 ~stop:0 "abc") 0;
empty_pos String.Sub.(stop @@ v ~start:0 ~stop:1 "abc") 1;
empty_pos String.Sub.(stop @@ v ~start:0 ~stop:2 "abc") 2;
empty_pos String.Sub.(stop @@ v ~start:0 ~stop:3 "abc") 3;
empty_pos String.Sub.(stop @@ v ~start:1 ~stop:1 "abc") 1;
empty_pos String.Sub.(stop @@ v ~start:1 ~stop:2 "abc") 2;
empty_pos String.Sub.(stop @@ v ~start:1 ~stop:3 "abc") 3;
empty_pos String.Sub.(stop @@ v ~start:2 ~stop:2 "abc") 2;
empty_pos String.Sub.(stop @@ v ~start:2 ~stop:3 "abc") 3;
empty_pos String.Sub.(stop @@ v ~start:3 ~stop:3 "abc") 3;
()
let tail = test "String.Sub.tail" @@ fun () ->
empty_pos String.Sub.(tail @@ v "") 0;
empty_pos String.Sub.(tail @@ v ~start:0 ~stop:0 "abc") 0;
empty_pos String.Sub.(tail @@ v ~start:0 ~stop:1 "abc") 1;
eqs String.Sub.(tail @@ v ~start:0 ~stop:2 "abc") "b";
eqs String.Sub.(tail @@ v ~start:0 ~stop:3 "abc") "bc";
empty_pos String.Sub.(tail @@ v ~start:1 ~stop:1 "abc") 1;
empty_pos String.Sub.(tail @@ v ~start:1 ~stop:2 "abc") 2;
eqs String.Sub.(tail @@ v ~start:1 ~stop:3 "abc") "c";
empty_pos String.Sub.(tail @@ v ~start:2 ~stop:2 "abc") 2;
empty_pos String.Sub.(tail @@ v ~start:2 ~stop:3 "abc") 3;
empty_pos String.Sub.(tail @@ v ~start:3 ~stop:3 "abc") 3;
()
let base = test "String.Sub.base" @@ fun () ->
eqs String.Sub.(base @@ v "") "";
eqs String.Sub.(base @@ v ~start:0 ~stop:0 "abc") "abc";
eqs String.Sub.(base @@ v ~start:0 ~stop:1 "abc") "abc";
eqs String.Sub.(base @@ v ~start:0 ~stop:2 "abc") "abc";
eqs String.Sub.(base @@ v ~start:0 ~stop:3 "abc") "abc";
eqs String.Sub.(base @@ v ~start:1 ~stop:1 "abc") "abc";
eqs String.Sub.(base @@ v ~start:1 ~stop:2 "abc") "abc";
eqs String.Sub.(base @@ v ~start:1 ~stop:3 "abc") "abc";
eqs String.Sub.(base @@ v ~start:2 ~stop:2 "abc") "abc";
eqs String.Sub.(base @@ v ~start:2 ~stop:3 "abc") "abc";
eqs String.Sub.(base @@ v ~start:3 ~stop:3 "abc") "abc";
()
let extend = test "String.Sub.extend" @@ fun () ->
let empty = String.Sub.v ~start:2 ~stop:2 "abcdefg" in
let abcd = String.Sub.v ~start:0 ~stop:4 "abcdefg" in
let cd = String.Sub.v ~start:2 ~stop:4 "abcdefg" in
app_invalid ~pp:String.Sub.pp (fun s -> String.Sub.extend ~max:(-1) s) abcd;
eqs (String.Sub.extend empty) "cdefg";
eqs (String.Sub.extend ~rev:true empty) "ab";
eqs (String.Sub.extend ~max:2 empty) "cd";
eqs (String.Sub.extend ~sat:(fun c -> c < 'f') empty) "cde";
eqs (String.Sub.extend ~rev:true ~max:1 empty) "b";
eqs (String.Sub.extend abcd) "abcdefg";
eqs (String.Sub.extend ~max:2 abcd) "abcdef";
eqs (String.Sub.extend ~max:2 ~sat:(fun c -> c < 'e') abcd) "abcd";
eqs (String.Sub.extend ~max:2 ~sat:(fun c -> c < 'f') abcd) "abcde";
eqs (String.Sub.extend ~max:2 ~sat:(fun c -> c < 'g') abcd) "abcdef";
eqs (String.Sub.extend ~rev:true abcd) "abcd";
eqs (String.Sub.extend ~rev:true ~max:2 ~sat:(fun c -> c > 'a') cd) "bcd";
eqs (String.Sub.extend ~rev:true ~max:2 ~sat:(fun c -> c >= 'a') cd) "abcd";
eqs (String.Sub.extend ~rev:true ~max:1 ~sat:(fun c -> c >= 'a') cd) "bcd";
eqs (String.Sub.extend ~max:2 ~sat:(fun c -> c >= 'a') cd) "cdef";
eqs (String.Sub.extend ~sat:(fun c -> c >= 'a') cd) "cdefg";
()
let reduce = test "String.Sub.reduce" @@ fun () ->
let empty = String.Sub.v ~start:2 ~stop:2 "abcdefg" in
let abcd = String.Sub.v ~start:0 ~stop:4 "abcdefg" in
let cd = String.Sub.v ~start:2 ~stop:4 "abcdefg" in
app_invalid ~pp:String.Sub.pp (fun s -> String.Sub.reduce ~max:(-1) s) abcd;
empty_pos (String.Sub.reduce ~rev:false empty) 2;
empty_pos (String.Sub.reduce ~rev:true empty) 2;
empty_pos (String.Sub.reduce ~rev:false ~max:2 empty) 2;
empty_pos (String.Sub.reduce ~rev:true ~max:2 empty) 2;
empty_pos (String.Sub.reduce ~rev:false ~sat:(fun c -> c < 'f') empty) 2;
empty_pos (String.Sub.reduce ~rev:true ~sat:(fun c -> c < 'f') empty) 2;
empty_pos (String.Sub.reduce ~rev:false ~max:1 empty) 2;
empty_pos (String.Sub.reduce ~rev:true ~max:1 empty) 2;
empty_pos (String.Sub.reduce ~rev:false abcd) 0;
empty_pos (String.Sub.reduce ~rev:true abcd) 4;
eqs (String.Sub.reduce ~rev:false ~max:0 abcd) "abcd";
eqs (String.Sub.reduce ~rev:true ~max:0 abcd) "abcd";
eqs (String.Sub.reduce ~rev:false ~max:0 abcd) "abcd";
eqs (String.Sub.reduce ~rev:true ~max:0 abcd) "abcd";
eqs (String.Sub.reduce ~rev:false ~max:2 abcd) "ab";
eqs (String.Sub.reduce ~rev:true ~max:2 abcd) "cd";
eqs (String.Sub.reduce ~rev:false ~max:2 ~sat:(fun c -> c < 'c') abcd) "abcd";
eqs (String.Sub.reduce ~rev:true ~max:2 ~sat:(fun c -> c < 'c') abcd) "cd";
eqs (String.Sub.reduce ~rev:false ~max:2 ~sat:(fun c -> c > 'b') abcd) "ab";
eqs (String.Sub.reduce ~rev:true ~max:2 ~sat:(fun c -> c < 'b') abcd) "bcd";
eqs (String.Sub.reduce ~rev:false ~max:2 ~sat:(fun c -> c > 'a') abcd) "ab";
eqs (String.Sub.reduce ~rev:true ~max:2 ~sat:(fun c -> c < 'a') abcd) "abcd";
empty_pos (String.Sub.reduce ~rev:false ~max:2 ~sat:(fun c -> c > 'a') cd) 2;
empty_pos (String.Sub.reduce ~rev:true ~max:2 ~sat:(fun c -> c > 'a') cd) 4;
eqs (String.Sub.reduce ~rev:false ~max:2 ~sat:(fun c -> c >= 'd') cd) "c";
eqs (String.Sub.reduce ~rev:true ~max:2 ~sat:(fun c -> c >= 'd') cd) "cd";
eqs (String.Sub.reduce ~rev:false ~max:1 ~sat:(fun c -> c > 'c') cd) "c";
eqs (String.Sub.reduce ~rev:true ~max:1 ~sat:(fun c -> c > 'c') cd) "cd";
eqs (String.Sub.reduce ~rev:false ~sat:(fun c -> c > 'c') cd) "c";
eqs (String.Sub.reduce ~rev:true ~sat:(fun c -> c > 'c') cd) "cd";
()
let extent = test "String.Sub.extent" @@ fun () ->
app_invalid ~pp:String.Sub.dump_raw
String.Sub.(extent (v "a")) (String.Sub.(v "b"));
let abcd = "abcd" in
let e0 = String.Sub.v ~start:0 ~stop:0 abcd in
let a = String.Sub.v ~start:0 ~stop:1 abcd in
let ab = String.Sub.v ~start:0 ~stop:2 abcd in
let abc = String.Sub.v ~start:0 ~stop:3 abcd in
let _abcd = String.Sub.v ~start:0 ~stop:4 abcd in
let e1 = String.Sub.v ~start:1 ~stop:1 abcd in
let b = String.Sub.v ~start:1 ~stop:2 abcd in
let bc = String.Sub.v ~start:1 ~stop:3 abcd in
let bcd = String.Sub.v ~start:1 ~stop:4 abcd in
let e2 = String.Sub.v ~start:2 ~stop:2 abcd in
let c = String.Sub.v ~start:2 ~stop:3 abcd in
let cd = String.Sub.v ~start:2 ~stop:4 abcd in
let e3 = String.Sub.v ~start:3 ~stop:3 abcd in
let d = String.Sub.v ~start:3 ~stop:4 abcd in
let e4 = String.Sub.v ~start:4 ~stop:4 abcd in
empty_pos (String.Sub.extent e0 e0) 0;
eqs (String.Sub.extent e0 e1) "a";
eqs (String.Sub.extent e1 e0) "a";
eqs (String.Sub.extent e0 e2) "ab";
eqs (String.Sub.extent e2 e0) "ab";
eqs (String.Sub.extent e0 e3) "abc";
eqs (String.Sub.extent e3 e0) "abc";
eqs (String.Sub.extent e0 e4) "abcd";
eqs (String.Sub.extent e4 e0) "abcd";
empty_pos (String.Sub.extent e1 e1) 1;
eqs (String.Sub.extent e1 e2) "b";
eqs (String.Sub.extent e2 e1) "b";
eqs (String.Sub.extent e1 e3) "bc";
eqs (String.Sub.extent e3 e1) "bc";
eqs (String.Sub.extent e1 e4) "bcd";
eqs (String.Sub.extent e4 e1) "bcd";
empty_pos (String.Sub.extent e2 e2) 2;
eqs (String.Sub.extent e2 e3) "c";
eqs (String.Sub.extent e3 e2) "c";
eqs (String.Sub.extent e2 e4) "cd";
eqs (String.Sub.extent e4 e2) "cd";
empty_pos (String.Sub.extent e3 e3) 3;
eqs (String.Sub.extent e3 e4) "d";
eqs (String.Sub.extent e4 e3) "d";
empty_pos (String.Sub.extent e4 e4) 4;
eqs (String.Sub.extent a d) "abcd";
eqs (String.Sub.extent d a) "abcd";
eqs (String.Sub.extent b d) "bcd";
eqs (String.Sub.extent d b) "bcd";
eqs (String.Sub.extent c cd) "cd";
eqs (String.Sub.extent cd c) "cd";
eqs (String.Sub.extent e0 _abcd) "abcd";
eqs (String.Sub.extent _abcd e0) "abcd";
eqs (String.Sub.extent ab c) "abc";
eqs (String.Sub.extent c ab) "abc";
eqs (String.Sub.extent bc c) "bc";
eqs (String.Sub.extent c bc) "bc";
eqs (String.Sub.extent abc d) "abcd";
eqs (String.Sub.extent d abc) "abcd";
eqs (String.Sub.extent d bcd) "bcd";
eqs (String.Sub.extent bcd d) "bcd";
()
let overlap = test "String.Sub.overlap" @@ fun () ->
let empty_pos sub pos = match sub with
| None -> fail "no sub" | Some sub -> empty_pos sub pos
in
app_invalid ~pp:String.Sub.dump_raw
String.Sub.(extent (v "a")) (String.Sub.(v "b"));
let abcd = "abcd" in
let e0 = String.Sub.v ~start:0 ~stop:0 abcd in
let a = String.Sub.v ~start:0 ~stop:1 abcd in
let ab = String.Sub.v ~start:0 ~stop:2 abcd in
let abc = String.Sub.v ~start:0 ~stop:3 abcd in
let _abcd = String.Sub.v ~start:0 ~stop:4 abcd in
let e1 = String.Sub.v ~start:1 ~stop:1 abcd in
let b = String.Sub.v ~start:1 ~stop:2 abcd in
let bc = String.Sub.v ~start:1 ~stop:3 abcd in
let bcd = String.Sub.v ~start:1 ~stop:4 abcd in
let e2 = String.Sub.v ~start:2 ~stop:2 abcd in
let c = String.Sub.v ~start:2 ~stop:3 abcd in
let cd = String.Sub.v ~start:2 ~stop:4 abcd in
let e3 = String.Sub.v ~start:3 ~stop:3 abcd in
let d = String.Sub.v ~start:3 ~stop:4 abcd in
let e4 = String.Sub.v ~start:4 ~stop:4 abcd in
empty_pos (String.Sub.overlap e0 a) 0;
empty_pos (String.Sub.overlap a e0) 0;
eqs_opt (String.Sub.overlap e0 b) None;
eqs_opt (String.Sub.overlap b e0) None;
empty_pos (String.Sub.overlap a b) 1;
empty_pos (String.Sub.overlap b a) 1;
eqs_opt (String.Sub.overlap a a) (Some "a");
eqs_opt (String.Sub.overlap a ab) (Some "a");
eqs_opt (String.Sub.overlap ab ab) (Some "ab");
eqs_opt (String.Sub.overlap ab abc) (Some "ab");
eqs_opt (String.Sub.overlap abc ab) (Some "ab");
eqs_opt (String.Sub.overlap b abc) (Some "b");
eqs_opt (String.Sub.overlap abc b) (Some "b");
empty_pos (String.Sub.overlap abc e3) 3;
empty_pos (String.Sub.overlap e3 abc) 3;
eqs_opt (String.Sub.overlap ab bc) (Some "b");
eqs_opt (String.Sub.overlap bc ab) (Some "b");
eqs_opt (String.Sub.overlap bcd bc) (Some "bc");
eqs_opt (String.Sub.overlap bc bcd) (Some "bc");
eqs_opt (String.Sub.overlap bcd d) (Some "d");
eqs_opt (String.Sub.overlap d bcd) (Some "d");
eqs_opt (String.Sub.overlap bcd cd) (Some "cd");
eqs_opt (String.Sub.overlap cd bcd) (Some "cd");
eqs_opt (String.Sub.overlap bcd c) (Some "c");
eqs_opt (String.Sub.overlap c bcd) (Some "c");
empty_pos (String.Sub.overlap e2 bcd) 2;
empty_pos (String.Sub.overlap bcd e2) 2;
empty_pos (String.Sub.overlap bcd e3) 3;
empty_pos (String.Sub.overlap e3 bcd) 3;
empty_pos (String.Sub.overlap bcd e4) 4;
empty_pos (String.Sub.overlap e4 bcd) 4;
empty_pos (String.Sub.overlap e1 e1) 1;
eqs_opt (String.Sub.overlap e0 bcd) None;
()
(* Appending substrings. *)
let append = test "String.Sub.append" @@ fun () ->
let no_allocl s s' =
eq_bool (String.Sub.(base_string @@ append s s') ==
String.Sub.(base_string s)) true
in
let no_allocr s s' =
eq_bool (String.Sub.(base_string @@ append s s') ==
String.Sub.(base_string s')) true
in
no_allocl String.Sub.empty String.Sub.empty;
no_allocr String.Sub.empty String.Sub.empty;
no_allocl (String.Sub.v "bla") (String.Sub.v ~start:0 ~stop:0 "abcd");
no_allocr (String.Sub.v ~start:1 ~stop:1 "b") (String.Sub.v "bli");
let a = String.sub_with_index_range ~first:0 ~last:0 "abcd" in
let ab = String.sub_with_index_range ~first:0 ~last:1 "abcd" in
let cd = String.sub_with_index_range ~first:2 ~last:3 "abcd" in
let empty = String.Sub.v ~start:4 ~stop:4 "abcd" in
eqb (String.Sub.append a empty) "a";
eqb (String.Sub.append empty a) "a";
eqb (String.Sub.append ab empty) "ab";
eqb (String.Sub.append empty ab) "ab";
eqb (String.Sub.append ab cd) "abcd";
eqb (String.Sub.append cd ab) "cdab";
()
let concat = test "String.Sub.concat" @@ fun () ->
let dash = String.sub_with_range ~first:2 ~len:1 "ab-d" in
let ddash = String.sub_with_range ~first:1 ~len:2 "a--d" in
let empty = String.sub_with_range ~first:2 ~len:0 "ab-d" in
let no_alloc ?sep s =
let r = String.Sub.(base_string (concat ?sep [s])) in
eq_bool (r == String.Sub.base_string s || r == String.empty) true
in
no_alloc empty;
no_alloc (String.Sub.v "hey");
no_alloc ~sep:empty empty;
no_alloc ~sep:dash empty;
no_alloc ~sep:empty (String.Sub.v "abc");
no_alloc ~sep:dash (String.Sub.v "abc");
let sempty = String.Sub.v ~start:2 ~stop:2 "abc" in
let a = String.Sub.v ~start:2 ~stop:3 "kka" in
let b = String.Sub.v ~start:1 ~stop:2 "ubd" in
let c = String.Sub.v ~start:0 ~stop:1 "cdd" in
let ab = String.Sub.v ~start:1 ~stop:3 "gabi" in
let abc = String.Sub.v ~start:5 ~stop:8 "zuuuuabcbb" in
eqb (String.Sub.concat ~sep:empty []) "";
eqb (String.Sub.concat ~sep:empty [sempty]) "";
eqb (String.Sub.concat ~sep:empty [sempty;sempty]) "";
eqb (String.Sub.concat ~sep:empty [a;b;]) "ab";
eqb (String.Sub.concat ~sep:empty [a;b;sempty;c]) "abc";
eqb (String.Sub.concat ~sep:dash []) "";
eqb (String.Sub.concat ~sep:dash [sempty]) "";
eqb (String.Sub.concat ~sep:dash [a]) "a";
eqb (String.Sub.concat ~sep:dash [a;sempty]) "a-";
eqb (String.Sub.concat ~sep:dash [sempty;a]) "-a";
eqb (String.Sub.concat ~sep:dash [sempty;a;sempty]) "-a-";
eqb (String.Sub.concat ~sep:dash [a;b;c]) "a-b-c";
eqb (String.Sub.concat ~sep:ddash [a;b;c]) "a--b--c";
eqb (String.Sub.concat ~sep:ab [a;b;c]) "aabbabc";
eqb (String.Sub.concat ~sep:ab [abc;b;c]) "abcabbabc";
()
(* Predicates *)
let is_empty = test "String.Sub.is_empty" @@ fun () ->
eq_bool (String.Sub.is_empty (String.Sub.v "")) true;
eq_bool (String.Sub.is_empty (String.Sub.v ~start:4 ~stop:4 "abcd")) true;
eq_bool (String.Sub.is_empty (String.Sub.v ~start:0 ~stop:0 "huiy")) true;
eq_bool (String.Sub.is_empty (String.Sub.v ~start:0 ~stop:1 "huiy")) false;
eq_bool (String.Sub.is_empty (String.Sub.v ~start:0 ~stop:2 "huiy")) false;
eq_bool (String.Sub.is_empty (String.Sub.v ~start:0 ~stop:3 "huiy")) false;
eq_bool (String.Sub.is_empty (String.Sub.v ~start:0 ~stop:4 "huiy")) false;
eq_bool (String.Sub.is_empty (String.Sub.v ~start:1 ~stop:1 "abcd")) true;
eq_bool (String.Sub.is_empty (String.Sub.v ~start:1 ~stop:2 "huiy")) false;
eq_bool (String.Sub.is_empty (String.Sub.v ~start:1 ~stop:3 "huiy")) false;
eq_bool (String.Sub.is_empty (String.Sub.v ~start:1 ~stop:4 "huiy")) false;
eq_bool (String.Sub.is_empty (String.Sub.v ~start:2 ~stop:2 "abcd")) true;
eq_bool (String.Sub.is_empty (String.Sub.v ~start:2 ~stop:3 "huiy")) false;
eq_bool (String.Sub.is_empty (String.Sub.v ~start:3 ~stop:3 "abcd")) true;
eq_bool (String.Sub.is_empty (String.Sub.v ~start:3 ~stop:4 "huiy")) false;
eq_bool (String.Sub.is_empty (String.Sub.v ~start:4 ~stop:4 "huiy")) true;
()
let is_prefix = test "String.Sub.is_prefix" @@ fun () ->
let empty = String.sub_with_range ~first:3 ~len:0 "ugoadfj" in
let sempty = String.sub_with_range ~first:4 ~len:0 "dfkdjf" in
let habla = String.sub_with_range ~first:2 ~len:5 "abhablablu" in
let h = String.sub_with_range ~first:0 ~len:1 "hadfdffdf" in
let ha = String.sub_with_range ~first:0 ~len:2 "hadfdffdf" in
let hab = String.sub_with_range ~first:3 ~len:3 "hadhabdffdf" in
let abla = String.sub_with_range ~first:1 ~len:4 "iabla" in
eqs empty ""; eqs sempty ""; eqs habla "habla"; eqs h "h"; eqs ha "ha";
eqs hab "hab"; eqs abla "abla";
eq_bool (String.Sub.is_prefix ~affix:empty sempty) true;
eq_bool (String.Sub.is_prefix ~affix:empty habla) true;
eq_bool (String.Sub.is_prefix ~affix:ha sempty) false;
eq_bool (String.Sub.is_prefix ~affix:ha h) false;
eq_bool (String.Sub.is_prefix ~affix:ha ha) true;
eq_bool (String.Sub.is_prefix ~affix:ha hab) true;
eq_bool (String.Sub.is_prefix ~affix:ha habla) true;
eq_bool (String.Sub.is_prefix ~affix:ha abla) false;
()
let is_infix = test "String.Sub.is_infix" @@ fun () ->
let empty = String.sub_with_range ~first:1 ~len:0 "ugoadfj" in
let sempty = String.sub_with_range ~first:2 ~len:0 "dfkdjf" in
let asdf = String.sub_with_range ~first:1 ~len:4 "aasdflablu" in
let a = String.sub_with_range ~first:2 ~len:1 "cda" in
let h = String.sub_with_range ~first:0 ~len:1 "h" in
let ha = String.sub_with_range ~first:1 ~len:2 "uhadfdffdf" in
let ah = String.sub_with_range ~first:0 ~len:2 "ah" in
let aha = String.sub_with_range ~first:2 ~len:3 "aaaha" in
let haha = String.sub_with_range ~first:1 ~len:4 "ahaha" in
let hahb = String.sub_with_range ~first:0 ~len:4 "hahbdfdf" in
let blhahb = String.sub_with_range ~first:0 ~len:6 "blhahbdfdf" in
let blha = String.sub_with_range ~first:1 ~len:4 "fblhahbdfdf" in
let blh = String.sub_with_range ~first:1 ~len:3 "fblhahbdfdf" in
eqs asdf "asdf"; eqs ha "ha"; eqs h "h"; eqs a "a"; eqs aha "aha";
eqs haha "haha"; eqs hahb "hahb"; eqs blhahb "blhahb"; eqs blha "blha";
eqs blh "blh";
eq_bool (String.Sub.is_infix ~affix:empty sempty) true;
eq_bool (String.Sub.is_infix ~affix:empty asdf) true;
eq_bool (String.Sub.is_infix ~affix:empty ha) true;
eq_bool (String.Sub.is_infix ~affix:ha sempty) false;
eq_bool (String.Sub.is_infix ~affix:ha a) false;
eq_bool (String.Sub.is_infix ~affix:ha h) false;
eq_bool (String.Sub.is_infix ~affix:ha ah) false;
eq_bool (String.Sub.is_infix ~affix:ha ha) true;
eq_bool (String.Sub.is_infix ~affix:ha aha) true;
eq_bool (String.Sub.is_infix ~affix:ha haha) true;
eq_bool (String.Sub.is_infix ~affix:ha hahb) true;
eq_bool (String.Sub.is_infix ~affix:ha blhahb) true;
eq_bool (String.Sub.is_infix ~affix:ha blha) true;
eq_bool (String.Sub.is_infix ~affix:ha blh) false;
()
let is_suffix = test "String.Sub.is_suffix" @@ fun () ->
let empty = String.sub_with_range ~first:1 ~len:0 "ugoadfj" in
let sempty = String.sub_with_range ~first:2 ~len:0 "dfkdjf" in
let asdf = String.sub_with_range ~first:1 ~len:4 "aasdflablu" in
let ha = String.sub_with_range ~first:1 ~len:2 "uhadfdffdf" in
let h = String.sub_with_range ~first:0 ~len:1 "h" in
let a = String.sub_with_range ~first:2 ~len:1 "cda" in
let ah = String.sub_with_range ~first:0 ~len:2 "ah" in
let aha = String.sub_with_range ~first:2 ~len:3 "aaaha" in
let haha = String.sub_with_range ~first:1 ~len:4 "ahaha" in
let hahb = String.sub_with_range ~first:0 ~len:4 "hahbdfdf" in
eqs asdf "asdf"; eqs ha "ha"; eqs h "h"; eqs a "a"; eqs aha "aha";
eqs haha "haha"; eqs hahb "hahb";
eq_bool (String.Sub.is_suffix ~affix:empty sempty) true;
eq_bool (String.Sub.is_suffix ~affix:empty asdf) true;
eq_bool (String.Sub.is_suffix ~affix:ha sempty) false;
eq_bool (String.Sub.is_suffix ~affix:ha a) false;
eq_bool (String.Sub.is_suffix ~affix:ha h) false;
eq_bool (String.Sub.is_suffix ~affix:ha ah) false;
eq_bool (String.Sub.is_suffix ~affix:ha ha) true;
eq_bool (String.Sub.is_suffix ~affix:ha aha) true;
eq_bool (String.Sub.is_suffix ~affix:ha haha) true;
eq_bool (String.Sub.is_suffix ~affix:ha hahb) false;
()
let for_all = test "String.Sub.for_all" @@ fun () ->
let empty = String.Sub.v ~start:3 ~stop:3 "asldfksaf" in
let s123 = String.Sub.v ~start:2 ~stop:5 "sf123df" in
let s412 = String.Sub.v "412" in
let s142 = String.Sub.v ~start:3 "aaa142" in
let s124 = String.Sub.v ~start:3 "aad124" in
eqs empty ""; eqs s123 "123"; eqs s412 "412"; eqs s142 "142"; eqs s124 "124";
eq_bool (String.Sub.for_all (fun _ -> false) empty) true;
eq_bool (String.Sub.for_all (fun _ -> true) empty) true;
eq_bool (String.Sub.for_all (fun c -> Char.to_int c < 0x34) s123) true;
eq_bool (String.Sub.for_all (fun c -> Char.to_int c < 0x34) s412) false;
eq_bool (String.Sub.for_all (fun c -> Char.to_int c < 0x34) s142) false;
eq_bool (String.Sub.for_all (fun c -> Char.to_int c < 0x34) s124) false;
()
let exists = test "String.Sub.exists" @@ fun () ->
let empty = String.Sub.v ~start:3 ~stop:3 "asldfksaf" in
let s541 = String.sub_with_index_range ~first:1 ~last:3 "a541" in
let s154 = String.sub_with_index_range ~first:1 "a154" in
let s654 = String.sub_with_index_range ~last:2 "654adf" in
eqs s541 "541"; eqs s154 "154"; eqs s654 "654";
eq_bool (String.Sub.exists (fun _ -> false) empty) false;
eq_bool (String.Sub.exists (fun _ -> true) empty) false;
eq_bool (String.Sub.exists (fun c -> Char.to_int c < 0x34) s541) true;
eq_bool (String.Sub.exists (fun c -> Char.to_int c < 0x34) s541) true;
eq_bool (String.Sub.exists (fun c -> Char.to_int c < 0x34) s154) true;
eq_bool (String.Sub.exists (fun c -> Char.to_int c < 0x34) s654) false;
()
let same_base = test "String.Sub.same_base" @@ fun () ->
let abcd = "abcd" in
let a = String.sub_with_index_range ~first:0 ~last:0 abcd in
let ab = String.sub_with_index_range ~first:0 ~last:1 abcd in
let abce = String.sub_with_index_range ~first:0 ~last:1 "abce" in
eq_bool (String.Sub.same_base a ab) true;
eq_bool (String.Sub.same_base ab a) true;
eq_bool (String.Sub.same_base abce a) false;
eq_bool (String.Sub.same_base abce a) false;
()
let equal_bytes = test "String.Sub.equal_bytes" @@ fun () ->
let a = String.sub_with_index_range ~first:0 ~last:0 "abcd" in
let ab = String.sub_with_index_range ~first:0 ~last:1 "abcd" in
let ab' = String.sub_with_index_range ~first:2 ~last:3 "cdab" in
let cd = String.sub_with_index_range ~first:2 ~last:3 "abcd" in
let empty = String.Sub.v ~start:4 ~stop:4 "abcd" in
eq_bool (String.Sub.equal_bytes empty empty) true;
eq_bool (String.Sub.equal_bytes empty a) false;
eq_bool (String.Sub.equal_bytes a empty) false;
eq_bool (String.Sub.equal_bytes a a) true;
eq_bool (String.Sub.equal_bytes ab ab') true;
eq_bool (String.Sub.equal_bytes cd ab) false;
()
let compare_bytes = test "String.Sub.compare_bytes" @@ fun () ->
let empty = String.Sub.v "" in
let ab = String.sub_with_index_range ~first:0 ~last:1 "abcd" in
let ab' = String.sub_with_index_range ~first:2 ~last:3 "cdab" in
let abc = String.Sub.v ~start:3 ~stop:5 "adabcdd" in
eq_int (String.Sub.compare_bytes empty ab) (-1);
eq_int (String.Sub.compare_bytes empty empty) (0);
eq_int (String.Sub.compare_bytes ab ab') (0);
eq_int (String.Sub.compare_bytes ab empty) (1);
eq_int (String.Sub.compare_bytes ab abc) (-1);
()
let equal = test "String.Sub.equal" @@ fun () ->
app_invalid ~pp:pp_bool (String.Sub.(equal (v "b"))) (String.Sub.v "a");
let base = "abcd" in
let a = String.Sub.v ~start:0 ~stop:1 base in
let empty = String.Sub.v ~start:4 ~stop:4 base in
let ab = String.sub_with_index_range ~first:0 ~last:1 base in
let cd = String.sub_with_index_range ~first:2 ~last:3 base in
eq_bool (String.Sub.equal empty empty) true;
eq_bool (String.Sub.equal empty a) false;
eq_bool (String.Sub.equal a empty) false;
eq_bool (String.Sub.equal a a) true;
eq_bool (String.Sub.equal ab ab) true;
eq_bool (String.Sub.equal cd ab) false;
eq_bool (String.Sub.equal ab cd) false;
eq_bool (String.Sub.equal cd cd) true;
()
let compare = test "String.Sub.compare" @@ fun () ->
app_invalid ~pp:pp_bool (String.Sub.(equal (v "b"))) (String.Sub.v "a");
let base = "abcd" in
let a = String.Sub.v ~start:0 ~stop:1 base in
let empty = String.Sub.v ~start:4 ~stop:4 base in
let ab = String.sub_with_index_range ~first:0 ~last:1 base in
let cd = String.sub_with_index_range ~first:2 ~last:3 base in
eq_int (String.Sub.compare empty empty) 0;
eq_int (String.Sub.compare empty a) 1;
eq_int (String.Sub.compare a empty) (-1);
eq_int (String.Sub.compare a a) 0;
eq_int (String.Sub.compare ab ab) 0;
eq_int (String.Sub.compare cd ab) 1;
eq_int (String.Sub.compare ab cd) (-1);
eq_int (String.Sub.compare cd cd) 0;
()
(* Extracting substrings *)
let with_range = test "String.Sub.with_range" @@ fun () ->
let invalid ?first ?len s =
app_invalid ~pp:String.Sub.pp (String.Sub.with_range ?first ?len) s
in
let empty_pos ?first ?len s pos =
empty_pos (String.Sub.with_range ?first ?len s) pos
in
let base = "00abc1234" in
let abc = String.sub ~start:2 ~stop:5 base in
let a = String.sub ~start:2 ~stop:3 base in
let empty = String.sub ~start:2 ~stop:2 base in
empty_pos empty ~first:1 ~len:0 2;
empty_pos empty ~first:1 ~len:0 2;
empty_pos empty ~first:0 ~len:1 2;
empty_pos empty ~first:(-1) ~len:1 2;
invalid empty ~first:0 ~len:(-1);
eqs (String.Sub.with_range a ~first:0 ~len:0) "";
eqs (String.Sub.with_range a ~first:1 ~len:0) "";
empty_pos a ~first:1 ~len:1 3;
empty_pos a ~first:(-1) ~len:1 2;
eqs (String.Sub.with_range ~first:1 abc) "bc";
eqs (String.Sub.with_range ~first:2 abc) "c";
eqs (String.Sub.with_range ~first:3 abc) "";
empty_pos ~first:4 abc 5;
eqs (String.Sub.with_range abc ~first:0 ~len:0) "";
eqs (String.Sub.with_range abc ~first:0 ~len:1) "a";
eqs (String.Sub.with_range abc ~first:0 ~len:2) "ab";
eqs (String.Sub.with_range abc ~first:0 ~len:4) "abc";
eqs (String.Sub.with_range abc ~first:1 ~len:0) "";
eqs (String.Sub.with_range abc ~first:1 ~len:1) "b";
eqs (String.Sub.with_range abc ~first:1 ~len:2) "bc";
eqs (String.Sub.with_range abc ~first:1 ~len:3) "bc";
eqs (String.Sub.with_range abc ~first:2 ~len:0) "";
eqs (String.Sub.with_range abc ~first:2 ~len:1) "c";
eqs (String.Sub.with_range abc ~first:2 ~len:2) "c";
eqs (String.Sub.with_range abc ~first:3 ~len:0) "";
eqs (String.Sub.with_range abc ~first:1 ~len:4) "bc";
empty_pos abc ~first:(-1) ~len:1 2;
()
let with_index_range = test "String.Sub.with_index_range" @@ fun () ->
let empty_pos ?first ?last s pos =
empty_pos (String.Sub.with_index_range ?first ?last s) pos
in
let base = "00abc1234" in
let abc = String.sub ~start:2 ~stop:5 base in
let a = String.sub ~start:2 ~stop:3 base in
let empty = String.sub ~start:2 ~stop:2 base in
empty_pos empty 2;
empty_pos empty ~first:0 ~last:0 2;
empty_pos empty ~first:1 ~last:0 2;
empty_pos empty ~first:0 ~last:1 2;
empty_pos empty ~first:(-1) ~last:1 2;
empty_pos empty ~first:0 ~last:(-1) 2;
eqs (String.Sub.with_index_range ~first:0 ~last:2 a) "a";
empty_pos a ~first:0 ~last:(-1) 2;
eqs (String.Sub.with_index_range ~first:0 ~last:2 a) "a";
eqs (String.Sub.with_index_range ~first:(-1) ~last:0 a) "a";
eqs (String.Sub.with_index_range ~first:1 abc) "bc";
eqs (String.Sub.with_index_range ~first:2 abc) "c";
empty_pos ~first:3 abc 5;
empty_pos ~first:4 abc 5;
eqs (String.Sub.with_index_range abc ~first:0 ~last:0) "a";
eqs (String.Sub.with_index_range abc ~first:0 ~last:1) "ab";
eqs (String.Sub.with_index_range abc ~first:0 ~last:3) "abc";
eqs (String.Sub.with_index_range abc ~first:1 ~last:1) "b";
eqs (String.Sub.with_index_range abc ~first:1 ~last:2) "bc";
empty_pos abc ~first:1 ~last:0 3;
eqs (String.Sub.with_index_range abc ~first:1 ~last:3) "bc";
eqs (String.Sub.with_index_range abc ~first:2 ~last:2) "c";
empty_pos abc ~first:2 ~last:0 4;
empty_pos abc ~first:2 ~last:1 4;
eqs (String.Sub.with_index_range abc ~first:2 ~last:3) "c";
empty_pos abc ~first:3 ~last:0 5;
empty_pos abc ~first:3 ~last:1 5;
empty_pos abc ~first:3 ~last:2 5;
empty_pos abc ~first:3 ~last:3 5;
eqs (String.Sub.with_index_range abc ~first:(-1) ~last:0) "a";
()
let span = test "String.Sub.{span,take,drop}" @@ fun () ->
let eq_pair (l0, r0) (l1, r1) = String.Sub.(equal l0 l1 && equal r0 r1) in
let eq_pair = eq ~eq:eq_pair ~pp:pp_pair in
let eq ?(rev = false) ?min ?max ?sat s (sl, sr as spec) =
let (l, r as pair) = String.Sub.span ~rev ?min ?max ?sat s in
let t = String.Sub.take ~rev ?min ?max ?sat s in
let d = String.Sub.drop ~rev ?min ?max ?sat s in
eq_pair pair spec;
eq_sub t (if rev then sr else sl);
eq_sub d (if rev then sl else sr);
in
let invalid ?rev ?min ?max ?sat s =
app_invalid ~pp:pp_pair (String.Sub.span ?rev ?min ?max ?sat) s
in
let base = "0ab cd0" in
let empty = String.sub ~start:3 ~stop:3 base in
let ab_cd = String.sub ~start:1 ~stop:6 base in
let ab = String.sub ~start:1 ~stop:3 base in
let _cd = String.sub ~start:3 ~stop:6 base in
let cd = String.sub ~start:4 ~stop:6 base in
let ab_ = String.sub ~start:1 ~stop:4 base in
let a = String.sub ~start:1 ~stop:2 base in
let b_cd = String.sub ~start:2 ~stop:6 base in
let b = String.sub ~start:2 ~stop:3 base in
let d = String.sub ~start:5 ~stop:6 base in
let ab_c = String.sub ~start:1 ~stop:5 base in
eq ~rev:false ~min:1 ~max:0 ab_cd (String.Sub.start ab_cd, ab_cd);
eq ~rev:true ~min:1 ~max:0 ab_cd (ab_cd, String.Sub.stop ab_cd);
eq ~sat:Char.Ascii.is_white ab_cd (String.Sub.start ab_cd, ab_cd);
eq ~sat:Char.Ascii.is_letter ab_cd (ab, _cd);
eq ~max:1 ~sat:Char.Ascii.is_letter ab_cd (a, b_cd);
eq ~max:0 ~sat:Char.Ascii.is_letter ab_cd (String.Sub.start ab_cd, ab_cd);
eq ~rev:true ~sat:Char.Ascii.is_white ab_cd (ab_cd, String.Sub.stop ab_cd);
eq ~rev:true ~sat:Char.Ascii.is_letter ab_cd (ab_, cd);
eq ~rev:true ~max:1 ~sat:Char.Ascii.is_letter ab_cd (ab_c, d);
eq ~rev:true ~max:0 ~sat:Char.Ascii.is_letter ab_cd (ab_cd,
String.Sub.stop ab_cd);
eq ~sat:Char.Ascii.is_letter ab (ab, String.Sub.stop ab);
eq ~max:1 ~sat:Char.Ascii.is_letter ab (a, b);
eq ~sat:Char.Ascii.is_letter b (b, empty);
eq ~rev:true ~max:1 ~sat:Char.Ascii.is_letter ab (a, b);
eq ~max:1 ~sat:Char.Ascii.is_white ab (String.Sub.start ab, ab);
eq ~rev:true ~sat:Char.Ascii.is_white empty (empty, empty);
eq ~sat:Char.Ascii.is_white empty (empty, empty);
invalid ~rev:false ~min:(-1) empty;
invalid ~rev:true ~min:(-1) empty;
invalid ~rev:false ~max:(-1) empty;
invalid ~rev:true ~max:(-1) empty;
eq ~rev:false empty (empty,empty);
eq ~rev:true empty (empty,empty);
eq ~rev:false ~min:0 ~max:0 empty (empty,empty);
eq ~rev:true ~min:0 ~max:0 empty (empty,empty);
eq ~rev:false ~min:1 ~max:0 empty (empty,empty);
eq ~rev:true ~min:1 ~max:0 empty (empty,empty);
eq ~rev:false ~max:0 ab_cd (String.Sub.start ab_cd, ab_cd);
eq ~rev:true ~max:0 ab_cd (ab_cd, String.Sub.stop ab_cd);
eq ~rev:false ~max:2 ab_cd (ab, _cd);
eq ~rev:true ~max:2 ab_cd (ab_, cd);
eq ~rev:false ~min:6 ab_cd (String.Sub.start ab_cd, ab_cd);
eq ~rev:true ~min:6 ab_cd (ab_cd, String.Sub.stop ab_cd);
eq ~rev:false ab_cd (ab_cd, String.Sub.stop ab_cd);
eq ~rev:true ab_cd (String.Sub.start ab_cd, ab_cd);
eq ~rev:false ~max:30 ab_cd (ab_cd, String.Sub.stop ab_cd);
eq ~rev:true ~max:30 ab_cd (String.Sub.start ab_cd, ab_cd);
eq ~rev:false ~sat:Char.Ascii.is_white ab_cd (String.Sub.start ab_cd,ab_cd);
eq ~rev:true ~sat:Char.Ascii.is_white ab_cd (ab_cd,String.Sub.stop ab_cd);
eq ~rev:false ~sat:Char.Ascii.is_letter ab_cd (ab, _cd);
eq ~rev:true ~sat:Char.Ascii.is_letter ab_cd (ab_, cd);
eq ~rev:false ~sat:Char.Ascii.is_letter ~max:0 ab_cd (String.Sub.start ab_cd,
ab_cd);
eq ~rev:true ~sat:Char.Ascii.is_letter ~max:0 ab_cd (ab_cd,
String.Sub.stop ab_cd);
eq ~rev:false ~sat:Char.Ascii.is_letter ~max:1 ab_cd (a, b_cd);
eq ~rev:true ~sat:Char.Ascii.is_letter ~max:1 ab_cd (ab_c, d);
eq ~rev:false ~sat:Char.Ascii.is_letter ~min:2 ~max:1 ab_cd
(String.Sub.start ab_cd, ab_cd);
eq ~rev:true ~sat:Char.Ascii.is_letter ~min:2 ~max:1 ab_cd
(ab_cd, String.Sub.stop ab_cd);
eq ~rev:false ~sat:Char.Ascii.is_letter ~min:3 ab_cd
(String.Sub.start ab_cd, ab_cd);
eq ~rev:true ~sat:Char.Ascii.is_letter ~min:3 ab_cd
(ab_cd, String.Sub.stop ab_cd);
()
let trim = test "String.Sub.trim" @@ fun () ->
let drop_a c = c = 'a' in
let base = "00aaaabcdaaaa00" in
let aaaabcdaaaa = String.sub ~start:2 ~stop:13 base in
let aaaabcd = String.sub ~start:2 ~stop:9 base in
let bcdaaaa = String.sub ~start:6 ~stop:13 base in
let aaaa = String.sub ~start:2 ~stop:6 base in
eqs (String.Sub.trim (String.sub "\t abcd \r ")) "abcd";
eqs (String.Sub.trim aaaabcdaaaa) "aaaabcdaaaa";
eqs (String.Sub.trim ~drop:drop_a aaaabcdaaaa) "bcd";
eqs (String.Sub.trim ~drop:drop_a aaaabcd) "bcd";
eqs (String.Sub.trim ~drop:drop_a bcdaaaa) "bcd";
empty_pos (String.Sub.trim ~drop:drop_a aaaa) 4;
empty_pos (String.Sub.trim (String.sub " ")) 2;
()
let cut = test "String.Sub.cut" @@ fun () ->
let ppp = pp_option pp_pair in
let eqo = eqs_pair_opt in
let s s =
String.sub ~start:1 ~stop:(1 + (String.length s)) (strf "\x00%s\x00" s)
in
let cut ?rev ~sep str = String.Sub.cut ?rev ~sep:(s sep) (s str) in
app_invalid ~pp:ppp (cut ~sep:"") "";
app_invalid ~pp:ppp (cut ~sep:"") "123";
eqo (cut "," "") None;
eqo (cut "," ",") (Some ("", ""));
eqo (cut "," ",,") (Some ("", ","));
eqo (cut "," ",,,") (Some ("", ",,"));
eqo (cut "," "123") None;
eqo (cut "," ",123") (Some ("", "123"));
eqo (cut "," "123,") (Some ("123", ""));
eqo (cut "," "1,2,3") (Some ("1", "2,3"));
eqo (cut "," " 1,2,3") (Some (" 1", "2,3"));
eqo (cut "<>" "") None;
eqo (cut "<>" "<>") (Some ("", ""));
eqo (cut "<>" "<><>") (Some ("", "<>"));
eqo (cut "<>" "<><><>") (Some ("", "<><>"));
eqo (cut ~rev:true ~sep:"<>" "1") None;
eqo (cut "<>" "123") None;
eqo (cut "<>" "<>123") (Some ("", "123"));
eqo (cut "<>" "123<>") (Some ("123", ""));
eqo (cut "<>" "1<>2<>3") (Some ("1", "2<>3"));
eqo (cut "<>" " 1<>2<>3") (Some (" 1", "2<>3"));
eqo (cut "<>" ">>><>>>><>>>><>>>>") (Some (">>>", ">>><>>>><>>>>"));
eqo (cut "<->" "<->>->") (Some ("", ">->"));
eqo (cut ~rev:true ~sep:"<->" "<-") None;
eqo (cut "aa" "aa") (Some ("", ""));
eqo (cut "aa" "aaa") (Some ("", "a"));
eqo (cut "aa" "aaaa") (Some ("", "aa"));
eqo (cut "aa" "aaaaa") (Some ("", "aaa";));
eqo (cut "aa" "aaaaaa") (Some ("", "aaaa"));
eqo (cut ~sep:"ab" "faaaa") None;
eqo (String.Sub.cut ~sep:(String.sub "/") (String.sub ~start:2 "a/b/c"))
(Some ("b", "c"));
let rev = true in
app_invalid ~pp:ppp (cut ~rev ~sep:"") "";
app_invalid ~pp:ppp (cut ~rev ~sep:"") "123";
eqo (cut ~rev ~sep:"," "") None;
eqo (cut ~rev ~sep:"," ",") (Some ("", ""));
eqo (cut ~rev ~sep:"," ",,") (Some (",", ""));
eqo (cut ~rev ~sep:"," ",,,") (Some (",,", ""));
eqo (cut ~rev ~sep:"," "123") None;
eqo (cut ~rev ~sep:"," ",123") (Some ("", "123"));
eqo (cut ~rev ~sep:"," "123,") (Some ("123", ""));
eqo (cut ~rev ~sep:"," "1,2,3") (Some ("1,2", "3"));
eqo (cut ~rev ~sep:"," "1,2,3 ") (Some ("1,2", "3 "));
eqo (cut ~rev ~sep:"<>" "") None;
eqo (cut ~rev ~sep:"<>" "<>") (Some ("", ""));
eqo (cut ~rev ~sep:"<>" "<><>") (Some ("<>", ""));
eqo (cut ~rev ~sep:"<>" "<><><>") (Some ("<><>", ""));
eqo (cut ~rev ~sep:"<>" "1") None;
eqo (cut ~rev ~sep:"<>" "123") None;
eqo (cut ~rev ~sep:"<>" "<>123") (Some ("", "123"));
eqo (cut ~rev ~sep:"<>" "123<>") (Some ("123", ""));
eqo (cut ~rev ~sep:"<>" "1<>2<>3") (Some ("1<>2", "3"));
eqo (cut ~rev ~sep:"<>" "1<>2<>3 ") (Some ("1<>2", "3 "));
eqo (cut ~rev ~sep:"<>" ">>><>>>><>>>><>>>>")
(Some (">>><>>>><>>>>", ">>>"));
eqo (cut ~rev ~sep:"<->" "<->>->") (Some ("", ">->"));
eqo (cut ~rev ~sep:"<->" "<-") None;
eqo (cut ~rev ~sep:"aa" "aa") (Some ("", ""));
eqo (cut ~rev ~sep:"aa" "aaa") (Some ("a", ""));
eqo (cut ~rev ~sep:"aa" "aaaa") (Some ("aa", ""));
eqo (cut ~rev ~sep:"aa" "aaaaa") (Some ("aaa", "";));
eqo (cut ~rev ~sep:"aa" "aaaaaa") (Some ("aaaa", ""));
eqo (cut ~rev ~sep:"ab" "afaaaa") None;
eqo (String.Sub.cut ~sep:(String.sub "/") (String.sub ~stop:3 "a/b/c"))
(Some ("a", "b"));
()
let cuts = test "String.Sub.cuts" @@ fun () ->
let ppl = pp_list String.Sub.dump in
let eql subs l =
let subs = List.map String.Sub.to_string subs in
eq_list ~eq:String.equal ~pp:String.dump subs l
in
let s s =
String.sub ~start:1 ~stop:(1 + (String.length s)) (strf "\x00%s\x00" s)
in
let cuts ?rev ?empty ~sep str =
String.Sub.cuts ?rev ?empty ~sep:(s sep) (s str)
in
app_invalid ~pp:ppl (cuts ~sep:"") "";
app_invalid ~pp:ppl (cuts ~sep:"") "123";
eql (cuts ~empty:true ~sep:"," "") [""];
eql (cuts ~empty:false ~sep:"," "") [];
eql (cuts ~empty:true ~sep:"," ",") [""; ""];
eql (cuts ~empty:false ~sep:"," ",") [];
eql (cuts ~empty:true ~sep:"," ",,") [""; ""; ""];
eql (cuts ~empty:false ~sep:"," ",,") [];
eql (cuts ~empty:true ~sep:"," ",,,") [""; ""; ""; ""];
eql (cuts ~empty:false ~sep:"," ",,,") [];
eql (cuts ~empty:true ~sep:"," "123") ["123"];
eql (cuts ~empty:false ~sep:"," "123") ["123"];
eql (cuts ~empty:true ~sep:"," ",123") [""; "123"];
eql (cuts ~empty:false ~sep:"," ",123") ["123"];
eql (cuts ~empty:true ~sep:"," "123,") ["123"; ""];
eql (cuts ~empty:false ~sep:"," "123,") ["123";];
eql (cuts ~empty:true ~sep:"," "1,2,3") ["1"; "2"; "3"];
eql (cuts ~empty:false ~sep:"," "1,2,3") ["1"; "2"; "3"];
eql (cuts ~empty:true ~sep:"," "1, 2, 3") ["1"; " 2"; " 3"];
eql (cuts ~empty:false ~sep:"," "1, 2, 3") ["1"; " 2"; " 3"];
eql (cuts ~empty:true ~sep:"," ",1,2,,3,") [""; "1"; "2"; ""; "3"; ""];
eql (cuts ~empty:false ~sep:"," ",1,2,,3,") ["1"; "2"; "3";];
eql (cuts ~empty:true ~sep:"," ", 1, 2,, 3,")
[""; " 1"; " 2"; ""; " 3"; ""];
eql (cuts ~empty:false ~sep:"," ", 1, 2,, 3,") [" 1"; " 2";" 3";];
eql (cuts ~empty:true ~sep:"<>" "") [""];
eql (cuts ~empty:false ~sep:"<>" "") [];
eql (cuts ~empty:true ~sep:"<>" "<>") [""; ""];
eql (cuts ~empty:false ~sep:"<>" "<>") [];
eql (cuts ~empty:true ~sep:"<>" "<><>") [""; ""; ""];
eql (cuts ~empty:false ~sep:"<>" "<><>") [];
eql (cuts ~empty:true ~sep:"<>" "<><><>") [""; ""; ""; ""];
eql (cuts ~empty:false ~sep:"<>" "<><><>") [];
eql (cuts ~empty:true ~sep:"<>" "123") [ "123" ];
eql (cuts ~empty:false ~sep:"<>" "123") [ "123" ];
eql (cuts ~empty:true ~sep:"<>" "<>123") [""; "123"];
eql (cuts ~empty:false ~sep:"<>" "<>123") ["123"];
eql (cuts ~empty:true ~sep:"<>" "123<>") ["123"; ""];
eql (cuts ~empty:false ~sep:"<>" "123<>") ["123"];
eql (cuts ~empty:true ~sep:"<>" "1<>2<>3") ["1"; "2"; "3"];
eql (cuts ~empty:false ~sep:"<>" "1<>2<>3") ["1"; "2"; "3"];
eql (cuts ~empty:true ~sep:"<>" "1<> 2<> 3") ["1"; " 2"; " 3"];
eql (cuts ~empty:false ~sep:"<>" "1<> 2<> 3") ["1"; " 2"; " 3"];
eql (cuts ~empty:true ~sep:"<>" "<>1<>2<><>3<>")
[""; "1"; "2"; ""; "3"; ""];
eql (cuts ~empty:false ~sep:"<>" "<>1<>2<><>3<>") ["1"; "2";"3";];
eql (cuts ~empty:true ~sep:"<>" "<> 1<> 2<><> 3<>")
[""; " 1"; " 2"; ""; " 3";""];
eql (cuts ~empty:false ~sep:"<>" "<> 1<> 2<><> 3<>")[" 1"; " 2"; " 3"];
eql (cuts ~empty:true ~sep:"<>" ">>><>>>><>>>><>>>>")
[">>>"; ">>>"; ">>>"; ">>>" ];
eql (cuts ~empty:false ~sep:"<>" ">>><>>>><>>>><>>>>")
[">>>"; ">>>"; ">>>"; ">>>" ];
eql (cuts ~empty:true ~sep:"<->" "<->>->") [""; ">->"];
eql (cuts ~empty:false ~sep:"<->" "<->>->") [">->"];
eql (cuts ~empty:true ~sep:"aa" "aa") [""; ""];
eql (cuts ~empty:false ~sep:"aa" "aa") [];
eql (cuts ~empty:true ~sep:"aa" "aaa") [""; "a"];
eql (cuts ~empty:false ~sep:"aa" "aaa") ["a"];
eql (cuts ~empty:true ~sep:"aa" "aaaa") [""; ""; ""];
eql (cuts ~empty:false ~sep:"aa" "aaaa") [];
eql (cuts ~empty:true ~sep:"aa" "aaaaa") [""; ""; "a"];
eql (cuts ~empty:false ~sep:"aa" "aaaaa") ["a"];
eql (cuts ~empty:true ~sep:"aa" "aaaaaa") [""; ""; ""; ""];
eql (cuts ~empty:false ~sep:"aa" "aaaaaa") [];
let rev = true in
app_invalid ~pp:ppl (cuts ~rev ~sep:"") "";
app_invalid ~pp:ppl (cuts ~rev ~sep:"") "123";
eql (cuts ~rev ~empty:true ~sep:"," "") [""];
eql (cuts ~rev ~empty:false ~sep:"," "") [];
eql (cuts ~rev ~empty:true ~sep:"," ",") [""; ""];
eql (cuts ~rev ~empty:false ~sep:"," ",") [];
eql (cuts ~rev ~empty:true ~sep:"," ",,") [""; ""; ""];
eql (cuts ~rev ~empty:false ~sep:"," ",,") [];
eql (cuts ~rev ~empty:true ~sep:"," ",,,") [""; ""; ""; ""];
eql (cuts ~rev ~empty:false ~sep:"," ",,,") [];
eql (cuts ~rev ~empty:true ~sep:"," "123") ["123"];
eql (cuts ~rev ~empty:false ~sep:"," "123") ["123"];
eql (cuts ~rev ~empty:true ~sep:"," ",123") [""; "123"];
eql (cuts ~rev ~empty:false ~sep:"," ",123") ["123"];
eql (cuts ~rev ~empty:true ~sep:"," "123,") ["123"; ""];
eql (cuts ~rev ~empty:false ~sep:"," "123,") ["123";];
eql (cuts ~rev ~empty:true ~sep:"," "1,2,3") ["1"; "2"; "3"];
eql (cuts ~rev ~empty:false ~sep:"," "1,2,3") ["1"; "2"; "3"];
eql (cuts ~rev ~empty:true ~sep:"," "1, 2, 3") ["1"; " 2"; " 3"];
eql (cuts ~rev ~empty:false ~sep:"," "1, 2, 3") ["1"; " 2"; " 3"];
eql (cuts ~rev ~empty:true ~sep:"," ",1,2,,3,")
[""; "1"; "2"; ""; "3"; ""];
eql (cuts ~rev ~empty:false ~sep:"," ",1,2,,3,") ["1"; "2"; "3"];
eql (cuts ~rev ~empty:true ~sep:"," ", 1, 2,, 3,")
[""; " 1"; " 2"; ""; " 3"; ""];
eql (cuts ~rev ~empty:false ~sep:"," ", 1, 2,, 3,") [" 1"; " 2"; " 3"];
eql (cuts ~rev ~empty:true ~sep:"<>" "") [""];
eql (cuts ~rev ~empty:false ~sep:"<>" "") [];
eql (cuts ~rev ~empty:true ~sep:"<>" "<>") [""; ""];
eql (cuts ~rev ~empty:false ~sep:"<>" "<>") [];
eql (cuts ~rev ~empty:true ~sep:"<>" "<><>") [""; ""; ""];
eql (cuts ~rev ~empty:false ~sep:"<>" "<><>") [];
eql (cuts ~rev ~empty:true ~sep:"<>" "<><><>") [""; ""; ""; ""];
eql (cuts ~rev ~empty:false ~sep:"<>" "<><><>") [];
eql (cuts ~rev ~empty:true ~sep:"<>" "123") [ "123" ];
eql (cuts ~rev ~empty:false ~sep:"<>" "123") [ "123" ];
eql (cuts ~rev ~empty:true ~sep:"<>" "<>123") [""; "123"];
eql (cuts ~rev ~empty:false ~sep:"<>" "<>123") ["123"];
eql (cuts ~rev ~empty:true ~sep:"<>" "123<>") ["123"; ""];
eql (cuts ~rev ~empty:false ~sep:"<>" "123<>") ["123";];
eql (cuts ~rev ~empty:true ~sep:"<>" "1<>2<>3") ["1"; "2"; "3"];
eql (cuts ~rev ~empty:false ~sep:"<>" "1<>2<>3") ["1"; "2"; "3"];
eql (cuts ~rev ~empty:true ~sep:"<>" "1<> 2<> 3") ["1"; " 2"; " 3"];
eql (cuts ~rev ~empty:false ~sep:"<>" "1<> 2<> 3") ["1"; " 2"; " 3"];
eql (cuts ~rev ~empty:true ~sep:"<>" "<>1<>2<><>3<>")
[""; "1"; "2"; ""; "3"; ""];
eql (cuts ~rev ~empty:false ~sep:"<>" "<>1<>2<><>3<>")
["1"; "2"; "3"];
eql (cuts ~rev ~empty:true ~sep:"<>" "<> 1<> 2<><> 3<>")
[""; " 1"; " 2"; ""; " 3";""];
eql (cuts ~rev ~empty:false ~sep:"<>" "<> 1<> 2<><> 3<>")
[" 1"; " 2"; " 3";];
eql (cuts ~rev ~empty:true ~sep:"<>" ">>><>>>><>>>><>>>>")
[">>>"; ">>>"; ">>>"; ">>>" ];
eql (cuts ~rev ~empty:false ~sep:"<>" ">>><>>>><>>>><>>>>")
[">>>"; ">>>"; ">>>"; ">>>" ];
eql (cuts ~rev ~empty:true ~sep:"<->" "<->>->") [""; ">->"];
eql (cuts ~rev ~empty:false ~sep:"<->" "<->>->") [">->"];
eql (cuts ~rev ~empty:true ~sep:"aa" "aa") [""; ""];
eql (cuts ~rev ~empty:false ~sep:"aa" "aa") [];
eql (cuts ~rev ~empty:true ~sep:"aa" "aaa") ["a"; ""];
eql (cuts ~rev ~empty:false ~sep:"aa" "aaa") ["a"];
eql (cuts ~rev ~empty:true ~sep:"aa" "aaaa") [""; ""; ""];
eql (cuts ~rev ~empty:false ~sep:"aa" "aaaa") [];
eql (cuts ~rev ~empty:true ~sep:"aa" "aaaaa") ["a"; ""; "";];
eql (cuts ~rev ~empty:false ~sep:"aa" "aaaaa") ["a";];
eql (cuts ~rev ~empty:true ~sep:"aa" "aaaaaa") [""; ""; ""; ""];
eql (cuts ~rev ~empty:false ~sep:"aa" "aaaaaa") [];
()
let fields = test "String.Sub.fields" @@ fun () ->
let eql subs l =
let subs = List.map String.Sub.to_string subs in
eq_list ~eq:String.equal ~pp:String.dump subs l
in
let s s =
String.sub ~start:1 ~stop:(1 + (String.length s)) (strf "\x00%s\x00" s)
in
let fields ?empty ?is_sep str = String.Sub.fields ?empty ?is_sep (s str) in
let is_a c = c = 'a' in
eql (fields ~empty:true "a") ["a"];
eql (fields ~empty:false "a") ["a"];
eql (fields ~empty:true "abc") ["abc"];
eql (fields ~empty:false "abc") ["abc"];
eql (fields ~empty:true ~is_sep:is_a "bcdf") ["bcdf"];
eql (fields ~empty:false ~is_sep:is_a "bcdf") ["bcdf"];
eql (fields ~empty:true "") [""];
eql (fields ~empty:false "") [];
eql (fields ~empty:true "\n\r") ["";"";""];
eql (fields ~empty:false "\n\r") [];
eql (fields ~empty:true " \n\rabc") ["";"";"";"abc"];
eql (fields ~empty:false " \n\rabc") ["abc"];
eql (fields ~empty:true " \n\racd de") ["";"";"";"acd";"de"];
eql (fields ~empty:false " \n\racd de") ["acd";"de"];
eql (fields ~empty:true " \n\racd de ") ["";"";"";"acd";"de";""];
eql (fields ~empty:false " \n\racd de ") ["acd";"de"];
eql (fields ~empty:true "\n\racd\nde \r") ["";"";"acd";"de";"";""];
eql (fields ~empty:false "\n\racd\nde \r") ["acd";"de"];
eql (fields ~empty:true ~is_sep:is_a "") [""];
eql (fields ~empty:false ~is_sep:is_a "") [];
eql (fields ~empty:true ~is_sep:is_a "abaac aaa")
["";"b";"";"c ";"";"";""];
eql (fields ~empty:false ~is_sep:is_a "abaac aaa") ["b"; "c "];
eql (fields ~empty:true ~is_sep:is_a "aaaa") ["";"";"";"";""];
eql (fields ~empty:false ~is_sep:is_a "aaaa") [];
eql (fields ~empty:true ~is_sep:is_a "aaaa ") ["";"";"";"";" "];
eql (fields ~empty:false ~is_sep:is_a "aaaa ") [" "];
eql (fields ~empty:true ~is_sep:is_a "aaaab") ["";"";"";"";"b"];
eql (fields ~empty:false ~is_sep:is_a "aaaab") ["b"];
eql (fields ~empty:true ~is_sep:is_a "baaaa") ["b";"";"";"";""];
eql (fields ~empty:false ~is_sep:is_a "baaaa") ["b"];
eql (fields ~empty:true ~is_sep:is_a "abaaaa") ["";"b";"";"";"";""];
eql (fields ~empty:false ~is_sep:is_a "abaaaa") ["b"];
eql (fields ~empty:true ~is_sep:is_a "aba") ["";"b";""];
eql (fields ~empty:false ~is_sep:is_a "aba") ["b"];
eql (fields ~empty:false "tokenize me please")
["tokenize"; "me"; "please"];
()
(* Traversing *)
let find = test "String.Sub.find" @@ fun () ->
let abcbd = "abcbd" in
let empty = String.sub ~start:3 ~stop:3 abcbd in
let a = String.sub ~start:0 ~stop:1 abcbd in
let ab = String.sub ~start:0 ~stop:2 abcbd in
let c = String.sub ~start:2 ~stop:3 abcbd in
let b0 = String.sub ~start:1 ~stop:2 abcbd in
let b1 = String.sub ~start:3 ~stop:4 abcbd in
let abcbd = String.sub abcbd in
let eq = eq_option ~eq:String.Sub.equal ~pp:String.Sub.dump_raw in
eq (String.Sub.find (fun c -> c = 'b') empty) None;
eq (String.Sub.find ~rev:true (fun c -> c = 'b') empty) None;
eq (String.Sub.find (fun c -> c = 'b') a) None;
eq (String.Sub.find ~rev:true (fun c -> c = 'b') a) None;
eq (String.Sub.find (fun c -> c = 'b') c) None;
eq (String.Sub.find ~rev:true (fun c -> c = 'b') c) None;
eq (String.Sub.find (fun c -> c = 'b') abcbd) (Some b0);
eq (String.Sub.find ~rev:true (fun c -> c = 'b') abcbd) (Some b1);
eq (String.Sub.find (fun c -> c = 'b') ab) (Some b0);
eq (String.Sub.find ~rev:true (fun c -> c = 'b') ab) (Some b0);
()
let find_sub = test "String.Sub.find_sub" @@ fun () ->
let abcbd = "abcbd" in
let empty = String.sub ~start:3 ~stop:3 abcbd in
let ab = String.sub ~start:0 ~stop:2 abcbd in
let b0 = String.sub ~start:1 ~stop:2 abcbd in
let b1 = String.sub ~start:3 ~stop:4 abcbd in
let abcbd = String.sub abcbd in
let eq = eq_option ~eq:String.Sub.equal ~pp:String.Sub.dump_raw in
eq (String.Sub.find_sub ~sub:ab empty) None;
eq (String.Sub.find_sub ~rev:true ~sub:ab empty) None;
eq (String.Sub.find_sub ~sub:(String.sub "") empty) (Some empty);
eq (String.Sub.find_sub ~rev:true ~sub:(String.sub "") empty) (Some empty);
eq (String.Sub.find_sub ~sub:ab abcbd) (Some ab);
eq (String.Sub.find_sub ~rev:true ~sub:ab abcbd) (Some ab);
eq (String.Sub.find_sub ~sub:empty abcbd) (Some (String.Sub.start abcbd));
eq (String.Sub.find_sub ~rev:true ~sub:empty abcbd)
(Some (String.Sub.stop abcbd));
eq (String.Sub.find_sub ~sub:(String.sub "b") abcbd) (Some b0);
eq (String.Sub.find_sub ~rev:true ~sub:(String.sub "b") abcbd) (Some b1);
eq (String.Sub.find_sub ~sub:b1 ab) (Some b0);
eq (String.Sub.find_sub ~rev:true ~sub:b1 ab) (Some b0);
()
let map = test "String.Sub.map[i]" @@ fun () ->
let next_letter c = Char.(of_byte @@ to_int c + 1) in
let base = "i34abcdbbb" in
let abcd = String.Sub.v ~start:3 ~stop:7 base in
let empty = String.Sub.v ~start:2 ~stop:2 base in
eqs (String.Sub.map (fun c -> fail "invoked"; c) empty) "";
eqs (String.Sub.map (fun c -> c) abcd) "abcd";
eqs (String.Sub.map next_letter abcd) "bcde";
eq_str String.Sub.(base_string (map next_letter abcd)) "bcde";
eqs (String.Sub.mapi (fun _ c -> fail "invoked"; c) empty) "";
eqs (String.Sub.mapi (fun i c -> Char.(of_byte @@ to_int c + i)) abcd) "aceg";
()
let fold = test "String.Sub.fold_{left,right}" @@ fun () ->
let empty = String.Sub.v ~start:2 ~stop:2 "ab" in
let abc = String.Sub.v ~start:3 ~stop:6 "i34abcdbbb" in
let eql = eq_list ~eq:(=) ~pp:pp_char in
String.Sub.fold_left (fun _ _ -> fail "invoked") () empty;
eql (String.Sub.fold_left (fun acc c -> c :: acc) [] empty) [];
eql (String.Sub.fold_left (fun acc c -> c :: acc) [] abc) ['c';'b';'a'];
String.Sub.fold_right (fun _ _ -> fail "invoked") empty ();
eql (String.Sub.fold_right (fun c acc -> c :: acc) empty []) [];
eql (String.Sub.fold_right (fun c acc -> c :: acc) abc []) ['a';'b';'c'];
()
let iter = test "String.Sub.iter[i]" @@ fun () ->
let empty = String.Sub.v ~start:2 ~stop:2 "ab" in
let abc = String.Sub.v ~start:3 ~stop:6 "i34abcdbbb" in
String.Sub.iter (fun _ -> fail "invoked") empty;
String.Sub.iteri (fun _ _ -> fail "invoked") empty;
(let i = ref 0 in
String.Sub.iter (fun c -> eq_char (String.Sub.get abc !i) c; incr i) abc);
String.Sub.iteri (fun i c -> eq_char (String.Sub.get abc i) c) abc;
()
(* Suite *)
let suite = suite "Base String functions"
[ misc;
head;
to_string;
start;
stop;
tail;
base;
extend;
reduce;
extent;
overlap;
append;
concat;
is_empty;
is_prefix;
is_infix;
is_suffix;
for_all;
exists;
same_base;
equal_bytes;
compare_bytes;
equal;
compare;
with_range;
with_index_range;
span;
trim;
cut;
cuts;
fields;
find;
find_sub;
map;
fold;
iter; ]
(*---------------------------------------------------------------------------
Copyright (c) 2015 The astring programmers
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
---------------------------------------------------------------------------*)
|