suffix.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  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 at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package resource
  14. import (
  15. "strconv"
  16. )
  17. type suffix string
  18. // suffixer can interpret and construct suffixes.
  19. type suffixer interface {
  20. interpret(suffix) (base, exponent int32, fmt Format, ok bool)
  21. construct(base, exponent int32, fmt Format) (s suffix, ok bool)
  22. constructBytes(base, exponent int32, fmt Format) (s []byte, ok bool)
  23. }
  24. // quantitySuffixer handles suffixes for all three formats that quantity
  25. // can handle.
  26. var quantitySuffixer = newSuffixer()
  27. type bePair struct {
  28. base, exponent int32
  29. }
  30. type listSuffixer struct {
  31. suffixToBE map[suffix]bePair
  32. beToSuffix map[bePair]suffix
  33. beToSuffixBytes map[bePair][]byte
  34. }
  35. func (ls *listSuffixer) addSuffix(s suffix, pair bePair) {
  36. if ls.suffixToBE == nil {
  37. ls.suffixToBE = map[suffix]bePair{}
  38. }
  39. if ls.beToSuffix == nil {
  40. ls.beToSuffix = map[bePair]suffix{}
  41. }
  42. if ls.beToSuffixBytes == nil {
  43. ls.beToSuffixBytes = map[bePair][]byte{}
  44. }
  45. ls.suffixToBE[s] = pair
  46. ls.beToSuffix[pair] = s
  47. ls.beToSuffixBytes[pair] = []byte(s)
  48. }
  49. func (ls *listSuffixer) lookup(s suffix) (base, exponent int32, ok bool) {
  50. pair, ok := ls.suffixToBE[s]
  51. if !ok {
  52. return 0, 0, false
  53. }
  54. return pair.base, pair.exponent, true
  55. }
  56. func (ls *listSuffixer) construct(base, exponent int32) (s suffix, ok bool) {
  57. s, ok = ls.beToSuffix[bePair{base, exponent}]
  58. return
  59. }
  60. func (ls *listSuffixer) constructBytes(base, exponent int32) (s []byte, ok bool) {
  61. s, ok = ls.beToSuffixBytes[bePair{base, exponent}]
  62. return
  63. }
  64. type suffixHandler struct {
  65. decSuffixes listSuffixer
  66. binSuffixes listSuffixer
  67. }
  68. type fastLookup struct {
  69. *suffixHandler
  70. }
  71. func (l fastLookup) interpret(s suffix) (base, exponent int32, format Format, ok bool) {
  72. switch s {
  73. case "":
  74. return 10, 0, DecimalSI, true
  75. case "n":
  76. return 10, -9, DecimalSI, true
  77. case "u":
  78. return 10, -6, DecimalSI, true
  79. case "m":
  80. return 10, -3, DecimalSI, true
  81. case "k":
  82. return 10, 3, DecimalSI, true
  83. case "M":
  84. return 10, 6, DecimalSI, true
  85. case "G":
  86. return 10, 9, DecimalSI, true
  87. }
  88. return l.suffixHandler.interpret(s)
  89. }
  90. func newSuffixer() suffixer {
  91. sh := &suffixHandler{}
  92. // IMPORTANT: if you change this section you must change fastLookup
  93. sh.binSuffixes.addSuffix("Ki", bePair{2, 10})
  94. sh.binSuffixes.addSuffix("Mi", bePair{2, 20})
  95. sh.binSuffixes.addSuffix("Gi", bePair{2, 30})
  96. sh.binSuffixes.addSuffix("Ti", bePair{2, 40})
  97. sh.binSuffixes.addSuffix("Pi", bePair{2, 50})
  98. sh.binSuffixes.addSuffix("Ei", bePair{2, 60})
  99. // Don't emit an error when trying to produce
  100. // a suffix for 2^0.
  101. sh.decSuffixes.addSuffix("", bePair{2, 0})
  102. sh.decSuffixes.addSuffix("n", bePair{10, -9})
  103. sh.decSuffixes.addSuffix("u", bePair{10, -6})
  104. sh.decSuffixes.addSuffix("m", bePair{10, -3})
  105. sh.decSuffixes.addSuffix("", bePair{10, 0})
  106. sh.decSuffixes.addSuffix("k", bePair{10, 3})
  107. sh.decSuffixes.addSuffix("M", bePair{10, 6})
  108. sh.decSuffixes.addSuffix("G", bePair{10, 9})
  109. sh.decSuffixes.addSuffix("T", bePair{10, 12})
  110. sh.decSuffixes.addSuffix("P", bePair{10, 15})
  111. sh.decSuffixes.addSuffix("E", bePair{10, 18})
  112. return fastLookup{sh}
  113. }
  114. func (sh *suffixHandler) construct(base, exponent int32, fmt Format) (s suffix, ok bool) {
  115. switch fmt {
  116. case DecimalSI:
  117. return sh.decSuffixes.construct(base, exponent)
  118. case BinarySI:
  119. return sh.binSuffixes.construct(base, exponent)
  120. case DecimalExponent:
  121. if base != 10 {
  122. return "", false
  123. }
  124. if exponent == 0 {
  125. return "", true
  126. }
  127. return suffix("e" + strconv.FormatInt(int64(exponent), 10)), true
  128. }
  129. return "", false
  130. }
  131. func (sh *suffixHandler) constructBytes(base, exponent int32, format Format) (s []byte, ok bool) {
  132. switch format {
  133. case DecimalSI:
  134. return sh.decSuffixes.constructBytes(base, exponent)
  135. case BinarySI:
  136. return sh.binSuffixes.constructBytes(base, exponent)
  137. case DecimalExponent:
  138. if base != 10 {
  139. return nil, false
  140. }
  141. if exponent == 0 {
  142. return nil, true
  143. }
  144. result := make([]byte, 8, 8)
  145. result[0] = 'e'
  146. number := strconv.AppendInt(result[1:1], int64(exponent), 10)
  147. if &result[1] == &number[0] {
  148. return result[:1+len(number)], true
  149. }
  150. result = append(result[:1], number...)
  151. return result, true
  152. }
  153. return nil, false
  154. }
  155. func (sh *suffixHandler) interpret(suffix suffix) (base, exponent int32, fmt Format, ok bool) {
  156. // Try lookup tables first
  157. if b, e, ok := sh.decSuffixes.lookup(suffix); ok {
  158. return b, e, DecimalSI, true
  159. }
  160. if b, e, ok := sh.binSuffixes.lookup(suffix); ok {
  161. return b, e, BinarySI, true
  162. }
  163. if len(suffix) > 1 && (suffix[0] == 'E' || suffix[0] == 'e') {
  164. parsed, err := strconv.ParseInt(string(suffix[1:]), 10, 64)
  165. if err != nil {
  166. return 0, 0, DecimalExponent, false
  167. }
  168. return 10, int32(parsed), DecimalExponent, true
  169. }
  170. return 0, 0, DecimalExponent, false
  171. }