logarion/lib/id.ml

34 lines
957 B
OCaml
Raw Normal View History

let random_state = Random.State.make_self_init
(*module UUID = struct*)
(*type t = Uuidm.t*)
(*let compare = Uuidm.compare*)
(*let to_string = Uuidm.to_string*)
(*let of_string = Uuidm.of_string*)
(*let to_bytes = Uuidm.to_bytes*)
(*let of_bytes = Uuidm.of_bytes*)
(*let generate ?(random_state=random_state ()) = Uuidm.v4_gen random_state*)
(*let nil = Uuidm.nil*)
(*end*)
type t = string
let compare = String.compare
let nil = ""
let short ?(len) id =
let id_len = String.length id in
let l = match len with Some l -> l | None -> if id_len = 36 then 8 else 6 in
String.sub id 0 (min l id_len)
let generate ?(len=6) ?(seed=random_state ()) () =
let b32 i = char_of_int @@
if i < 10 then i+48 else
if i < 18 then i+87 else
if i < 20 then i+88 else
if i < 22 then i+89 else
if i < 27 then i+90 else
if i < 32 then i+91 else
(invalid_arg ("id.char" ^ string_of_int i)) in
let c _ = b32 (Random.State.int seed 31) in
String.init len c