本文整理汇总了Golang中fmt.ScanState类的典型用法代码示例。如果您正苦于以下问题:Golang ScanState类的具体用法?Golang ScanState怎么用?Golang ScanState使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ScanState类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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")
}
开发者ID:0987363,项目名称:zfswatcher,代码行数:50,代码来源:scanenum.go
示例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
}
开发者ID:zxpbenson,项目名称:rog-go,代码行数:30,代码来源:parse.go
示例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
}
开发者ID:40a,项目名称:bootkube,代码行数:13,代码来源:dec.go
示例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
}
开发者ID:finkf,项目名称:gocropy,代码行数:10,代码来源:bbox.go
示例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
}
开发者ID:hraban,项目名称:chess,代码行数:10,代码来源:chess.go
最后编辑: kuteng 文档更新时间: 2021-08-23 19:14 作者:kuteng