代码拉取完成,页面将自动刷新
package mfmt
import (
"fmt"
"regexp"
"strings"
"gitee.com/wecisecode/util/cast"
)
const (
_ = iota
KB int64 = 1 << (iota * 10)
MB
GB
TB
PB
EB
)
var reBytes = regexp.MustCompile(`^(\d+)\s*(.*)$`)
// 支持的单位:
// "K", "KB",
// "M", "MB",
// "G", "GB",
// "T", "TB",
// "P", "PB",
// "E", "EB",
// "B", "BYTE", "BYTES"
// 默认 BYTE
func ParseBytesCount(s string) int64 {
s = strings.TrimSpace(s)
if s == "" {
return 0
}
sign := int64(1)
if s[0] == '-' {
sign = -1
s = s[1:]
}
ss := reBytes.FindStringSubmatch(s)
if len(ss) < 3 {
return 0
}
unit := int64(0)
switch strings.ToUpper(ss[2]) {
case "K", "KB":
unit = KB
case "M", "MB":
unit = MB
case "G", "GB":
unit = GB
case "T", "TB":
unit = TB
case "P", "PB":
unit = PB
case "E", "EB":
unit = EB
case "", "B", "BYTE", "BYTES":
unit = 1
}
return sign * cast.ToInt64(ss[1]) * unit
}
func BytesSize(b int64) string {
if b <= -10000 {
b = (b - 512) / 1024
if b > -10000 {
return fmt.Sprintf("%dK", b)
}
b = (b - 512) / 1024
if b > -10000 {
return fmt.Sprintf("%dM", b)
}
b = (b - 512) / 1024
if b > -10000 {
return fmt.Sprintf("%dG", b)
}
b = (b - 512) / 1024
if b > -10000 {
return fmt.Sprintf("%dT", b)
}
b = (b - 512) / 1024
return fmt.Sprintf("%dP", b)
}
if b >= 10000 {
b = (b + 512) / 1024
if b < 10000 {
return fmt.Sprintf("%dK", b)
}
b = (b + 512) / 1024
if b < 10000 {
return fmt.Sprintf("%dM", b)
}
b = (b + 512) / 1024
if b < 10000 {
return fmt.Sprintf("%dG", b)
}
b = (b + 512) / 1024
if b < 10000 {
return fmt.Sprintf("%dT", b)
}
b = (b + 512) / 1024
return fmt.Sprintf("%dP", b)
}
return fmt.Sprintf("%d", b)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。