1
1
package tagalign
2
2
3
3
import (
4
+ "cmp"
4
5
"fmt"
5
6
"go/ast"
6
7
"reflect"
7
- "sort "
8
+ "slices "
8
9
"strconv"
9
10
"strings"
10
11
@@ -200,7 +201,7 @@ func (w *Helper) Process(pass *analysis.Pass) {
200
201
cp [i ] = tag
201
202
}
202
203
notSortedTagsGroup = append (notSortedTagsGroup , cp )
203
- sortBy (w .fixedTagOrder , tags )
204
+ sortTags (w .fixedTagOrder , tags )
204
205
}
205
206
for _ , t := range tags .Tags () {
206
207
addKey (t .Key )
@@ -211,7 +212,7 @@ func (w *Helper) Process(pass *analysis.Pass) {
211
212
}
212
213
213
214
if w .sort && StrictStyle == w .style {
214
- sortAllKeys (w .fixedTagOrder , uniqueKeys )
215
+ sortKeys (w .fixedTagOrder , uniqueKeys )
215
216
maxTagNum = len (uniqueKeys )
216
217
}
217
218
@@ -318,7 +319,7 @@ func (w *Helper) Process(pass *analysis.Pass) {
318
319
}
319
320
originalTags := append ([]* structtag.Tag (nil ), tags .Tags ()... )
320
321
if w .sort {
321
- sortBy (w .fixedTagOrder , tags )
322
+ sortTags (w .fixedTagOrder , tags )
322
323
}
323
324
324
325
newTagValue := fmt .Sprintf ("`%s`" , tags .String ())
@@ -333,61 +334,37 @@ func (w *Helper) Process(pass *analysis.Pass) {
333
334
}
334
335
}
335
336
336
- // sortBy sorts tags by fixed order.
337
+ // sortTags sorts tags by fixed order.
337
338
// If a tag is not in the fixed order, it will be sorted by name.
338
- func sortBy (fixedOrder []string , tags * structtag.Tags ) {
339
- // sort by fixed order
340
- sort .Slice (tags .Tags (), func (i , j int ) bool {
341
- ti := tags .Tags ()[i ]
342
- tj := tags .Tags ()[j ]
343
-
344
- oi := findIndex (fixedOrder , ti .Key )
345
- oj := findIndex (fixedOrder , tj .Key )
346
-
347
- if oi == - 1 && oj == - 1 {
348
- return ti .Key < tj .Key
349
- }
350
-
351
- if oi == - 1 {
352
- return false
353
- }
354
-
355
- if oj == - 1 {
356
- return true
357
- }
358
-
359
- return oi < oj
339
+ func sortTags (fixedOrder []string , tags * structtag.Tags ) {
340
+ slices .SortFunc (tags .Tags (), func (a , b * structtag.Tag ) int {
341
+ return compareByFixedOrder (fixedOrder )(a .Key , b .Key )
360
342
})
361
343
}
362
344
363
- func sortAllKeys (fixedOrder []string , keys []string ) {
364
- sort .Slice (keys , func (i , j int ) bool {
365
- oi := findIndex (fixedOrder , keys [i ])
366
- oj := findIndex (fixedOrder , keys [j ])
345
+ func sortKeys (fixedOrder []string , keys []string ) {
346
+ slices .SortFunc (keys , compareByFixedOrder (fixedOrder ))
347
+ }
348
+
349
+ func compareByFixedOrder (fixedOrder []string ) func (a , b string ) int {
350
+ return func (a , b string ) int {
351
+ oi := slices .Index (fixedOrder , a )
352
+ oj := slices .Index (fixedOrder , b )
367
353
368
354
if oi == - 1 && oj == - 1 {
369
- return keys [ i ] < keys [ j ]
355
+ return strings . Compare ( a , b )
370
356
}
371
357
372
358
if oi == - 1 {
373
- return false
359
+ return 1
374
360
}
375
361
376
362
if oj == - 1 {
377
- return true
363
+ return - 1
378
364
}
379
365
380
- return oi < oj
381
- })
382
- }
383
-
384
- func findIndex (s []string , e string ) int {
385
- for i , a := range s {
386
- if a == e {
387
- return i
388
- }
366
+ return cmp .Compare (oi , oj )
389
367
}
390
- return - 1
391
368
}
392
369
393
370
func alignFormat (length int ) string {
0 commit comments