mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 14:52:17 +00:00
common: Delete unused functions (#2452)
These functions were not used anywhere within tendermint, or the cosmos-sdk. (The functionality is already duplicated in the cosmos-sdk types package) * common: Delete unused functions within byteslice * remove more unused code from strings.go and int.go * Remove more unused code from int.go * Fix testcase
This commit is contained in:
parent
f76312ffe6
commit
0d6b75bd53
@ -12,6 +12,11 @@ BREAKING CHANGES:
|
|||||||
* Go API
|
* Go API
|
||||||
* \#2310 Mempool.ReapMaxBytes -> Mempool.ReapMaxBytesMaxGas
|
* \#2310 Mempool.ReapMaxBytes -> Mempool.ReapMaxBytesMaxGas
|
||||||
* \#2431 Remove Word256 code in libs/common, due to lack of use
|
* \#2431 Remove Word256 code in libs/common, due to lack of use
|
||||||
|
* \#2452 Remove the following methods from libs/common due to lack of use:
|
||||||
|
* byteslice.go: cmn.IsZeros, cmn.RightPadBytes, cmn.LeftPadBytes, cmn.PrefixEndBytes
|
||||||
|
* strings.go: cmn.IsHex, cmn.StripHex
|
||||||
|
* int.go: Uint64Slice, all put/get int64 methods
|
||||||
|
|
||||||
* Blockchain Protocol
|
* Blockchain Protocol
|
||||||
|
|
||||||
* P2P Protocol
|
* P2P Protocol
|
||||||
|
@ -1,9 +1,5 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Fingerprint returns the first 6 bytes of a byte slice.
|
// Fingerprint returns the first 6 bytes of a byte slice.
|
||||||
// If the slice is less than 6 bytes, the fingerprint
|
// If the slice is less than 6 bytes, the fingerprint
|
||||||
// contains trailing zeroes.
|
// contains trailing zeroes.
|
||||||
@ -12,62 +8,3 @@ func Fingerprint(slice []byte) []byte {
|
|||||||
copy(fingerprint, slice)
|
copy(fingerprint, slice)
|
||||||
return fingerprint
|
return fingerprint
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsZeros(slice []byte) bool {
|
|
||||||
for _, byt := range slice {
|
|
||||||
if byt != byte(0) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
func TrimmedString(b []byte) string {
|
|
||||||
trimSet := string([]byte{0})
|
|
||||||
return string(bytes.TrimLeft(b, trimSet))
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// PrefixEndBytes returns the end byteslice for a noninclusive range
|
|
||||||
// that would include all byte slices for which the input is the prefix
|
|
||||||
func PrefixEndBytes(prefix []byte) []byte {
|
|
||||||
if prefix == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
end := make([]byte, len(prefix))
|
|
||||||
copy(end, prefix)
|
|
||||||
finished := false
|
|
||||||
|
|
||||||
for !finished {
|
|
||||||
if end[len(end)-1] != byte(255) {
|
|
||||||
end[len(end)-1]++
|
|
||||||
finished = true
|
|
||||||
} else {
|
|
||||||
end = end[:len(end)-1]
|
|
||||||
if len(end) == 0 {
|
|
||||||
end = nil
|
|
||||||
finished = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return end
|
|
||||||
}
|
|
||||||
|
@ -1,28 +0,0 @@
|
|||||||
package common
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestPrefixEndBytes(t *testing.T) {
|
|
||||||
assert := assert.New(t)
|
|
||||||
|
|
||||||
var testCases = []struct {
|
|
||||||
prefix []byte
|
|
||||||
expected []byte
|
|
||||||
}{
|
|
||||||
{[]byte{byte(55), byte(255), byte(255), byte(0)}, []byte{byte(55), byte(255), byte(255), byte(1)}},
|
|
||||||
{[]byte{byte(55), byte(255), byte(255), byte(15)}, []byte{byte(55), byte(255), byte(255), byte(16)}},
|
|
||||||
{[]byte{byte(55), byte(200), byte(255)}, []byte{byte(55), byte(201)}},
|
|
||||||
{[]byte{byte(55), byte(255), byte(255)}, []byte{byte(56)}},
|
|
||||||
{[]byte{byte(255), byte(255), byte(255)}, nil},
|
|
||||||
{nil, nil},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, test := range testCases {
|
|
||||||
end := PrefixEndBytes(test.prefix)
|
|
||||||
assert.Equal(test.expected, end)
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,59 +1,5 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/binary"
|
|
||||||
"sort"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Sort for []uint64
|
|
||||||
|
|
||||||
type Uint64Slice []uint64
|
|
||||||
|
|
||||||
func (p Uint64Slice) Len() int { return len(p) }
|
|
||||||
func (p Uint64Slice) Less(i, j int) bool { return p[i] < p[j] }
|
|
||||||
func (p Uint64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
||||||
func (p Uint64Slice) Sort() { sort.Sort(p) }
|
|
||||||
|
|
||||||
func SearchUint64s(a []uint64, x uint64) int {
|
|
||||||
return sort.Search(len(a), func(i int) bool { return a[i] >= x })
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p Uint64Slice) Search(x uint64) int { return SearchUint64s(p, x) }
|
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
func PutUint64LE(dest []byte, i uint64) {
|
|
||||||
binary.LittleEndian.PutUint64(dest, i)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetUint64LE(src []byte) uint64 {
|
|
||||||
return binary.LittleEndian.Uint64(src)
|
|
||||||
}
|
|
||||||
|
|
||||||
func PutUint64BE(dest []byte, i uint64) {
|
|
||||||
binary.BigEndian.PutUint64(dest, i)
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetUint64BE(src []byte) uint64 {
|
|
||||||
return binary.BigEndian.Uint64(src)
|
|
||||||
}
|
|
||||||
|
|
||||||
func PutInt64LE(dest []byte, i int64) {
|
|
||||||
binary.LittleEndian.PutUint64(dest, uint64(i))
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetInt64LE(src []byte) int64 {
|
|
||||||
return int64(binary.LittleEndian.Uint64(src))
|
|
||||||
}
|
|
||||||
|
|
||||||
func PutInt64BE(dest []byte, i int64) {
|
|
||||||
binary.BigEndian.PutUint64(dest, uint64(i))
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetInt64BE(src []byte) int64 {
|
|
||||||
return int64(binary.BigEndian.Uint64(src))
|
|
||||||
}
|
|
||||||
|
|
||||||
// IntInSlice returns true if a is found in the list.
|
// IntInSlice returns true if a is found in the list.
|
||||||
func IntInSlice(a int, list []int) bool {
|
func IntInSlice(a int, list []int) bool {
|
||||||
for _, b := range list {
|
for _, b := range list {
|
||||||
|
@ -1,28 +1,10 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/hex"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// IsHex returns true for non-empty hex-string prefixed with "0x"
|
|
||||||
func IsHex(s string) bool {
|
|
||||||
if len(s) > 2 && strings.EqualFold(s[:2], "0x") {
|
|
||||||
_, err := hex.DecodeString(s[2:])
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// StripHex returns hex string without leading "0x"
|
|
||||||
func StripHex(s string) string {
|
|
||||||
if IsHex(s) {
|
|
||||||
return s[2:]
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
// StringInSlice returns true if a is found the list.
|
// StringInSlice returns true if a is found the list.
|
||||||
func StringInSlice(a string, list []string) bool {
|
func StringInSlice(a string, list []string) bool {
|
||||||
for _, b := range list {
|
for _, b := range list {
|
||||||
|
@ -13,30 +13,12 @@ func TestStringInSlice(t *testing.T) {
|
|||||||
assert.False(t, StringInSlice("", []string{}))
|
assert.False(t, StringInSlice("", []string{}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsHex(t *testing.T) {
|
|
||||||
notHex := []string{
|
|
||||||
"", " ", "a", "x", "0", "0x", "0X", "0x ", "0X ", "0X a",
|
|
||||||
"0xf ", "0x f", "0xp", "0x-",
|
|
||||||
"0xf", "0XBED", "0xF", "0xbed", // Odd lengths
|
|
||||||
}
|
|
||||||
for _, v := range notHex {
|
|
||||||
assert.False(t, IsHex(v), "%q is not hex", v)
|
|
||||||
}
|
|
||||||
hex := []string{
|
|
||||||
"0x00", "0x0a", "0x0F", "0xFFFFFF", "0Xdeadbeef", "0x0BED",
|
|
||||||
"0X12", "0X0A",
|
|
||||||
}
|
|
||||||
for _, v := range hex {
|
|
||||||
assert.True(t, IsHex(v), "%q is hex", v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestIsASCIIText(t *testing.T) {
|
func TestIsASCIIText(t *testing.T) {
|
||||||
notASCIIText := []string{
|
notASCIIText := []string{
|
||||||
"", "\xC2", "\xC2\xA2", "\xFF", "\x80", "\xF0", "\n", "\t",
|
"", "\xC2", "\xC2\xA2", "\xFF", "\x80", "\xF0", "\n", "\t",
|
||||||
}
|
}
|
||||||
for _, v := range notASCIIText {
|
for _, v := range notASCIIText {
|
||||||
assert.False(t, IsHex(v), "%q is not ascii-text", v)
|
assert.False(t, IsASCIIText(v), "%q is not ascii-text", v)
|
||||||
}
|
}
|
||||||
asciiText := []string{
|
asciiText := []string{
|
||||||
" ", ".", "x", "$", "_", "abcdefg;", "-", "0x00", "0", "123",
|
" ", ".", "x", "$", "_", "abcdefg;", "-", "0x00", "0", "123",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user