text
stringlengths
0
601k
let contains_from s i c = let l = length s in if i < 0 || i > l then invalid_arg " String . contains_from / Bytes . contains_from " else try ignore ( index_rec s l i c ) ; true with Not_found -> false
let contains s c = contains_from s 0 c
let rcontains_from s i c = if i < 0 || i >= length s then invalid_arg " String . rcontains_from / Bytes . rcontains_from " else try ignore ( rindex_rec s i c ) ; true with Not_found -> false
let uppercase_ascii s = B . uppercase_ascii ( bos s ) |> bts
let lowercase_ascii s = B . lowercase_ascii ( bos s ) |> bts
let capitalize_ascii s = B . capitalize_ascii ( bos s ) |> bts
let uncapitalize_ascii s = B . uncapitalize_ascii ( bos s ) |> bts
let compare ( x : t ) ( y : t ) = Stdlib . compare x y
let split_on_char sep s = let r = ref [ ] in let j = ref ( length s ) in for i = length s - 1 downto 0 do if unsafe_get s i = sep then begin r := sub s ( i + 1 ) ( ! j - i - 1 ) :: ! r ; j := i end done ; sub s 0 ! j :: ! r
let uppercase s = B . uppercase ( bos s ) |> bts
let lowercase s = B . lowercase ( bos s ) |> bts
let capitalize s = B . capitalize ( bos s ) |> bts
let uncapitalize s = B . uncapitalize ( bos s ) |> bts
let to_seq s = bos s |> B . to_seq
let to_seqi s = bos s |> B . to_seqi
let of_seq g = B . of_seq g |> bts
module String = struct external get : string -> int -> char = " % string_safe_get " external length : string -> int = " % string_length " external unsafe_get : string -> int -> char = " % string_unsafe_get " include Bytes_set_primitives end
let ( ^ ) = ( ^ )
let concat ( ? sep = " " ) l = match l with | [ ] -> " " | [ x ] -> x | l -> Caml . String . concat ~ sep l ; ;
let iter t ~ f = Caml . String . iter t ~ f
let string_after s n = String . sub s n ( String . length s - n )
let quote s = let len = String . length s in let buf = Buffer . create ( 2 * len ) in for i = 0 to len - 1 do match s . [ i ] with ' [ ' | ' ] ' | ' ' * | ' . ' | ' ' \\ | ' ' ? | ' ' + | ' ' ^ | ' ' $ as c -> Buffer . add_char buf ' ' ; \\ Buffer . add_char buf c | c -> Buffer . add_char buf c done ; Buffer . contents...
let split_char_bounded str ~ on ~ max = if str = " " then [ ] else if max = 1 then [ str ] else let rec loop offset tokens = if tokens = max - 1 then [ sub str offset ( length str - offset ) ] else try let index = index_from str offset on in if index = offset then " " ( :: loop ( offset + 1 ) ( tokens + 1 ) ) else let ...
let split_char_unbounded str ~ on = if str = " " then [ ] else let rec loop acc offset = try begin let index = rindex_from str offset on in if index = offset then loop ( " " :: acc ) ( index - 1 ) else let token = sub str ( index + 1 ) ( offset - index ) in loop ( token :: acc ) ( index - 1 ) end with Not_found -> ( su...
let of_char = String . make 1
let full_split str ~ on = if str = " " then [ ] else let sep = of_char on in let rec loop acc offset = try begin let index = rindex_from str offset on in if index = offset then loop ( sep :: acc ) ( index - 1 ) else let token = sub str ( index + 1 ) ( offset - index ) in loop ( sep :: token :: acc ) ( index - 1 ) end w...
let split ? max s ~ on = match max with | None -> split_char_unbounded s ~ on | Some max -> split_char_bounded s ~ on ~ max
let rindex_from_on s ~ offset ~ on = let rec loop i = if i < 0 then raise Not_found else if String . contains on s . [ i ] then i else loop ( i - 1 ) in loop offset
let trim_left_sub s ~ pos ~ len ~ chars = let start_pos = let final = pos + len in let rec loop last_char i = if i = final then last_char else if String . contains chars s . [ i ] then loop ( i + 1 ) ( i + 1 ) else last_char in loop pos pos in let new_len = len - ( start_pos - pos ) in String . sub s start_pos new_len
let split_trim_left str ~ on ~ trim = if str = " " then [ ] else let rec loop acc offset = try begin let index = rindex_from_on str ~ offset ~ on in if index = offset then loop ( " " :: acc ) ( index - 1 ) else let token = trim_left_sub str ~ pos ( : index + 1 ) ~ len ( : offset - index ) ~ chars : trim in loop ( token...
let first_char_ne s c = String . length s > 0 && s . [ 0 ] <> c
let trim_left s = if first_char_ne s ' ' then s else let len = String . length s in try for i = 0 to len - 1 do if s . [ i ] <> ' ' then raise ( Found_int i ) done ; " " with Found_int non_space -> sub s non_space ( len - non_space )
let substr_eq ( ? start = 0 ) s ~ pattern = try for i = 0 to String . length pattern - 1 do if s . [ i + start ] <> pattern . [ i ] then raise Exit done ; true with _ -> false
let find_from ( ? start = 0 ) str ~ pattern = try for i = start to ( String . length str ) - ( String . length pattern ) do if substr_eq ~ start : i str ~ pattern then raise ( Found_int i ) done ; None with | Found_int i -> Some i | _ -> None
let find_min l ~ f = let rec loop x fx = function | [ ] -> Some ( x , fx ) | x ' :: xs -> let fx ' = f x ' in if fx ' < fx then loop x ' fx ' xs else loop x fx xs in match l with | [ ] -> None | x :: xs -> loop x ( f x ) xs
let replace_all str ~ pattern ~ with_ = let ( slen , plen ) = String . ( length str , length pattern ) in let buf = Buffer . create slen in let rec loop i = match find_from ~ start : i str ~ pattern with | None -> Buffer . add_substring buf str i ( slen - i ) ; Buffer . contents buf | Some j -> Buffer . add_substring b...
let replace_all_assoc str tbl = let slen = String . length str in let buf = Buffer . create slen in let rec loop i = if i >= slen then Buffer . contents buf else let r = try let found = ref false in let e = find_min tbl ~ f ( : fun ( pattern , with_ ) -> match find_from ~ start : i str ~ pattern with | None -> max_int ...
let iteri f l = let rec loop i = function | [ ] -> ( ) | x :: xs -> ( f i x ) ; loop ( succ i ) xs in loop 0 l
let of_list xs = let l = List . length xs in let s = Bytes . create l in iteri ( fun i c -> Bytes . set s i c ) xs ; Bytes . unsafe_to_string s
let to_list s = let rec loop acc i = if i = - 1 then acc else loop ( s . [ i ] :: acc ) ( pred i ) in loop [ ] ( String . length s - 1 )
let of_array a = let len = Array . length a in let bytes = Bytes . create len in for i = 0 to len - 1 do Bytes . set bytes i a . ( i ) done ; Bytes . unsafe_to_string bytes
let to_array s = Array . init ( String . length s ) ( String . get s )
let cut s ~ on = let sep_max = length on - 1 in if sep_max < 0 then invalid_arg " Stringext . cut : empty separator " else let s_max = length s - 1 in if s_max < 0 then None else let k = ref 0 in let i = ref 0 in try while ( ! i + sep_max <= s_max ) do if unsafe_get s ! i <> unsafe_get on 0 then incr i else begin k := ...
let rcut s ~ on = let sep_max = length on - 1 in if sep_max < 0 then invalid_arg " Stringext . rcut : empty separator " else let s_max = length s - 1 in if s_max < 0 then None else let k = ref 0 in let i = ref s_max in try while ( ! i >= sep_max ) do if unsafe_get s ! i <> unsafe_get on sep_max then decr i else begin l...
let chop_prefix s ~ prefix = let prefix_l = String . length prefix in let string_l = String . length s in if prefix_l > string_l then None else try for i = 0 to prefix_l - 1 do if s . [ i ] <> prefix . [ i ] then raise Exit ; done ; Some ( String . sub s prefix_l ( string_l - prefix_l ) ) with _ -> None
let drop s n = let l = String . length s in if n >= l then " " else String . sub s n ( l - n )
let take s n = if n >= String . length s then s else String . sub s 0 n
let init len f = let s = create len in begin for i = 0 to len - 1 do unsafe_set s i ( f i ) done ; s end
let is_empty s = s = " "
let not_empty s = s <> " "
let starts_with str p = let len = length p in if length str < len then false else Return . cc ( fun id -> begin for i = 0 to len - 1 do if unsafe_get str i <> unsafe_get p i then Return . k id false done ; true end )
let ends_with str p = let el = length p and sl = length str in let diff = sl - el in if diff < 0 then false else Return . cc @@ fun id -> begin for i = 0 to el - 1 do if get str ( diff + i ) <> get p i then Return . k id false done ; true end
let of_char = make 1
let drop_while f s = let len = length s in let found = ref false in let i = ref 0 in begin while ! i < len && not ! found do if not ( f s . [ ! i ] ) then found := true else incr i done ; String . sub s ! i ( len - ! i ) end
let neg n = let len = String . length n in if len > 0 && n . [ 0 ] = ' ' - then String . sub n 1 ( len - 1 ) else " " - ^ n
let map f s = let l = length s in if l = 0 then s else let r = create l in ( for i = 0 to l - 1 do unsafe_set r i ( f ( unsafe_get s i ) ) done ; r )
let lowercase s = map Char . lowercase s
let find_from str ofs sub = let sublen = length sub in if sublen = 0 then ofs else let len = length str in if len = 0 then raise Not_found else if 0 > ofs || ofs >= len then raise ( Invalid_argument " index out of bounds " ) else Return . cc ( fun id -> begin for i = ofs to len - sublen do done ; end )
let find str sub = find_from str 0 sub
let split str sep = let p = find str sep in let len = length sep in let slen = length str in ( sub str 0 p , sub str ( p + len ) ( slen - p - len ) )
let rfind_from str suf sub = let sublen = length sub and len = length str in if sublen = 0 then len else if len = 0 then raise Not_found else if 0 > suf || suf >= len then raise ( Invalid_argument " index out of bounds " ) else Return . cc ( fun id -> begin for i = suf - sublen + 1 downto 0 do done ; end )
let rfind str sub = rfind_from str ( String . length str - 1 ) sub
let nsplit str sep = if str = " " then [ ] else if sep = " " then invalid_arg " nsplit : empty sep not allowed " else let seplen = String . length sep in let rec aux acc ofs = if ofs >= 0 then ( match ( try Some ( rfind_from str ofs sep ) with Not_found -> None ) with | Some idx -> let end_of_sep = idx + seplen - 1 in ...
let edit_distance a b cutoff = let la , lb = String . length a , String . length b in let cutoff = min ( max la lb ) cutoff in if abs ( la - lb ) > cutoff then None else begin let m = Array . make_matrix ( la + 1 ) ( lb + 1 ) ( cutoff + 1 ) in m . ( 0 ) . ( 0 ) <- 0 ; for i = 1 to la do m . ( i ) . ( 0 ) <- i ; done ; ...
type genericMap = int CharMap . t
let string_of_op = function | Add -> " " + | Mul -> " " * | Neg | Sub -> " " - | Div -> " " / | Mod -> " " % | Caret -> " " ^ | And -> " " && | Or -> " " || | Not -> " " ! | Lte -> " " <= | Gte -> " " >= | Neq -> " " != | Equals -> " " === | Lt -> " " < | Gt -> " " > | Cons -> " " :: ; ;
let string_of_type ( t : Ast . primitiveType ) = let rec aux ( t : primitiveType ) ( chr : int ) ( map : genericMap ) = match t with | TNum -> " num " , chr , map | TString -> " string " , chr , map | TBool -> " bool " , chr , map | TAny -> " any " , chr , map | TExn -> " exn " , chr , map | TUnit -> " unit " , chr , m...
let rec string_of_expr = function | Binop ( e1 , o , e2 ) -> String . concat " " [ string_of_expr e1 ; string_of_op o ; string_of_expr e2 ] | Unop ( o , e1 ) -> String . concat " " [ string_of_op o ; string_of_expr e1 ] | NumLit ( x ) -> string_of_float x | Throw ( e ) -> let s = string_of_expr e in " throw " ^ s | Try...
let rec string_of_aexpr ( ae : aexpr ) : string = match ae with | ABoolLit ( b , t ) -> Printf . sprintf " ( % s : % s ) " ( string_of_bool b ) ( string_of_type t ) | ANumLit ( x , t ) -> Printf . sprintf " ( % s : % s ) " ( string_of_float x ) ( string_of_type t ) | AStrLit ( s , t ) -> Printf . sprintf " ( % s : % s ...
module Make ( S : Stringlike_intf . S_base ) = struct include S let of_string s : t = match S . of_string_opt s with | Some s -> s | None -> Code_error . raise ( " Invalid " ^ S . module_ ^ " . t " ) [ ( " s " , Dyn . string s ) ] let error_message s = Printf . sprintf " % S is an invalid % s . " s S . description let ...
module type S_base = sig type t val module_ : string val description : string val description_of_valid_string : ' a Pp . t option val hint_valid : ( string -> string ) option val of_string_opt : string -> t option val to_string : t -> string end
module type S = sig type t val of_string : string -> t val to_string : t -> string val parse_string_exn : Loc . t * string -> t val to_dyn : t -> Dyn . t val of_string_opt : string -> t option val of_string_user_error : Loc . t * string -> ( t , User_message . t ) result val conv : ( string -> ( t , [ > ` Msg of string...
let rec tst01 s = match s with
let ( ) = assert ( tst01 " " = 0 ) ; assert ( tst01 " \ 000 \ 000 \ 000 \ 003 " = 1 ) ; assert ( tst01 " \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \ 007 " = 1 ) ; ( )
let tst02 s = let len = String . length s in match s with | " " when len < 0 -> assert false | " " -> 1 | _ when len = 0 -> assert false | " A " -> 2 | _ -> 3
let ( ) = assert ( tst02 " " = 1 ) ; assert ( tst02 " A " = 2 ) ; assert ( tst02 " B " = 3 ) ; assert ( tst02 " \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \ 007 " = 3 ) ; assert ( tst02 " \ 000 \ 000 \ 000 \ 003 " = 3 ) ; ( )
let tst03 s = match s with
let ( ) = assert ( tst03 s00 = 0 ) ; assert ( tst03 t00 = - 1 ) ; assert ( tst03 s01 = 1 ) ; assert ( tst03 t01 = - 1 ) ; assert ( tst03 s02 = 2 ) ; assert ( tst03 t02 = 2 ) ; assert ( tst03 s03 = 3 ) ; assert ( tst03 t03 = - 1 ) ; assert ( tst03 s04 = 4 ) ; assert ( tst03 t04 = - 1 ) ; assert ( tst03 s05 = 5 ) ; asser...
let tst04 s = match s with
let ( ) = assert ( tst04 s00 = 0 ) ; assert ( tst04 s01 = 1 ) ; assert ( tst04 s02 = 2 ) ; assert ( tst04 s03 = 3 ) ; assert ( tst04 s04 = 4 ) ; assert ( tst04 s05 = 5 ) ; assert ( tst04 s06 = 6 ) ; assert ( tst04 s07 = 7 ) ; assert ( tst04 s08 = 8 ) ; assert ( tst04 s09 = 9 ) ; assert ( tst04 s10 = 10 ) ; assert ( tst...
let tst05 s = match s with
let ( ) = assert ( tst05 s00 = 0 ) ; assert ( tst05 s01 = 1 ) ; assert ( tst05 s02 = 2 ) ; assert ( tst05 s03 = 3 ) ; assert ( tst05 s04 = 4 ) ; assert ( tst05 s05 = 5 ) ; assert ( tst05 s06 = 6 ) ; assert ( tst05 s07 = 7 ) ; assert ( tst05 s08 = 8 ) ; assert ( tst05 s09 = 9 ) ; assert ( tst05 s10 = 10 ) ; assert ( tst...
let tst06 s = match s with
let ( ) = assert ( tst06 s00 = 0 ) ; assert ( tst06 t00 = - 1 ) ; assert ( tst06 s01 = 1 ) ; assert ( tst06 t01 = - 1 ) ; assert ( tst06 s02 = 2 ) ; assert ( tst06 t02 = - 1 ) ; assert ( tst06 s03 = 3 ) ; assert ( tst06 t03 = - 1 ) ; assert ( tst06 s04 = 4 ) ; assert ( tst06 t04 = - 1 ) ; assert ( tst06 s05 = 5 ) ; ass...
let rec tst01 s = match s with
let ( ) = assert ( tst01 " " = 0 ) 0 ; assert ( tst01 " \ 000 \ 000 \ 000 \ 003 " = 1 ) 1 ; assert ( tst01 " \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \ 007 " = 1 ) 1 ; ( )
let tst02 s = let len = String . length s in match s with | " " when len < 0 -> assert false | " " -> 1 | _ when len = 0 -> assert false | " A " -> 2 | _ -> 3
let ( ) = assert ( tst02 " " = 1 ) 1 ; assert ( tst02 " A " = 2 ) 2 ; assert ( tst02 " B " = 3 ) 3 ; assert ( tst02 " \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \ 007 " = 3 ) 3 ; assert ( tst02 " \ 000 \ 000 \ 000 \ 003 " = 3 ) 3 ; ( )
let tst03 s = match s with
let ( ) = assert ( tst03 s00 = 0 ) 0 ; assert ( tst03 t00 = - 1 ) 1 ; assert ( tst03 s01 = 1 ) 1 ; assert ( tst03 t01 = - 1 ) 1 ; assert ( tst03 s02 = 2 ) 2 ; assert ( tst03 t02 = 2 ) 2 ; assert ( tst03 s03 = 3 ) 3 ; assert ( tst03 t03 = - 1 ) 1 ; assert ( tst03 s04 = 4 ) 4 ; assert ( tst03 t04 = - 1 ) 1 ; assert ( tst...
let tst04 s = match s with
let ( ) = assert ( tst04 s00 = 0 ) 0 ; assert ( tst04 s01 = 1 ) 1 ; assert ( tst04 s02 = 2 ) 2 ; assert ( tst04 s03 = 3 ) 3 ; assert ( tst04 s04 = 4 ) 4 ; assert ( tst04 s05 = 5 ) 5 ; assert ( tst04 s06 = 6 ) 6 ; assert ( tst04 s07 = 7 ) 7 ; assert ( tst04 s08 = 8 ) 8 ; assert ( tst04 s09 = 9 ) 9 ; assert ( tst04 s10 =...
let tst05 s = match s with
let ( ) = assert ( tst05 s00 = 0 ) 0 ; assert ( tst05 s01 = 1 ) 1 ; assert ( tst05 s02 = 2 ) 2 ; assert ( tst05 s03 = 3 ) 3 ; assert ( tst05 s04 = 4 ) 4 ; assert ( tst05 s05 = 5 ) 5 ; assert ( tst05 s06 = 6 ) 6 ; assert ( tst05 s07 = 7 ) 7 ; assert ( tst05 s08 = 8 ) 8 ; assert ( tst05 s09 = 9 ) 9 ; assert ( tst05 s10 =...
let tst06 s = match s with
let ( ) = assert ( tst06 s00 = 0 ) 0 ; assert ( tst06 t00 = - 1 ) 1 ; assert ( tst06 s01 = 1 ) 1 ; assert ( tst06 t01 = - 1 ) 1 ; assert ( tst06 s02 = 2 ) 2 ; assert ( tst06 t02 = - 1 ) 1 ; assert ( tst06 s03 = 3 ) 3 ; assert ( tst06 t03 = - 1 ) 1 ; assert ( tst06 s04 = 4 ) 4 ; assert ( tst06 t04 = - 1 ) 1 ; assert ( t...
module type Type = sig type text exception Prohibited of UChar . t exception Bad_bidi type profile = [ ` Nameprep | ` Nodeprep | ` Resourceprep | ` Saslprep | ` Trace | ` Iscsi | ` Mib ] val stringprep : profile -> text -> text end
module Make ( Config : ConfigInt . Type ) Type ( Text : UnicodeString . Type ) Type : Type with type text = Text . t = struct module UNF = UNF . Make ( Config ) ( Text ) module UCharInfo = UCharInfo . Make ( Config ) module StringPrep_data ' = StringPrep_data . Make ( Config ) open StringPrep_data ' type text = Text . ...
type mapping = | Diff of int | List of UChar . t list
module MappingHash = struct type t = mapping let hash = Hashtbl . hash let equal = ( ) = end