From c29afaeacca66ea5dbc3ec21d55fd814668a3186 Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Tue, 18 Jun 2019 23:13:59 +0000 Subject: [PATCH] Nope, NOW I'm done with the number types. On reading the docs, I noticed that I missed an implementation for floats 'nan' and 'inf'. All taken care of now! --- value_number.go | 21 +++++++++++---------- value_number_test.go | 6 ++++++ 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/value_number.go b/value_number.go index 6d73169..2b28c1a 100644 --- a/value_number.go +++ b/value_number.go @@ -1,6 +1,7 @@ package parser import ( + "math" "strconv" "git.makaay.nl/mauricem/go-parsekit/parse" @@ -61,22 +62,22 @@ var ( zeroFloat = a.Signed(a.Str("0.0")) // Special float values can also be expressed. They are always lowercase. - infinity = a.Signed(a.Str("inf")) - nan = a.Signed(a.Str("nan")) + inf = a.Signed(a.Str("inf")) + nan = a.Signed(a.Str("nan")) ) func (t *parser) startNumber(p *parse.API) { switch { - case p.Accept(float): - f, err := strconv.ParseFloat(p.Result().String(), 64) - if err != nil { - p.Error("Cannot parse value 0%s: %s", p.Result().String(), err) + case p.Accept(tok.Float64(nil, float)): + t.emitCommand(csetFloatVal, p.Result().Value(0).(float64)) + case p.Accept(nan): + t.emitCommand(csetFloatVal, math.NaN()) + case p.Accept(inf): + if p.Result().Rune(0) == '-' { + t.emitCommand(csetFloatVal, math.Inf(-1)) } else { - t.emitCommand(csetFloatVal, f) + t.emitCommand(csetFloatVal, math.Inf(+1)) } - case p.Accept(zeroFloat): - f, _ := strconv.ParseFloat(p.Result().String(), 64) - t.emitCommand(csetFloatVal, f) case p.Accept(zero): p.Handle(t.startIntegerStartingWithZero) case p.Accept(tok.Int64(nil, integer)): diff --git a/value_number_test.go b/value_number_test.go index fd71760..2868aef 100644 --- a/value_number_test.go +++ b/value_number_test.go @@ -94,6 +94,12 @@ func TestFloat(t *testing.T) { {`6.626e-34`, []string{`float(6.626e-34)`}}, {`224_617.445_991_228`, []string{`float(224617.445991228)`}}, {`12_345.111_222e+1_2_3`, []string{`float(1.2345111222e+127)`}}, + {`+nan`, []string{`float(NaN)`}}, + {`-nan`, []string{`float(NaN)`}}, + {`nan`, []string{`float(NaN)`}}, + {`inf`, []string{`float(+Inf)`}}, + {`+inf`, []string{`float(+Inf)`}}, + {`-inf`, []string{`float(-Inf)`}}, } { p := &parser{} testParseHandler(t, p, p.startNumber, test)