File: scan.go

package info (click to toggle)
golang-gopkg-gcfg.v1 0.0~git20150907.0.0ef1a85-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 244 kB
  • ctags: 244
  • sloc: makefile: 7
file content (23 lines) | stat: -rw-r--r-- 623 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package types

import (
	"fmt"
	"io"
	"reflect"
)

// ScanFully uses fmt.Sscanf with verb to fully scan val into ptr.
func ScanFully(ptr interface{}, val string, verb byte) error {
	t := reflect.ValueOf(ptr).Elem().Type()
	// attempt to read extra bytes to make sure the value is consumed
	var b []byte
	n, err := fmt.Sscanf(val, "%"+string(verb)+"%s", ptr, &b)
	switch {
	case n < 1 || n == 1 && err != io.EOF:
		return fmt.Errorf("failed to parse %q as %v: %v", val, t, err)
	case n > 1:
		return fmt.Errorf("failed to parse %q as %v: extra characters %q", val, t, string(b))
	}
	// n == 1 && err == io.EOF
	return nil
}