Browse Source

Added some common methods

pull/32/head
Jae Kwon 9 years ago
parent
commit
722474a9b3
2 changed files with 62 additions and 2 deletions
  1. +20
    -2
      common/byteslice.go
  2. +42
    -0
      common/math.go

+ 20
- 2
common/byteslice.go View File

@ -1,7 +1,25 @@
package common
func Fingerprint(bytez []byte) []byte {
func Fingerprint(slice []byte) []byte {
fingerprint := make([]byte, 6)
copy(fingerprint, bytez)
copy(fingerprint, slice)
return fingerprint
}
func RightPadBytes(slice []byte, l int) []byte {
if l < len(slice) {
return slice
}
padded := make([]byte, l)
copy(padded[0:len(slice)], slice)
return padded
}
func LeftPadBytes(slice []byte, l int) []byte {
if l < len(slice) {
return slice
}
padded := make([]byte, l)
copy(padded[l-len(slice):], slice)
return padded
}

+ 42
- 0
common/math.go View File

@ -42,6 +42,20 @@ func MaxUint32(a, b uint32) uint32 {
return b
}
func MaxInt64(a, b int64) int64 {
if a > b {
return a
}
return b
}
func MaxUint64(a, b uint64) uint64 {
if a > b {
return a
}
return b
}
func MaxInt(a, b int) int {
if a > b {
return a
@ -100,6 +114,20 @@ func MinUint32(a, b uint32) uint32 {
return b
}
func MinInt64(a, b int64) int64 {
if a < b {
return a
}
return b
}
func MinUint64(a, b uint64) uint64 {
if a < b {
return a
}
return b
}
func MinInt(a, b int) int {
if a < b {
return a
@ -113,3 +141,17 @@ func MinUint(a, b uint) uint {
}
return b
}
//-----------------------------------------------------------------------------
func ExpUint64(a, b uint64) uint64 {
accum := uint64(1)
for b > 0 {
if b&1 == 1 {
accum *= a
}
a *= a
b >>= 1
}
return accum
}

Loading…
Cancel
Save