forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/gopkg.in/yaml.v3/sorter.go

253 lines
3.4 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Copyright (c) 2011-2019 Canonical Ltd
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
2024-02-18 10:42:21 +00:00
// you may not use this file except in compliance with the License.
2024-02-18 10:42:21 +00:00
// You may obtain a copy of the License at
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Unless required by applicable law or agreed to in writing, software
2024-02-18 10:42:21 +00:00
// distributed under the License is distributed on an "AS IS" BASIS,
2024-02-18 10:42:21 +00:00
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2024-02-18 10:42:21 +00:00
// See the License for the specific language governing permissions and
2024-02-18 10:42:21 +00:00
// limitations under the License.
package yaml
import (
"reflect"
"unicode"
)
type keyList []reflect.Value
func (l keyList) Len() int { return len(l) }
2024-02-18 10:42:21 +00:00
func (l keyList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
2024-02-18 10:42:21 +00:00
func (l keyList) Less(i, j int) bool {
2024-02-18 10:42:21 +00:00
a := l[i]
2024-02-18 10:42:21 +00:00
b := l[j]
2024-02-18 10:42:21 +00:00
ak := a.Kind()
2024-02-18 10:42:21 +00:00
bk := b.Kind()
2024-02-18 10:42:21 +00:00
for (ak == reflect.Interface || ak == reflect.Ptr) && !a.IsNil() {
2024-02-18 10:42:21 +00:00
a = a.Elem()
2024-02-18 10:42:21 +00:00
ak = a.Kind()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for (bk == reflect.Interface || bk == reflect.Ptr) && !b.IsNil() {
2024-02-18 10:42:21 +00:00
b = b.Elem()
2024-02-18 10:42:21 +00:00
bk = b.Kind()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
af, aok := keyFloat(a)
2024-02-18 10:42:21 +00:00
bf, bok := keyFloat(b)
2024-02-18 10:42:21 +00:00
if aok && bok {
2024-02-18 10:42:21 +00:00
if af != bf {
2024-02-18 10:42:21 +00:00
return af < bf
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if ak != bk {
2024-02-18 10:42:21 +00:00
return ak < bk
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return numLess(a, b)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if ak != reflect.String || bk != reflect.String {
2024-02-18 10:42:21 +00:00
return ak < bk
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
ar, br := []rune(a.String()), []rune(b.String())
2024-02-18 10:42:21 +00:00
digits := false
2024-02-18 10:42:21 +00:00
for i := 0; i < len(ar) && i < len(br); i++ {
2024-02-18 10:42:21 +00:00
if ar[i] == br[i] {
2024-02-18 10:42:21 +00:00
digits = unicode.IsDigit(ar[i])
2024-02-18 10:42:21 +00:00
continue
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
al := unicode.IsLetter(ar[i])
2024-02-18 10:42:21 +00:00
bl := unicode.IsLetter(br[i])
2024-02-18 10:42:21 +00:00
if al && bl {
2024-02-18 10:42:21 +00:00
return ar[i] < br[i]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if al || bl {
2024-02-18 10:42:21 +00:00
if digits {
2024-02-18 10:42:21 +00:00
return al
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
return bl
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var ai, bi int
2024-02-18 10:42:21 +00:00
var an, bn int64
2024-02-18 10:42:21 +00:00
if ar[i] == '0' || br[i] == '0' {
2024-02-18 10:42:21 +00:00
for j := i - 1; j >= 0 && unicode.IsDigit(ar[j]); j-- {
2024-02-18 10:42:21 +00:00
if ar[j] != '0' {
2024-02-18 10:42:21 +00:00
an = 1
2024-02-18 10:42:21 +00:00
bn = 1
2024-02-18 10:42:21 +00:00
break
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for ai = i; ai < len(ar) && unicode.IsDigit(ar[ai]); ai++ {
2024-02-18 10:42:21 +00:00
an = an*10 + int64(ar[ai]-'0')
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
for bi = i; bi < len(br) && unicode.IsDigit(br[bi]); bi++ {
2024-02-18 10:42:21 +00:00
bn = bn*10 + int64(br[bi]-'0')
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if an != bn {
2024-02-18 10:42:21 +00:00
return an < bn
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if ai != bi {
2024-02-18 10:42:21 +00:00
return ai < bi
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return ar[i] < br[i]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return len(ar) < len(br)
2024-02-18 10:42:21 +00:00
}
// keyFloat returns a float value for v if it is a number/bool
2024-02-18 10:42:21 +00:00
// and whether it is a number/bool or not.
2024-02-18 10:42:21 +00:00
func keyFloat(v reflect.Value) (f float64, ok bool) {
2024-02-18 10:42:21 +00:00
switch v.Kind() {
2024-02-18 10:42:21 +00:00
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
2024-02-18 10:42:21 +00:00
return float64(v.Int()), true
2024-02-18 10:42:21 +00:00
case reflect.Float32, reflect.Float64:
2024-02-18 10:42:21 +00:00
return v.Float(), true
2024-02-18 10:42:21 +00:00
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
2024-02-18 10:42:21 +00:00
return float64(v.Uint()), true
2024-02-18 10:42:21 +00:00
case reflect.Bool:
2024-02-18 10:42:21 +00:00
if v.Bool() {
2024-02-18 10:42:21 +00:00
return 1, true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return 0, true
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return 0, false
2024-02-18 10:42:21 +00:00
}
// numLess returns whether a < b.
2024-02-18 10:42:21 +00:00
// a and b must necessarily have the same kind.
2024-02-18 10:42:21 +00:00
func numLess(a, b reflect.Value) bool {
2024-02-18 10:42:21 +00:00
switch a.Kind() {
2024-02-18 10:42:21 +00:00
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
2024-02-18 10:42:21 +00:00
return a.Int() < b.Int()
2024-02-18 10:42:21 +00:00
case reflect.Float32, reflect.Float64:
2024-02-18 10:42:21 +00:00
return a.Float() < b.Float()
2024-02-18 10:42:21 +00:00
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
2024-02-18 10:42:21 +00:00
return a.Uint() < b.Uint()
2024-02-18 10:42:21 +00:00
case reflect.Bool:
2024-02-18 10:42:21 +00:00
return !a.Bool() && b.Bool()
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
panic("not a number")
2024-02-18 10:42:21 +00:00
}