text
stringlengths 12
786k
|
---|
let get_count r = S . value r . count |
let counter r = r . counter |
let get_counter r = S . value r . counter |
let set_counter r v = r . set_counter v |
let add_counter r v = r . set_counter ( S . value r . counter + v ) v |
let add r x = if S . value r . recording then begin r . tmp_macro <- x :: r . tmp_macro ; r . set_count ( S . value r . count + 1 ) 1 end |
let contents r = r . macro |
module Core = struct module Re = URe . Make ( MakeZed_rope . Text_core ) Text_core type t = Re . compiled_regexp type match_result = ( Re . index * Re . index ) index option array option let compile = Re . compile let convert_success arr = Array . map ( function | Some sub -> let _rope , zip1 , zip2 = Re . SubText . context sub in Some ( Somezip1 , zip2 ) zip2 | None -> None ) None arr let convert = function | Some arr -> Some ( Someconvert_success arr ) arr | None -> None let regexp_match ? sem regexp rope idx = convert ( Re . regexp_match ? sem regexp rope ( Zed_rope . Zip . make_f rope idx ) idx ) idx let search_forward ? sem regexp rope idx = convert ( try Re . search_forward ? sem regexp rope ( Zed_rope . Zip . make_f rope idx ) idx with Not_found -> None ) None let search_backward ? sem regexp rope idx = let rec loop zip = match Re . regexp_match ? sem regexp rope zip with | Some arr -> Some ( Someconvert_success arr ) arr | None -> if Zed_rope . Zip . at_bos zip then None else loop ( Zed_rope . Zip . move ( - 1 ) 1 zip ) zip in loop ( Zed_rope . Zip . make_f rope idx ) idx let subtext_to_uChars = let module CS = Zed_utils . Convert ( ConvertRe . SubText ) SubText in CS . to_uChars end |
module Raw = struct module Re = URe . Make ( MakeZed_rope . Text_raw ) Text_raw type t = Re . compiled_regexp type match_result = ( Re . index * Re . index ) index option array option let compile = Re . compile let convert_success arr = Array . map ( function | Some sub -> let _rope , zip1 , zip2 = Re . SubText . context sub in Some ( Somezip1 , zip2 ) zip2 | None -> None ) None arr let convert = function | Some arr -> Some ( Someconvert_success arr ) arr | None -> None let regexp_match ? sem regexp rope idx = convert ( Re . regexp_match ? sem regexp rope ( Zed_rope . Zip_raw . make_f rope idx ) idx ) idx let search_forward ? sem regexp rope idx = convert ( try Re . search_forward ? sem regexp rope ( Zed_rope . Zip_raw . make_f rope idx ) idx with Not_found -> None ) None let search_backward ? sem regexp rope idx = let rec loop zip = match Re . regexp_match ? sem regexp rope zip with | Some arr -> Some ( Someconvert_success arr ) arr | None -> if Zed_rope . Zip_raw . at_bos zip then None else loop ( Zed_rope . Zip_raw . move ( - 1 ) 1 zip ) zip in loop ( Zed_rope . Zip_raw . make_f rope idx ) idx let subtext_to_uChars = let module CS = Zed_utils . Convert ( ConvertRe . SubText ) SubText in CS . to_uChars end |
type t = | Leaf of Zed_string . t * ( int * int ) int | Node of int * ( int * int ) int * t * ( int * int ) int * t |
let empty ( ) = Leaf ( Zed_string . empty ( ) , ( 0 , 0 ) 0 ) 0 |
let length = function | Leaf ( Leaf_ , ( len , _ ) _ ) _ -> len | Node ( Node_ , ( len_l , _ ) _ , _ , ( len_r , _ ) _ , _ ) _ -> len_l + len_r |
let size = function | Leaf ( Leaf_ , ( _ , size ) size ) size -> size | Node ( Node_ , ( _ , size_l ) size_l , _ , ( _ , size_r ) size_r , _ ) _ -> size_l + size_r |
let depth = function | Leaf _ -> 0 | Node ( Noded , _ , _ , _ , _ ) _ -> d |
let is_empty = function | Leaf ( Leaf_ , ( 0 , 0 ) 0 ) 0 -> true | _ -> false |
let rec trim_hd t = match t with | Leaf ( str , ( l , _ ) _ ) _ -> let hd , _ = Zed_string . extract_next str 0 in let hd = hd |> Zed_char . to_utf8 |> Zed_string . unsafe_of_utf8 in let after = Zed_string . after str 1 in let size = Zed_string . size after in ( Leaf ( after , ( l - 1 , size ) size ) size , hd ) hd | Node ( d , ( ll , _sl ) _sl , l , ( lr , sr ) sr , r ) r -> let t , hd = trim_hd l in let size = size t in ( Node ( d , ( ll - 1 , size ) size , t , ( lr , sr ) sr , r ) r , hd ) hd |
let append_cm t cm = let size = Zed_string . size cm in let rec append_cm t = match t with | Leaf ( str , ( l , s ) s ) s -> Leaf ( Zed_string . append str cm , ( l , s + size ) size ) size | Node ( d , ( ll , sl ) sl , l , ( lr , sr ) sr , r ) r -> Node ( d , ( ll , sl ) sl , l , ( lr , sr + size ) size , append_cm r ) r in append_cm t |
let rec make_fibo acc a b = let c = a + b in if c < b then acc else make_fibo ( c :: acc ) acc b c |
let fibo = let l = make_fibo [ 1 ; 1 ; 0 ] 0 1 1 in let n = List . length l in let fibo = Array . make n 0 in let rec loop i = function | [ ] -> fibo | x :: l -> fibo ( . i ) i <- x ; loop ( i - 1 ) 1 l in loop ( n - 1 ) 1 l |
let unsafe_concat rope1 rope2 = match rope1 , rope2 with | Leaf ( Leaf_ , ( 0 , _ ) _ ) _ , _ -> rope2 | _ , Leaf ( Leaf_ , ( 0 , _ ) _ ) _ -> rope1 | _ -> Node ( Node 1 + max ( depth rope1 ) rope1 ( depth rope2 ) rope2 , ( length rope1 , size rope1 ) rope1 , rope1 , ( length rope2 , size rope2 ) rope2 , rope2 ) rope2 |
let rec insert_to_forest forest acc idx = let acc = unsafe_concat forest ( . idx ) idx acc in if length acc < fibo ( . idx + 1 ) 1 then forest ( . idx ) idx <- acc else begin forest ( . idx ) idx <- empty ( ) ; insert_to_forest forest acc ( idx + 1 ) 1 end |
let rec concat_forest_until forest acc idx rope = if length rope < fibo ( . idx + 1 ) 1 then insert_to_forest forest ( unsafe_concat acc rope ) rope idx else begin let acc = unsafe_concat forest ( . idx ) idx acc in forest ( . idx ) idx <- empty ( ) ; concat_forest_until forest acc ( idx + 1 ) 1 rope end |
let rec balance_rec forest rope = match rope with | Leaf _ -> concat_forest_until forest ( empty ( ) ) 2 rope | Node ( Node_depth , _len_l , rope_l , _len_r , rope_r ) rope_r -> balance_rec forest rope_l ; balance_rec forest rope_r |
let rec concat_forest forest acc idx = if idx = max_depth then acc else concat_forest forest ( unsafe_concat forest ( . idx ) idx acc ) acc ( idx + 1 ) 1 |
let balance rope = match length rope with | 0 | 1 -> rope | len when len >= fibo ( . depth rope + 2 ) 2 -> rope | _len -> let forest = Array . make max_depth ( empty ( ) ) in balance_rec forest rope ; concat_forest forest ( empty ( ) ) 2 |
let rec unsafe_get idx rope = match rope with | Leaf ( Leaftext , _ ) _ -> Zed_string . get text idx | Node ( Node_ , ( len_l , _ ) _ , rope_l , _len_r , rope_r ) rope_r -> if idx < len_l then unsafe_get idx rope_l else unsafe_get ( idx - len_l ) len_l rope_r |
let get rope idx = if idx < 0 || idx >= length rope then raise Out_of_bounds else unsafe_get idx rope |
let rec unsafe_get_raw idx rope = match rope with | Leaf ( Leaftext , _ ) _ -> Zed_string . get_raw text idx | Node ( Node_ , ( _ , size_l ) size_l , rope_l , _len_r , rope_r ) rope_r -> if idx < size_l then unsafe_get_raw idx rope_l else unsafe_get_raw ( idx - size_l ) size_l rope_r |
let get_raw rope idx = if idx < 0 || idx >= size rope then raise Out_of_bounds else unsafe_get_raw idx rope |
let append rope1 rope2 = let len_12_comb = if length rope1 > 0 && length rope2 > 0 then Zed_char . is_combining_mark ( Zed_char . core ( get rope2 0 ) 0 ) 0 else false in let len12 l1 l2 = if len_12_comb then l1 + l2 - 1 else l1 + l2 in match rope1 , rope2 with | Leaf ( Leaf_ , ( 0 , _ ) _ ) _ , _ -> rope2 | _ , Leaf ( Leaf_ , ( 0 , _ ) _ ) _ -> rope1 | Leaf ( Leaftext1 , ( len1 , size1 ) size1 ) size1 , Leaf ( Leaftext2 , ( len2 , size2 ) size2 ) size2 when len12 len1 len2 <= max_leaf_size -> Leaf ( LeafZed_string . append text1 text2 , ( len12 len1 len2 , size1 + size2 ) size2 ) size2 | Node ( Noded , len_l , rope_l , _ , Leaf ( Leaftext1 , ( len1 , size1 ) size1 ) size1 ) size1 , Leaf ( Leaftext2 , ( len2 , size2 ) size2 ) size2 when len12 len1 len2 <= max_leaf_size -> let ls = len12 len1 len2 , size1 + size2 in Node ( Node d , len_l , rope_l , ls , Leaf ( LeafZed_string . append text1 text2 , ls ) ls ) ls | Leaf ( Leaftext1 , ( len1 , size1 ) size1 ) size1 , Node ( Noded , _ , Leaf ( Leaftext2 , ( len2 , size2 ) size2 ) size2 , len_r , rope_r ) rope_r when len12 len1 len2 <= max_leaf_size -> let ls = len12 len1 len2 , size1 + size2 in Node ( Node d , ls , Leaf ( LeafZed_string . append text1 text2 , ls ) ls , len_r , rope_r ) rope_r | _ -> let rope1 , rope2 = if length rope1 > 0 && length rope2 > 0 then if Zed_char . is_combining_mark ( Zed_char . core ( get rope2 0 ) 0 ) 0 then let r2 , hd = trim_hd rope2 in let r1 = append_cm rope1 hd in r1 , r2 else rope1 , rope2 else rope1 , rope2 in balance ( Node ( Node 1 + max ( depth rope1 ) rope1 ( depth rope2 ) rope2 , ( length rope1 , size rope1 ) rope1 , rope1 , ( length rope2 , size rope2 ) rope2 , rope2 ) rope2 ) rope2 |
let concat sep l = let rec loop acc = function | [ ] -> acc | x :: l -> loop ( append ( append acc sep ) sep x ) x l in match l with | [ ] -> empty ( ) | x :: l -> loop x l |
let rec unsafe_sub rope idx len = match rope with | Leaf ( Leaftext , _ ) _ -> let str = Zed_string . sub ~ pos : idx ~ len text in let size = Zed_string . size str in Leaf ( Leafstr , ( len , size ) size ) size | Node ( Node_ , ( len_l , _ ) _ , rope_l , ( len_r , _ ) _ , rope_r ) rope_r -> if len = len_l + len_r then rope else if idx >= len_l then unsafe_sub rope_r ( idx - len_l ) len_l len else if idx + len <= len_l then unsafe_sub rope_l idx len else append ( unsafe_sub rope_l idx ( len_l - idx ) idx ) idx ( unsafe_sub rope_r 0 ( len - len_l + idx ) idx ) idx |
let sub rope idx len = if idx < 0 || len < 0 || idx + len > length rope then raise Out_of_bounds else unsafe_sub rope idx len |
let make length char = if length < max_leaf_size then Leaf ( LeafZed_string . make length char , ( length , length ) length ) length else begin let text = Zed_string . make max_leaf_size char in let chunk = Leaf ( Leaftext , ( max_leaf_size , max_leaf_size ) max_leaf_size ) max_leaf_size in let rec loop acc n = if n = 0 then acc else if n < max_leaf_size then let str = Zed_string . sub ~ pos : 0 ~ len : n text in let size = Zed_string . size str in append acc ( Leaf ( Leafstr , ( n , size ) size ) size ) size else loop ( append acc chunk ) chunk ( n - max_leaf_size ) max_leaf_size in loop ( empty ( ) ) length end |
let singleton ch = Leaf ( LeafZed_string . make 1 ch , ( 1 , 1 ) 1 ) 1 |
let break rope pos = let len = length rope in if pos < 0 || pos > len then raise Out_of_bounds ; ( unsafe_sub rope 0 pos , unsafe_sub rope pos ( len - pos ) pos ) pos |
let before rope pos = sub rope 0 pos |
let after rope pos = sub rope pos ( length rope - pos ) pos |
let insert rope pos sub = let before , after = break rope pos in append before ( append sub after ) after |
let remove rope pos len = append ( sub rope 0 pos ) pos ( sub rope ( pos + len ) len ( length rope - pos - len ) len ) len |
let replace rope pos len repl = append ( sub rope 0 pos ) pos ( append repl ( sub rope ( pos + len ) len ( length rope - pos - len ) len ) len ) len |
let insert_uChar rope pos ch = let open CamomileLibraryDefault . Camomile in if UChar . code ch = 0 then rope else if Zed_char . is_combining_mark ch then if length rope = 0 then failwith " inserting an individual combining mark " else if pos = 0 then failwith " inserting an individual combining mark " else let pos = if pos > 0 then pos - 1 else pos in let glyph = get rope pos in if Zed_char . is_printable_core ( Zed_char . core glyph ) glyph then let glyph = Zed_char . append glyph ch in replace rope pos 1 ( Leaf ( Zed_string . implode [ glyph ] glyph , ( 1 , 1 ) 1 ) 1 ) 1 else failwith " inserting an individual combining mark " else let sub = Leaf ( Zed_string . implode [ Zed_char . unsafe_of_uChar ch ] ch , ( 1 , 1 ) 1 ) 1 in insert rope pos sub |
let lchop = function | Leaf ( Leaf_ , ( 0 , _ ) _ ) _ -> empty ( ) | rope -> sub rope 1 ( length rope - 1 ) 1 |
let rchop = function | Leaf ( Leaf_ , ( 0 , _ ) _ ) _ -> empty ( ) | rope -> sub rope 0 ( length rope - 1 ) 1 |
let rec iter f = function | Leaf ( Leaftext , _ ) _ -> Zed_string . iter f text | Node ( Node_ , _ , rope_l , _ , rope_r ) rope_r -> iter f rope_l ; iter f rope_r |
let rec rev_iter f = function | Leaf ( Leaftext , _ ) _ -> Zed_string . rev_iter f text | Node ( Node_ , _ , rope_l , _ , rope_r ) rope_r -> rev_iter f rope_r ; rev_iter f rope_l |
let rec fold f rope acc = match rope with | Leaf ( Leaftext , _ ) _ -> Zed_string . fold f text acc | Node ( Node_ , _ , rope_l , _ , rope_r ) rope_r -> fold f rope_r ( fold f rope_l acc ) acc |
let rec rev_fold f rope acc = match rope with | Leaf ( Leaftext , _ ) _ -> Zed_string . rev_fold f text acc | Node ( Node_ , _ , rope_l , _ , rope_r ) rope_r -> rev_fold f rope_l ( rev_fold f rope_r acc ) acc |
let rec map f = function | Leaf ( Leaftxt , len ) len -> Leaf ( LeafZed_string . map f txt , len ) len | Node ( Nodedepth , length_l , rope_l , length_r , rope_r ) rope_r -> let rope_l ' = map f rope_l in let rope_r ' = map f rope_r in Node ( Nodedepth , length_l , rope_l ' , length_r , rope_r ' ) rope_r ' |
let rec rev_map f = function | Leaf ( Leaftxt , len ) len -> Leaf ( LeafZed_string . rev_map f txt , len ) len | Node ( Nodedepth , length_l , rope_l , length_r , rope_r ) rope_r -> let rope_l ' = rev_map f rope_l in let rope_r ' = rev_map f rope_r in Node ( Nodedepth , length_r , rope_r ' , length_l , rope_l ' ) rope_l ' |
let rec iter_leaf f = function | Leaf ( Leaftext , _ ) _ -> f text | Node ( Node_ , _ , rope_l , _ , rope_r ) rope_r -> iter_leaf f rope_l ; iter_leaf f rope_r |
let rec rev_iter_leaf f = function | Leaf ( Leaftext , _ ) _ -> f text | Node ( Node_ , _ , rope_l , _ , rope_r ) rope_r -> rev_iter_leaf f rope_r ; rev_iter_leaf f rope_l |
let rec fold_leaf f rope acc = match rope with | Leaf ( Leaftext , _ ) _ -> f text acc | Node ( Node_ , _ , rope_l , _ , rope_r ) rope_r -> fold_leaf f rope_r ( fold_leaf f rope_l acc ) acc |
let rec rev_fold_leaf f rope acc = match rope with | Leaf ( Leaftext , _ ) _ -> f text acc | Node ( Node_ , _ , rope_l , _ , rope_r ) rope_r -> rev_fold_leaf f rope_l ( rev_fold_leaf f rope_r acc ) acc |
let rec cmp_loop str1 ofs1 str2 ofs2 rest1 rest2 = if ofs1 = Zed_string . bytes str1 then match rest1 with | [ ] -> if ofs2 = Zed_string . length str2 && rest2 = [ ] then 0 else - 1 | rope1 :: rest1 -> cmp_search1 rope1 str2 ofs2 rest1 rest2 else if ofs2 = Zed_string . bytes str2 then match rest2 with | [ ] -> 1 | rope2 :: rest2 -> cmp_search2 rope2 str1 ofs1 rest1 rest2 else let chr1 , ofs1 = Zed_string . extract_next str1 ofs1 and chr2 , ofs2 = Zed_string . extract_next str2 ofs2 in let d = Zed_char . compare_raw chr1 chr2 in if d = 0 then cmp_loop str1 ofs1 str2 ofs2 rest1 rest2 else d match rope1 with | Leaf ( Leafstr1 , _ ) _ -> cmp_loop str1 0 str2 ofs2 rest1 rest2 | Node ( Node_ , _ , rope1_l , _ , rope1_r ) rope1_r -> cmp_search1 rope1_l str2 ofs2 ( rope1_r :: rest1 ) rest1 rest2 match rope2 with | Leaf ( Leafstr2 , _ ) _ -> cmp_loop str1 ofs1 str2 0 rest1 rest2 | Node ( Node_ , _ , rope2_l , _ , rope2_r ) rope2_r -> cmp_search2 rope2_l str1 ofs1 rest1 ( rope2_r :: rest2 ) rest2 |
let rec cmp_init rope1 rope2 rest1 = match rope1 with | Leaf ( Leafstr1 , _ ) _ -> cmp_search2 rope2 str1 0 rest1 [ ] | Node ( Node_ , _ , rope1_l , _ , rope1_r ) rope1_r -> cmp_init rope1_l rope2 ( rope1_r :: rest1 ) rest1 |
let compare r1 r2 = cmp_init r1 r2 [ ] |
let equal r1 r2 = length r1 = length r2 && compare r1 r2 = 0 |
module Zip = struct type rope_zipper = { str : Zed_string . t ; ofs : int ; leaf : t ; rest_b : t list ; rest_f : t list ; } type t = { idx : int ; pos : int ; zip : rope_zipper ; } let rec make_rec ofs rope pos rest_b rest_f = match rope with | Leaf ( Leafstr , _ ) _ -> { idx = Zed_string . move str 0 pos ; pos = pos ; zip = { str ; ofs = ofs - pos ; leaf = rope ; rest_b ; rest_f } } | Node ( Node_ , _ , r1 , _ , r2 ) r2 -> let len1 = length r1 in if pos < len1 then make_rec ofs r1 pos rest_b ( r2 :: rest_f ) rest_f else make_rec ofs r2 ( pos - len1 ) len1 ( r1 :: rest_b ) rest_b rest_f let make_f rope pos = if pos < 0 || pos > length rope then raise Out_of_bounds ; make_rec pos rope pos [ ] [ ] let make_b rope pos = let len = length rope in if pos < 0 || pos > len then raise Out_of_bounds ; let pos = len - pos in make_rec pos rope pos [ ] [ ] let offset zip = zip . zip . ofs + zip . pos let rec next_leaf ofs rope rest_b rest_f = match rope with | Leaf ( Leafstr , _ ) _ -> let chr , idx = Zed_string . extract_next str 0 in ( chr , { idx ; pos = 1 ; zip = { str ; ofs ; leaf = rope ; rest_b ; rest_f } } ) | Node ( Node_ , _ , r1 , _ , r2 ) r2 -> next_leaf ofs r1 rest_b ( r2 :: rest_f ) rest_f let next zip = if zip . idx = Zed_string . bytes zip . zip . str then match zip . zip . rest_f with | [ ] -> raise Out_of_bounds | rope :: rest -> next_leaf ( zip . zip . ofs + length zip . zip . leaf ) leaf rope ( zip . zip . leaf :: zip . zip . rest_b ) rest_b rest else let chr , idx = Zed_string . extract_next zip . zip . str zip . idx in ( chr , { zip with idx ; pos = zip . pos + 1 } ) let rec prev_leaf ofs rope rest_b rest_f = match rope with | Leaf ( Leafstr , ( len , _size ) _size ) _size -> let chr , idx = Zed_string . extract_prev str ( Zed_string . bytes str ) str in ( chr , { idx ; pos = len - 1 ; zip = { str ; ofs = ofs - len ; leaf = rope ; rest_b ; rest_f } } ) | Node ( Node_ , _ , r1 , _ , r2 ) r2 -> prev_leaf ofs r2 ( r1 :: rest_b ) rest_b rest_f let prev zip = if zip . pos = 0 then match zip . zip . rest_b with | [ ] -> raise Out_of_bounds | rope :: rest -> prev_leaf zip . zip . ofs rope rest ( zip . zip . leaf :: zip . zip . rest_f ) rest_f else let chr , idx = Zed_string . extract_prev zip . zip . str zip . idx in ( chr , { zip with idx ; pos = zip . pos - 1 } ) let rec move_f n ofs rope rest_b rest_f = match rope with | Leaf ( Leafstr , ( len , _size ) _size ) _size -> if n <= len then { idx = Zed_string . move str 0 n ; pos = n ; zip = { str ; ofs ; leaf = rope ; rest_b ; rest_f } } else begin match rest_f with | [ ] -> raise Out_of_bounds | rope ' :: rest_f -> move_f ( n - len ) len ( ofs + len ) len rope ' ( rope :: rest_b ) rest_b rest_f end | Node ( Node_ , _ , r1 , _ , r2 ) r2 -> move_f n ofs r1 rest_b ( r2 :: rest_f ) rest_f let rec move_b n ofs rope rest_b rest_f = match rope with | Leaf ( Leafstr , ( len , _size ) _size ) _size -> if n <= len then { idx = Zed_string . move str ( Zed_string . bytes str ) str ( - n ) n ; pos = len - n ; zip = { str ; ofs ; leaf = rope ; rest_b ; rest_f } } else begin match rest_b with | [ ] -> raise Out_of_bounds | rope ' :: rest_b -> move_b ( n - len ) len ( ofs - len ) len rope ' rest_b ( rope :: rest_f ) rest_f end | Node ( Node_ , _ , r1 , _ , r2 ) r2 -> move_b n ofs r2 ( r1 :: rest_b ) rest_b rest_f let move n zip = if n > 0 then let len = length zip . zip . leaf in if zip . pos + n <= len then { zip with idx = Zed_string . move zip . zip . str zip . idx n ; pos = zip . pos + n } else match zip . zip . rest_f with | [ ] -> raise Out_of_bounds | rope :: rest_f -> move_f ( n - ( len - zip . pos ) pos ) pos ( zip . zip . ofs + len ) len rope ( zip . zip . leaf :: zip . zip . rest_b ) rest_b rest_f else if zip . pos + n >= 0 then { zip with idx = Zed_string . move zip . zip . str zip . idx n ; pos = zip . pos + n } else match zip . zip . rest_b with | [ ] -> raise Out_of_bounds | rope :: rest_b -> move_b ( n - zip . pos ) pos zip . zip . ofs rope rest_b ( zip . zip . leaf :: zip . zip . rest_f ) rest_f let at_bos zip = zip . zip . rest_b = [ ] && zip . idx = 0 let at_eos zip = zip . zip . rest_f = [ ] && zip . idx = Zed_string . bytes zip . zip . str let rec sub_rec acc ropes len = match ropes with | [ ] -> if len > 0 then raise Out_of_bounds else acc | rope :: rest -> let len ' = length rope in if len <= len ' then append acc ( sub rope 0 len ) len else sub_rec ( append acc rope ) rope rest ( len - len ' ) len ' let sub zip len = if len < 0 then raise Out_of_bounds else let len ' = length zip . zip . leaf - zip . pos in if len <= len ' then let str = Zed_string . sub ~ pos : zip . pos ~ len zip . zip . str in let size = Zed_string . size str in Leaf ( Leafstr , ( len , size ) size ) size else let str = Zed_string . sub ~ pos : zip . pos ~ len ( : Zed_string . length zip . zip . str - zip . pos ) pos zip . zip . str in let size = Zed_string . size str in sub_rec ( Leaf ( Leafstr , ( len ' , size ) size ) size ) size zip . zip . rest_f ( len - len ' ) len ' let slice zip1 zip2 = let ofs1 = offset zip1 and ofs2 = offset zip2 in if ofs1 <= ofs2 then sub zip1 ( ofs2 - ofs1 ) ofs1 else sub zip2 ( ofs1 - ofs2 ) ofs2 let rec find_f f zip = if at_eos zip then zip else let ch , zip ' = next zip in if f ch then zip else find_f f zip ' let rec find_b f zip = if at_bos zip then zip else let ch , zip ' = prev zip in if f ch then zip else find_b f zip ' end |
module Zip_raw = struct type rope_zipper = { str : Zed_string . t ; ofs : int ; leaf : t ; rest_b : t list ; rest_f : t list ; } type t = { idx : int ; pos : int ; zip : rope_zipper ; } let rec make_f_rec ofs rope pos rest_b rest_f = match rope with | Leaf ( Leafstr , _ ) _ -> { idx = Zed_string . move_raw str 0 pos ; pos = pos ; zip = { str ; ofs = ofs - pos ; leaf = rope ; rest_b ; rest_f } } | Node ( Node_ , _ , r1 , _ , r2 ) r2 -> let size1 = size r1 in if pos < size1 then make_f_rec ofs r1 pos rest_b ( r2 :: rest_f ) rest_f else make_f_rec ofs r2 ( pos - size1 ) size1 ( r1 :: rest_b ) rest_b rest_f let make_f rope pos = if pos < 0 || pos > size rope then raise Out_of_bounds ; make_f_rec pos rope pos [ ] [ ] let rec make_b_rec ofs rope pos rest_b rest_f = match rope with | Leaf ( Leafstr , ( len , _ ) _ ) _ -> { idx = Zed_string . move_raw str ( Zed_string . bytes str ) str ( - ( len - pos ) pos ) pos ; pos = pos ; zip = { str ; ofs = ofs - pos ; leaf = rope ; rest_b ; rest_f } } | Node ( Node_ , _ , r1 , _ , r2 ) r2 -> let len1 = length r1 in if pos < len1 then make_b_rec ofs r1 pos rest_b ( r2 :: rest_f ) rest_f else make_b_rec ofs r2 ( pos - len1 ) len1 ( r1 :: rest_b ) rest_b rest_f let make_b rope pos = let size = size rope in if pos < 0 || pos > size then raise Out_of_bounds ; let pos = size - pos in make_b_rec pos rope pos [ ] [ ] let offset zip = zip . zip . ofs + zip . pos let rec next_leaf ofs rope rest_b rest_f = match rope with | Leaf ( Leafstr , _ ) _ -> let chr , idx = Zed_utf8 . unsafe_extract_next ( Zed_string . to_utf8 str ) str 0 in ( chr , { idx ; pos = 1 ; zip = { str ; ofs ; leaf = rope ; rest_b ; rest_f } } ) | Node ( Node_ , _ , r1 , _ , r2 ) r2 -> next_leaf ofs r1 rest_b ( r2 :: rest_f ) rest_f let next zip = if zip . pos = Zed_string . size zip . zip . str then match zip . zip . rest_f with | [ ] -> raise Out_of_bounds | rope :: rest -> next_leaf ( zip . zip . ofs + size zip . zip . leaf ) leaf rope ( zip . zip . leaf :: zip . zip . rest_b ) rest_b rest else let chr , idx = Zed_utf8 . unsafe_extract_next ( Zed_string . to_utf8 zip . zip . str ) str zip . idx in ( chr , { zip with idx ; pos = zip . pos + 1 } ) let rec prev_leaf ofs rope rest_b rest_f = match rope with | Leaf ( Leafstr , ( _len , size ) size ) size -> let chr , idx = let str = Zed_string . to_utf8 str in Zed_utf8 . unsafe_extract_prev str ( String . length str ) str in ( chr , { idx ; pos = size - 1 ; zip = { str ; ofs = ofs - size ; leaf = rope ; rest_b ; rest_f } } ) | Node ( Node_ , _ , r1 , _ , r2 ) r2 -> prev_leaf ofs r2 ( r1 :: rest_b ) rest_b rest_f let prev zip = if zip . pos = 0 then match zip . zip . rest_b with | [ ] -> raise Out_of_bounds | rope :: rest -> prev_leaf zip . zip . ofs rope rest ( zip . zip . leaf :: zip . zip . rest_f ) rest_f else let chr , idx = Zed_utf8 . unsafe_extract_prev ( Zed_string . to_utf8 zip . zip . str ) str zip . idx in ( chr , { zip with idx ; pos = zip . pos - 1 } ) let rec move_f n ofs rope rest_b rest_f = match rope with | Leaf ( Leafstr , ( _ , size ) size ) size -> if n <= size then { idx = Zed_string . move_raw str 0 n ; pos = n ; zip = { str ; ofs ; leaf = rope ; rest_b ; rest_f } } else begin match rest_f with | [ ] -> raise Out_of_bounds | rope ' :: rest_f -> move_f ( n - size ) size ( ofs + size ) size rope ' ( rope :: rest_b ) rest_b rest_f end | Node ( Node_ , _ , r1 , _ , r2 ) r2 -> move_f n ofs r1 rest_b ( r2 :: rest_f ) rest_f let rec move_b n ofs rope rest_b rest_f = match rope with | Leaf ( Leafstr , ( _ , size ) size ) size -> if n <= size then { idx = Zed_string . move_raw str ( Zed_string . bytes str ) str ( - n ) n ; pos = size - n ; zip = { str ; ofs ; leaf = rope ; rest_b ; rest_f } } else begin match rest_b with | [ ] -> raise Out_of_bounds | rope ' :: rest_b -> move_b ( n - size ) size ( ofs - size ) size rope ' rest_b ( rope :: rest_f ) rest_f end | Node ( Node_ , _ , r1 , _ , r2 ) r2 -> move_b n ofs r2 ( r1 :: rest_b ) rest_b rest_f let move n zip = if n > 0 then let size = size zip . zip . leaf in if zip . pos + n <= size then { zip with idx = Zed_string . move_raw zip . zip . str zip . idx n ; pos = zip . pos + n } else match zip . zip . rest_f with | [ ] -> raise Out_of_bounds | rope :: rest_f -> move_f ( n - ( size - zip . pos ) pos ) pos ( zip . zip . ofs + size ) size rope ( zip . zip . leaf :: zip . zip . rest_b ) rest_b rest_f else if zip . pos + n >= 0 then { zip with idx = Zed_string . move_raw zip . zip . str zip . idx ( - n ) n ; pos = zip . pos + n } else match zip . zip . rest_b with | [ ] -> raise Out_of_bounds | rope :: rest_b -> move_b ( n - zip . pos ) pos zip . zip . ofs rope rest_b ( zip . zip . leaf :: zip . zip . rest_f ) rest_f let at_bos zip = zip . zip . rest_b = [ ] && zip . idx = 0 let at_eos zip = zip . zip . rest_f = [ ] && zip . idx = Zed_string . bytes zip . zip . str let rec find_f f zip = if at_eos zip then zip else let ch , zip ' = next zip in if f ch then zip else find_f f zip ' let rec find_b f zip = if at_bos zip then zip else let ch , zip ' = prev zip in if f ch then zip else find_b f zip ' end |
module Buffer = struct type t = { mutable acc : rope ; mutable buf : Zed_string . Buf . buf ; mutable idx : int ; } let create ( ) = { acc = empty ( ) ; buf = Zed_string . Buf . create 1024 ; idx = 0 ; } let add buffer x = if buffer . idx = max_leaf_size then begin let str = Zed_string . Buf . contents buffer . buf in let size = Zed_string . size str in buffer . acc <- append buffer . acc ( Leaf ( Leafstr , ( max_leaf_size , size ) size ) size ) size ; Zed_string . Buf . reset buffer . buf ; Zed_string . Buf . add_zChar buffer . buf x ; buffer . idx <- Zed_string . Buf . length buffer . buf end else begin Zed_string . Buf . add_zChar buffer . buf x ; buffer . idx <- Zed_string . Buf . length buffer . buf end let add_uChar buffer x = if buffer . idx = max_leaf_size then begin let str = Zed_string . Buf . contents buffer . buf in let size = Zed_string . size str in buffer . acc <- append buffer . acc ( Leaf ( Leafstr , ( max_leaf_size , size ) size ) size ) size ; Zed_string . Buf . reset buffer . buf ; Zed_string . Buf . add_uChar buffer . buf x ; buffer . idx <- Zed_string . Buf . length buffer . buf end else begin Zed_string . Buf . add_uChar buffer . buf x ; buffer . idx <- Zed_string . Buf . length buffer . buf end let add_rope buf rope = iter ( add buf ) buf rope let add_string buf str = Zed_string . iter ( add buf ) buf str let contents buffer = if buffer . idx = 0 then buffer . acc else let str = Zed_string . Buf . contents buffer . buf in let size = Zed_string . size str in append buffer . acc ( Leaf ( str , ( buffer . idx , size ) size ) size ) size let reset buffer = Zed_string . Buf . reset buffer . buf ; buffer . acc <- empty ( ) ; buffer . idx <- 0 end |
let init n f = let buf = Buffer . create ( ) in for i = 0 to n - 1 do Buffer . add buf ( f i ) i done ; Buffer . contents buf |
let init_from_uChars len f = match len with | 0 -> empty ( ) | len when len > 0 -> let rec create n = if n > 0 then f ( len - n ) n :: create ( n - 1 ) 1 else [ ] in let uChars = create len in let zChars , _ = Zed_char . zChars_of_uChars uChars in let buf = Buffer . create ( ) in List . iter ( Buffer . add buf ) buf zChars ; Buffer . contents buf | _ -> raise ( Invalid_argument " Zed_rope . init_from_uChars ) " |
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 = iter let compare = compare let first rope = Zip . make_f rope 0 let last rope = Zip . make_b rope 1 let move _ zip delta = Zip . move delta zip let compare_index _ zip1 zip2 = Zip . offset zip1 - Zip . offset zip2 module Buf = struct type buf = Buffer . t let create _ = Buffer . create ( ) let contents = Buffer . contents let clear = Buffer . reset let reset = Buffer . reset let add_char = Buffer . add_uChar let add_string = Buffer . add_rope let add_buffer buf buf ' = add_string buf ( Buffer . contents buf ' ) buf ' end end |
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 prev _ zip = Zip_raw . move ( - 1 ) 1 zip let out_of_range _ zip = Zip_raw . at_eos zip let iter = iter let compare = compare let first rope = Zip_raw . make_f rope 0 let last rope = Zip_raw . make_b rope 1 let move _ zip delta = Zip_raw . move delta zip let compare_index _ zip1 zip2 = Zip_raw . offset zip1 - Zip_raw . offset zip2 module Buf = struct type buf = Buffer . t let create _ = Buffer . create ( ) let contents = Buffer . contents let clear = Buffer . reset let reset = Buffer . reset let add_char = Buffer . add_uChar let add_string = Buffer . add_rope let add_buffer buf buf ' = add_string buf ( Buffer . contents buf ' ) buf ' end end |
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 } width -> width let bytes str = String . length str let size str = Zed_utf8 . length str let copy t = t let unsafe_next str ofs = let str_len = String . length str in let rec skip str ofs = if ofs >= str_len then str_len else let chr , next = Zed_utf8 . unsafe_extract_next str ofs in if Zed_char . is_combining_mark chr then skip str next else ofs in if ofs < 0 || ofs >= String . length str then raise Out_of_bounds else let chr , next = Zed_utf8 . unsafe_extract_next str ofs in if Zed_char . is_printable chr then skip str next else next let next_ofs str ofs = let str_len = String . length str in let rec skip str ofs = if ofs >= str_len then str_len else let chr , next = Zed_utf8 . unsafe_extract_next str ofs in if Zed_char . is_combining_mark chr then skip str next else ofs in if ofs < 0 || ofs >= String . length str then raise Out_of_bounds else let chr , next = Zed_utf8 . unsafe_extract_next str ofs in if Zed_char . is_printable_core chr then skip str next else if Zed_char . is_combining_mark chr then fail str ofs " individual combining marks encountered " else next let length str = let eos = String . length str in let rec length len ofs = if ofs < eos then length ( len + 1 ) 1 ( unsafe_next str ofs ) ofs else len in length 0 0 let unsafe_prev str ofs = let rec skip str ofs = if ofs = 0 then ofs else let chr , prev = Zed_utf8 . unsafe_extract_prev str ofs in if Zed_char . is_combining_mark chr then skip str prev else prev in if ofs <= 0 || ofs > String . length str then raise Out_of_bounds else let chr , prev = Zed_utf8 . extract_prev str ofs in if Zed_char . is_combining_mark chr then skip str prev else prev let prev_ofs str ofs = let rec skip str ofs = if ofs = 0 then ofs else let chr , prev = Zed_utf8 . unsafe_extract_prev str ofs in if Zed_char . is_combining_mark chr then skip str prev else prev in if ofs <= 0 || ofs > String . length str then raise Out_of_bounds else let chr , prev = Zed_utf8 . extract_prev str ofs in if Zed_char . is_combining_mark chr then let prev = skip str prev in if prev = 0 then if Zed_char . is_printable_core ( Zed_utf8 . unsafe_extract str 0 ) 0 then prev else fail str 0 " individual combining marks encountered " else let chr , next = Zed_utf8 . unsafe_extract_next str prev in match Zed_char . prop_uChar chr with | Printable 0 | Other | Null -> fail str next " individual combining marks encountered " | _ -> prev else prev 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 move_b str ofs len = let rec move str ofs len = if len = 0 then ofs else if ofs < 0 then raise Out_of_bounds else move str ( unsafe_prev str ofs ) ofs ( len - 1 ) 1 in if ofs < 0 || ofs > String . length str then raise Out_of_bounds else move str ofs len let rec move_l_raw str ofs len = if len = 0 then ofs else if ofs >= String . length str then raise Out_of_bounds else move_l_raw str ( Zed_utf8 . unsafe_next str ofs ) ofs ( len - 1 ) 1 let move_b_raw str ofs len = let rec move str ofs len = if len = 0 then ofs else if ofs < 0 then raise Out_of_bounds else move str ( Zed_utf8 . unsafe_prev str ofs ) ofs ( len - 1 ) 1 in if ofs < 0 || ofs > String . length str then raise Out_of_bounds else move str ofs len let extract str ofs = let next = next_ofs str ofs in Zed_char . unsafe_of_utf8 ( String . sub str ofs ( next - ofs ) ofs ) ofs let extract_next str ofs = let next = next_ofs str ofs in ( Zed_char . unsafe_of_utf8 ( String . sub str ofs ( next - ofs ) ofs ) ofs , next ) next let extract_prev str ofs = let prev = prev_ofs str ofs in ( Zed_char . unsafe_of_utf8 ( String . sub str prev ( ofs - prev ) prev ) prev , prev ) prev let to_raw_list str = Zed_utf8 . explode str let to_raw_array str = Array . of_list ( to_raw_list str ) str type index = int let get str idx = if idx < 0 then raise Out_of_bounds else extract str ( move_l str 0 idx ) idx let get_raw = Zed_utf8 . get let empty ( ) = " " let width_ofs ( ? start = 0 ) 0 ? num str = let str_len = String . length str in let rec calc w idx ofs = if ofs < str_len then let chr , next = extract_next str ofs in let chr_width = Zed_char . width chr in if chr_width > 0 then calc ( w + chr_width ) chr_width ( idx + 1 ) 1 next else Error { start ; len = idx - start ; width = w } else Ok { len = idx - start ; width = w } in let calc_num num w idx ofs = let rec calc n w idx ofs = if ofs < str_len && n > 0 then let chr , next = extract_next str ofs in let chr_width = Zed_char . width chr in if chr_width > 0 then calc ( n - 1 ) 1 ( w + chr_width ) chr_width ( idx + 1 ) 1 next else Error { start ; len = idx - start ; width = w } else Ok { len = idx - start ; width = w } in calc num w idx ofs in match num with | Some num -> calc_num num 0 start start | None -> calc 0 start start let width ( ? start = 0 ) 0 ? num str = let ofs = move_l str 0 start in width_ofs ~ start : ofs ? num str let explode str = let str_len = String . length str in let rec aux acc str ofs = if ofs > 0 then let chr , prev = extract_prev str ofs in aux ( chr :: acc ) acc str prev else acc in if str_len > 0 then aux [ ] str str_len else [ ] let rev_explode str = let str_len = String . length str in let rec aux acc ofs = if ofs < str_len then let chr , next = extract_next str ofs in aux ( chr :: acc ) acc next else [ ] in if str_len > 0 then aux [ ] 0 else [ ] let unsafe_explode str = let str_len = String . length str in let rec aux acc str ofs = if ofs > 0 then let chr , prev = extract_prev str ofs in aux ( chr :: acc ) acc str prev else acc in if str_len > 0 then aux [ ] str str_len else [ ] let unsafe_rev_explode str = let str_len = String . length str in let rec aux acc ofs = if ofs < str_len then let chr , next = extract_next str ofs in aux ( chr :: acc ) acc next else [ ] in if str_len > 0 then aux [ ] 0 else [ ] let implode chars = String . concat " " ( List . map Zed_char . to_utf8 chars ) chars let init len ( f : int -> Zed_char . t ) t = let rec create acc n = if n > 0 then create ( ( f ( n - 1 ) 1 ) 1 :: acc ) acc ( n - 1 ) 1 else acc in implode ( create [ ] len ) len let init_from_uChars len f = match len with | 0 -> empty ( ) | len when len > 0 -> let rec create acc n = if n > 0 then create ( ( f ( n - 1 ) 1 ) 1 :: acc ) acc ( n - 1 ) 1 else acc in let uChars = create [ ] len in let zChars , _ = Zed_char . zChars_of_uChars uChars in implode zChars | _ -> raise ( Invalid_argument " Zed_string0 . init_from_uChars ) " let unsafe_of_uChars uChars = match uChars with | [ ] -> " " | _ -> String . concat " " ( List . map Zed_utf8 . singleton uChars ) uChars let of_uChars uChars = match uChars with | [ ] -> " " , [ ] | fst :: _ -> if Zed_char . is_combining_mark fst then ( " " , uChars ) uChars else ( uChars |> List . map Zed_utf8 . singleton |> String . concat " " , [ ] ) let unsafe_append s1 s2 = s1 ^ s2 let append s1 s2 = let validate_s2 ( ) = let s2_first = Zed_utf8 . unsafe_extract s2 0 in if Zed_char . is_combining_mark s2_first then fail s2 0 " individual combining marks encountered " else s2 in if s1 = " " then validate_s2 ( ) else if s2 = " " then s1 else let ( s1_last , _ ) _ = extract_prev s1 ( bytes s1 ) s1 in if Zed_char ( . is_printable_core ( core s1_last ) s1_last ) s1_last then unsafe_append s1 s2 else unsafe_append s1 ( validate_s2 ( ) ) external id : ' a -> ' a = " % identity " let unsafe_of_utf8 : string -> t = id let of_utf8 : string -> t = fun str -> if String . length str = 0 then " " else if Zed_char . is_combining_mark ( Zed_utf8 . extract str 0 ) 0 then fail str 0 " individual combining marks encountered " else unsafe_of_utf8 str let to_utf8 : t -> string = id let for_all p str = List . for_all p ( explode str ) str let check_range t n = n >= 0 && n <= length t let look str ofs = Zed_utf8 . extract str ofs let nth t n = if check_range t n then n else raise ( Invalid_argument " Zed_string . nth ) " let next t n = let n = n + 1 in if check_range t n then n else raise ( Invalid_argument " Zed_string . next ) " let prev t n = let n = n - 1 in if check_range t n then n else raise ( Invalid_argument " Zed_string . prev ) " let out_of_range t n = n < 0 || n >= length t let iter f str = List . iter f ( explode str ) str let rev_iter f str = List . iter f ( rev_explode str ) str let fold f str acc = let rec aux f chars acc = match chars with | [ ] -> acc | chr :: tl -> aux f tl ( f chr acc ) acc in aux f ( explode str ) str acc let rev_fold f str acc = let rec aux f chars acc = match chars with | [ ] -> acc | chr :: tl -> aux f tl ( f chr acc ) acc in aux f ( rev_explode str ) str acc let map f str = implode ( List . map f ( explode str ) str ) str let rev_map f str = implode ( List . map f ( rev_explode str ) str ) str let compare str1 str2 = Zed_utils . list_compare ~ compare : Zed_char . compare_raw ( explode str1 ) str1 ( explode str2 ) str2 let first ( _ : t ) t = 0 let last t = max ( length t - 1 ) 1 0 let move t i n = if n >= 0 then move_l t i n else move_b t i n let move_raw t i n = if n >= 0 then move_l_raw t i n else move_b_raw t i n let compare_index ( _ : t ) t i j = pervasives_compare i j let sub_ofs ~ ofs ~ len s = if ofs < 0 || len < 0 || ofs > bytes s - len then invalid_arg " Zed_string . sub " else String . sub s ofs len let sub ~ pos ~ len s = if pos < 0 || len < 0 || pos > length s - len then invalid_arg " Zed_string . sub " else let ofs_start = move_l s 0 pos in let ofs_end = move_l s ofs_start len in String . sub s ofs_start ( ofs_end - ofs_start ) ofs_start let after s i = let len = length s in if i < len then sub ~ pos : i ~ len ( : len - i ) i s else empty ( ) 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 starts_with ~ prefix str = if String . length prefix > String . length str then false else unsafe_sub_equal str 0 prefix 0 let make len c = implode ( Array . to_list ( Array . make len c ) c ) c let ends_with ~ suffix str = Zed_utf8 . ends_with str suffix module Buf0 = struct type buf = Buffer . t let create n = Buffer . create n let contents b = Buffer . contents b let clear b = Buffer . clear b let reset b = Buffer . reset b let length b = length ( contents b ) b let add_zChar b zChar = Buffer . add_string b ( Zed_char . to_utf8 zChar ) zChar let add_uChar b uChar = Buffer . add_string b ( Zed_utf8 . singleton uChar ) uChar let add_string b s = Buffer . add_string b s let add_buffer b1 b2 = Buffer . add_buffer b1 b2 end module US0 ( US0US : UnicodeString . Type ) Type = struct module Convert = Zed_utils . Convert ( ConvertUS ) ConvertUS let of_t t = Zed_utf8 . explode t |> Convert . of_list let to_t us = let first = US . first us and last = US . last us in let length = US . length us in let rec create acc i = if US . compare_index us i first >= 0 then create ( US . look us i :: acc ) acc ( US . prev us i ) i else acc in let uChars = if length > 0 then create [ ] last else [ ] in of_uChars uChars let to_t_exn us = let t , _ = to_t us in t end end |
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 str2 ) str2 let to_list str = explode str |> List . map Zed_char . core let to_array str = to_list str |> Array . of_list module US ( USUS : UnicodeString . Type ) Type = struct module Convert = Zed_utils . Convert ( ConvertUS ) ConvertUS let of_t t = ( explode t ) t |> List . map Zed_char . core |> Convert . of_list end module Buf = struct include Buf0 let add_char = add_uChar end end |
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 . extract str ofs let nth str idx = Zed_string0 . move_l str 0 idx let next = Zed_utf8 . next let prev = Zed_utf8 . prev let first _ = 0 let last str = Zed_utf8 . prev str ( String . length str ) str let move = Zed_string0 . move_raw let compare_index _str = pervasives_compare let iter f str = List . iter f ( Zed_utf8 . explode str ) str let compare str1 str2 = Zed_utils . list_compare ~ compare : UChar . compare ( Zed_utf8 . explode str1 ) str1 ( Zed_utf8 . explode str2 ) str2 module US = Zed_string0 . US0 module Buf = struct include Zed_string0 . Buf0 let add_char = add_uChar end end |
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 ) " else begin let byte1 = Char . code ( String . unsafe_get s ( i + 1 ) 1 ) 1 in if byte1 land 0xc0 != 0x80 then ( i , ulen , " malformed UTF8 sequence ) " else if ( ( Char . code ch land 0x1f ) 0x1f lsl 6 ) 6 lor ( byte1 land 0x3f ) 0x3f < 0x80 then ( i , ulen , " overlong UTF8 sequence ) " else main ( i + 2 ) 2 ( ulen + 1 ) 1 end | ' \ xe0 ' . . ' \ xef ' -> if i + 2 >= len then ( i , ulen , " premature end of UTF8 sequence ) " else begin let byte1 = Char . code ( String . unsafe_get s ( i + 1 ) 1 ) 1 and byte2 = Char . code ( String . unsafe_get s ( i + 2 ) 2 ) 2 in if byte1 land 0xc0 != 0x80 then ( i , ulen , " malformed UTF8 sequence ) " else if byte2 land 0xc0 != 0x80 then ( i , ulen , " malformed UTF8 sequence ) " else if ( ( Char . code ch land 0x0f ) 0x0f lsl 12 ) 12 lor ( ( byte1 land 0x3f ) 0x3f lsl 6 ) 6 lor ( byte2 land 0x3f ) 0x3f < 0x800 then ( i , ulen , " overlong UTF8 sequence ) " else main ( i + 3 ) 3 ( ulen + 1 ) 1 end | ' \ xf0 ' . . ' \ xf7 ' -> if i + 3 >= len then ( i , ulen , " premature end of UTF8 sequence ) " else begin let byte1 = Char . code ( String . unsafe_get s ( i + 1 ) 1 ) 1 and byte2 = Char . code ( String . unsafe_get s ( i + 2 ) 2 ) 2 and byte3 = Char . code ( String . unsafe_get s ( i + 3 ) 3 ) 3 in if byte1 land 0xc0 != 0x80 then ( i , ulen , " malformed UTF8 sequence ) " else if byte2 land 0xc0 != 0x80 then ( i , ulen , " malformed UTF8 sequence ) " else if byte3 land 0xc0 != 0x80 then ( i , ulen , " malformed UTF8 sequence ) " else if ( ( Char . code ch land 0x07 ) 0x07 lsl 18 ) 18 lor ( ( byte1 land 0x3f ) 0x3f lsl 12 ) 12 lor ( ( byte2 land 0x3f ) 0x3f lsl 6 ) 6 lor ( byte3 land 0x3f ) 0x3f < 0x10000 then ( i , ulen , " overlong UTF8 sequence ) " else main ( i + 4 ) 4 ( ulen + 1 ) 1 end | _ -> ( i , ulen , " invalid start of UTF8 sequence ) " in main i 0 |
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 " unterminated UTF - 8 sequence " else ofs + 3 | ' \ xf0 ' . . ' \ xf7 ' -> if ofs + 4 > String . length str then fail str ofs " unterminated UTF - 8 sequence " else ofs + 4 | _ -> fail str ofs " invalid start of UTF - 8 sequence " |
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 ( ofs - 3 ) 3 with | ' \ xe0 ' . . ' \ xef ' -> ofs - 3 | ' \ x80 ' . . ' \ xbf ' -> if ofs >= 4 then match String . unsafe_get str ( ofs - 4 ) 4 with | ' \ xf0 ' . . ' \ xf7 ' -> ofs - 4 | _ -> fail str ( ofs - 4 ) 4 " invalid start of UTF - 8 sequence " else fail str ( ofs - 3 ) 3 " invalid start of UTF - 8 string " | _ -> fail str ( ofs - 3 ) 3 " invalid middle of UTF - 8 sequence " else fail str ( ofs - 2 ) 2 " invaild start of UTF - 8 string " | _ -> fail str ( ofs - 2 ) 2 " invalid middle of UTF - 8 sequence " else fail str ( ofs - 1 ) 1 " invalid start of UTF - 8 string " | _ -> fail str ( ofs - 1 ) 1 " invalid end of UTF - 8 sequence " |
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 ( byte str ( ofs + 1 ) 1 land 0x3f ) 0x3f ) 0x3f | ' \ xe0 ' . . ' \ xef ' -> if ofs + 3 > String . length str then fail str ofs " unterminated UTF - 8 sequence " else UChar . of_int ( ( ( Char . code ch land 0x0f ) 0x0f lsl 12 ) 12 lor ( ( byte str ( ofs + 1 ) 1 land 0x3f ) 0x3f lsl 6 ) 6 lor ( byte str ( ofs + 2 ) 2 land 0x3f ) 0x3f ) 0x3f | ' \ xf0 ' . . ' \ xf7 ' -> if ofs + 4 > String . length str then fail str ofs " unterminated UTF - 8 sequence " else UChar . of_int ( ( ( Char . code ch land 0x07 ) 0x07 lsl 18 ) 18 lor ( ( byte str ( ofs + 1 ) 1 land 0x3f ) 0x3f lsl 12 ) 12 lor ( ( byte str ( ofs + 2 ) 2 land 0x3f ) 0x3f lsl 6 ) 6 lor ( byte str ( ofs + 3 ) 3 land 0x3f ) 0x3f ) 0x3f | _ -> fail str ofs " invalid start of UTF - 8 sequence " |
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 ) 0x1f lsl 6 ) 6 lor ( byte str ( ofs + 1 ) 1 land 0x3f ) 0x3f ) 0x3f , ofs + 2 ) 2 | ' \ xe0 ' . . ' \ xef ' -> if ofs + 3 > String . length str then fail str ofs " unterminated UTF - 8 sequence " else ( UChar . of_int ( ( ( Char . code ch land 0x0f ) 0x0f lsl 12 ) 12 lor ( ( byte str ( ofs + 1 ) 1 land 0x3f ) 0x3f lsl 6 ) 6 lor ( byte str ( ofs + 2 ) 2 land 0x3f ) 0x3f ) 0x3f , ofs + 3 ) 3 | ' \ xf0 ' . . ' \ xf7 ' -> if ofs + 4 > String . length str then fail str ofs " unterminated UTF - 8 sequence " else ( UChar . of_int ( ( ( Char . code ch land 0x07 ) 0x07 lsl 18 ) 18 lor ( ( byte str ( ofs + 1 ) 1 land 0x3f ) 0x3f lsl 12 ) 12 lor ( ( byte str ( ofs + 2 ) 2 land 0x3f ) 0x3f lsl 6 ) 6 lor ( byte str ( ofs + 3 ) 3 land 0x3f ) 0x3f ) 0x3f , ofs + 4 ) 4 | _ -> fail str ofs " invalid start of UTF - 8 sequence " |
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_int ( ( ( Char . code ch2 land 0x1f ) 0x1f lsl 6 ) 6 lor ( Char . code ch1 land 0x3f ) 0x3f ) 0x3f , ofs - 2 ) 2 | ' \ x80 ' . . ' \ xbf ' -> if ofs >= 3 then let ch3 = String . unsafe_get str ( ofs - 3 ) 3 in match ch3 with | ' \ xe0 ' . . ' \ xef ' -> ( UChar . of_int ( ( ( Char . code ch3 land 0x0f ) 0x0f lsl 12 ) 12 lor ( ( Char . code ch2 land 0x3f ) 0x3f lsl 6 ) 6 lor ( Char . code ch1 land 0x3f ) 0x3f ) 0x3f , ofs - 3 ) 3 | ' \ x80 ' . . ' \ xbf ' -> if ofs >= 4 then let ch4 = String . unsafe_get str ( ofs - 4 ) 4 in match ch4 with | ' \ xf0 ' . . ' \ xf7 ' -> ( UChar . of_int ( ( ( Char . code ch4 land 0x07 ) 0x07 lsl 18 ) 18 lor ( ( Char . code ch3 land 0x3f ) 0x3f lsl 12 ) 12 lor ( ( Char . code ch2 land 0x3f ) 0x3f lsl 6 ) 6 lor ( Char . code ch1 land 0x3f ) 0x3f ) 0x3f , ofs - 4 ) 4 | _ -> fail str ( ofs - 4 ) 4 " invalid start of UTF - 8 sequence " else fail str ( ofs - 3 ) 3 " invalid start of UTF - 8 string " | _ -> fail str ( ofs - 3 ) 3 " invalid middle of UTF - 8 sequence " else fail str ( ofs - 2 ) 2 " invaild start of UTF - 8 string " | _ -> fail str ( ofs - 2 ) 2 " invalid middle of UTF - 8 sequence " else fail str ( ofs - 1 ) 1 " invalid start of UTF - 8 string " | _ -> fail str ( ofs - 1 ) 1 " invalid end of UTF - 8 sequence " |
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 ) 0x80 ; s end else if code <= 0x10000 then begin let s = Bytes . create 3 in set_byte s 0 ( ( code lsr 12 ) 12 lor 0xe0 ) 0xe0 ; set_byte s 1 ( ( ( code lsr 6 ) 6 land 0x3f ) 0x3f lor 0x80 ) 0x80 ; set_byte s 2 ( ( code land 0x3f ) 0x3f lor 0x80 ) 0x80 ; s end else if code <= 0x10ffff then begin let s = Bytes . create 4 in set_byte s 0 ( ( code lsr 18 ) 18 lor 0xf0 ) 0xf0 ; set_byte s 1 ( ( ( code lsr 12 ) 12 land 0x3f ) 0x3f lor 0x80 ) 0x80 ; set_byte s 2 ( ( ( code lsr 6 ) 6 land 0x3f ) 0x3f lor 0x80 ) 0x80 ; set_byte s 3 ( ( code land 0x3f ) 0x3f lor 0x80 ) 0x80 ; s end else invalid_arg " Zed_utf8 . singleton " |
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 code2 in if d <> 0 then d else compare_rec str1 ofs1 str2 ofs2 |
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_string res |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.