Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions bytes/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func (*Bytes) FormatDecimal(value int64) string {
// For example, 6GiB (6Gi is also valid) will return 6442450944, and
// 6GB (6G is also valid) will return 6000000000.
func (b *Bytes) Parse(value string) (int64, error) {
value = strings.TrimSpace(value)

i, err := b.ParseBinary(value)
if err == nil {
Expand All @@ -134,6 +135,7 @@ func (b *Bytes) Parse(value string) (int64, error) {
// ParseBinary parses human readable bytes string to bytes integer.
// For example, 6GiB (6Gi is also valid) will return 6442450944.
func (*Bytes) ParseBinary(value string) (i int64, err error) {
value = strings.TrimSpace(value)
parts := patternBinary.FindStringSubmatch(value)
if len(parts) < 3 {
return 0, fmt.Errorf("error parsing value=%s", value)
Expand Down Expand Up @@ -166,6 +168,7 @@ func (*Bytes) ParseBinary(value string) (i int64, err error) {
// ParseDecimal parses human readable bytes string to bytes integer.
// For example, 6GB (6G is also valid) will return 6000000000.
func (*Bytes) ParseDecimal(value string) (i int64, err error) {
value = strings.TrimSpace(value)
parts := patternDecimal.FindStringSubmatch(value)
if len(parts) < 3 {
return 0, fmt.Errorf("error parsing value=%s", value)
Expand Down
10 changes: 10 additions & 0 deletions bytes/bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,13 @@ func TestBytesParse(t *testing.T) {
assert.Equal(t, int64(8000000000000000000), b)
}
}

func TestBytesParseTrimSpace(t *testing.T) {
b, err := Parse(" 12KiB ")
assert.NoError(t, err)
assert.Equal(t, int64(12288), b)

b, err = Parse("\t6GiB\n")
assert.NoError(t, err)
assert.Equal(t, int64(6442450944), b)
}
Loading