2021-09-27 11:29:47 +08:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
2021-10-04 16:21:48 +08:00
|
|
|
"gitee.com/timedb/wheatCache/pkg/errorx"
|
2021-09-27 11:29:47 +08:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ParseSizeToBit
|
2021-10-03 16:22:33 +08:00
|
|
|
// 支持MB, GB, KB, 格式 "5KB" 或者 "5kb"等等
|
2021-10-04 16:21:48 +08:00
|
|
|
func ParseSizeToBit(size string) interface{} {
|
2021-09-27 11:29:47 +08:00
|
|
|
|
|
|
|
sizes := regexp.MustCompile("^\\d+")
|
|
|
|
sizeRes := sizes.FindAllString(size, 1)
|
|
|
|
unit := strings.Split(size, sizeRes[0])
|
2021-10-03 16:22:33 +08:00
|
|
|
Res, _ := strconv.ParseInt(sizeRes[0], 10, 64)
|
|
|
|
if strings.ToUpper(unit[1]) == "BIT" || strings.ToUpper(unit[1]) == "B"{
|
|
|
|
return Res * 8
|
|
|
|
}else if strings.ToUpper(unit[1]) == "KB"{
|
|
|
|
return Res * 1024 * 8
|
|
|
|
} else if strings.ToUpper(unit[1])=="MB"{
|
|
|
|
return Res * 1024 * 1024 * 8
|
|
|
|
} else if strings.ToUpper(unit[1]) == "GB"{
|
|
|
|
return Res * 1024 *1024 * 1024 * 8
|
2021-09-27 11:29:47 +08:00
|
|
|
}
|
2021-10-04 16:21:48 +08:00
|
|
|
return errorx.New("your size is wrong")
|
2021-09-27 11:29:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|