另客网go项目公用的代码库

stream_int.go 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package jsoniter
  2. var digits []uint32
  3. func init() {
  4. digits = make([]uint32, 1000)
  5. for i := uint32(0); i < 1000; i++ {
  6. digits[i] = (((i / 100) + '0') << 16) + ((((i / 10) % 10) + '0') << 8) + i%10 + '0'
  7. if i < 10 {
  8. digits[i] += 2 << 24
  9. } else if i < 100 {
  10. digits[i] += 1 << 24
  11. }
  12. }
  13. }
  14. func writeFirstBuf(space []byte, v uint32) []byte {
  15. start := v >> 24
  16. if start == 0 {
  17. space = append(space, byte(v>>16), byte(v>>8))
  18. } else if start == 1 {
  19. space = append(space, byte(v>>8))
  20. }
  21. space = append(space, byte(v))
  22. return space
  23. }
  24. func writeBuf(buf []byte, v uint32) []byte {
  25. return append(buf, byte(v>>16), byte(v>>8), byte(v))
  26. }
  27. // WriteUint8 write uint8 to stream
  28. func (stream *Stream) WriteUint8(val uint8) {
  29. stream.buf = writeFirstBuf(stream.buf, digits[val])
  30. }
  31. // WriteInt8 write int8 to stream
  32. func (stream *Stream) WriteInt8(nval int8) {
  33. var val uint8
  34. if nval < 0 {
  35. val = uint8(-nval)
  36. stream.buf = append(stream.buf, '-')
  37. } else {
  38. val = uint8(nval)
  39. }
  40. stream.buf = writeFirstBuf(stream.buf, digits[val])
  41. }
  42. // WriteUint16 write uint16 to stream
  43. func (stream *Stream) WriteUint16(val uint16) {
  44. q1 := val / 1000
  45. if q1 == 0 {
  46. stream.buf = writeFirstBuf(stream.buf, digits[val])
  47. return
  48. }
  49. r1 := val - q1*1000
  50. stream.buf = writeFirstBuf(stream.buf, digits[q1])
  51. stream.buf = writeBuf(stream.buf, digits[r1])
  52. return
  53. }
  54. // WriteInt16 write int16 to stream
  55. func (stream *Stream) WriteInt16(nval int16) {
  56. var val uint16
  57. if nval < 0 {
  58. val = uint16(-nval)
  59. stream.buf = append(stream.buf, '-')
  60. } else {
  61. val = uint16(nval)
  62. }
  63. stream.WriteUint16(val)
  64. }
  65. // WriteUint32 write uint32 to stream
  66. func (stream *Stream) WriteUint32(val uint32) {
  67. q1 := val / 1000
  68. if q1 == 0 {
  69. stream.buf = writeFirstBuf(stream.buf, digits[val])
  70. return
  71. }
  72. r1 := val - q1*1000
  73. q2 := q1 / 1000
  74. if q2 == 0 {
  75. stream.buf = writeFirstBuf(stream.buf, digits[q1])
  76. stream.buf = writeBuf(stream.buf, digits[r1])
  77. return
  78. }
  79. r2 := q1 - q2*1000
  80. q3 := q2 / 1000
  81. if q3 == 0 {
  82. stream.buf = writeFirstBuf(stream.buf, digits[q2])
  83. } else {
  84. r3 := q2 - q3*1000
  85. stream.buf = append(stream.buf, byte(q3+'0'))
  86. stream.buf = writeBuf(stream.buf, digits[r3])
  87. }
  88. stream.buf = writeBuf(stream.buf, digits[r2])
  89. stream.buf = writeBuf(stream.buf, digits[r1])
  90. }
  91. // WriteInt32 write int32 to stream
  92. func (stream *Stream) WriteInt32(nval int32) {
  93. var val uint32
  94. if nval < 0 {
  95. val = uint32(-nval)
  96. stream.buf = append(stream.buf, '-')
  97. } else {
  98. val = uint32(nval)
  99. }
  100. stream.WriteUint32(val)
  101. }
  102. // WriteUint64 write uint64 to stream
  103. func (stream *Stream) WriteUint64(val uint64) {
  104. q1 := val / 1000
  105. if q1 == 0 {
  106. stream.buf = writeFirstBuf(stream.buf, digits[val])
  107. return
  108. }
  109. r1 := val - q1*1000
  110. q2 := q1 / 1000
  111. if q2 == 0 {
  112. stream.buf = writeFirstBuf(stream.buf, digits[q1])
  113. stream.buf = writeBuf(stream.buf, digits[r1])
  114. return
  115. }
  116. r2 := q1 - q2*1000
  117. q3 := q2 / 1000
  118. if q3 == 0 {
  119. stream.buf = writeFirstBuf(stream.buf, digits[q2])
  120. stream.buf = writeBuf(stream.buf, digits[r2])
  121. stream.buf = writeBuf(stream.buf, digits[r1])
  122. return
  123. }
  124. r3 := q2 - q3*1000
  125. q4 := q3 / 1000
  126. if q4 == 0 {
  127. stream.buf = writeFirstBuf(stream.buf, digits[q3])
  128. stream.buf = writeBuf(stream.buf, digits[r3])
  129. stream.buf = writeBuf(stream.buf, digits[r2])
  130. stream.buf = writeBuf(stream.buf, digits[r1])
  131. return
  132. }
  133. r4 := q3 - q4*1000
  134. q5 := q4 / 1000
  135. if q5 == 0 {
  136. stream.buf = writeFirstBuf(stream.buf, digits[q4])
  137. stream.buf = writeBuf(stream.buf, digits[r4])
  138. stream.buf = writeBuf(stream.buf, digits[r3])
  139. stream.buf = writeBuf(stream.buf, digits[r2])
  140. stream.buf = writeBuf(stream.buf, digits[r1])
  141. return
  142. }
  143. r5 := q4 - q5*1000
  144. q6 := q5 / 1000
  145. if q6 == 0 {
  146. stream.buf = writeFirstBuf(stream.buf, digits[q5])
  147. } else {
  148. stream.buf = writeFirstBuf(stream.buf, digits[q6])
  149. r6 := q5 - q6*1000
  150. stream.buf = writeBuf(stream.buf, digits[r6])
  151. }
  152. stream.buf = writeBuf(stream.buf, digits[r5])
  153. stream.buf = writeBuf(stream.buf, digits[r4])
  154. stream.buf = writeBuf(stream.buf, digits[r3])
  155. stream.buf = writeBuf(stream.buf, digits[r2])
  156. stream.buf = writeBuf(stream.buf, digits[r1])
  157. }
  158. // WriteInt64 write int64 to stream
  159. func (stream *Stream) WriteInt64(nval int64) {
  160. var val uint64
  161. if nval < 0 {
  162. val = uint64(-nval)
  163. stream.buf = append(stream.buf, '-')
  164. } else {
  165. val = uint64(nval)
  166. }
  167. stream.WriteUint64(val)
  168. }
  169. // WriteInt write int to stream
  170. func (stream *Stream) WriteInt(val int) {
  171. stream.WriteInt64(int64(val))
  172. }
  173. // WriteUint write uint to stream
  174. func (stream *Stream) WriteUint(val uint) {
  175. stream.WriteUint64(uint64(val))
  176. }