Skip to content

Commit 11dc4c5

Browse files
committed
string to bytes value formatter
1 parent 063a60c commit 11dc4c5

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

format.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
2+
// go-aah/essentials source code and usage is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package ess
6+
7+
import (
8+
"fmt"
9+
"regexp"
10+
"strconv"
11+
"strings"
12+
)
13+
14+
// Byte unit value
15+
const (
16+
ByteSize = 1.0
17+
KiloByteSize = 1024 * ByteSize
18+
MegaByteSize = 1024 * KiloByteSize
19+
GigaByteSize = 1024 * MegaByteSize
20+
TeraByteSize = 1024 * GigaByteSize
21+
)
22+
23+
var sizePattern = regexp.MustCompile(`(?i)^(\d+)([kmgt]?[i]?[b?|b])$`)
24+
25+
// StrToBytes method returns bytes value for given string value.
26+
// For e.g.:
27+
// 2mb ==> 2097152 bytes
28+
// 2MB ==> 2097152 bytes
29+
// 2MiB ==> 2097152 bytes
30+
func StrToBytes(value string) (int64, error) {
31+
segments := sizePattern.FindStringSubmatch(strings.TrimSpace(value))
32+
if len(segments) < 3 {
33+
return 0, fmt.Errorf("format: invalid input '%s'", value)
34+
}
35+
36+
unitValue, err := strconv.ParseInt(segments[1], 10, 0)
37+
if err != nil {
38+
return 0, fmt.Errorf("format: unable to parse '%d'", unitValue)
39+
}
40+
41+
var bytes int64
42+
unit := strings.ToLower(segments[2])
43+
switch unit[:1] {
44+
case "t":
45+
bytes = unitValue * TeraByteSize
46+
case "g":
47+
bytes = unitValue * GigaByteSize
48+
case "m":
49+
bytes = unitValue * MegaByteSize
50+
case "k":
51+
bytes = unitValue * KiloByteSize
52+
case "b":
53+
bytes = unitValue * ByteSize
54+
}
55+
56+
return bytes, nil
57+
}

format_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
2+
// go-aah/essentials source code and usage is governed by a MIT style
3+
// license that can be found in the LICENSE file.
4+
5+
package ess
6+
7+
import (
8+
"testing"
9+
10+
"aahframework.org/test/assert"
11+
)
12+
13+
func TestBStrToBytes(t *testing.T) {
14+
checkBytesValue(t, "2b", int64(2))
15+
checkBytesValue(t, "2B", int64(2))
16+
checkBytesValue(t, "2b", int64(2))
17+
checkBytesValue(t, "2B", int64(2))
18+
}
19+
20+
func TestKBStrToBytes(t *testing.T) {
21+
checkBytesValue(t, "2kb", int64(2048))
22+
checkBytesValue(t, "2KB", int64(2048))
23+
checkBytesValue(t, "2kib", int64(2048))
24+
checkBytesValue(t, "2KiB", int64(2048))
25+
}
26+
27+
func TestMBStrToBytes(t *testing.T) {
28+
checkBytesValue(t, "2mb", int64(2097152))
29+
checkBytesValue(t, "2MB", int64(2097152))
30+
checkBytesValue(t, "2mib", int64(2097152))
31+
checkBytesValue(t, "2MiB", int64(2097152))
32+
}
33+
34+
func TestGBStrToBytes(t *testing.T) {
35+
checkBytesValue(t, "2gb", int64(2147483648))
36+
checkBytesValue(t, "2GB", int64(2147483648))
37+
checkBytesValue(t, "2Gib", int64(2147483648))
38+
checkBytesValue(t, "2GiB", int64(2147483648))
39+
}
40+
41+
func TestTBStrToBytes(t *testing.T) {
42+
checkBytesValue(t, "2tb", int64(2199023255552))
43+
checkBytesValue(t, "2TB", int64(2199023255552))
44+
checkBytesValue(t, "2Tib", int64(2199023255552))
45+
checkBytesValue(t, "2TiB", int64(2199023255552))
46+
}
47+
48+
func TestErrStrToBytes(t *testing.T) {
49+
v1, err := StrToBytes("2")
50+
assert.NotNil(t, err)
51+
assert.Equal(t, "format: invalid input '2'", err.Error())
52+
assert.Equal(t, int64(0), v1)
53+
}
54+
55+
func checkBytesValue(t *testing.T, value string, expt int64) {
56+
v, err := StrToBytes(value)
57+
assert.Nil(t, err)
58+
assert.Equal(t, expt, v)
59+
}

0 commit comments

Comments
 (0)