Skip to content

Commit 543e684

Browse files
committed
uses real names for Knowledge.Name.t sexp-serialization
1 parent 7ddeba0 commit 543e684

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

lib/knowledge/bap_knowledge.ml

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -209,41 +209,6 @@ end = struct
209209
then name
210210
else package ^ ":" ^ name
211211

212-
module Id : sig
213-
type t [@@deriving bin_io, compare, sexp]
214-
val intern : fullname -> t
215-
val fullname : t -> fullname
216-
val hash : t -> int
217-
end = struct
218-
219-
let registry = Hashtbl.create (module Int63)
220-
221-
(* using FNV-1a algorithm *)
222-
let hash_name =
223-
let open Int63 in
224-
let init = of_int64_exn 0xCBF29CE484222325L in
225-
let m = of_int64_exn 0x100000001B3L in
226-
let hash init = String.fold ~init ~f:(fun h c ->
227-
(h lxor of_int (Char.to_int c)) * m) in
228-
fun {package; name} ->
229-
hash (hash init package) name
230-
231-
let intern name =
232-
let id = hash_name name in
233-
match Hashtbl.find registry id with
234-
| None -> Hashtbl.add_exn registry id name; id
235-
| Some name' ->
236-
if equal_fullname name name'
237-
then id
238-
else invalid_argf "Names %S and %S have the same hash value, \
239-
Change one of them."
240-
(full name) (full name') ()
241-
242-
let fullname = Hashtbl.find_exn registry
243-
include Int63
244-
end
245-
type t = Id.t [@@deriving bin_io, compare, sexp]
246-
247212
let separator = ':'
248213
let escape_char = '\\'
249214
let escapeworthy = [separator]
@@ -340,6 +305,48 @@ end = struct
340305
{package; name}
341306
end
342307

308+
module Id : sig
309+
type t [@@deriving bin_io, compare, sexp]
310+
val intern : fullname -> t
311+
val fullname : t -> fullname
312+
val hash : t -> int
313+
end = struct
314+
315+
let registry = Hashtbl.create (module Int63)
316+
317+
(* using FNV-1a algorithm *)
318+
let hash_name =
319+
let open Int63 in
320+
let init = of_int64_exn 0xCBF29CE484222325L in
321+
let m = of_int64_exn 0x100000001B3L in
322+
let hash init = String.fold ~init ~f:(fun h c ->
323+
(h lxor of_int (Char.to_int c)) * m) in
324+
fun {package; name} ->
325+
hash (hash init package) name
326+
327+
let intern name =
328+
let id = hash_name name in
329+
match Hashtbl.find registry id with
330+
| None -> Hashtbl.add_exn registry id name; id
331+
| Some name' ->
332+
if equal_fullname name name'
333+
then id
334+
else invalid_argf "Names %S and %S have the same hash value, \
335+
Change one of them."
336+
(full name) (full name') ()
337+
338+
let fullname = Hashtbl.find_exn registry
339+
include Int63
340+
let sexp_of_t id =
341+
Sexp.Atom (Full.to_string (fullname id))
342+
let t_of_sexp = function
343+
| Sexp.Atom str -> intern (Full.read str)
344+
| _ -> invalid_arg "KB.Name.sexp_of_t: expects an atom"
345+
346+
end
347+
type t = Id.t [@@deriving bin_io, compare, sexp]
348+
349+
343350
let full = Id.fullname
344351

345352
let create ?package name =

0 commit comments

Comments
 (0)