reader.go 22 KB

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