From dd83cd27e4e79af3fe3f4e7ff0c1a28d0b4b0d79 Mon Sep 17 00:00:00 2001 From: Kento Okura Date: Tue, 25 Aug 2026 12:18:54 +0200 Subject: [PATCH] Domain safe URI hash table --- lib/core/URI.ml | 28 +++++++++++++++++++++++++++- lib/core/URI.mli | 17 ++++++++++++++++- lib/core/dune | 3 ++- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/lib/core/URI.ml b/lib/core/URI.ml index f401b8d..c3fe96f 100644 --- a/lib/core/URI.ml +++ b/lib/core/URI.ml @@ -102,7 +102,33 @@ end module Set = Set.Make (Basics) module Map = Map.Make (Basics) -module Tbl = Hashtbl.Make (Basics) + +module Tbl = struct + module S = Saturn.Htbl + + type key = Basics.t + type 'a t = (key, 'a) S.t + + let create : int -> 'a t = + fun min_buckets -> S.create ~hashed_type:(module Basics) ~min_buckets () + + let clear : 'a t -> unit = fun t -> ignore (S.remove_all t) + let mem : 'a t -> key -> bool = S.mem + let find_opt : 'a t -> key -> 'a option = S.find_opt + + let replace : 'a t -> key -> 'a -> unit = + fun t k v -> if not (S.try_set t k v) then ignore (S.try_add t k v) + + let add : 'a t -> key -> 'a -> unit = replace + let remove : 'a t -> key -> unit = fun t k -> ignore (S.try_remove t k) + let to_seq : 'a t -> (key * 'a) Seq.t = S.to_seq + let to_seq_keys : 'a t -> key Seq.t = fun t -> Seq.map fst (S.to_seq t) + let to_seq_values : 'a t -> 'a Seq.t = fun t -> Seq.map snd (S.to_seq t) + + let iter : (key -> 'a -> unit) -> 'a t -> unit = + fun f t -> Seq.iter (fun (k, v) -> f k v) (S.to_seq t) +end + include Basics let named_uri ~base name = diff --git a/lib/core/URI.mli b/lib/core/URI.mli index 17b58b2..bb213dc 100644 --- a/lib/core/URI.mli +++ b/lib/core/URI.mli @@ -38,8 +38,23 @@ val of_string_exn : string -> t module Set : Set.S with type elt = t module Map : Map.S with type key = t -module Tbl : Hashtbl.S with type key = t +module Tbl : sig + type key = t + type 'a t + + val create : int -> 'a t + val clear : 'a t -> unit + val mem : 'a t -> key -> bool + val find_opt : 'a t -> key -> 'a option + val replace : 'a t -> key -> 'a -> unit + val add : 'a t -> key -> 'a -> unit + val remove : 'a t -> key -> unit + val to_seq : 'a t -> (key * 'a) Seq.t + val to_seq_keys : 'a t -> key Seq.t + val to_seq_values : 'a t -> 'a Seq.t + val iter : (key -> 'a -> unit) -> 'a t -> unit +end val named_uri : base:t -> string -> t val canonical_host : t -> string option val same_site : base:t -> t -> bool diff --git a/lib/core/dune b/lib/core/dune index 63cac07..5803cc2 100644 --- a/lib/core/dune +++ b/lib/core/dune @@ -26,7 +26,8 @@ logs.fmt str lsp - algaeff) + algaeff + saturn) (public_name forester.core)) (env -- 2.51.2