]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
tpl/compare: Sort special float values as string
authoracclassic <84147355+acclassic@users.noreply.github.com>
Mon, 2 Jan 2023 16:35:08 +0000 (17:35 +0100)
committerGitHub <noreply@github.com>
Mon, 2 Jan 2023 16:35:08 +0000 (17:35 +0100)
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

.gitignore [deleted file]
tpl/compare/compare.go
tpl/compare/compare_test.go

diff --git a/.gitignore b/.gitignore
deleted file mode 100644 (file)
index b2aeb91..0000000
+++ /dev/null
@@ -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
index 47f77cc99df6742977d5abcc5ba1bf10e9d2e566..2ef5aacfefaaef9ddb79a4073e9e2b1371ed15b9 100644 (file)
@@ -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
                }
index ce2016b386e47d852a373f90e30e329d133c9bfe..4c50f5f0f87895f275d2fa6b3ed5e4127b82d8f3 100644 (file)
@@ -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},