diff options
author | Alain Frisch <alain@frisch.fr> | 2016-11-07 17:11:35 +0100 |
---|---|---|
committer | David Allsopp <david.allsopp@metastack.com> | 2016-11-07 16:11:35 +0000 |
commit | 69263a989313ab334f85f68e7c53ba2ff0f049f3 (patch) | |
tree | 77e4f22f3a356da8f3ef71c391e8680325182305 /stdlib/string.ml | |
parent | a02958960640cba8f8058a7c7ca04af99a988097 (diff) | |
download | ocaml-69263a989313ab334f85f68e7c53ba2ff0f049f3.tar.gz |
Option-returning variants of stdlib functions (#885)
Provide an xxx_opt alternative for functions raising Not_found
and many instances of Failure/Invalid_arg.
The only exception is the rarely used Buffer.add_substitute, where
the [Not_found] can really be interpreted as an error condition.
Most new functions are implemented directly (instead of wrapping the
raising version). This is for performance reasons and also to avoid
destroying the stacktrace (if the function is used in an exception
handler). One could instead implement the raising versions on top of
the new functions, but there might be a small penalty.
Diffstat (limited to 'stdlib/string.ml')
-rw-r--r-- | stdlib/string.ml | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/stdlib/string.ml b/stdlib/string.ml index 7fee8b1d27..17da8432ea 100644 --- a/stdlib/string.ml +++ b/stdlib/string.ml @@ -63,7 +63,7 @@ let rec unsafe_blits dst pos sep seplen = function let concat sep = function [] -> "" | l -> let seplen = length sep in bts @@ - unsafe_blits + unsafe_blits (B.create (sum_lengths 0 seplen l)) 0 sep seplen l @@ -105,12 +105,20 @@ let escaped s = let index s c = B.index (bos s) c +let index_opt s c = + B.index_opt (bos s) c let rindex s c = B.rindex (bos s) c +let rindex_opt s c = + B.rindex_opt (bos s) c let index_from s i c= B.index_from (bos s) i c +let index_from_opt s i c= + B.index_from_opt (bos s) i c let rindex_from s i c = B.rindex_from (bos s) i c +let rindex_from_opt s i c = + B.rindex_from_opt (bos s) i c let contains s c = B.contains (bos s) c let contains_from s i c = |