feature_stream.go 6.2 KB

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