add CCString.set for updating immutable strings

This commit is contained in:
Simon Cruanes 2015-05-25 00:04:48 +02:00
parent 8f33484dff
commit 0c624461d5
2 changed files with 16 additions and 0 deletions

View file

@ -281,6 +281,16 @@ let unlines l = String.concat "\n" l
let unlines_gen g = concat_gen ~sep:"\n" g
let set s i c =
if i<0 || i>= String.length s then invalid_arg "CCString.set";
init (String.length s) (fun j -> if i=j then c else s.[j])
(*$T
set "abcd" 1 '_' = "a_cd"
set "abcd" 0 '-' = "-bcd"
(try set "abc" 5 '_'; false with Invalid_argument _ -> true)
*)
let pp buf s =
Buffer.add_char buf '"';
Buffer.add_string buf s;

View file

@ -137,6 +137,12 @@ val unlines_gen : string gen -> string
Q.printable_string (fun s -> unlines (lines s) = s)
*)
val set : string -> int -> char -> string
(** [set s i c] creates a new string which is a copy of [s], except
for index [i], which becomes [c].
@raise Invalid_argument if [i] is an invalid index
@since NEXT_RELEASE *)
include S with type t := string
(** {2 Splitting} *)