forked from p93542168/wheat-cache
30 lines
660 B
Go
30 lines
660 B
Go
|
package util
|
||
|
|
||
|
import (
|
||
|
"regexp"
|
||
|
"strconv"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
// ParseSizeToBit
|
||
|
// 支持MB, GB, KB, 格式 "5KB" 或者 "5kb"
|
||
|
func ParseSizeToBit(size string) int64 {
|
||
|
|
||
|
sizes := regexp.MustCompile("^\\d+")
|
||
|
sizeRes := sizes.FindAllString(size, 1)
|
||
|
unit := strings.Split(size, sizeRes[0])
|
||
|
if unit[1] == "KB"|| unit[1] == "kb"{
|
||
|
Res, _ := strconv.ParseInt(sizeRes[0], 10, 64)
|
||
|
return Res * 1024
|
||
|
} else if unit[1] == "MB"|| unit[1] == "mb"{
|
||
|
Res, _ := strconv.ParseInt(sizeRes[0], 10, 64)
|
||
|
return Res * 1024 * 1024
|
||
|
} else if unit[1] == "GB"|| unit[1] == "fb"{
|
||
|
Res, _ := strconv.ParseInt(sizeRes[0], 10, 64)
|
||
|
return Res * 1024 *1024 * 1024
|
||
|
}
|
||
|
return 0
|
||
|
}
|
||
|
|
||
|
|