1 Star 0 Fork 0

庸人妄语 / msgo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
byte.go 1.32 KB
一键复制 编辑 原始数据 按行查看 历史
庸人妄语 提交于 2023-09-28 14:51 . 修改strs
package strs
import (
"math"
"strconv"
"strings"
)
func ParseBytes(s string) uint64 {
s = strings.ReplaceAll(s, " ", "")
n, e := strconv.ParseUint(s, 10, 0)
if e == nil {
return n
}
nums := strings.Split(s, "*")
if len(nums) > 1 {
size := uint64(1)
for _, num := range nums {
n, err := strconv.ParseUint(num, 10, 0)
if err != nil {
return 0
}
size *= n
}
return size
}
ss := strings.ToUpper(s)
bs := []byte(ss)
size := uint64(0)
i := 0
for ; i < len(bs); i++ {
if bs[i] >= '0' && bs[i] <= '9' {
size = size*10 + uint64(bs[i]-'0')
} else {
break
}
}
const KB = 1024
const MB = KB * 1024
const GB = MB * 1024
const TB = GB * 1024
hasB := false
if i < len(bs) {
switch bs[i] {
case 'B':
hasB = true
case 'K':
if size < math.MaxUint64/KB {
size *= KB
}
case 'M':
if size < math.MaxUint64/MB {
size *= MB
}
case 'G':
if size < math.MaxUint64/GB {
size *= GB
}
case 'T':
if size < math.MaxUint64/TB {
size *= TB
}
default:
return 0
}
} else {
return size
}
i++
if i < len(bs) {
//byte
if !hasB {
if bs[i] != 'B' {
return 0
}
i++
if i == len(bs) {
return size
}
}
if i+3 == len(bs) && bs[i] == 'Y' && bs[i+1] == 'T' && bs[i+2] == 'E' {
return size
}
} else {
return size
}
return 0
}
Go
1
https://gitee.com/yrwy/msgo.git
git@gitee.com:yrwy/msgo.git
yrwy
msgo
msgo
v0.2.5

搜索帮助

53164aa7 5694891 3bd8fe86 5694891