reader.go 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. v = z.b[z.c]
  611. z.c++
  612. return
  613. }
  614. func (z *bytesDecReader) readn(num uint8) (bs [rwNLen]byte) {
  615. // if z.c >= uint(len(z.b)) || z.c+uint(num) >= uint(len(z.b)) {
  616. // panic(io.EOF)
  617. // }
  618. // for bounds-check elimination, reslice z.b and ensure bs is within len
  619. // bb := z.b[z.c:][:num]
  620. bb := z.b[z.c : z.c+uint(num)]
  621. _ = bs[len(bb)-1]
  622. // for i := uint(0); i < uint(len(bb)); i++ {
  623. for i := 0; i < len(bb); i++ {
  624. bs[i] = bb[i]
  625. }
  626. z.c += uint(num)
  627. return
  628. }
  629. func (z *bytesDecReader) skip(accept *bitset256) (token byte) {
  630. i := z.c
  631. // if i == len(z.b) {
  632. // goto END
  633. // // panic(io.EOF)
  634. // }
  635. // Replace loop with goto construct, so that this can be inlined
  636. // for i := z.c; i < blen; i++ {
  637. // if !accept.isset(z.b[i]) {
  638. // token = z.b[i]
  639. // i++
  640. // z.a -= (i - z.c)
  641. // z.c = i
  642. // return
  643. // }
  644. // }
  645. // i := z.c
  646. LOOP:
  647. // if i < uint(len(z.b)) {
  648. token = z.b[i]
  649. i++
  650. if accept.isset(token) {
  651. goto LOOP
  652. }
  653. // z.a -= (i - z.c)
  654. z.c = i
  655. return
  656. // }
  657. // panic(io.EOF)
  658. // END:
  659. // // z.a = 0
  660. // z.c = blen
  661. // return
  662. }
  663. func (z *bytesDecReader) readTo(accept *bitset256) (out []byte) {
  664. i := z.c
  665. LOOP:
  666. if i < uint(len(z.b)) {
  667. if accept.isset(z.b[i]) {
  668. i++
  669. goto LOOP
  670. }
  671. }
  672. out = z.b[z.c:i]
  673. z.c = i
  674. return // z.b[c:i]
  675. }
  676. func (z *bytesDecReader) readUntil(stop byte, includeLast bool) (out []byte) {
  677. i := z.c
  678. LOOP:
  679. // if i < uint(len(z.b)) {
  680. if z.b[i] == stop {
  681. i++
  682. if includeLast {
  683. out = z.b[z.c:i]
  684. } else {
  685. out = z.b[z.c : i-1]
  686. }
  687. // z.a -= (i - z.c)
  688. z.c = i
  689. return
  690. }
  691. i++
  692. goto LOOP
  693. // }
  694. // panic(io.EOF)
  695. }
  696. func (z *bytesDecReader) track() {
  697. z.t = z.c
  698. }
  699. func (z *bytesDecReader) stopTrack() (bs []byte) {
  700. return z.b[z.t:z.c]
  701. }
  702. // --------------
  703. type decRd struct {
  704. mtr bool // is maptype a known type?
  705. str bool // is slicetype a known type?
  706. be bool // is binary encoding
  707. js bool // is json handle
  708. jsms bool // is json handle, and MapKeyAsString
  709. cbor bool // is cbor handle
  710. bytes bool // is bytes reader
  711. bufio bool // is this a bufioDecReader?
  712. rb bytesDecReader
  713. ri *ioDecReader
  714. bi *bufioDecReader
  715. }
  716. // numread, track and stopTrack are always inlined, as they just check int fields, etc.
  717. // the if/else-if/else block is expensive to inline.
  718. // Each node of this construct costs a lot and dominates the budget.
  719. // Best to only do an if fast-path else block (so fast-path is inlined).
  720. // This is irrespective of inlineExtraCallCost set in $GOROOT/src/cmd/compile/internal/gc/inl.go
  721. //
  722. // In decRd methods below, we delegate all IO functions into their own methods.
  723. // This allows for the inlining of the common path when z.bytes=true.
  724. // Go 1.12+ supports inlining methods with up to 1 inlined function (or 2 if no other constructs).
  725. //
  726. // However, up through Go 1.13, decRd's readXXX, skip and unreadXXX methods are not inlined.
  727. // Consequently, there is no benefit to do the xxxIO methods for decRd at this time.
  728. // Instead, we have a if/else-if/else block so that IO calls do not have to jump through
  729. // a second unnecessary function call.
  730. func (z *decRd) numread() uint {
  731. if z.bytes {
  732. return z.rb.numread()
  733. } else if z.bufio {
  734. return z.bi.numread()
  735. } else {
  736. return z.ri.numread()
  737. }
  738. }
  739. func (z *decRd) track() {
  740. if z.bytes {
  741. z.rb.track()
  742. } else if z.bufio {
  743. z.bi.track()
  744. } else {
  745. z.ri.track()
  746. }
  747. }
  748. func (z *decRd) stopTrack() []byte {
  749. if z.bytes {
  750. return z.rb.stopTrack()
  751. } else if z.bufio {
  752. return z.bi.stopTrack()
  753. } else {
  754. return z.ri.stopTrack()
  755. }
  756. }
  757. func (z *decRd) unreadn1() {
  758. if z.bytes {
  759. z.rb.unreadn1()
  760. } else if z.bufio {
  761. z.bi.unreadn1()
  762. } else {
  763. z.ri.unreadn1() // not inlined
  764. }
  765. }
  766. func (z *decRd) readn(num uint8) [rwNLen]byte {
  767. if z.bytes {
  768. return z.rb.readn(num)
  769. }
  770. if z.bufio {
  771. return z.bi.readn(num)
  772. }
  773. return z.ri.readn(num)
  774. }
  775. func (z *decRd) readx(n uint) []byte {
  776. if z.bytes {
  777. return z.rb.readx(n)
  778. }
  779. if z.bufio {
  780. return z.bi.readx(n)
  781. }
  782. return z.ri.readx(n)
  783. }
  784. func (z *decRd) readb(s []byte) {
  785. if z.bytes {
  786. z.rb.readb(s)
  787. } else if z.bufio {
  788. z.bi.readb(s)
  789. } else {
  790. z.ri.readb(s)
  791. }
  792. }
  793. func (z *decRd) readn1() uint8 {
  794. if z.bytes {
  795. return z.rb.readn1()
  796. }
  797. if z.bufio {
  798. return z.bi.readn1()
  799. }
  800. return z.ri.readn1()
  801. }
  802. func (z *decRd) skip(accept *bitset256) (token byte) {
  803. if z.bytes {
  804. return z.rb.skip(accept)
  805. }
  806. if z.bufio {
  807. return z.bi.skip(accept)
  808. }
  809. return z.ri.skip(accept)
  810. }
  811. func (z *decRd) readTo(accept *bitset256) (out []byte) {
  812. if z.bytes {
  813. return z.rb.readTo(accept)
  814. }
  815. if z.bufio {
  816. return z.bi.readTo(accept)
  817. }
  818. return z.ri.readTo(accept)
  819. }
  820. func (z *decRd) readUntil(stop byte, includeLast bool) (out []byte) {
  821. if z.bytes {
  822. return z.rb.readUntil(stop, includeLast)
  823. }
  824. if z.bufio {
  825. return z.bi.readUntil(stop, includeLast)
  826. }
  827. return z.ri.readUntil(stop, includeLast)
  828. }
  829. /*
  830. // If golang inlining gets better and bytesDecReader methods can be inlined,
  831. // then we can revert to using these 2 functions so the bytesDecReader
  832. // methods are inlined and the IO paths call out to a function.
  833. func (z *decRd) unreadn1() {
  834. if z.bytes {
  835. z.rb.unreadn1()
  836. } else {
  837. z.unreadn1IO()
  838. }
  839. }
  840. func (z *decRd) unreadn1IO() {
  841. if z.bufio {
  842. z.bi.unreadn1()
  843. } else {
  844. z.ri.unreadn1()
  845. }
  846. }
  847. func (z *decRd) readn(num uint8) [rwNLen]byte {
  848. if z.bytes {
  849. return z.rb.readn(num)
  850. }
  851. return z.readnIO(num)
  852. }
  853. func (z *decRd) readnIO(num uint8) [rwNLen]byte {
  854. if z.bufio {
  855. return z.bi.readn(num)
  856. }
  857. return z.ri.readn(num)
  858. }
  859. func (z *decRd) readx(n uint) []byte {
  860. if z.bytes {
  861. return z.rb.readx(n)
  862. }
  863. return z.readxIO(n)
  864. }
  865. func (z *decRd) readxIO(n uint) []byte {
  866. if z.bufio {
  867. return z.bi.readx(n)
  868. }
  869. return z.ri.readx(n)
  870. }
  871. func (z *decRd) readb(s []byte) {
  872. if z.bytes {
  873. z.rb.readb(s)
  874. } else {
  875. z.readbIO(s)
  876. }
  877. }
  878. func (z *decRd) readbIO(s []byte) {
  879. if z.bufio {
  880. z.bi.readb(s)
  881. } else {
  882. z.ri.readb(s)
  883. }
  884. }
  885. func (z *decRd) readn1() uint8 {
  886. if z.bytes {
  887. return z.rb.readn1()
  888. }
  889. return z.readn1IO()
  890. }
  891. func (z *decRd) readn1IO() uint8 {
  892. if z.bufio {
  893. return z.bi.readn1()
  894. }
  895. return z.ri.readn1()
  896. }
  897. func (z *decRd) skip(accept *bitset256) (token byte) {
  898. if z.bytes {
  899. return z.rb.skip(accept)
  900. }
  901. return z.skipIO(accept)
  902. }
  903. func (z *decRd) skipIO(accept *bitset256) (token byte) {
  904. if z.bufio {
  905. return z.bi.skip(accept)
  906. }
  907. return z.ri.skip(accept)
  908. }
  909. func (z *decRd) readTo(accept *bitset256) (out []byte) {
  910. if z.bytes {
  911. return z.rb.readTo(accept)
  912. }
  913. return z.readToIO(accept)
  914. }
  915. func (z *decRd) readToIO(accept *bitset256) (out []byte) {
  916. if z.bufio {
  917. return z.bi.readTo(accept)
  918. }
  919. return z.ri.readTo(accept)
  920. }
  921. func (z *decRd) readUntil(stop byte) (out []byte) {
  922. if z.bytes {
  923. return z.rb.readUntil(stop)
  924. }
  925. return z.readUntilIO(stop)
  926. }
  927. func (z *decRd) readUntilIO(stop byte) (out []byte) {
  928. if z.bufio {
  929. return z.bi.readUntil(stop)
  930. }
  931. return z.ri.readUntil(stop)
  932. }
  933. */
  934. var _ decReader = (*decRd)(nil)