feature_stream.go 6.2 KB

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