avoid to from bytes conversion for some string operations: such conversion is expensive in Backends which has different representation for string and bytes

master
Hongbo Zhang 2016-09-09 13:31:42 -04:00 committed by David Allsopp
parent 21a8eab0b7
commit 8fef6b380d
1 changed files with 42 additions and 13 deletions

View File

@ -68,9 +68,11 @@ let concat sep = function
0 sep seplen l
let iter f s =
B.iter f (bos s)
for i = 0 to length s - 1 do f (unsafe_get s i) done
let iteri f s =
B.iteri f (bos s)
for i = 0 to length s - 1 do f i (unsafe_get s i) done
let map f s =
B.map f (bos s) |> bts
let mapi f s =
@ -103,28 +105,55 @@ let escaped s =
else
s
let index s c =
B.index (bos s) c
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
let index s c = index_rec s (length s) 0 c
let index_opt s c =
B.index_opt (bos s) c
let rindex s c =
B.rindex (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
let index_from s i c=
B.index_from (bos s) i c
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
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
if i < -1 || i >= length s then
invalid_arg "String.rindex_from / Bytes.rindex_from"
else
rindex_rec 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 =
B.contains_from (bos s) i c
let l = length s in
if i < 0 || i > l then
invalid_arg "String.contains_from / Bytes.contains_from"
else
try ignore (index_rec s l i c); true with Not_found -> false
let contains s c = contains_from s 0 c
let rcontains_from s i c =
B.rcontains_from (bos s) i c
if i < 0 || i >= length s then
invalid_arg "String.rcontains_from / Bytes.rcontains_from"
else
try ignore (rindex_rec s i c); true with Not_found -> false
let uppercase_ascii s =
B.uppercase_ascii (bos s) |> bts