aboutsummaryrefslogtreecommitdiff
path: root/weed/util/bytes.go
diff options
context:
space:
mode:
authorChris Lu <chrislusf@users.noreply.github.com>2021-04-26 21:54:09 -0700
committerGitHub <noreply@github.com>2021-04-26 21:54:09 -0700
commit400c8ef8d88eae27a1abbef3da0badd8ef1bab6d (patch)
tree621d6f4f432cefe75c455939e084193eb7462001 /weed/util/bytes.go
parent86185262bb86e31f9a2f71e85d02df2502c7ad40 (diff)
parentcf552417a7a422d1313c53972fd1175684e758e0 (diff)
downloadseaweedfs-400c8ef8d88eae27a1abbef3da0badd8ef1bab6d.tar.xz
seaweedfs-400c8ef8d88eae27a1abbef3da0badd8ef1bab6d.zip
Merge pull request #2025 from bingoohuang/master
improvement for minFreeSpace argument
Diffstat (limited to 'weed/util/bytes.go')
-rw-r--r--weed/util/bytes.go95
1 files changed, 95 insertions, 0 deletions
diff --git a/weed/util/bytes.go b/weed/util/bytes.go
index c2a4df108..26da91033 100644
--- a/weed/util/bytes.go
+++ b/weed/util/bytes.go
@@ -7,6 +7,10 @@ import (
"encoding/base64"
"fmt"
"io"
+ "math"
+ "strconv"
+ "strings"
+ "unicode"
)
// BytesToHumanReadable returns the converted human readable representation of the bytes.
@@ -161,3 +165,94 @@ func NewBytesReader(b []byte) *BytesReader {
Reader: bytes.NewReader(b),
}
}
+
+// EmptyTo returns to if s is empty.
+func EmptyTo(s, to string) string {
+ if s == "" {
+ return to
+ }
+
+ return s
+}
+
+// IfElse works like b ? this : that.
+func IfElse(b bool, this, that string) string {
+ if b {
+ return this
+ }
+ return that
+}
+
+// ParseBytes parses a string representation of bytes into the number
+// of bytes it represents.
+//
+// See Also: Bytes, IBytes.
+//
+// ParseBytes("42MB") -> 42000000, nil
+// ParseBytes("42 MB") -> 42000000, nil
+// ParseBytes("42 mib") -> 44040192, nil
+func ParseBytes(s string) (uint64, error) {
+ lastDigit := 0
+ hasComma := false
+ for _, r := range s {
+ if !(unicode.IsDigit(r) || r == '.' || r == ',') {
+ break
+ }
+ if r == ',' {
+ hasComma = true
+ }
+ lastDigit++
+ }
+
+ num := s[:lastDigit]
+ if hasComma {
+ num = strings.Replace(num, ",", "", -1)
+ }
+
+ f, err := strconv.ParseFloat(num, 64)
+ if err != nil {
+ return 0, err
+ }
+
+ extra := strings.ToLower(strings.TrimSpace(s[lastDigit:]))
+ if m, ok := bytesSizeTable[extra]; ok {
+ f *= float64(m)
+ if f >= math.MaxUint64 {
+ return 0, fmt.Errorf("too large: %v", s)
+ }
+ return uint64(f), nil
+ }
+
+ return 0, fmt.Errorf("unhandled size name: %v", extra)
+}
+
+var bytesSizeTable = map[string]uint64{
+ "b": Byte, "kib": KiByte, "kb": KByte, "mib": MiByte, "mb": MByte, "gib": GiByte, "gb": GByte,
+ "tib": TiByte, "tb": TByte, "pib": PiByte, "pb": PByte, "eib": EiByte, "eb": EByte,
+ // Without suffix
+ "": Byte, "ki": KiByte, "k": KByte, "mi": MiByte, "m": MByte, "gi": GiByte, "g": GByte,
+ "ti": TiByte, "t": TByte, "pi": PiByte, "p": PByte, "ei": EiByte, "e": EByte,
+}
+
+// IEC Sizes.
+// kibis of bits
+const (
+ Byte = 1 << (iota * 10)
+ KiByte
+ MiByte
+ GiByte
+ TiByte
+ PiByte
+ EiByte
+)
+
+// SI Sizes.
+const (
+ IByte = 1
+ KByte = IByte * 1000
+ MByte = KByte * 1000
+ GByte = MByte * 1000
+ TByte = GByte * 1000
+ PByte = TByte * 1000
+ EByte = PByte * 1000
+)