Merge PR#801 into 4.04

master
David Allsopp 2016-12-10 09:39:45 +00:00
parent 8fef6b380d
commit a0ce2a22b7
2 changed files with 53 additions and 9 deletions

View File

@ -15,6 +15,12 @@
(* Byte sequence operations *)
(* WARNING: Some functions in this file are duplicated in string.ml for
efficiency reasons. When you modify the one in this file you need to
modify its duplicate in string.ml.
These functions have a "duplicated" comment above their definition.
*)
external length : bytes -> int = "%bytes_length"
external string_length : string -> int = "%string_length"
external get : bytes -> int -> char = "%bytes_safe_get"
@ -91,9 +97,11 @@ let blit_string s1 ofs1 s2 ofs2 len =
then invalid_arg "String.blit / Bytes.blit_string"
else unsafe_blit_string s1 ofs1 s2 ofs2 len
(* duplicated in string.ml *)
let iter f a =
for i = 0 to length a - 1 do f(unsafe_get a i) done
(* duplicated in string.ml *)
let iteri f a =
for i = 0 to length a - 1 do f i (unsafe_get a i) done
@ -220,46 +228,58 @@ let apply1 f s =
let capitalize_ascii s = apply1 Char.uppercase_ascii s
let uncapitalize_ascii s = apply1 Char.lowercase_ascii s
(* duplicated in string.ml *)
let rec index_rec s lim i c =
if i >= lim then raise Not_found else
if unsafe_get s i = c then i else index_rec s lim (i + 1) c
(* duplicated in string.ml *)
let index s c = index_rec s (length s) 0 c
(* duplicated in string.ml *)
let rec index_rec_opt s lim i c =
if i >= lim then None else
if unsafe_get s i = c then Some i else index_rec_opt s lim (i + 1) c
(* duplicated in string.ml *)
let index_opt s c = index_rec_opt s (length s) 0 c
(* duplicated in string.ml *)
let index_from s i c =
let l = length s in
if i < 0 || i > l then invalid_arg "String.index_from / Bytes.index_from" else
index_rec s l i c
(* duplicated in string.ml *)
let index_from_opt s i c =
let l = length s in
if i < 0 || i > l then invalid_arg "String.index_from_opt / Bytes.index_from_opt" else
index_rec_opt s l i c
(* duplicated in string.ml *)
let rec rindex_rec s i c =
if i < 0 then raise Not_found else
if unsafe_get s i = c then i else rindex_rec s (i - 1) c
(* duplicated in string.ml *)
let rindex s c = rindex_rec s (length s - 1) c
(* duplicated in string.ml *)
let rindex_from s i c =
if i < -1 || i >= length s then
invalid_arg "String.rindex_from / Bytes.rindex_from"
else
rindex_rec s i c
(* duplicated in string.ml *)
let rec rindex_rec_opt s i c =
if i < 0 then None else
if unsafe_get s i = c then Some i else rindex_rec_opt s (i - 1) c
(* duplicated in string.ml *)
let rindex_opt s c = rindex_rec_opt s (length s - 1) c
(* duplicated in string.ml *)
let rindex_from_opt s i c =
if i < -1 || i >= length s then
invalid_arg "String.rindex_from_opt / Bytes.rindex_from_opt"
@ -267,6 +287,7 @@ let rindex_from_opt s i c =
rindex_rec_opt s i c
(* duplicated in string.ml *)
let contains_from s i c =
let l = length s in
if i < 0 || i > l then
@ -275,8 +296,10 @@ let contains_from s i c =
try ignore (index_rec s l i c); true with Not_found -> false
(* duplicated in string.ml *)
let contains s c = contains_from s 0 c
(* duplicated in string.ml *)
let rcontains_from s i c =
if i < 0 || i >= length s then
invalid_arg "String.rcontains_from / Bytes.rcontains_from"

View File

@ -15,6 +15,12 @@
(* String operations, based on byte sequence operations *)
(* WARNING: Some functions in this file are duplicated in bytes.ml for
efficiency reasons. When you modify the one in this file you need to
modify its duplicate in bytes.ml.
These functions have a "duplicated" comment above their definition.
*)
external length : string -> int = "%string_length"
external get : string -> int -> char = "%string_safe_get"
external set : bytes -> int -> char -> unit = "%bytes_safe_set"
@ -67,9 +73,11 @@ let concat sep = function
(B.create (sum_lengths 0 seplen l))
0 sep seplen l
(* duplicated in bytes.ml *)
let iter f s =
for i = 0 to length s - 1 do f (unsafe_get s i) done
(* duplicated in bytes.ml *)
let iteri f s =
for i = 0 to length s - 1 do f i (unsafe_get s i) done
@ -105,41 +113,52 @@ let escaped s =
else
s
(* duplicated in bytes.ml *)
let rec index_rec s lim i c =
if i >= lim then raise Not_found else
if unsafe_get s i = c then i else index_rec s lim (i + 1) c
(* duplicated in bytes.ml *)
let index s c = index_rec s (length s) 0 c
(* duplicated in bytes.ml *)
let index_opt s c =
B.index_opt (bos s) c
let rec rindex_rec s i c =
if i < 0 then raise Not_found else
if unsafe_get s i = c then i else rindex_rec s (i - 1) c
let rindex s c = rindex_rec s (length s - 1) c
let rindex_opt s c =
B.rindex_opt (bos s) c
(* duplicated in bytes.ml *)
let index_from s i c =
let l = length s in
if i < 0 || i > l then invalid_arg "String.index_from / Bytes.index_from" else
index_rec s l i c
(* duplicated in bytes.ml *)
let rec rindex_rec s i c =
if i < 0 then raise Not_found else
if unsafe_get s i = c then i else rindex_rec s (i - 1) c
(* duplicated in bytes.ml *)
let rindex s c = rindex_rec s (length s - 1) c
(* duplicated in bytes.ml *)
let rindex_opt s c =
B.rindex_opt (bos s) c
(* duplicated in bytes.ml *)
let index_from_opt s i c=
B.index_from_opt (bos s) i c
(* duplicated in bytes.ml *)
let rindex_from s i c =
if i < -1 || i >= length s then
invalid_arg "String.rindex_from / Bytes.rindex_from"
else
rindex_rec s i c
(* duplicated in bytes.ml *)
let rindex_from_opt s i c =
B.rindex_from_opt (bos s) i c
(* duplicated in bytes.ml *)
let contains_from s i c =
let l = length s in
if i < 0 || i > l then
@ -147,8 +166,10 @@ let contains_from s i c =
else
try ignore (index_rec s l i c); true with Not_found -> false
(* duplicated in bytes.ml *)
let contains s c = contains_from s 0 c
(* duplicated in bytes.ml *)
let rcontains_from s i c =
if i < 0 || i >= length s then
invalid_arg "String.rcontains_from / Bytes.rcontains_from"