本文整理匯總了Golang中io.ByteReader類的典型用法代碼示例。如果您正苦於以下問題:Golang ByteReader類的具體用法?Golang ByteReader怎麽用?Golang ByteReader使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了ByteReader類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: drain
func drain(r io.ByteReader, results chan int) {
var i int
for _, e := r.ReadByte(); e == nil; _, e = r.ReadByte() {
i++
}
results <- i
}
示例2: unrolledUnPack4
func unrolledUnPack4(buffer []int64, offset, len int, r io.ByteReader) error {
numHops := 2
remainder := len % numHops
endOffset := offset + len
endUnroll := endOffset - remainder
var val uint64
for i := offset; i < endUnroll; i = i + numHops {
byt, err := r.ReadByte()
if err != nil {
return err
}
val = uint64(byt)
buffer[i] = int64((val >> 4) & 15)
buffer[i+1] = int64(val & 15)
}
if remainder > 0 {
startShift := 4
byt, err := r.ReadByte()
if err != nil {
return err
}
val = uint64(byt)
for i := endUnroll; i < endOffset; i++ {
buffer[i] = int64((val >> uint64(startShift)) & 15)
startShift -= 4
}
}
return nil
}
示例3: eatToken
// eats a single token returning:
// a bool, if it is an xml processing instruction
// a tag and optional default namespace, if an element
// and an error
func eatToken(in io.ByteReader) (bool, string, string, error) {
c, err := in.ReadByte()
if err != nil {
return false, "", "", ErrInvalid
}
switch c {
default:
valid, tag, ns := eatElement(in, c)
if valid {
return false, tag, ns, nil
}
case '!':
// can be a comment or a DOCTYPE
if eatComment(in) {
return false, "", "", nil
}
case '?':
valid, decl := eatPI(in)
if valid {
return decl, "", "", nil
}
case '<', '>', ' ', '\r', '\n', '\t':
}
return false, "", "", ErrInvalid
}
示例4: Eval
func (i InstReadFromInput) Eval(t Tape, in io.ByteReader, out io.ByteWriter) {
b, _ := in.ReadByte()
if b == byte(0) {
return
}
t.SetByte(b)
}
示例5: ScanUntil
// Assumes that even partial matches may not overlap. Stops at match or newline.
func ScanUntil(r io.ByteReader, target string, ignorespace bool) (scanned string, pos int, err error) {
var i, j, k int
var b byte
buf := make([]byte, 0, len(target))
pos = -1
for {
b, err = r.ReadByte()
if err != nil {
return
} else if b == '\n' {
return
}
switch target[j] {
case b:
buf = append(buf, b)
j++
default:
if j > 0 && (b == ' ' || b == '\t') {
buf = append(buf, b)
k++
} else {
j = 0
}
}
if j >= len(target) {
scanned = string(buf)
pos = i - j - k + 1
return
}
i++
}
return
}
示例6: eatDOCTYPE
func eatDOCTYPE(in io.ByteReader) bool {
expect := make([]byte, 6)
for i := range expect {
c, err := in.ReadByte()
if err != nil {
return false
}
expect[i] = c
}
if string(expect) != "OCTYPE" {
return false
}
var depth int
for {
c, err := in.ReadByte()
if err != nil {
return false
}
switch c {
case '>':
if depth == 0 {
return true
}
depth--
case '<':
depth++
}
}
}
示例7: eatElement
// eats an element returning:
// a bool to signfify if a valid element
// the tag name
// the default (xmlns=) namespace.
func eatElement(in io.ByteReader, c byte) (bool, string, string) {
buf := make([]byte, 32)
var (
err error
tag, ns string
idx int
)
for ; err == nil; c, err = in.ReadByte() {
switch c {
default:
if idx >= len(buf) {
cp := make([]byte, len(buf)*2)
copy(cp, buf)
buf = cp
}
buf[idx] = c
idx++
case '>', ' ', '\r', '\n', '\t':
if tag == "" {
tag = string(buf[:idx])
} else if ns == "" {
ns = extractNS(buf[:idx])
}
if c == '>' {
return true, tag, ns
}
idx = 0
}
}
return false, "", ""
}
示例8: readBitPackedInts
func readBitPackedInts(buffer []int64, offset int, length int, bitSize int, r io.ByteReader) error {
var bitsLeft int
var current int
for i := offset; i < (offset + length); i++ {
var result int64
bitsLeftToRead := bitSize
for bitsLeftToRead > bitsLeft {
result <<= uint64(bitsLeft)
result |= int64(current & ((1 << uint64(bitsLeft)) - 1))
bitsLeftToRead -= bitsLeft
b, err := r.ReadByte()
if err != nil {
return err
}
current = int(b)
bitsLeft = 8
}
// handle the left over bits
if bitsLeftToRead > 0 {
result <<= uint64(bitsLeftToRead)
bitsLeft -= bitsLeftToRead
result |= int64((current >> uint64(bitsLeft)) & ((1 << uint64(bitsLeftToRead)) - 1))
}
buffer[i] = result
}
return nil
}
示例9: Read
func (st *StringTable) Read(reader io.ByteReader) error {
var (
err error
currentString string = ""
currentIndex uint32 = 0
b byte
)
st.index = make(map[string]uint32)
st.length, err = readMultiByteUint32(reader)
if err == nil && st.length > 0 {
st.content = make([]byte, st.length)
var index uint32
for index = 0; index < st.length; index++ {
b, err = reader.ReadByte()
if err != nil {
break
}
st.content[index] = b
if b > 0 {
currentString += fmt.Sprintf("%c", b)
} else {
st.index[currentString] = currentIndex
currentString = ""
currentIndex = index + 1
}
}
}
return err
}
示例10: readByte
func readByte(r io.ByteReader) (byte, error) {
b, err := r.ReadByte()
if err == io.EOF {
err = io.ErrUnexpectedEOF
}
return b, err
}
示例11: ReadBytes
// ReadBytes reads an encoded []byte from buf, returning the number of bytes
// read, and any read error encountered.
func ReadBytes(buf io.ByteReader) (ret []byte, n int, err error) {
tmpBuf := bytes.Buffer{}
acc := byte(0)
for i := 0; i < ReadByteLimit; i++ {
o := byte(0)
if o, err = buf.ReadByte(); err != nil {
return
}
n++
b := o & 0xfe // user data
m := uint(i % 8)
if m == 0 {
acc = b
} else {
// ignore err since bytes.Buffer.WriteByte can never return one.
_ = tmpBuf.WriteByte(acc | (b >> (8 - m)))
acc = (b << m)
}
if o&1 == 0 { // stop bit is 0
ret = tmpBuf.Bytes()
return
}
}
err = ErrByteLimitExceeded
return
}
示例12: cmp
func cmp(br1, br2 io.ByteReader, f1, f2 string) {
status := 0
char := int64(0)
line := int64(1)
for {
b1, e1 := br1.ReadByte()
b2, e2 := br2.ReadByte()
char++
if e1 != nil || e2 != nil {
if e1 == e2 {
break
}
if e1 == io.EOF {
fmt.Printf("EOF on %v\n", f1)
}
if e2 == io.EOF {
fmt.Printf("EOF on %v\n", f2)
}
break
}
if b1 == '\n' {
line++
}
if b1 != b2 {
printDiff(f1, f2, char, line, b1, b2)
status = 1
}
}
os.Exit(status)
}
示例13: SkipLine
func SkipLine(r io.ByteReader) error {
for {
if b, err := r.ReadByte(); err != nil || b == '\n' {
return err
}
}
return nil
}
示例14: match
// match implements the internal logic for running a single match, reading
// in each byte of the ByteReader and traversing the internal AC tree to look
// for matches.
func (t *acTree) match(reader io.ByteReader, output chan Match) {
m := t.Matcher()
for b, err := reader.ReadByte(); err != io.EOF; b, err = reader.ReadByte() {
for _, match := range m.Next(b) {
output <- match
}
}
close(output)
}
示例15: more
func more(r io.ByteReader, more byte) (x uint64, err error) {
x = 0
for more != 0 {
var b byte
if b, err = r.ReadByte(); err != nil {
return
}
more--
x += uint64(b) << (8 * more)
}
return
}