Both functions perform the same logic and they just vary on the base
multiplication units. We can refactor the common code into a single
place.
Docker-DCO-1.1-Signed-off-by: Francisco Carriedo <fcarriedo@gmail.com> (github: fcarriedo)
| ... | ... |
@@ -7,6 +7,11 @@ import ( |
| 7 | 7 |
"strings" |
| 8 | 8 |
) |
| 9 | 9 |
|
| 10 |
+const ( |
|
| 11 |
+ decimalKUnit = 1000 |
|
| 12 |
+ binaryKUnit = 1024 |
|
| 13 |
+) |
|
| 14 |
+ |
|
| 10 | 15 |
var sizeRegex *regexp.Regexp |
| 11 | 16 |
|
| 12 | 17 |
func init() {
|
| ... | ... |
@@ -30,36 +35,10 @@ func HumanSize(size int64) string {
|
| 30 | 30 |
return fmt.Sprintf("%.4g %s", sizef, bytePrefixes[i])
|
| 31 | 31 |
} |
| 32 | 32 |
|
| 33 |
-// FromHumanSize returns an integer from a human-readable specification of a size |
|
| 34 |
-// using SI standard (eg. "44kB", "17MB") |
|
| 33 |
+// FromHumanSize returns an integer from a human-readable specification of a |
|
| 34 |
+// size using SI standard (eg. "44kB", "17MB") |
|
| 35 | 35 |
func FromHumanSize(size string) (int64, error) {
|
| 36 |
- matches := sizeRegex.FindStringSubmatch(size) |
|
| 37 |
- |
|
| 38 |
- if len(matches) != 3 {
|
|
| 39 |
- return -1, fmt.Errorf("Invalid size: '%s'", size)
|
|
| 40 |
- } |
|
| 41 |
- |
|
| 42 |
- theSize, err := strconv.ParseInt(matches[1], 10, 0) |
|
| 43 |
- if err != nil {
|
|
| 44 |
- return -1, err |
|
| 45 |
- } |
|
| 46 |
- |
|
| 47 |
- unit := strings.ToLower(matches[2]) |
|
| 48 |
- |
|
| 49 |
- switch unit {
|
|
| 50 |
- case "k": |
|
| 51 |
- theSize *= 1000 |
|
| 52 |
- case "m": |
|
| 53 |
- theSize *= 1000 * 1000 |
|
| 54 |
- case "g": |
|
| 55 |
- theSize *= 1000 * 1000 * 1000 |
|
| 56 |
- case "t": |
|
| 57 |
- theSize *= 1000 * 1000 * 1000 * 1000 |
|
| 58 |
- case "p": |
|
| 59 |
- theSize *= 1000 * 1000 * 1000 * 1000 * 1000 |
|
| 60 |
- } |
|
| 61 |
- |
|
| 62 |
- return theSize, nil |
|
| 36 |
+ return parseSize(size, decimalKUnit) |
|
| 63 | 37 |
} |
| 64 | 38 |
|
| 65 | 39 |
// Parses a human-readable string representing an amount of RAM |
| ... | ... |
@@ -67,31 +46,37 @@ func FromHumanSize(size string) (int64, error) {
|
| 67 | 67 |
// returns the number of bytes, or -1 if the string is unparseable. |
| 68 | 68 |
// Units are case-insensitive, and the 'b' suffix is optional. |
| 69 | 69 |
func RAMInBytes(size string) (int64, error) {
|
| 70 |
+ return parseSize(size, binaryKUnit) |
|
| 71 |
+} |
|
| 72 |
+ |
|
| 73 |
+// Parses the human-readable size string into the amount it represents given |
|
| 74 |
+// the desired kilo unit [decimalKiloUnit=1000|binaryKiloUnit=1024] |
|
| 75 |
+func parseSize(size string, kUnit int64) (int64, error) {
|
|
| 70 | 76 |
matches := sizeRegex.FindStringSubmatch(size) |
| 71 | 77 |
|
| 72 | 78 |
if len(matches) != 3 {
|
| 73 | 79 |
return -1, fmt.Errorf("Invalid size: '%s'", size)
|
| 74 | 80 |
} |
| 75 | 81 |
|
| 76 |
- memLimit, err := strconv.ParseInt(matches[1], 10, 0) |
|
| 82 |
+ theSize, err := strconv.ParseInt(matches[1], 10, 0) |
|
| 77 | 83 |
if err != nil {
|
| 78 | 84 |
return -1, err |
| 79 | 85 |
} |
| 80 | 86 |
|
| 81 |
- unit := strings.ToLower(matches[2]) |
|
| 87 |
+ unitPrefix := strings.ToLower(matches[2]) |
|
| 82 | 88 |
|
| 83 |
- switch unit {
|
|
| 89 |
+ switch unitPrefix {
|
|
| 84 | 90 |
case "k": |
| 85 |
- memLimit *= 1024 |
|
| 91 |
+ theSize *= kUnit |
|
| 86 | 92 |
case "m": |
| 87 |
- memLimit *= 1024 * 1024 |
|
| 93 |
+ theSize *= kUnit * kUnit |
|
| 88 | 94 |
case "g": |
| 89 |
- memLimit *= 1024 * 1024 * 1024 |
|
| 95 |
+ theSize *= kUnit * kUnit * kUnit |
|
| 90 | 96 |
case "t": |
| 91 |
- memLimit *= 1024 * 1024 * 1024 * 1024 |
|
| 97 |
+ theSize *= kUnit * kUnit * kUnit * kUnit |
|
| 92 | 98 |
case "p": |
| 93 |
- memLimit *= 1024 * 1024 * 1024 * 1024 * 1024 |
|
| 99 |
+ theSize *= kUnit * kUnit * kUnit * kUnit * kUnit |
|
| 94 | 100 |
} |
| 95 | 101 |
|
| 96 |
- return memLimit, nil |
|
| 102 |
+ return theSize, nil |
|
| 97 | 103 |
} |