Skip to content

Commit 086d688

Browse files
committed
Humaner.Format(): change method to take a Humanable as argument
Add a new `FormatNumber()` method with the old behavior. Remove `Human()` from the `Humanable` interface.
1 parent 95f2966 commit 086d688

File tree

3 files changed

+19
-27
lines changed

3 files changed

+19
-27
lines changed

counts/human.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@ package counts
22

33
import (
44
"fmt"
5-
"math"
65
)
76

87
// A quantity that can be made human-readable using Human().
98
type Humanable interface {
10-
// Return the value and units as separate strings.
11-
Human(Humaner, string) (string, string)
12-
139
// Return the value as a uint64, and a boolean telling whether it
1410
// overflowed.
1511
ToUint64() (uint64, bool)
@@ -50,10 +46,11 @@ var Binary = Humaner{
5046
},
5147
}
5248

53-
// Format values, aligned, in `len(unit) + 10` or fewer characters
54-
// (except for extremely large numbers).
55-
func (h *Humaner) Format(n uint64, unit string) (string, string) {
49+
// Format n, aligned, in `len(unit) + 10` or fewer characters (except
50+
// for extremely large numbers).
51+
func (h *Humaner) FormatNumber(n uint64, unit string) (string, string) {
5652
prefix := h.prefixes[0]
53+
5754
wholePart := n
5855
for _, p := range h.prefixes {
5956
w := n / p.Multiplier
@@ -81,18 +78,13 @@ func (h *Humaner) Format(n uint64, unit string) (string, string) {
8178
}
8279
}
8380

84-
func (n Count32) Human(humaner Humaner, unit string) (string, string) {
85-
if n == math.MaxUint32 {
81+
// Format values, aligned, in `len(unit) + 10` or fewer characters
82+
// (except for extremely large numbers).
83+
func (h *Humaner) Format(value Humanable, unit string) (string, string) {
84+
n, overflow := value.ToUint64()
85+
if overflow {
8686
return "∞", unit
87-
} else {
88-
return humaner.Format(uint64(n), unit)
8987
}
90-
}
9188

92-
func (n Count64) Human(humaner Humaner, unit string) (string, string) {
93-
if n == math.MaxUint64 {
94-
return "∞", unit
95-
} else {
96-
return humaner.Format(uint64(n), unit)
97-
}
89+
return h.FormatNumber(n, unit)
9890
}

counts/human_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ func TestMetric(t *testing.T) {
5252
{12345678900000000000, "12346", "Pcd"}, // Not ideal, but ok
5353
{0xffffffffffffffff, "18447", "Pcd"}, // Not ideal, but ok
5454
} {
55-
number, unit := counts.Metric.Format(ht.n, "cd")
55+
number, unit := counts.Metric.FormatNumber(ht.n, "cd")
5656
assert.Equalf(ht.number, number, "Number for %d in metric", ht.n)
5757
assert.Equalf(ht.unit, unit, "Unit for %d in metric", ht.n)
5858
if ht.n < 0xffffffff {
5959
c := counts.NewCount32(ht.n)
60-
number, unit := c.Human(counts.Metric, "cd")
60+
number, unit := counts.Metric.Format(c, "cd")
6161
assert.Equalf(ht.number, number, "Number for Count32(%d) in metric", ht.n)
6262
assert.Equalf(ht.unit, unit, "Unit for Count32(%d) in metric", ht.n)
6363
}
6464
if ht.n < 0xffffffffffffffff {
6565
c := counts.NewCount64(ht.n)
66-
number, unit := c.Human(counts.Metric, "cd")
66+
number, unit := counts.Metric.Format(c, "cd")
6767
assert.Equalf(ht.number, number, "Number for Count64(%d) in metric", ht.n)
6868
assert.Equalf(ht.unit, unit, "Unit for Count64(%d) in metric", ht.n)
6969
}
@@ -91,18 +91,18 @@ func TestBinary(t *testing.T) {
9191
{1152921504606846976, "1024", "PiB"},
9292
{0xffffffffffffffff, "16384", "PiB"},
9393
} {
94-
number, unit := counts.Binary.Format(ht.n, "B")
94+
number, unit := counts.Binary.FormatNumber(ht.n, "B")
9595
assert.Equalf(ht.number, number, "Number for %d in binary", ht.n)
9696
assert.Equalf(ht.unit, unit, "Unit for %d in binary", ht.n)
9797
if ht.n < 0xffffffff {
9898
c := counts.NewCount32(ht.n)
99-
number, unit := c.Human(counts.Binary, "B")
99+
number, unit := counts.Binary.Format(c, "B")
100100
assert.Equalf(ht.number, number, "Number for Count32(%d) in binary", ht.n)
101101
assert.Equalf(ht.unit, unit, "Unit for Count32(%d) in binary", ht.n)
102102
}
103103
if ht.n < 0xffffffffffffffff {
104104
c := counts.NewCount64(ht.n)
105-
number, unit := c.Human(counts.Binary, "B")
105+
number, unit := counts.Binary.Format(c, "B")
106106
assert.Equalf(ht.number, number, "Number for Count64(%d) in binary", ht.n)
107107
assert.Equalf(ht.unit, unit, "Unit for Count64(%d) in binary", ht.n)
108108
}
@@ -113,7 +113,7 @@ func TestLimits32(t *testing.T) {
113113
assert := assert.New(t)
114114

115115
c := counts.NewCount32(0xffffffff)
116-
number, unit := c.Human(counts.Metric, "cd")
116+
number, unit := counts.Metric.Format(c, "cd")
117117
assert.Equalf("∞", number, "Number for Count32(0xffffffff) in metric")
118118
assert.Equalf("cd", unit, "Unit for Count32(0xffffffff) in metric")
119119
}
@@ -122,7 +122,7 @@ func TestLimits64(t *testing.T) {
122122
assert := assert.New(t)
123123

124124
c := counts.NewCount64(0xffffffffffffffff)
125-
number, unit := c.Human(counts.Metric, "B")
125+
number, unit := counts.Metric.Format(c, "B")
126126
assert.Equalf("∞", number, "Number for Count64(0xffffffffffffffff) in metric")
127127
assert.Equalf("B", unit, "Unit for Count64(0xffffffffffffffff) in metric")
128128
}

sizes/output.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (l *item) Emit(t *table) {
131131
if !interesting {
132132
return
133133
}
134-
valueString, unitString := l.value.Human(l.humaner, l.unit)
134+
valueString, unitString := l.humaner.Format(l.value, l.unit)
135135
t.formatRow(
136136
l.name, t.footnotes.CreateCitation(l.Footnote(t.nameStyle)),
137137
valueString, unitString,

0 commit comments

Comments
 (0)