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 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
|
(*---------------------------------------------------------------------------
Copyright (c) 2015 The astring programmers. All rights reserved.
Distributed under the ISC license, see terms at the end of the file.
---------------------------------------------------------------------------*)
(** Alternative [Char] and [String] modules.
Open the module to use it. This defines {{!strf}one value} in your
scope, redefines the [(^)] operator, the [Char] module and the [String]
module.
Consult the {{!diff}differences} with the OCaml
{{!Stdlib.String}[String]}
module, the {{!port}porting guide} and a few {{!examples}examples}. *)
(** {1 String} *)
val strf : ('a, Format.formatter, unit, string) format4 -> 'a
(** [strf] is {!Format.asprintf}. *)
val ( ^ ) : string -> string -> string
(** [s ^ s'] is {!val:String.append}. *)
(** Characters (bytes in fact). *)
module Char : sig
(** {1 Bytes} *)
type t = char
(** The type for bytes. *)
val of_byte : int -> char
(** [of_byte b] is a byte from [b].
@raise Invalid_argument if [b] is not in the range \[[0x00];[0xFF]\]. *)
(**/**)
val unsafe_of_byte : int -> char
(**/**)
val of_int : int -> char option
(** [of_int b] is a byte from [b]. [None] is returned if [b] is not in the
range \[[0x00];[0xFF]\]. *)
val to_int : char -> int
(** [to_int b] is the byte [b] as an integer. *)
val hash : char -> int
(** [hash] is {!Hashtbl.hash}. *)
(** {1:pred Predicates} *)
val equal : char -> char -> bool
(** [equal b b'] is [b = b']. *)
val compare : char -> char -> int
(** [compare b b'] is {!Stdlib.compare}[ b b']. *)
(** {1 Bytes as US-ASCII characters} *)
(** US-ASCII character support
The following functions act only on US-ASCII code points, that
is on the bytes in range \[[0x00];[0x7F]\]. The functions can
be safely used on UTF-8 encoded strings, they will of course
only deal with US-ASCII related matters.
{b References.}
{ul
{- Vint Cerf.
{{:http://tools.ietf.org/html/rfc20}
{e ASCII format for Network Interchange}}. RFC 20, 1969.}} *)
module Ascii : sig
(** {1 Predicates} *)
val is_valid : char -> bool
(** [is_valid c] is [true] iff [c] is an US-ASCII character,
that is a byte in the range \[[0x00];[0x7F]\]. *)
val is_digit : char -> bool
(** [is_digit c] is [true] iff [c] is an US-ASCII digit
['0'] ... ['9'], that is a byte in the range \[[0x30];[0x39]\]. *)
val is_hex_digit : char -> bool
(** [is_hex_digit c] is [true] iff [c] is an US-ASCII hexadecimal
digit ['0'] ... ['9'], ['a'] ... ['f'], ['A'] ... ['F'],
that is a byte in one of the ranges \[[0x30];[0x39]\],
\[[0x41];[0x46]\], \[[0x61];[0x66]\]. *)
val is_upper : char -> bool
(** [is_upper c] is [true] iff [c] is an US-ASCII uppercase
letter ['A'] ... ['Z'], that is a byte in the range
\[[0x41];[0x5A]\]. *)
val is_lower : char -> bool
(** [is_lower c] is [true] iff [c] is an US-ASCII lowercase
letter ['a'] ... ['z'], that is a byte in the range
\[[0x61];[0x7A]\]. *)
val is_letter : char -> bool
(** [is_letter c] is [is_lower c || is_upper c]. *)
val is_alphanum : char -> bool
(** [is_alphanum c] is [is_letter c || is_digit c]. *)
val is_white : char -> bool
(** [is_white c] is [true] iff [c] is an US-ASCII white space
character, that is one of space [' '] ([0x20]), tab ['\t']
([0x09]), newline ['\n'] ([0x0A]), vertical tab ([0x0B]), form
feed ([0x0C]), carriage return ['\r'] ([0x0D]). *)
val is_blank : char -> bool
(** [is_blank c] is [true] iff [c] is an US-ASCII blank character,
that is either space [' '] ([0x20]) or tab ['\t'] ([0x09]). *)
val is_graphic : char -> bool
(** [is_graphic c] is [true] iff [c] is an US-ASCII graphic
character that is a byte in the range \[[0x21];[0x7E]\]. *)
val is_print : char -> bool
(** [is_print c] is [is_graphic c || c = ' ']. *)
val is_control : char -> bool
(** [is_control c] is [true] iff [c] is an US-ASCII control character,
that is a byte in the range \[[0x00];[0x1F]\] or [0x7F]. *)
(** {1 Casing transforms} *)
val uppercase : char -> char
(** [uppercase c] is [c] with US-ASCII characters ['a'] to ['z'] mapped
to ['A'] to ['Z']. *)
val lowercase : char -> char
(** [lowercase c] is [c] with US-ASCII characters ['A'] to ['Z'] mapped
to ['a'] to ['z']. *)
(** {1 Escaping to printable US-ASCII} *)
val escape : char -> string
(** [escape c] escapes [c] with:
{ul
{- ['\\'] ([0x5C]) escaped to the sequence ["\\\\"] ([0x5C],[0x5C]).}
{- Any byte in the ranges \[[0x00];[0x1F]\] and
\[[0x7F];[0xFF]\] escaped by an {e hexadecimal} ["\xHH"]
escape with [H] a capital hexadecimal number. These bytes
are the US-ASCII control characters and non US-ASCII bytes.}
{- Any other byte is left unchanged.}}
Use {!String.Ascii.unescape} to unescape. *)
val escape_char : char -> string
(** [escape_char c] is like {!escape} except is escapes [s] according
to OCaml's lexical conventions for characters with:
{ul
{- ['\b'] ([0x08]) escaped to the sequence ["\\b"] ([0x5C,0x62]).}
{- ['\t'] ([0x09]) escaped to the sequence ["\\t"] ([0x5C,0x74]).}
{- ['\n'] ([0x0A]) escaped to the sequence ["\\n"] ([0x5C,0x6E]).}
{- ['\r'] ([0x0D]) escaped to the sequence ["\\r"] ([0x5C,0x72]).}
{- ['\\''] ([0x27]) escaped to the sequence ["\\'"] ([0x5C,0x27]).}
{- Other bytes follow the rules of {!escape}}}
Use {!String.Ascii.unescape_string} to unescape. *)
end
(** {1:pp Pretty printing} *)
val pp : Format.formatter -> char -> unit
(** [pp ppf c] prints [c] on [ppf]. *)
val dump : Format.formatter -> char -> unit
(** [dump ppf c] prints [c] as a syntactically valid OCaml
char on [ppf] using {!Ascii.escape_char} *)
end
(** Strings, {{!Sub}substrings}, string {{!Set}sets} and {{!Map}maps}.
A string [s] of length [l] is a zero-based indexed sequence of [l]
bytes. An index [i] of [s] is an integer in the range
\[[0];[l-1]\], it represents the [i]th byte of [s] which can be
accessed using the string indexing operator [s.[i]].
{b Important.} OCaml's [string]s became immutable since 4.02.
Whenever possible compile your code with the [-safe-string]
option. This module does not expose any mutable operation on
strings and {b assumes} strings are immutable. See the
{{!port}porting guide}. *)
module String : sig
(** {1 String} *)
type t = string
(** The type for strings. Finite sequences of immutable bytes. *)
val empty : string
(** [empty] is an empty string. *)
val v : len:int -> (int -> char) -> string
(** [v len f] is a string [s] of length [len] with [s.[i] = f
i] for all indices [i] of [s]. [f] is invoked
in increasing index order.
@raise Invalid_argument if [len] is not in the range \[[0];
{!Sys.max_string_length}\]. *)
val length : string -> int
(** [length s] is the number of bytes in [s]. *)
val get : string -> int -> char
(** [get s i] is the byte of [s]' at index [i]. This is
equivalent to the [s.[i]] notation.
@raise Invalid_argument if [i] is not an index of [s]. *)
val get_byte : string -> int -> int
(** [get_byte s i] is [Char.to_int (get s i)] *)
(**/**)
val unsafe_get : string -> int -> char
val unsafe_get_byte : string -> int -> int
(**/**)
val head : ?rev:bool -> string -> char option
(** [head s] is [Some (get s h)] with [h = 0] if [rev = false] (default) or
[h = length s - 1] if [rev = true]. [None] is returned if [s] is
empty. *)
val get_head : ?rev:bool -> string -> char
(** [get_head s] is like {!head} but @raise Invalid_argument if [s]
is empty. *)
val hash : string -> int
(** [hash s] is {!Hashtbl.hash}[ s]. *)
(** {1:append Appending strings} *)
val append : string -> string -> string
(** [append s s'] appends [s'] to [s]. This is equivalent to
[s ^ s'].
@raise Invalid_argument if the result is longer than
{!Sys.max_string_length}. *)
val concat : ?sep:string -> string list -> string
(** [concat ~sep ss] concatenates the list of strings [ss], separating
each consecutive elements in the list [ss] with [sep] (defaults to
{!empty}).
@raise Invalid_argument if the result is longer than
{!Sys.max_string_length}. *)
(** {1 Predicates} *)
val is_empty : string -> bool
(** [is_empty s] is [length s = 0]. *)
val is_prefix : affix:string -> string -> bool
(** [is_prefix ~affix s] is [true] iff [affix.[i] = s.[i]] for
all indices [i] of [affix]. *)
val is_infix : affix:string -> string -> bool
(** [is_infix ~affix s] is [true] iff there exists an index [j] in [s] such
that for all indices [i] of [affix] we have [affix.[i] = s.[j + i]]. *)
val is_suffix : affix:string -> string -> bool
(** [is_suffix ~affix s] is true iff [affix.[n - i] = s.[m - i]] for all
indices [i] of [affix] with [n = String.length affix - 1] and [m =
String.length s - 1]. *)
val for_all : (char -> bool) -> string -> bool
(** [for_all p s] is [true] iff for all indices [i] of [s], [p s.[i]
= true]. *)
val exists : (char -> bool) -> string -> bool
(** [exists p s] is [true] iff there exists an index [i] of [s] with
[p s.[i] = true]. *)
val equal : string -> string -> bool
(** [equal s s'] is [s = s']. *)
val compare : string -> string -> int
(** [compare s s'] is [Stdlib.compare s s'], it compares the
byte sequences of [s] and [s'] in lexicographical order. *)
(** {1:extract Extracting substrings}
{b Tip.} These functions extract substrings as new strings. Using
{{!Sub}substrings} may be less wasteful and more flexible. *)
val with_range : ?first:int -> ?len:int -> string -> string
(** [with_range ~first ~len s] are the consecutive bytes of [s] whose
indices exist in the range \[[first];[first + len - 1]\].
[first] defaults to [0] and [len] to [max_int]. Note that
[first] can be any integer and [len] any positive integer.
@raise Invalid_argument if [len] is negative. *)
val with_index_range : ?first:int -> ?last:int -> string -> string
(** [with_index_range ~first ~last s] are the consecutive bytes of
[s] whose indices exist in the range \[[first];[last]\].
[first] defaults to [0] and [last] to [String.length s - 1].
Note that both [first] and [last] can be any integer. If
[first > last] the interval is empty and the empty string
is returned. *)
val trim : ?drop:(char -> bool) -> string -> string
(** [trim ~drop s] is [s] with prefix and suffix bytes satisfying
[drop] in [s] removed. [drop] defaults to {!Char.Ascii.is_white}. *)
val span : ?rev:bool -> ?min:int -> ?max:int -> ?sat:(char -> bool) ->
string -> (string * string)
(** [span ~rev ~min ~max ~sat s] is [(l, r)] where:
{ul
{- if [rev] is [false] (default), [l] is at least [min]
and at most [max] consecutive [sat] satisfying initial bytes of
[s] or {!empty} if there are no such bytes. [r] are the remaining
bytes of [s].}
{- if [rev] is [true], [r] is at least [min] and at most [max]
consecutive [sat] satisfying final bytes of [s] or {!empty}
if there are no such bytes. [l] are the remaining
the bytes of [s].}}
If [max] is unspecified the span is unlimited. If [min]
is unspecified it defaults to [0]. If [min > max] the condition
can't be satisfied and the left or right span, depending on [rev], is
always empty. [sat] defaults to [(fun _ -> true)].
The invariant [l ^ r = s] holds.
@raise Invalid_argument if [max] or [min] is negative. *)
val take : ?rev:bool -> ?min:int -> ?max:int -> ?sat:(char -> bool) ->
string -> string
(** [take ~rev ~min ~max ~sat s] is the matching span of {!span} without
the remaining one. In other words:
{[(if rev then snd else fst) @@ span ~rev ~min ~max ~sat s]} *)
val drop : ?rev:bool -> ?min:int -> ?max:int -> ?sat:(char -> bool) ->
string -> string
(** [drop ~rev ~min ~max ~sat s] is the remaining span of {!span} without
the matching span. In other words:
{[(if rev then fst else snd) @@ span ~rev ~min ~max ~sat s]} *)
val cut : ?rev:bool -> sep:string -> string -> (string * string) option
(** [cut ~sep s] is either the pair [Some (l,r)] of the two
(possibly empty) substrings of [s] that are delimited by the
first match of the non empty separator string [sep] or [None] if
[sep] can't be matched in [s]. Matching starts from the
beginning of [s] ([rev] is [false], default) or the end ([rev]
is [true]).
The invariant [l ^ sep ^ r = s] holds.
@raise Invalid_argument if [sep] is the empty string. *)
val cuts : ?rev:bool -> ?empty:bool -> sep:string -> string -> string list
(** [cuts sep s] is the list of all substrings of [s] that are
delimited by matches of the non empty separator string
[sep]. Empty substrings are omitted in the list if [empty] is
[false] (defaults to [true]).
Matching separators in [s] starts from the beginning of [s]
([rev] is [false], default) or the end ([rev] is [true]). Once
one is found, the separator is skipped and matching starts
again, that is separator matches can't overlap. If there is no
separator match in [s], the list [[s]] is returned.
The following invariants hold:
{ul
{- [concat ~sep (cuts ~empty:true ~sep s) = s]}
{- [cuts ~empty:true ~sep s <> []]}}
@raise Invalid_argument if [sep] is the empty string. *)
val fields : ?empty:bool -> ?is_sep:(char -> bool) -> string -> string list
(** [fields ~empty ~is_sep s] is the list of (possibly empty)
substrings that are delimited by bytes for which [is_sep] is
[true]. Empty substrings are omitted in the list if [empty] is
[false] (defaults to [true]). [is_sep] defaults to
{!Char.Ascii.is_white}. *)
(** {1:subs Substrings} *)
type sub
(** The type for {{!Sub}substrings}. *)
val sub : ?start:int -> ?stop:int -> string -> sub
(** [sub] is {!Sub.v}. *)
val sub_with_range : ?first:int -> ?len:int -> string -> sub
(** [sub_with_range] is like {!with_range} but returns a substring
value. If [first] is smaller than [0] the empty string at the start
of [s] is returned. If [first] is greater than the last index of [s]
the empty string at the end of [s] is returned. *)
val sub_with_index_range : ?first:int -> ?last:int -> string -> sub
(** [sub_with_index_range] is like {!with_index_range} but returns
a substring value. If [first] and [last] are smaller than [0]
the empty string at the start of [s] is returned. If [first] and
is greater than the last index of [s] the empty string at
the end of [s] is returned. If [first > last] and [first] is an
index of [s] the empty string at [first] is returned. *)
(** Substrings.
A substring defines a possibly empty subsequence of bytes in
a {e base} string.
The positions of a string [s] of length [l] are the slits found
before each byte and after the last byte of the string. They
are labelled from left to right by increasing number in the
range \[[0];[l]\].
{v
positions 0 1 2 3 4 l-1 l
+---+---+---+---+ +-----+
indices | 0 | 1 | 2 | 3 | ... | l-1 |
+---+---+---+---+ +-----+
v}
The [i]th byte index is between positions [i] and [i+1].
Formally we define a substring of [s] as being a subsequence
of bytes defined by a {e start} and a {e stop} position. The
former is always smaller or equal to the latter. When both
positions are equal the substring is {e empty}. Note that for a
given base string there are as many empty substrings as there
are positions in the string.
Like in strings, we index the bytes of a substring using
zero-based indices.
See how to {{!examples}use} substrings to parse data. *)
module Sub : sig
(** {1 Substrings} *)
type t = sub
(** The type for substrings. *)
val empty : sub
(** [empty] is the empty substring of the empty string {!String.empty}. *)
val v : ?start:int -> ?stop:int -> string -> sub
(** [v ~start ~stop s] is the substring of [s] that starts
at position [start] (defaults to [0]) and stops at position
[stop] (defaults to [String.length s]).
@raise Invalid_argument if [start] or [stop] are not positions of
[s] or if [stop < start]. *)
val start_pos : sub -> int
(** [start_pos s] is [s]'s start position in the base string. *)
val stop_pos : sub -> int
(** [stop_pos s] is [s]'s stop position in the base string. *)
val base_string : sub -> string
(** [base_string s] is [s]'s base string. *)
val length : sub -> int
(** [length s] is the number of bytes in [s]. *)
val get : sub -> int -> char
(** [get s i] is the byte of [s] at its zero-based index [i].
@raise Invalid_argument if [i] is not an index of [s]. *)
val get_byte : sub -> int -> int
(** [get_byte s i] is [Char.to_int (get s i)]. *)
(**/**)
val unsafe_get : sub -> int -> char
val unsafe_get_byte : sub -> int -> int
(**/**)
val head : ?rev:bool -> sub -> char option
(** [head s] is [Some (get s h)] with [h = 0] if [rev = false] (default) or
[h = length s - 1] if [rev = true]. [None] is returned if [s] is
empty. *)
val get_head : ?rev:bool -> sub -> char
(** [get_head s] is like {!head} but @raise Invalid_argument if [s]
is empty. *)
val of_string : string -> sub
(** [of_string s] is [v s] *)
val to_string : sub -> string
(** [to_string s] is the bytes of [s] as a string. *)
val rebase : sub -> sub
(** [rebase s] is [v (to_string s)]. This puts [s] on a base
string made solely of its bytes. *)
val hash : sub -> int
(** [hash s] is {!Hashtbl.hash s}. *)
(** {1:stretch Stretching substrings}
See the {{!fig}graphical guide}. *)
val start : sub -> sub
(** [start s] is the empty substring at the start position of [s]. *)
val stop : sub -> sub
(** [stop s] is the empty substring at the stop position of [s]. *)
val base : sub -> sub
(** [base s] is a substring that spans the whole base string of [s]. *)
val tail : ?rev:bool -> sub -> sub
(** [tail s] is [s] without its first ([rev] is [false], default)
or last ([rev] is [true]) byte or [s] if it is empty. *)
val extend : ?rev:bool -> ?max:int -> ?sat:(char -> bool) -> sub -> sub
(** [extend ~rev ~max ~sat s] extends [s] by at most [max]
consecutive [sat] satisfiying bytes of the base string located
after [stop s] ([rev] is [false], default) or before [start s]
([rev] is [true]). If [max] is unspecified the extension is
limited by the extents of the base string of [s]. [sat]
defaults to [fun _ -> true].
@raise Invalid_argument if [max] is negative. *)
val reduce : ?rev:bool -> ?max:int -> ?sat:(char -> bool) -> sub -> sub
(** [reduce ~rev ~max ~sat s] reduces [s] by at most [max]
consecutive [sat] satisfying bytes of [s] located before [stop
s] ([rev] is [false], default) or after [start s] ([rev] is
[true]). If [max] is unspecified the reduction is limited by
the extents of the substring [s]. [sat] defaults to [fun _ ->
true].
@raise Invalid_argument if [max] is negative. *)
val extent : sub -> sub -> sub
(** [extent s s'] is the smallest substring that includes all the
positions of [s] and [s'].
@raise Invalid_argument if [s] and [s'] are not on the same base
string according to physical equality. *)
val overlap : sub -> sub -> sub option
(** [overlap s s'] is the smallest substring that includes all the
positions common to [s] and [s'] or [None] if there are no
such positions. Note that the overlap substring may be empty.
@raise Invalid_argument if [s] and [s'] are not on the same base
string according to physical equality. *)
(** {1:append Appending substrings} *)
val append : sub -> sub -> sub
(** [append s s'] is like {!String.append}. The substrings can be
on different bases and the result is on a base string that holds
exactly the appended bytes. *)
val concat : ?sep:sub -> sub list -> sub
(** [concat ~sep ss] is like {!String.concat}. The substrings can
all be on different bases and the result is on a base string that
holds exactly the concatenated bytes. *)
(** {1:pred Predicates} *)
val is_empty : sub -> bool
(** [is_empty s] is [length s = 0]. *)
val is_prefix : affix:sub -> sub -> bool
(** [is_prefix] is like {!String.is_prefix}. Only bytes
are compared, [affix] can be on a different base string. *)
val is_infix : affix:sub -> sub -> bool
(** [is_infix] is like {!String.is_infix}. Only bytes
are compared, [affix] can be on a different base string. *)
val is_suffix : affix:sub -> sub -> bool
(** [is_suffix] is like {!String.is_suffix}. Only bytes
are compared, [affix] can be on a different base string. *)
val for_all : (char -> bool) -> sub -> bool
(** [for_all] is like {!String.for_all} on the substring. *)
val exists : (char -> bool) -> sub -> bool
(** [exists] is like {!String.exists} on the substring. *)
val same_base : sub -> sub -> bool
(** [same_base s s'] is [true] iff the substrings [s] and [s']
have the same base string according to physical equality. *)
val equal_bytes : sub -> sub -> bool
(** [equal_bytes s s'] is [true] iff the substrings [s] and [s'] have
exactly the same bytes. The substrings can be on a different
base string. *)
val compare_bytes : sub -> sub -> int
(** [compare_bytes s s'] compares the bytes of [s] and [s]' in
lexicographical order. The substrings can be on a different
base string. *)
val equal : sub -> sub -> bool
(** [equal s s'] is [true] iff [s] and [s'] have the same positions.
@raise Invalid_argument if [s] and [s'] are not on the same base
string according to physical equality. *)
val compare : sub -> sub -> int
(** [compare s s'] compares the positions of [s] and [s'] in
lexicographical order.
@raise Invalid_argument if [s] and [s'] are not on the same base
string according to physical equality. *)
(** {1:extract Extracting substrings}
Extracted substrings are always on the same base string as the
substring [s] acted upon. *)
val with_range : ?first:int -> ?len:int -> sub -> sub
(** [with_range] is like {!String.sub_with_range}. The indices are the
substring's zero-based ones, not those in the base string. *)
val with_index_range : ?first:int -> ?last:int -> sub -> sub
(** [with_index_range] is like {!String.sub_with_index_range}. The
indices are the substring's zero-based ones, not those in the
base string. *)
val trim : ?drop:(char -> bool) -> sub -> sub
(** [trim] is like {!String.trim}. If all bytes are dropped returns
an empty string located in the middle of the argument. *)
val span : ?rev:bool -> ?min:int -> ?max:int -> ?sat:(char -> bool) ->
sub -> (sub * sub)
(** [span] is like {!String.span}. For a substring [s] a left
empty span is [start s] and a right empty span is [stop s]. *)
val take : ?rev:bool -> ?min:int -> ?max:int -> ?sat:(char -> bool) ->
sub -> sub
(** [take] is like {!String.take}. *)
val drop : ?rev:bool -> ?min:int -> ?max:int -> ?sat:(char -> bool) ->
sub -> sub
(** [drop] is like {!String.drop}. *)
val cut : ?rev:bool -> sep:sub -> sub -> (sub * sub) option
(** [cut] is like {!String.cut}. [sep] can be on a different base string *)
val cuts : ?rev:bool -> ?empty:bool -> sep:sub -> sub -> sub list
(** [cuts] is like {!String.cuts}. [sep] can be on a different base
string *)
val fields : ?empty:bool -> ?is_sep:(char -> bool) -> sub -> sub list
(** [fields] is like {!String.fields}. *)
(** {1:traverse Traversing substrings} *)
val find : ?rev:bool -> (char -> bool) -> sub -> sub option
(** [find ~rev sat s] is the substring of [s] (if any) that spans the
first byte that satisfies [sat] in [s] after position [start s]
([rev] is [false], default) or before [stop s] ([rev] is [true]).
[None] is returned if there is no matching byte in [s]. *)
val find_sub :?rev:bool -> sub:sub -> sub -> sub option
(** [find_sub ~rev ~sub s] is the substring of [s] (if any) that
spans the first match of [sub] in [s] after position [start s]
([rev] is [false], default) or before [stop s] ([rev] is
[true]). Only bytes are compared and [sub] can be on a
different base string. [None] is returned if there is no match of
[sub] in [s]. *)
val filter : (char -> bool) -> sub -> sub
(** [filter sat s] is like {!String.filter}. The result is on a
base string that holds only the filtered bytes. *)
val filter_map : (char -> char option) -> sub -> sub
(** [filter_map f s] is like {!String.filter_map}. The result is on a
base string that holds only the filtered bytes. *)
val map : (char -> char) -> sub -> sub
(** [map] is like {!String.map}. The result is on a base string that
holds only the mapped bytes. *)
val mapi : (int -> char -> char) -> sub -> sub
(** [mapi] is like {!String.mapi}. The result is on a base string that
holds only the mapped bytes. The indices are the substring's
zero-based ones, not those in the base string. *)
val fold_left : ('a -> char -> 'a) -> 'a -> sub -> 'a
(** [fold_left] is like {!String.fold_left}. *)
val fold_right : (char -> 'a -> 'a) -> sub -> 'a -> 'a
(** [fold_right] is like {!String.fold_right}. *)
val iter : (char -> unit) -> sub -> unit
(** [iter] is like {!String.iter}. *)
val iteri : (int -> char -> unit) -> sub -> unit
(** [iteri] is like {!String.iteri}. The indices are the
substring's zero-based ones, not those in the base string. *)
(** {1:pp Pretty printing} *)
val pp : Format.formatter -> sub -> unit
(** [pp ppf s] prints [s]'s bytes on [ppf]. *)
val dump : Format.formatter -> sub -> unit
(** [dump ppf s] prints [s] as a syntactically valid OCaml string
on [ppf] using {!Ascii.escape_string}. *)
val dump_raw : Format.formatter -> sub -> unit
(** [dump_raw ppf s] prints an unspecified raw internal
representation of [s] on ppf. *)
(** {1:convert OCaml base type conversions} *)
val of_char : char -> sub
(** [of_char c] is a string that contains the byte [c]. *)
val to_char : sub -> char option
(** [to_char s] is the single byte in [s] or [None] if there is no byte
or more than one in [s]. *)
val of_bool : bool -> sub
(** [of_bool b] is a string representation for [b]. Relies on
{!Stdlib.string_of_bool}. *)
val to_bool : sub -> bool option
(** [to_bool s] is a [bool] from [s], if any. Relies on
{!Stdlib.bool_of_string}. *)
val of_int : int -> sub
(** [of_int i] is a string representation for [i]. Relies on
{!Stdlib.string_of_int}. *)
val to_int : sub -> int option
(** [to_int] is an [int] from [s], if any. Relies on
{!Stdlib.int_of_string}. *)
val of_nativeint : nativeint -> sub
(** [of_nativeint i] is a string representation for [i]. Relies on
{!Nativeint.of_string}. *)
val to_nativeint : sub -> nativeint option
(** [to_nativeint] is an [nativeint] from [s], if any. Relies on
{!Nativeint.to_string}. *)
val of_int32 : int32 -> sub
(** [of_int32 i] is a string representation for [i]. Relies on
{!Int32.of_string}. *)
val to_int32 : sub -> int32 option
(** [to_int32] is an [int32] from [s], if any. Relies on
{!Int32.to_string}. *)
val of_int64 : int64 -> sub
(** [of_int64 i] is a string representation for [i]. Relies on
{!Int64.of_string}. *)
val to_int64 : sub -> int64 option
(** [to_int64] is an [int64] from [s], if any. Relies on
{!Int64.to_string}. *)
val of_float : float -> sub
(** [of_float f] is a string representation for [f]. Relies on
{!Stdlib.string_of_float}. *)
val to_float : sub -> float option
(** [to_float s] is a [float] from [s], if any. Relies
on {!Stdlib.float_of_string}. *)
(** {1:fig Substring stretching graphical guide}
{v
+---+---+---+---+---+---+---+---+---+---+---+
| R | e | v | o | l | t | | n | o | w | ! |
+---+---+---+---+---+---+---+---+---+---+---+
|---------------| a
| start a
| stop a
|-----------| tail a
|-----------| tail ~rev:true a
|-----------------------------------| extend a
|-----------------------| extend ~rev:true a
|-------------------------------------------| base a
|-----------| b
| start b
| stop b
|-------| tail b
|-------| tail ~rev:true b
|-------------------------------------------| extend b
|-----------| extend ~rev:true b
|-------------------------------------------| base b
|-----------------------| extent a b
|---| overlap a b
| c
| start c
| stop c
| tail c
| tail ~rev:true c
|---------------| extend c
|---------------------------| extend ~rev:true c
|-------------------------------------------| base c
|-------------------| extent a c
None overlap a c
|---------------| d
| start d
| stop d
|-----------| tail d
|-----------| tail ~rev:true d
|---------------| extend d
|-------------------------------------------| extend ~rev:true d
|-------------------------------------------| base d
|---------------| extent d c
| overlap d c
v} *)
end
(** {1:traverse Traversing strings} *)
val find : ?rev:bool -> ?start:int -> (char -> bool) -> string -> int option
(** [find ~rev ~start sat s] is:
{ul
{- If [rev] is [false] (default). The smallest index [i], if any,
greater or equal to [start] such that [sat s.[i]] is [true].
[start] defaults to [0].}
{- If [rev] is [true]. The greatest index [i], if any, smaller or equal
to [start] such that [sat s.[i]] is [true].
[start] defaults to [String.length s - 1].}}
Note that [start] can be any integer. *)
val find_sub :?rev:bool -> ?start:int -> sub:string -> string -> int option
(** [find_sub ~rev ~start ~sub s] is:
{ul
{- If [rev] is [false] (default). The smallest index [i], if any,
greater or equal to [start] such that [sub] can be found starting
at [i] in [s] that is [s.[i] = sub.[0]], [s.[i+1] = sub.[1]], ...
[start] defaults to [0].}
{- If [rev] is [true]. The greatest index [i], if any, smaller
or equal to [start] such that [sub] can be found starting at
[i] in [s] that is [s.[i] = sub.[0]], [s.[i+1] = sub.[1]], ...
[start] defaults to [String.length s - 1].}}
Note that [start] can be any integer. *)
val filter : (char -> bool) -> string -> string
(** [filter sat s] is the string made of the bytes of [s] that satisfy [sat],
in the same order. *)
val filter_map : (char -> char option) -> string -> string
(** [filter_map f s] is the string made of the bytes of [s] as mapped by
[f], in the same order. *)
val map : (char -> char) -> string -> string
(** [map f s] is [s'] with [s'.[i] = f s.[i]] for all indices [i]
of [s]. [f] is invoked in increasing index order. *)
val mapi : (int -> char -> char) -> string -> string
(** [mapi f s] is [s'] with [s'.[i] = f i s.[i]] for all indices [i]
of [s]. [f] is invoked in increasing index order. *)
val fold_left : ('a -> char -> 'a) -> 'a -> string -> 'a
(** [fold_left f acc s] is
[f (]...[(f (f acc s.[0]) s.[1])]...[) s.[m]]
with [m = String.length s - 1]. *)
val fold_right : (char -> 'a -> 'a) -> string -> 'a -> 'a
(** [fold_right f s acc] is
[f s.[0] (f s.[1] (]...[(f s.[m] acc) )]...[)]
with [m = String.length s - 1]. *)
val iter : (char -> unit) -> string -> unit
(** [iter f s] is [f s.[0]; f s.[1];] ...
[f s.[m]] with [m = String.length s - 1]. *)
val iteri : (int -> char -> unit) -> string -> unit
(** [iteri f s] is [f 0 s.[0]; f 1 s.[1];] ...
[f m s.[m]] with [m = String.length s - 1]. *)
(** {1:unique Uniqueness} *)
val uniquify : string list -> string list
(** [uniquify ss] is [ss] without duplicates, the list order is
preserved. *)
(** {1:ascii Strings as US-ASCII character sequences} *)
(** US-ASCII string support.
{b References.}
{ul
{- Vint Cerf.
{{:http://tools.ietf.org/html/rfc20}
{e ASCII format for Network Interchange}}. RFC 20, 1969.}} *)
module Ascii : sig
(** {1:pred Predicates} *)
val is_valid : string -> bool
(** [is_valid s] is [true] iff only for all indices [i] of [s],
[s.[i]] is an US-ASCII character, i.e. a byte in the range
\[[0x00];[0x7F]\]. *)
(** {1:case Casing transforms}
The following functions act only on US-ASCII code points that
is on bytes in range \[[0x00];[0x7F]\], leaving any other byte
intact. The functions can be safely used on UTF-8 encoded
strings; they will of course only deal with US-ASCII
casings. *)
val uppercase : string -> string
(** [uppercase s] is [s] with US-ASCII characters ['a'] to ['z'] mapped
to ['A'] to ['Z']. *)
val lowercase : string -> string
(** [lowercase s] is [s] with US-ASCII characters ['A'] to ['Z'] mapped
to ['a'] to ['z']. *)
val capitalize : string -> string
(** [capitalize s] is like {!uppercase} but performs the map only
on [s.[0]]. *)
val uncapitalize : string -> string
(** [uncapitalize s] is like {!lowercase} but performs the map only
on [s.[0]]. *)
(** {1:esc Escaping to printable US-ASCII} *)
val escape : string -> string
(** [escape s] is [s] with:
{ul
{- Any ['\\'] ([0x5C]) escaped to the sequence
["\\\\"] ([0x5C],[0x5C]).}
{- Any byte in the ranges \[[0x00];[0x1F]\] and
\[[0x7F];[0xFF]\] escaped by an {e hexadecimal} ["\xHH"]
escape with [H] a capital hexadecimal number. These bytes
are the US-ASCII control characters and non US-ASCII bytes.}
{- Any other byte is left unchanged.}} *)
val unescape : string -> string option
(** [unescape s] unescapes what {!escape} did. The letters of hex
escapes can be upper, lower or mixed case, and any two letter
hex escape is decoded to its corresponding byte. Any other
escape not defined by {!escape} or truncated escape makes the
function return [None].
The invariant [unescape (escape s) = Some s] holds. *)
val escape_string : string -> string
(** [escape_string s] is like {!escape} except it escapes [s]
according to OCaml's lexical conventions for strings with:
{ul
{- Any ['\b'] ([0x08]) escaped to the sequence ["\\b"] ([0x5C,0x62]).}
{- Any ['\t'] ([0x09]) escaped to the sequence ["\\t"] ([0x5C,0x74]).}
{- Any ['\n'] ([0x0A]) escaped to the sequence ["\\n"] ([0x5C,0x6E]).}
{- Any ['\r'] ([0x0D]) escaped to the sequence ["\\r"] ([0x5C,0x72]).}
{- Any ['\"'] ([0x22]) escaped to the sequence ["\\\""] ([0x5C,0x22]).}
{- Any other byte follows the rules of {!escape}}} *)
val unescape_string : string -> string option
(** [unescape_string] is to {!escape_string} what {!unescape}
is to {!escape} and also additionally unescapes
the sequence ["\\'"] ([0x5C,0x27]) to ["'"] ([0x27]). *)
end
(** {1:pp Pretty printing} *)
val pp : Format.formatter -> string -> unit
(** [pp ppf s] prints [s]'s bytes on [ppf]. *)
val dump : Format.formatter -> string -> unit
(** [dump ppf s] prints [s] as a syntactically valid OCaml string on
[ppf] using {!Ascii.escape_string}. *)
(** {1 String sets and maps} *)
type set
(** The type for string sets. *)
(** String sets. *)
module Set : sig
(** {1 String sets} *)
include Set.S with type elt := string
and type t := set
type t = set
val min_elt : set -> string option
(** Exception safe {!Set.S.min_elt}. *)
val get_min_elt : set -> string
(** [get_min_elt] is like {!min_elt} but @raise Invalid_argument
on the empty set. *)
val max_elt : set -> string option
(** Exception safe {!Set.S.max_elt}. *)
val get_max_elt : set -> string
(** [get_max_elt] is like {!max_elt} but @raise Invalid_argument
on the empty set. *)
val choose : set -> string option
(** Exception safe {!Set.S.choose}. *)
val get_any_elt : set -> string
(** [get_any_elt] is like {!choose} but @raise Invalid_argument on the
empty set. *)
val find : string -> set -> string option
(** Exception safe {!Set.S.find}. *)
val get : string -> set -> string
(** [get] is like {!Set.S.find} but @raise Invalid_argument if
[elt] is not in [s]. *)
val of_list : string list -> set
(** [of_list ss] is a set from the list [ss]. *)
val of_stdlib_set : Set.Make(String).t -> set
(** [of_stdlib_set s] is a set from the stdlib-compatible set [s]. *)
val to_stdlib_set : set -> Set.Make(String).t
(** [to_stdlib_set s] is the stdlib-compatible set equivalent to [s]. *)
val pp : ?sep:(Format.formatter -> unit -> unit) ->
(Format.formatter -> string -> unit) ->
Format.formatter -> set -> unit
(** [pp ~sep pp_elt ppf ss] formats the elements of [ss] on
[ppf]. Each element is formatted with [pp_elt] and elements
are separated by [~sep] (defaults to
{!Format.pp_print_cut}. If the set is empty leaves [ppf]
untouched. *)
val dump : Format.formatter -> set -> unit
(** [dump ppf ss] prints an unspecified representation of [ss] on
[ppf]. *)
end
(** String maps. *)
module Map : sig
(** {1 String maps} *)
include Map.S with type key := string
val min_binding : 'a t -> (string * 'a) option
(** Exception safe {!Map.S.min_binding}. *)
val get_min_binding : 'a t -> (string * 'a)
(** [get_min_binding] is like {!min_binding} but @raise Invalid_argument
on the empty map. *)
val max_binding : 'a t -> (string * 'a) option
(** Exception safe {!Map.S.max_binding}. *)
val get_max_binding : 'a t -> string * 'a
(** [get_max_binding] is like {!max_binding} but @raise Invalid_argument
on the empty map. *)
val choose : 'a t -> (string * 'a) option
(** Exception safe {!Map.S.choose}. *)
val get_any_binding : 'a t -> (string * 'a)
(** [get_any_binding] is like {!choose} but @raise Invalid_argument
on the empty map. *)
val find : string -> 'a t -> 'a option
(** Exception safe {!Map.S.find}. *)
val get : string -> 'a t -> 'a
(** [get k m] is like {!Map.S.find} but raises [Invalid_argument] if
[k] is not bound in [m]. *)
val dom : 'a t -> set
(** [dom m] is the domain of [m]. *)
val of_list : (string * 'a) list -> 'a t
(** [of_list bs] is [List.fold_left (fun m (k, v) -> add k v m) empty
bs]. *)
val of_stdlib_map : 'a Map.Make(String).t -> 'a t
(** [of_stdlib_map m] is a map from the stdlib-compatible map [m]. *)
val to_stdlib_map : 'a t -> 'a Map.Make(String).t
(** [to_stdlib_map m] is the stdlib-compatible map equivalent to [m]. *)
val pp : ?sep:(Format.formatter -> unit -> unit) ->
(Format.formatter -> string * 'a -> unit) -> Format.formatter ->
'a t -> unit
(** [pp ~sep pp_binding ppf m] formats the bindings of [m] on
[ppf]. Each binding is formatted with [pp_binding] and
bindings are separated by [sep] (defaults to
{!Format.pp_print_cut}). If the map is empty leaves [ppf]
untouched. *)
val dump : (Format.formatter -> 'a -> unit) -> Format.formatter ->
'a t -> unit
(** [dump pp_v ppf m] prints an unspecified representation of [m] on
[ppf] using [pp_v] to print the map codomain elements. *)
val dump_string_map : Format.formatter -> string t -> unit
(** [dump_string_map ppf m] prints an unspecified representation of the
string map [m] on [ppf]. *)
end
type +'a map = 'a Map.t
(** The type for maps from strings to values of type 'a. *)
(** {1:convert OCaml base type conversions} *)
val of_char : char -> string
(** [of_char c] is a string that contains the byte [c]. *)
val to_char : string -> char option
(** [to_char s] is the single byte in [s] or [None] if there is no byte
or more than one in [s]. *)
val of_bool : bool -> string
(** [of_bool b] is a string representation for [b]. Relies on
{!Stdlib.string_of_bool}. *)
val to_bool : string -> bool option
(** [to_bool s] is a [bool] from [s], if any. Relies on
{!Stdlib.bool_of_string}. *)
val of_int : int -> string
(** [of_int i] is a string representation for [i]. Relies on
{!Stdlib.string_of_int}. *)
val to_int : string -> int option
(** [to_int] is an [int] from [s], if any. Relies on
{!Stdlib.int_of_string}. *)
val of_nativeint : nativeint -> string
(** [of_nativeint i] is a string representation for [i]. Relies on
{!Nativeint.of_string}. *)
val to_nativeint : string -> nativeint option
(** [to_nativeint] is an [nativeint] from [s], if any. Relies on
{!Nativeint.to_string}. *)
val of_int32 : int32 -> string
(** [of_int32 i] is a string representation for [i]. Relies on
{!Int32.of_string}. *)
val to_int32 : string -> int32 option
(** [to_int32] is an [int32] from [s], if any. Relies on
{!Int32.to_string}. *)
val of_int64 : int64 -> string
(** [of_int64 i] is a string representation for [i]. Relies on
{!Int64.of_string}. *)
val to_int64 : string -> int64 option
(** [to_int64] is an [int64] from [s], if any. Relies on
{!Int64.to_string}. *)
val of_float : float -> string
(** [of_float f] is a string representation for [f]. Relies on
{!Stdlib.string_of_float}. *)
val to_float : string -> float option
(** [to_float s] is a [float] from [s], if any. Relies
on {!Stdlib.float_of_string}. *)
end
(** {1:diff Differences with the OCaml [String] module}
First note that it is not a goal of {!Astring} to maintain
compatibility with the OCaml {{!Stdlib.String}[String]} module.
In [Astring]:
{ul
{- Strings are assumed to be immutable.}
{- Deprecated functions are not included.}
{- Some rarely used functions are dropped, some signatures and names
are altered, a few often needed functions are added.}
{- Scanning functions are not doubled for supporting forward and
reverse directions. Both directions are supported via a single
function and an optional [rev] argument.}
{- Functions do not raise [Not_found]. They return [option] values
instead.}
{- Functions escaping bytes to printable US-ASCII characters use
capital hexadecimal escapes rather than decimal ones.}
{- US-ASCII string support is collected in the {!Char.Ascii} and
{!String.Ascii} submodules. The functions make sure to operate
only on the US-ASCII code points (rather than
{{:http://www.ecma-international.org/publications/standards/Ecma-094.htm}ISO/IEC
8859-1} code points). This means they can safely be used on
UTF-8 encoded strings, they will of course only deal with the
US-ASCII subset U+0000 to U+007F of
{{:http://unicode.org/glossary/#unicode_scalar_value} Unicode
scalar values}.}
{- The module has pre-applied exception safe {!String.Set}
and {!String.Map} submodules.}}
{1:port Porting guide}
Opening [Astring] at the top of a module that uses the OCaml
standard library in a project that compiles with [-safe-string]
will either result in typing errors or compatible behaviour except
for uses of the {!String.trim} function, {{!porttrim}see below}.
If for some reason you can't compile your project with
[-safe-string] this {b may} not be a problem. However you have to
make sure that your code does not depend on fresh strings being
returned by functions of the [String] module. The functions of
{!Astring.String} assume strings to be immutable and thus do not
always allocate fresh strings for their results. This is the case
for example for the {!( ^ )} operator redefinition: no string is
allocated whenever one of its arguments is an empty string. That
being said it is still better to first make your project compile
with [-safe-string] and then port to [Astring].
The
{{:http://caml.inria.fr/pub/docs/manual-ocaml/libref/String.html#VALsub}[String.sub]} function
is renamed to {!String.with_range}. If you are working with
{!String.find} you may find it easier to use
{!String.with_index_range} which takes indices as arguments and is thus
directly usable with the result of {!String.find}. But in general
index based string processing should be frowned upon and replaced
by {{!String.extract} substring extraction} combinators.
{2:porttrim Porting [String.trim] usages}
The standard OCaml [String.trim] function only trims the
characters [' '], ['\t'], ['\n'], ['\012'], ['\r']. In
[Astring] the {{!Char.Ascii.is_white}default set} adds
vertical tab ([0x0B]) to the set to match the behaviour of
the C [isspace(3)] function.
If you want to preserve the behaviour of the original function you
can replace any use of [String.trim] with the following
[std_ocaml_trim] function:
{[
let std_ocaml_trim s =
let drop = function
| ' ' | '\n' | '\012' | '\r' | '\t' -> true
| _ -> false
in
String.trim ~drop s
]}
{1:examples Examples}
We show how to use {{!String.Sub}substrings} to quickly devise LL(1)
parsers. To keep it simple we do not implement precise error
report, but note that it would be easy to add it by replacing the
[raise Exit] calls by an exception with more information: we have
everything at hand at these call points to report good error
messages.
The first example parses version numbers structured as follows:
{[
[v|V]major.minor[.patch][(+|-)info]
]}
an unreadable {!Str} regular expression for this would be:
{[
"[vV]?\\([0-9]+\\)\\.\\([0-9]+\\)\\(\\.\\([0-9]+\\)\\)?\\([+-]\\(.*\\)\\)?"
]}
Using substrings is certainly less terse but note that the parser is
made of reusable sub-functions.
{[
let parse_version : string -> (int * int * int * string option) option =
fun s -> try
let parse_opt_v s = match String.Sub.head s with
| Some ('v'|'V') -> String.Sub.tail s
| Some _ -> s
| None -> raise Exit
in
let parse_dot s = match String.Sub.head s with
| Some '.' -> String.Sub.tail s
| Some _ | None -> raise Exit
in
let parse_int s =
match String.Sub.span ~min:1 ~sat:Char.Ascii.is_digit s with
| (i, _) when String.Sub.is_empty i -> raise Exit
| (i, s) ->
match String.Sub.to_int i with
| None -> raise Exit | Some i -> i, s
in
let maj, s = parse_int (parse_opt_v (String.sub s)) in
let min, s = parse_int (parse_dot s) in
let patch, s = match String.Sub.head s with
| Some '.' -> parse_int (parse_dot s)
| _ -> 0, s
in
let info = match String.Sub.head s with
| Some ('+' | '-') -> Some (String.Sub.(to_string (tail s)))
| Some _ -> raise Exit
| None -> None
in
Some (maj, min, patch, info)
with Exit -> None
]}
The second example parses space separated key-value bindings
environments of the form:
{[
key0 = value0 key2 = value2 ...]}
To support values with spaces, values can be quoted between two
['"'] characters. If they are quoted then any ["\\\""] subsequence
([0x2F],[0x22]) is interpreted as the character ['"'] ([0x22]) and
["\\\\"] ([0x2F],[0x2F]) is interpreted as the character ['\\']
([0x22]).
{[
let parse_env : string -> string String.map option =
fun s -> try
let skip_white s = String.Sub.drop ~sat:Char.Ascii.is_white s in
let parse_key s =
let id_char c = Char.Ascii.is_letter c || c = '_' in
match String.Sub.span ~min:1 ~sat:id_char s with
| (key, _) when String.Sub.is_empty key -> raise Exit
| (key, rem) -> (String.Sub.to_string key), rem
in
let parse_eq s = match String.Sub.head s with
| Some '=' -> String.Sub.tail s
| Some _ | None -> raise Exit
in
let parse_value s = match String.Sub.head s with
| Some '"' -> (* quoted *)
let is_data = function '\\' | '"' -> false | _ -> true in
let rec loop acc s =
let data, rem = String.Sub.span ~sat:is_data s in
match String.Sub.head rem with
| Some '"' ->
let acc = List.rev (data :: acc) in
String.Sub.(to_string @@ concat acc), (String.Sub.tail rem)
| Some '\\' ->
let rem = String.Sub.tail rem in
begin match String.Sub.head rem with
| Some ('"' | '\\' as c) ->
let acc = String.(sub (of_char c)) :: data :: acc in
loop acc (String.Sub.tail rem)
| Some _ | None -> raise Exit
end
| None | Some _ -> raise Exit
in
loop [] (String.Sub.tail s)
| Some _ ->
let is_data c = not (Char.Ascii.is_white c) in
let data, rem = String.Sub.span ~sat:is_data s in
String.Sub.to_string data, rem
| None -> "", s
in
let rec parse_bindings acc s =
if String.Sub.is_empty s then acc else
let key, s = parse_key s in
let value, s = s |> skip_white |> parse_eq |> skip_white |> parse_value in
parse_bindings (String.Map.add key value acc) (skip_white s)
in
Some (String.sub s |> skip_white |> parse_bindings String.Map.empty)
with Exit -> None
]}
*)
(*---------------------------------------------------------------------------
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.
---------------------------------------------------------------------------*)
|