Source File
hex.go
Belonging Package
encoding/hex
package hex
import (
)
const hextable = "0123456789abcdef"
type InvalidByteError byte
func ( InvalidByteError) () string {
return fmt.Sprintf("encoding/hex: invalid byte: %#U", rune())
}
func (, []byte) (int, error) {
, := 0, 1
for ; < len(); += 2 {
, := fromHexChar([-1])
if ! {
return , InvalidByteError([-1])
}
, := fromHexChar([])
if ! {
return , InvalidByteError([])
}
[] = ( << 4) |
++
}
if , := fromHexChar([-1]); ! {
return , InvalidByteError([-1])
}
return , ErrLength
}
return , nil
}
, := Decode(, )
return [:],
}
const bufferSize = 1024
type encoder struct {
w io.Writer
err error
out [bufferSize]byte // output buffer
}
func ( io.Writer) io.Writer {
return &encoder{w: }
}
func ( *encoder) ( []byte) ( int, error) {
for len() > 0 && .err == nil {
:= bufferSize / 2
if len() < {
= len()
}
var int
:= Encode(.out[:], [:])
, .err = .w.Write(.out[:])
+= / 2
= [:]
}
return , .err
}
type decoder struct {
r io.Reader
err error
in []byte // input buffer (encoded form)
arr [bufferSize]byte // backing array for in
}
func ( io.Writer) io.WriteCloser {
return &dumper{w: }
}
type dumper struct {
w io.Writer
rightChars [18]byte
buf [14]byte
used int // number of bytes in the current line
n uint // number of bytes, total
closed bool
}
func ( byte) byte {
if < 32 || > 126 {
return '.'
}
return
}
func ( *dumper) ( []byte) ( int, error) {
if .closed {
return 0, errors.New("encoding/hex: dumper closed")
}
for := range {
.buf[3] = ' '
= 4
.buf[3] = ' '
.buf[4] = '|'
= 5
}
_, = .w.Write(.buf[:])
if != nil {
return
}
++
.rightChars[.used] = toChar([])
.used++
.n++
if .used == 16 {
.rightChars[16] = '|'
.rightChars[17] = '\n'
_, = .w.Write(.rightChars[:])
if != nil {
return
}
.used = 0
}
}
return
}
if .closed {
return
}
.closed = true
if .used == 0 {
return
}
.buf[0] = ' '
.buf[1] = ' '
.buf[2] = ' '
.buf[3] = ' '
.buf[4] = '|'
:= .used
for .used < 16 {
:= 3
if .used == 7 {
= 4
} else if .used == 15 {
= 5
}
_, = .w.Write(.buf[:])
if != nil {
return
}
.used++
}
.rightChars[] = '|'
.rightChars[+1] = '\n'
_, = .w.Write(.rightChars[:+2])
return
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |