sorter.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. digits := false
  52. for i := 0; i < len(ar) && i < len(br); i++ {
  53. if ar[i] == br[i] {
  54. digits = unicode.IsDigit(ar[i])
  55. continue
  56. }
  57. al := unicode.IsLetter(ar[i])
  58. bl := unicode.IsLetter(br[i])
  59. if al && bl {
  60. return ar[i] < br[i]
  61. }
  62. if al || bl {
  63. if digits {
  64. return al
  65. } else {
  66. return bl
  67. }
  68. }
  69. var ai, bi int
  70. var an, bn int64
  71. if ar[i] == '0' || br[i] == '0' {
  72. for j := i - 1; j >= 0 && unicode.IsDigit(ar[j]); j-- {
  73. if ar[j] != '0' {
  74. an = 1
  75. bn = 1
  76. break
  77. }
  78. }
  79. }
  80. for ai = i; ai < len(ar) && unicode.IsDigit(ar[ai]); ai++ {
  81. an = an*10 + int64(ar[ai]-'0')
  82. }
  83. for bi = i; bi < len(br) && unicode.IsDigit(br[bi]); bi++ {
  84. bn = bn*10 + int64(br[bi]-'0')
  85. }
  86. if an != bn {
  87. return an < bn
  88. }
  89. if ai != bi {
  90. return ai < bi
  91. }
  92. return ar[i] < br[i]
  93. }
  94. return len(ar) < len(br)
  95. }
  96. // keyFloat returns a float value for v if it is a number/bool
  97. // and whether it is a number/bool or not.
  98. func keyFloat(v reflect.Value) (f float64, ok bool) {
  99. switch v.Kind() {
  100. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  101. return float64(v.Int()), true
  102. case reflect.Float32, reflect.Float64:
  103. return v.Float(), true
  104. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  105. return float64(v.Uint()), true
  106. case reflect.Bool:
  107. if v.Bool() {
  108. return 1, true
  109. }
  110. return 0, true
  111. }
  112. return 0, false
  113. }
  114. // numLess returns whether a < b.
  115. // a and b must necessarily have the same kind.
  116. func numLess(a, b reflect.Value) bool {
  117. switch a.Kind() {
  118. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  119. return a.Int() < b.Int()
  120. case reflect.Float32, reflect.Float64:
  121. return a.Float() < b.Float()
  122. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  123. return a.Uint() < b.Uint()
  124. case reflect.Bool:
  125. return !a.Bool() && b.Bool()
  126. }
  127. panic("not a number")
  128. }