diff options
author | Wim <wim@42.be> | 2017-02-18 23:00:46 +0100 |
---|---|---|
committer | Wim <wim@42.be> | 2017-02-18 23:11:48 +0100 |
commit | 930b639cc9cd2d2873302f30303378c0e53816a8 (patch) | |
tree | 8cd3f1d464fb5d4e5607fe16255c35a31a9d8b62 /vendor/github.com/labstack/gommon/bytes | |
parent | 58483ea70c2c99a352592c5e50686fb03985650e (diff) | |
download | matterbridge-msglm-930b639cc9cd2d2873302f30303378c0e53816a8.tar.gz matterbridge-msglm-930b639cc9cd2d2873302f30303378c0e53816a8.tar.bz2 matterbridge-msglm-930b639cc9cd2d2873302f30303378c0e53816a8.zip |
Update vendor
Diffstat (limited to 'vendor/github.com/labstack/gommon/bytes')
-rw-r--r-- | vendor/github.com/labstack/gommon/bytes/LICENSE | 22 | ||||
-rw-r--r-- | vendor/github.com/labstack/gommon/bytes/bytes.go | 106 |
2 files changed, 128 insertions, 0 deletions
diff --git a/vendor/github.com/labstack/gommon/bytes/LICENSE b/vendor/github.com/labstack/gommon/bytes/LICENSE new file mode 100644 index 00000000..d2ae3edf --- /dev/null +++ b/vendor/github.com/labstack/gommon/bytes/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 labstack + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/vendor/github.com/labstack/gommon/bytes/bytes.go b/vendor/github.com/labstack/gommon/bytes/bytes.go new file mode 100644 index 00000000..fd97e6d1 --- /dev/null +++ b/vendor/github.com/labstack/gommon/bytes/bytes.go @@ -0,0 +1,106 @@ +package bytes + +import ( + "fmt" + "regexp" + "strconv" +) + +type ( + Bytes struct { + } +) + +const ( + B = 1 << (10 * iota) + KB + MB + GB + TB + PB + EB +) + +var ( + pattern = regexp.MustCompile(`(?i)^(-?\d+)([KMGTP]B?|B)$`) + global = New() +) + +// New creates a Bytes instance. +func New() *Bytes { + return &Bytes{} +} + +// Format formats bytes integer to human readable string. +// For example, 31323 bytes will return 30.59KB. +func (*Bytes) Format(b int64) string { + multiple := "" + value := float64(b) + + switch { + case b < KB: + return strconv.FormatInt(b, 10) + "B" + case b < MB: + value /= KB + multiple = "KB" + case b < MB: + value /= KB + multiple = "KB" + case b < GB: + value /= MB + multiple = "MB" + case b < TB: + value /= GB + multiple = "GB" + case b < PB: + value /= TB + multiple = "TB" + case b < EB: + value /= PB + multiple = "PB" + } + + return fmt.Sprintf("%.02f%s", value, multiple) +} + +// Parse parses human readable bytes string to bytes integer. +// For example, 6GB (6G is also valid) will return 6442450944. +func (*Bytes) Parse(value string) (i int64, err error) { + parts := pattern.FindStringSubmatch(value) + if len(parts) < 3 { + return 0, fmt.Errorf("error parsing value=%s", value) + } + bytesString := parts[1] + multiple := parts[2] + bytes, err := strconv.ParseInt(bytesString, 10, 64) + if err != nil { + return + } + + switch multiple { + case "B": + return bytes * B, nil + case "K", "KB": + return bytes * KB, nil + case "M", "MB": + return bytes * MB, nil + case "G", "GB": + return bytes * GB, nil + case "T", "TB": + return bytes * TB, nil + case "P", "PB": + return bytes * PB, nil + } + + return +} + +// Format wraps global Bytes's Format function. +func Format(b int64) string { + return global.Format(b) +} + +// Parse wraps global Bytes's Parse function. +func Parse(val string) (int64, error) { + return global.Parse(val) +} |