mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-12 14:00:33 -05:00
use Buffer whenever string mutation is needed
This commit is contained in:
parent
ad705fd758
commit
786334dcce
3 changed files with 21 additions and 30 deletions
|
|
@ -56,9 +56,9 @@ let compare = String.compare
|
||||||
let hash s = Hashtbl.hash s
|
let hash s = Hashtbl.hash s
|
||||||
|
|
||||||
let init n f =
|
let init n f =
|
||||||
let s = String.make n ' ' in
|
let buf = Buffer.create n in
|
||||||
for i = 0 to n-1 do s.[i] <- f i done;
|
for i = 0 to n-1 do Buffer.add_char buf (f i) done;
|
||||||
s
|
Buffer.contents buf
|
||||||
|
|
||||||
let length = String.length
|
let length = String.length
|
||||||
|
|
||||||
|
|
@ -168,11 +168,7 @@ let repeat s n =
|
||||||
assert (n>=0);
|
assert (n>=0);
|
||||||
let len = String.length s in
|
let len = String.length s in
|
||||||
assert(len > 0);
|
assert(len > 0);
|
||||||
let buf = String.create (len * n) in
|
init (len * n) (fun i -> s.[i mod len])
|
||||||
for i = 0 to n-1 do
|
|
||||||
String.blit s 0 buf (i * len) len;
|
|
||||||
done;
|
|
||||||
buf
|
|
||||||
|
|
||||||
let prefix ~pre s =
|
let prefix ~pre s =
|
||||||
String.length pre <= String.length s &&
|
String.length pre <= String.length s &&
|
||||||
|
|
@ -213,26 +209,23 @@ let rec _to_klist s i len () =
|
||||||
else `Cons (s.[i], _to_klist s (i+1)(len-1))
|
else `Cons (s.[i], _to_klist s (i+1)(len-1))
|
||||||
|
|
||||||
let of_klist l =
|
let of_klist l =
|
||||||
let rec aux acc n l = match l() with
|
let b = Buffer.create 15 in
|
||||||
|
let rec aux l = match l() with
|
||||||
| `Nil ->
|
| `Nil ->
|
||||||
let s = String.create n in
|
Buffer.contents b
|
||||||
let acc = ref acc in
|
| `Cons (x,l') ->
|
||||||
for i=n-1 downto 0 do
|
Buffer.add_char b x;
|
||||||
s.[i] <- List.hd !acc;
|
aux l'
|
||||||
acc := List.tl !acc
|
in aux l
|
||||||
done;
|
|
||||||
s
|
|
||||||
| `Cons (x,l') -> aux (x::acc) (n+1) l'
|
|
||||||
in aux [] 0 l
|
|
||||||
|
|
||||||
let to_klist s = _to_klist s 0 (String.length s)
|
let to_klist s = _to_klist s 0 (String.length s)
|
||||||
|
|
||||||
let to_list s = _to_list s [] 0 (String.length s)
|
let to_list s = _to_list s [] 0 (String.length s)
|
||||||
|
|
||||||
let of_list l =
|
let of_list l =
|
||||||
let s = String.make (List.length l) ' ' in
|
let buf = Buffer.create (List.length l) in
|
||||||
List.iteri (fun i c -> s.[i] <- c) l;
|
List.iter (Buffer.add_char buf) l;
|
||||||
s
|
Buffer.contents buf
|
||||||
|
|
||||||
(*$T
|
(*$T
|
||||||
of_list ['a'; 'b'; 'c'] = "abc"
|
of_list ['a'; 'b'; 'c'] = "abc"
|
||||||
|
|
@ -240,9 +233,7 @@ let of_list l =
|
||||||
*)
|
*)
|
||||||
|
|
||||||
let of_array a =
|
let of_array a =
|
||||||
let s = String.make (Array.length a) ' ' in
|
init (Array.length a) (fun i -> a.(i))
|
||||||
Array.iteri (fun i c -> s.[i] <- c) a;
|
|
||||||
s
|
|
||||||
|
|
||||||
let to_array s =
|
let to_array s =
|
||||||
Array.init (String.length s) (fun i -> s.[i])
|
Array.init (String.length s) (fun i -> s.[i])
|
||||||
|
|
|
||||||
|
|
@ -535,9 +535,9 @@ module String = Make(struct
|
||||||
let compare = Char.compare
|
let compare = Char.compare
|
||||||
let to_seq s k = String.iter k s
|
let to_seq s k = String.iter k s
|
||||||
let of_list l =
|
let of_list l =
|
||||||
let s = String.create (List.length l) in
|
let buf = Buffer.create (List.length l) in
|
||||||
List.iteri (fun i c -> s.[i] <- c) l;
|
List.iter (fun c -> Buffer.add_char buf c) l;
|
||||||
s
|
Buffer.contents buf
|
||||||
end)
|
end)
|
||||||
|
|
||||||
(*$T
|
(*$T
|
||||||
|
|
|
||||||
|
|
@ -643,9 +643,9 @@ include Make(struct
|
||||||
let length = String.length
|
let length = String.length
|
||||||
let get = String.get
|
let get = String.get
|
||||||
let of_list l =
|
let of_list l =
|
||||||
let s = String.make (List.length l) ' ' in
|
let buf = Buffer.create (List.length l) in
|
||||||
List.iteri (fun i c -> s.[i] <- c) l;
|
List.iter (fun c -> Buffer.add_char buf c) l;
|
||||||
s
|
Buffer.contents buf
|
||||||
end)
|
end)
|
||||||
|
|
||||||
let debug_print = debug_print output_char
|
let debug_print = debug_print output_char
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue