Просмотр исходного кода

Fix unstable map key ordering (#195).

Gustavo Niemeyer 8 лет назад
Родитель
Сommit
dcd83b31fd
2 измененных файлов с 18 добавлено и 1 удалено
  1. 8 1
      encode_test.go
  2. 10 0
      sorter.go

+ 8 - 1
encode_test.go

@@ -527,8 +527,13 @@ func (s *S) TestSortedOutput(c *C) {
 		"1",
 		"2",
 		"a!10",
-		"a/2",
+		"a/0001",
+		"a/002",
+		"a/3",
 		"a/10",
+		"a/11",
+		"a/0012",
+		"a/100",
 		"a~10",
 		"ab/1",
 		"b/1",
@@ -543,6 +548,8 @@ func (s *S) TestSortedOutput(c *C) {
 		"c2.10",
 		"c10.2",
 		"d1",
+		"d7",
+		"d7abc",
 		"d12",
 		"d12a",
 	}

+ 10 - 0
sorter.go

@@ -51,6 +51,16 @@ func (l keyList) Less(i, j int) bool {
 		}
 		var ai, bi int
 		var an, bn int64
+		if ar[i] == '0' || br[i] == '0' {
+			j := i
+			for j > 0 && unicode.IsDigit(ar[j-1]) {
+				j--
+			}
+			if j != i && ar[j] != '0' {
+				an = 1
+				bn = 1
+			}
+		}
 		for ai = i; ai < len(ar) && unicode.IsDigit(ar[ai]); ai++ {
 			an = an*10 + int64(ar[ai]-'0')
 		}