本文整理汇总了Golang中io.ByteScanner类的典型用法代码示例。如果您正苦于以下问题:Golang ByteScanner类的具体用法?Golang ByteScanner怎么用?Golang ByteScanner使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ByteScanner类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: parseCombinator
func parseCombinator(rdr io.ByteScanner, p *Link) error {
rdr.UnreadByte()
for c, err := rdr.ReadByte(); err != io.EOF; c, err = rdr.ReadByte() {
if err != nil {
return err
}
switch c {
case '{':
rdr.UnreadByte()
return EOS
case ',':
return fmt.Errorf("Encountered ',' after combinator")
case ' ', '\t', '\n', '\r', '\f':
case '>', '+', '~':
if p.Combinator == Descendant {
p.Combinator = combinatorMap[c]
} else {
return fmt.Errorf("Can't combine multiple combinators")
}
default:
rdr.UnreadByte()
return nil
}
}
return nil
}
示例2: Read
func (t *Terminal) Read(r io.ByteScanner) error {
c, err := r.ReadByte()
if err != nil {
return err
}
switch {
case c == 0x7: // bell
// ignore
case c == 0x8: // backspace
if t.Col > 0 {
t.Col--
}
case c == 0x1b:
return t.readEscape(r)
case c == '\r':
t.Col = 0
case c == '\n':
t.Col = 0
t.Row++
t.fixPosition()
case c == '\t':
t.Col += 8 - (t.Col % 8)
t.fixPosition()
case c >= ' ' && c <= '~':
t.writeRune(rune(c), t.Attr)
default:
r.UnreadByte()
return t.readUTF8(r)
}
return nil
}
示例3: readString
func readString(buf io.ByteScanner) Literal {
strbuf := []byte{}
loop:
for {
b, err := buf.ReadByte()
if err != nil {
break
}
switch b {
case '"':
return String(string(strbuf))
case '\\':
b, err := buf.ReadByte()
if err != nil {
break loop
}
switch b {
case 't':
strbuf = append(strbuf, '\t')
case 'n':
strbuf = append(strbuf, '\n')
case '\\':
strbuf = append(strbuf, '\\')
case '"':
strbuf = append(strbuf, '"')
default:
panic("read: unknown escape")
}
default:
strbuf = append(strbuf, b)
}
}
panic("read: unterminated string")
return String("")
}
示例4: read
func read(buf io.ByteScanner) Literal {
skipws(buf)
b, err := buf.ReadByte()
if err == io.EOF {
panic("read: premature end of file")
}
if err != nil {
panic("read: input error: " + err.Error())
}
switch b {
case ')':
panic("read: unmatched close-parenthesis")
case '"':
return readString(buf)
case '\'':
return readQuote(buf)
case '`':
return readQuasi(buf)
case ',':
return readComma(buf)
case '&':
return readAmpersand(buf)
case '(':
return readList(buf)
}
buf.UnreadByte()
return readAtom(buf)
}
示例5: readAtom
func readAtom(buf io.ByteScanner) Literal {
atomBuf := []byte{}
loop:
for {
b, err := buf.ReadByte()
if err != nil {
break
}
switch b {
case '(', ')', '\'', '"', ' ', '\t', '\n':
buf.UnreadByte()
break loop
}
atomBuf = append(atomBuf, b)
}
if len(atomBuf) == 0 {
panic("read: empty atom")
}
atom := string(atomBuf)
n, err := strconv.ParseFloat(atom, 64)
if err == nil {
return Number(n)
}
return Intern(atom)
}
示例6: nextLine
func nextLine(buf io.ByteScanner) {
for {
b, err := buf.ReadByte()
if err != nil || b == '\n' {
break
}
}
}
示例7: TestMockByteScanner
func TestMockByteScanner(t *testing.T) {
var s io.ByteScanner
m := &ByteScannerMock{}
s = m
m.On("ReadByte").Return(byte('_'), nil)
b, err := s.ReadByte()
assert.Equal(t, byte('_'), b)
assert.Equal(t, nil, err)
}
示例8: readTerminator
func readTerminator(r io.ByteScanner, term byte) (bool, error) {
tok, err := r.ReadByte()
if err != nil {
return false, err
} else if tok == term {
return true, nil
}
return false, r.UnreadByte()
}
示例9: expect
func (t *Terminal) expect(r io.ByteScanner, exp byte) (bool, error) {
c, err := r.ReadByte()
if err != nil {
return false, err
}
ok := c == exp
if !ok {
log.Printf("expect %s failed, got %s", showChar(exp), showChar(c))
}
return ok, nil
}
示例10: readAmpersand
func readAmpersand(buf io.ByteScanner) Literal {
skipws(buf)
b, err := buf.ReadByte()
if err != nil {
panic("read: incomplete input")
}
if b != '(' {
panic("read: ill-formed ampersand")
}
x := readList(buf)
return newListLiteral(Intern("ampersand"), x)
}
示例11: parseSequence
func parseSequence(rdr io.ByteScanner) (Sequence, error) {
seq := []SimpleSelector{}
rdr.UnreadByte()
for c, err := rdr.ReadByte(); err != io.EOF; c, err = rdr.ReadByte() {
if err != nil {
return nil, err
}
switch c {
case '*':
seq = append(seq, SimpleSelector{Type: Universal})
case '#':
sel := SimpleSelector{Type: Id, AttrName: "id"}
if err := parseSimpleSelector(rdr, &sel); err != nil {
return nil, err
}
seq = append(seq, sel)
case '.':
sel := SimpleSelector{Type: Class, AttrName: "class"}
if err := parseSimpleSelector(rdr, &sel); err != nil {
return nil, err
}
seq = append(seq, sel)
case ':':
sel := SimpleSelector{Type: PseudoClass}
if err := parseSimpleSelector(rdr, &sel); err != nil {
return nil, err
}
seq = append(seq, sel)
case '[':
sel := SimpleSelector{Type: Attr}
if err := parseSimpleAttr(rdr, &sel); err != nil {
return nil, err
}
seq = append(seq, sel)
case '{':
rdr.UnreadByte()
return seq, EOS
case ' ', '\t', '\n', '\r', '\f', '>', '+', '~':
rdr.UnreadByte()
return seq, nil
default:
sel := SimpleSelector{Type: Tag, Tag: string(c)}
if err := parseSimpleTag(rdr, &sel); err != nil {
return nil, err
}
seq = append(seq, sel)
}
}
return seq, nil
}
示例12: readTo
func (t *Terminal) readTo(r io.ByteScanner, end byte) ([]byte, error) {
var buf []byte
for i := 0; i < 1000; i++ {
c, err := r.ReadByte()
if err != nil {
return nil, err
}
if c == end {
return buf, nil
}
buf = append(buf, c)
}
return nil, fmt.Errorf("term: readTo(%s) overlong", showChar(end))
}
示例13: readComma
func readComma(buf io.ByteScanner) Literal {
b, err := buf.ReadByte()
if err != nil {
panic("read: incomplete comma")
}
tag := Intern("unquote")
if b == '@' {
tag = Intern("unquotesplicing")
} else {
buf.UnreadByte()
}
x := read(buf)
return newListLiteral(tag, x)
}
示例14: scanSign
func scanSign(r io.ByteScanner) (neg bool, err error) {
var ch byte
if ch, err = r.ReadByte(); err != nil {
return false, err
}
switch ch {
case '-':
neg = true
case '+':
// nothing to do
default:
r.UnreadByte()
}
return
}
示例15: readInt
func (t *Terminal) readInt(r io.ByteScanner) (int, error) {
n := 0
for i := 0; i < 20; i++ {
c, err := r.ReadByte()
if err != nil {
return -1, err
}
if c >= '0' && c <= '9' {
n = n*10 + int(c) - '0'
} else {
r.UnreadByte()
return n, err
}
}
return -1, fmt.Errorf("term: readInt overlong")
}