Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve number handling by returning NaN for non-numbers #68

Merged
merged 2 commits into from
Sep 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,19 @@ func asNumber(t iterator, o interface{}) float64 {
return typ
case string:
v, err := strconv.ParseFloat(typ, 64)
if err != nil {
panic(errors.New("ceiling() function argument type must be a node-set or number"))
if err == nil {
return v
}
return v
}
return 0
return math.NaN()
}

// ceilingFunc is a XPath Node Set functions ceiling(node-set).
func ceilingFunc(q query, t iterator) interface{} {
val := asNumber(t, functionArgs(q).Evaluate(t))
// if math.IsNaN(val) {
// panic(errors.New("ceiling() function argument type must be a valid number"))
// }
return math.Ceil(val)
}

Expand Down
3 changes: 2 additions & 1 deletion func_go110.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build go1.10
// +build go1.10

package xpath
Expand All @@ -11,6 +12,6 @@ func round(f float64) int {
return int(math.Round(f))
}

func newStringBuilder() stringBuilder{
func newStringBuilder() stringBuilder {
return &strings.Builder{}
}
1 change: 1 addition & 0 deletions func_pre_go110.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !go1.10
// +build !go1.10

package xpath
Expand Down