summaryrefslogtreecommitdiff
path: root/vendor/codeberg.org/gruf/go-bytesize/bytesize.go
diff options
context:
space:
mode:
authorLibravatar kim <grufwub@gmail.com>2025-08-21 16:41:50 +0200
committerLibravatar kim <gruf@noreply.codeberg.org>2025-08-21 16:41:50 +0200
commita79f83cbded3fb1165b338ce316dc9f2217467ff (patch)
tree4b4a2b7084aced51c0a691cee6352b4ecae90df8 /vendor/codeberg.org/gruf/go-bytesize/bytesize.go
parent[chore] bump golangci-lint version in CI, disable var-naming package name che... (diff)
downloadgotosocial-a79f83cbded3fb1165b338ce316dc9f2217467ff.tar.xz
[chore] update dependencies (#4386)
- codeberg.org/gruf/go-bytesize v1.0.3 -> v1.0.4 - codeberg.org/gruf/go-kv/v2 v2.0.6 -> v2.0.7 - codeberg.org/gruf/go-mutexes v1.5.2 -> v1.5.3 - codeberg.org/gruf/go-structr v0.9.7 -> v0.9.8 - codeberg.org/gruf/go-ffmpreg v0.6.8 -> v0.6.9 - github.com/tomnomnom/linkheader HEAD@2018 -> HEAD@2025 all of the above codeberg.org/gruf updates are in preparation for Go1.25, except for bytesize, and also ffmpreg which is a rebuild with the latest version of ffmpeg (v5.1.7) Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4386 Co-authored-by: kim <grufwub@gmail.com> Co-committed-by: kim <grufwub@gmail.com>
Diffstat (limited to 'vendor/codeberg.org/gruf/go-bytesize/bytesize.go')
-rw-r--r--vendor/codeberg.org/gruf/go-bytesize/bytesize.go76
1 files changed, 44 insertions, 32 deletions
diff --git a/vendor/codeberg.org/gruf/go-bytesize/bytesize.go b/vendor/codeberg.org/gruf/go-bytesize/bytesize.go
index d24ff5f0f..25283d8b6 100644
--- a/vendor/codeberg.org/gruf/go-bytesize/bytesize.go
+++ b/vendor/codeberg.org/gruf/go-bytesize/bytesize.go
@@ -94,8 +94,10 @@ var (
// methods for byte sizes in both IEC and SI units.
type Size uint64
-// ParseSize will parse a valid Size from given string. Both IEC and SI units are supported.
+// ParseSize will parse a valid Size from given
+// string. Both IEC and SI units are supported.
func ParseSize(s string) (Size, error) {
+
// Parse units from string
unit, l, err := parseUnit(s)
if err != nil {
@@ -121,10 +123,15 @@ func (sz *Size) Set(in string) error {
return nil
}
+// AppendText implements encoding.TextAppender{}.
+func (sz Size) AppendText(b []byte) ([]byte, error) {
+ return sz.AppendFormatIEC(b), nil
+}
+
// MarshalText implements encoding.TextMarshaler{}.
-func (sz *Size) MarshalText() ([]byte, error) {
+func (sz Size) MarshalText() ([]byte, error) {
const maxLen = 7 // max IEC string length
- return sz.AppendFormatIEC(make([]byte, 0, maxLen)), nil
+ return sz.AppendText(make([]byte, 0, maxLen))
}
// UnmarshalText implements encoding.TextUnmarshaler{}.
@@ -143,8 +150,12 @@ func (sz Size) AppendFormatSI(dst []byte) []byte {
dst = itoa(dst, uint64(sz))
dst = append(dst, 'B')
return dst
- } // above is fast-path, .appendFormat() is outlined
- return sz.appendFormat(dst, 1000, &sipows, "B")
+ }
+ f, u := sztof(sz, 1000, sipows)
+ dst = ftoa(dst, f)
+ dst = append(dst, u)
+ dst = append(dst, 'B')
+ return dst
}
// AppendFormatIEC will append IEC formatted size to 'dst'.
@@ -153,35 +164,11 @@ func (sz Size) AppendFormatIEC(dst []byte) []byte {
dst = itoa(dst, uint64(sz))
dst = append(dst, 'B')
return dst
- } // above is fast-path, .appendFormat() is outlined
- return sz.appendFormat(dst, 1024, &iecpows, "iB")
-}
-
-// appendFormat will append formatted Size to 'dst', depending on base, powers table and single unit suffix.
-func (sz Size) appendFormat(dst []byte, base uint64, pows *[6]float64, sunit string) []byte {
- const (
- // min "small" unit threshold
- min = 0.75
-
- // binary unit chars.
- units = `kMGTPE`
- )
-
- // Larger number: get value of
- // i / unit size. We have a 'min'
- // threshold after which we prefer
- // using the unit 1 down
- n := bits.Len64(uint64(sz)) / 10
- f := float64(sz) / pows[n-1]
- if f < min {
- f *= float64(base)
- n--
}
-
- // Append formatted float with units
+ f, u := sztof(sz, 1024, iecpows)
dst = ftoa(dst, f)
- dst = append(dst, units[n-1])
- dst = append(dst, sunit...)
+ dst = append(dst, u)
+ dst = append(dst, 'i', 'B')
return dst
}
@@ -261,6 +248,31 @@ func parseUnit(s string) (float64, int, error) {
return sivals[c], l, nil
}
+// sztof divides a Size with base and power units to a float value with power.
+func sztof(sz Size, base float64, pows [6]float64) (float64, byte) {
+ const (
+ // min "small"
+ // unit threshold.
+ min = 0.75
+
+ // binary unit chars.
+ units = `kMGTPE`
+ )
+
+ // Larger number: get value of
+ // i / unit size. We have a 'min'
+ // threshold after which we prefer
+ // using the unit 1 down
+ n := bits.Len64(uint64(sz)) / 10
+ f := float64(sz) / pows[n-1]
+ if f < min {
+ f *= base
+ n--
+ }
+
+ return f, units[n-1]
+}
+
// ftoa appends string formatted 'f' to 'dst', assumed < ~800.
func ftoa(dst []byte, f float64) []byte {
switch i := uint64(f); {