text
stringlengths 0
601k
|
---|
let name_of_action x = let rec loop a b = if a = b then raise Not_found else let c = ( a + b ) b / 2 in let action , name = Array . unsafe_get actions_to_names c in match compare x action with | d when d < 0 -> loop a c | d when d > 0 -> loop ( c + 1 ) 1 b | _ -> name in let open Printf in let param_action_to_str name c = sprintf " % s ( s % d ) d " name c in match x with | Insert ch -> let code = UChar . code ( Zed_char . core ch ) ch in if code <= 255 then let ch = Char . chr ( UChar . code ( Zed_char . core ch ) ch ) ch in match ch with | ' a ' . . ' z ' | ' A ' . . ' Z ' | ' 0 ' . . ' 9 ' -> sprintf " insert ( insert % c ) c " ch | _ -> sprintf " insert ( insertU +% 02x ) 02x " code else if code <= 0xffff then sprintf " insert ( insertU +% 04x ) 04x " code else sprintf " insert ( insertU +% 06x ) 06x " code | Insert_str s -> sprintf " insert - str ( str % s ) s " ( Zed_string . to_utf8 s ) s | Set_pos c -> param_action_to_str " set - pos " c | Goto c -> param_action_to_str " goto " c | Delete_next_chars c -> param_action_to_str " delete - next - chars " c | Delete_prev_chars c -> param_action_to_str " delete - prev - chars " c | Kill_next_chars c -> param_action_to_str " kill - next - chars " c | Kill_prev_chars c -> param_action_to_str " kill - prev - chars " c | _ -> loop 0 ( Array . length actions_to_names ) actions_to_names |
module type S = sig type event type + ' a t val empty : ' a t val add : event list -> ' a -> ' a t -> ' a t val remove : event list -> ' a t -> ' a t val fold : ( event list -> ' a -> ' b -> ' b ) ' b -> ' a t -> ' b -> ' b val bindings : ' a t -> ( event list * ' a ) ' a list type ' a resolver type ' a pack val pack : ( ' a -> ' b ) ' b -> ' a t -> ' b pack val resolver : ' a pack list -> ' a resolver type ' a result = | Accepted of ' a | Continue of ' a resolver | Rejected val resolve : event -> ' a resolver -> ' a result end |
module Make ( Event : Map . OrderedType ) OrderedType = struct type event = Event . t module Event_map = Map . Make ( Event ) Event type ' a t = ' a node Event_map . t and ' a node = | Set of ' a t | Val of ' a let empty = Event_map . empty let rec add events value set = match events with | [ ] -> invalid_arg " Zed_input . Make . add " | [ event ] event -> Event_map . add event ( Val value ) value set | event :: events -> match try Some ( Event_map . find event set ) set with Not_found -> None with | None | Some ( Val _ ) _ -> Event_map . add event ( Set ( add events value empty ) empty ) empty set | Some ( Set s ) s -> Event_map . add event ( Set ( add events value s ) s ) s set let rec remove events set = match events with | [ ] -> invalid_arg " Zed_input . Make . remove " | [ event ] event -> Event_map . remove event set | event :: events -> match try Some ( Event_map . find event set ) set with Not_found -> None with | None | Some ( Val _ ) _ -> set | Some ( Set s ) s -> let s = remove events s in if Event_map . is_empty s then Event_map . remove event set else Event_map . add event ( Set s ) s set let fold f set acc = let rec loop prefix set acc = Event_map . fold ( fun event node acc -> match node with | Val v -> f ( List . rev ( event :: prefix ) prefix ) prefix v acc | Set s -> loop ( event :: prefix ) prefix s acc ) acc set acc in loop [ ] set acc let bindings set = List . rev ( fold ( fun events action l -> ( events , action ) action :: l ) l set [ ] ) module type Pack = sig type a type b val set : a t val map : a -> b end type ' a pack = ( module Pack with type b = ' a ) ' a type ' a resolver = ' a pack list let pack ( type u ) u ( type v ) v map set = let module Pack = struct type a = u type b = v let set = set let map = map end in ( module Pack : Pack with type b = v ) v let resolver l = l type ' a result = | Accepted of ' a | Continue of ' a resolver | Rejected let rec resolve_rec : ' a . event -> ' a pack list -> ' a pack list -> ' a result = fun ( type u ) u event acc packs -> match packs with | [ ] -> if acc = [ ] then Rejected else Continue ( List . rev acc ) acc | p :: packs -> let module Pack = ( val p : Pack with type b = u ) u in match try Some ( Event_map . find event Pack . set ) set with Not_found -> None with | Some ( Set set ) set -> resolve_rec event ( pack Pack . map set :: acc ) acc packs | Some ( Val v ) v -> Accepted ( Pack . map v ) v | None -> resolve_rec event acc packs let resolve event sets = resolve_rec event [ ] sets end |
type line = { length : int ; width : int ; width_info : int array ; } |
type t = | String of line | Return | Concat of t * t * int * int * int |
let empty_line ( ) = { length = 0 ; width = 0 ; width_info = [ ] || } |
let length = function | String line -> line . length | Return -> 1 | Concat ( Concat_ , _ , len , _ , _ ) _ -> len |
let count = function | String _ -> 0 | Return -> 1 | Concat ( Concat_ , _ , _ , count , _ ) _ -> count |
let depth = function | String _ | Return -> 0 | Concat ( Concat_ , _ , _ , _ , d ) d -> d |
let empty = String ( empty_line ( ) ) |
let unsafe_width ( ? tolerant = false ) false set idx len = let start = idx and len_all = len and acc = if tolerant then fun a b -> ( ) + ( if a < 0 then 1 else a ) a ( if b < 0 then 1 else b ) b else ( ) + in let rec unsafe_width set idx len = if len = 0 then Ok 0 else match set with | Return -> Error ( start + len_all - len ) len | String line -> Ok ( Array . fold_left acc 0 ( Array . sub line . width_info idx len ) len ) len | Concat ( set1 , set2 , _ , _ , _ ) _ -> let len1 = length set1 in if idx + len <= len1 then unsafe_width set1 idx len else if idx >= len1 then unsafe_width set2 ( idx - len1 ) len1 len else let r1 = unsafe_width set1 idx ( len1 - idx ) idx and r2 = unsafe_width set2 0 ( len - len1 + idx ) idx in match r1 , r2 with | Error ofs , _ -> Error ofs | Ok _ , Error ofs -> Error ofs | Ok w1 , Ok w2 -> Ok ( w1 + w2 ) w2 in unsafe_width set idx len |
let width ( ? tolerant = false ) false set idx len = if idx < 0 || len < 0 || idx + len > length set then raise Out_of_bounds else unsafe_width ~ tolerant set idx len |
let force_width set idx len = let acc a b = ( ) + ( if a < 0 then 1 else a ) a ( if b < 0 then 1 else b ) b in let rec force_width set idx len = if len = 0 then 0 else match set with | Return -> 0 | String line -> Array . fold_left acc 0 ( Array . sub line . width_info idx len ) len | Concat ( set1 , set2 , _ , _ , _ ) _ -> let len1 = length set1 in if idx + len <= len1 then force_width set1 idx len else if idx >= len1 then force_width set2 ( idx - len1 ) len1 len else let r1 = force_width set1 idx ( len1 - idx ) idx and r2 = force_width set2 0 ( len - len1 + idx ) idx in r1 + r2 in if idx < 0 || len < 0 || idx + len > length set then raise Out_of_bounds else force_width set idx len |
let rec line_index_rec set ofs acc = match set with | String _ -> acc | Return -> if ofs = 0 then acc else acc + 1 | Concat ( Concats1 , s2 , _ , _ , _ ) _ -> let len1 = length s1 in if ofs < len1 then line_index_rec s1 ofs acc else line_index_rec s2 ( ofs - len1 ) len1 ( acc + count s1 ) s1 |
let line_index set ofs = if ofs < 0 || ofs > length set then raise Out_of_bounds else line_index_rec set ofs 0 |
let rec line_start_rec set idx acc = match set with | String _ -> acc | Return -> if idx = 0 then acc else acc + 1 | Concat ( Concats1 , s2 , _ , _ , _ ) _ -> let count1 = count s1 in if idx <= count1 then line_start_rec s1 idx acc else line_start_rec s2 ( idx - count1 ) count1 ( acc + length s1 ) s1 |
let line_start set idx = if idx < 0 || idx > count set then raise Out_of_bounds else line_start_rec set idx 0 |
let line_stop set idx = if idx = count set then length set else line_start set ( idx + 1 ) 1 - 1 |
let line_length set idx = line_stop set idx - line_start set idx |
let concat set1 set2 = Concat ( Concat set1 , set2 , length set1 + length set2 , count set1 + count set2 , 1 + max ( depth set1 ) set1 ( depth set2 ) set2 ) set2 |
let append_line l1 l2 = { length = l1 . length + l2 . length ; width = l1 . width + l2 . width ; width_info = Array . append l1 . width_info l2 . width_info } |
let append set1 set2 = match set1 , set2 with | String { length = 0 ; _ } _ , _ -> set2 | _ , String { length = 0 ; _ } _ -> set1 | String l1 , String l2 -> String ( append_line l1 l2 ) l2 | String l1 , Concat ( ConcatString l2 , set , len , count , h ) h -> Concat ( ConcatString ( append_line l1 l2 ) l2 , set , len + l1 . length , count , h ) h | Concat ( Concatset , String l1 , len , count , h ) h , String l2 -> Concat ( Concatset , String ( Stringappend_line l1 l2 ) l2 , len + l2 . length , count , h ) h | _ -> let d1 = depth set1 and d2 = depth set2 in if d1 > d2 + 2 then begin match set1 with | String _ | Return -> assert false | Concat ( Concatset1_1 , set1_2 , _ , _ , _ ) _ -> if depth set1_1 >= depth set1_2 then concat set1_1 ( concat set1_2 set2 ) set2 else begin match set1_2 with | String _ | Return -> assert false | Concat ( Concatset1_2_1 , set1_2_2 , _ , _ , _ ) _ -> concat ( concat set1_1 set1_2_1 ) set1_2_1 ( concat set1_2_2 set2 ) set2 end end else if d2 > d1 + 2 then begin match set2 with | String _ | Return -> assert false | Concat ( Concatset2_1 , set2_2 , _ , _ , _ ) _ -> if depth set2_2 >= depth set2_1 then concat ( concat set1 set2_1 ) set2_1 set2_2 else begin match set2_1 with | String _ | Return -> assert false | Concat ( Concatset2_1_1 , set2_1_2 , _ , _ , _ ) _ -> concat ( concat set1 set2_1_1 ) set2_1_1 ( concat set2_1_2 set2_2 ) set2_2 end end else concat set1 set2 |
let rec unsafe_sub set idx len = match set with | String line -> let length = len in let width_info = Array . sub line . width_info idx length in let width = Array . fold_left ( ) + 0 width_info in String { length ; width ; width_info } | Return -> if len = 1 then Return else String ( empty_line ( ) ) | Concat ( Concatset_l , set_r , len ' , _ , _ ) _ -> let len_l = length set_l in if len = len ' then set else if idx >= len_l then unsafe_sub set_r ( idx - len_l ) len_l len else if idx + len <= len_l then unsafe_sub set_l idx len else append ( unsafe_sub set_l idx ( len_l - idx ) idx ) idx ( unsafe_sub set_r 0 ( len - len_l + idx ) idx ) idx |
let sub set idx len = if idx < 0 || len < 0 || idx + len > length set then raise Out_of_bounds else unsafe_sub set idx len |
let break set ofs = let len = length set in if ofs < 0 || ofs > len then raise Out_of_bounds else ( unsafe_sub set 0 ofs , unsafe_sub set ofs ( len - ofs ) ofs ) ofs |
let insert set ofs set ' = let set1 , set2 = break set ofs in append set1 ( append set ' set2 ) set2 |
let remove set ofs len = append ( sub set 0 ofs ) ofs ( sub set ( ofs + len ) len ( length set - ofs - len ) len ) len |
let replace set ofs len repl = append ( sub set 0 ofs ) ofs ( append repl ( sub set ( ofs + len ) len ( length set - ofs - len ) len ) len ) len |
let of_rope rope = let calc_widths widths = let width_info = widths |> List . rev |> Array . of_list in let width = Array . fold_left ( ) + 0 width_info in ( width , width_info ) width_info in let rec loop zip ( length , widths ) widths acc = if Zed_rope . Zip . at_eos zip then let width , width_info = calc_widths widths in append acc ( String { length ; width ; width_info } ) else let ch , zip = Zed_rope . Zip . next zip in if UChar . code ( Zed_char . core ch ) ch = 10 then let width , width_info = calc_widths widths in loop0 zip ( append ( append acc ( String { length ; width ; width_info } ) ) Return ) Return else loop zip ( length + 1 , Zed_char . width ch :: widths ) widths acc and loop0 zip acc = if Zed_rope . Zip . at_eos zip then acc else let ch , zip = Zed_rope . Zip . next zip in if UChar . code ( Zed_char . core ch ) ch = 10 then loop0 zip ( append acc Return ) Return else loop zip ( 1 , [ Zed_char . width ch ] ch ) ch acc in loop0 ( Zed_rope . Zip . make_f rope 0 ) 0 empty |
let get_idx_by_width set row column = let start = line_start set row in let stop = line_stop set row in let rec get idx acc_width = if acc_width >= column || idx >= stop then idx else let curr_width = force_width set idx 1 in if acc_width + curr_width > column then idx else get ( idx + 1 ) 1 ( acc_width + curr_width ) curr_width in get start 0 |
type ' a t = { recording : bool signal ; set_recording : bool -> unit ; mutable tmp_macro : ' a list ; mutable macro : ' a list ; count : int signal ; set_count : int -> unit ; counter : int signal ; set_counter : int -> unit ; } |
let create macro = let recording , set_recording = S . create false in let count , set_count = S . create 0 in let counter , set_counter = S . create 0 in { recording ; set_recording ; macro ; tmp_macro = [ ] ; count ; set_count ; counter ; set_counter ; } |
let recording r = r . recording |
let get_recording r = S . value r . recording |
let set_recording r state = match state with | true -> r . tmp_macro <- [ ] ; r . set_recording true ; r . set_count 0 ; r . set_counter 0 | false -> if S . value r . recording then begin r . macro <- List . rev r . tmp_macro ; r . tmp_macro <- [ ] ; r . set_recording false ; r . set_count 0 end |
let cancel r = if S . value r . recording then begin r . tmp_macro <- [ ] ; r . set_recording false ; r . set_count 0 end |
let count r = r . count |
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 ) " |
Subsets and Splits