github.com/coveo/gotemplate@v2.7.7+incompatible/template/math_bits.go (about) 1 package template 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 ) 8 9 func leftShift(a, b interface{}) (r interface{}, err error) { 10 defer func() { err = trapError(err, recover()) }() 11 return process(a, func(a interface{}) interface{} { return toInt64(a) << toUnsignedInteger(b) }) 12 } 13 14 func rightShift(a, b interface{}) (r interface{}, err error) { 15 defer func() { err = trapError(err, recover()) }() 16 return process(a, func(a interface{}) interface{} { return toInt64(a) >> toUnsignedInteger(b) }) 17 } 18 19 func bitwiseOr(a, b interface{}, rest ...interface{}) (r interface{}, err error) { 20 defer func() { err = trapError(err, recover()) }() 21 result := toInt64(a) | toInt64(b) 22 for i := range rest { 23 result = result | toInt64(rest[i]) 24 } 25 return result, nil 26 } 27 28 func bitwiseAnd(a, b interface{}, rest ...interface{}) (r interface{}, err error) { 29 defer func() { err = trapError(err, recover()) }() 30 result := toInt64(a) & toInt64(b) 31 for i := range rest { 32 result = result & toInt64(rest[i]) 33 } 34 return result, nil 35 } 36 37 func bitwiseXor(a, b interface{}, rest ...interface{}) (r interface{}, err error) { 38 defer func() { err = trapError(err, recover()) }() 39 result := toInt64(a) ^ toInt64(b) 40 for i := range rest { 41 result = result ^ toInt64(rest[i]) 42 } 43 return result, nil 44 } 45 46 func bitwiseClear(a, b interface{}, rest ...interface{}) (r interface{}, err error) { 47 defer func() { err = trapError(err, recover()) }() 48 result := toInt64(a) &^ toInt64(b) 49 for i := range rest { 50 result = result &^ toInt64(rest[i]) 51 } 52 return result, nil 53 } 54 55 func hex(a interface{}) (r interface{}, err error) { 56 defer func() { err = trapError(err, recover()) }() 57 return process(a, func(a interface{}) interface{} { 58 return fmt.Sprintf("0x%X", toInt(a)) 59 }) 60 } 61 62 func decimal(a interface{}) (r interface{}, err error) { 63 defer func() { err = trapError(err, recover()) }() 64 return process(a, func(a interface{}) interface{} { 65 return must(strconv.ParseInt(strings.TrimPrefix(fmt.Sprint(a), "0x"), 16, 64)) 66 }) 67 }