writer.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. // Copyright (c) 2012-2018 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. package codec
  4. import "io"
  5. // encWriter abstracts writing to a byte array or to an io.Writer.
  6. type encWriter interface {
  7. writeb([]byte)
  8. writestr(string)
  9. writeqstr(string) // write string wrapped in quotes ie "..."
  10. writen1(byte)
  11. writen2(byte, byte)
  12. // writen will write up to 7 bytes at a time.
  13. writen(b [rwNLen]byte, num uint8)
  14. end()
  15. }
  16. // ---------------------------------------------
  17. // bufioEncWriter
  18. type bufioEncWriter struct {
  19. w io.Writer
  20. buf []byte
  21. n int
  22. b [16]byte // scratch buffer and padding (cache-aligned)
  23. }
  24. func (z *bufioEncWriter) reset(w io.Writer, bufsize int, blist *bytesFreelist) {
  25. z.w = w
  26. z.n = 0
  27. if bufsize <= 0 {
  28. bufsize = defEncByteBufSize
  29. }
  30. // bufsize must be >= 8, to accomodate writen methods (where n <= 8)
  31. if bufsize <= 8 {
  32. bufsize = 8
  33. }
  34. if cap(z.buf) < bufsize {
  35. if len(z.buf) > 0 && &z.buf[0] != &z.b[0] {
  36. blist.put(z.buf)
  37. }
  38. if len(z.b) > bufsize {
  39. z.buf = z.b[:]
  40. } else {
  41. z.buf = blist.get(bufsize)
  42. }
  43. }
  44. z.buf = z.buf[:cap(z.buf)]
  45. }
  46. //go:noinline - flush only called intermittently
  47. func (z *bufioEncWriter) flushErr() (err error) {
  48. n, err := z.w.Write(z.buf[:z.n])
  49. z.n -= n
  50. if z.n > 0 && err == nil {
  51. err = io.ErrShortWrite
  52. }
  53. if n > 0 && z.n > 0 {
  54. copy(z.buf, z.buf[n:z.n+n])
  55. }
  56. return err
  57. }
  58. func (z *bufioEncWriter) flush() {
  59. if err := z.flushErr(); err != nil {
  60. panic(err)
  61. }
  62. }
  63. func (z *bufioEncWriter) writeb(s []byte) {
  64. LOOP:
  65. a := len(z.buf) - z.n
  66. if len(s) > a {
  67. z.n += copy(z.buf[z.n:], s[:a])
  68. s = s[a:]
  69. z.flush()
  70. goto LOOP
  71. }
  72. z.n += copy(z.buf[z.n:], s)
  73. }
  74. func (z *bufioEncWriter) writestr(s string) {
  75. // z.writeb(bytesView(s)) // inlined below
  76. LOOP:
  77. a := len(z.buf) - z.n
  78. if len(s) > a {
  79. z.n += copy(z.buf[z.n:], s[:a])
  80. s = s[a:]
  81. z.flush()
  82. goto LOOP
  83. }
  84. z.n += copy(z.buf[z.n:], s)
  85. }
  86. func (z *bufioEncWriter) writeqstr(s string) {
  87. // z.writen1('"')
  88. // z.writestr(s)
  89. // z.writen1('"')
  90. if z.n+len(s)+2 > len(z.buf) {
  91. z.flush()
  92. }
  93. z.buf[z.n] = '"'
  94. z.n++
  95. LOOP:
  96. a := len(z.buf) - z.n
  97. if len(s)+1 > a {
  98. z.n += copy(z.buf[z.n:], s[:a])
  99. s = s[a:]
  100. z.flush()
  101. goto LOOP
  102. }
  103. z.n += copy(z.buf[z.n:], s)
  104. z.buf[z.n] = '"'
  105. z.n++
  106. }
  107. func (z *bufioEncWriter) writen1(b1 byte) {
  108. if 1 > len(z.buf)-z.n {
  109. z.flush()
  110. }
  111. z.buf[z.n] = b1
  112. z.n++
  113. }
  114. func (z *bufioEncWriter) writen2(b1, b2 byte) {
  115. if 2 > len(z.buf)-z.n {
  116. z.flush()
  117. }
  118. z.buf[z.n+1] = b2
  119. z.buf[z.n] = b1
  120. z.n += 2
  121. }
  122. func (z *bufioEncWriter) writen(b [rwNLen]byte, num uint8) {
  123. if int(num) > len(z.buf)-z.n {
  124. z.flush()
  125. }
  126. copy(z.buf[z.n:], b[:num])
  127. z.n += int(num)
  128. }
  129. func (z *bufioEncWriter) endErr() (err error) {
  130. if z.n > 0 {
  131. err = z.flushErr()
  132. }
  133. return
  134. }
  135. // ---------------------------------------------
  136. // bytesEncAppender implements encWriter and can write to an byte slice.
  137. type bytesEncAppender struct {
  138. b []byte
  139. out *[]byte
  140. }
  141. func (z *bytesEncAppender) writeb(s []byte) {
  142. z.b = append(z.b, s...)
  143. }
  144. func (z *bytesEncAppender) writestr(s string) {
  145. z.b = append(z.b, s...)
  146. }
  147. func (z *bytesEncAppender) writeqstr(s string) {
  148. z.b = append(append(append(z.b, '"'), s...), '"')
  149. // z.b = append(z.b, '"')
  150. // z.b = append(z.b, s...)
  151. // z.b = append(z.b, '"')
  152. }
  153. func (z *bytesEncAppender) writen1(b1 byte) {
  154. z.b = append(z.b, b1)
  155. }
  156. func (z *bytesEncAppender) writen2(b1, b2 byte) {
  157. z.b = append(z.b, b1, b2) // cost: 81
  158. }
  159. func (z *bytesEncAppender) writen(s [rwNLen]byte, num uint8) {
  160. // if num <= rwNLen {
  161. if int(num) <= len(s) {
  162. z.b = append(z.b, s[:num]...)
  163. }
  164. }
  165. func (z *bytesEncAppender) endErr() error {
  166. *(z.out) = z.b
  167. return nil
  168. }
  169. func (z *bytesEncAppender) reset(in []byte, out *[]byte) {
  170. z.b = in[:0]
  171. z.out = out
  172. }
  173. // --------------------------------------------------
  174. type encWr struct {
  175. bytes bool // encoding to []byte
  176. js bool // is json encoder?
  177. be bool // is binary encoder?
  178. c containerState
  179. calls uint16
  180. wb bytesEncAppender
  181. wf *bufioEncWriter
  182. }
  183. func (z *encWr) writeb(s []byte) {
  184. if z.bytes {
  185. z.wb.writeb(s)
  186. } else {
  187. z.wf.writeb(s)
  188. }
  189. }
  190. func (z *encWr) writeqstr(s string) {
  191. if z.bytes {
  192. // unfortunately, calling the function prevents inlining it here.
  193. // explicitly writing it here will allow it inline.
  194. // NOTE: Keep in sync with function implementation.
  195. //
  196. // z.wb.writeqstr(s)
  197. z.wb.b = append(append(append(z.wb.b, '"'), s...), '"')
  198. } else {
  199. z.wf.writeqstr(s)
  200. }
  201. }
  202. func (z *encWr) writestr(s string) {
  203. if z.bytes {
  204. z.wb.writestr(s)
  205. } else {
  206. z.wf.writestr(s)
  207. }
  208. }
  209. func (z *encWr) writen1(b1 byte) {
  210. if z.bytes {
  211. z.wb.writen1(b1)
  212. } else {
  213. z.wf.writen1(b1)
  214. }
  215. }
  216. func (z *encWr) writen2(b1, b2 byte) {
  217. if z.bytes {
  218. // unfortunately, calling the function prevents inlining it here.
  219. // explicitly writing it here will allow it inline.
  220. // NOTE: Keep in sync with function implementation.
  221. //
  222. // z.wb.writen2(b1, b2)
  223. z.wb.b = append(z.wb.b, b1, b2)
  224. } else {
  225. z.wf.writen2(b1, b2)
  226. }
  227. }
  228. func (z *encWr) writen(b [rwNLen]byte, num uint8) {
  229. if z.bytes {
  230. z.wb.writen(b, num)
  231. } else {
  232. z.wf.writen(b, num)
  233. }
  234. }
  235. func (z *encWr) endErr() error {
  236. if z.bytes {
  237. return z.wb.endErr()
  238. }
  239. return z.wf.endErr()
  240. }
  241. func (z *encWr) end() {
  242. if err := z.endErr(); err != nil {
  243. panic(err)
  244. }
  245. }
  246. var _ encWriter = (*encWr)(nil)