feature_stream.go 5.3 KB

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