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 · 1 comment
Labels
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants