Skip to content
This repository
tag: v334
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 264 lines (232 sloc) 6.714 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)

type t =
  | Leaf of int * string (* string, real string length *)
  | Node of int * t * t (* left, right, len *)

(* joke: who does want a Walker ? *)

(* start size for a Leaf, after that perform each realloc * 2 *)
let create hint = Leaf (0, String.create hint)

external length : t -> int = "%field0"

let concat t t' = Node ((length t) + (length t'), t, t')

let diverge = function
  | Leaf (i, s) -> Leaf (i, String.copy s)
  | Node (i, t, Leaf (j, s)) -> Node (i, t, Leaf (j, String.copy s))
  | t -> t

let s_alloc min (i:int) =
  Pervasives.min Sys.max_string_length (Pervasives.max min i)

let add_leaf e len_e s =
  let full_e = String.length e in
  let len_s = String.length s in
  let len_es = len_e + len_s in
  let rest_e = full_e - len_e in
  if len_s <= rest_e
  then (* s can be inserted in place *)
    let _ = String.unsafe_blit s 0 e len_e len_s in
    Leaf (len_es, e)
  else
    (* blit fully e, and allocate a new Leaf *)
    let _ = String.unsafe_blit s 0 e len_e rest_e in
    let rest_s = len_s - rest_e in
    let snew = String.create (s_alloc rest_s (2 * full_e)) in
    let _ = String.unsafe_blit s rest_e snew 0 rest_s in
    Node (len_es, Leaf (full_e, e), Leaf (rest_s, snew))

let add t s =
  match t with
  | Node (len_t, t, Leaf (len_e, e)) ->
      let t' = add_leaf e len_e s in
      Node (len_t + (String.length s), t, t')
  | Leaf (len_e, e) -> add_leaf e len_e s
  | _ ->
      let len_t = length t in
      let len_s = String.length s in
      Node (len_t + len_s, t, Leaf (len_s, s))

let addln t s = add (add t s) "\n"

let iter f t =
  let stack = Stack.create () in
  let push t = Stack.push t stack in
  let pop () =
    match Stack.pop stack with
    | Node (_, l, r) ->
        push r ;
        push l
    | Leaf (len_s, s) ->
        f s 0 len_s
  in
  try
    push t ;
    while true do
      pop ()
    done
  with
  | Stack.Empty -> ()

let fold f acc t =
  let stack = Stack.create () in
  let push t = Stack.push t stack in
  let acc = ref acc in
  let pop () =
    match Stack.pop stack with
    | Node (_, l, r) ->
        push r ;
        push l
    | Leaf (len_s, s) ->
        acc := f !acc s 0 len_s
  in
  try
    push t ;
    while true do
      pop ()
    done ;
    !acc
  with
  | Stack.Empty -> !acc

let rev_iter f t =
  let stack = Stack.create () in
  let push t = Stack.push t stack in
  let pop () =
    match Stack.pop stack with
    | Node (_, l, r) ->
        push l ;
        push r
    | Leaf (len_s, s) ->
        f s 0 len_s
  in
  try
    push t ;
    while true do
      pop ()
    done
  with
  | Stack.Empty -> ()

let rev_fold f acc t =
  let stack = Stack.create () in
  let push t = Stack.push t stack in
  let acc = ref acc in
  let pop () =
    match Stack.pop stack with
    | Node (_, l, r) ->
        push l ;
        push r
    | Leaf (len_s, s) ->
        acc := f !acc s 0 len_s
  in
  try
    push t ;
    while true do
      pop ()
    done ;
    !acc
  with
  | Stack.Empty -> !acc


let iter_sub f = iter (fun s start len -> f (String.sub s start len))
let fold_sub f = fold (fun acc s start len -> f acc (String.sub s start len))

let add_substring t s start len = add t (String.sub s start len)

let contents t =
  let len = length t in
  if len > Sys.max_string_length then invalid_arg "SRope.contents" else
  let s = String.create len in
  let push =
    let p = ref 0 in
    ( fun s' start len ->
        Base.String.unsafe_blit s' start s !p len ;
        p := !p + len ) in
  iter push t ;
  s

(* should be tail rec => Stack *)
let sub t start len =
  let s = String.create len in
  (* bliting from left to right *)
  let blit =
    let p = ref 0 in
    (fun src start len -> String.unsafe_blit src start s !p len ; p := !p + len)
  in
  let stack = Stack.create () in
  let push t = Stack.push t stack in
  let pop () =
    match Stack.pop stack with
    | t, start, len ->
        begin
          match t with
          | Leaf (len_s, s) ->
              if start + len > len_s then invalid_arg "SRope.sub"
              else blit s start len
          | Node (len_n, l, r) ->
              let stop = start + len in
              if start + len > len_n then invalid_arg "SRope.sub"
              else
                let a = length l in
                if stop < a
                then push (l, start, len)
                else
                  (* stop >= a *)
                  if start >= a
                  then push (r, (start - a), len)
                  else
                    (* should take from l and from r, Stack => push r first *)
                    let from_a = a - start in
                    ( push (r, 0, (len - from_a)) ;
                      push (l, start, from_a) )
        end
  in
  try
    push (t, start, len) ;
    while true do
      pop ()
    done ;
    s
  with
  | Stack.Empty -> s

let output oc = iter (Pervasives.output oc)

(* format *)
let make_formatter t_ref =
  Format.make_formatter
    (fun s start len -> t_ref := add_substring !t_ref s start len)
    (fun _ -> ())

let fmt fmt =
  let pp = fst (Format.pp_get_formatter_output_functions fmt ()) in
  iter pp

let printf t fmt =
  let t = ref t in
  let formatter = make_formatter t in
  Format.kfprintf (fun _ -> Format.pp_print_flush formatter (); !t) formatter fmt

let sprintf fmt =
  let t = ref (create 1024) in
  let formatter = make_formatter t in
  Format.kfprintf (fun _ -> Format.pp_print_flush formatter (); contents !t) formatter fmt

(* dynamic choice of impl *)
let implementation =
  { FBuffer.
    create = create ;
    add = add ;
    addln = addln ;
    concat = concat ;
    add_substring = add_substring ;
    diverge = diverge ;
    contents = contents ;
    output = output ;
    length = length ;
    sub = sub ;
    iter = iter ;
    fold = fold ;
    rev_iter = rev_iter ;
    rev_fold = rev_fold ;
    iter_sub = iter_sub ;
    fold_sub = fold_sub ;
    fmt = fmt ;
    printf = printf ;
    sprintf = sprintf ;
  }


(* deprecated, backward compat *)
let make ?name:_ i = create i
let union = concat
let write t oc = output oc t
Something went wrong with that request. Please try again.