add Printable example to docs

This commit is contained in:
Pieter Joost van de Sande 2019-01-10 08:57:11 +01:00
parent b96c21210a
commit ead5bb5bc6
2 changed files with 14 additions and 2 deletions

View File

@ -380,8 +380,15 @@ func (k Key) FDBKey() Key {
// String describes the key as a hexadecimal encoded string.
func (k Key) String() string {
return Printable(k)
}
// Printable returns a human readable version of a byte array. The bytes that correspond with
// ASCII printable characters [32-127) are passed through. Other bytes are
// replaced with \x followed by a two character zero-padded hex code for byte.
func Printable(d []byte) string {
var sb strings.Builder
for _, b := range k {
for _, b := range d {
if b >= 32 && b < 127 && b != '\\' {
sb.WriteByte(b)
continue

View File

@ -268,8 +268,8 @@ func TestKeyToString(t *testing.T) {
key fdb.Key
expect string
}{
{fdb.Key("plain-text"), "plain-text"},
{fdb.Key([]byte{0}), "\\x00"},
{fdb.Key("plain-text"), "plain-text"},
{fdb.Key("\xbdascii☻☺"), "\\xbdascii\\xe2\\x98\\xbb\\xe2\\x98\\xba"},
}
@ -279,3 +279,8 @@ func TestKeyToString(t *testing.T) {
}
}
}
func ExamplePrintable() {
fmt.Println(fdb.Printable([]byte{0, 1, 2, 'a', 'b', 'c', '1', '2', '3', '!', '?', 255}))
// Output: \x00\x01\x02abc123!?\xff
}