feature_stream.go 5.9 KB

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