本文整理匯總了Golang中fmt.ScanState類的典型用法代碼示例。如果您正苦於以下問題:Golang ScanState類的具體用法?Golang ScanState怎麽用?Golang ScanState使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了ScanState類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: scanEnum
// ScanEnum is a helper function to simplify the implementation of fmt.Scanner
// methods for "enum-like" types, that is, user-defined types where the set of
// values and string representations is fixed.
// ScanEnum allows multiple string representations for the same value.
//
// State is the state passed to the implementation of the fmt.Scanner method.
// Values holds as map values the values of the type, with their string
// representations as keys.
// If fold is true, comparison of the string representation uses
// strings.EqualFold, otherwise the equal operator for strings.
//
// On a match, ScanEnum stops after reading the last rune of the matched string,
// and returns the corresponding value together with a nil error.
// On no match, ScanEnum attempts to unread the last rune (the first rune that
// could not potentially match any of the values), and returns a non-nil error,
// together with a nil value for interface{}.
// On I/O error, ScanEnum returns the I/O error, together with a nil value for
// interface{}.
//
func scanEnum(state fmt.ScanState, values map[string]interface{}, fold bool) (
interface{}, error) {
//
rd := make([]rune, 0, scanEnumBufferHint)
keys := make(map[string]struct{}, len(values)) // potential keys
for s, _ := range values {
keys[s] = struct{}{}
}
for {
r, _, err := state.ReadRune()
if err != nil {
return nil, err
}
rd = append(rd, r)
srd := string(rd)
lrd := len(srd)
for s, _ := range keys {
if strEq(srd, s, fold) {
return values[s], nil
}
if len(rd) < len(s) && !strEq(srd, s[:lrd], fold) {
delete(keys, s)
}
}
if len(keys) == 0 {
state.UnreadRune()
return nil, fmt.Errorf("unsupported value %q", srd)
}
}
panic("never reached")
}
示例2: Scan
func (u *unit) Scan(state fmt.ScanState, verb rune) error {
var x float64
_, err := fmt.Fscan(state, &x)
if err != nil {
return err
}
tok, err := state.Token(false, unicode.IsLetter)
if err != nil {
return err
}
units := string(tok)
switch units {
case "ns", "", "b":
// already in nanoseconds or bytes
case "us":
x *= 1e3
case "ms":
x *= 1e6
case "s":
x *= 1e9
case "k", "kb", "K", "KB":
x *= 1024
case "m", "mb", "M", "MB":
x *= 1024 * 1024
default:
return fmt.Errorf("unknown time or size unit %q", units)
}
*u = unit(x)
return nil
}
示例3: Scan
// Scan is a support routine for fmt.Scanner; it sets z to the value of
// the scanned number. It accepts the decimal formats 'd' and 'f', and
// handles both equivalently. Bases 2, 8, 16 are not supported.
// The scale of z is the number of digits after the decimal point
// (including any trailing 0s), or 0 if there is no decimal point.
func (z *Dec) Scan(s fmt.ScanState, ch rune) error {
if ch != 'd' && ch != 'f' && ch != 's' && ch != 'v' {
return fmt.Errorf("Dec.Scan: invalid verb '%c'", ch)
}
s.SkipSpace()
_, err := z.scan(s)
return err
}
示例4: scanInt
func scanInt(state fmt.ScanState) (int, error) {
token, err := state.Token(true, func(r rune) bool {
return unicode.IsDigit(r)
})
if err != nil {
return 0, err
}
res, err := strconv.ParseInt(string(token), 0, 64)
return int(res), err
}
示例5: Scan
func (c *coords) Scan(state fmt.ScanState, verb rune) error {
rx, _, _ := state.ReadRune()
ry, _, _ := state.ReadRune()
if rx < 'A' || 'G' < rx || ry < '1' || '8' < ry {
return fmt.Errorf("Illegal chess coordinates: <%c, %c>", rx, ry)
}
c.x = int(rx - 'A')
c.y = int(ry - '1')
return nil
}
示例6: Scan
// Scan implements fmt.Scanner interface. This makes it possible to use
// fmt.Sscan*() functions to parse syslog facility codes. Also
// "gcfg" package can parse them in configuration files.
func (s *Severity) Scan(state fmt.ScanState, verb rune) error {
sevstr, err := state.Token(false, func(r rune) bool { return true })
if err != nil {
return err
}
sev, ok := severityCodes[string(sevstr)]
if !ok {
return errors.New(`invalid severity "` + string(sevstr) + `"`)
}
*s = sev
return nil
}
示例7: Scan
// Implement fmt.Scanner interface. This makes it possible to use fmt.Sscan*()
// functions to parse syslog facility codes directly. Also "gcfg" package can
// parse them in configuration files.
func (f *SyslogFacility) Scan(state fmt.ScanState, verb rune) error {
facstr, err := state.Token(false, func(r rune) bool { return true })
if err != nil {
return err
}
fac, ok := syslogFacilityCodes[string(facstr)]
if !ok {
return errors.New(`invalid facility "` + string(facstr) + `"`)
}
*f = fac
return nil
}
示例8: Scan
// Implement fmt.Scanner interface.
func (i *ibpiID) Scan(state fmt.ScanState, verb rune) error {
ibpistr, err := state.Token(false, func(r rune) bool { return true })
if err != nil {
return err
}
ibpiid, ok := ibpiNameToId[string(ibpistr)]
if !ok {
return errors.New(`invalid IBPI string "` + string(ibpistr) + `"`)
}
*i = ibpiid
return nil
}
示例9: Scan
// Scan is a support routine for fmt.Scanner. It accepts the formats
// 'e', 'E', 'f', 'F', 'g', 'G', and 'v'. All formats are equivalent.
func (z *Rat) Scan(s fmt.ScanState, ch rune) error {
tok, err := s.Token(true, ratTok)
if err != nil {
return err
}
if strings.IndexRune("efgEFGv", ch) < 0 {
return errors.New("Rat.Scan: invalid verb")
}
if _, ok := z.SetString(string(tok)); !ok {
return errors.New("Rat.Scan: invalid syntax")
}
return nil
}
示例10: Scan
func (h *Hand) Scan(st fmt.ScanState, x int) os.Error {
str, e := st.Token()
if e != nil {
return e
}
l := len(str)
hSpades := ReadSuit(str)
str, e = st.Token()
if e != nil {
return e
}
hHearts := ReadSuit(str[l:])
l = len(str)
str, e = st.Token()
if e != nil {
return e
}
hDiamonds := ReadSuit(str[l:])
l = len(str)
str, e = st.Token()
if e != nil {
return e
}
hClubs := ReadSuit(str[l:])
*h = Hand(hClubs) + (Hand(hDiamonds) << 8) + (Hand(hHearts) << 16) + (Hand(hSpades) << 24)
return e
}
示例11: Scan
// Scan is a support routine for fmt.Scanner; it sets z to the value of
// the scanned number. It accepts the formats 'b' (binary), 'o' (octal),
// 'd' (decimal), 'x' (lowercase hexadecimal), and 'X' (uppercase hexadecimal).
func (z *Int) Scan(s fmt.ScanState, ch int) os.Error {
s.SkipSpace() // skip leading space characters
base := 0
switch ch {
case 'b':
base = 2
case 'o':
base = 8
case 'd':
base = 10
case 'x', 'X':
base = 16
case 's', 'v':
// let scan determine the base
default:
return os.NewError("Int.Scan: invalid verb")
}
_, _, err := z.scan(s, base)
return err
}
示例12: Scan
func (pKey *peerKey) Scan(state fmt.ScanState, verb rune) error {
ipB, err := state.Token(true, nil)
if err != nil {
return err
}
pKey.peerIP = net.ParseIP(string(ipB))
macB, err := state.Token(true, nil)
if err != nil {
return err
}
pKey.peerMac, err = net.ParseMAC(string(macB))
if err != nil {
return err
}
return nil
}
示例13: Scan
// Implement fmt.Scanner interface.
func (smapp *stringToStringMap) Scan(state fmt.ScanState, verb rune) error {
smap := make(stringToStringMap)
for {
tok, err := state.Token(true, nil)
if err != nil {
return err
}
if len(tok) == 0 { // end of string
break
}
str := string(tok)
pair := strings.SplitN(str, ":", 2)
if len(pair) != 2 {
return errors.New(`invalid map entry "` + str + `"`)
}
smap[pair[0]] = pair[1]
}
*smapp = smap
return nil
}
示例14: Scan
func (h Histogram) Scan(state fmt.ScanState, verb rune) (e error) {
braceCount := 0
matchBrace := func(r rune) bool {
if r == '{' {
braceCount += 1
} else if r == '{' {
braceCount -= 1
}
return braceCount > 0
}
var data []byte
if data, e = state.Token(true, matchBrace); e != nil {
return
}
json.Unmarshal(data, &h)
return
}
示例15: Scan
func (d *Direction) Scan(state fmt.ScanState, verb rune) error {
bs, err := state.Token(true, nil)
if err != nil {
return err
}
bs = bytes.ToLower(bs)
switch {
case bytes.Compare(bs, []byte("up")) == 0:
*d = UP
case bytes.Compare(bs, []byte("down")) == 0:
*d = DOWN
case bytes.Compare(bs, []byte("left")) == 0:
*d = LEFT
case bytes.Compare(bs, []byte("right")) == 0:
*d = RIGHT
default:
return fmt.Errorf("Invalid direction: %s", bs)
}
return nil
}