writer.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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 = make([]byte, bufsize)
  136. }
  137. }
  138. func (z *bufioEncWriter) release() {
  139. z.buf = nil
  140. z.bytesBufPooler.end()
  141. }
  142. //go:noinline - flush only called intermittently
  143. func (z *bufioEncWriter) flushErr() (err error) {
  144. n, err := z.w.Write(z.buf[:z.n])
  145. z.n -= n
  146. if z.n > 0 && err == nil {
  147. err = io.ErrShortWrite
  148. }
  149. if n > 0 && z.n > 0 {
  150. copy(z.buf, z.buf[n:z.n+n])
  151. }
  152. return err
  153. }
  154. func (z *bufioEncWriter) flush() {
  155. if err := z.flushErr(); err != nil {
  156. panic(err)
  157. }
  158. }
  159. func (z *bufioEncWriter) writeb(s []byte) {
  160. LOOP:
  161. a := len(z.buf) - z.n
  162. if len(s) > a {
  163. z.n += copy(z.buf[z.n:], s[:a])
  164. s = s[a:]
  165. z.flush()
  166. goto LOOP
  167. }
  168. z.n += copy(z.buf[z.n:], s)
  169. }
  170. func (z *bufioEncWriter) writestr(s string) {
  171. // z.writeb(bytesView(s)) // inlined below
  172. LOOP:
  173. a := len(z.buf) - z.n
  174. if len(s) > a {
  175. z.n += copy(z.buf[z.n:], s[:a])
  176. s = s[a:]
  177. z.flush()
  178. goto LOOP
  179. }
  180. z.n += copy(z.buf[z.n:], s)
  181. }
  182. func (z *bufioEncWriter) writeqstr(s string) {
  183. // z.writen1('"')
  184. // z.writestr(s)
  185. // z.writen1('"')
  186. if z.n+len(s)+2 > len(z.buf) {
  187. z.flush()
  188. }
  189. z.buf[z.n] = '"'
  190. z.n++
  191. LOOP:
  192. a := len(z.buf) - z.n
  193. if len(s)+1 > a {
  194. z.n += copy(z.buf[z.n:], s[:a])
  195. s = s[a:]
  196. z.flush()
  197. goto LOOP
  198. }
  199. z.n += copy(z.buf[z.n:], s)
  200. z.buf[z.n] = '"'
  201. z.n++
  202. }
  203. func (z *bufioEncWriter) writen1(b1 byte) {
  204. if 1 > len(z.buf)-z.n {
  205. z.flush()
  206. }
  207. z.buf[z.n] = b1
  208. z.n++
  209. }
  210. func (z *bufioEncWriter) writen2(b1, b2 byte) {
  211. if 2 > len(z.buf)-z.n {
  212. z.flush()
  213. }
  214. z.buf[z.n+1] = b2
  215. z.buf[z.n] = b1
  216. z.n += 2
  217. }
  218. func (z *bufioEncWriter) endErr() (err error) {
  219. if z.n > 0 {
  220. err = z.flushErr()
  221. }
  222. return
  223. }
  224. // ---------------------------------------------
  225. // bytesEncAppender implements encWriter and can write to an byte slice.
  226. type bytesEncAppender struct {
  227. b []byte
  228. out *[]byte
  229. }
  230. func (z *bytesEncAppender) writeb(s []byte) {
  231. z.b = append(z.b, s...)
  232. }
  233. func (z *bytesEncAppender) writestr(s string) {
  234. z.b = append(z.b, s...)
  235. }
  236. func (z *bytesEncAppender) writeqstr(s string) {
  237. // z.writen1('"')
  238. // z.writestr(s)
  239. // z.writen1('"')
  240. z.b = append(append(append(z.b, '"'), s...), '"')
  241. // z.b = append(z.b, '"')
  242. // z.b = append(z.b, s...)
  243. // z.b = append(z.b, '"')
  244. }
  245. func (z *bytesEncAppender) writen1(b1 byte) {
  246. z.b = append(z.b, b1)
  247. }
  248. func (z *bytesEncAppender) writen2(b1, b2 byte) {
  249. z.b = append(z.b, b1, b2)
  250. }
  251. func (z *bytesEncAppender) endErr() error {
  252. *(z.out) = z.b
  253. return nil
  254. }
  255. func (z *bytesEncAppender) reset(in []byte, out *[]byte) {
  256. z.b = in[:0]
  257. z.out = out
  258. }
  259. // --------------------------------------------------
  260. type encWriterSwitch struct {
  261. esep bool // whether it has elem separators
  262. bytes bool // encoding to []byte
  263. isas bool // whether e.as != nil
  264. js bool // is json encoder?
  265. be bool // is binary encoder?
  266. c containerState
  267. // _ [3]byte // padding
  268. // _ [2]uint64 // padding
  269. // _ uint64 // padding
  270. // wi *ioEncWriter
  271. wb bytesEncAppender
  272. wf *bufioEncWriter
  273. // typ entryType
  274. }
  275. func (z *encWriterSwitch) writeb(s []byte) {
  276. if z.bytes {
  277. z.wb.writeb(s)
  278. } else {
  279. z.wf.writeb(s)
  280. }
  281. }
  282. func (z *encWriterSwitch) writeqstr(s string) {
  283. if z.bytes {
  284. z.wb.writeqstr(s)
  285. } else {
  286. z.wf.writeqstr(s)
  287. }
  288. }
  289. func (z *encWriterSwitch) writestr(s string) {
  290. if z.bytes {
  291. z.wb.writestr(s)
  292. } else {
  293. z.wf.writestr(s)
  294. }
  295. }
  296. func (z *encWriterSwitch) writen1(b1 byte) {
  297. if z.bytes {
  298. z.wb.writen1(b1)
  299. } else {
  300. z.wf.writen1(b1)
  301. }
  302. }
  303. func (z *encWriterSwitch) writen2(b1, b2 byte) {
  304. if z.bytes {
  305. z.wb.writen2(b1, b2)
  306. } else {
  307. z.wf.writen2(b1, b2)
  308. }
  309. }
  310. func (z *encWriterSwitch) endErr() error {
  311. if z.bytes {
  312. return z.wb.endErr()
  313. }
  314. return z.wf.endErr()
  315. }
  316. func (z *encWriterSwitch) end() {
  317. if err := z.endErr(); err != nil {
  318. panic(err)
  319. }
  320. }
  321. /*
  322. // ------------------------------------------
  323. func (z *encWriterSwitch) writeb(s []byte) {
  324. switch z.typ {
  325. case entryTypeBytes:
  326. z.wb.writeb(s)
  327. case entryTypeIo:
  328. z.wi.writeb(s)
  329. default:
  330. z.wf.writeb(s)
  331. }
  332. }
  333. func (z *encWriterSwitch) writestr(s string) {
  334. switch z.typ {
  335. case entryTypeBytes:
  336. z.wb.writestr(s)
  337. case entryTypeIo:
  338. z.wi.writestr(s)
  339. default:
  340. z.wf.writestr(s)
  341. }
  342. }
  343. func (z *encWriterSwitch) writen1(b1 byte) {
  344. switch z.typ {
  345. case entryTypeBytes:
  346. z.wb.writen1(b1)
  347. case entryTypeIo:
  348. z.wi.writen1(b1)
  349. default:
  350. z.wf.writen1(b1)
  351. }
  352. }
  353. func (z *encWriterSwitch) writen2(b1, b2 byte) {
  354. switch z.typ {
  355. case entryTypeBytes:
  356. z.wb.writen2(b1, b2)
  357. case entryTypeIo:
  358. z.wi.writen2(b1, b2)
  359. default:
  360. z.wf.writen2(b1, b2)
  361. }
  362. }
  363. func (z *encWriterSwitch) end() {
  364. switch z.typ {
  365. case entryTypeBytes:
  366. z.wb.end()
  367. case entryTypeIo:
  368. z.wi.end()
  369. default:
  370. z.wf.end()
  371. }
  372. }
  373. // ------------------------------------------
  374. func (z *encWriterSwitch) writeb(s []byte) {
  375. if z.bytes {
  376. z.wb.writeb(s)
  377. } else {
  378. z.wi.writeb(s)
  379. }
  380. }
  381. func (z *encWriterSwitch) writestr(s string) {
  382. if z.bytes {
  383. z.wb.writestr(s)
  384. } else {
  385. z.wi.writestr(s)
  386. }
  387. }
  388. func (z *encWriterSwitch) writen1(b1 byte) {
  389. if z.bytes {
  390. z.wb.writen1(b1)
  391. } else {
  392. z.wi.writen1(b1)
  393. }
  394. }
  395. func (z *encWriterSwitch) writen2(b1, b2 byte) {
  396. if z.bytes {
  397. z.wb.writen2(b1, b2)
  398. } else {
  399. z.wi.writen2(b1, b2)
  400. }
  401. }
  402. func (z *encWriterSwitch) end() {
  403. if z.bytes {
  404. z.wb.end()
  405. } else {
  406. z.wi.end()
  407. }
  408. }
  409. */