more tweaks and benchmarks for CCString.{prefix,suffix}

This commit is contained in:
Simon Cruanes 2017-09-12 10:55:24 +02:00
parent ff469211af
commit 7405c1c346
2 changed files with 27 additions and 24 deletions

View file

@ -1178,7 +1178,7 @@ module Str = struct
let bench_rfind = bench_find_ ~dir:`Reverse let bench_rfind = bench_find_ ~dir:`Reverse
module Pre = struct module Pre = struct
let prefix_pure ~pre s = let prefix_rec ~pre s =
let rec same s1 s2 i = let rec same s1 s2 i =
if i = String.length s1 then true if i = String.length s1 then true
else ( else (
@ -1200,7 +1200,7 @@ module Str = struct
exception Exit_false exception Exit_false
let prefix_loop ~pre s = let prefix_for_exn ~pre s =
String.length pre <= String.length s && String.length pre <= String.length s &&
try try
for i=0 to String.length pre-1 do for i=0 to String.length pre-1 do
@ -1249,7 +1249,7 @@ module Str = struct
in in
let output = let output =
Array.map Array.map
(fun (pre, str) -> prefix_pure ~pre str) (fun (pre, str) -> prefix_rec ~pre str)
input input
in in
let test f () = let test f () =
@ -1263,8 +1263,8 @@ module Str = struct
[ [
"containers", test CCString.prefix, (); "containers", test CCString.prefix, ();
"while_unsafe", test prefix_while, (); "while_unsafe", test prefix_while, ();
"loop_unsafe", test prefix_pure, (); "rec_unsafe", test prefix_rec, ();
"for_unsafe", test prefix_loop, (); "for_exn_unsafe", test prefix_for_exn, ();
"sub_eq", test prefix_sub, (); "sub_eq", test prefix_sub, ();
"bat_prefix", test bat_prefix, (); "bat_prefix", test bat_prefix, ();
] ]
@ -1301,6 +1301,7 @@ module Str = struct
"prefix" @>>> "prefix" @>>>
[ "max_len:1000,max_pre_len:15" @>> app_ints (Pre.make ~max_len:1000 ~max_len_prefix:15) [100; 1_000]; [ "max_len:1000,max_pre_len:15" @>> app_ints (Pre.make ~max_len:1000 ~max_len_prefix:15) [100; 1_000];
"max_len:1000,max_pre_len:100" @>> app_ints (Pre.make ~max_len:1000 ~max_len_prefix:100) [100; 1_000]; "max_len:1000,max_pre_len:100" @>> app_ints (Pre.make ~max_len:1000 ~max_len_prefix:100) [100; 1_000];
"max_len:1000,max_pre_len:300" @>> app_ints (Pre.make ~max_len:1000 ~max_len_prefix:300) [100; 1_000];
] ]
]) ])

View file

@ -490,28 +490,30 @@ let repeat s n =
assert(len > 0); assert(len > 0);
init (len * n) (fun i -> s.[i mod len]) init (len * n) (fun i -> s.[i mod len])
exception Exit_false
let prefix ~pre s = let prefix ~pre s =
String.length pre <= String.length s && let len = String.length pre in
try if len > String.length s then false
for i=0 to String.length pre-1 do else (
if String.unsafe_get s i != String.unsafe_get pre i let rec check i =
then raise Exit_false if i=len then true
done; else if String.unsafe_get s i != String.unsafe_get pre i then false
true else check (i+1)
with Exit_false -> false in
check 0
)
let suffix ~suf s = let suffix ~suf s =
String.length suf <= String.length s && let len = String.length suf in
try if len > String.length s then false
let off = String.length s - String.length suf in else (
for i=0 to String.length suf-1 do let off = String.length s - len in
if String.unsafe_get s (off+i) != String.unsafe_get suf i let rec check i =
then raise Exit_false if i=len then true
done; else if String.unsafe_get s (off+i) != String.unsafe_get suf i then false
true else check (i+1)
with Exit_false -> false in
check 0
)
let take n s = let take n s =
if n < String.length s if n < String.length s