text
stringlengths
0
601k
let of_byte b = if b < 0 || b > 255 then invalid_arg ( err_byte b ) else unsafe_of_byte b
let of_int b = if b < 0 || b > 255 then None else ( Some ( unsafe_of_byte b ) )
let hash c = Hashtbl . hash c
let equal : t -> t -> bool = fun c0 c1 -> c0 = c1
let compare : t -> t -> int = fun c0 c1 -> compare c0 c1
module Ascii = struct let max_ascii = ' \ x7F ' let is_valid : t -> bool = fun c -> c <= max_ascii let is_digit = function ' 0 ' . . ' 9 ' -> true | _ -> false let is_hex_digit = function | ' 0 ' . . ' 9 ' | ' A ' . . ' F ' | ' a ' . . ' f ' -> true | _ -> false let is_upper = function ' A ' . . ' Z ' -> true | _ -> fa...
let dump ppf c = Format . pp_print_char ppf ' ' ' ; \ Format . pp_print_string ppf ( Ascii . escape_char c ) ; Format . pp_print_char ppf ' ' ' ; \ ( )
let hex_escape b k c = let byte = char_to_byte c in let hi = byte / 16 in let lo = byte mod 16 in bytes_unsafe_set b ( k ) ' ' ; \\ bytes_unsafe_set b ( k + 1 ) ' x ' ; bytes_unsafe_set b ( k + 2 ) ( array_unsafe_get hex_digit hi ) ; bytes_unsafe_set b ( k + 3 ) ( array_unsafe_get hex_digit lo ) ; ( )
let letter_escape b k letter = bytes_unsafe_set b ( k ) ' ' ; \\ bytes_unsafe_set b ( k + 1 ) letter ; ( )
let char_escape = function let b = Bytes . create 1 in bytes_unsafe_set b 0 c ; bytes_unsafe_to_string b let b = Bytes . create 4 in hex_escape b 0 c ; bytes_unsafe_to_string b
let char_escape_char = function let b = Bytes . create 1 in bytes_unsafe_set b 0 c ; bytes_unsafe_to_string b let b = Bytes . create 4 in hex_escape b 0 c ; bytes_unsafe_to_string b
let escape s = let max_idx = string_length s - 1 in let rec escaped_len i l = if i > max_idx then l else match string_unsafe_get s i with | ' ' \\ -> escaped_len ( i + 1 ) ( l + 2 ) | ' \ x20 ' . . ' \ x7E ' -> escaped_len ( i + 1 ) ( l + 1 ) | _ -> escaped_len ( i + 1 ) ( l + 4 ) in let escaped_len = escaped_len 0 0 i...
let escape_string s = let max_idx = string_length s - 1 in let rec escaped_len i l = if i > max_idx then l else match string_unsafe_get s i with | ' \ b ' | ' \ t ' | ' \ n ' | ' \ r ' | ' " ' \ | ' ' \\ -> escaped_len ( i + 1 ) ( l + 2 ) | ' \ x20 ' . . ' \ x7E ' -> escaped_len ( i + 1 ) ( l + 1 ) | _ -> escaped_len (...
let unescaped_len ~ ocaml s = let max_idx = string_length s - 1 in let rec loop i l = if i > max_idx then Some l else if string_unsafe_get s i <> ' ' \\ then loop ( i + 1 ) ( l + 1 ) else let i = i + 1 in if i > max_idx then None else match string_unsafe_get s i with | ' ' \\ -> loop ( i + 1 ) ( l + 1 ) | ' x ' -> let ...
let _unescape ~ ocaml s = match unescaped_len ~ ocaml s with let b = Bytes . create l in let max_idx = string_length s - 1 in let rec loop i k = if i > max_idx then Some ( bytes_unsafe_to_string b ) else let c = string_unsafe_get s i in if c <> ' ' \\ then ( bytes_unsafe_set b k c ; loop ( i + 1 ) ( k + 1 ) ) else let ...
let unescape s = _unescape ~ ocaml : false s
let unescape_string s = _unescape ~ ocaml : true s
let v ~ len f = let b = Bytes . create len in for i = 0 to len - 1 do bytes_unsafe_set b i ( f i ) done ; bytes_unsafe_to_string b
let get_byte s i = char_to_byte ( get s i )
let unsafe_get_byte s i = char_to_byte ( unsafe_get s i )
let head ( ? rev = false ) s = let len = length s in if len = 0 then None else Some ( string_unsafe_get s ( if rev then len - 1 else 0 ) )
let get_head ( ? rev = false ) s = let len = length s in if len = 0 then invalid_arg Astring_base . err_empty_string else string_unsafe_get s ( if rev then len - 1 else 0 )
let hash c = Hashtbl . hash c
let append s0 s1 = let l0 = length s0 in if l0 = 0 then s1 else let l1 = length s1 in if l1 = 0 then s0 else let b = Bytes . create ( l0 + l1 ) in bytes_unsafe_blit_string s0 0 b 0 l0 ; bytes_unsafe_blit_string s1 0 b l0 l1 ; bytes_unsafe_to_string b
let concat ( ? sep = empty ) = function let s_len = length s in let sep_len = length sep in let rec cat_len sep_count l ss = if l < 0 then l else match ss with | s :: ss -> cat_len ( sep_count + 1 ) ( l + length s ) ss | [ ] -> if sep_len = 0 then l else let max_sep_count = Sys . max_string_length / sep_len in if sep_c...
let is_empty s = length s = 0
let is_prefix ~ affix s = let len_a = length affix in let len_s = length s in if len_a > len_s then false else let max_idx_a = len_a - 1 in let rec loop i = if i > max_idx_a then true else if unsafe_get affix i <> unsafe_get s i then false else loop ( i + 1 ) in loop 0
let is_infix ~ affix s = let len_a = length affix in let len_s = length s in if len_a > len_s then false else let max_idx_a = len_a - 1 in let max_idx_s = len_s - len_a in let rec loop i k = if i > max_idx_s then false else if k > max_idx_a then true else if k > 0 then if unsafe_get affix k = unsafe_get s ( i + k ) the...
let is_suffix ~ affix s = let max_idx_a = length affix - 1 in let max_idx_s = length s - 1 in if max_idx_a > max_idx_s then false else let rec loop i = if i > max_idx_a then true else if unsafe_get affix ( max_idx_a - i ) <> unsafe_get s ( max_idx_s - i ) then false else loop ( i + 1 ) in loop 0
let for_all sat s = Astring_base . for_all sat s ~ first : 0 ~ last ( : length s - 1 )
let exists sat s = Astring_base . exists sat s ~ first : 0 ~ last ( : length s - 1 )
let with_range ( ? first = 0 ) ( ? len = max_int ) s = if len < 0 then invalid_arg ( Astring_base . err_neg_len len ) else if len = 0 then empty else let s_len = length s in let max_idx = s_len - 1 in let last = match len with | len when len = max_int -> max_idx | len -> let last = first + len - 1 in if last > max_idx ...
let with_index_range ( ? first = 0 ) ? last s = let s_len = length s in let max_idx = s_len - 1 in let last = match last with | None -> max_idx | Some last -> if last > max_idx then max_idx else last in let first = if first < 0 then 0 else first in if first > max_idx || last < 0 || first > last then empty else if first...
let trim ( ? drop = Astring_char . Ascii . is_white ) s = let len = length s in if len = 0 then s else let max_idx = len - 1 in let rec left_pos i = if i > max_idx then len else if drop ( unsafe_get s i ) then left_pos ( i + 1 ) else i in let rec right_pos i = if i < 0 then 0 else if drop ( unsafe_get s i ) then right_...
let fspan ( ? min = 0 ) ( ? max = max_int ) ( ? sat = fun _ -> true ) s = if min < 0 then invalid_arg ( Astring_base . err_neg_min min ) else if max < 0 then invalid_arg ( Astring_base . err_neg_max max ) else if min > max || max = 0 then ( empty , s ) else let len = length s in let max_idx = len - 1 in let max_idx = l...
let rspan ( ? min = 0 ) ( ? max = max_int ) ( ? sat = fun _ -> true ) s = if min < 0 then invalid_arg ( Astring_base . err_neg_min min ) else if max < 0 then invalid_arg ( Astring_base . err_neg_max max ) else if min > max || max = 0 then ( s , empty ) else let len = length s in let max_idx = len - 1 in let min_idx = l...
let span ( ? rev = false ) ? min ? max ? sat s = match rev with
let ftake ( ? min = 0 ) ( ? max = max_int ) ( ? sat = fun _ -> true ) s = if min < 0 then invalid_arg ( Astring_base . err_neg_min min ) else if max < 0 then invalid_arg ( Astring_base . err_neg_max max ) else if min > max || max = 0 then empty else let len = length s in let max_idx = len - 1 in let max_idx = let k = m...
let rtake ( ? min = 0 ) ( ? max = max_int ) ( ? sat = fun _ -> true ) s = if min < 0 then invalid_arg ( Astring_base . err_neg_min min ) else if max < 0 then invalid_arg ( Astring_base . err_neg_max max ) else if min > max || max = 0 then empty else let len = length s in let max_idx = len - 1 in let min_idx = let k = l...
let take ( ? rev = false ) ? min ? max ? sat s = match rev with
let fdrop ( ? min = 0 ) ( ? max = max_int ) ( ? sat = fun _ -> true ) s = if min < 0 then invalid_arg ( Astring_base . err_neg_min min ) else if max < 0 then invalid_arg ( Astring_base . err_neg_max max ) else if min > max || max = 0 then s else let len = length s in let max_idx = len - 1 in let max_idx = let k = max -...
let rdrop ( ? min = 0 ) ( ? max = max_int ) ( ? sat = fun _ -> true ) s = if min < 0 then invalid_arg ( Astring_base . err_neg_min min ) else if max < 0 then invalid_arg ( Astring_base . err_neg_max max ) else if min > max || max = 0 then s else let len = length s in let max_idx = len - 1 in let min_idx = let k = len -...
let drop ( ? rev = false ) ? min ? max ? sat s = match rev with
let fcut ~ sep s = let sep_len = length sep in if sep_len = 0 then invalid_arg Astring_base . err_empty_sep else let s_len = length s in let max_sep_idx = sep_len - 1 in let max_s_idx = s_len - sep_len in let rec check_sep i k = if k > max_sep_idx then let r_start = i + sep_len in Some ( unsafe_string_sub s 0 i , unsaf...
let rcut ~ sep s = let sep_len = length sep in if sep_len = 0 then invalid_arg Astring_base . err_empty_sep else let s_len = length s in let max_sep_idx = sep_len - 1 in let max_s_idx = s_len - 1 in let rec check_sep i k = if k > max_sep_idx then let r_start = i + sep_len in Some ( unsafe_string_sub s 0 i , unsafe_stri...
let cut ( ? rev = false ) ~ sep s = if rev then rcut ~ sep s else fcut ~ sep s
let add_sub ~ no_empty s ~ start ~ stop acc = if start = stop then ( if no_empty then acc else empty :: acc ) else unsafe_string_sub s start ( stop - start ) :: acc
let fcuts ~ no_empty ~ sep s = let sep_len = length sep in if sep_len = 0 then invalid_arg Astring_base . err_empty_sep else let s_len = length s in let max_sep_idx = sep_len - 1 in let max_s_idx = s_len - sep_len in let rec check_sep start i k acc = if k > max_sep_idx then let new_start = i + sep_len in scan new_start...
let rcuts ~ no_empty ~ sep s = let sep_len = length sep in if sep_len = 0 then invalid_arg Astring_base . err_empty_sep else let s_len = length s in let max_sep_idx = sep_len - 1 in let max_s_idx = s_len - 1 in let rec check_sep stop i k acc = if k > max_sep_idx then let start = i + sep_len in rscan i ( i - sep_len ) (...
let cuts ( ? rev = false ) ( ? empty = true ) ~ sep s = match rev with
let fields ( ? empty = true ) ( ? is_sep = Astring_char . Ascii . is_white ) s = let no_empty = not empty in let max_pos = length s in let rec loop i end_pos acc = if i < 0 then begin if end_pos = max_pos then ( if no_empty && max_pos = 0 then [ ] else [ s ] ) else add_sub ~ no_empty s ~ start : 0 ~ stop : end_pos acc ...
let ffind ? start sat s = let max_idx = length s - 1 in let rec loop i = if i > max_idx then None else if sat ( unsafe_get s i ) then Some i else loop ( i + 1 ) in match start with | None -> loop 0 | Some i when i < 0 -> loop 0 | Some i -> loop i
let rfind ? start sat s = let max_idx = length s - 1 in let rec loop i = if i < 0 then None else if sat ( unsafe_get s i ) then Some i else loop ( i - 1 ) in match start with | None -> loop max_idx | Some i when i > max_idx -> loop max_idx | Some i -> loop i
let find ( ? rev = false ) ? start sat s = match rev with
let ffind_sub ? start ~ sub s = let len_sub = length sub in let len_s = length s in let max_idx_sub = len_sub - 1 in let max_idx_s = if len_sub <> 0 then len_s - len_sub else len_s - 1 in let rec loop i k = if i > max_idx_s then None else if k > max_idx_sub then Some i else if k > 0 then if unsafe_get sub k = unsafe_ge...
let rfind_sub ? start ~ sub s = let len_sub = length sub in let len_s = length s in let max_idx_sub = len_sub - 1 in let max_idx_s = if len_sub <> 0 then len_s - len_sub else len_s - 1 in let rec loop i k = if i < 0 then None else if k > max_idx_sub then Some i else if k > 0 then if unsafe_get sub k = unsafe_get s ( i ...
let find_sub ( ? rev = false ) ? start ~ sub s = match rev with
let filter sat s = let max_idx = length s - 1 in let rec with_buf b k i = if i > max_idx then Bytes . sub_string b 0 k else let c = unsafe_get s i in if sat c then ( bytes_unsafe_set b k c ; with_buf b ( k + 1 ) ( i + 1 ) ) else with_buf b k ( i + 1 ) in let rec try_no_alloc i = if i > max_idx then s else if ( sat ( un...
let filter_map f s = let max_idx = length s - 1 in let rec with_buf b k i = if i > max_idx then ( if k > max_idx then bytes_unsafe_to_string b else Bytes . sub_string b 0 k ) else match f ( unsafe_get s i ) with | None -> with_buf b k ( i + 1 ) | Some c -> bytes_unsafe_set b k c ; with_buf b ( k + 1 ) ( i + 1 ) in let ...
let map f s = let max_idx = length s - 1 in let rec with_buf b i = if i > max_idx then bytes_unsafe_to_string b else ( bytes_unsafe_set b i ( f ( unsafe_get s i ) ) ; with_buf b ( i + 1 ) ) in let rec try_no_alloc i = if i > max_idx then s else let c = unsafe_get s i in match f c with | cm when cm <> c -> let b = Bytes...
let mapi f s = let max_idx = length s - 1 in let rec with_buf b i = if i > max_idx then bytes_unsafe_to_string b else ( bytes_unsafe_set b i ( f i ( unsafe_get s i ) ) ; with_buf b ( i + 1 ) ) in let rec try_no_alloc i = if i > max_idx then s else let c = unsafe_get s i in match f i c with | cm when cm <> c -> let b = ...
let fold_left f acc s = Astring_base . fold_left f acc s ~ first : 0 ~ last ( : length s - 1 )
let fold_right f s acc = Astring_base . fold_right f s acc ~ first : 0 ~ last ( : length s - 1 )
let iter f s = for i = 0 to length s - 1 do f ( unsafe_get s i ) done
let iteri f s = for i = 0 to length s - 1 do f i ( unsafe_get s i ) done
module Ascii = struct let is_valid s = let max_idx = length s - 1 in let rec loop i = if i > max_idx then true else if unsafe_get s i > Astring_char . Ascii . max_ascii then false else loop ( i + 1 ) in loop 0 let caseify is_not_case to_case s = let max_idx = length s - 1 in let caseify b i = for k = i to max_idx do by...
let dump ppf s = Format . pp_print_char ppf ' " ' ; Format . pp_print_string ppf ( Ascii . escape_string s ) ; Format . pp_print_char ppf ' " ' ; ( )
module Set = struct include Set . Make ( String ) let pp ? sep ( : pp_sep = Format . pp_print_cut ) pp_elt ppf ss = let pp_elt elt is_first = if is_first then ( ) else pp_sep ppf ( ) ; pp_elt ppf elt ; false in ignore ( fold pp_elt ss true ) let dump_str = dump let dump ppf ss = let pp_elt elt is_first = if is_first th...
module Map = struct include Map . Make ( String ) let err_empty ( ) = invalid_arg " empty map " let err_absent s = invalid_arg ( strf " % a is not bound in map " dump s ) let get_min_binding m = try min_binding m with Not_found -> err_empty ( ) let min_binding m = try Some ( min_binding m ) with Not_found -> None let g...
type ' a map = ' a Map . t
let uniquify ss = let add ( seen , ss as acc ) v = if Set . mem v seen then acc else ( Set . add v seen , v :: ss ) in List . rev ( snd ( List . fold_left add ( Set . empty , [ ] ) ss ) )
let err_base = " not on the same base string "
let err_empty_sub pos = strf " empty substring [ % d ; % d ] " pos pos
let err_pos_range start stop len = strf " invalid start :% d stop :% d for position range [ 0 ; % d ] " start stop len
let v ( ? start = 0 ) ? stop s = let s_len = string_length s in let stop = match stop with None -> s_len | Some stop -> stop in if start < 0 || stop > s_len || stop < start then invalid_arg ( err_pos_range start stop s_len ) else ( s , start , stop )
let of_string_with_range ( ? first = 0 ) ( ? len = max_int ) s = if len < 0 then invalid_arg ( Astring_base . err_neg_len len ) else let s_len = string_length s in let max_idx = s_len - 1 in let empty = function | first when first < 0 -> ( s , 0 , 0 ) | first when first > max_idx -> ( s , s_len , s_len ) | first -> ( s...
let of_string_with_index_range ( ? first = 0 ) ? last s = let s_len = string_length s in let max_idx = s_len - 1 in let empty = function | first when first < 0 -> ( s , 0 , 0 ) | first when first > max_idx -> ( s , s_len , s_len ) | first -> ( s , first , first ) in let last = match last with | None -> max_idx | Some l...
type t = string * int * int
let empty = ( Astring_base . empty , 0 , 0 )
let start_pos ( _ , start , _ ) = start
let stop_pos ( _ , _ , stop ) = stop
let base_string ( s , _ , _ ) = s
let length ( _ , start , stop ) = stop - start
let get ( s , start , _ ) i = string_safe_get s ( start + i )
let get_byte s i = char_to_byte ( get s i )
let unsafe_get ( s , start , _ ) i = string_unsafe_get s ( start + i )
let unsafe_get_byte s i = char_to_byte ( unsafe_get s i )
let head ( ? rev = false ) ( s , start , stop ) = if start = stop then None else Some ( string_unsafe_get s ( if rev then stop - 1 else start ) )
let get_head ( ? rev = false ) ( s , start , stop ) = if start = stop then invalid_arg ( err_empty_sub start ) else string_unsafe_get s ( if rev then stop - 1 else start )
let of_string s = v s
let to_string ( s , start , stop ) = if start = stop then Astring_base . empty else if start = 0 && stop = string_length s then s else unsafe_string_sub s start ( stop - start )
let rebase ( _ , start , stop as sub ) = ( to_string sub , 0 , stop - start )
let hash s = Hashtbl . hash s
let start ( s , start , _ ) = ( s , start , start )
let stop ( s , _ , stop ) = ( s , stop , stop )
let base ( s , _ , _ ) = ( s , 0 , string_length s )
let tail ( ? rev = false ) ( s , start , stop as sub ) = if start = stop then sub else if rev then ( s , start , stop - 1 ) else ( s , start + 1 , stop )
let fextend ? max ~ sat ( s , start , stop ) = let max_idx = string_length s - 1 in let max_idx = match max with | None -> max_idx | Some max when max < 0 -> invalid_arg ( Astring_base . err_neg_max max ) | Some max -> let i = stop + max - 1 in if i > max_idx then max_idx else i in let rec loop i = if i > max_idx then ...
let rextend ? max ~ sat ( s , start , stop ) = let min_idx = match max with | None -> 0 | Some max when max < 0 -> invalid_arg ( Astring_base . err_neg_max max ) | Some max -> let i = start - max in if i < 0 then 0 else i in let rec loop i = if i < min_idx then ( s , min_idx , stop ) else if sat ( string_unsafe_get s i...
let extend ( ? rev = false ) ? max ( ? sat = ( fun _ -> true ) ) sub = match rev with