From f36e1adaa2c72d74dc669b596ea1c4df5e938def Mon Sep 17 00:00:00 2001 From: Emmanuel Odeke Date: Sun, 9 Oct 2016 22:45:17 -0700 Subject: math/big: implement Float.Scan, type assert fmt interfaces to enforce docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implements Float.Scan which satisfies fmt.Scanner interface. Also enforces docs' interface implementation claims with compile time type assertions, that is: + Float always implements fmt.Formatter and fmt.Scanner + Int always implements fmt.Formatter and fmt.Scanner + Rat always implements fmt.Formatter which will ensure that the API claims are strictly matched. Also note that Float.Scan doesn't handle ±Inf. Fixes #17391 Change-Id: I3d3dfbe7f602066975c7a7794fe25b4c645440ce Reviewed-on: https://go-review.googlesource.com/30723 Reviewed-by: Robert Griesemer --- src/math/big/floatconv.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/math/big/floatconv.go') diff --git a/src/math/big/floatconv.go b/src/math/big/floatconv.go index 4ba03bc105..186dfe4a6e 100644 --- a/src/math/big/floatconv.go +++ b/src/math/big/floatconv.go @@ -12,6 +12,8 @@ import ( "strings" ) +var floatZero Float + // SetString sets z to the value of s and returns z and a boolean indicating // success. s must be a floating-point number of the same format as accepted // by Parse, with base argument 0. The entire string (not just a prefix) must @@ -276,3 +278,16 @@ func (z *Float) Parse(s string, base int) (f *Float, b int, err error) { func ParseFloat(s string, base int, prec uint, mode RoundingMode) (f *Float, b int, err error) { return new(Float).SetPrec(prec).SetMode(mode).Parse(s, base) } + +var _ fmt.Scanner = &floatZero // *Float must implement fmt.Scanner + +// Scan is a support routine for fmt.Scanner; it sets z to the value of +// the scanned number. It accepts formats whose verbs are supported by +// fmt.Scan for floating point values, which are: +// 'b' (binary), 'e', 'E', 'f', 'F', 'g' and 'G'. +// Scan doesn't handle ±Inf. +func (z *Float) Scan(s fmt.ScanState, ch rune) error { + s.SkipSpace() + _, _, err := z.scan(byteReader{s}, 0) + return err +} -- cgit v1.3