Skip to content

Commit

Permalink
sync stdlib to 4.05 (thx test/std.ml)
Browse files Browse the repository at this point in the history
  • Loading branch information
ygrek committed Mar 29, 2017
1 parent 5536077 commit f013ba2
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/extBuffer.mli
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,10 @@ val add_channel : t -> in_channel -> int -> unit

val output_buffer : out_channel -> t -> unit

#ifdef OCAML4_05

val truncate : t -> int -> unit

#endif

end
32 changes: 31 additions & 1 deletion src/extList.ml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ let nth l index =
if index < 0 then raise (Invalid_index index);
let rec loop n = function
| [] -> raise (Invalid_index index);
| h :: t ->
| h :: t ->
if n = 0 then h else loop (n - 1) t
in
loop index l
Expand Down Expand Up @@ -525,6 +525,36 @@ let of_enum e =
let cons x l = x :: l
#endif

#ifndef OCAML4_05

let assoc_opt k l = try Some (assoc k l) with Not_found -> None
let assq_opt k l = try Some (assq k l) with Not_found -> None
let find_opt p l = try Some (find p l) with Not_found -> None

let nth_opt =
let rec loop n = function
| [] -> None
| h :: t ->
if n = 0 then Some h else loop (n - 1) t
in
fun l index -> if index < 0 then None else loop index l

let rec compare_lengths l1 l2 =
match l1, l2 with
| [], [] -> 0
| [], _ -> -1
| _, [] -> 1
| _ :: l1, _ :: l2 -> compare_lengths l1 l2

let rec compare_length_with l n =
match l, n with
| [], 0 -> 0
| [], _ -> if n > 0 then -1 else 1
| _, 0 -> 1
| _ :: l, n -> compare_length_with l (n-1)

#endif

end

let ( @ ) = List.append
9 changes: 9 additions & 0 deletions src/extList.mli
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ module List :

val cons : 'a -> 'a list -> 'a list

val assoc_opt : 'a -> ('a * 'b) list -> 'b option
val assq_opt : 'a -> ('a * 'b) list -> 'b option

val find_opt : ('a -> bool) -> 'a list -> 'a option
val nth_opt : 'a list -> int -> 'a option

val compare_lengths : 'a list -> 'b list -> int
val compare_length_with : 'a list -> int -> int

(** {6 Modified functions} *)

(** Some minor modifications have been made to the specification of some
Expand Down
27 changes: 27 additions & 0 deletions src/extString.ml
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,31 @@ let split_on_char sep s =
sub s 0 !j :: !r
#endif

#ifndef OCAML4_05

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

let index_opt s c = index_rec_opt s (length s) 0 c

let index_from_opt s i c =
let l = length s in
if i < 0 || i > l then invalid_arg "ExtString.index_from_opt" else
index_rec_opt s l i c

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

let rindex_opt s c = rindex_rec_opt s (length s - 1) c

let rindex_from_opt s i c =
if i < -1 || i >= length s then
invalid_arg "ExtString.rindex_from_opt"
else
rindex_rec_opt s i c

#endif

end
4 changes: 4 additions & 0 deletions src/extString.mli
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,13 @@ module String :
val iter : (char -> unit) -> string -> unit
val escaped : string -> string
val index : string -> char -> int
val index_opt : string -> char -> int option
val rindex : string -> char -> int
val rindex_opt : string -> char -> int option
val index_from : string -> int -> char -> int
val index_from_opt : string -> int -> char -> int option
val rindex_from : string -> int -> char -> int
val rindex_from_opt : string -> int -> char -> int option
val contains : string -> char -> bool
val contains_from : string -> int -> char -> bool
val rcontains_from : string -> int -> char -> bool
Expand Down
4 changes: 2 additions & 2 deletions test/std.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ module XS = (struct
external set : bytes -> int -> char -> unit = "%string_safe_set"
external create : int -> bytes = "caml_create_string"
external unsafe_set : bytes -> int -> char -> unit = "%string_unsafe_set"
external unsafe_blit : string -> int -> bytes -> int -> int -> unit = "caml_blit_string" "noalloc"
external unsafe_fill : bytes -> int -> int -> char -> unit = "caml_fill_string" "noalloc"
external unsafe_blit : string -> int -> bytes -> int -> int -> unit = "caml_blit_string" [@@noalloc]
external unsafe_fill : bytes -> int -> int -> char -> unit = "caml_fill_string" [@@noalloc]
end : module type of String)

module XL = (struct
Expand Down

0 comments on commit f013ba2

Please sign in to comment.