本文整理汇总了Golang中io.RuneScanner.ReadRune方法的典型用法代码示例。如果您正苦于以下问题:Golang RuneScanner.ReadRune方法的具体用法?Golang RuneScanner.ReadRune怎么用?Golang RuneScanner.ReadRune使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类io.RuneScanner
的用法示例。
在下文中一共展示了RuneScanner.ReadRune方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: 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)
}
}
示例2: 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
}
}
}
示例3: 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")
}
}
}
示例4: 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
}
}
}
示例5: 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
}
示例6: 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[]")
}
}
}
示例7: 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
}
}
示例8: readRune
func readRune(r io.RuneScanner) int {
rune, _, err := r.ReadRune()
if err != nil {
return -1
}
return rune
}
示例9: parseCompoundAddr
func parseCompoundAddr(rs io.RuneScanner) (Address, error) {
var a1 Address
for {
switch r, _, err := rs.ReadRune(); {
case err == io.EOF:
return a1, nil
case err != nil:
return nil, err
case strings.ContainsRune(simpleFirst, r):
if err := rs.UnreadRune(); err != nil {
return nil, err
}
switch a2, err := parseSimpleAddr(rs); {
case err != nil:
return nil, err
case a1 != nil:
a1 = a1.Plus(a2)
default:
a1 = a2
}
case r == '+' || r == '-':
if a1 == nil {
a1 = Dot
}
a2, err := parseSimpleAddr(rs)
if a2 == nil {
a2 = Line(1)
}
switch {
case err != nil:
return nil, err
case r == '+':
a1 = a1.Plus(a2)
default:
a1 = a1.Minus(a2)
}
case r == ',' || r == ';':
if a1 == nil {
a1 = Line(0)
}
a2, err := parseCompoundAddr(rs)
if a2 == nil {
a2 = End
}
switch {
case err != nil:
return nil, err
case r == ',':
a1 = a1.To(a2)
default:
a1 = a1.Then(a2)
}
case unicode.IsSpace(r) && r != '\n':
continue
default:
return a1, rs.UnreadRune()
}
}
}
示例10: 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
}
示例11: 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
}
示例12: 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
}
}
}
示例13: 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
}
}
}
示例14: 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()
}
}
示例15: readNumber
func readNumber(b int, r io.RuneScanner) string {
s := string(b)
rune, _, err := r.ReadRune()
for err == nil && string(rune) != " " && string(rune) != "(" && string(rune) != ")" {
s = s + string(rune)
rune, _, err = r.ReadRune()
}
if err == nil {
r.UnreadRune()
}
return s
}