ends_with ~suffix s is true if and only if s ends with suffix.
since 4.13
Sourceval includes : affix:string ->string -> bool
includes affix s is true if and only if affix occurs in s.
Note. To test the same affix string multiple times, partially applying the ~affix argument and using the resulting function repeatedly is more efficient.
cut_last_while p s is (drop_last_while p s, take_last_while p s).
since 5.5
Splitting with separators
Note. To split the same sep string multiple times, partially applying the ~sep argument of these functions and using the resulting function repeatedly is more efficient.
split_first sep s is the pair Some (left, right) made of the two (possibly empty) substrings of s that are delimited by the first match of the separator sep in s or None if sep can't be found. Search for sep starts at position 0 and uses find_first.
split_last sep s is the pair Some (left, right) made of the two (possibly empty) substrings of s that are delimited by the last match of the separator sep in s or None if sep can't be found. Search for sep starts at position length s and uses find_last.
If sep is "", this is Some (s, "").
The invariant concat sep [left; right] = s holds.
since 5.5
Sourceval split_all : sep:string ->?drop:(string -> bool)->string ->string list
split_all sep s is the list of all substrings of s that are delimited by non-overlapping matches of the separator sep or the list [s] if sep can't be found. Search for sep starts at position 0 in increasing indexing order and uses find_all.
Substrings sub for which drop sub is true are not included in the result. drop defaults to Fun.const false.
If sep is "", this is [""; c0; ...; cn; ""] with ci the string of_char s.[i].
The invariant concat sep (split_all sep s) = s holds.
since 5.5
Sourceval rsplit_all : sep:string ->?drop:(string -> bool)->string ->string list
rsplit_all sep s is the list of all substrings of s that are delimited by non-overlapping matches of the separator sep or [s] if sep can't be found. Search for sep starts at position length s in decreasing indexing order and uses rfind_all.
Substrings sub for which drop sub is true are not included in the result. drop defaults to Fun.const false.
If sep is "", this is [""; c0; ...; cn; ""] with ci the string of_char s.[i].
The invariant concat sep (rsplit_all sep s) = s holds.
since 5.5
Sourceval split_on_char : char ->string ->string list
split_on_char sep s is the list of all (possibly empty) substrings of s that are delimited by the character sep. If s is empty, the result is the singleton list [""].
The function's result is specified by the following invariants:
The list is not empty.
Concatenating its elements using sep as a separator returns a string equal to the input (concat (make 1 sep) (split_on_char sep s) = s).
No string in the result contains the sep character.
find_first_index p start s is the index of the first character of s that satisfies predicate p at or after the index or position start (defaults to 0).
find_last_index p start s is the index of the last character of s that satisfies predicate p at or before the index or position start (defaults to length s).
Note. To find the same sub string multiple times, partially applying the ~sub argument of these functions and using the resulting function repeatedly is more efficient
find_last sub start s is the starting position of the last occurrence of sub in s at or before the index or position start (defaults to String.length s).
If sub is "" the result is Some start. The result of the function is always a valid index of s except when sub is "" and start is length s.
rfind_all sub f start s acc, starting with acc, folds f by decreasing index order over all non-overlapping starting positions of sub in s at or before the index or position start (defaults to String.length s). The result is acc if sub could not be found in s.
If sub is "", f gets invoked on all positions of s at or before start.
Note. To replace the same sub string multiple times, partially applying the ~sub argument of these functions and using the resulting function repeatedly is more efficient.
replace_all sub by start s replaces by by all non-overlapping occurrences of sub in s at or after the index or position start (defaults to 0). Occurrences are found in increasing indexing order.
If sub is "", this inserts by on all positions from start on.
spellcheck iter_dict s are the strings enumerated by the iterator iter_dict whose edit distance to s is the smallest and at most max_dist s. If multiple corrections are returned their order is as found in iter_dict. The default max_dist s is:
0 if s has 0 to 2 Unicode characters.
1 if s has 3 to 4 Unicode characters.
2 otherwise.
If your dictionary is a list l, a suitable iter_dict is given by (fun yield -> List.iter yield l).
All strings are assumed to be UTF-8 encoded, decoding errors are replaced by Uchar.rep characters.
since 5.4
Binary decoding of integers
The functions in this section binary decode integers from strings.
All following functions raise Invalid_argument if the characters needed at index i to decode the integer are not available.
Little-endian (resp. big-endian) encoding means that least (resp. most) significant bytes are stored first. Big-endian is also known as network byte order. Native-endian encoding is either little-endian or big-endian depending on Sys.big_endian.
32-bit and 64-bit integers are represented by the int32 and int64 types, which can be interpreted either as signed or unsigned numbers.
8-bit and 16-bit integers are represented by the int type, which has more bits than the binary encoding. These extra bits are sign-extended (or zero-extended) for functions which decode 8-bit or 16-bit integers and represented them with int values.
A seeded hash function for strings, with the same output value as Hashtbl.seeded_hash. This function allows this module to be passed as argument to the functor Hashtbl.MakeSeeded.
blit src src_pos dst dst_pos len copies len characters from string src starting at character indice src_pos, to the Bytes sequence dst starting at character indice dst_pos. Like String.blit. Compatible with the -safe-string option.
is_sub ~sub ~sub_pos s ~pos ~sub_len returns true iff the substring of sub starting at position sub_pos and of length sub_len is a substring of s starting at position pos.
filter_map f s calls (f a0) (f a1) … (f an) where a0 … an are the characters of s. It returns the string of characters ci such as f ai = Some ci (when f returns None, the corresponding element of s is discarded).
Sourceval split : by:string ->string ->string list
split ~by s splits the string s along the given string by. Alias to Split.list_cpy.
since 1.2
Utils
Sourceval compare_versions : string ->string -> int
compare_versions s1 s2 compares version stringss1 and s2, considering that numbers are above text.
since 0.13
Sourceval compare_natural : string ->string -> int
compare_natural s1 s2 is the Natural Sort Order, comparing chunks of digits as natural numbers. https://en.wikipedia.org/wiki/Natural_sort_order
since 1.3
Sourceval edit_distance : ?cutoff:int ->string ->string -> int
edit_distance ~cutoff s1 s2 is the edition distance between the two strings s1 and s2. This satisfies the classical distance axioms: it is always positive, symmetric, and satisfies the formula distance s1 s2 + distance s2 s3 >= distance s1 s3.
parametercutoff
if provided, it's a cap on the number of iterations. (since 3.0). This is useful if you just want to check whether the edit distance is less or equal than 2 without (use edit_distance s1 s2 ~cutoff:3 <= 2). note that contrary to what was previously documented here, the result can still be higher than cutoff if it's reached in <cutoff iterations. However if the result is < cutoff then it is accurate.