forked from Logicalis/asn1
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsort.go
34 lines (26 loc) · 1.05 KB
/
sort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package asn1
import "sort"
// isTagLessThan compares two tags (class + tag number)
// TODO: maybe a common Tag type can simplify that.
func isTagLessThan(c1, t1, c2, t2 uint) bool {
if c1 == c2 {
return t1 < t2
}
return c1 < c2
}
// rawValueSlice is a helper type to sort an slice of RawValues
type rawValueSlice []*rawValue
var _ sort.Interface = rawValueSlice{}
func (s rawValueSlice) Len() int { return len(s) }
func (s rawValueSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s rawValueSlice) Less(i, j int) bool {
return isTagLessThan(s[i].Class, s[i].Tag, s[j].Class, s[j].Tag)
}
// expectedFieldElementSlice is a helper type to sort an slice of expectedFieldElement
type expectedFieldElementSlice []expectedFieldElement
var _ sort.Interface = expectedFieldElementSlice{}
func (s expectedFieldElementSlice) Len() int { return len(s) }
func (s expectedFieldElementSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s expectedFieldElementSlice) Less(i, j int) bool {
return isTagLessThan(s[i].class, s[i].tag, s[j].class, s[j].tag)
}