diff --git a/collate/collate_test.go b/collate/collate_test.go index 0e78b96f..b9360f26 100644 --- a/collate/collate_test.go +++ b/collate/collate_test.go @@ -6,6 +6,7 @@ package collate import ( "bytes" + "strings" "testing" "golang.org/x/text/internal/colltab" @@ -473,7 +474,20 @@ func TestNumeric(t *testing.T) { {"A-1", "A-2", -1}, {"A-2", "A-12", -1}, {"A-12", "A-2", 1}, - {"A-0001", "A-1", 0}, + {"A-0001", "A-1", 1}, + {"0000-", "1-", -1}, + {"00001", "1", 1}, + {"00", "00", 0}, + {"0", "00", -1}, + {"00", "0", 1}, + {"01", "001", -1}, + {"01", "1", 1}, + {"1", "01", -1}, + {"9-A", "0-A", 1}, + {"99-A", "0-A", 1}, + {"9-A", "1-A", 1}, + {"99-A", "1-A", 1}, + {strings.Repeat("9", 270)+"-A", "1-A", 1}, } { if got := c.CompareString(tt.a, tt.b); got != tt.want { t.Errorf("%d: CompareString(%s, %s) = %d; want %d", i, tt.a, tt.b, got, tt.want) diff --git a/internal/colltab/numeric.go b/internal/colltab/numeric.go index 53b819cc..ebc2acf3 100644 --- a/internal/colltab/numeric.go +++ b/internal/colltab/numeric.go @@ -80,6 +80,7 @@ func (nw *numericWeighter) AppendNext(buf []Elem, s []byte) (ce []Elem, n int) { } // ce might have been grown already, so take it instead of buf. nc.init(ce, len(buf), isZero) + old_index := len(nc.elems) for n < len(s) { ce, sz := nw.Weighter.AppendNext(nc.elems, s[n:]) nc.b = s @@ -87,7 +88,12 @@ func (nw *numericWeighter) AppendNext(buf []Elem, s []byte) (ce []Elem, n int) { if !nc.update(ce) { break } + old_index = len(nc.elems) } + nc.elems = append(nc.elems, 0) + copy(nc.elems[old_index+1:], nc.elems[old_index:]) + nc.elems[old_index], _ = MakeElem(nc.zero+1, defaultSecondary, defaultTertiary, 0) + return nc.result(), n } @@ -105,6 +111,7 @@ func (nw *numericWeighter) AppendNextString(buf []Elem, s string) (ce []Elem, n return ce, n } nc.init(ce, len(buf), isZero) + old_index := len(nc.elems) for n < len(s) { ce, sz := nw.Weighter.AppendNextString(nc.elems, s[n:]) nc.s = s @@ -112,7 +119,12 @@ func (nw *numericWeighter) AppendNextString(buf []Elem, s string) (ce []Elem, n if !nc.update(ce) { break } + old_index = len(nc.elems) } + nc.elems = append(nc.elems, 0) + copy(nc.elems[old_index+1:], nc.elems[old_index:]) + nc.elems[old_index], _ = MakeElem(nc.zero+1, defaultSecondary, defaultTertiary, 0) + return nc.result(), n } @@ -122,6 +134,7 @@ type numberConverter struct { elems []Elem nDigits int lenIndex int + zero int s string // set if the input was of type string b []byte // set if the input was of type []byte @@ -133,6 +146,7 @@ func (nc *numberConverter) init(elems []Elem, oldLen int, isZero bool) { // Insert a marker indicating the start of a number and a placeholder // for the number of digits. if isZero { + nc.zero++ elems = append(elems[:oldLen], nc.w.numberStart, 0) } else { elems = append(elems, 0, 0) @@ -217,6 +231,9 @@ const maxDigits = 1<