writer.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. /*
  6. // encWriter abstracts writing to a byte array or to an io.Writer.
  7. //
  8. //
  9. // Deprecated: Use encWriterSwitch instead.
  10. type encWriter interface {
  11. writeb([]byte)
  12. writestr(string)
  13. writeqstr(string) // write string wrapped in quotes ie "..."
  14. writen1(byte)
  15. writen2(byte, byte)
  16. end()
  17. }
  18. */
  19. // type ioEncWriterWriter interface {
  20. // WriteByte(c byte) error
  21. // WriteString(s string) (n int, err error)
  22. // Write(p []byte) (n int, err error)
  23. // }
  24. // ---------------------------------------------
  25. /*
  26. type ioEncStringWriter interface {
  27. WriteString(s string) (n int, err error)
  28. }
  29. // ioEncWriter implements encWriter and can write to an io.Writer implementation
  30. type ioEncWriter struct {
  31. w io.Writer
  32. ww io.Writer
  33. bw io.ByteWriter
  34. sw ioEncStringWriter
  35. fw ioFlusher
  36. b [8]byte
  37. }
  38. func (z *ioEncWriter) reset(w io.Writer) {
  39. z.w = w
  40. var ok bool
  41. if z.bw, ok = w.(io.ByteWriter); !ok {
  42. z.bw = z
  43. }
  44. if z.sw, ok = w.(ioEncStringWriter); !ok {
  45. z.sw = z
  46. }
  47. z.fw, _ = w.(ioFlusher)
  48. z.ww = w
  49. }
  50. func (z *ioEncWriter) WriteByte(b byte) (err error) {
  51. z.b[0] = b
  52. _, err = z.w.Write(z.b[:1])
  53. return
  54. }
  55. func (z *ioEncWriter) WriteString(s string) (n int, err error) {
  56. return z.w.Write(bytesView(s))
  57. }
  58. func (z *ioEncWriter) writeb(bs []byte) {
  59. if _, err := z.ww.Write(bs); err != nil {
  60. panic(err)
  61. }
  62. }
  63. func (z *ioEncWriter) writestr(s string) {
  64. if _, err := z.sw.WriteString(s); err != nil {
  65. panic(err)
  66. }
  67. }
  68. func (z *ioEncWriter) writeqstr(s string) {
  69. writestr("\"" + s + "\"")
  70. }
  71. func (z *ioEncWriter) writen1(b byte) {
  72. if err := z.bw.WriteByte(b); err != nil {
  73. panic(err)
  74. }
  75. }
  76. func (z *ioEncWriter) writen2(b1, b2 byte) {
  77. var err error
  78. if err = z.bw.WriteByte(b1); err == nil {
  79. if err = z.bw.WriteByte(b2); err == nil {
  80. return
  81. }
  82. }
  83. panic(err)
  84. }
  85. // func (z *ioEncWriter) writen5(b1, b2, b3, b4, b5 byte) {
  86. // z.b[0], z.b[1], z.b[2], z.b[3], z.b[4] = b1, b2, b3, b4, b5
  87. // if _, err := z.ww.Write(z.b[:5]); err != nil {
  88. // panic(err)
  89. // }
  90. // }
  91. //go:noinline - so *encWriterSwitch.XXX has the bytesEncAppender.XXX inlined
  92. func (z *ioEncWriter) end() {
  93. if z.fw != nil {
  94. if err := z.fw.Flush(); err != nil {
  95. panic(err)
  96. }
  97. }
  98. }
  99. */
  100. // ---------------------------------------------
  101. // bufioEncWriter
  102. type bufioEncWriter struct {
  103. w io.Writer
  104. buf []byte
  105. n int
  106. // Extensions can call Encode() within a current Encode() call.
  107. // We need to know when the top level Encode() call returns,
  108. // so we can decide whether to Release() or not.
  109. calls uint16 // what depth in mustDecode are we in now.
  110. sz int // buf size
  111. // _ uint64 // padding (cache-aligned)
  112. // ---- cache line
  113. // write-most fields below
  114. // less used fields
  115. bytesBufPooler
  116. b [40]byte // scratch buffer and padding (cache-aligned)
  117. // a int
  118. // b [4]byte
  119. // err
  120. }
  121. func (z *bufioEncWriter) reset(w io.Writer, bufsize int) {
  122. z.w = w
  123. z.n = 0
  124. z.calls = 0
  125. if bufsize <= 0 {
  126. bufsize = defEncByteBufSize
  127. }
  128. z.sz = bufsize
  129. if cap(z.buf) >= bufsize {
  130. z.buf = z.buf[:cap(z.buf)]
  131. } else if bufsize <= len(z.b) {
  132. z.buf = z.b[:]
  133. } else {
  134. z.buf = z.bytesBufPooler.get(bufsize)
  135. z.buf = z.buf[:cap(z.buf)]
  136. // z.buf = make([]byte, bufsize)
  137. }
  138. }
  139. func (z *bufioEncWriter) release() {
  140. z.buf = nil
  141. z.bytesBufPooler.end()
  142. }
  143. //go:noinline - flush only called intermittently
  144. func (z *bufioEncWriter) flushErr() (err error) {
  145. n, err := z.w.Write(z.buf[:z.n])
  146. z.n -= n
  147. if z.n > 0 && err == nil {
  148. err = io.ErrShortWrite
  149. }
  150. if n > 0 && z.n > 0 {
  151. copy(z.buf, z.buf[n:z.n+n])
  152. }
  153. return err
  154. }
  155. func (z *bufioEncWriter) flush() {
  156. if err := z.flushErr(); err != nil {
  157. panic(err)
  158. }
  159. }
  160. func (z *bufioEncWriter) writeb(s []byte) {
  161. LOOP:
  162. a := len(z.buf) - z.n
  163. if len(s) > a {
  164. z.n += copy(z.buf[z.n:], s[:a])
  165. s = s[a:]
  166. z.flush()
  167. goto LOOP
  168. }
  169. z.n += copy(z.buf[z.n:], s)
  170. }
  171. func (z *bufioEncWriter) writestr(s string) {
  172. // z.writeb(bytesView(s)) // inlined below
  173. LOOP:
  174. a := len(z.buf) - z.n
  175. if len(s) > a {
  176. z.n += copy(z.buf[z.n:], s[:a])
  177. s = s[a:]
  178. z.flush()
  179. goto LOOP
  180. }
  181. z.n += copy(z.buf[z.n:], s)
  182. }
  183. func (z *bufioEncWriter) writeqstr(s string) {
  184. // z.writen1('"')
  185. // z.writestr(s)
  186. // z.writen1('"')
  187. if z.n+len(s)+2 > len(z.buf) {
  188. z.flush()
  189. }
  190. z.buf[z.n] = '"'
  191. z.n++
  192. LOOP:
  193. a := len(z.buf) - z.n
  194. if len(s)+1 > a {
  195. z.n += copy(z.buf[z.n:], s[:a])
  196. s = s[a:]
  197. z.flush()
  198. goto LOOP
  199. }
  200. z.n += copy(z.buf[z.n:], s)
  201. z.buf[z.n] = '"'
  202. z.n++
  203. }
  204. func (z *bufioEncWriter) writen1(b1 byte) {
  205. if 1 > len(z.buf)-z.n {
  206. z.flush()
  207. }
  208. z.buf[z.n] = b1
  209. z.n++
  210. }
  211. func (z *bufioEncWriter) writen2(b1, b2 byte) {
  212. if 2 > len(z.buf)-z.n {
  213. z.flush()
  214. }
  215. z.buf[z.n+1] = b2
  216. z.buf[z.n] = b1
  217. z.n += 2
  218. }
  219. func (z *bufioEncWriter) endErr() (err error) {
  220. if z.n > 0 {
  221. err = z.flushErr()
  222. }
  223. return
  224. }
  225. // ---------------------------------------------
  226. // bytesEncAppender implements encWriter and can write to an byte slice.
  227. type bytesEncAppender struct {
  228. b []byte
  229. out *[]byte
  230. }
  231. func (z *bytesEncAppender) writeb(s []byte) {
  232. z.b = append(z.b, s...)
  233. }
  234. func (z *bytesEncAppender) writestr(s string) {
  235. z.b = append(z.b, s...)
  236. }
  237. func (z *bytesEncAppender) writeqstr(s string) {
  238. // z.writen1('"')
  239. // z.writestr(s)
  240. // z.writen1('"')
  241. z.b = append(append(append(z.b, '"'), s...), '"')
  242. // z.b = append(z.b, '"')
  243. // z.b = append(z.b, s...)
  244. // z.b = append(z.b, '"')
  245. }
  246. func (z *bytesEncAppender) writen1(b1 byte) {
  247. z.b = append(z.b, b1)
  248. }
  249. func (z *bytesEncAppender) writen2(b1, b2 byte) {
  250. z.b = append(z.b, b1, b2)
  251. }
  252. func (z *bytesEncAppender) endErr() error {
  253. *(z.out) = z.b
  254. return nil
  255. }
  256. func (z *bytesEncAppender) reset(in []byte, out *[]byte) {
  257. z.b = in[:0]
  258. z.out = out
  259. }
  260. // --------------------------------------------------
  261. type encWriterSwitch struct {
  262. esep bool // whether it has elem separators
  263. bytes bool // encoding to []byte
  264. isas bool // whether e.as != nil
  265. js bool // is json encoder?
  266. be bool // is binary encoder?
  267. c containerState
  268. // _ [3]byte // padding
  269. // _ [2]uint64 // padding
  270. // _ uint64 // padding
  271. // wi *ioEncWriter
  272. wb bytesEncAppender
  273. wf *bufioEncWriter
  274. // typ entryType
  275. }
  276. func (z *encWriterSwitch) writeb(s []byte) {
  277. if z.bytes {
  278. z.wb.writeb(s)
  279. } else {
  280. z.wf.writeb(s)
  281. }
  282. }
  283. func (z *encWriterSwitch) writeqstr(s string) {
  284. if z.bytes {
  285. z.wb.writeqstr(s)
  286. } else {
  287. z.wf.writeqstr(s)
  288. }
  289. }
  290. func (z *encWriterSwitch) writestr(s string) {
  291. if z.bytes {
  292. z.wb.writestr(s)
  293. } else {
  294. z.wf.writestr(s)
  295. }
  296. }
  297. func (z *encWriterSwitch) writen1(b1 byte) {
  298. if z.bytes {
  299. z.wb.writen1(b1)
  300. } else {
  301. z.wf.writen1(b1)
  302. }
  303. }
  304. func (z *encWriterSwitch) writen2(b1, b2 byte) {
  305. if z.bytes {
  306. z.wb.writen2(b1, b2)
  307. } else {
  308. z.wf.writen2(b1, b2)
  309. }
  310. }
  311. func (z *encWriterSwitch) endErr() error {
  312. if z.bytes {
  313. return z.wb.endErr()
  314. }
  315. return z.wf.endErr()
  316. }
  317. func (z *encWriterSwitch) end() {
  318. if err := z.endErr(); err != nil {
  319. panic(err)
  320. }
  321. }
  322. /*
  323. // ------------------------------------------
  324. func (z *encWriterSwitch) writeb(s []byte) {
  325. switch z.typ {
  326. case entryTypeBytes:
  327. z.wb.writeb(s)
  328. case entryTypeIo:
  329. z.wi.writeb(s)
  330. default:
  331. z.wf.writeb(s)
  332. }
  333. }
  334. func (z *encWriterSwitch) writestr(s string) {
  335. switch z.typ {
  336. case entryTypeBytes:
  337. z.wb.writestr(s)
  338. case entryTypeIo:
  339. z.wi.writestr(s)
  340. default:
  341. z.wf.writestr(s)
  342. }
  343. }
  344. func (z *encWriterSwitch) writen1(b1 byte) {
  345. switch z.typ {
  346. case entryTypeBytes:
  347. z.wb.writen1(b1)
  348. case entryTypeIo:
  349. z.wi.writen1(b1)
  350. default:
  351. z.wf.writen1(b1)
  352. }
  353. }
  354. func (z *encWriterSwitch) writen2(b1, b2 byte) {
  355. switch z.typ {
  356. case entryTypeBytes:
  357. z.wb.writen2(b1, b2)
  358. case entryTypeIo:
  359. z.wi.writen2(b1, b2)
  360. default:
  361. z.wf.writen2(b1, b2)
  362. }
  363. }
  364. func (z *encWriterSwitch) end() {
  365. switch z.typ {
  366. case entryTypeBytes:
  367. z.wb.end()
  368. case entryTypeIo:
  369. z.wi.end()
  370. default:
  371. z.wf.end()
  372. }
  373. }
  374. // ------------------------------------------
  375. func (z *encWriterSwitch) writeb(s []byte) {
  376. if z.bytes {
  377. z.wb.writeb(s)
  378. } else {
  379. z.wi.writeb(s)
  380. }
  381. }
  382. func (z *encWriterSwitch) writestr(s string) {
  383. if z.bytes {
  384. z.wb.writestr(s)
  385. } else {
  386. z.wi.writestr(s)
  387. }
  388. }
  389. func (z *encWriterSwitch) writen1(b1 byte) {
  390. if z.bytes {
  391. z.wb.writen1(b1)
  392. } else {
  393. z.wi.writen1(b1)
  394. }
  395. }
  396. func (z *encWriterSwitch) writen2(b1, b2 byte) {
  397. if z.bytes {
  398. z.wb.writen2(b1, b2)
  399. } else {
  400. z.wi.writen2(b1, b2)
  401. }
  402. }
  403. func (z *encWriterSwitch) end() {
  404. if z.bytes {
  405. z.wb.end()
  406. } else {
  407. z.wi.end()
  408. }
  409. }
  410. */