Browse Source

IntInSlice and StringInSlice functions

Refs https://github.com/tendermint/tendermint/pull/835
pull/1842/head
Anton Kaliaev 7 years ago
parent
commit
33abe87c5b
No known key found for this signature in database GPG Key ID: 7B6881D965918214
4 changed files with 48 additions and 0 deletions
  1. +10
    -0
      common/int.go
  2. +14
    -0
      common/int_test.go
  3. +10
    -0
      common/string.go
  4. +14
    -0
      common/string_test.go

+ 10
- 0
common/int.go View File

@ -53,3 +53,13 @@ func PutInt64BE(dest []byte, i int64) {
func GetInt64BE(src []byte) int64 { func GetInt64BE(src []byte) int64 {
return int64(binary.BigEndian.Uint64(src)) return int64(binary.BigEndian.Uint64(src))
} }
// IntInSlice returns true if a is found in the list.
func IntInSlice(a int, list []int) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}

+ 14
- 0
common/int_test.go View File

@ -0,0 +1,14 @@
package common
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestIntInSlice(t *testing.T) {
assert.True(t, IntInSlice(1, []int{1, 2, 3}))
assert.False(t, IntInSlice(4, []int{1, 2, 3}))
assert.True(t, IntInSlice(0, []int{0}))
assert.False(t, IntInSlice(0, []int{}))
}

+ 10
- 0
common/string.go View File

@ -43,3 +43,13 @@ func StripHex(s string) string {
} }
return s return s
} }
// StringInSlice returns true if a is found the list.
func StringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}

+ 14
- 0
common/string_test.go View File

@ -0,0 +1,14 @@
package common
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestStringInSlice(t *testing.T) {
assert.True(t, StringInSlice("a", []string{"a", "b", "c"}))
assert.False(t, StringInSlice("d", []string{"a", "b", "c"}))
assert.True(t, StringInSlice("", []string{""}))
assert.False(t, StringInSlice("", []string{}))
}

Loading…
Cancel
Save