From 32fb4cf0442c79556538b487a8d5f672e6b92520 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sun, 27 Sep 2015 17:58:44 +0200 Subject: [PATCH] raise exception in `CCString.replace` if `sub=""` --- src/core/CCString.cppo.ml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/core/CCString.cppo.ml b/src/core/CCString.cppo.ml index eb6c33db..bf48b58a 100644 --- a/src/core/CCString.cppo.ml +++ b/src/core/CCString.cppo.ml @@ -81,7 +81,7 @@ let _is_sub ~sub i s j ~len = let rec check k = if k = len then true - else sub.[i + k] = s.[j+k] && check (k+1) + else sub.[i+k] = s.[j+k] && check (k+1) in j+len <= String.length s && check 0 @@ -124,7 +124,9 @@ let replace_at_ ~pos ~len ~by s = Buffer.add_substring b s (pos+len) (String.length s - pos - len); Buffer.contents b -let replace ?(which=`All) ~sub ~by s = match which with +let replace ?(which=`All) ~sub ~by s = + if sub="" then invalid_arg "CCstring.replace"; + match which with | `Left -> let i = find ~sub s in if i>=0 then replace_at_ ~pos:i ~len:(String.length sub) ~by s else s