package core

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file byte_units0.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
open! Import
open Std_internal
module Repr = Int63

module T : sig
  type t [@@deriving compare, hash, sexp_of, typerep] [@@immediate64]

  val to_string : t -> string
  val to_string_hum : t -> string
  val of_repr : Repr.t -> t
  val to_repr : t -> Repr.t
end = struct
  type t = Repr.t [@@deriving compare, hash, typerep]

  let of_repr = Fn.id
  let to_repr = Fn.id

  let to_string n =
    let open Repr in
    let kib = of_int 1024 in
    let mib = kib * kib in
    let gib = kib * mib in
    let n_abs = abs n in
    if n_abs < kib
    then sprintf "%dB" (to_int_exn n)
    else if n_abs < mib
    then sprintf "%.7gK" (to_float n /. to_float kib)
    else if n_abs < gib
    then sprintf "%.10gM" (to_float n /. to_float mib)
    else sprintf "%.13gG" (to_float n /. to_float gib)
  ;;

  let to_string_hum n =
    let open Repr in
    let kib = of_int 1024 in
    let mib = kib * kib in
    let gib = kib * mib in
    let tib = kib * gib in
    let pib = kib * tib in
    let n_abs = abs n in
    let f ~suffix ~size n =
      let f = to_float n /. to_float size in
      if Float.( >= ) f 999.5
      then sprintf "%.0f%s" f suffix
      else sprintf "%.3g%s" f suffix
    in
    if n_abs < kib
    then sprintf "%dB" (to_int_exn n)
    else if n_abs < mib
    then f ~suffix:"K" ~size:kib n
    else if n_abs < gib
    then f ~suffix:"M" ~size:mib n
    else if n_abs < tib
    then f ~suffix:"G" ~size:gib n
    else if n_abs < pib
    then f ~suffix:"T" ~size:tib n
    else f ~suffix:"P" ~size:pib n
  ;;

  let sexp_of_t n = Sexp.Atom (to_string n)
end

include T

let bytes_int_exn t = Repr.to_int_exn (to_repr t)
OCaml

Innovation. Community. Security.