-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevicenames.go
219 lines (186 loc) · 3.89 KB
/
devicenames.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package network
import (
"sort"
"strconv"
"unicode/utf8"
)
const (
EOF = iota
LITERAL
NUMBER
)
type token int
type deviceNameScanner struct {
src string
// scanning state
ch rune // current character
offset int // character offset
rdOffset int // reading offset (position of next ch)
}
type deviceName struct {
name string
tokens []int
}
type devices []deviceName
func (d devices) Len() int {
return len(d)
}
func (d devices) Less(i, j int) bool {
if r := intCompare(d[i].tokens, d[j].tokens); r == -1 {
return true
} else {
return false
}
}
func (d devices) Swap(i, j int) {
d[i], d[j] = d[j], d[i]
}
// adapted from runtime/noasm.go
func intCompare(s1, s2 []int) int {
l := len(s1)
if len(s2) < l {
l = len(s2)
}
if l == 0 || &s1[0] == &s2[0] {
goto samebytes
}
for i := 0; i < l; i++ {
c1, c2 := s1[i], s2[i]
if c1 < c2 {
return -1
}
if c1 > c2 {
return +1
}
}
samebytes:
if len(s1) < len(s2) {
return -1
}
if len(s1) > len(s2) {
return +1
}
return 0
}
func (s *deviceNameScanner) init(src string) {
s.src = src
s.ch = ' '
s.offset = 0
s.rdOffset = 0
s.next()
}
func (s *deviceNameScanner) next() {
if s.rdOffset < len(s.src) {
s.offset = s.rdOffset
r, w := rune(s.src[s.rdOffset]), 1
s.rdOffset += w
s.ch = r
} else {
s.offset = len(s.src)
s.ch = -1 // EOF
}
}
func (s *deviceNameScanner) peek() rune {
if s.rdOffset < len(s.src) {
r, _ := rune(s.src[s.rdOffset]), 1
return r
}
return -1
}
func isDigit(ch rune) bool {
return '0' <= ch && ch <= '9'
}
func (s *deviceNameScanner) scanNumber() string {
// Treat leading zeros as discrete numbers as this aids the
// natural sort ordering. We also only parse whole numbers;
// floating point values are considered an integer- and
// fractional-part.
if s.ch == '0' && s.peek() == '0' {
s.next()
return "0"
}
cur := s.offset
for isDigit(s.ch) {
s.next()
}
return s.src[cur:s.offset]
}
func (s *deviceNameScanner) scan() (tok token, lit string) {
switch ch := s.ch; {
case -1 == ch:
return EOF, ""
case '0' <= ch && ch <= '9':
return NUMBER, s.scanNumber()
default:
lit = string(s.ch)
s.next()
return LITERAL, lit
}
}
func parseDeviceName(src string) deviceName {
var s deviceNameScanner
s.init(src)
d := deviceName{name: src}
for {
tok, lit := s.scan()
switch tok {
case EOF:
return d
case LITERAL:
x, _ := utf8.DecodeRuneInString(lit)
d.tokens = append(d.tokens, int(x))
case NUMBER:
val, _ := strconv.Atoi(lit)
d.tokens = append(d.tokens, val)
}
}
}
func parseDeviceNames(args ...string) devices {
devices := make(devices, 0)
for _, a := range args {
devices = append(devices, parseDeviceName(a))
}
return devices
}
// NaturallySortDeviceNames returns an ordered list of names based on
// a natural ordering where 'natural' is an ordering of the string
// value in alphabetical order, execept that multi-digit numbers are
// ordered as a single character.
//
// For example, sorting:
//
// [ br-eth10 br-eth1 br-eth2 ]
//
// would sort as:
//
// [ br-eth1 br-eth2 br-eth10 ]
//
// In purely alphabetical sorting "br-eth10" would be sorted before
// "br-eth2" because "1" is sorted as smaller than "2", while in
// natural sorting "br-eth2" is sorted before "br-eth10" because "2"
// is sorted as smaller than "10".
//
// This also extends to multiply repeated numbers (e.g., VLANs).
//
// For example, sorting:
//
// [ br-eth2 br-eth10.10 br-eth200.0 br-eth1.0 br-eth2.0 ]
//
// would sort as:
//
// [ br-eth1.0 br-eth2 br-eth2.0 br-eth10.10 br-eth200.0 ]
//
func NaturallySortDeviceNames(names ...string) []string {
if names == nil {
return nil
}
devices := parseDeviceNames(names...)
sort.Sort(devices)
sortedNames := make([]string, len(devices))
for i, v := range devices {
sortedNames[i] = v.name
}
return sortedNames
}