Browse Source

pkg/units: Refactored common code to single func

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)
Francisco Carriedo 11 years ago
parent
commit
386d300a6e
1 changed files with 23 additions and 38 deletions
  1. 23 38
      pkg/units/size.go

+ 23 - 38
pkg/units/size.go

@@ -7,6 +7,11 @@ import (
 	"strings"
 	"strings"
 )
 )
 
 
+const (
+	decimalKUnit = 1000
+	binaryKUnit  = 1024
+)
+
 var sizeRegex *regexp.Regexp
 var sizeRegex *regexp.Regexp
 
 
 func init() {
 func init() {
@@ -30,36 +35,10 @@ func HumanSize(size int64) string {
 	return fmt.Sprintf("%.4g %s", sizef, bytePrefixes[i])
 	return fmt.Sprintf("%.4g %s", sizef, bytePrefixes[i])
 }
 }
 
 
-// FromHumanSize returns an integer from a human-readable specification of a size
-// using SI standard (eg. "44kB", "17MB")
+// FromHumanSize returns an integer from a human-readable specification of a
+// size using SI standard (eg. "44kB", "17MB")
 func FromHumanSize(size string) (int64, error) {
 func FromHumanSize(size string) (int64, error) {
-	matches := sizeRegex.FindStringSubmatch(size)
-
-	if len(matches) != 3 {
-		return -1, fmt.Errorf("Invalid size: '%s'", size)
-	}
-
-	theSize, err := strconv.ParseInt(matches[1], 10, 0)
-	if err != nil {
-		return -1, err
-	}
-
-	unit := strings.ToLower(matches[2])
-
-	switch unit {
-	case "k":
-		theSize *= 1000
-	case "m":
-		theSize *= 1000 * 1000
-	case "g":
-		theSize *= 1000 * 1000 * 1000
-	case "t":
-		theSize *= 1000 * 1000 * 1000 * 1000
-	case "p":
-		theSize *= 1000 * 1000 * 1000 * 1000 * 1000
-	}
-
-	return theSize, nil
+	return parseSize(size, decimalKUnit)
 }
 }
 
 
 // Parses a human-readable string representing an amount of RAM
 // Parses a human-readable string representing an amount of RAM
@@ -67,31 +46,37 @@ func FromHumanSize(size string) (int64, error) {
 // returns the number of bytes, or -1 if the string is unparseable.
 // returns the number of bytes, or -1 if the string is unparseable.
 // Units are case-insensitive, and the 'b' suffix is optional.
 // Units are case-insensitive, and the 'b' suffix is optional.
 func RAMInBytes(size string) (int64, error) {
 func RAMInBytes(size string) (int64, error) {
+	return parseSize(size, binaryKUnit)
+}
+
+// Parses the human-readable size string into the amount it represents given
+// the desired kilo unit [decimalKiloUnit=1000|binaryKiloUnit=1024]
+func parseSize(size string, kUnit int64) (int64, error) {
 	matches := sizeRegex.FindStringSubmatch(size)
 	matches := sizeRegex.FindStringSubmatch(size)
 
 
 	if len(matches) != 3 {
 	if len(matches) != 3 {
 		return -1, fmt.Errorf("Invalid size: '%s'", size)
 		return -1, fmt.Errorf("Invalid size: '%s'", size)
 	}
 	}
 
 
-	memLimit, err := strconv.ParseInt(matches[1], 10, 0)
+	theSize, err := strconv.ParseInt(matches[1], 10, 0)
 	if err != nil {
 	if err != nil {
 		return -1, err
 		return -1, err
 	}
 	}
 
 
-	unit := strings.ToLower(matches[2])
+	unitPrefix := strings.ToLower(matches[2])
 
 
-	switch unit {
+	switch unitPrefix {
 	case "k":
 	case "k":
-		memLimit *= 1024
+		theSize *= kUnit
 	case "m":
 	case "m":
-		memLimit *= 1024 * 1024
+		theSize *= kUnit * kUnit
 	case "g":
 	case "g":
-		memLimit *= 1024 * 1024 * 1024
+		theSize *= kUnit * kUnit * kUnit
 	case "t":
 	case "t":
-		memLimit *= 1024 * 1024 * 1024 * 1024
+		theSize *= kUnit * kUnit * kUnit * kUnit
 	case "p":
 	case "p":
-		memLimit *= 1024 * 1024 * 1024 * 1024 * 1024
+		theSize *= kUnit * kUnit * kUnit * kUnit * kUnit
 	}
 	}
 
 
-	return memLimit, nil
+	return theSize, nil
 }
 }