writer.go 8.8 KB

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