Skip to content

Commit

Permalink
Backport caml_string_hash from OCaml 5 to runtime 4 (ocaml-flambda#…
Browse files Browse the repository at this point in the history
  • Loading branch information
xclerc authored Nov 17, 2023
1 parent 430bb7c commit 1ec72dd
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 12 deletions.
9 changes: 9 additions & 0 deletions ocaml/runtime4/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,15 @@ CAMLprim value caml_hash(value count, value limit, value seed, value obj)
return Val_int(h & 0x3FFFFFFFU);
}

CAMLprim value caml_string_hash(value seed, value string)
{
uint32_t h;
h = Int_val(seed);
h = caml_hash_mix_string (h, string);
FINAL_MIX(h);
return Val_int(h & 0x3FFFFFFFU);
}

/* Hashing variant tags */

CAMLexport value caml_hash_variant(char const * tag)
Expand Down
2 changes: 0 additions & 2 deletions ocaml/stdlib/string.ml
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,8 @@ let ends_with ~suffix s =
else aux (i + 1)
in diff >= 0 && aux 0

(* BACKPORT
external seeded_hash : int -> string -> int = "caml_string_hash" [@@noalloc]
let hash x = seeded_hash 0 x
*)

(* duplicated in bytes.ml *)
let split_on_char sep s =
Expand Down
3 changes: 0 additions & 3 deletions ocaml/stdlib/string.mli
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,6 @@ val get_int32_ne : string -> int -> int32
@since 4.13
*)

(* BACKPORT
not in 4.x runtime (caml_string_hash)
val hash : t -> int
(** An unseeded hash function for strings, with the same output value as
{!Hashtbl.hash}. This function allows this module to be passed as argument
Expand All @@ -498,7 +496,6 @@ val seeded_hash : int -> t -> int
argument to the functor {!Hashtbl.MakeSeeded}.
@since 5.0 *)
*)

val get_int32_be : string -> int -> int32
(** [get_int32_be b i] is [b]'s big-endian 32-bit integer
Expand Down
4 changes: 0 additions & 4 deletions ocaml/stdlib/stringLabels.mli
Original file line number Diff line number Diff line change
Expand Up @@ -485,9 +485,6 @@ val get_int32_ne : string -> int -> int32
@since 4.13
*)

(* BACKPORT
not in 4.x runtime (caml_string_hash)
val hash : t -> int
(** An unseeded hash function for strings, with the same output value as
{!Hashtbl.hash}. This function allows this module to be passed as argument
Expand All @@ -501,7 +498,6 @@ val seeded_hash : int -> t -> int
argument to the functor {!Hashtbl.MakeSeeded}.
@since 5.0 *)
*)

val get_int32_be : string -> int -> int32
(** [get_int32_be b i] is [b]'s big-endian 32-bit integer
Expand Down
3 changes: 0 additions & 3 deletions ocaml/testsuite/tests/lib-string/test_string.ml
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,12 @@ let () =
done
;;


(* BACKPORT
let () =
printf "-- Hashtbl.hash raw_string: %x\n%!" (Hashtbl.hash raw_string);
printf "-- String.hash raw_string: %x\n%!" (String.hash raw_string);
printf "-- Hashtbl.seeded_hash 16 raw_string: %x\n%!" (Hashtbl.seeded_hash 16 raw_string);
printf "-- String.seeded_hash 16 raw_string: %x\n%!" (String.seeded_hash 16 raw_string);
;;
*)

(* GPR#805/815/833 *)

Expand Down
4 changes: 4 additions & 0 deletions ocaml/testsuite/tests/lib-string/test_string.reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Hashtbl.hash raw_string: 240a0e56
-- String.hash raw_string: 240a0e56
-- Hashtbl.seeded_hash 16 raw_string: 3210af30
-- String.seeded_hash 16 raw_string: 3210af30

0 comments on commit 1ec72dd

Please sign in to comment.