feature_stream.go 6.2 KB

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