From b8b0cc3645c6766b2bd96e491f762496f7378da3 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 18 Dec 2014 14:05:12 +0100 Subject: [PATCH] more efficient CCString.init --- src/core/CCString.cppo.ml | 5 ++--- src/core/CCString.mli | 5 +++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/core/CCString.cppo.ml b/src/core/CCString.cppo.ml index 3948c8a2..0ac4cca7 100644 --- a/src/core/CCString.cppo.ml +++ b/src/core/CCString.cppo.ml @@ -62,9 +62,8 @@ let init = String.init #else let init n f = - let buf = Buffer.create n in - for i = 0 to n-1 do Buffer.add_char buf (f i) done; - Buffer.contents buf + let buf = Bytes.init n f in + Bytes.unsafe_to_string buf #endif diff --git a/src/core/CCString.mli b/src/core/CCString.mli index 307ff7c3..06dd50e8 100644 --- a/src/core/CCString.mli +++ b/src/core/CCString.mli @@ -66,6 +66,11 @@ val init : int -> (int -> char) -> string (** Analog to [Array.init]. @since 0.3.3 *) +(*$T + init 3 (fun i -> [|'a'; 'b'; 'c'|].(i)) = "abc" + init 0 (fun _ -> assert false) = "" +*) + val of_gen : char gen -> string val of_seq : char sequence -> string val of_klist : char klist -> string