本文整理汇总了Golang中io.RuneScanner类的典型用法代码示例。如果您正苦于以下问题:Golang RuneScanner类的具体用法?Golang RuneScanner怎么用?Golang RuneScanner使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RuneScanner类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: readRune
func readRune(r io.RuneScanner) int {
rune, _, err := r.ReadRune()
if err != nil {
return -1
}
return rune
}
示例2: ReadChar
// like in.ReadRune() but ignore all leading whitespace.
func ReadChar(in io.RuneScanner) (r rune, size int, err error) {
r, size, err = in.ReadRune()
for unicode.IsSpace(r) && err == nil {
r, size, err = in.ReadRune()
}
return
}
示例3: parseDelimited
// ParseDelimited returns the string
// up to the first unescaped delimiter,
// raw newline (rune 0xA),
// or the end of input.
// A delimiter preceeded by \ is escaped and is non-terminating.
// The letter n preceeded by \ is a newline literal.
func parseDelimited(delim rune, rs io.RuneScanner) (string, error) {
var s string
var esc bool
for {
switch r, _, err := rs.ReadRune(); {
case err == io.EOF:
return s, nil
case err != nil:
return "", err
case esc && r == delim:
s += string(delim)
esc = false
case r == delim || r == '\n':
return s, nil
case !esc && r == '\\':
esc = true
case esc && r == 'n':
s += "\n"
esc = false
default:
if esc {
s += "\\"
}
s += string(r)
esc = false
}
}
}
示例4: readSDElement
// readSDElement reads an SD-ELEMENT as defined by RFC-5424
//
// SD-ELEMENT = "[" SD-ID *(SP SD-PARAM) "]"
// SD-PARAM = PARAM-NAME "=" %d34 PARAM-VALUE %d34
// SD-ID = SD-NAME
// PARAM-NAME = SD-NAME
// PARAM-VALUE = UTF-8-STRING ; characters '"', '\' and ']' MUST be escaped.
// SD-NAME = 1*32PRINTUSASCII except '=', SP, ']', %d34 (")
func readSDElement(r io.RuneScanner) (element StructuredData, err error) {
ch, _, err := r.ReadRune()
if err != nil {
return element, err // hard to reach without underlying IO error
}
if ch != '[' {
return element, BadFormat("StructuredData[]") // unreachable
}
element.ID, err = readSdID(r)
if err != nil {
return element, err
}
for {
ch, _, err := r.ReadRune()
if err != nil {
return element, err
} else if ch == ']' {
return element, nil
} else if ch == ' ' {
param, err := readSdParam(r)
if err != nil {
return element, err
}
element.Parameters = append(element.Parameters, *param)
} else {
return element, BadFormat("StructuredData[]")
}
}
}
示例5: readStructuredData
// readStructuredData reads a STRUCTURED-DATA (as defined in RFC-5424)
// from `r` and assigns the StructuredData member.
//
// STRUCTURED-DATA = NILVALUE / 1*SD-ELEMENT
// SD-ELEMENT = "[" SD-ID *(SP SD-PARAM) "]"
// SD-PARAM = PARAM-NAME "=" %d34 PARAM-VALUE %d34
// SD-ID = SD-NAME
// PARAM-NAME = SD-NAME
// PARAM-VALUE = UTF-8-STRING ; characters '"', '\' and ']' MUST be escaped.
// SD-NAME = 1*32PRINTUSASCII except '=', SP, ']', %d34 (")
func (m *Message) readStructuredData(r io.RuneScanner) (err error) {
m.StructuredData = []StructuredData{}
ch, _, err := r.ReadRune()
if err != nil {
return err
}
if ch == '-' {
return nil
}
r.UnreadRune()
for {
ch, _, err := r.ReadRune()
if err == io.EOF {
return nil
} else if err != nil {
return err // hard to reach without underlying IO error
} else if ch == ' ' {
r.UnreadRune()
return nil
} else if ch == '[' {
r.UnreadRune()
sde, err := readSDElement(r)
if err != nil {
return err
}
m.StructuredData = append(m.StructuredData, sde)
} else {
return BadFormat("StructuredData")
}
}
}
示例6: readPriority
// readPriority reads the PRI as defined in RFC-5424 and assigns Severity and
// Facility accordingly.
func (m *Message) readPriority(r io.RuneScanner) error {
ch, _, err := r.ReadRune()
if err != nil {
return err
}
if ch != '<' {
return BadFormat("Priority")
}
rv := &bytes.Buffer{}
for {
ch, _, err := r.ReadRune()
if err != nil {
return err
}
if unicode.IsDigit(ch) {
rv.WriteRune(ch)
continue
}
if ch != '>' {
return BadFormat("Priority")
}
// We have a complete integer expression
priority, err := strconv.ParseInt(string(rv.Bytes()), 10, 32)
if err != nil {
return BadFormat("Priority")
}
m.Priority = int(priority)
return nil
}
}
示例7: parseNumber
// ParseNumber parses and returns a positive integer.
// Leading spaces are ignored.
// If EOF is reached before any digits are encountered, 1 is returned.
func parseNumber(rs io.RuneScanner) (int, error) {
if err := skipSpace(rs); err != nil {
return 0, err
}
var s string
for {
switch r, _, err := rs.ReadRune(); {
case err == io.EOF:
break
case err != nil:
return 0, err
case unicode.IsDigit(r):
s += string(r)
continue
default:
if err := rs.UnreadRune(); err != nil {
return 0, err
}
}
if len(s) == 0 {
return 1, nil
}
return strconv.Atoi(s)
}
}
示例8: parseCmd
func parseCmd(rs io.RuneScanner) (string, error) {
if err := skipSpace(rs); err != nil {
return "", err
}
var esc bool
var cmd string
for {
switch r, _, err := rs.ReadRune(); {
case err == io.EOF:
return cmd, nil
case err != nil:
return "", nil
case r == '\n':
return cmd, nil
case r == '\\':
esc = true
case esc && r == 'n':
cmd += "\n"
esc = false
default:
if esc {
cmd += "\\"
}
cmd += string(r)
esc = false
}
}
}
示例9: readSpace
// readSpace reads a single space
func readSpace(r io.RuneScanner) error {
ch, _, err := r.ReadRune()
if err != nil {
return err
}
if ch != ' ' {
return BadFormat("expected space")
}
return nil
}
示例10: readVersion
// readVersion reads the version string fails if it isn't `1`
func (m *Message) readVersion(r io.RuneScanner) error {
ch, _, err := r.ReadRune()
if err != nil {
return err
}
if ch != '1' {
return BadFormat("Version")
}
return nil
}
示例11: chompBOM
func chompBOM(r io.RuneScanner) (err error) {
for {
c, _, err := r.ReadRune()
if err != nil {
return err
}
if c != BOM {
r.UnreadRune()
return nil
}
}
}
示例12: chompWhitespace
func chompWhitespace(r io.RuneScanner) (err error) {
for {
c, _, err := r.ReadRune()
if err != nil {
return err
}
if !unicode.IsSpace(c) {
r.UnreadRune()
return nil
}
}
}
示例13: skipSingleNewline
func skipSingleNewline(rs io.RuneScanner) error {
// Eat a single trailing newline.
switch r, _, err := rs.ReadRune(); {
case err == io.EOF:
return nil
case err != nil:
return err
case r == '\n':
return nil
default:
return rs.UnreadRune()
}
}
示例14: scan
func (d *Dec) scan(r io.RuneScanner) error {
d.coef.hi = 0
d.coef.lo = 0
d.scale = 0
ch, _, err := r.ReadRune()
if err != nil {
return err
}
var neg bool
switch ch {
case '-':
neg = true
case '+':
default:
r.UnreadRune()
}
var dec bool
for {
ch, _, err = r.ReadRune()
if err == io.EOF {
goto ExitLoop
}
if err != nil {
return err
}
switch {
case ch == '.':
if dec {
r.UnreadRune()
goto ExitLoop
}
dec = true
case ch >= '0' && ch <= '9':
d.coef.Mul(&d.coef, intTen)
var z Int128
z.SetInt64(int64(ch - '0'))
d.coef.Add(&d.coef, &z)
if dec {
d.scale++
}
default:
r.UnreadRune()
goto ExitLoop
}
}
ExitLoop:
if neg {
d.Neg(d)
}
return nil
}
示例15: parseMarkRune
func parseMarkRune(rs io.RuneScanner) (rune, error) {
for {
switch r, _, err := rs.ReadRune(); {
case err == io.EOF:
return '.', nil
case err != nil:
return 0, err
case unicode.IsSpace(r):
continue
default:
return r, nil
}
}
}