diff -Nru golang-github-alecthomas-units-0.0~git20210927.59d0afb/bytes.go golang-github-alecthomas-units-0.0~git20211218.b94a6e3/bytes.go --- golang-github-alecthomas-units-0.0~git20210927.59d0afb/bytes.go 2021-11-17 21:13:53.000000000 +0000 +++ golang-github-alecthomas-units-0.0~git20211218.b94a6e3/bytes.go 2021-12-18 09:36:45.000000000 +0000 @@ -72,6 +72,43 @@ } } +// Round returns Base2Bytes with all but the first n units zeroed out. So that e.g. 1GiB1MiB1KiB → 1GiB1MiB, if n is 2. +func (b Base2Bytes) Round(n int) Base2Bytes { + idx := 0 + + switch { + case b > Exbibyte: + idx = n + case b > Pebibyte: + idx = n + 1 + case b > Tebibyte: + idx = n + 2 + case b > Gibibyte: + idx = n + 3 + case b > Mebibyte: + idx = n + 4 + case b > Kibibyte: + idx = n + 5 + } + + switch idx { + case 1: + return b - b%Exbibyte + case 2: + return b - b%Pebibyte + case 3: + return b - b%Tebibyte + case 4: + return b - b%Gibibyte + case 5: + return b - b%Mebibyte + case 6: + return b - b%Kibibyte + default: + return b + } +} + var metricBytesUnitMap = MakeUnitMap("B", "B", 1000) // MetricBytes are SI byte units (1000 bytes in a kilobyte). @@ -122,6 +159,43 @@ default: return b } +} + +// Round returns MetricBytes with all but the first n units zeroed out. So that e.g. 1GB1MB1KB → 1GB1MB, if n is 2. +func (b MetricBytes) Round(n int) MetricBytes { + idx := 0 + + switch { + case b > Exabyte: + idx = n + case b > Petabyte: + idx = n + 1 + case b > Terabyte: + idx = n + 2 + case b > Gigabyte: + idx = n + 3 + case b > Megabyte: + idx = n + 4 + case b > Kilobyte: + idx = n + 5 + } + + switch idx { + case 1: + return b - b%Exabyte + case 2: + return b - b%Petabyte + case 3: + return b - b%Terabyte + case 4: + return b - b%Gigabyte + case 5: + return b - b%Megabyte + case 6: + return b - b%Kilobyte + default: + return b + } } // ParseStrictBytes supports both iB and B suffixes for base 2 and metric, diff -Nru golang-github-alecthomas-units-0.0~git20210927.59d0afb/bytes_test.go golang-github-alecthomas-units-0.0~git20211218.b94a6e3/bytes_test.go --- golang-github-alecthomas-units-0.0~git20210927.59d0afb/bytes_test.go 2021-11-17 21:13:53.000000000 +0000 +++ golang-github-alecthomas-units-0.0~git20211218.b94a6e3/bytes_test.go 2021-12-18 09:36:45.000000000 +0000 @@ -72,7 +72,7 @@ assert.Equal(t, 1572864, int(n)) } -func TestBase2RoundToLargest(t *testing.T) { +func TestBase2Floor(t *testing.T) { var n Base2Bytes = KiB assert.Equal(t, "1KiB", n.Floor().String()) n = MiB + KiB @@ -83,6 +83,25 @@ assert.Equal(t, "3GiB", n.Floor().String()) } +func TestBase2Round(t *testing.T) { + var n Base2Bytes = KiB + assert.Equal(t, "1KiB", n.Round(1).String()) + n = MiB + KiB + assert.Equal(t, "1MiB", n.Round(1).String()) + n = GiB + MiB + KiB + assert.Equal(t, "1GiB", n.Round(1).String()) + n = 3*GiB + 2*MiB + KiB + assert.Equal(t, "3GiB", n.Round(1).String()) + n = KiB + assert.Equal(t, "1KiB", n.Round(2).String()) + n = MiB + KiB + assert.Equal(t, "1MiB1KiB", n.Round(2).String()) + n = GiB + MiB + KiB + assert.Equal(t, "1GiB1MiB", n.Round(2).String()) + n = 3*GiB + 2*MiB + KiB + assert.Equal(t, "3GiB2MiB", n.Round(2).String()) +} + func TestMetricBytesString(t *testing.T) { assert.Equal(t, MetricBytes(0).String(), "0B") // TODO: SI standard prefix is lowercase "kB" @@ -142,7 +161,7 @@ assert.Equal(t, 1500000, int(n)) } -func TestMetricRoundToLargest(t *testing.T) { +func TestMetricFloor(t *testing.T) { var n MetricBytes = KB assert.Equal(t, "1KB", n.Floor().String()) n = MB + KB @@ -153,6 +172,25 @@ assert.Equal(t, "3GB", n.Floor().String()) } +func TestMetricRound(t *testing.T) { + var n MetricBytes = KB + assert.Equal(t, "1KB", n.Round(1).String()) + n = MB + KB + assert.Equal(t, "1MB", n.Round(1).String()) + n = GB + MB + KB + assert.Equal(t, "1GB", n.Round(1).String()) + n = 3*GB + 2*MB + KB + assert.Equal(t, "3GB", n.Round(1).String()) + n = KB + assert.Equal(t, "1KB", n.Round(2).String()) + n = MB + KB + assert.Equal(t, "1MB1KB", n.Round(2).String()) + n = GB + MB + KB + assert.Equal(t, "1GB1MB", n.Round(2).String()) + n = 3*GB + 2*MB + KB + assert.Equal(t, "3GB2MB", n.Round(2).String()) +} + func TestJSON(t *testing.T) { type args struct { b Base2Bytes diff -Nru golang-github-alecthomas-units-0.0~git20210927.59d0afb/debian/changelog golang-github-alecthomas-units-0.0~git20211218.b94a6e3/debian/changelog --- golang-github-alecthomas-units-0.0~git20210927.59d0afb/debian/changelog 2021-11-17 21:30:00.000000000 +0000 +++ golang-github-alecthomas-units-0.0~git20211218.b94a6e3/debian/changelog 2021-12-20 14:50:35.000000000 +0000 @@ -1,3 +1,12 @@ +golang-github-alecthomas-units (0.0~git20211218.b94a6e3-1) unstable; urgency=medium + + * Team upload. + * New upstream snapshot. + * Add a new debian/gitlab-ci-yml file. + * Mark -dev package as Multi-Arch: foreign. + + -- Guillem Jover Mon, 20 Dec 2021 15:50:35 +0100 + golang-github-alecthomas-units (0.0~git20210927.59d0afb-1) unstable; urgency=medium * Team upload. @@ -6,7 +15,7 @@ * Remove constraints unnecessary since buster. [ Guillem Jover ] - * New upstream release. + * New upstream snapshot. * Update gbp.conf to stop using pristine-tar workflow. * Switch to dh-sequence-golang from dh-golang and --with=golang. * Switch to Standards-Version 4.6.0 (no changes needed). diff -Nru golang-github-alecthomas-units-0.0~git20210927.59d0afb/debian/control golang-github-alecthomas-units-0.0~git20211218.b94a6e3/debian/control --- golang-github-alecthomas-units-0.0~git20210927.59d0afb/debian/control 2021-11-17 21:20:38.000000000 +0000 +++ golang-github-alecthomas-units-0.0~git20211218.b94a6e3/debian/control 2021-12-20 14:48:54.000000000 +0000 @@ -17,6 +17,7 @@ Package: golang-github-alecthomas-units-dev Architecture: all +Multi-Arch: foreign Depends: ${misc:Depends}, Description: Go package for parsing byte units This package provides multipliers and string conversion functions diff -Nru golang-github-alecthomas-units-0.0~git20210927.59d0afb/debian/gitlab-ci.yml golang-github-alecthomas-units-0.0~git20211218.b94a6e3/debian/gitlab-ci.yml --- golang-github-alecthomas-units-0.0~git20210927.59d0afb/debian/gitlab-ci.yml 1970-01-01 00:00:00.000000000 +0000 +++ golang-github-alecthomas-units-0.0~git20211218.b94a6e3/debian/gitlab-ci.yml 2021-12-20 14:48:54.000000000 +0000 @@ -0,0 +1,6 @@ +# auto-generated, DO NOT MODIFY. +# The authoritative copy of this file lives at: +# https://salsa.debian.org/go-team/infra/pkg-go-tools/blob/master/config/gitlabciyml.go +--- +include: + - https://salsa.debian.org/go-team/infra/pkg-go-tools/-/raw/master/pipeline/test-archive.yml