text stringlengths 0 601k |
|---|
let of_substring s i len = let len_s = String . length s in if i < 0 || len < 0 || i > len_s - len then invalid_arg " Rope . of_substring " ; if len >= len_s - ( len / 10 ) then Sub ( s , i , len ) else unsafe_of_substring s i len |
let of_char c = Sub ( String . make 1 c , 0 , 1 ) |
let rec make_of_gen gen ofs len ~ n = if n <= 1 then if len > 0 then Sub ( gen ofs len , 0 , len ) else empty else let nl = n / 2 in let ll = nl * max_flatten_length in let l = make_of_gen gen ofs ll ~ n : nl in let r = make_of_gen gen ( ofs + ll ) ( len - ll ) ~ n ( : n - nl ) in Concat ( 1 + max ( height l ) ( height... |
let make_length_mask = make_length - 1 |
let make_n_chunks len = if len land make_length_mask = 0 then len lsr make_length_pow2 else len lsr make_length_pow2 + 1 |
let make len c = if len < 0 then failwith " Rope . make : len must be >= 0 " ; if len <= make_length then Sub ( String . make len c , 0 , len ) else let base = String . make make_length c in make_of_gen ( fun _ _ -> base ) 0 len ~ n ( : make_n_chunks len ) |
let init len f = if len < 0 then failwith " Rope . init : len must be >= 0 " ; if len <= make_length then Sub ( String . init len f , 0 , len ) else let gen ofs len = let b = Bytes . create len in for i = 0 to len - 1 do Bytes . set b i ( f ( ofs + i ) ) done ; Bytes . unsafe_to_string b in make_of_gen gen 0 len ~ n ( ... |
let rec copy_to_subbytes t ofs = function | Sub ( s , i0 , len ) -> Bytes . blit_string s i0 t ofs len | Concat ( _ , _ , l , ll , r ) -> copy_to_subbytes t ofs l ; copy_to_subbytes t ( ofs + ll ) r |
let to_string = function | Sub ( s , i0 , len ) -> if i0 = 0 && len = String . length s then s else String . sub s i0 len | r -> let len = length r in if len > Sys . max_string_length then failwith " Rope . to_string : rope length > Sys . max_string_length " ; let t = Bytes . create len in copy_to_subbytes t 0 r ; Byte... |
let rec unsafe_blit src srcofs dst dstofs len = match src with | Sub ( s , i0 , _ ) -> String . blit s ( i0 + srcofs ) dst dstofs len | Concat ( _ , _ , l , ll , r ) -> let rofs = srcofs - ll in if rofs >= 0 then unsafe_blit r rofs dst dstofs len else let llen = - rofs in if len <= llen then unsafe_blit l srcofs dst ds... |
let blit src srcofs dst dstofs len = if len < 0 then failwith " Rope . blit : len >= 0 required " ; if srcofs < 0 || srcofs > length src - len then failwith " Rope . blit : not a valid range of src " ; if dstofs < 0 || dstofs > Bytes . length dst - len then failwith " Rope . blit : not a valid range of dst " ; unsafe_b... |
let flatten = function | Sub ( _ , _ , _ ) as r -> r | r -> let len = length r in assert ( len <= Sys . max_string_length ) ; let t = Bytes . create len in copy_to_subbytes t 0 r ; Sub ( Bytes . unsafe_to_string t , 0 , len ) |
let rec get rope i = match rope with | Sub ( s , i0 , len ) -> if i < 0 || i >= len then raise ( Out_of_bounds " Rope . get " ) else s . [ i0 + i ] | Concat ( _ , _ , l , left_len , r ) -> if i < left_len then get l i else get r ( i - left_len ) |
let rec iter f = function | Sub ( s , i0 , len ) -> for i = i0 to i0 + len - 1 do f s . [ i ] done | Concat ( _ , _ , l , _ , r ) -> iter f l ; iter f r |
let rec iteri_rec f init = function | Sub ( s , i0 , len ) -> let offset = init - i0 in for i = i0 to i0 + len - 1 do f ( i + offset ) s . [ i ] done | Concat ( _ , _ , l , ll , r ) -> iteri_rec f init l ; iteri_rec f ( init + ll ) r |
let iteri f r = ignore ( iteri_rec f 0 r ) |
let rec map ~ f = function | Sub ( s , i0 , len ) -> let b = Bytes . create len in for i = 0 to len - 1 do Bytes . set b i ( f ( String . unsafe_get s ( i0 + i ) ) ) done ; Sub ( Bytes . unsafe_to_string b , 0 , len ) | Concat ( h , len , l , ll , r ) -> let l = map ~ f l in let r = map ~ f r in Concat ( h , len , l , ... |
let rec mapi_rec ~ f idx0 = function | Sub ( s , i0 , len ) -> let b = Bytes . create len in for i = 0 to len - 1 do Bytes . set b i ( f ( idx0 + i ) s . [ i0 + i ] ) done ; Sub ( Bytes . unsafe_to_string b , 0 , len ) | Concat ( h , len , l , ll , r ) -> let l = mapi_rec ~ f idx0 l in let r = mapi_rec ~ f ( idx0 + ll ... |
let mapi ~ f r = mapi_rec ~ f 0 r |
let balance_concat rope1 rope2 = let len1 = length rope1 and len2 = length rope2 in if len1 = 0 then rope2 else if len2 = 0 then rope1 else let h = 1 + max ( height rope1 ) ( height rope2 ) in Concat ( h , len1 + len2 , rope1 , len1 , rope2 ) |
let add_nonempty_to_forest forest r = let len = length r in let n = ref 0 in let sum = ref empty in while len > min_length . ( ! n + 1 ) do if is_not_empty forest . ( ! n ) then ( sum := balance_concat forest . ( ! n ) ! sum ; forest . ( ! n ) <- empty ; ) ; if ! n = level_flatten then sum := flatten ! sum ; incr n don... |
let add_to_forest forest r = if is_not_empty r then add_nonempty_to_forest forest r |
let rec balance_insert forest rope = match rope with | Sub ( s , i0 , len ) -> if 25 * len <= String . length s then add_nonempty_to_forest forest ( Sub ( String . sub s i0 len , 0 , len ) ) else add_nonempty_to_forest forest rope | Concat ( h , len , l , _ , r ) -> if h >= max_height || len < min_length . ( h ) then (... |
let concat_forest forest = let concat ( n , sum ) r = let sum = balance_concat r sum in ( n + 1 , if n = level_flatten then flatten sum else sum ) in snd ( Array . fold_left concat ( 0 , empty ) forest ) |
let balance = function | Sub ( s , i0 , len ) as r -> if 0 < len && len <= extract_sub_length then Sub ( String . sub s i0 len , 0 , len ) else r | r -> let forest = Array . make max_height empty in balance_insert forest r ; concat_forest forest |
let balance_if_needed r = if height r >= rebalancing_height then balance r else r |
let rec relocate_topright rope leaf len_leaf = match rope with | Sub ( _ , _ , _ ) -> raise Relocation_failure | Concat ( h , len , l , ll , r ) -> let hr = height r + 1 in if hr < h then let lr = length r in Concat ( h , len + len_leaf , l , ll , Concat ( hr , lr + len_leaf , r , lr , leaf ) ) else Concat ( h , len + ... |
let rec relocate_topleft leaf len_leaf rope = match rope with | Sub ( _ , _ , _ ) -> raise Relocation_failure | Concat ( h , len , l , ll , r ) -> let hl = height l + 1 in if hl < h then let len_left = len_leaf + ll in let left = Concat ( hl , len_left , leaf , len_leaf , l ) in Concat ( h , len_leaf + len , left , len... |
let concat2_nonempty rope1 rope2 = match rope1 , rope2 with | Sub ( s1 , i1 , len1 ) , Sub ( s2 , i2 , len2 ) -> let len = len1 + len2 in if len <= small_rope_length then let s = Bytes . create len in Bytes . blit_string s1 i1 s 0 len1 ; Bytes . blit_string s2 i2 s len1 len2 ; Sub ( Bytes . unsafe_to_string s , 0 , len... |
let concat2 rope1 rope2 = let len1 = length rope1 and len2 = length rope2 in let len = len1 + len2 in if len1 = 0 then rope2 else if len2 = 0 then rope1 else begin if len < len1 then failwith " Rope . concat2 : the length of the resulting rope exceeds max_int " ; let h = 1 + max ( height rope1 ) ( height rope2 ) in if ... |
let rec sub_to_substring flat j i len = function | Sub ( s , i0 , _ ) -> Bytes . blit_string s ( i0 + i ) flat j len | Concat ( _ , _ , l , ll , r ) -> let ri = i - ll in if ri >= 0 then sub_to_substring flat j ri len r else let lenr = ri + len in if lenr <= 0 then sub_to_substring flat j i len l else ( sub_to_substrin... |
let flatten_subrope rope i len = assert ( len <= Sys . max_string_length ) ; let flat = Bytes . create len in sub_to_substring flat 0 i len rope ; Sub ( Bytes . unsafe_to_string flat , 0 , len ) ; ; |
let rec sub_rec i len = function | Sub ( s , i0 , lens ) -> assert ( i >= 0 && i <= lens - len ) ; Sub ( s , i0 + i , len ) | Concat ( _ , rope_len , l , ll , r ) -> let rl = rope_len - ll in let ri = i - ll in if ri >= 0 then if len = rl then r else sub_rec ri len r else let rlen = ri + len in if rlen <= 0 then if len... |
let sub rope i len = let len_rope = length rope in if i < 0 || len < 0 || i > len_rope - len then invalid_arg " Rope . sub " else if len = 0 then empty else if len <= max_flatten_length && len_rope >= 32768 then flatten_subrope rope i len else sub_rec i len rope |
let is_space = function | ' ' | ' \ 012 ' | ' \ n ' | ' \ r ' | ' \ t ' -> true | _ -> false |
let rec trim_left = function | Sub ( s , i0 , len ) -> let i = ref i0 in let i_max = i0 + len in while ! i < i_max && is_space ( String . unsafe_get s ! i ) do incr i done ; if ! i = i_max then empty else Sub ( s , ! i , i_max - ! i ) | Concat ( _ , _ , l , _ , r ) -> let l = trim_left l in if is_empty l then trim_left... |
let rec trim_right = function | Sub ( s , i0 , len ) -> let i = ref ( i0 + len - 1 ) in while ! i >= i0 && is_space ( String . unsafe_get s ! i ) do decr i done ; if ! i < i0 then empty else Sub ( s , i0 , ! i - i0 + 1 ) | Concat ( _ , _ , l , ll , r ) -> let r = trim_right r in if is_empty r then trim_right l else let... |
let trim r = trim_right ( trim_left r ) |
let escaped_sub s i0 len = let n = ref 0 in let i1 = i0 + len - 1 in for i = i0 to i1 do n := ! n + ( match String . unsafe_get s i with | ' " ' \ | ' ' \\ | ' \ n ' | ' \ t ' | ' \ r ' | ' \ b ' -> 2 | ' ' . . ' ' ~ -> 1 | _ -> 4 ) done ; if ! n = len then Sub ( s , i0 , len ) else ( let s ' = Bytes . create ! n in n ... |
let rec escaped = function | Sub ( s , i0 , len ) -> escaped_sub s i0 len | Concat ( h , _ , l , _ , r ) -> let l = escaped l in let ll = length l in let r = escaped r in Concat ( h , ll + length r , l , ll , r ) |
let rec index_string offset s i i1 c = if i >= i1 then - 1 else if s . [ i ] = c then offset + i else index_string offset s ( i + 1 ) i1 c ; ; |
let rec unsafe_index offset i c = function | Sub ( s , i0 , len ) -> index_string ( offset - i0 ) s ( i0 + i ) ( i0 + len ) c | Concat ( _ , _ , l , ll , r ) -> if i >= ll then unsafe_index ( offset + ll ) ( i - ll ) c r else let li = unsafe_index offset i c l in if li >= 0 then li else unsafe_index ( offset + ll ) 0 c... |
let index_from r i c = if i < 0 || i >= length r then invalid_arg " Rope . index_from " else let j = unsafe_index 0 i c r in if j >= 0 then j else raise Not_found |
let index_from_opt r i c = if i < 0 || i >= length r then invalid_arg " Rope . index_from_opt " ; let j = unsafe_index 0 i c r in if j >= 0 then Some j else None |
let index r c = let j = unsafe_index 0 0 c r in if j >= 0 then j else raise Not_found |
let index_opt r c = let j = unsafe_index 0 0 c r in if j >= 0 then Some j else None |
let contains_from r i c = if i < 0 || i >= length r then invalid_arg " Rope . contains_from " else unsafe_index 0 i c r >= 0 |
let contains r c = unsafe_index 0 0 c r >= 0 |
let rec rindex_string offset s i0 i c = if i < i0 then - 1 else if s . [ i ] = c then offset + i else rindex_string offset s i0 ( i - 1 ) c |
let rec unsafe_rindex offset i c = function | Sub ( s , i0 , _ ) -> rindex_string ( offset - i0 ) s i0 ( i0 + i ) c | Concat ( _ , _ , l , ll , r ) -> if i < ll then unsafe_rindex offset i c l else let ri = unsafe_rindex ( offset + ll ) ( i - ll ) c r in if ri >= 0 then ri else unsafe_rindex offset ( ll - 1 ) c l |
let rindex_from r i c = if i < 0 || i > length r then invalid_arg " Rope . rindex_from " else let j = unsafe_rindex 0 i c r in if j >= 0 then j else raise Not_found |
let rindex_from_opt r i c = if i < 0 || i > length r then invalid_arg " Rope . rindex_from_opt " ; let j = unsafe_rindex 0 i c r in if j >= 0 then Some j else None |
let rindex r c = let j = unsafe_rindex 0 ( length r - 1 ) c r in if j >= 0 then j else raise Not_found |
let rindex_opt r c = let j = unsafe_rindex 0 ( length r - 1 ) c r in if j >= 0 then Some j else None |
let rcontains_from r i c = if i < 0 || i >= length r then invalid_arg " Rope . rcontains_from " else unsafe_rindex 0 i c r >= 0 |
let lowercase_ascii r = map ~ f : Char . lowercase_ascii r |
let uppercase_ascii r = map ~ f : Char . uppercase_ascii r |
let rec map1 f = function | Concat ( h , len , l , ll , r ) -> Concat ( h , len , map1 f l , ll , r ) | Sub ( s , i0 , len ) -> if len = 0 then empty else begin let s ' = Bytes . create len in Bytes . set s ' 0 ( f ( String . unsafe_get s i0 ) ) ; Bytes . blit_string s ( i0 + 1 ) s ' 1 ( len - 1 ) ; Sub ( Bytes . unsaf... |
let capitalize_ascii r = map1 Char . uppercase_ascii r |
let uncapitalize_ascii r = map1 Char . lowercase_ascii r |
module Iterator = struct type t = { rope : rope ; len : int ; mutable i : int ; mutable path : ( rope * int ) list ; mutable current : string ; mutable current_g0 : int ; mutable current_g1 : int ; mutable current_offset : int ; } let rec set_current_for_index_rec itr g0 i = function | Sub ( s , i0 , len ) -> assert ( ... |
let compare r1 r2 = let len1 = length r1 and len2 = length r2 in let i1 = Iterator . make r1 0 and i2 = Iterator . make r2 0 in try for _i = 1 to min len1 len2 do let c1 = Iterator . get i1 and c2 = Iterator . get i2 in if c1 < c2 then raise Less ; if c1 > c2 then raise Greater ; Iterator . incr i1 ; Iterator . incr i2... |
let equal r1 r2 = let len1 = length r1 and len2 = length r2 in if len1 <> len2 then false else ( let i1 = Iterator . make r1 0 and i2 = Iterator . make r2 0 in try for _i = 1 to len1 do if Iterator . get i1 <> Iterator . get i2 then raise Exit ; Iterator . incr i1 ; Iterator . incr i2 ; done ; true with Exit -> false ) |
let init_next p = let m = String . length p in let next = Array . make m 0 in let i = ref 1 and j = ref 0 in while ! i < m - 1 do if p . [ ! i ] = p . [ ! j ] then begin incr i ; incr j ; next . ( ! i ) <- ! j end else if ! j = 0 then begin incr i ; next . ( ! i ) <- 0 end else j := next . ( ! j ) done ; next |
let search_forward_string p = if String . length p > Sys . max_array_length then failwith " Rope . search_forward : string to search too long " ; let next = init_next p and m = String . length p in fun rope i0 -> let i = Iterator . make rope i0 and j = ref 0 in ( try while ! j < m do if p . [ ! j ] = Iterator . get i t... |
module Buffer = struct type t = { mutable buf : Bytes . t ; buf_len : int ; mutable pos : int ; mutable length : int ; forest : rope array ; } let create n = let n = if n < 1 then small_rope_length else if n > Sys . max_string_length then Sys . max_string_length else n in { buf = Bytes . create n ; buf_len = n ; pos = ... |
let concat sep = function | [ ] -> empty | r0 :: tl -> let b = Buffer . create 1 in Buffer . add_rope b r0 ; List . iter ( fun r -> Buffer . add_rope b sep ; Buffer . add_rope b r ) tl ; Buffer . contents b |
let input_line ( ? leaf_length = 128 ) chan = let b = Buffer . create leaf_length in let rec scan ( ) = let n = input_scan_line chan in if n = 0 then if Buffer . length b = 0 then raise End_of_file else Buffer . contents b else if n > 0 then ( Buffer . add_channel b chan ( n - 1 ) ; ignore ( input_char chan ) ; Buffer ... |
let read_line ( ) = flush stdout ; input_line stdin |
let rec output_string fh = function | Sub ( s , i0 , len ) -> output fh ( Bytes . unsafe_of_string s ) i0 len | Concat ( _ , _ , l , _ , r ) -> output_string fh l ; output_string fh r ; ; |
let print_string rope = output_string stdout rope |
let print_endline rope = output_string stdout rope ; print_newline ( ) |
let prerr_string rope = output_string stderr rope |
let prerr_endline rope = output_string stderr rope ; prerr_newline ( ) |
let rec number_leaves = function | Sub ( _ , _ , _ ) -> 1 | Concat ( _ , _ , l , _ , r ) -> number_leaves l + number_leaves r |
let rec number_concat = function | Sub ( _ , _ , _ ) -> 0 | Concat ( _ , _ , l , _ , r ) -> 1 + number_concat l + number_concat r |
let rec length_leaves = function | Sub ( _ , _ , len ) -> ( len , len ) | Concat ( _ , _ , l , _ , r ) -> let ( min1 , max1 ) = length_leaves l and ( min2 , max2 ) = length_leaves r in ( min min1 min2 , max max1 max2 ) |
module IMap = Map . Make ( struct type t = int let compare = Pervasives . compare end ) |
let distrib_leaves = let rec add_leaves m = function | Sub ( _ , _ , len ) -> ( try incr ( IMap . find len ! m ) with _ -> m := IMap . add len ( ref 1 ) ! m ) | Concat ( _ , _ , l , _ , r ) -> add_leaves m l ; add_leaves m r in fun r -> let m = ref ( IMap . empty ) in add_leaves m r ; ! m |
module Rope_toploop = struct open Format let max_display_length = ref 400 let ellipsis = ref " . . . " let rec printer_lim max_len ( fm : formatter ) r = if max_len > 0 then match r with | Concat ( _ , _ , l , _ , r ) -> let to_be_printed = printer_lim max_len fm l in printer_lim to_be_printed fm r | Sub ( s , i0 , len... |
module Regexp = struct end ; ; |
let router ~ graphql_uri ~ pool ~ logger route body = let open Deferred . Result . Let_syntax in let get_graphql_uri_or_error ( ) = match graphql_uri with | None -> Deferred . Result . fail ( ` App ( Errors . create ` Graphql_uri_not_set ) Graphql_uri_not_set ) Graphql_uri_not_set | Some graphql_uri -> Deferred . Resul... |
let pg_log_data ~ logger ~ pool : unit Deferred . t = match % bind Lazy . force pool with | Ok pool -> let get_logs ( ) : ( unit , _ ) _ Deferred . Result . t = Caqti_async . Pool . use ( fun db -> let open Deferred . Result . Let_syntax in let % bind num_conns = Pg_data . run_connection_count db ( ) in let % map num_l... |
let server_handler ~ pool ~ graphql_uri ~ logger ~ body _sock req = let uri = Cohttp_async . Request . uri req in let % bind body = Cohttp_async . Body . to_string body in let route = List . tl_exn ( String . split ~ on : ' / ' ( Uri . path uri ) uri ) uri in let % bind result = match Yojson . Safe . from_string body w... |
let command = let open Command . Let_syntax in let % map_open archive_uri = flag " -- archive - uri " ~ aliases [ : " archive - uri " ] ~ doc " : Postgres connection string URI corresponding to archive node " Cli . optional_uri and graphql_uri = flag " -- graphql - uri " ~ aliases [ : " graphql - uri " ] ~ doc " : URI ... |
module List_utils = struct let rec remove p xs = match xs with | [ ] -> [ ] | y :: ys -> if ( p y ) then ys else y :: ( remove p ys ) let rec replace p x xs = match xs with | [ ] -> raise Not_found | y :: ys -> if ( p y ) then x :: ys else y :: ( replace p x ys ) let rec insert_before p x xs = match xs with | [ ] -> ra... |
let take_section get_level hs = let rec aux hs section level = match hs with | [ ] -> section , [ ] | h :: hs -> if ( get_level h ) > level then aux hs ( h :: section ) level else section , ( h :: hs ) in match hs with | [ ] -> failwith " Cannot take any section from an empty list of headings " | [ h ] -> ( h , [ ] ) ,... |
module Path_tree = struct type ( ' a , ' b ) path_tree = { id : ' a ; data : ' b ; children : ( ' a , ' b ) path_tree list } exception Empty_path exception Duplicate_child exception Insert_error of string let make id data = { id = id ; data = data ; children = [ ] } let make_full id data children = { id = id ; data = d... |
type ' a tree = { value : ' a ; children : ( ' a tree ) list } |
let number_elements xs = let rec aux xs num acc = match xs with | [ ] -> List . rev acc | x :: xs -> aux xs ( num + 1 ) ( ( num , x ) :: acc ) in aux xs 1 [ ] |
let break_into_sections get_level hs = let rec aux hs acc = match hs with | [ ] -> acc | _ -> let section , remainder = take_section get_level hs in aux remainder ( section :: acc ) in List . rev @@ aux hs [ ] |
let rec from_path_tree t = let data = Path_tree . data_of_node t in match ( Path_tree . children_of_node t ) with | [ ] -> { value = data ; children [ ] } = | cs -> { value = data ; children ( = List . map from_path_tree cs ) } |
let from_list get_level hs = let get_level ( _ , x ) = get_level x in let sections = number_elements hs |> break_into_sections get_level in List . map ( fun ( ( id , h ) , cs ) -> Path_tree . from_list get_level ( Path_tree . make id h ) cs ) sections |> List . map from_path_tree |
module type Stringable = sig type t val to_string : t -> string end [ %% expect { | { ( " Stringable " , module type ) -> . < 2 ; > } |
module type Stringable = sig type t val to_string : t -> string end } ] | |
module Pair ( X : Stringable ) ( Y : Stringable ) = struct type t = X . t * Y . t let to_string ( x , y ) = X . to_string x ^ " " ^ Y . to_string y end [ %% expect { | { ( " Pair " , module ) -> Abs . < 9 > ( X / 284 , Abs ( Y / 285 , { ( " t " , type ) -> . < 5 ; > ( " to_string " , value ) -> . < 6 ; > } ) ) ; } |
module Pair : functor ( X : Stringable ) ( Y : Stringable ) -> sig type t = X . t * Y . t val to_string : X . t * Y . t -> string end } ] | |
module Int = struct type t = int let to_string i = string_of_int i end [ %% expect { | { ( " Int " , module ) -> { . < 13 > ( " t " , type ) -> . < 10 ; > ( " to_string " , value ) -> . < 11 ; > } ; } |
module Int : sig type t = int val to_string : int -> string end } ] | |
module String = struct type t = string let to_string s = s end [ %% expect { | { ( " String " , module ) -> { . < 17 > ( " t " , type ) -> . < 14 ; > ( " to_string " , value ) -> . < 15 ; > } ; } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.