41 lines
937 B
OCaml
41 lines
937 B
OCaml
|
|
let until_nul s =
|
|
match String.index_opt s '\x00' with
|
|
| None -> s
|
|
| Some i -> String.sub s 0 i
|
|
|
|
module type IntType = sig
|
|
type t
|
|
val add : t -> t -> t
|
|
val sub : t -> t -> t
|
|
val mul : t -> t -> t
|
|
val of_int : int -> t
|
|
val to_int : t -> int
|
|
end
|
|
|
|
|
|
module IntForInt = struct
|
|
type t = int
|
|
let add = Int.add
|
|
let sub = Int.sub
|
|
let mul = Int.mul
|
|
let of_int x = x
|
|
let to_int x = x
|
|
end
|
|
|
|
let octal_reader (type a) (module I : IntType with type t = a) sin : a =
|
|
let ( + ) = I.add in
|
|
let ( * ) = I.mul in
|
|
let ( - ) = I.sub in
|
|
let s = (until_nul sin) in
|
|
let l = List.init (String.length s) (String.get s) in
|
|
let rec aux a = function
|
|
| c :: rest ->
|
|
aux ((a * (I.of_int 8)) + (I.of_int (int_of_char c)) - (I.of_int (int_of_char '0'))) rest
|
|
| [] -> a
|
|
in aux (I.of_int 0) l
|
|
|
|
(* lmao *)
|
|
let to_int = octal_reader (module IntForInt)
|
|
let to_int64 = octal_reader (module Int64)
|