[lxc-devel] [lxd/master] util improvements: ParseByteSizeString(), ParseBitSizeString(), add Int64InSlice()

brauner on Github lxc-bot at linuxcontainers.org
Sat Jan 14 10:56:00 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 640 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20170114/0cc28520/attachment.bin>
-------------- next part --------------
From 842063c8aa876846ca4e40df79cf42442d616c20 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Fri, 13 Jan 2017 17:01:41 +0100
Subject: [PATCH 1/3] shared/util: add Int64InSlice()

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 shared/util.go | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/shared/util.go b/shared/util.go
index 351bbd6..a2d6204 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -394,6 +394,15 @@ func IntInSlice(key int, list []int) bool {
 	return false
 }
 
+func Int64InSlice(key int64, list []int64) bool {
+	for _, entry := range list {
+		if entry == key {
+			return true
+		}
+	}
+	return false
+}
+
 func IsTrue(value string) bool {
 	if StringInSlice(strings.ToLower(value), []string{"true", "1", "yes", "on"}) {
 		return true

From f5163b74c3829a3071eaf354447beddfb7eedaef Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Fri, 13 Jan 2017 17:02:17 +0100
Subject: [PATCH 2/3] shared/util: improve ParseByteSizeString()

- handle strings that are already given in bytes
- turn suffix to upper letters to have a uniform handle on e.g. "kB", "kb", and
  "KB"
- remove whitespace that might have existed between value and unit

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 shared/util.go | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/shared/util.go b/shared/util.go
index a2d6204..9cddcd8 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -634,11 +634,24 @@ func ParseByteSizeString(input string) (int64, error) {
 		return -1, fmt.Errorf("Invalid value: %s", input)
 	}
 
+	isInBytes := strings.HasSuffix(strings.ToUpper(input), "BYTES")
+
 	// Extract the suffix
 	suffix := input[len(input)-2:]
+	if isInBytes {
+		suffix = input[len(input)-len("BYTES"):]
+	}
 
 	// Extract the value
 	value := input[0 : len(input)-2]
+	if isInBytes {
+		value = input[0 : len(input)-len("BYTES")]
+	}
+
+	// COMMENT(brauner): Remove any whitespace that might have been left
+	// between the value and the unit.
+	value = strings.TrimRight(value, " ")
+
 	valueInt, err := strconv.ParseInt(value, 10, 64)
 	if err != nil {
 		return -1, fmt.Errorf("Invalid integer: %s", input)
@@ -648,10 +661,14 @@ func ParseByteSizeString(input string) (int64, error) {
 		return -1, fmt.Errorf("Invalid value: %d", valueInt)
 	}
 
+	if isInBytes {
+		return valueInt, nil
+	}
+
 	// Figure out the multiplicator
 	multiplicator := int64(0)
-	switch suffix {
-	case "kB":
+	switch strings.ToUpper(suffix) {
+	case "KB":
 		multiplicator = 1024
 	case "MB":
 		multiplicator = 1024 * 1024

From ea405551cf8074266a9d652b822cd0f369844518 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Sat, 14 Jan 2017 11:46:13 +0100
Subject: [PATCH 3/3] shared/util: improve ParseBitSizeString()

- turn suffix to upper letters to have a uniform handle on e.g. "kBit", "KBIT"
  etc.

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 shared/util.go | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

diff --git a/shared/util.go b/shared/util.go
index 9cddcd8..b17b01d 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -703,6 +703,11 @@ func ParseBitSizeString(input string) (int64, error) {
 
 	// Extract the value
 	value := input[0 : len(input)-4]
+
+	// COMMENT(brauner): Remove any whitespace that might have been left
+	// between the value and the unit.
+	value = strings.TrimRight(value, " ")
+
 	valueInt, err := strconv.ParseInt(value, 10, 64)
 	if err != nil {
 		return -1, fmt.Errorf("Invalid integer: %s", input)
@@ -714,18 +719,18 @@ func ParseBitSizeString(input string) (int64, error) {
 
 	// Figure out the multiplicator
 	multiplicator := int64(0)
-	switch suffix {
-	case "kbit":
+	switch strings.ToUpper(suffix) {
+	case "KBIT":
 		multiplicator = 1000
-	case "Mbit":
+	case "MBIT":
 		multiplicator = 1000 * 1000
-	case "Gbit":
+	case "GBIT":
 		multiplicator = 1000 * 1000 * 1000
-	case "Tbit":
+	case "TBIT":
 		multiplicator = 1000 * 1000 * 1000 * 1000
-	case "Pbit":
+	case "PBIT":
 		multiplicator = 1000 * 1000 * 1000 * 1000 * 1000
-	case "Ebit":
+	case "EBIT":
 		multiplicator = 1000 * 1000 * 1000 * 1000 * 1000 * 1000
 	default:
 		return -1, fmt.Errorf("Unsupported suffix: %s", suffix)


More information about the lxc-devel mailing list