itoa.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package strconv
  5. // FormatUint returns the string representation of i in the given base,
  6. // for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
  7. // for digit values >= 10.
  8. func FormatUint(i uint64, base int) string {
  9. _, s := formatBits(nil, i, base, false, false)
  10. return s
  11. }
  12. // FormatInt returns the string representation of i in the given base,
  13. // for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z'
  14. // for digit values >= 10.
  15. func FormatInt(i int64, base int) string {
  16. _, s := formatBits(nil, uint64(i), base, i < 0, false)
  17. return s
  18. }
  19. // Itoa is shorthand for FormatInt(i, 10).
  20. func Itoa(i int) string {
  21. return FormatInt(int64(i), 10)
  22. }
  23. // AppendInt appends the string form of the integer i,
  24. // as generated by FormatInt, to dst and returns the extended buffer.
  25. func AppendInt(dst []byte, i int64, base int) []byte {
  26. dst, _ = formatBits(dst, uint64(i), base, i < 0, true)
  27. return dst
  28. }
  29. // AppendUint appends the string form of the unsigned integer i,
  30. // as generated by FormatUint, to dst and returns the extended buffer.
  31. func AppendUint(dst []byte, i uint64, base int) []byte {
  32. dst, _ = formatBits(dst, i, base, false, true)
  33. return dst
  34. }
  35. const (
  36. digits = "0123456789abcdefghijklmnopqrstuvwxyz"
  37. digits01 = "0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
  38. digits10 = "0000000000111111111122222222223333333333444444444455555555556666666666777777777788888888889999999999"
  39. )
  40. var shifts = [len(digits) + 1]uint{
  41. 1 << 1: 1,
  42. 1 << 2: 2,
  43. 1 << 3: 3,
  44. 1 << 4: 4,
  45. 1 << 5: 5,
  46. }
  47. // formatBits computes the string representation of u in the given base.
  48. // If neg is set, u is treated as negative int64 value. If append_ is
  49. // set, the string is appended to dst and the resulting byte slice is
  50. // returned as the first result value; otherwise the string is returned
  51. // as the second result value.
  52. //
  53. func formatBits(dst []byte, u uint64, base int, neg, append_ bool) (d []byte, s string) {
  54. if base < 2 || base > len(digits) {
  55. panic("strconv: illegal AppendInt/FormatInt base")
  56. }
  57. // 2 <= base && base <= len(digits)
  58. var a [64 + 1]byte // +1 for sign of 64bit value in base 2
  59. i := len(a)
  60. if neg {
  61. u = -u
  62. }
  63. // convert bits
  64. if base == 10 {
  65. // common case: use constants for / and % because
  66. // the compiler can optimize it into a multiply+shift,
  67. // and unroll loop
  68. for u >= 100 {
  69. i -= 2
  70. q := u / 100
  71. j := uintptr(u - q*100)
  72. a[i+1] = digits01[j]
  73. a[i+0] = digits10[j]
  74. u = q
  75. }
  76. if u >= 10 {
  77. i--
  78. q := u / 10
  79. a[i] = digits[uintptr(u-q*10)]
  80. u = q
  81. }
  82. } else if s := shifts[base]; s > 0 {
  83. // base is power of 2: use shifts and masks instead of / and %
  84. b := uint64(base)
  85. m := uintptr(b) - 1 // == 1<<s - 1
  86. for u >= b {
  87. i--
  88. a[i] = digits[uintptr(u)&m]
  89. u >>= s
  90. }
  91. } else {
  92. // general case
  93. b := uint64(base)
  94. for u >= b {
  95. i--
  96. a[i] = digits[uintptr(u%b)]
  97. u /= b
  98. }
  99. }
  100. // u < base
  101. i--
  102. a[i] = digits[uintptr(u)]
  103. // add sign, if any
  104. if neg {
  105. i--
  106. a[i] = '-'
  107. }
  108. if append_ {
  109. d = append(dst, a[i:]...)
  110. return
  111. }
  112. s = string(a[i:])
  113. return
  114. }