feature_stream.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package jsoniter
  2. import (
  3. "io"
  4. )
  5. type Stream struct {
  6. out io.Writer
  7. buf []byte
  8. n int
  9. Error error
  10. indention int
  11. IndentionStep int
  12. }
  13. func NewStream(out io.Writer, bufSize int) *Stream {
  14. return &Stream{out, make([]byte, bufSize), 0, nil, 0, 0}
  15. }
  16. func (b *Stream) Reset(out io.Writer) {
  17. b.out = out
  18. b.n = 0
  19. }
  20. // Available returns how many bytes are unused in the buffer.
  21. func (b *Stream) Available() int {
  22. return len(b.buf) - b.n
  23. }
  24. // Buffered returns the number of bytes that have been written into the current buffer.
  25. func (b *Stream) Buffered() int {
  26. return b.n
  27. }
  28. func (b *Stream) Buffer() []byte {
  29. return b.buf[:b.n]
  30. }
  31. // Write writes the contents of p into the buffer.
  32. // It returns the number of bytes written.
  33. // If nn < len(p), it also returns an error explaining
  34. // why the write is short.
  35. func (b *Stream) Write(p []byte) (nn int, err error) {
  36. for len(p) > b.Available() && b.Error == nil {
  37. if b.out == nil {
  38. b.growAtLeast(len(p))
  39. } else {
  40. var n int
  41. if b.Buffered() == 0 {
  42. // Large write, empty buffer.
  43. // Write directly from p to avoid copy.
  44. n, b.Error = b.out.Write(p)
  45. } else {
  46. n = copy(b.buf[b.n:], p)
  47. b.n += n
  48. b.Flush()
  49. }
  50. nn += n
  51. p = p[n:]
  52. }
  53. }
  54. if b.Error != nil {
  55. return nn, b.Error
  56. }
  57. n := copy(b.buf[b.n:], p)
  58. b.n += n
  59. nn += n
  60. return nn, nil
  61. }
  62. // WriteByte writes a single byte.
  63. func (b *Stream) writeByte(c byte) {
  64. if b.Error != nil {
  65. return
  66. }
  67. if b.Available() < 1 {
  68. b.growAtLeast(1)
  69. }
  70. b.buf[b.n] = c
  71. b.n++
  72. }
  73. func (b *Stream) writeTwoBytes(c1 byte, c2 byte) {
  74. if b.Error != nil {
  75. return
  76. }
  77. if b.Available() < 2 {
  78. b.growAtLeast(2)
  79. }
  80. b.buf[b.n] = c1
  81. b.buf[b.n+1] = c2
  82. b.n += 2
  83. }
  84. func (b *Stream) writeThreeBytes(c1 byte, c2 byte, c3 byte) {
  85. if b.Error != nil {
  86. return
  87. }
  88. if b.Available() < 3 {
  89. b.growAtLeast(3)
  90. }
  91. b.buf[b.n] = c1
  92. b.buf[b.n+1] = c2
  93. b.buf[b.n+2] = c3
  94. b.n += 3
  95. }
  96. func (b *Stream) writeFourBytes(c1 byte, c2 byte, c3 byte, c4 byte) {
  97. if b.Error != nil {
  98. return
  99. }
  100. if b.Available() < 4 {
  101. b.growAtLeast(4)
  102. }
  103. b.buf[b.n] = c1
  104. b.buf[b.n+1] = c2
  105. b.buf[b.n+2] = c3
  106. b.buf[b.n+3] = c4
  107. b.n += 4
  108. }
  109. func (b *Stream) writeFiveBytes(c1 byte, c2 byte, c3 byte, c4 byte, c5 byte) {
  110. if b.Error != nil {
  111. return
  112. }
  113. if b.Available() < 5 {
  114. b.growAtLeast(5)
  115. }
  116. b.buf[b.n] = c1
  117. b.buf[b.n+1] = c2
  118. b.buf[b.n+2] = c3
  119. b.buf[b.n+3] = c4
  120. b.buf[b.n+4] = c5
  121. b.n += 5
  122. }
  123. // Flush writes any buffered data to the underlying io.Writer.
  124. func (b *Stream) Flush() error {
  125. if b.out == nil {
  126. return nil
  127. }
  128. if b.Error != nil {
  129. return b.Error
  130. }
  131. if b.n == 0 {
  132. return nil
  133. }
  134. n, err := b.out.Write(b.buf[0:b.n])
  135. if n < b.n && err == nil {
  136. err = io.ErrShortWrite
  137. }
  138. if err != nil {
  139. if n > 0 && n < b.n {
  140. copy(b.buf[0:b.n-n], b.buf[n:b.n])
  141. }
  142. b.n -= n
  143. b.Error = err
  144. return err
  145. }
  146. b.n = 0
  147. return nil
  148. }
  149. func (b *Stream) ensure(minimal int) {
  150. available := b.Available()
  151. if available < minimal {
  152. if b.n > 1024 {
  153. b.Flush()
  154. }
  155. b.growAtLeast(minimal)
  156. }
  157. }
  158. func (b *Stream) growAtLeast(minimal int) {
  159. toGrow := len(b.buf)
  160. if toGrow < minimal {
  161. toGrow = minimal
  162. }
  163. newBuf := make([]byte, len(b.buf)+toGrow)
  164. copy(newBuf, b.Buffer())
  165. b.buf = newBuf
  166. }
  167. func (b *Stream) WriteRaw(s string) {
  168. b.ensure(len(s))
  169. if b.Error != nil {
  170. return
  171. }
  172. n := copy(b.buf[b.n:], s)
  173. b.n += n
  174. }
  175. func (stream *Stream) WriteString(s string) {
  176. stream.ensure(32)
  177. valLen := len(s)
  178. toWriteLen := valLen
  179. bufLengthMinusTwo := len(stream.buf) - 2 // make room for the quotes
  180. if stream.n+toWriteLen > bufLengthMinusTwo {
  181. toWriteLen = bufLengthMinusTwo - stream.n
  182. }
  183. n := stream.n
  184. stream.buf[n] = '"'
  185. n++
  186. // write string, the fast path, without utf8 and escape support
  187. i := 0
  188. for ; i < toWriteLen; i++ {
  189. c := s[i]
  190. if c > 31 && c != '"' && c != '\\' {
  191. stream.buf[n] = c
  192. n++
  193. } else {
  194. break
  195. }
  196. }
  197. if i == valLen {
  198. stream.buf[n] = '"'
  199. n++
  200. stream.n = n
  201. return
  202. }
  203. stream.n = n
  204. // for the remaining parts, we process them char by char
  205. stream.writeStringSlowPath(s, i, valLen)
  206. stream.writeByte('"')
  207. }
  208. func (stream *Stream) writeStringSlowPath(s string, i int, valLen int) {
  209. for ; i < valLen; i++ {
  210. c := s[i]
  211. switch c {
  212. case '"':
  213. stream.writeTwoBytes('\\', '"')
  214. case '\\':
  215. stream.writeTwoBytes('\\', '\\')
  216. case '\b':
  217. stream.writeTwoBytes('\\', 'b')
  218. case '\f':
  219. stream.writeTwoBytes('\\', 'f')
  220. case '\n':
  221. stream.writeTwoBytes('\\', 'n')
  222. case '\r':
  223. stream.writeTwoBytes('\\', 'r')
  224. case '\t':
  225. stream.writeTwoBytes('\\', 't')
  226. default:
  227. stream.writeByte(c)
  228. }
  229. }
  230. }
  231. func (stream *Stream) WriteNil() {
  232. stream.writeFourBytes('n', 'u', 'l', 'l')
  233. }
  234. func (stream *Stream) WriteTrue() {
  235. stream.writeFourBytes('t', 'r', 'u', 'e')
  236. }
  237. func (stream *Stream) WriteFalse() {
  238. stream.writeFiveBytes('f', 'a', 'l', 's', 'e')
  239. }
  240. func (stream *Stream) WriteBool(val bool) {
  241. if val {
  242. stream.WriteTrue()
  243. } else {
  244. stream.WriteFalse()
  245. }
  246. }
  247. func (stream *Stream) WriteObjectStart() {
  248. stream.indention += stream.IndentionStep
  249. stream.writeByte('{')
  250. stream.writeIndention(0)
  251. }
  252. func (stream *Stream) WriteObjectField(field string) {
  253. stream.WriteString(field)
  254. stream.writeByte(':')
  255. }
  256. func (stream *Stream) WriteObjectEnd() {
  257. stream.writeIndention(stream.IndentionStep)
  258. stream.indention -= stream.IndentionStep
  259. stream.writeByte('}')
  260. }
  261. func (stream *Stream) WriteEmptyObject() {
  262. stream.writeByte('{')
  263. stream.writeByte('}')
  264. }
  265. func (stream *Stream) WriteMore() {
  266. stream.writeByte(',')
  267. stream.writeIndention(0)
  268. }
  269. func (stream *Stream) WriteArrayStart() {
  270. stream.indention += stream.IndentionStep
  271. stream.writeByte('[')
  272. stream.writeIndention(0)
  273. }
  274. func (stream *Stream) WriteEmptyArray() {
  275. stream.writeByte('[')
  276. stream.writeByte(']')
  277. }
  278. func (stream *Stream) WriteArrayEnd() {
  279. stream.writeIndention(stream.IndentionStep)
  280. stream.indention -= stream.IndentionStep
  281. stream.writeByte(']')
  282. }
  283. func (stream *Stream) writeIndention(delta int) {
  284. if stream.indention == 0 {
  285. return
  286. }
  287. stream.writeByte('\n')
  288. toWrite := stream.indention - delta
  289. stream.ensure(toWrite)
  290. for i := 0; i < toWrite && stream.n < len(stream.buf); i++ {
  291. stream.buf[stream.n] = ' '
  292. stream.n++
  293. }
  294. }