tpl/compare: Sort special float values as string - hugo - [fork] hugo port for 9front
(HTM) git clone git@git.drkhsh.at/hugo.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) Submodules
(DIR) README
(DIR) LICENSE
---
(DIR) commit f95fd57aaccb999b6fefe7b2ac9239a6c3325b0f
(DIR) parent e754d5cb3e6545ab5c21668a3e775ca5e9084761
(HTM) Author: acclassic <84147355+acclassic@users.noreply.github.com>
Date: Mon, 2 Jan 2023 17:35:08 +0100
tpl/compare: Sort special float values as string
When sorting strings a worng order is returned. This happens because the strings are first converted
to floating values to check whether or not they should be sorted as
floating values. When an error is returned the strings will be
handled as string literals.
No error will be returned when parsing Inf, Infinity or NaN (case insensitive) because they
will be coverted to special floating point values and therefore are
legal float values.
Now we check if the returned converted values are special floating
values and treat them as string literals.
Fixes #10389
Diffstat:
D .gitignore | 29 -----------------------------
M tpl/compare/compare.go | 7 +++++--
M tpl/compare/compare_test.go | 2 ++
3 files changed, 7 insertions(+), 31 deletions(-)
---
(DIR) diff --git a/.gitignore b/.gitignore
@@ -1,29 +0,0 @@
-/hugo
-docs/public*
-/.idea
-.vscode/*
-hugo.exe
-*.test
-*.prof
-nohup.out
-cover.out
-*.swp
-*.swo
-.DS_Store
-*~
-vendor/*/
-*.bench
-*.debug
-coverage*.out
-
-dock.sh
-
-GoBuilds
-dist
-
-hugolib/hugo_stats.json
-resources/sunset.jpg
-
-vendor
-
-.hugo_build.lock
(DIR) diff --git a/tpl/compare/compare.go b/tpl/compare/compare.go
@@ -16,6 +16,7 @@ package compare
import (
"fmt"
+ "math"
"reflect"
"strconv"
"time"
@@ -273,7 +274,8 @@ func (ns *Namespace) compareGetWithCollator(collator *langs.Collator, a any, b a
case reflect.String:
var err error
left, err = strconv.ParseFloat(av.String(), 64)
- if err != nil {
+ // Check if float is a special floating value and cast value as string.
+ if math.IsInf(left, 0) || math.IsNaN(left) || err != nil {
str := av.String()
leftStr = &str
}
@@ -300,7 +302,8 @@ func (ns *Namespace) compareGetWithCollator(collator *langs.Collator, a any, b a
case reflect.String:
var err error
right, err = strconv.ParseFloat(bv.String(), 64)
- if err != nil {
+ // Check if float is a special floating value and cast value as string.
+ if math.IsInf(right, 0) || math.IsNaN(right) || err != nil {
str := bv.String()
rightStr = &str
}
(DIR) diff --git a/tpl/compare/compare_test.go b/tpl/compare/compare_test.go
@@ -217,6 +217,8 @@ func doTestCompare(t *testing.T, tp tstCompareType, funcUnderTest func(a, b any)
{"a", "a", 0},
{"a", "b", -1},
{"b", "a", 1},
+ {"infinity", "infinity", 0},
+ {"nan", "nan", 0},
{tstEqerType1("a"), tstEqerType1("a"), 0},
{tstEqerType1("a"), tstEqerType2("a"), 0},
{tstEqerType2("a"), tstEqerType1("a"), 0},