common: improve comments

This commit is contained in:
Felix Lange 2018-05-08 15:17:03 +02:00
parent a570060a1b
commit d9c8cbe51f

View file

@ -19,17 +19,18 @@ package common
import "encoding/hex" import "encoding/hex"
// ToHex returns string representation of b and either prepends '0x' or initializes value of '0'. // ToHex returns the hex representation of b, prefixed with '0x'.
// For empty slices, the return value is "0x0".
func ToHex(b []byte) string { func ToHex(b []byte) string {
hex := Bytes2Hex(b) hex := Bytes2Hex(b)
// Prefer output of "0x0" instead of "0x"
if len(hex) == 0 { if len(hex) == 0 {
hex = "0" hex = "0"
} }
return "0x" + hex return "0x" + hex
} }
//FromHex returns bytes of s after removing prefix, or prepends 0 if s has odd length. // FromHex returns the bytes represented by the hexadecimal string s.
// s may be prefixed with "0x".
func FromHex(s string) []byte { func FromHex(s string) []byte {
if len(s) > 1 { if len(s) > 1 {
if s[0:2] == "0x" || s[0:2] == "0X" { if s[0:2] == "0x" || s[0:2] == "0X" {
@ -42,7 +43,7 @@ func FromHex(s string) []byte {
return Hex2Bytes(s) return Hex2Bytes(s)
} }
// CopyBytes returns an exact copy of the provided bytes // CopyBytes returns an exact copy of the provided bytes.
func CopyBytes(b []byte) (copiedBytes []byte) { func CopyBytes(b []byte) (copiedBytes []byte) {
if b == nil { if b == nil {
return nil return nil
@ -84,7 +85,6 @@ func Bytes2Hex(d []byte) string {
// Hex2Bytes returns the bytes represented by the hexadecimal string str. // Hex2Bytes returns the bytes represented by the hexadecimal string str.
func Hex2Bytes(str string) []byte { func Hex2Bytes(str string) []byte {
h, _ := hex.DecodeString(str) h, _ := hex.DecodeString(str)
return h return h
} }
@ -102,7 +102,7 @@ func Hex2BytesFixed(str string, flen int) []byte {
return hh return hh
} }
// RightPadBytes copies slice into a byte slice of length l. // RightPadBytes zero-pads slice to the right up to length l.
func RightPadBytes(slice []byte, l int) []byte { func RightPadBytes(slice []byte, l int) []byte {
if l <= len(slice) { if l <= len(slice) {
return slice return slice
@ -114,7 +114,7 @@ func RightPadBytes(slice []byte, l int) []byte {
return padded return padded
} }
// LeftPadBytes copies via inverted index slice into a byte slice of length l. // LeftPadBytes zero-pads slice to the left up to length l.
func LeftPadBytes(slice []byte, l int) []byte { func LeftPadBytes(slice []byte, l int) []byte {
if l <= len(slice) { if l <= len(slice) {
return slice return slice