writer.go 8.8 KB

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