sorter.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // Copyright (c) 2011-2019 Canonical Ltd
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package yaml
  16. import (
  17. "reflect"
  18. "unicode"
  19. )
  20. type keyList []reflect.Value
  21. func (l keyList) Len() int { return len(l) }
  22. func (l keyList) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
  23. func (l keyList) Less(i, j int) bool {
  24. a := l[i]
  25. b := l[j]
  26. ak := a.Kind()
  27. bk := b.Kind()
  28. for (ak == reflect.Interface || ak == reflect.Ptr) && !a.IsNil() {
  29. a = a.Elem()
  30. ak = a.Kind()
  31. }
  32. for (bk == reflect.Interface || bk == reflect.Ptr) && !b.IsNil() {
  33. b = b.Elem()
  34. bk = b.Kind()
  35. }
  36. af, aok := keyFloat(a)
  37. bf, bok := keyFloat(b)
  38. if aok && bok {
  39. if af != bf {
  40. return af < bf
  41. }
  42. if ak != bk {
  43. return ak < bk
  44. }
  45. return numLess(a, b)
  46. }
  47. if ak != reflect.String || bk != reflect.String {
  48. return ak < bk
  49. }
  50. ar, br := []rune(a.String()), []rune(b.String())
  51. for i := 0; i < len(ar) && i < len(br); i++ {
  52. if ar[i] == br[i] {
  53. continue
  54. }
  55. al := unicode.IsLetter(ar[i])
  56. bl := unicode.IsLetter(br[i])
  57. if al && bl {
  58. return ar[i] < br[i]
  59. }
  60. if al || bl {
  61. return bl
  62. }
  63. var ai, bi int
  64. var an, bn int64
  65. if ar[i] == '0' || br[i] == '0' {
  66. for j := i - 1; j >= 0 && unicode.IsDigit(ar[j]); j-- {
  67. if ar[j] != '0' {
  68. an = 1
  69. bn = 1
  70. break
  71. }
  72. }
  73. }
  74. for ai = i; ai < len(ar) && unicode.IsDigit(ar[ai]); ai++ {
  75. an = an*10 + int64(ar[ai]-'0')
  76. }
  77. for bi = i; bi < len(br) && unicode.IsDigit(br[bi]); bi++ {
  78. bn = bn*10 + int64(br[bi]-'0')
  79. }
  80. if an != bn {
  81. return an < bn
  82. }
  83. if ai != bi {
  84. return ai < bi
  85. }
  86. return ar[i] < br[i]
  87. }
  88. return len(ar) < len(br)
  89. }
  90. // keyFloat returns a float value for v if it is a number/bool
  91. // and whether it is a number/bool or not.
  92. func keyFloat(v reflect.Value) (f float64, ok bool) {
  93. switch v.Kind() {
  94. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  95. return float64(v.Int()), true
  96. case reflect.Float32, reflect.Float64:
  97. return v.Float(), true
  98. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  99. return float64(v.Uint()), true
  100. case reflect.Bool:
  101. if v.Bool() {
  102. return 1, true
  103. }
  104. return 0, true
  105. }
  106. return 0, false
  107. }
  108. // numLess returns whether a < b.
  109. // a and b must necessarily have the same kind.
  110. func numLess(a, b reflect.Value) bool {
  111. switch a.Kind() {
  112. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  113. return a.Int() < b.Int()
  114. case reflect.Float32, reflect.Float64:
  115. return a.Float() < b.Float()
  116. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  117. return a.Uint() < b.Uint()
  118. case reflect.Bool:
  119. return !a.Bool() && b.Bool()
  120. }
  121. panic("not a number")
  122. }