Browse code

pkg/units: Add FromHumanSize

This does the "reverse" of HumanSize, i.e. maps a string to an int64
using SI prefixes for the extension.

Docker-DCO-1.1-Signed-off-by: Alexander Larsson <alexl@redhat.com> (github: alexlarsson)

Alexander Larsson authored on 2014/03/31 16:30:20
Showing 2 changed files
... ...
@@ -21,6 +21,42 @@ func HumanSize(size int64) string {
21 21
 	return fmt.Sprintf("%.4g %s", sizef, units[i])
22 22
 }
23 23
 
24
+// FromHumanSize returns an integer from a human-readable specification of a size
25
+// using SI standard (eg. "44kB", "17MB")
26
+func FromHumanSize(size string) (int64, error) {
27
+	re, error := regexp.Compile("^(\\d+)([kKmMgGtTpP])?[bB]?$")
28
+	if error != nil {
29
+		return -1, fmt.Errorf("%s does not specify not a size", size)
30
+	}
31
+
32
+	matches := re.FindStringSubmatch(size)
33
+
34
+	if len(matches) != 3 {
35
+		return -1, fmt.Errorf("Invalid size: '%s'", size)
36
+	}
37
+
38
+	theSize, error := strconv.ParseInt(matches[1], 10, 0)
39
+	if error != nil {
40
+		return -1, error
41
+	}
42
+
43
+	unit := strings.ToLower(matches[2])
44
+
45
+	if unit == "k" {
46
+		theSize *= 1000
47
+	} else if unit == "m" {
48
+		theSize *= 1000 * 1000
49
+	} else if unit == "g" {
50
+		theSize *= 1000 * 1000 * 1000
51
+	} else if unit == "t" {
52
+		theSize *= 1000 * 1000 * 1000 * 1000
53
+	} else if unit == "p" {
54
+		theSize *= 1000 * 1000 * 1000 * 1000 * 1000
55
+	}
56
+
57
+	return theSize, nil
58
+}
59
+
24 60
 // Parses a human-readable string representing an amount of RAM
25 61
 // in bytes, kibibytes, mebibytes or gibibytes, and returns the
26 62
 // number of bytes, or -1 if the string is unparseable.
... ...
@@ -20,6 +20,41 @@ func TestHumanSize(t *testing.T) {
20 20
 	}
21 21
 }
22 22
 
23
+func TestFromHumanSize(t *testing.T) {
24
+	assertFromHumanSize(t, "32", false, 32)
25
+	assertFromHumanSize(t, "32b", false, 32)
26
+	assertFromHumanSize(t, "32B", false, 32)
27
+	assertFromHumanSize(t, "32k", false, 32*1000)
28
+	assertFromHumanSize(t, "32K", false, 32*1000)
29
+	assertFromHumanSize(t, "32kb", false, 32*1000)
30
+	assertFromHumanSize(t, "32Kb", false, 32*1000)
31
+	assertFromHumanSize(t, "32Mb", false, 32*1000*1000)
32
+	assertFromHumanSize(t, "32Gb", false, 32*1000*1000*1000)
33
+	assertFromHumanSize(t, "32Tb", false, 32*1000*1000*1000*1000)
34
+	assertFromHumanSize(t, "8Pb", false, 8*1000*1000*1000*1000*1000)
35
+
36
+	assertFromHumanSize(t, "", true, -1)
37
+	assertFromHumanSize(t, "hello", true, -1)
38
+	assertFromHumanSize(t, "-32", true, -1)
39
+	assertFromHumanSize(t, " 32 ", true, -1)
40
+	assertFromHumanSize(t, "32 mb", true, -1)
41
+	assertFromHumanSize(t, "32m b", true, -1)
42
+	assertFromHumanSize(t, "32bm", true, -1)
43
+}
44
+
45
+func assertFromHumanSize(t *testing.T, size string, expectError bool, expectedBytes int64) {
46
+	actualBytes, err := FromHumanSize(size)
47
+	if (err != nil) && !expectError {
48
+		t.Errorf("Unexpected error parsing '%s': %s", size, err)
49
+	}
50
+	if (err == nil) && expectError {
51
+		t.Errorf("Expected to get an error parsing '%s', but got none (bytes=%d)", size, actualBytes)
52
+	}
53
+	if actualBytes != expectedBytes {
54
+		t.Errorf("Expected '%s' to parse as %d bytes, got %d", size, expectedBytes, actualBytes)
55
+	}
56
+}
57
+
23 58
 func TestRAMInBytes(t *testing.T) {
24 59
 	assertRAMInBytes(t, "32", false, 32)
25 60
 	assertRAMInBytes(t, "32b", false, 32)