summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/labstack/gommon
diff options
context:
space:
mode:
authorWim <wim@42.be>2022-11-27 00:42:16 +0100
committerGitHub <noreply@github.com>2022-11-27 00:42:16 +0100
commit4fd0a7672777f0ed15692ae2ba47838208537558 (patch)
treeb119834a8b9ee78aa8f1b2ad05efa7da50516cbf /vendor/github.com/labstack/gommon
parent6da9d567dc9195e9a5211f23a6795a41f56a1bfc (diff)
downloadmatterbridge-msglm-4fd0a7672777f0ed15692ae2ba47838208537558.tar.gz
matterbridge-msglm-4fd0a7672777f0ed15692ae2ba47838208537558.tar.bz2
matterbridge-msglm-4fd0a7672777f0ed15692ae2ba47838208537558.zip
Update dependencies (#1929)
Diffstat (limited to 'vendor/github.com/labstack/gommon')
-rw-r--r--vendor/github.com/labstack/gommon/bytes/bytes.go173
-rw-r--r--vendor/github.com/labstack/gommon/log/log.go6
2 files changed, 144 insertions, 35 deletions
diff --git a/vendor/github.com/labstack/gommon/bytes/bytes.go b/vendor/github.com/labstack/gommon/bytes/bytes.go
index 2f6bcec6..b07e31cd 100644
--- a/vendor/github.com/labstack/gommon/bytes/bytes.go
+++ b/vendor/github.com/labstack/gommon/bytes/bytes.go
@@ -12,19 +12,31 @@ type (
Bytes struct{}
)
+// binary units (IEC 60027)
const (
_ = 1.0 << (10 * iota) // ignore first value by assigning to blank identifier
- KB
- MB
- GB
- TB
- PB
- EB
+ KiB
+ MiB
+ GiB
+ TiB
+ PiB
+ EiB
+)
+
+// decimal units (SI international system of units)
+const (
+ KB = 1000
+ MB = KB * 1000
+ GB = MB * 1000
+ TB = GB * 1000
+ PB = TB * 1000
+ EB = PB * 1000
)
var (
- pattern = regexp.MustCompile(`(?i)^(-?\d+(?:\.\d+)?)\s?([KMGTPE]B?|B?)$`)
- global = New()
+ patternBinary = regexp.MustCompile(`(?i)^(-?\d+(?:\.\d+)?)\s?([KMGTPE]iB?)$`)
+ patternDecimal = regexp.MustCompile(`(?i)^(-?\d+(?:\.\d+)?)\s?([KMGTPE]B?|B?)$`)
+ global = New()
)
// New creates a Bytes instance.
@@ -32,44 +44,97 @@ func New() *Bytes {
return &Bytes{}
}
-// Format formats bytes integer to human readable string.
+// Format formats bytes integer to human readable string according to IEC 60027.
+// For example, 31323 bytes will return 30.59KB.
+func (b *Bytes) Format(value int64) string {
+ return b.FormatBinary(value)
+}
+
+// FormatBinary formats bytes integer to human readable string according to IEC 60027.
// For example, 31323 bytes will return 30.59KB.
-func (*Bytes) Format(b int64) string {
+func (*Bytes) FormatBinary(value int64) string {
+ multiple := ""
+ val := float64(value)
+
+ switch {
+ case value >= EiB:
+ val /= EiB
+ multiple = "EiB"
+ case value >= PiB:
+ val /= PiB
+ multiple = "PiB"
+ case value >= TiB:
+ val /= TiB
+ multiple = "TiB"
+ case value >= GiB:
+ val /= GiB
+ multiple = "GiB"
+ case value >= MiB:
+ val /= MiB
+ multiple = "MiB"
+ case value >= KiB:
+ val /= KiB
+ multiple = "KiB"
+ case value == 0:
+ return "0"
+ default:
+ return strconv.FormatInt(value, 10) + "B"
+ }
+
+ return fmt.Sprintf("%.2f%s", val, multiple)
+}
+
+// FormatDecimal formats bytes integer to human readable string according to SI international system of units.
+// For example, 31323 bytes will return 31.32KB.
+func (*Bytes) FormatDecimal(value int64) string {
multiple := ""
- value := float64(b)
+ val := float64(value)
switch {
- case b >= EB:
- value /= EB
+ case value >= EB:
+ val /= EB
multiple = "EB"
- case b >= PB:
- value /= PB
+ case value >= PB:
+ val /= PB
multiple = "PB"
- case b >= TB:
- value /= TB
+ case value >= TB:
+ val /= TB
multiple = "TB"
- case b >= GB:
- value /= GB
+ case value >= GB:
+ val /= GB
multiple = "GB"
- case b >= MB:
- value /= MB
+ case value >= MB:
+ val /= MB
multiple = "MB"
- case b >= KB:
- value /= KB
+ case value >= KB:
+ val /= KB
multiple = "KB"
- case b == 0:
+ case value == 0:
return "0"
default:
- return strconv.FormatInt(b, 10) + "B"
+ return strconv.FormatInt(value, 10) + "B"
}
- return fmt.Sprintf("%.2f%s", value, multiple)
+ return fmt.Sprintf("%.2f%s", val, 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)
+// For example, 6GiB (6Gi is also valid) will return 6442450944, and
+// 6GB (6G is also valid) will return 6000000000.
+func (b *Bytes) Parse(value string) (int64, error) {
+
+ i, err := b.ParseBinary(value)
+ if err == nil {
+ return i, err
+ }
+
+ return b.ParseDecimal(value)
+}
+
+// ParseBinary parses human readable bytes string to bytes integer.
+// For example, 6GiB (6Gi is also valid) will return 6442450944.
+func (*Bytes) ParseBinary(value string) (i int64, err error) {
+ parts := patternBinary.FindStringSubmatch(value)
if len(parts) < 3 {
return 0, fmt.Errorf("error parsing value=%s", value)
}
@@ -81,8 +146,38 @@ func (*Bytes) Parse(value string) (i int64, err error) {
}
switch multiple {
+ case "KI", "KIB":
+ return int64(bytes * KiB), nil
+ case "MI", "MIB":
+ return int64(bytes * MiB), nil
+ case "GI", "GIB":
+ return int64(bytes * GiB), nil
+ case "TI", "TIB":
+ return int64(bytes * TiB), nil
+ case "PI", "PIB":
+ return int64(bytes * PiB), nil
+ case "EI", "EIB":
+ return int64(bytes * EiB), nil
default:
return int64(bytes), nil
+ }
+}
+
+// ParseDecimal parses human readable bytes string to bytes integer.
+// For example, 6GB (6G is also valid) will return 6000000000.
+func (*Bytes) ParseDecimal(value string) (i int64, err error) {
+ parts := patternDecimal.FindStringSubmatch(value)
+ if len(parts) < 3 {
+ return 0, fmt.Errorf("error parsing value=%s", value)
+ }
+ bytesString := parts[1]
+ multiple := strings.ToUpper(parts[2])
+ bytes, err := strconv.ParseFloat(bytesString, 64)
+ if err != nil {
+ return
+ }
+
+ switch multiple {
case "K", "KB":
return int64(bytes * KB), nil
case "M", "MB":
@@ -95,15 +190,27 @@ func (*Bytes) Parse(value string) (i int64, err error) {
return int64(bytes * PB), nil
case "E", "EB":
return int64(bytes * EB), nil
+ default:
+ return int64(bytes), nil
}
}
// Format wraps global Bytes's Format function.
-func Format(b int64) string {
- return global.Format(b)
+func Format(value int64) string {
+ return global.Format(value)
+}
+
+// FormatBinary wraps global Bytes's FormatBinary function.
+func FormatBinary(value int64) string {
+ return global.FormatBinary(value)
+}
+
+// FormatDecimal wraps global Bytes's FormatDecimal function.
+func FormatDecimal(value int64) string {
+ return global.FormatDecimal(value)
}
// Parse wraps global Bytes's Parse function.
-func Parse(val string) (int64, error) {
- return global.Parse(val)
+func Parse(value string) (int64, error) {
+ return global.Parse(value)
}
diff --git a/vendor/github.com/labstack/gommon/log/log.go b/vendor/github.com/labstack/gommon/log/log.go
index 06fa37e0..25f719aa 100644
--- a/vendor/github.com/labstack/gommon/log/log.go
+++ b/vendor/github.com/labstack/gommon/log/log.go
@@ -391,7 +391,7 @@ func (l *Logger) log(level Lvl, format string, args ...interface{}) {
if err == nil {
s := buf.String()
i := buf.Len() - 1
- if s[i] == '}' {
+ if i >= 0 && s[i] == '}' {
// JSON header
buf.Truncate(i)
buf.WriteByte(',')
@@ -404,7 +404,9 @@ func (l *Logger) log(level Lvl, format string, args ...interface{}) {
}
} else {
// Text header
- buf.WriteByte(' ')
+ if len(s) > 0 {
+ buf.WriteByte(' ')
+ }
buf.WriteString(message)
}
buf.WriteByte('\n')