github.com/inazumav/sing-box@v0.0.0-20230926072359-ab51429a14f1/transport/hysteria/speed.go (about) 1 package hysteria 2 3 import ( 4 "regexp" 5 "strconv" 6 ) 7 8 func StringToBps(s string) uint64 { 9 if s == "" { 10 return 0 11 } 12 m := regexp.MustCompile(`^(\d+)\s*([KMGT]?)([Bb])ps$`).FindStringSubmatch(s) 13 if m == nil { 14 return 0 15 } 16 var n uint64 17 switch m[2] { 18 case "K": 19 n = 1 << 10 20 case "M": 21 n = 1 << 20 22 case "G": 23 n = 1 << 30 24 case "T": 25 n = 1 << 40 26 default: 27 n = 1 28 } 29 v, _ := strconv.ParseUint(m[1], 10, 64) 30 n = v * n 31 if m[3] == "b" { 32 // Bits, need to convert to bytes 33 n = n >> 3 34 } 35 return n 36 }