From 13429e5e8801dd8545b9ed9aad03a7f1ef9ea800 Mon Sep 17 00:00:00 2001 From: favonia Date: Tue, 18 May 2021 09:31:37 -0500 Subject: [PATCH] feat(hash): add `bytes` --- src/core/CCHash.ml | 7 ++++--- src/core/CCHash.mli | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/core/CCHash.ml b/src/core/CCHash.ml index 74b67c41..ea6e6341 100644 --- a/src/core/CCHash.ml +++ b/src/core/CCHash.ml @@ -86,14 +86,15 @@ let int32 (x:int32) = Hashtbl.hash x (* TODO: FNV *) let int64 (x:int64) = Hashtbl.hash x (* TODO: FNV *) let nativeint (x:nativeint) = Hashtbl.hash x -let string (x:string) = +let bytes (x:bytes) = let h = ref fnv_offset_basis in - for i = 0 to String.length x - 1 do + for i = 0 to Bytes.length x - 1 do h := Int64.(mul !h fnv_prime); - let byte = Char.code (String.unsafe_get x i) in + let byte = Char.code (Bytes.unsafe_get x i) in h := Int64.(logxor !h (of_int byte)); done; Int64.to_int !h land max_int +let string (x:string) = bytes (Bytes.unsafe_of_string x) (*$T int 42 >= 0 diff --git a/src/core/CCHash.mli b/src/core/CCHash.mli index 1b7379f2..efddcf0b 100644 --- a/src/core/CCHash.mli +++ b/src/core/CCHash.mli @@ -30,6 +30,7 @@ val slice : string -> int -> int t (** [slice s i len state] hashes the slice [i, …, i+len-1] of [s] into [state]. *) +val bytes : bytes t val string : string t val list : 'a t -> 'a list t