github.com/markusbkk/elvish@v0.0.0-20231204143114-91dc52438621/pkg/mods/unix/rlim_t_uint64.go (about) 1 //go:build !freebsd && !windows && !plan9 && !js 2 // +build !freebsd,!windows,!plan9,!js 3 4 package unix 5 6 import ( 7 "math" 8 "math/big" 9 "strconv" 10 ) 11 12 type rlimT = uint64 13 14 var rlimTValid = "number between 0 and " + strconv.FormatUint(math.MaxUint64, 10) 15 16 const maxInt = uint64(^uint(0) >> 1) 17 18 func convertRlimT(x uint64) interface{} { 19 if x <= maxInt { 20 return int(x) 21 } 22 if x <= math.MaxInt64 { 23 return big.NewInt(int64(x)) 24 } 25 z := big.NewInt(int64(x / 2)) 26 z.Lsh(z, 1) 27 if x%2 == 1 { 28 z.Bits()[0] |= 1 29 } 30 return z 31 32 } 33 34 func parseRlimT(val interface{}) (uint64, bool) { 35 switch val := val.(type) { 36 case int: 37 if val >= 0 { 38 return uint64(val), true 39 } 40 case *big.Int: 41 if val.IsUint64() { 42 return val.Uint64(), true 43 } 44 case string: 45 num, err := strconv.ParseUint(val, 0, 64) 46 if err == nil { 47 return num, true 48 } 49 } 50 return 0, false 51 }