Path: blob/main/vendor/go.yaml.in/yaml/v3/sorter.go
2872 views
//1// Copyright (c) 2011-2019 Canonical Ltd2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.1415package yaml1617import (18"reflect"19"unicode"20)2122type keyList []reflect.Value2324func (l keyList) Len() int { return len(l) }25func (l keyList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }26func (l keyList) Less(i, j int) bool {27a := l[i]28b := l[j]29ak := a.Kind()30bk := b.Kind()31for (ak == reflect.Interface || ak == reflect.Ptr) && !a.IsNil() {32a = a.Elem()33ak = a.Kind()34}35for (bk == reflect.Interface || bk == reflect.Ptr) && !b.IsNil() {36b = b.Elem()37bk = b.Kind()38}39af, aok := keyFloat(a)40bf, bok := keyFloat(b)41if aok && bok {42if af != bf {43return af < bf44}45if ak != bk {46return ak < bk47}48return numLess(a, b)49}50if ak != reflect.String || bk != reflect.String {51return ak < bk52}53ar, br := []rune(a.String()), []rune(b.String())54digits := false55for i := 0; i < len(ar) && i < len(br); i++ {56if ar[i] == br[i] {57digits = unicode.IsDigit(ar[i])58continue59}60al := unicode.IsLetter(ar[i])61bl := unicode.IsLetter(br[i])62if al && bl {63return ar[i] < br[i]64}65if al || bl {66if digits {67return al68} else {69return bl70}71}72var ai, bi int73var an, bn int6474if ar[i] == '0' || br[i] == '0' {75for j := i - 1; j >= 0 && unicode.IsDigit(ar[j]); j-- {76if ar[j] != '0' {77an = 178bn = 179break80}81}82}83for ai = i; ai < len(ar) && unicode.IsDigit(ar[ai]); ai++ {84an = an*10 + int64(ar[ai]-'0')85}86for bi = i; bi < len(br) && unicode.IsDigit(br[bi]); bi++ {87bn = bn*10 + int64(br[bi]-'0')88}89if an != bn {90return an < bn91}92if ai != bi {93return ai < bi94}95return ar[i] < br[i]96}97return len(ar) < len(br)98}99100// keyFloat returns a float value for v if it is a number/bool101// and whether it is a number/bool or not.102func keyFloat(v reflect.Value) (f float64, ok bool) {103switch v.Kind() {104case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:105return float64(v.Int()), true106case reflect.Float32, reflect.Float64:107return v.Float(), true108case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:109return float64(v.Uint()), true110case reflect.Bool:111if v.Bool() {112return 1, true113}114return 0, true115}116return 0, false117}118119// numLess returns whether a < b.120// a and b must necessarily have the same kind.121func numLess(a, b reflect.Value) bool {122switch a.Kind() {123case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:124return a.Int() < b.Int()125case reflect.Float32, reflect.Float64:126return a.Float() < b.Float()127case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:128return a.Uint() < b.Uint()129case reflect.Bool:130return !a.Bool() && b.Bool()131}132panic("not a number")133}134135136