text stringlengths 0 601k |
|---|
let of_string s = let buf = Buffer . create ( ) in Buffer . add_string buf s ; Buffer . contents buf |
let rec to_string t = match t with | Leaf ( s , _ ) _ -> s | Node ( _ , _ , l , _ , r ) r -> Zed_string . append ( to_string l ) l ( to_string r ) r |
module Text = struct type t = rope let get = get let init = init let length = length type index = Zip . t let look _ zip = fst ( Zip . next zip ) zip let nth rope idx = Zip . make_f rope idx let next _ zip = Zip . move 1 zip let prev _ zip = Zip . move ( - 1 ) 1 zip let out_of_range _ zip = Zip . at_eos zip let iter = ... |
module Text_core = struct include Text let get t i = Zed_char . core ( get t i ) i let init = init_from_uChars let look _ zip = Zed_char . core ( fst ( Zip . next zip ) zip ) zip let iter f = iter ( fun c -> f ( Zed_char . core c ) c ) c end |
module Text_raw = struct type t = rope type index = Zip_raw . t let get = get_raw let init = init_from_uChars let length = length let look _ zip = fst ( Zip_raw . next zip ) zip let iter f = iter ( fun c -> f ( Zed_char . core c ) c ) c let nth rope idx = Zip_raw . make_f rope idx let next _ zip = Zip_raw . move 1 zip ... |
let fail str pos msg = raise ( Invalid ( InvalidPrintf . sprintf " at position % d : % s " pos msg , str ) str ) str |
module Zed_string0 = struct type seg_width = { start : int ; len : int ; width : int ; } type all_width = { len : int ; width : int ; } type width = ( all_width , seg_width ) seg_width result type t = Zed_utf8 . t let aval_width = function | Ok { len = _ ; width } width -> width | Error { start = _ ; len = _ ; width } ... |
module US_Core = struct include Zed_string0 let get str i = Zed_char . core ( get str i ) i let init = init_from_uChars let iter f str = iter ( fun zChar -> f ( Zed_char . core zChar ) zChar ) zChar str let compare str1 str2 = Zed_utils . list_compare ~ compare : Zed_char . compare_core ( explode str1 ) str1 ( explode ... |
module US_Raw = struct type t = Zed_string0 . t let get = Zed_string0 . get_raw let init = Zed_string0 . init_from_uChars let length = Zed_utf8 . length type index = int let check_range t n = n >= 0 && n < Zed_string0 . size t let out_of_range str ofs = ofs < 0 || ofs >= String . length str let look str ofs = Zed_utf8 ... |
let fail str pos msg = raise ( Invalid ( InvalidPrintf . sprintf " at position % d : % s " pos msg , str ) str ) str |
let byte str i = Char . code ( String . unsafe_get str i ) i |
let set_byte str i n = Bytes . unsafe_set str i ( Char . unsafe_chr n ) n |
type check_result = | Correct of int | Message of string |
let next_error s i = let len = String . length s in let rec main i ulen = if i = len then ( i , ulen , ) " " else let ch = String . unsafe_get s i in match ch with | ' \ x00 ' . . ' \ x7f ' -> main ( i + 1 ) 1 ( ulen + 1 ) 1 | ' \ xc0 ' . . ' \ xdf ' -> if i + 1 >= len then ( i , ulen , " premature end of UTF8 sequence... |
let check str = let ofs , len , msg = next_error str 0 in if ofs = String . length str then Correct len else Message ( Printf . sprintf " at position % d : % s " ofs msg ) msg |
let validate str = let ofs , len , msg = next_error str 0 in if ofs = String . length str then len else fail str ofs msg |
let unsafe_next str ofs = match String . unsafe_get str ofs with | ' \ x00 ' . . ' \ x7f ' -> ofs + 1 | ' \ xc0 ' . . ' \ xdf ' -> if ofs + 2 > String . length str then fail str ofs " unterminated UTF - 8 sequence " else ofs + 2 | ' \ xe0 ' . . ' \ xef ' -> if ofs + 3 > String . length str then fail str ofs " untermina... |
let unsafe_prev str ofs = match String . unsafe_get str ( ofs - 1 ) 1 with | ' \ x00 ' . . ' \ x7f ' -> ofs - 1 | ' \ x80 ' . . ' \ xbf ' -> if ofs >= 2 then match String . unsafe_get str ( ofs - 2 ) 2 with | ' \ xc0 ' . . ' \ xdf ' -> ofs - 2 | ' \ x80 ' . . ' \ xbf ' -> if ofs >= 3 then match String . unsafe_get str ... |
let unsafe_extract str ofs = let ch = String . unsafe_get str ofs in match ch with | ' \ x00 ' . . ' \ x7f ' -> UChar . of_char ch | ' \ xc0 ' . . ' \ xdf ' -> if ofs + 2 > String . length str then fail str ofs " unterminated UTF - 8 sequence " else UChar . of_int ( ( ( Char . code ch land 0x1f ) 0x1f lsl 6 ) 6 lor ( b... |
let unsafe_extract_next str ofs = let ch = String . unsafe_get str ofs in match ch with | ' \ x00 ' . . ' \ x7f ' -> ( UChar . of_char ch , ofs + 1 ) 1 | ' \ xc0 ' . . ' \ xdf ' -> if ofs + 2 > String . length str then fail str ofs " unterminated UTF - 8 sequence " else ( UChar . of_int ( ( ( Char . code ch land 0x1f )... |
let unsafe_extract_prev str ofs = let ch1 = String . unsafe_get str ( ofs - 1 ) 1 in match ch1 with | ' \ x00 ' . . ' \ x7f ' -> ( UChar . of_char ch1 , ofs - 1 ) 1 | ' \ x80 ' . . ' \ xbf ' -> if ofs >= 2 then let ch2 = String . unsafe_get str ( ofs - 2 ) 2 in match ch2 with | ' \ xc0 ' . . ' \ xdf ' -> ( UChar . of_i... |
let rec move_l str ofs len = if len = 0 then ofs else if ofs = String . length str then raise Out_of_bounds else move_l str ( unsafe_next str ofs ) ofs ( len - 1 ) 1 |
let unsafe_sub str ofs len = let res = Bytes . create len in String . unsafe_blit str ofs res 0 len ; Bytes . unsafe_to_string res |
let singleton char = let code = UChar . code char in Bytes . unsafe_to_string @@ if code < 0x80 then begin let s = Bytes . create 1 in set_byte s 0 code ; s end else if code <= 0x800 then begin let s = Bytes . create 2 in set_byte s 0 ( ( code lsr 6 ) 6 lor 0xc0 ) 0xc0 ; set_byte s 1 ( ( code land 0x3f ) 0x3f lor 0x80 ... |
let make n code = let str = singleton code in let len = String . length str in let res = Bytes . create ( n * len ) len in let ofs = ref 0 in for _ = 1 to n do String . unsafe_blit str 0 res ! ofs len ; ofs := ! ofs + len done ; Bytes . unsafe_to_string res |
let init n f = let buf = Buffer . create n in for i = 0 to n - 1 do Buffer . add_string buf ( singleton ( f i ) i ) i done ; Buffer . contents buf |
let rev_init n f = let buf = Buffer . create n in for i = n - 1 downto 0 do Buffer . add_string buf ( singleton ( f i ) i ) i done ; Buffer . contents buf |
let rec length_rec str ofs len = if ofs = String . length str then len else length_rec str ( unsafe_next str ofs ) ofs ( len + 1 ) 1 |
let length str = length_rec str 0 0 |
let rec compare_rec str1 ofs1 str2 ofs2 = if ofs1 = String . length str1 then if ofs2 = String . length str2 then 0 else - 1 else if ofs2 = String . length str2 then 1 else let code1 , ofs1 = unsafe_extract_next str1 ofs1 and code2 , ofs2 = unsafe_extract_next str2 ofs2 in let d = UChar . code code1 - UChar . code code... |
let compare str1 str2 = compare_rec str1 0 str2 0 |
let get str idx = if idx < 0 then raise Out_of_bounds else unsafe_extract str ( move_l str 0 idx ) idx |
let sub str idx len = if idx < 0 || len < 0 then raise Out_of_bounds else let ofs1 = move_l str 0 idx in let ofs2 = move_l str ofs1 len in unsafe_sub str ofs1 ( ofs2 - ofs1 ) ofs1 |
let break str idx = if idx < 0 then raise Out_of_bounds else let ofs = move_l str 0 idx in ( unsafe_sub str 0 ofs , unsafe_sub str ofs ( String . length str - ofs ) ofs ) ofs |
let before str idx = if idx < 0 then raise Out_of_bounds else let ofs = move_l str 0 idx in unsafe_sub str 0 ofs |
let after str idx = if idx < 0 then raise Out_of_bounds else let ofs = move_l str 0 idx in unsafe_sub str ofs ( String . length str - ofs ) ofs |
let concat3 a b c = let lena = String . length a and lenb = String . length b and lenc = String . length c in let res = Bytes . create ( lena + lenb + lenc ) lenc in String . unsafe_blit a 0 res 0 lena ; String . unsafe_blit b 0 res lena lenb ; String . unsafe_blit c 0 res ( lena + lenb ) lenb lenc ; Bytes . unsafe_to_... |
let insert str idx sub = let a , b = break str idx in concat3 a sub b |
let remove str idx len = if idx < 0 || len < 0 then raise Out_of_bounds else let ofs1 = move_l str 0 idx in let ofs2 = move_l str ofs1 len in unsafe_sub str 0 ofs1 ^ unsafe_sub str ofs2 ( String . length str - ofs2 ) ofs2 |
let replace str idx len repl = if idx < 0 || len < 0 then raise Out_of_bounds else let ofs1 = move_l str 0 idx in let ofs2 = move_l str ofs1 len in concat3 ( unsafe_sub str 0 ofs1 ) ofs1 repl ( unsafe_sub str ofs2 ( String . length str - ofs2 ) ofs2 ) ofs2 |
let rec rev_rec ( res : Bytes . t ) t str ofs_src ofs_dst = if ofs_src = String . length str then Bytes . unsafe_to_string res else begin let ofs_src ' = unsafe_next str ofs_src in let len = ofs_src ' - ofs_src in let ofs_dst = ofs_dst - len in String . unsafe_blit str ofs_src res ofs_dst len ; rev_rec res str ofs_src ... |
let rev str = let len = String . length str in rev_rec ( Bytes . create len ) len str 0 len |
let concat sep l = match l with | [ ] -> " " | x :: l -> let sep_len = String . length sep in let len = List . fold_left ( fun len str -> len + sep_len + String . length str ) str ( String . length x ) x l in let res = Bytes . create len in String . unsafe_blit x 0 res 0 ( String . length x ) x ; ignore ( List . fold_l... |
let rev_concat sep l = match l with | [ ] -> " " | x :: l -> let sep_len = String . length sep in let len = List . fold_left ( fun len str -> len + sep_len + String . length str ) str ( String . length x ) x l in let res = Bytes . create len in let ofs = len - String . length x in String . unsafe_blit x 0 res ofs ( Str... |
let rec explode_rec str ofs acc = if ofs = 0 then acc else let x , ofs = unsafe_extract_prev str ofs in explode_rec str ofs ( x :: acc ) acc |
let explode str = explode_rec str ( String . length str ) str [ ] |
let rec rev_explode_rec str ofs acc = if ofs = String . length str then acc else let x , ofs = unsafe_extract_next str ofs in rev_explode_rec str ofs ( x :: acc ) acc |
let rev_explode str = rev_explode_rec str 0 [ ] |
let implode l = let l = List . map singleton l in let len = List . fold_left ( fun len str -> len + String . length str ) str 0 l in let res = Bytes . create len in ignore ( List . fold_left ( fun ofs str -> let len = String . length str in String . unsafe_blit str 0 res ofs len ; ofs + len ) len 0 l ) l ; Bytes . unsa... |
let rev_implode l = let l = List . map singleton l in let len = List . fold_left ( fun len str -> len + String . length str ) str 0 l in let res = Bytes . create len in ignore ( List . fold_left ( fun ofs str -> let len = String . length str in let ofs = ofs - len in String . unsafe_blit str 0 res ofs len ; ofs ) ofs l... |
let rec iter_rec f str ofs = if ofs = String . length str then ( ) else begin let chr , ofs = unsafe_extract_next str ofs in f chr ; iter_rec f str ofs end |
let iter f str = iter_rec f str 0 |
let rec rev_iter_rec f str ofs = if ofs = 0 then ( ) else begin let chr , ofs = unsafe_extract_prev str ofs in f chr ; rev_iter_rec f str ofs end |
let rev_iter f str = rev_iter_rec f str ( String . length str ) str |
let rec fold_rec f str ofs acc = if ofs = String . length str then acc else begin let chr , ofs = unsafe_extract_next str ofs in fold_rec f str ofs ( f chr acc ) acc end |
let fold f str acc = fold_rec f str 0 acc |
let rec rev_fold_rec f str ofs acc = if ofs = 0 then acc else begin let chr , ofs = unsafe_extract_prev str ofs in rev_fold_rec f str ofs ( f chr acc ) acc end |
let rev_fold f str acc = rev_fold_rec f str ( String . length str ) str acc |
let rec map_rec buf f str ofs = if ofs = String . length str then Buffer . contents buf else begin let chr , ofs = unsafe_extract_next str ofs in Buffer . add_string buf ( singleton ( f chr ) chr ) chr ; map_rec buf f str ofs end |
let map f str = map_rec ( Buffer . create ( String . length str ) str ) str f str 0 |
let rec map_concat_rec buf f str ofs = if ofs = String . length str then Buffer . contents buf else begin let chr , ofs = unsafe_extract_next str ofs in Buffer . add_string buf ( f chr ) chr ; map_concat_rec buf f str ofs end |
let map_concat f str = map_concat_rec ( Buffer . create ( String . length str ) str ) str f str 0 |
let rec rev_map_rec buf f str ofs = if ofs = 0 then Buffer . contents buf else begin let chr , ofs = unsafe_extract_prev str ofs in Buffer . add_string buf ( singleton ( f chr ) chr ) chr ; rev_map_rec buf f str ofs end |
let rev_map f str = rev_map_rec ( Buffer . create ( String . length str ) str ) str f str ( String . length str ) str |
let rec rev_map_concat_rec buf f str ofs = if ofs = 0 then Buffer . contents buf else begin let chr , ofs = unsafe_extract_prev str ofs in Buffer . add_string buf ( f chr ) chr ; rev_map_concat_rec buf f str ofs end |
let rev_map_concat f str = rev_map_concat_rec ( Buffer . create ( String . length str ) str ) str f str ( String . length str ) str |
let rec filter_rec buf f str ofs = if ofs = String . length str then Buffer . contents buf else begin let chr , ofs = unsafe_extract_next str ofs in if f chr then Buffer . add_string buf ( singleton chr ) chr ; filter_rec buf f str ofs end |
let filter f str = filter_rec ( Buffer . create ( String . length str ) str ) str f str 0 |
let rec rev_filter_rec buf f str ofs = if ofs = 0 then Buffer . contents buf else begin let chr , ofs = unsafe_extract_prev str ofs in if f chr then Buffer . add_string buf ( singleton chr ) chr ; rev_filter_rec buf f str ofs end |
let rev_filter f str = rev_filter_rec ( Buffer . create ( String . length str ) str ) str f str ( String . length str ) str |
let rec filter_map_rec buf f str ofs = if ofs = String . length str then Buffer . contents buf else begin let chr , ofs = unsafe_extract_next str ofs in ( match f chr with | Some chr -> Buffer . add_string buf ( singleton chr ) chr | None -> ( ) ) ; filter_map_rec buf f str ofs end |
let filter_map f str = filter_map_rec ( Buffer . create ( String . length str ) str ) str f str 0 |
let rec filter_map_concat_rec buf f str ofs = if ofs = String . length str then Buffer . contents buf else begin let chr , ofs = unsafe_extract_next str ofs in ( match f chr with | Some txt -> Buffer . add_string buf txt | None -> ( ) ) ; filter_map_concat_rec buf f str ofs end |
let filter_map_concat f str = filter_map_concat_rec ( Buffer . create ( String . length str ) str ) str f str 0 |
let rec rev_filter_map_rec buf f str ofs = if ofs = 0 then Buffer . contents buf else begin let chr , ofs = unsafe_extract_prev str ofs in ( match f chr with | Some chr -> Buffer . add_string buf ( singleton chr ) chr | None -> ( ) ) ; rev_filter_map_rec buf f str ofs end |
let rev_filter_map f str = rev_filter_map_rec ( Buffer . create ( String . length str ) str ) str f str ( String . length str ) str |
let rec rev_filter_map_concat_rec buf f str ofs = if ofs = 0 then Buffer . contents buf else begin let chr , ofs = unsafe_extract_prev str ofs in ( match f chr with | Some txt -> Buffer . add_string buf txt | None -> ( ) ) ; rev_filter_map_concat_rec buf f str ofs end |
let rev_filter_map_concat f str = rev_filter_map_concat_rec ( Buffer . create ( String . length str ) str ) str f str ( String . length str ) str |
let rec for_all_rec f str ofs = if ofs = String . length str then true else let chr , ofs = unsafe_extract_next str ofs in f chr && for_all_rec f str ofs |
let for_all f str = for_all_rec f str 0 |
let rec exists_rec f str ofs = if ofs = String . length str then false else let chr , ofs = unsafe_extract_next str ofs in f chr || exists_rec f str ofs |
let exists f str = exists_rec f str 0 |
let rec count_rec f str ofs n = if ofs = String . length str then n else let chr , ofs = unsafe_extract_next str ofs in count_rec f str ofs ( if f chr then n + 1 else n ) n |
let count f str = count_rec f str 0 0 |
let rec unsafe_sub_equal str ofs sub ofs_sub = if ofs_sub = String . length sub then true else ( String . unsafe_get str ofs = String . unsafe_get sub ofs_sub ) ofs_sub && unsafe_sub_equal str ( ofs + 1 ) 1 sub ( ofs_sub + 1 ) 1 |
let rec contains_rec str sub ofs = if ofs + String . length sub > String . length str then false else unsafe_sub_equal str ofs sub 0 || contains_rec str sub ( unsafe_next str ofs ) ofs |
let contains str sub = contains_rec str sub 0 |
let starts_with str prefix = if String . length prefix > String . length str then false else unsafe_sub_equal str 0 prefix 0 |
let ends_with str suffix = let ofs = String . length str - String . length suffix in if ofs < 0 then false else unsafe_sub_equal str ofs suffix 0 |
let rec lfind predicate str ofs = if ofs = String . length str then ofs else let chr , ofs ' = unsafe_extract_next str ofs in if predicate chr then lfind predicate str ofs ' else ofs |
let rec rfind predicate str ofs = if ofs = 0 then 0 else let chr , ofs ' = unsafe_extract_prev str ofs in if predicate chr then rfind predicate str ofs ' else ofs |
let spaces = UCharInfo . load_property_tbl ` White_Space |
let is_space ch = UCharTbl . Bool . get spaces ch |
let strip ( ? predicate = is_space ) is_space str = let lofs = lfind predicate str 0 and rofs = rfind predicate str ( String . length str ) str in if lofs < rofs then unsafe_sub str lofs ( rofs - lofs ) lofs else " " |
let lstrip ( ? predicate = is_space ) is_space str = let lofs = lfind predicate str 0 in unsafe_sub str lofs ( String . length str - lofs ) lofs |
let rstrip ( ? predicate = is_space ) is_space str = let rofs = rfind predicate str ( String . length str ) str in unsafe_sub str 0 rofs |
let lchop = function | " " -> " " | str -> let ofs = unsafe_next str 0 in unsafe_sub str ofs ( String . length str - ofs ) ofs |
let rchop = function | " " -> " " | str -> let ofs = unsafe_prev str ( String . length str ) str in unsafe_sub str 0 ofs |
let add buf char = let code = UChar . code char in if code < 0x80 then Buffer . add_char buf ( Char . unsafe_chr code ) code else if code <= 0x800 then begin Buffer . add_char buf ( Char . unsafe_chr ( ( code lsr 6 ) 6 lor 0xc0 ) 0xc0 ) 0xc0 ; Buffer . add_char buf ( Char . unsafe_chr ( ( code land 0x3f ) 0x3f lor 0x80... |
let extract str ofs = if ofs < 0 || ofs >= String . length str then raise Out_of_bounds else unsafe_extract str ofs |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.