Skip to content

Commit 5f422f8

Browse files
committed
[fmtutil] Code refactoring
1 parent 67ed7c1 commit 5f422f8

4 files changed

Lines changed: 70 additions & 34 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- **`[ease]`** Fixed `SineIn` and `SineOut` calculation
1414
- **`[ease]`** Fixed overshoot constant for `BackIn`, `BackOut`, and `BackInOut`
1515
- **`[fmtc]`** Fixed Off-by-One bug in 256-Color range validation
16+
- **`[fmtutil]`** Method `ParseSize` now returns parsing error
1617
- **`[timeutil]`** Fixed `%c` output in `Format`
1718
- **`[timeutil]`** Fixed bug with formatting timezone offset
1819
- **`[timeutil]`** Fixed ISO week calculation
@@ -27,6 +28,7 @@
2728
- **`[errors]`** Code refactoring
2829
- **`[events]`** Code refactoring
2930
- **`[fmtc]`** Code refactoring
31+
- **`[fmtutil]`** Code refactoring
3032
- **`[log]`** Code refactoring
3133
- **`[req]`** Code refactoring
3234

fmtutil/example_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,14 @@ func ExampleParseSize() {
119119
s2 := "34Mb"
120120
s3 := "2.2 GB"
121121

122-
fmt.Printf("%s → %d\n", s1, ParseSize(s1))
123-
fmt.Printf("%s → %d\n", s2, ParseSize(s2))
124-
fmt.Printf("%s → %d\n", s3, ParseSize(s3))
122+
si1, _ := ParseSize(s1)
123+
fmt.Printf("%s → %d\n", s1, si1)
124+
125+
si2, _ := ParseSize(s2)
126+
fmt.Printf("%s → %d\n", s2, si2)
127+
128+
si3, _ := ParseSize(s3)
129+
fmt.Printf("%s → %d\n", s3, si3)
125130

126131
// Output:
127132
// 160 → 160

fmtutil/fmtutil.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,28 +162,23 @@ func PrettyBool(b bool, vals ...string) string {
162162

163163
// ParseSize parses a human-readable size string and returns the equivalent byte count
164164
// (e.g. "2.2 GB" → 2362232012).
165-
func ParseSize(size string) uint64 {
165+
func ParseSize(size string) (uint64, error) {
166166
v := strings.ToLower(strings.ReplaceAll(size, " ", ""))
167167
mod, suf := extractSizeInfo(v)
168168

169-
if suf == "" {
170-
num, err := strconv.ParseUint(size, 10, 64)
171-
172-
if err != nil {
173-
return 0
174-
}
169+
v = strings.TrimRight(v, suf)
175170

176-
return num
171+
if v == "" {
172+
return 0, fmt.Errorf("size has no digits")
177173
}
178174

179-
v = strings.TrimRight(v, suf)
180175
numFlt, err := strconv.ParseFloat(v, 64)
181176

182177
if err != nil {
183-
return 0
178+
return 0, err
184179
}
185180

186-
return uint64(numFlt * mod)
181+
return uint64(numFlt * mod), nil
187182
}
188183

189184
// Float rounds f to 2 decimal places below 10, or 1 decimal place above. Returns 0.0
@@ -353,8 +348,7 @@ func appendPrettySymbol(str, sep string) string {
353348

354349
// extractSizeInfo returns the byte multiplier and suffix for a size string
355350
func extractSizeInfo(s string) (float64, string) {
356-
var mod float64
357-
351+
mod := 1.0
358352
suf := strings.TrimLeft(s, "0123456789. ")
359353

360354
switch suf {
@@ -375,7 +369,7 @@ func extractSizeInfo(s string) (float64, string) {
375369
case "k":
376370
mod = 1000
377371
case "b":
378-
mod = 1
372+
// okay
379373
default:
380374
suf = ""
381375
}

fmtutil/fmtutil_test.go

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,58 @@ func (s *FmtUtilSuite) TestPrettyBool(c *C) {
9292
}
9393

9494
func (s *FmtUtilSuite) TestParseSize(c *C) {
95-
c.Assert(ParseSize("1 MB"), Equals, uint64(1024*1024))
96-
c.Assert(ParseSize("1 M"), Equals, uint64(1000*1000))
97-
c.Assert(ParseSize("2tb"), Equals, uint64(2*1024*1024*1024*1024))
98-
c.Assert(ParseSize("2t"), Equals, uint64(2*1000*1000*1000*1000))
99-
c.Assert(ParseSize("5gB"), Equals, uint64(5*1024*1024*1024))
100-
c.Assert(ParseSize("5g"), Equals, uint64(5*1000*1000*1000))
101-
c.Assert(ParseSize("13kb"), Equals, uint64(13*1024))
102-
c.Assert(ParseSize("13k"), Equals, uint64(13*1000))
103-
c.Assert(ParseSize("13kk"), Equals, uint64(13*1000*1000))
104-
c.Assert(ParseSize("13kkk"), Equals, uint64(13*1000*1000*1000))
105-
c.Assert(ParseSize("512"), Equals, uint64(512))
106-
c.Assert(ParseSize("kb"), Equals, uint64(0))
107-
c.Assert(ParseSize("123!"), Equals, uint64(0))
108-
109-
c.Assert(ParseSize(PrettySize(345)), Equals, uint64(345))
110-
c.Assert(ParseSize(PrettySize(1025)), Equals, uint64(1024))
111-
c.Assert(ParseSize(PrettySize(1024*1024)), Equals, uint64(1024*1024))
95+
size, err := ParseSize("1 MB")
96+
c.Assert(err, IsNil)
97+
c.Assert(size, Equals, uint64(1024*1024))
98+
99+
size, _ = ParseSize("2tb")
100+
c.Assert(size, Equals, uint64(2*1024*1024*1024*1024))
101+
102+
size, _ = ParseSize("5gB")
103+
c.Assert(size, Equals, uint64(5*1024*1024*1024))
104+
105+
size, _ = ParseSize("13kb")
106+
c.Assert(size, Equals, uint64(13*1024))
107+
108+
size, _ = ParseSize("1 M")
109+
c.Assert(size, Equals, uint64(1000*1000))
110+
111+
size, _ = ParseSize("2t")
112+
c.Assert(size, Equals, uint64(2*1000*1000*1000*1000))
113+
114+
size, _ = ParseSize("5g")
115+
c.Assert(size, Equals, uint64(5*1000*1000*1000))
116+
117+
size, _ = ParseSize("13k")
118+
c.Assert(size, Equals, uint64(13*1000))
119+
120+
size, _ = ParseSize("13kk")
121+
c.Assert(size, Equals, uint64(13*1000*1000))
122+
123+
size, _ = ParseSize("13kkk")
124+
c.Assert(size, Equals, uint64(13*1000*1000*1000))
125+
126+
size, _ = ParseSize("512")
127+
c.Assert(size, Equals, uint64(512))
128+
129+
size, _ = ParseSize(PrettySize(345))
130+
c.Assert(size, Equals, uint64(345))
131+
132+
size, _ = ParseSize(PrettySize(1025))
133+
c.Assert(size, Equals, uint64(1024))
134+
135+
size, _ = ParseSize(PrettySize(1024 * 1024))
136+
c.Assert(size, Equals, uint64(1024*1024))
137+
138+
size, err = ParseSize("kb")
139+
c.Assert(err, NotNil)
140+
c.Assert(err.Error(), Equals, `size has no digits`)
141+
c.Assert(size, Equals, uint64(0))
142+
143+
size, err = ParseSize("123!")
144+
c.Assert(err, NotNil)
145+
c.Assert(err.Error(), Equals, `strconv.ParseFloat: parsing "123!": invalid syntax`)
146+
c.Assert(size, Equals, uint64(0))
112147
}
113148

114149
func (s *FmtUtilSuite) TestFloat(c *C) {

0 commit comments

Comments
 (0)