iota.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * Copyright 2014 Paul Querna
  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. *
  16. */
  17. /* Portions of this file are on Go stdlib's strconv/iota.go */
  18. // Copyright 2009 The Go Authors. All rights reserved.
  19. // Use of this source code is governed by a BSD-style
  20. // license that can be found in the LICENSE file.
  21. package v1
  22. import (
  23. "io"
  24. )
  25. const (
  26. digits = "0123456789abcdefghijklmnopqrstuvwxyz"
  27. digits01 = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
  28. digits10 = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999"
  29. )
  30. var shifts = [len(digits) + 1]uint{
  31. 1 << 1: 1,
  32. 1 << 2: 2,
  33. 1 << 3: 3,
  34. 1 << 4: 4,
  35. 1 << 5: 5,
  36. }
  37. var smallNumbers = [][]byte{
  38. []byte("0"),
  39. []byte("1"),
  40. []byte("2"),
  41. []byte("3"),
  42. []byte("4"),
  43. []byte("5"),
  44. []byte("6"),
  45. []byte("7"),
  46. []byte("8"),
  47. []byte("9"),
  48. []byte("10"),
  49. }
  50. type FormatBitsWriter interface {
  51. io.Writer
  52. io.ByteWriter
  53. }
  54. type FormatBitsScratch struct{}
  55. //
  56. // DEPRECIATED: `scratch` is no longer used, FormatBits2 is available.
  57. //
  58. // FormatBits computes the string representation of u in the given base.
  59. // If neg is set, u is treated as negative int64 value. If append_ is
  60. // set, the string is appended to dst and the resulting byte slice is
  61. // returned as the first result value; otherwise the string is returned
  62. // as the second result value.
  63. //
  64. func FormatBits(scratch *FormatBitsScratch, dst FormatBitsWriter, u uint64, base int, neg bool) {
  65. FormatBits2(dst, u, base, neg)
  66. }
  67. // FormatBits2 computes the string representation of u in the given base.
  68. // If neg is set, u is treated as negative int64 value. If append_ is
  69. // set, the string is appended to dst and the resulting byte slice is
  70. // returned as the first result value; otherwise the string is returned
  71. // as the second result value.
  72. //
  73. func FormatBits2(dst FormatBitsWriter, u uint64, base int, neg bool) {
  74. if base < 2 || base > len(digits) {
  75. panic("strconv: illegal AppendInt/FormatInt base")
  76. }
  77. // fast path for small common numbers
  78. if u <= 10 {
  79. if neg {
  80. dst.WriteByte('-')
  81. }
  82. dst.Write(smallNumbers[u])
  83. return
  84. }
  85. // 2 <= base && base <= len(digits)
  86. var a = makeSlice(65)
  87. // var a [64 + 1]byte // +1 for sign of 64bit value in base 2
  88. i := len(a)
  89. if neg {
  90. u = -u
  91. }
  92. // convert bits
  93. if base == 10 {
  94. // common case: use constants for / and % because
  95. // the compiler can optimize it into a multiply+shift,
  96. // and unroll loop
  97. for u >= 100 {
  98. i -= 2
  99. q := u / 100
  100. j := uintptr(u - q*100)
  101. a[i+1] = digits01[j]
  102. a[i+0] = digits10[j]
  103. u = q
  104. }
  105. if u >= 10 {
  106. i--
  107. q := u / 10
  108. a[i] = digits[uintptr(u-q*10)]
  109. u = q
  110. }
  111. } else if s := shifts[base]; s > 0 {
  112. // base is power of 2: use shifts and masks instead of / and %
  113. b := uint64(base)
  114. m := uintptr(b) - 1 // == 1<<s - 1
  115. for u >= b {
  116. i--
  117. a[i] = digits[uintptr(u)&m]
  118. u >>= s
  119. }
  120. } else {
  121. // general case
  122. b := uint64(base)
  123. for u >= b {
  124. i--
  125. a[i] = digits[uintptr(u%b)]
  126. u /= b
  127. }
  128. }
  129. // u < base
  130. i--
  131. a[i] = digits[uintptr(u)]
  132. // add sign, if any
  133. if neg {
  134. i--
  135. a[i] = '-'
  136. }
  137. dst.Write(a[i:])
  138. Pool(a)
  139. return
  140. }