reader.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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. // decReader abstracts the reading source, allowing implementations that can
  6. // read from an io.Reader or directly off a byte slice with zero-copying.
  7. type decReader interface {
  8. unreadn1()
  9. // readx will use the implementation scratch buffer if possible i.e. n < len(scratchbuf), OR
  10. // just return a view of the []byte being decoded from.
  11. // Ensure you call detachZeroCopyBytes later if this needs to be sent outside codec control.
  12. readx(n uint) []byte
  13. readb([]byte)
  14. readn1() uint8
  15. // read up to 7 bytes at a time
  16. readn(num uint8) (v [rwNLen]byte)
  17. numread() uint // number of bytes read
  18. track()
  19. stopTrack() []byte
  20. // skip will skip any byte that matches, and return the first non-matching byte
  21. skip(accept *bitset256) (token byte)
  22. // readTo will read any byte that matches, stopping once no-longer matching.
  23. readTo(accept *bitset256) (out []byte)
  24. // readUntil will read, only stopping once it matches the 'stop' byte.
  25. readUntil(stop byte, includeLast bool) (out []byte)
  26. }
  27. // ------------------------------------------------
  28. type unreadByteStatus uint8
  29. // unreadByteStatus goes from
  30. // undefined (when initialized) -- (read) --> canUnread -- (unread) --> canRead ...
  31. const (
  32. unreadByteUndefined unreadByteStatus = iota
  33. unreadByteCanRead
  34. unreadByteCanUnread
  35. )
  36. // --------------------
  37. type ioDecReaderCommon struct {
  38. r io.Reader // the reader passed in
  39. n uint // num read
  40. l byte // last byte
  41. ls unreadByteStatus // last byte status
  42. trb bool // tracking bytes turned on
  43. _ bool
  44. b [4]byte // tiny buffer for reading single bytes
  45. blist *bytesFreelist
  46. tr []byte // buffer for tracking bytes
  47. bufr []byte // buffer for readTo/readUntil
  48. }
  49. func (z *ioDecReaderCommon) last() byte {
  50. return z.l
  51. }
  52. func (z *ioDecReaderCommon) reset(r io.Reader, blist *bytesFreelist) {
  53. z.blist = blist
  54. z.r = r
  55. z.ls = unreadByteUndefined
  56. z.l, z.n = 0, 0
  57. z.trb = false
  58. }
  59. func (z *ioDecReaderCommon) numread() uint {
  60. return z.n
  61. }
  62. func (z *ioDecReaderCommon) track() {
  63. z.tr = z.blist.check(z.tr, 256)[:0]
  64. z.trb = true
  65. }
  66. func (z *ioDecReaderCommon) stopTrack() (bs []byte) {
  67. z.trb = false
  68. return z.tr
  69. }
  70. // ------------------------------------------
  71. // ioDecReader is a decReader that reads off an io.Reader.
  72. //
  73. // It also has a fallback implementation of ByteScanner if needed.
  74. type ioDecReader struct {
  75. ioDecReaderCommon
  76. // rr io.Reader
  77. br io.ByteScanner
  78. x [64 + 16]byte // for: get struct field name, swallow valueTypeBytes, etc
  79. // _ [1]uint64 // padding
  80. }
  81. func (z *ioDecReader) reset(r io.Reader, blist *bytesFreelist) {
  82. z.ioDecReaderCommon.reset(r, blist)
  83. z.br, _ = r.(io.ByteScanner)
  84. }
  85. func (z *ioDecReader) Read(p []byte) (n int, err error) {
  86. if len(p) == 0 {
  87. return
  88. }
  89. var firstByte bool
  90. if z.ls == unreadByteCanRead {
  91. z.ls = unreadByteCanUnread
  92. p[0] = z.l
  93. if len(p) == 1 {
  94. n = 1
  95. return
  96. }
  97. firstByte = true
  98. p = p[1:]
  99. }
  100. n, err = z.r.Read(p)
  101. if n > 0 {
  102. if err == io.EOF && n == len(p) {
  103. err = nil // read was successful, so postpone EOF (till next time)
  104. }
  105. z.l = p[n-1]
  106. z.ls = unreadByteCanUnread
  107. }
  108. if firstByte {
  109. n++
  110. }
  111. return
  112. }
  113. func (z *ioDecReader) ReadByte() (c byte, err error) {
  114. if z.br != nil {
  115. c, err = z.br.ReadByte()
  116. if err == nil {
  117. z.l = c
  118. z.ls = unreadByteCanUnread
  119. }
  120. return
  121. }
  122. n, err := z.Read(z.b[:1])
  123. if n == 1 {
  124. c = z.b[0]
  125. if err == io.EOF {
  126. err = nil // read was successful, so postpone EOF (till next time)
  127. }
  128. }
  129. return
  130. }
  131. func (z *ioDecReader) UnreadByte() (err error) {
  132. if z.br != nil {
  133. err = z.br.UnreadByte()
  134. if err == nil {
  135. z.ls = unreadByteCanRead
  136. }
  137. return
  138. }
  139. switch z.ls {
  140. case unreadByteCanUnread:
  141. z.ls = unreadByteCanRead
  142. case unreadByteCanRead:
  143. err = errDecUnreadByteLastByteNotRead
  144. case unreadByteUndefined:
  145. err = errDecUnreadByteNothingToRead
  146. default:
  147. err = errDecUnreadByteUnknown
  148. }
  149. return
  150. }
  151. func (z *ioDecReader) readn(num uint8) (bs [rwNLen]byte) {
  152. z.readb(bs[:num])
  153. // copy(bs[:], z.readx(uint(num)))
  154. return
  155. }
  156. func (z *ioDecReader) readx(n uint) (bs []byte) {
  157. if n == 0 {
  158. return
  159. }
  160. if n < uint(len(z.x)) {
  161. bs = z.x[:n]
  162. } else {
  163. bs = make([]byte, n)
  164. }
  165. if _, err := decReadFull(z.r, bs); err != nil {
  166. panic(err)
  167. }
  168. z.n += uint(len(bs))
  169. if z.trb {
  170. z.tr = append(z.tr, bs...)
  171. }
  172. return
  173. }
  174. func (z *ioDecReader) readb(bs []byte) {
  175. if len(bs) == 0 {
  176. return
  177. }
  178. if _, err := decReadFull(z.r, bs); err != nil {
  179. panic(err)
  180. }
  181. z.n += uint(len(bs))
  182. if z.trb {
  183. z.tr = append(z.tr, bs...)
  184. }
  185. }
  186. func (z *ioDecReader) readn1eof() (b uint8, eof bool) {
  187. b, err := z.ReadByte()
  188. if err == nil {
  189. z.n++
  190. if z.trb {
  191. z.tr = append(z.tr, b)
  192. }
  193. } else if err == io.EOF {
  194. eof = true
  195. } else {
  196. panic(err)
  197. }
  198. return
  199. }
  200. func (z *ioDecReader) readn1() (b uint8) {
  201. b, err := z.ReadByte()
  202. if err == nil {
  203. z.n++
  204. if z.trb {
  205. z.tr = append(z.tr, b)
  206. }
  207. return
  208. }
  209. panic(err)
  210. }
  211. func (z *ioDecReader) skip(accept *bitset256) (token byte) {
  212. var eof bool
  213. LOOP:
  214. token, eof = z.readn1eof()
  215. if eof {
  216. return
  217. }
  218. if accept.isset(token) {
  219. goto LOOP
  220. }
  221. return
  222. }
  223. func (z *ioDecReader) readTo(accept *bitset256) []byte {
  224. z.bufr = z.blist.check(z.bufr, 256)[:0]
  225. LOOP:
  226. token, eof := z.readn1eof()
  227. if eof {
  228. return z.bufr
  229. }
  230. if accept.isset(token) {
  231. z.bufr = append(z.bufr, token)
  232. goto LOOP
  233. }
  234. z.unreadn1()
  235. return z.bufr
  236. }
  237. func (z *ioDecReader) readUntil(stop byte, includeLast bool) []byte {
  238. z.bufr = z.blist.check(z.bufr, 256)[:0]
  239. LOOP:
  240. token, eof := z.readn1eof()
  241. if eof {
  242. panic(io.EOF)
  243. }
  244. z.bufr = append(z.bufr, token)
  245. if token == stop {
  246. if includeLast {
  247. return z.bufr
  248. }
  249. return z.bufr[:len(z.bufr)-1]
  250. }
  251. goto LOOP
  252. }
  253. //go:noinline
  254. func (z *ioDecReader) unreadn1() {
  255. err := z.UnreadByte()
  256. if err != nil {
  257. panic(err)
  258. }
  259. z.n--
  260. if z.trb {
  261. if l := len(z.tr) - 1; l >= 0 {
  262. z.tr = z.tr[:l]
  263. }
  264. }
  265. }
  266. // ------------------------------------
  267. type bufioDecReader struct {
  268. ioDecReaderCommon
  269. c uint // cursor
  270. buf []byte
  271. }
  272. func (z *bufioDecReader) reset(r io.Reader, bufsize int, blist *bytesFreelist) {
  273. z.ioDecReaderCommon.reset(r, blist)
  274. z.c = 0
  275. if cap(z.buf) < bufsize {
  276. z.buf = blist.get(bufsize)
  277. }
  278. z.buf = z.buf[:0]
  279. }
  280. func (z *bufioDecReader) readb(p []byte) {
  281. var n = uint(copy(p, z.buf[z.c:]))
  282. z.n += n
  283. z.c += n
  284. if len(p) == int(n) {
  285. if z.trb {
  286. z.tr = append(z.tr, p...)
  287. }
  288. } else {
  289. z.readbFill(p, n)
  290. }
  291. }
  292. func (z *bufioDecReader) readbFill(p0 []byte, n uint) {
  293. // at this point, there's nothing in z.buf to read (z.buf is fully consumed)
  294. p := p0[n:]
  295. var n2 uint
  296. var err error
  297. if len(p) > cap(z.buf) {
  298. n2, err = decReadFull(z.r, p)
  299. if err != nil {
  300. panic(err)
  301. }
  302. n += n2
  303. z.n += n2
  304. // always keep last byte in z.buf
  305. z.buf = z.buf[:1]
  306. z.buf[0] = p[len(p)-1]
  307. z.c = 1
  308. if z.trb {
  309. z.tr = append(z.tr, p0[:n]...)
  310. }
  311. return
  312. }
  313. // z.c is now 0, and len(p) <= cap(z.buf)
  314. LOOP:
  315. // for len(p) > 0 && z.err == nil {
  316. if len(p) > 0 {
  317. z.buf = z.buf[0:cap(z.buf)]
  318. var n1 int
  319. n1, err = z.r.Read(z.buf)
  320. n2 = uint(n1)
  321. if n2 == 0 && err != nil {
  322. panic(err)
  323. }
  324. z.buf = z.buf[:n2]
  325. n2 = uint(copy(p, z.buf))
  326. z.c = n2
  327. n += n2
  328. z.n += n2
  329. p = p[n2:]
  330. goto LOOP
  331. }
  332. if z.c == 0 {
  333. z.buf = z.buf[:1]
  334. z.buf[0] = p[len(p)-1]
  335. z.c = 1
  336. }
  337. if z.trb {
  338. z.tr = append(z.tr, p0[:n]...)
  339. }
  340. }
  341. func (z *bufioDecReader) last() byte {
  342. return z.buf[z.c-1]
  343. }
  344. func (z *bufioDecReader) readn1() (b byte) {
  345. // fast-path, so we elide calling into Read() most of the time
  346. if z.c < uint(len(z.buf)) {
  347. b = z.buf[z.c]
  348. z.c++
  349. z.n++
  350. if z.trb {
  351. z.tr = append(z.tr, b)
  352. }
  353. } else { // meaning z.c == len(z.buf) or greater ... so need to fill
  354. z.readbFill(z.b[:1], 0)
  355. b = z.b[0]
  356. }
  357. return
  358. }
  359. func (z *bufioDecReader) unreadn1() {
  360. if z.c == 0 {
  361. panic(errDecUnreadByteNothingToRead)
  362. }
  363. z.c--
  364. z.n--
  365. if z.trb {
  366. z.tr = z.tr[:len(z.tr)-1]
  367. }
  368. }
  369. func (z *bufioDecReader) readn(num uint8) (bs [rwNLen]byte) {
  370. z.readb(bs[:num])
  371. // copy(bs[:], z.readx(uint(num)))
  372. return
  373. }
  374. func (z *bufioDecReader) readx(n uint) (bs []byte) {
  375. if n == 0 {
  376. // return
  377. } else if z.c+n <= uint(len(z.buf)) {
  378. bs = z.buf[z.c : z.c+n]
  379. z.n += n
  380. z.c += n
  381. if z.trb {
  382. z.tr = append(z.tr, bs...)
  383. }
  384. } else {
  385. bs = make([]byte, n)
  386. // n no longer used - can reuse
  387. n = uint(copy(bs, z.buf[z.c:]))
  388. z.n += n
  389. z.c += n
  390. z.readbFill(bs, n)
  391. }
  392. return
  393. }
  394. func (z *bufioDecReader) skip(accept *bitset256) (token byte) {
  395. i := z.c
  396. LOOP:
  397. if i < uint(len(z.buf)) {
  398. // inline z.skipLoopFn(i) and refactor, so cost is within inline budget
  399. token = z.buf[i]
  400. i++
  401. if accept.isset(token) {
  402. goto LOOP
  403. }
  404. z.n += i - 2 - z.c
  405. if z.trb {
  406. z.tr = append(z.tr, z.buf[z.c:i]...) // z.doTrack(i)
  407. }
  408. z.c = i
  409. return
  410. }
  411. return z.skipFill(accept)
  412. }
  413. func (z *bufioDecReader) skipFill(accept *bitset256) (token byte) {
  414. z.n += uint(len(z.buf)) - z.c
  415. if z.trb {
  416. z.tr = append(z.tr, z.buf[z.c:]...)
  417. }
  418. var i, n2 int
  419. var err error
  420. for {
  421. z.c = 0
  422. z.buf = z.buf[0:cap(z.buf)]
  423. n2, err = z.r.Read(z.buf)
  424. if n2 == 0 && err != nil {
  425. panic(err)
  426. }
  427. z.buf = z.buf[:n2]
  428. for i, token = range z.buf {
  429. // if !accept.isset(token) {
  430. if accept.check(token) == 0 {
  431. z.n += (uint(i) - z.c) - 1
  432. z.loopFn(uint(i + 1))
  433. return
  434. }
  435. }
  436. z.n += uint(n2)
  437. if z.trb {
  438. z.tr = append(z.tr, z.buf...)
  439. }
  440. }
  441. }
  442. func (z *bufioDecReader) loopFn(i uint) {
  443. if z.trb {
  444. z.tr = append(z.tr, z.buf[z.c:i]...) // z.doTrack(i)
  445. }
  446. z.c = i
  447. }
  448. func (z *bufioDecReader) readTo(accept *bitset256) (out []byte) {
  449. i := z.c
  450. LOOP:
  451. if i < uint(len(z.buf)) {
  452. // if !accept.isset(z.buf[i]) {
  453. if accept.check(z.buf[i]) == 0 {
  454. // inline readToLoopFn here (for performance)
  455. z.n += (i - z.c) - 1
  456. out = z.buf[z.c:i]
  457. if z.trb {
  458. z.tr = append(z.tr, z.buf[z.c:i]...) // z.doTrack(i)
  459. }
  460. z.c = i
  461. return
  462. }
  463. i++
  464. goto LOOP
  465. }
  466. return z.readToFill(accept)
  467. }
  468. func (z *bufioDecReader) readToFill(accept *bitset256) []byte {
  469. z.bufr = z.blist.check(z.bufr, 256)[:0]
  470. z.n += uint(len(z.buf)) - z.c
  471. z.bufr = append(z.bufr, z.buf[z.c:]...)
  472. if z.trb {
  473. z.tr = append(z.tr, z.buf[z.c:]...)
  474. }
  475. var n2 int
  476. var err error
  477. for {
  478. z.c = 0
  479. z.buf = z.buf[:cap(z.buf)]
  480. n2, err = z.r.Read(z.buf)
  481. if n2 == 0 && err != nil {
  482. if err == io.EOF {
  483. return z.bufr // readTo should read until it matches or end is reached
  484. }
  485. panic(err)
  486. }
  487. z.buf = z.buf[:n2]
  488. for i, token := range z.buf {
  489. // if !accept.isset(token) {
  490. if accept.check(token) == 0 {
  491. z.n += (uint(i) - z.c) - 1
  492. z.bufr = append(z.bufr, z.buf[z.c:i]...)
  493. z.loopFn(uint(i))
  494. return z.bufr
  495. }
  496. }
  497. z.bufr = append(z.bufr, z.buf...)
  498. z.n += uint(n2)
  499. if z.trb {
  500. z.tr = append(z.tr, z.buf...)
  501. }
  502. }
  503. }
  504. func (z *bufioDecReader) readUntil(stop byte, includeLast bool) (out []byte) {
  505. i := z.c
  506. LOOP:
  507. if i < uint(len(z.buf)) {
  508. if z.buf[i] == stop {
  509. z.n += (i - z.c) - 1
  510. i++
  511. out = z.buf[z.c:i]
  512. if z.trb {
  513. z.tr = append(z.tr, z.buf[z.c:i]...) // z.doTrack(i)
  514. }
  515. z.c = i
  516. goto FINISH
  517. }
  518. i++
  519. goto LOOP
  520. }
  521. out = z.readUntilFill(stop)
  522. FINISH:
  523. if includeLast {
  524. return
  525. }
  526. return out[:len(out)-1]
  527. }
  528. func (z *bufioDecReader) readUntilFill(stop byte) []byte {
  529. z.bufr = z.blist.check(z.bufr, 256)[:0]
  530. z.n += uint(len(z.buf)) - z.c
  531. z.bufr = append(z.bufr, z.buf[z.c:]...)
  532. if z.trb {
  533. z.tr = append(z.tr, z.buf[z.c:]...)
  534. }
  535. for {
  536. z.c = 0
  537. z.buf = z.buf[0:cap(z.buf)]
  538. n1, err := z.r.Read(z.buf)
  539. if n1 == 0 && err != nil {
  540. panic(err)
  541. }
  542. n2 := uint(n1)
  543. z.buf = z.buf[:n2]
  544. for i, token := range z.buf {
  545. if token == stop {
  546. z.n += (uint(i) - z.c) - 1
  547. z.bufr = append(z.bufr, z.buf[z.c:i+1]...)
  548. z.loopFn(uint(i + 1))
  549. return z.bufr
  550. }
  551. }
  552. z.bufr = append(z.bufr, z.buf...)
  553. z.n += n2
  554. if z.trb {
  555. z.tr = append(z.tr, z.buf...)
  556. }
  557. }
  558. }
  559. // ------------------------------------
  560. // bytesDecReader is a decReader that reads off a byte slice with zero copying
  561. type bytesDecReader struct {
  562. b []byte // data
  563. c uint // cursor
  564. t uint // track start
  565. // a int // available
  566. }
  567. func (z *bytesDecReader) reset(in []byte) {
  568. z.b = in
  569. z.c = 0
  570. z.t = 0
  571. }
  572. func (z *bytesDecReader) numread() uint {
  573. return z.c
  574. }
  575. func (z *bytesDecReader) last() byte {
  576. return z.b[z.c-1]
  577. }
  578. func (z *bytesDecReader) unreadn1() {
  579. if z.c == 0 || len(z.b) == 0 {
  580. panic(errBytesDecReaderCannotUnread)
  581. }
  582. z.c--
  583. }
  584. func (z *bytesDecReader) readx(n uint) (bs []byte) {
  585. // slicing from a non-constant start position is more expensive,
  586. // as more computation is required to decipher the pointer start position.
  587. // However, we do it only once, and it's better than reslicing both z.b and return value.
  588. z.c += n
  589. return z.b[z.c-n : z.c]
  590. }
  591. func (z *bytesDecReader) readb(bs []byte) {
  592. copy(bs, z.readx(uint(len(bs))))
  593. }
  594. func (z *bytesDecReader) readn1() (v uint8) {
  595. v = z.b[z.c]
  596. z.c++
  597. return
  598. }
  599. func (z *bytesDecReader) readn(num uint8) (bs [rwNLen]byte) {
  600. // if z.c >= uint(len(z.b)) || z.c+uint(num) >= uint(len(z.b)) {
  601. // panic(io.EOF)
  602. // }
  603. // for bounds-check elimination, reslice z.b and ensure bs is within len
  604. // bb := z.b[z.c:][:num]
  605. bb := z.b[z.c : z.c+uint(num)]
  606. _ = bs[len(bb)-1]
  607. var i int
  608. LOOP:
  609. if i < len(bb) {
  610. bs[i] = bb[i]
  611. i++
  612. goto LOOP
  613. }
  614. z.c += uint(num)
  615. return
  616. }
  617. func (z *bytesDecReader) skip(accept *bitset256) (token byte) {
  618. i := z.c
  619. LOOP:
  620. // if i < uint(len(z.b)) {
  621. token = z.b[i]
  622. i++
  623. if accept.isset(token) {
  624. goto LOOP
  625. }
  626. z.c = i
  627. return
  628. }
  629. func (z *bytesDecReader) readTo(accept *bitset256) (out []byte) {
  630. i := z.c
  631. LOOP:
  632. if i < uint(len(z.b)) {
  633. if accept.isset(z.b[i]) {
  634. i++
  635. goto LOOP
  636. }
  637. }
  638. out = z.b[z.c:i]
  639. z.c = i
  640. return // z.b[c:i]
  641. }
  642. func (z *bytesDecReader) readUntil(stop byte, includeLast bool) (out []byte) {
  643. i := z.c
  644. LOOP:
  645. // if i < uint(len(z.b)) {
  646. if z.b[i] == stop {
  647. i++
  648. if includeLast {
  649. out = z.b[z.c:i]
  650. } else {
  651. out = z.b[z.c : i-1]
  652. }
  653. // z.a -= (i - z.c)
  654. z.c = i
  655. return
  656. }
  657. i++
  658. goto LOOP
  659. // }
  660. // panic(io.EOF)
  661. }
  662. func (z *bytesDecReader) track() {
  663. z.t = z.c
  664. }
  665. func (z *bytesDecReader) stopTrack() (bs []byte) {
  666. return z.b[z.t:z.c]
  667. }
  668. // --------------
  669. type decRd struct {
  670. mtr bool // is maptype a known type?
  671. str bool // is slicetype a known type?
  672. be bool // is binary encoding
  673. js bool // is json handle
  674. jsms bool // is json handle, and MapKeyAsString
  675. cbor bool // is cbor handle
  676. bytes bool // is bytes reader
  677. bufio bool // is this a bufioDecReader?
  678. rb bytesDecReader
  679. ri *ioDecReader
  680. bi *bufioDecReader
  681. }
  682. // numread, track and stopTrack are always inlined, as they just check int fields, etc.
  683. // the if/else-if/else block is expensive to inline.
  684. // Each node of this construct costs a lot and dominates the budget.
  685. // Best to only do an if fast-path else block (so fast-path is inlined).
  686. // This is irrespective of inlineExtraCallCost set in $GOROOT/src/cmd/compile/internal/gc/inl.go
  687. //
  688. // In decRd methods below, we delegate all IO functions into their own methods.
  689. // This allows for the inlining of the common path when z.bytes=true.
  690. // Go 1.12+ supports inlining methods with up to 1 inlined function (or 2 if no other constructs).
  691. //
  692. // However, up through Go 1.13, decRd's readXXX, skip and unreadXXX methods are not inlined.
  693. // Consequently, there is no benefit to do the xxxIO methods for decRd at this time.
  694. // Instead, we have a if/else-if/else block so that IO calls do not have to jump through
  695. // a second unnecessary function call.
  696. //
  697. // If golang inlining gets better and bytesDecReader methods can be inlined,
  698. // then we can revert to using these 2 functions so the bytesDecReader
  699. // methods are inlined and the IO paths call out to a function.
  700. func (z *decRd) numread() uint {
  701. if z.bytes {
  702. return z.rb.numread()
  703. } else if z.bufio {
  704. return z.bi.numread()
  705. } else {
  706. return z.ri.numread()
  707. }
  708. }
  709. func (z *decRd) stopTrack() []byte {
  710. if z.bytes {
  711. return z.rb.stopTrack()
  712. } else if z.bufio {
  713. return z.bi.stopTrack()
  714. } else {
  715. return z.ri.stopTrack()
  716. }
  717. }
  718. func (z *decRd) track() {
  719. if z.bytes {
  720. z.rb.track()
  721. } else if z.bufio {
  722. z.bi.track()
  723. } else {
  724. z.ri.track()
  725. }
  726. }
  727. func (z *decRd) unreadn1() {
  728. if z.bytes {
  729. z.rb.unreadn1()
  730. } else if z.bufio {
  731. z.bi.unreadn1()
  732. } else {
  733. z.ri.unreadn1() // not inlined
  734. }
  735. }
  736. func (z *decRd) readn(num uint8) [rwNLen]byte {
  737. if z.bytes {
  738. return z.rb.readn(num)
  739. } else if z.bufio {
  740. return z.bi.readn(num)
  741. } else {
  742. return z.ri.readn(num)
  743. }
  744. }
  745. func (z *decRd) readx(n uint) []byte {
  746. if z.bytes {
  747. return z.rb.readx(n)
  748. } else if z.bufio {
  749. return z.bi.readx(n)
  750. } else {
  751. return z.ri.readx(n)
  752. }
  753. }
  754. func (z *decRd) readb(s []byte) {
  755. if z.bytes {
  756. z.rb.readb(s)
  757. } else if z.bufio {
  758. z.bi.readb(s)
  759. } else {
  760. z.ri.readb(s)
  761. }
  762. }
  763. func (z *decRd) readn1() uint8 {
  764. if z.bytes {
  765. return z.rb.readn1()
  766. } else if z.bufio {
  767. return z.bi.readn1()
  768. } else {
  769. return z.ri.readn1()
  770. }
  771. }
  772. func (z *decRd) skip(accept *bitset256) (token byte) {
  773. if z.bytes {
  774. return z.rb.skip(accept)
  775. } else if z.bufio {
  776. return z.bi.skip(accept)
  777. } else {
  778. return z.ri.skip(accept)
  779. }
  780. }
  781. func (z *decRd) readTo(accept *bitset256) (out []byte) {
  782. if z.bytes {
  783. return z.rb.readTo(accept)
  784. } else if z.bufio {
  785. return z.bi.readTo(accept)
  786. } else {
  787. return z.ri.readTo(accept)
  788. }
  789. }
  790. func (z *decRd) readUntil(stop byte, includeLast bool) (out []byte) {
  791. if z.bytes {
  792. return z.rb.readUntil(stop, includeLast)
  793. } else if z.bufio {
  794. return z.bi.readUntil(stop, includeLast)
  795. } else {
  796. return z.ri.readUntil(stop, includeLast)
  797. }
  798. }
  799. /*
  800. func (z *decRd) track() {
  801. if z.bytes {
  802. z.rb.track()
  803. } else {
  804. z.trackIO()
  805. }
  806. }
  807. func (z *decRd) trackIO() {
  808. if z.bufio {
  809. z.bi.track()
  810. } else {
  811. z.ri.track()
  812. }
  813. }
  814. func (z *decRd) unreadn1() {
  815. if z.bytes {
  816. z.rb.unreadn1()
  817. } else {
  818. z.unreadn1IO()
  819. }
  820. }
  821. func (z *decRd) unreadn1IO() {
  822. if z.bufio {
  823. z.bi.unreadn1()
  824. } else {
  825. z.ri.unreadn1()
  826. }
  827. }
  828. func (z *decRd) readn(num uint8) [rwNLen]byte {
  829. if z.bytes {
  830. return z.rb.readn(num)
  831. }
  832. return z.readnIO(num)
  833. }
  834. func (z *decRd) readnIO(num uint8) [rwNLen]byte {
  835. if z.bufio {
  836. return z.bi.readn(num)
  837. }
  838. return z.ri.readn(num)
  839. }
  840. func (z *decRd) readx(n uint) []byte {
  841. if z.bytes {
  842. return z.rb.readx(n)
  843. }
  844. return z.readxIO(n)
  845. }
  846. func (z *decRd) readxIO(n uint) []byte {
  847. if z.bufio {
  848. return z.bi.readx(n)
  849. }
  850. return z.ri.readx(n)
  851. }
  852. func (z *decRd) readb(s []byte) {
  853. if z.bytes {
  854. z.rb.readb(s)
  855. } else {
  856. z.readbIO(s)
  857. }
  858. }
  859. func (z *decRd) readbIO(s []byte) {
  860. if z.bufio {
  861. z.bi.readb(s)
  862. } else {
  863. z.ri.readb(s)
  864. }
  865. }
  866. func (z *decRd) readn1() uint8 {
  867. if z.bytes {
  868. return z.rb.readn1()
  869. }
  870. return z.readn1IO()
  871. }
  872. func (z *decRd) readn1IO() uint8 {
  873. if z.bufio {
  874. return z.bi.readn1()
  875. }
  876. return z.ri.readn1()
  877. }
  878. func (z *decRd) skip(accept *bitset256) (token byte) {
  879. if z.bytes {
  880. return z.rb.skip(accept)
  881. }
  882. return z.skipIO(accept)
  883. }
  884. func (z *decRd) skipIO(accept *bitset256) (token byte) {
  885. if z.bufio {
  886. return z.bi.skip(accept)
  887. }
  888. return z.ri.skip(accept)
  889. }
  890. func (z *decRd) readTo(accept *bitset256) (out []byte) {
  891. if z.bytes {
  892. return z.rb.readTo(accept)
  893. }
  894. return z.readToIO(accept)
  895. }
  896. func (z *decRd) readToIO(accept *bitset256) (out []byte) {
  897. if z.bufio {
  898. return z.bi.readTo(accept)
  899. }
  900. return z.ri.readTo(accept)
  901. }
  902. func (z *decRd) readUntil(stop byte, includeLast bool) (out []byte) {
  903. if z.bytes {
  904. return z.rb.readUntil(stop, includeLast)
  905. }
  906. return z.readUntilIO(stop, includeLast)
  907. }
  908. func (z *decRd) readUntilIO(stop byte, includeLast bool) (out []byte) {
  909. if z.bufio {
  910. return z.bi.readUntil(stop, includeLast)
  911. }
  912. return z.ri.readUntil(stop, includeLast)
  913. }
  914. */
  915. var _ decReader = (*decRd)(nil)