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

x/text/internal/colltab: numeric.go should not skip "0" when is followed by a non-number #25554

Open
tianyiii opened this issue May 25, 2018 · 3 comments · May be fixed by golang/text#50
Open
Labels
FixPending Issues that have a fix which has not yet been reviewed or submitted. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@tianyiii
Copy link

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

go1.8.5

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/tlin/go"
GORACE=""
GOROOT="/usr/local/go"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/9g/342mj7bd3h776hmqdw5c9xdn5wch_k/T/go-build934806691=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
PKG_CONFIG="pkg-config"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"

What did you do?

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

package main

import (
	"fmt"

	"golang.org/x/text/collate"
	"golang.org/x/text/language"
)

func main() {
	strings := []string{
		"file0_1",
		"file1_1",
	}
	collator := collate.New(language.English, collate.Numeric)
	collator.SortStrings(strings)
	for _, s := range strings {
		fmt.Println(s)
	}
}

What did you expect to see?

The output to be:

file0_1
file1_1

What did you see instead?

The actual output is:

file1_1
file0_1

Possible fix would be:

+++ golang.org/x/text/internal/colltab/numeric.go
@@ -79,7 +79,11 @@
 		return ce, n
 	}
 	// ce might have been grown already, so take it instead of buf.
-	nc.init(ce, len(buf), isZero)
+      nextByte := byte('0');
+      if n < len(s) {
+         nextByte = s[n]
+      }
+	nc.init(ce, len(buf), isZero, nextByte)
 	for n < len(s) {
 		ce, sz := nw.Weighter.AppendNext(nc.elems, s[n:])
 		nc.b = s
@@ -104,7 +108,11 @@
 	if !ok {
 		return ce, n
 	}
-	nc.init(ce, len(buf), isZero)
+      nextByte := byte('0');
+      if n < len(s) {
+         nextByte = s[n]
+      }
+	nc.init(ce, len(buf), isZero, nextByte)
 	for n < len(s) {
 		ce, sz := nw.Weighter.AppendNextString(nc.elems, s[n:])
 		nc.s = s
@@ -129,10 +137,10 @@
 
 // init completes initialization of a numberConverter and prepares it for adding
 // more digits. elems is assumed to have a digit starting at oldLen.
-func (nc *numberConverter) init(elems []Elem, oldLen int, isZero bool) {
+func (nc *numberConverter) init(elems []Elem, oldLen int, isZero bool, nextByte byte) {
 	// Insert a marker indicating the start of a number and and a placeholder
 	// for the number of digits.
-	if isZero {
+	if isZero && nextByte >= byte('0') && nextByte <= byte('9') {
 		elems = append(elems[:oldLen], nc.w.numberStart, 0)
 	} else {
 		elems = append(elems, 0, 0)
@gopherbot gopherbot added this to the Unreleased milestone May 25, 2018
@ALTree ALTree added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 12, 2020
@dmitshur
Copy link
Contributor

CC @mpvl per owners.

lordwelch added a commit to lordwelch/text that referenced this issue May 5, 2024
Fixes golang/go#25554
Sorts zero (0) as the first number instead of the last
Sorts numbers with leading zeros after numbers with less leading zeros
lordwelch added a commit to lordwelch/text that referenced this issue May 5, 2024
Sorts zero (0) as the first number instead of the last
Sorts numbers with leading zeros after numbers with less leading zeros

Fixes golang/go#25554
lordwelch added a commit to lordwelch/text that referenced this issue May 5, 2024
Sorts zero (0) as the first number instead of the last
Sorts numbers with leading zeros after numbers with less leading zeros

Fixes golang/go#25554
lordwelch added a commit to lordwelch/text that referenced this issue May 5, 2024
Sorts zero (0) as the first number instead of the last
Sorts numbers with leading zeros after numbers with less leading zeros

Fixes golang/go#25554
lordwelch added a commit to lordwelch/text that referenced this issue May 5, 2024
Elem.Primary() == 0 has odd ordering properties, starting at 1 allows 0
  to consistently be ordered before other numbers when non-numeric text
  follows a 0
  Also fixes an issue comparing numbers > 269 characters with
  numbers < 270 characters

Fixes golang/go#25554
@lordwelch lordwelch linked a pull request May 5, 2024 that will close this issue
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/583257 mentions this issue: internal/colltab: Improve numeric sorting

@dmitshur dmitshur added the FixPending Issues that have a fix which has not yet been reviewed or submitted. label May 6, 2024
@WeiweiGao
Copy link

Any updates on this issue? It’s affecting our project, and a prompt fix would be greatly appreciated. Thank you!

lordwelch added a commit to lordwelch/text that referenced this issue Nov 7, 2024
Elem.Primary() == 0 has odd ordering properties, starting at 1 allows 0
  to consistently be ordered before other numbers when non-numeric text
  follows a 0
  Also fixes an issue comparing numbers > 269 characters with
  numbers < 270 characters

Fixes golang/go#25554
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FixPending Issues that have a fix which has not yet been reviewed or submitted. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants