reader.go 20 KB

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