Skip to content

Commit ce1bd17

Browse files
authored
flambda-backend: Backport caml_string_hash from OCaml 5 to runtime 4 (#2045)
1 parent 584cb01 commit ce1bd17

File tree

6 files changed

+13
-12
lines changed

6 files changed

+13
-12
lines changed

runtime4/hash.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,15 @@ CAMLprim value caml_hash(value count, value limit, value seed, value obj)
301301
return Val_int(h & 0x3FFFFFFFU);
302302
}
303303

304+
CAMLprim value caml_string_hash(value seed, value string)
305+
{
306+
uint32_t h;
307+
h = Int_val(seed);
308+
h = caml_hash_mix_string (h, string);
309+
FINAL_MIX(h);
310+
return Val_int(h & 0x3FFFFFFFU);
311+
}
312+
304313
/* Hashing variant tags */
305314

306315
CAMLexport value caml_hash_variant(char const * tag)

stdlib/string.ml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,8 @@ let ends_with ~suffix s =
223223
else aux (i + 1)
224224
in diff >= 0 && aux 0
225225

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

231229
(* duplicated in bytes.ml *)
232230
let split_on_char sep s =

stdlib/string.mli

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,8 +483,6 @@ val get_int32_ne : string -> int -> int32
483483
@since 4.13
484484
*)
485485

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

503500
val get_int32_be : string -> int -> int32
504501
(** [get_int32_be b i] is [b]'s big-endian 32-bit integer

stdlib/stringLabels.mli

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -485,9 +485,6 @@ val get_int32_ne : string -> int -> int32
485485
@since 4.13
486486
*)
487487

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

506502
val get_int32_be : string -> int -> int32
507503
(** [get_int32_be b i] is [b]'s big-endian 32-bit integer

testsuite/tests/lib-string/test_string.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,12 @@ let () =
4141
done
4242
;;
4343

44-
45-
(* BACKPORT
4644
let () =
4745
printf "-- Hashtbl.hash raw_string: %x\n%!" (Hashtbl.hash raw_string);
4846
printf "-- String.hash raw_string: %x\n%!" (String.hash raw_string);
4947
printf "-- Hashtbl.seeded_hash 16 raw_string: %x\n%!" (Hashtbl.seeded_hash 16 raw_string);
5048
printf "-- String.seeded_hash 16 raw_string: %x\n%!" (String.seeded_hash 16 raw_string);
5149
;;
52-
*)
5350

5451
(* GPR#805/815/833 *)
5552

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Hashtbl.hash raw_string: 240a0e56
2+
-- String.hash raw_string: 240a0e56
3+
-- Hashtbl.seeded_hash 16 raw_string: 3210af30
4+
-- String.seeded_hash 16 raw_string: 3210af30

0 commit comments

Comments
 (0)