Skip to content

Commit 9f86e0b

Browse files
committed
go.text/language: make it build with Go 1.1, which does not include
sort.Stable. Fixes golang/go#6523. R=r CC=golang-dev https://golang.org/cl/14265043
1 parent 6e2c2aa commit 9f86e0b

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

language/go1_1.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2013 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build !go1.2
6+
7+
package language
8+
9+
import "sort"
10+
11+
func sortStable(s sort.Interface) {
12+
ss := stableSort{
13+
s: s,
14+
pos: make([]int, s.Len()),
15+
}
16+
for i := range ss.pos {
17+
ss.pos[i] = i
18+
}
19+
sort.Sort(&ss)
20+
}
21+
22+
type stableSort struct {
23+
s sort.Interface
24+
pos []int
25+
}
26+
27+
func (s *stableSort) Len() int {
28+
return len(s.pos)
29+
}
30+
31+
func (s *stableSort) Less(i, j int) bool {
32+
return s.s.Less(i, j) || !s.s.Less(j, i) && s.pos[i] < s.pos[j]
33+
}
34+
35+
func (s *stableSort) Swap(i, j int) {
36+
s.s.Swap(i, j)
37+
s.pos[i], s.pos[j] = s.pos[j], s.pos[i]
38+
}

language/go1_2.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2013 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// +build go1.2
6+
7+
package language
8+
9+
import "sort"
10+
11+
var sortStable = sort.Stable

language/parse.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ func ParseAcceptLanguage(s string) (tag []Tag, q []float32, err error) {
595595
q = append(q, float32(w))
596596
}
597597
}
598-
sort.Stable(&tagSort{tag, q})
598+
sortStable(&tagSort{tag, q})
599599
return tag, q, nil
600600
}
601601

0 commit comments

Comments
 (0)