feature_stream.go 5.9 KB

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