From 317a1141c0d482a7c0f36ed29e572a567bef8d13 Mon Sep 17 00:00:00 2001 From: "Anand Babu (AB) Periasamy" Date: Sat, 28 Feb 2015 15:53:44 -0800 Subject: [PATCH] removed unitconv --- pkg/utils/unitconv/unitconv.go | 105 -------------------------- pkg/utils/unitconv/unitconv_test.go | 112 ---------------------------- 2 files changed, 217 deletions(-) delete mode 100644 pkg/utils/unitconv/unitconv.go delete mode 100644 pkg/utils/unitconv/unitconv_test.go diff --git a/pkg/utils/unitconv/unitconv.go b/pkg/utils/unitconv/unitconv.go deleted file mode 100644 index 7e1f59bd9..000000000 --- a/pkg/utils/unitconv/unitconv.go +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Mini Object Storage, (C) 2014 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package unitconv - -import ( - "errors" - "fmt" - "regexp" - "strconv" - "strings" -) - -// Units of various bytes in ascending order -const ( - UNIT_BYTE = 1 << (10 * iota) - UNIT_KILOBYTE - UNIT_MEGABYTE - UNIT_GIGABYTE - UNIT_TERABYTE - UNIT_PETABYTE -) - -// Convert bytes length in integer to human readable string -func BytesToString(bytes uint64) string { - var unit string = "B" - var value uint64 = 0 - - switch { - case bytes >= UNIT_TERABYTE: - unit = "TB" - value = uint64(bytes / UNIT_TERABYTE) - case bytes >= UNIT_GIGABYTE: - unit = "GB" - value = uint64(bytes / UNIT_GIGABYTE) - case bytes >= UNIT_MEGABYTE: - unit = "MB" - value = uint64(bytes / UNIT_MEGABYTE) - case bytes >= UNIT_KILOBYTE: - unit = "KB" - value = uint64(bytes / UNIT_KILOBYTE) - case bytes < UNIT_KILOBYTE && bytes >= UNIT_BYTE: - unit = "B" - value = uint64(bytes / UNIT_BYTE) - } - - return fmt.Sprintf("%d%s", value, unit) -} - -// Convert human readable string to bytes length in integer -func StringToBytes(s string) (uint64, error) { - var bytes uint64 - var err error - - bytes, err = strconv.ParseUint(s, 10, 64) - if err == nil { - return bytes, nil - } - - stringPattern, err := regexp.Compile(`(?i)^(-?\d+)([BKMGT])B?$`) - if err != nil { - return 0, err - } - - parts := stringPattern.FindStringSubmatch(strings.TrimSpace(s)) - if len(parts) < 2 { - return 0, errors.New("Incorrect string format must be K,KB,M,MB,G,GB") - } - - value, err := strconv.ParseUint(parts[1], 10, 0) - if err != nil || value < 1 { - return 0, err - } - - unit := strings.ToUpper(parts[2]) - switch unit { - case "T": - bytes = value * UNIT_TERABYTE - case "G": - bytes = value * UNIT_GIGABYTE - case "M": - bytes = value * UNIT_MEGABYTE - case "K": - bytes = value * UNIT_KILOBYTE - case "B": - bytes = value * UNIT_BYTE - default: - return 0, errors.New("Incorrect string format must be K,KB,M,MB,G,GB") - } - - return bytes, nil -} diff --git a/pkg/utils/unitconv/unitconv_test.go b/pkg/utils/unitconv/unitconv_test.go deleted file mode 100644 index c342e5d89..000000000 --- a/pkg/utils/unitconv/unitconv_test.go +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Mini Object Storage, (C) 2014 Minio, Inc. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package unitconv - -import ( - "testing" - - . "gopkg.in/check.v1" -) - -type MySuite struct{} - -var _ = Suite(&MySuite{}) - -func Test(t *testing.T) { TestingT(t) } - -func (s *MySuite) Test(c *C) { - value := BytesToString(100 * UNIT_BYTE) - c.Assert(value, Equals, "100B") - - value = BytesToString(100 * UNIT_KILOBYTE) - c.Assert(value, Equals, "100KB") - - value = BytesToString(100 * UNIT_MEGABYTE) - c.Assert(value, Equals, "100MB") - - value = BytesToString(100 * UNIT_GIGABYTE) - c.Assert(value, Equals, "100GB") - - value = BytesToString(100 * UNIT_TERABYTE) - c.Assert(value, Equals, "100TB") - - bytes, err := StringToBytes("100B") - c.Assert(err, IsNil) - c.Assert(bytes, Equals, uint64(100)) - - bytes, err = StringToBytes("100") - c.Assert(err, IsNil) - c.Assert(bytes, Equals, uint64(100)) - - bytes, err = StringToBytes("100KB") - c.Assert(err, IsNil) - c.Assert(bytes, Equals, uint64(100*UNIT_KILOBYTE)) - - bytes, err = StringToBytes("100K") - c.Assert(err, IsNil) - c.Assert(bytes, Equals, uint64(100*UNIT_KILOBYTE)) - - bytes, err = StringToBytes("100MB") - c.Assert(err, IsNil) - c.Assert(bytes, Equals, uint64(100*UNIT_MEGABYTE)) - - bytes, err = StringToBytes("100M") - c.Assert(err, IsNil) - c.Assert(bytes, Equals, uint64(100*UNIT_MEGABYTE)) - - bytes, err = StringToBytes("100GB") - c.Assert(err, IsNil) - c.Assert(bytes, Equals, uint64(100*UNIT_GIGABYTE)) - - bytes, err = StringToBytes("100G") - c.Assert(err, IsNil) - c.Assert(bytes, Equals, uint64(100*UNIT_GIGABYTE)) - - bytes, err = StringToBytes("100TB") - c.Assert(err, IsNil) - c.Assert(bytes, Equals, uint64(100*UNIT_TERABYTE)) - - bytes, err = StringToBytes("100T") - c.Assert(err, IsNil) - c.Assert(bytes, Equals, uint64(100*UNIT_TERABYTE)) - - bytes, err = StringToBytes("0") - c.Assert(err, IsNil) - c.Assert(bytes, Equals, uint64(0)) - - bytes, err = StringToBytes("23") - c.Assert(err, IsNil) - c.Assert(bytes, Equals, uint64(23)) - - bytes, err = StringToBytes("0TB") - c.Assert(err, IsNil) - c.Assert(bytes, Equals, uint64(0)) -} - -func (s *MySuite) TestBadInput(c *C) { - _, err := StringToBytes("") - c.Assert(err, Not(IsNil)) - - _, err = StringToBytes("HELLO") - c.Assert(err, Not(IsNil)) - - _, err = StringToBytes("-20B") - c.Assert(err, Not(IsNil)) - - _, err = StringToBytes("-20MB") - c.Assert(err, Not(IsNil)) -}