1
0
mirror of https://gitea.com/Sirherobrine23/tea.git synced 2024-08-20 20:57:25 +00:00
tea/vendor/github.com/src-d/gcfg/types/scan.go
Lunny Xiao d4f107b710 Add Makefile / .drone.yml, use go module with vendor (#20)
* add Makefile / .drone.yml, use go module with vendor

* Update .drone.yml

Co-Authored-By: lunny <xiaolunwen@gmail.com>
2019-04-25 20:06:53 +03:00

24 lines
623 B
Go

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
}