Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions common/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,21 +130,10 @@ func (a Address) Format(s fmt.State, c rune) {

// SetBytes sets the address to the value of b.
// If b is larger than len(a), b will be cropped from the left.
func (a *Address) SetBytes(b []byte) {
if len(b) > len(a) {
b = b[len(b)-length.Addr:]
}
copy(a[length.Addr-len(b):], b)
}
func (a *Address) SetBytes(b []byte) { fixedSetBytes(a[:], b) }

// MarshalText returns the hex representation of a.
func (a Address) MarshalText() ([]byte, error) {
b := a[:]
result := make([]byte, len(b)*2+2)
copy(result, hexutil.HexPrefix)
hex.Encode(result[2:], b)
return result, nil
}
func (a Address) MarshalText() ([]byte, error) { return hexutil.Bytes(a[:]).MarshalText() }

// UnmarshalText parses a hash in hex syntax.
func (a *Address) UnmarshalText(input []byte) error {
Expand Down
63 changes: 7 additions & 56 deletions common/bytes4.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package common

import (
"bytes"
"database/sql/driver"
"encoding/hex"
"fmt"
"math/rand"
"reflect"
Expand Down Expand Up @@ -48,76 +46,29 @@ func (b *Bytes4) UnmarshalText(input []byte) error {
}

// MarshalText returns the hex representation of a.
func (b Bytes4) MarshalText() ([]byte, error) {
bl := b[:]
result := make([]byte, len(b)*2+2)
copy(result, hexutil.HexPrefix)
hex.Encode(result[2:], bl)
return result, nil
}
func (b Bytes4) MarshalText() ([]byte, error) { return hexutil.Bytes(b[:]).MarshalText() }

// Format implements fmt.Formatter.
// Hash supports the %v, %s, %v, %x, %X and %d format verbs.
func (b Bytes4) Format(s fmt.State, c rune) {
hexb := make([]byte, 2+len(b)*2)
copy(hexb, "0x")
hex.Encode(hexb[2:], b[:])

switch c {
case 'x', 'X':
if !s.Flag('#') {
hexb = hexb[2:]
}
if c == 'X' {
hexb = bytes.ToUpper(hexb)
}
fallthrough
case 'v', 's':
s.Write(hexb)
case 'q':
q := []byte{'"'}
s.Write(q)
s.Write(hexb)
s.Write(q)
case 'd':
fmt.Fprint(s, ([len(b)]byte)(b))
default:
fmt.Fprintf(s, "%%!%c(hash=%x)", c, b)
}
}
func (b Bytes4) Format(s fmt.State, c rune) { fixedFormat(s, c, "hash", b[:]) }

// String implements the stringer interface and is used also by the logger when
// doing full logging into a file.
func (b Bytes4) String() string {
return b.Hex()
}
func (b Bytes4) String() string { return b.Hex() }

// SetBytes sets the hash to the value of i.
// If b is larger than len(h), b will be cropped from the left.
func (b *Bytes4) SetBytes(i []byte) {
if len(i) > len(b) {
i = i[len(i)-length.Hash:]
}

copy(b[length.Hash-len(i):], i)
}
func (b *Bytes4) SetBytes(i []byte) { fixedSetBytes(b[:], i) }

// Generate implements testing/quick.Generator.
func (b Bytes4) Generate(rand *rand.Rand, size int) reflect.Value {
m := rand.Intn(len(b))
for i := len(b) - 1; i > m; i-- {
b[i] = byte(rand.Uint32())
}
fixedGenerate(b[:], rand.Intn, rand.Uint32)
return reflect.ValueOf(b)
}

// Value implements valuer for database/sql.
func (b Bytes4) Value() (driver.Value, error) {
return b[:], nil
}
func (b Bytes4) Value() (driver.Value, error) { return b[:], nil }

// TerminalString implements log.TerminalStringer, formatting a string for console
// output during logging.
func (b Bytes4) TerminalString() string {
return fmt.Sprintf("%x", b)
}
func (b Bytes4) TerminalString() string { return fmt.Sprintf("%x", b) }
63 changes: 7 additions & 56 deletions common/bytes48.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package common

import (
"bytes"
"database/sql/driver"
"encoding/hex"
"fmt"
"math/rand"
"reflect"
Expand Down Expand Up @@ -48,76 +46,29 @@ func (b *Bytes48) UnmarshalText(input []byte) error {
}

// MarshalText returns the hex representation of a.
func (b Bytes48) MarshalText() ([]byte, error) {
bl := b[:]
result := make([]byte, len(b)*2+2)
copy(result, hexutil.HexPrefix)
hex.Encode(result[2:], bl)
return result, nil
}
func (b Bytes48) MarshalText() ([]byte, error) { return hexutil.Bytes(b[:]).MarshalText() }

// Format implements fmt.Formatter.
// Hash supports the %v, %s, %v, %x, %X and %d format verbs.
func (b Bytes48) Format(s fmt.State, c rune) {
hexb := make([]byte, 2+len(b)*2)
copy(hexb, "0x")
hex.Encode(hexb[2:], b[:])

switch c {
case 'x', 'X':
if !s.Flag('#') {
hexb = hexb[2:]
}
if c == 'X' {
hexb = bytes.ToUpper(hexb)
}
fallthrough
case 'v', 's':
s.Write(hexb)
case 'q':
q := []byte{'"'}
s.Write(q)
s.Write(hexb)
s.Write(q)
case 'd':
fmt.Fprint(s, ([len(b)]byte)(b))
default:
fmt.Fprintf(s, "%%!%c(hash=%x)", c, b)
}
}
func (b Bytes48) Format(s fmt.State, c rune) { fixedFormat(s, c, "hash", b[:]) }

// String implements the stringer interface and is used also by the logger when
// doing full logging into a file.
func (b Bytes48) String() string {
return b.Hex()
}
func (b Bytes48) String() string { return b.Hex() }

// SetBytes sets the hash to the value of i.
// If b is larger than len(h), b will be cropped from the left.
func (b *Bytes48) SetBytes(i []byte) {
if len(i) > len(b) {
i = i[len(i)-length.Hash:]
}

copy(b[length.Hash-len(i):], i)
}
func (b *Bytes48) SetBytes(i []byte) { fixedSetBytes(b[:], i) }

// Generate implements testing/quick.Generator.
func (b Bytes48) Generate(rand *rand.Rand, size int) reflect.Value {
m := rand.Intn(len(b))
for i := len(b) - 1; i > m; i-- {
b[i] = byte(rand.Uint32())
}
fixedGenerate(b[:], rand.Intn, rand.Uint32)
return reflect.ValueOf(b)
}

// Value implements valuer for database/sql.
func (b Bytes48) Value() (driver.Value, error) {
return b[:], nil
}
func (b Bytes48) Value() (driver.Value, error) { return b[:], nil }

// TerminalString implements log.TerminalStringer, formatting a string for console
// output during logging.
func (b Bytes48) TerminalString() string {
return fmt.Sprintf("%x…%x", b[:3], b[len(b)-3:])
}
func (b Bytes48) TerminalString() string { return fixedTerminalString(b[:]) }
58 changes: 6 additions & 52 deletions common/bytes64.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package common

import (
"bytes"
"database/sql/driver"
"encoding/hex"
"fmt"
"reflect"

Expand Down Expand Up @@ -47,67 +45,23 @@ func (b *Bytes64) UnmarshalText(input []byte) error {
}

// MarshalText returns the hex representation of a.
func (b Bytes64) MarshalText() ([]byte, error) {
bl := b[:]
result := make([]byte, len(b)*2+2)
copy(result, hexutil.HexPrefix)
hex.Encode(result[2:], bl)
return result, nil
}
func (b Bytes64) MarshalText() ([]byte, error) { return hexutil.Bytes(b[:]).MarshalText() }

// Format implements fmt.Formatter.
// Hash supports the %v, %s, %v, %x, %X and %d format verbs.
func (b Bytes64) Format(s fmt.State, c rune) {
hexb := make([]byte, 2+len(b)*2)
copy(hexb, "0x")
hex.Encode(hexb[2:], b[:])

switch c {
case 'x', 'X':
if !s.Flag('#') {
hexb = hexb[2:]
}
if c == 'X' {
hexb = bytes.ToUpper(hexb)
}
fallthrough
case 'v', 's':
s.Write(hexb)
case 'q':
q := []byte{'"'}
s.Write(q)
s.Write(hexb)
s.Write(q)
case 'd':
fmt.Fprint(s, ([len(b)]byte)(b))
default:
fmt.Fprintf(s, "%%!%c(hash=%x)", c, b)
}
}
func (b Bytes64) Format(s fmt.State, c rune) { fixedFormat(s, c, "hash", b[:]) }

// String implements the stringer interface and is used also by the logger when
// doing full logging into a file.
func (b Bytes64) String() string {
return b.Hex()
}
func (b Bytes64) String() string { return b.Hex() }

// SetBytes sets the hash to the value of i.
// If b is larger than len(h), b will be cropped from the left.
func (b *Bytes64) SetBytes(i []byte) {
if len(i) > len(b) {
i = i[len(i)-length.Hash:]
}

copy(b[length.Hash-len(i):], i)
}
func (b *Bytes64) SetBytes(i []byte) { fixedSetBytes(b[:], i) }

// Value implements valuer for database/sql.
func (b Bytes64) Value() (driver.Value, error) {
return b[:], nil
}
func (b Bytes64) Value() (driver.Value, error) { return b[:], nil }

// TerminalString implements log.TerminalStringer, formatting a string for console
// output during logging.
func (b Bytes64) TerminalString() string {
return fmt.Sprintf("%x…%x", b[:3], b[len(b)-3:])
}
func (b Bytes64) TerminalString() string { return fixedTerminalString(b[:]) }
63 changes: 7 additions & 56 deletions common/bytes96.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
package common

import (
"bytes"
"database/sql/driver"
"encoding/hex"
"fmt"
"math/rand"
"reflect"
Expand Down Expand Up @@ -48,76 +46,29 @@ func (b *Bytes96) UnmarshalText(input []byte) error {
}

// MarshalText returns the hex representation of a.
func (b Bytes96) MarshalText() ([]byte, error) {
bl := b[:]
result := make([]byte, len(b)*2+2)
copy(result, hexutil.HexPrefix)
hex.Encode(result[2:], bl)
return result, nil
}
func (b Bytes96) MarshalText() ([]byte, error) { return hexutil.Bytes(b[:]).MarshalText() }

// Format implements fmt.Formatter.
// Hash supports the %v, %s, %v, %x, %X and %d format verbs.
func (b Bytes96) Format(s fmt.State, c rune) {
hexb := make([]byte, 2+len(b)*2)
copy(hexb, "0x")
hex.Encode(hexb[2:], b[:])

switch c {
case 'x', 'X':
if !s.Flag('#') {
hexb = hexb[2:]
}
if c == 'X' {
hexb = bytes.ToUpper(hexb)
}
fallthrough
case 'v', 's':
s.Write(hexb)
case 'q':
q := []byte{'"'}
s.Write(q)
s.Write(hexb)
s.Write(q)
case 'd':
fmt.Fprint(s, ([len(b)]byte)(b))
default:
fmt.Fprintf(s, "%%!%c(hash=%x)", c, b)
}
}
func (b Bytes96) Format(s fmt.State, c rune) { fixedFormat(s, c, "hash", b[:]) }

// String implements the stringer interface and is used also by the logger when
// doing full logging into a file.
func (b Bytes96) String() string {
return b.Hex()
}
func (b Bytes96) String() string { return b.Hex() }

// SetBytes sets the hash to the value of i.
// If b is larger than len(h), b will be cropped from the left.
func (b *Bytes96) SetBytes(i []byte) {
if len(i) > len(b) {
i = i[len(i)-length.Hash:]
}

copy(b[length.Hash-len(i):], i)
}
func (b *Bytes96) SetBytes(i []byte) { fixedSetBytes(b[:], i) }

// Generate implements testing/quick.Generator.
func (b Bytes96) Generate(rand *rand.Rand, size int) reflect.Value {
m := rand.Intn(len(b))
for i := len(b) - 1; i > m; i-- {
b[i] = byte(rand.Uint32())
}
fixedGenerate(b[:], rand.Intn, rand.Uint32)
return reflect.ValueOf(b)
}

// Value implements valuer for database/sql.
func (b Bytes96) Value() (driver.Value, error) {
return b[:], nil
}
func (b Bytes96) Value() (driver.Value, error) { return b[:], nil }

// TerminalString implements log.TerminalStringer, formatting a string for console
// output during logging.
func (b Bytes96) TerminalString() string {
return fmt.Sprintf("%x…%x", b[:3], b[len(b)-3:])
}
func (b Bytes96) TerminalString() string { return fixedTerminalString(b[:]) }
Loading
Loading