Debian Codegraph @

tools/humanize/humanize.go

raw symbols SCIP
1 package humanizescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/
2
3 import (
4 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
5 "mathscip-go gomod github.com/golang/go/src go1.25.0 math/"
6 "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/"
7 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
8 "timescip-go gomod github.com/golang/go/src go1.25.0 time/"
9 "unicodescip-go gomod github.com/golang/go/src go1.25.0 unicode/"
10
11 "github.com/git-lfs/git-lfs/v3/errorsscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/errors`/"
12 "github.com/git-lfs/git-lfs/v3/trscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tr`/"
13 )
14
15 const (
16 Bytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Byte. = 1 << (iota * 10)
17 Kibibytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Kibibyte.
18 Mebibytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Mebibyte.
19 Gibibytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Gibibyte.
20 Tebibytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Tebibyte.
21 Pebibytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Pebibyte.
22
23 Kilobytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Kilobyte. = 1000 * Bytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Byte.
24 Megabytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Megabyte. = 1000 * Kilobytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Kilobyte.
25 Gigabytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Gigabyte. = 1000 * Megabytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Megabyte.
26 Terabytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Terabyte. = 1000 * Gigabytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Gigabyte.
27 Petabytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Petabyte. = 1000 * Terabytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Terabyte.
28
29 // eps is the machine epsilon, or a 64-bit floating point value
30 // reasonably close to zero.
31 epsscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/eps. float64 = 7.0/3 - 4.0/3 - 1.0
32 )
33
34 var bytesTablescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/bytesTable. = map[string]uint64{
35 "": Bytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Byte.,
36 "b": Bytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Byte.,
37
38 "kib": Kibibytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Kibibyte.,
39 "mib": Mebibytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Mebibyte.,
40 "gib": Gibibytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Gibibyte.,
41 "tib": Tebibytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Tebibyte.,
42 "pib": Pebibytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Pebibyte.,
43
44 "kb": Kilobytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Kilobyte.,
45 "mb": Megabytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Megabyte.,
46 "gb": Gigabytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Gigabyte.,
47 "tb": Terabytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Terabyte.,
48 "pb": Petabytescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/Petabyte.,
49 }
50
51 // ParseBytes parses a given human-readable bytes or ibytes string into a number
52 // of bytes, or an error if the string was unable to be parsed.
53 func ParseBytesscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/ParseBytes().(str string) (uint64, error) {
54 var sep int
55 for _, r := range str {
56 if !(unicodescip-go gomod github.com/golang/go/src go1.25.0 unicode/.IsDigitscip-go gomod github.com/golang/go/src go1.25.0 unicode/IsDigit().(r) || r == '.' || r == ',') {
57 break
58 }
59
60 sep = sep + 1
61 }
62
63 var f float64
64
65 if s := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Replacescip-go gomod github.com/golang/go/src go1.25.0 strings/Replace().(str[:sep], ",", "", -1); len(s) > 0 {
66 var err error
67
68 f, err = strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseFloatscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseFloat().(s, 64)
69 if err != nil {
70 return 0, err
71 }
72 }
73
74 m, err := ParseByteUnitscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/ParseByteUnit().(str[sep:])
75 if err != nil {
76 return 0, err
77 }
78
79 f = f * float64(m)
80 if f >= mathscip-go gomod github.com/golang/go/src go1.25.0 math/.MaxUint64scip-go gomod github.com/golang/go/src go1.25.0 math/MaxUint64. {
81 return 0, errorsscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/errors`/.Newscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/errors`/New().(trscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tr`/.Trscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tr`/Tr..Getscip-go gomod github.com/leonelquinteros/gotext v1.7.2 `github.com/leonelquinteros/gotext`/Locale#Get().("number of bytes too large"))
82 }
83 return uint64(f), nil
84 }
85
86 // ParseByteUnit returns the number of bytes in a given unit of storage, or an
87 // error, if that unit is unrecognized.
88 func ParseByteUnitscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/ParseByteUnit().(str string) (uint64, error) {
89 str = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSpacescip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSpace().(str)
90 str = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.ToLowerscip-go gomod github.com/golang/go/src go1.25.0 strings/ToLower().(str)
91
92 if u, ok := bytesTablescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/bytesTable.[str]; ok {
93 return u, nil
94 }
95 return 0, errorsscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/errors`/.Newscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/errors`/New().(trscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tr`/.Trscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tr`/Tr..Getscip-go gomod github.com/leonelquinteros/gotext v1.7.2 `github.com/leonelquinteros/gotext`/Locale#Get().("unknown unit: %q", str))
96 }
97
98 var sizesscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/sizes. = []string{"B", "KB", "MB", "GB", "TB", "PB"}
99
100 // FormatBytes outputs the given number of bytes "s" as a human-readable string,
101 // rounding to the nearest half within .01.
102 func FormatBytesscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/FormatBytes().(s uint64) string {
103 var e float64
104 if s == 0 {
105 e = 0
106 } else {
107 e = mathscip-go gomod github.com/golang/go/src go1.25.0 math/.Floorscip-go gomod github.com/golang/go/src go1.25.0 math/Floor().(logscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/log().(float64(s), 1000))
108 }
109
110 unit := uint64(mathscip-go gomod github.com/golang/go/src go1.25.0 math/.Powscip-go gomod github.com/golang/go/src go1.25.0 math/Pow().(1000, e))
111 suffix := sizesscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/sizes.[int(e)]
112
113 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%s %s",
114 FormatBytesUnitscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/FormatBytesUnit().(s, unit), suffix)
115 }
116
117 // FormatBytesUnit outputs the given number of bytes "s" as a quantity of the
118 // given units "u" to the nearest half within .01.
119 func FormatBytesUnitscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/FormatBytesUnit().(s, u uint64) string {
120 var rounded float64
121 if s < 10 {
122 rounded = float64(s)
123 } else {
124 rounded = mathscip-go gomod github.com/golang/go/src go1.25.0 math/.Floorscip-go gomod github.com/golang/go/src go1.25.0 math/Floor().(float64(s)/float64(u)*10+.5) / 10
125 }
126
127 format := "%.0f"
128 if rounded < 10 && u > 1 {
129 format = "%.1f"
130 }
131
132 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().(format, rounded)
133 }
134
135 // FormatByteRate outputs the given rate of transfer "r" as the quotient of "s"
136 // (the number of bytes transferred) over "d" (the duration of time that those
137 // bytes were transferred in).
138 //
139 // It displays the output as a quantity of a "per-unit-time" unit (i.e., B/s,
140 // MiB/s) in the most representative fashion possible, as above.
141 func FormatByteRatescip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/FormatByteRate().(s uint64, d timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#) string {
142 // e is the index of the most representative unit of storage.
143 var e float64
144
145 // f is the floating-point equivalent of "s", so as to avoid more
146 // conversions than necessary.
147 f := float64(s)
148
149 if f != 0 {
150 f = f / mathscip-go gomod github.com/golang/go/src go1.25.0 math/.Maxscip-go gomod github.com/golang/go/src go1.25.0 math/Max().(timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nanosecondscip-go gomod github.com/golang/go/src go1.25.0 time/Nanosecond..Secondsscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#Seconds().(), d.Secondsscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#Seconds().())
151 e = mathscip-go gomod github.com/golang/go/src go1.25.0 math/.Floorscip-go gomod github.com/golang/go/src go1.25.0 math/Floor().(logscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/log().(f, 1000))
152 if e <= epsscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/eps. {
153 // The result of math.Floor(log(r, 1000)) can be
154 // "close-enough" to zero that it should be effectively
155 // considered zero.
156 e = 0
157 }
158 }
159
160 unit := uint64(mathscip-go gomod github.com/golang/go/src go1.25.0 math/.Powscip-go gomod github.com/golang/go/src go1.25.0 math/Pow().(1000, e))
161 suffix := sizesscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/sizes.[int(e)]
162
163 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("%s %s/s",
164 FormatBytesUnitscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/FormatBytesUnit().(uint64(mathscip-go gomod github.com/golang/go/src go1.25.0 math/.Ceilscip-go gomod github.com/golang/go/src go1.25.0 math/Ceil().(f)), unit), suffix)
165 }
166
167 // log takes the log base "b" of "n" (\log_b{n})
168 func logscip-go gomod github.com/git-lfs/git-lfs/v3 . `github.com/git-lfs/git-lfs/v3/tools/humanize`/log().(n, b float64) float64 {
169 return mathscip-go gomod github.com/golang/go/src go1.25.0 math/.Logscip-go gomod github.com/golang/go/src go1.25.0 math/Log().(n) / mathscip-go gomod github.com/golang/go/src go1.25.0 math/.Logscip-go gomod github.com/golang/go/src go1.25.0 math/Log().(b)
170 }