reader.go 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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.readbFill(p, n, true, false)
  305. }
  306. }
  307. func (z *bufioDecReader) readbFill(p0 []byte, n uint, must bool, eof bool) (err error, isEOF bool) {
  308. // at this point, there's nothing in z.buf to read (z.buf is fully consumed)
  309. var p []byte
  310. if p0 != nil {
  311. p = p0[n:]
  312. }
  313. var n2 uint
  314. if len(p) > cap(z.buf) {
  315. n2, err = decReadFull(z.r, p)
  316. if err != nil {
  317. if err == io.EOF {
  318. isEOF = true
  319. }
  320. if must && !(eof && isEOF) {
  321. panic(err)
  322. }
  323. return
  324. }
  325. n += n2
  326. z.n += n2
  327. // always keep last byte in z.buf
  328. z.buf = z.buf[:1]
  329. z.buf[0] = p[len(p)-1]
  330. z.c = 1
  331. if z.trb {
  332. z.tr = append(z.tr, p0[:n]...)
  333. }
  334. return
  335. }
  336. // z.c is now 0, and len(p) <= cap(z.buf)
  337. var n1 int
  338. LOOP:
  339. // for len(p) > 0 && z.err == nil {
  340. z.buf = z.buf[0:cap(z.buf)]
  341. n1, err = z.r.Read(z.buf)
  342. n2 = uint(n1)
  343. if n2 == 0 && err != nil {
  344. if err == io.EOF {
  345. isEOF = true
  346. }
  347. if must && !(eof && isEOF) {
  348. panic(err)
  349. }
  350. return
  351. }
  352. err = nil // TODO: should i skip err if n2 > 0?
  353. z.buf = z.buf[:n2]
  354. z.c = 0
  355. if len(p) > 0 {
  356. n2 = uint(copy(p, z.buf))
  357. z.c = n2
  358. n += n2
  359. z.n += n2
  360. p = p[n2:]
  361. if len(p) > 0 {
  362. goto LOOP
  363. }
  364. if z.c == 0 {
  365. z.buf = z.buf[:1]
  366. z.buf[0] = p[len(p)-1]
  367. z.c = 1
  368. }
  369. }
  370. if z.trb && p0 != nil {
  371. z.tr = append(z.tr, p0[:n]...)
  372. }
  373. return
  374. }
  375. func (z *bufioDecReader) last() byte {
  376. return z.buf[z.c-1]
  377. }
  378. func (z *bufioDecReader) readn1eof() (b byte, eof bool) {
  379. if z.c >= uint(len(z.buf)) {
  380. _, eof = z.readbFill(nil, 0, true, true)
  381. if eof {
  382. return
  383. }
  384. }
  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. return
  392. }
  393. func (z *bufioDecReader) readn1() (b byte) {
  394. if z.c >= uint(len(z.buf)) {
  395. z.readbFill(nil, 0, true, false)
  396. }
  397. b = z.buf[z.c]
  398. z.c++
  399. z.n++
  400. if z.trb {
  401. z.tr = append(z.tr, b)
  402. }
  403. return
  404. }
  405. func (z *bufioDecReader) unreadn1() {
  406. if z.c == 0 {
  407. panic(errDecUnreadByteNothingToRead)
  408. }
  409. z.c--
  410. z.n--
  411. if z.trb {
  412. z.tr = z.tr[:len(z.tr)-1]
  413. }
  414. }
  415. func (z *bufioDecReader) readn(num uint8) (bs [rwNLen]byte) {
  416. z.readb(bs[:num])
  417. // copy(bs[:], z.readx(uint(num)))
  418. return
  419. }
  420. func (z *bufioDecReader) readx(n uint) (bs []byte) {
  421. if n == 0 {
  422. // return
  423. } else if z.c+n <= uint(len(z.buf)) {
  424. bs = z.buf[z.c : z.c+n]
  425. z.n += n
  426. z.c += n
  427. if z.trb {
  428. z.tr = append(z.tr, bs...)
  429. }
  430. } else {
  431. bs = make([]byte, n)
  432. // n no longer used - can reuse
  433. n = uint(copy(bs, z.buf[z.c:]))
  434. z.n += n
  435. z.c += n
  436. z.readbFill(bs, n, true, false)
  437. }
  438. return
  439. }
  440. // func (z *bufioDecReader) skip(accept *bitset256) (token byte) {
  441. // i := z.c
  442. // LOOP:
  443. // if i < uint(len(z.buf)) {
  444. // // inline z.skipLoopFn(i) and refactor, so cost is within inline budget
  445. // token = z.buf[i]
  446. // i++
  447. // if accept.isset(token) {
  448. // goto LOOP
  449. // }
  450. // z.n += i - 2 - z.c
  451. // if z.trb {
  452. // z.tr = append(z.tr, z.buf[z.c:i]...) // z.doTrack(i)
  453. // }
  454. // z.c = i
  455. // return
  456. // }
  457. // return z.skipFill(accept)
  458. // }
  459. // func (z *bufioDecReader) skipFill(accept *bitset256) (token byte) {
  460. // z.n += uint(len(z.buf)) - z.c
  461. // if z.trb {
  462. // z.tr = append(z.tr, z.buf[z.c:]...)
  463. // }
  464. // var i, n2 int
  465. // var err error
  466. // for {
  467. // z.c = 0
  468. // z.buf = z.buf[0:cap(z.buf)]
  469. // n2, err = z.r.Read(z.buf)
  470. // if n2 == 0 && err != nil {
  471. // panic(err)
  472. // }
  473. // z.buf = z.buf[:n2]
  474. // for i, token = range z.buf {
  475. // // if !accept.isset(token) {
  476. // if accept.isnotset(token) {
  477. // z.n += (uint(i) - z.c) - 1
  478. // z.loopFn(uint(i + 1))
  479. // return
  480. // }
  481. // }
  482. // z.n += uint(n2)
  483. // if z.trb {
  484. // z.tr = append(z.tr, z.buf...)
  485. // }
  486. // }
  487. // }
  488. func (z *bufioDecReader) skipWhitespace() (token byte) {
  489. i := z.c
  490. LOOP:
  491. if i < uint(len(z.buf)) {
  492. // inline z.skipLoopFn(i) and refactor, so cost is within inline budget
  493. token = z.buf[i]
  494. i++
  495. if isWhitespace(token) {
  496. goto LOOP
  497. }
  498. z.n += i - 2 - z.c
  499. if z.trb {
  500. z.tr = append(z.tr, z.buf[z.c:i]...) // z.doTrack(i)
  501. }
  502. z.c = i
  503. return
  504. }
  505. return z.skipFillWhitespace()
  506. }
  507. func (z *bufioDecReader) skipFillWhitespace() (token byte) {
  508. z.n += uint(len(z.buf)) - z.c
  509. if z.trb {
  510. z.tr = append(z.tr, z.buf[z.c:]...)
  511. }
  512. var i, n2 int
  513. var err error
  514. for {
  515. z.c = 0
  516. z.buf = z.buf[0:cap(z.buf)]
  517. n2, err = z.r.Read(z.buf)
  518. if n2 == 0 && err != nil {
  519. panic(err)
  520. }
  521. z.buf = z.buf[:n2]
  522. for i, token = range z.buf {
  523. if !isWhitespace(token) {
  524. z.n += (uint(i) - z.c) - 1
  525. z.loopFn(uint(i + 1))
  526. return
  527. }
  528. }
  529. z.n += uint(n2)
  530. if z.trb {
  531. z.tr = append(z.tr, z.buf...)
  532. }
  533. }
  534. }
  535. func (z *bufioDecReader) loopFn(i uint) {
  536. if z.trb {
  537. z.tr = append(z.tr, z.buf[z.c:i]...) // z.doTrack(i)
  538. }
  539. z.c = i
  540. }
  541. func (z *bufioDecReader) readTo(accept *bitset256) (out []byte) {
  542. i := z.c
  543. LOOP:
  544. if i < uint(len(z.buf)) {
  545. // if !accept.isset(z.buf[i]) {
  546. if accept.isnotset(z.buf[i]) {
  547. // inline readToLoopFn here (for performance)
  548. z.n += (i - z.c) - 1
  549. out = z.buf[z.c:i]
  550. if z.trb {
  551. z.tr = append(z.tr, z.buf[z.c:i]...) // z.doTrack(i)
  552. }
  553. z.c = i
  554. return
  555. }
  556. i++
  557. goto LOOP
  558. }
  559. return z.readToFill(accept)
  560. }
  561. func (z *bufioDecReader) readToFill(accept *bitset256) []byte {
  562. z.bufr = z.blist.check(z.bufr, 256)[:0]
  563. z.n += uint(len(z.buf)) - z.c
  564. z.bufr = append(z.bufr, z.buf[z.c:]...)
  565. if z.trb {
  566. z.tr = append(z.tr, z.buf[z.c:]...)
  567. }
  568. var n2 int
  569. var err error
  570. for {
  571. z.c = 0
  572. z.buf = z.buf[:cap(z.buf)]
  573. n2, err = z.r.Read(z.buf)
  574. if n2 == 0 && err != nil {
  575. if err == io.EOF {
  576. return z.bufr // readTo should read until it matches or end is reached
  577. }
  578. panic(err)
  579. }
  580. z.buf = z.buf[:n2]
  581. for i, token := range z.buf {
  582. // if !accept.isset(token) {
  583. if accept.isnotset(token) {
  584. z.n += (uint(i) - z.c) - 1
  585. z.bufr = append(z.bufr, z.buf[z.c:i]...)
  586. z.loopFn(uint(i))
  587. return z.bufr
  588. }
  589. }
  590. z.bufr = append(z.bufr, z.buf...)
  591. z.n += uint(n2)
  592. if z.trb {
  593. z.tr = append(z.tr, z.buf...)
  594. }
  595. }
  596. }
  597. func (z *bufioDecReader) readUntil(stop byte, includeLast bool) (out []byte) {
  598. i := z.c
  599. LOOP:
  600. if i < uint(len(z.buf)) {
  601. if z.buf[i] == stop {
  602. z.n += (i - z.c) - 1
  603. i++
  604. out = z.buf[z.c:i]
  605. if z.trb {
  606. z.tr = append(z.tr, z.buf[z.c:i]...) // z.doTrack(i)
  607. }
  608. z.c = i
  609. goto FINISH
  610. }
  611. i++
  612. goto LOOP
  613. }
  614. out = z.readUntilFill(stop)
  615. FINISH:
  616. if includeLast {
  617. return
  618. }
  619. return out[:len(out)-1]
  620. }
  621. func (z *bufioDecReader) readUntilFill(stop byte) []byte {
  622. z.bufr = z.blist.check(z.bufr, 256)[:0]
  623. z.n += uint(len(z.buf)) - z.c
  624. z.bufr = append(z.bufr, z.buf[z.c:]...)
  625. if z.trb {
  626. z.tr = append(z.tr, z.buf[z.c:]...)
  627. }
  628. for {
  629. z.c = 0
  630. z.buf = z.buf[0:cap(z.buf)]
  631. n1, err := z.r.Read(z.buf)
  632. if n1 == 0 && err != nil {
  633. panic(err)
  634. }
  635. n2 := uint(n1)
  636. z.buf = z.buf[:n2]
  637. for i, token := range z.buf {
  638. if token == stop {
  639. z.n += (uint(i) - z.c) - 1
  640. z.bufr = append(z.bufr, z.buf[z.c:i+1]...)
  641. z.loopFn(uint(i + 1))
  642. return z.bufr
  643. }
  644. }
  645. z.bufr = append(z.bufr, z.buf...)
  646. z.n += n2
  647. if z.trb {
  648. z.tr = append(z.tr, z.buf...)
  649. }
  650. }
  651. }
  652. // ------------------------------------
  653. // bytesDecReader is a decReader that reads off a byte slice with zero copying
  654. type bytesDecReader struct {
  655. b []byte // data
  656. c uint // cursor
  657. t uint // track start
  658. // a int // available
  659. }
  660. func (z *bytesDecReader) reset(in []byte) {
  661. z.b = in
  662. z.c = 0
  663. z.t = 0
  664. }
  665. func (z *bytesDecReader) numread() uint {
  666. return z.c
  667. }
  668. func (z *bytesDecReader) last() byte {
  669. return z.b[z.c-1]
  670. }
  671. func (z *bytesDecReader) unreadn1() {
  672. // if z.c == 0 || len(z.b) == 0 {
  673. // panic(errBytesDecReaderCannotUnread)
  674. // }
  675. z.c--
  676. }
  677. func (z *bytesDecReader) readx(n uint) (bs []byte) {
  678. // slicing from a non-constant start position is more expensive,
  679. // as more computation is required to decipher the pointer start position.
  680. // However, we do it only once, and it's better than reslicing both z.b and return value.
  681. z.c += n
  682. return z.b[z.c-n : z.c]
  683. }
  684. func (z *bytesDecReader) readb(bs []byte) {
  685. copy(bs, z.readx(uint(len(bs))))
  686. }
  687. func (z *bytesDecReader) readn1() (v uint8) {
  688. v = z.b[z.c] // cost 7
  689. z.c++ // cost 4
  690. return
  691. }
  692. // func (z *bytesDecReader) readn1() uint8 {
  693. // z.c++
  694. // return z.b[z.c-1]
  695. // }
  696. // func (z *bytesDecReader) readn1() uint8 {
  697. // v = z.b[z.c] // cost 7
  698. // c := z.c // cost 6
  699. // z.c++ // cost 4
  700. // z.c = z.c + 1 // cost 7
  701. // return z.b[z.c-1]
  702. // }
  703. func (z *bytesDecReader) readn1eof() (v uint8, eof bool) {
  704. if z.c >= uint(len(z.b)) {
  705. eof = true
  706. } else {
  707. v = z.b[z.c]
  708. z.c++
  709. }
  710. return
  711. }
  712. func (z *bytesDecReader) readn(num uint8) (bs [rwNLen]byte) {
  713. // if z.c >= uint(len(z.b)) || z.c+uint(num) >= uint(len(z.b)) {
  714. // panic(io.EOF)
  715. // }
  716. // for bounds-check elimination, reslice z.b and ensure bs is within len
  717. // bb := z.b[z.c:][:num]
  718. bb := z.b[z.c : z.c+uint(num)]
  719. _ = bs[len(bb)-1]
  720. var i int
  721. LOOP:
  722. if i < len(bb) {
  723. bs[i] = bb[i]
  724. i++
  725. goto LOOP
  726. }
  727. z.c += uint(num)
  728. return
  729. }
  730. // func (z *bytesDecReader) skip(accept *bitset256) (token byte) {
  731. // i := z.c
  732. // LOOP:
  733. // // if i < uint(len(z.b)) {
  734. // token = z.b[i]
  735. // i++
  736. // if accept.isset(token) {
  737. // goto LOOP
  738. // }
  739. // z.c = i
  740. // return
  741. // }
  742. func (z *bytesDecReader) skipWhitespace() (token byte) {
  743. i := z.c
  744. LOOP:
  745. // if i < uint(len(z.b)) {
  746. token = z.b[i]
  747. i++
  748. if isWhitespace(token) {
  749. goto LOOP
  750. }
  751. z.c = i
  752. return
  753. }
  754. func (z *bytesDecReader) readTo(accept *bitset256) (out []byte) {
  755. i := z.c
  756. LOOP:
  757. if i < uint(len(z.b)) {
  758. if accept.isset(z.b[i]) {
  759. i++
  760. goto LOOP
  761. }
  762. }
  763. out = z.b[z.c:i]
  764. z.c = i
  765. return // z.b[c:i]
  766. }
  767. func (z *bytesDecReader) readUntil(stop byte, includeLast bool) (out []byte) {
  768. i := z.c
  769. LOOP:
  770. // if i < uint(len(z.b)) {
  771. if z.b[i] == stop {
  772. i++
  773. if includeLast {
  774. out = z.b[z.c:i]
  775. } else {
  776. out = z.b[z.c : i-1]
  777. }
  778. // z.a -= (i - z.c)
  779. z.c = i
  780. return
  781. }
  782. i++
  783. goto LOOP
  784. // }
  785. // panic(io.EOF)
  786. }
  787. func (z *bytesDecReader) track() {
  788. z.t = z.c
  789. }
  790. func (z *bytesDecReader) stopTrack() (bs []byte) {
  791. return z.b[z.t:z.c]
  792. }
  793. // --------------
  794. type decRd struct {
  795. mtr bool // is maptype a known type?
  796. str bool // is slicetype a known type?
  797. be bool // is binary encoding
  798. js bool // is json handle
  799. jsms bool // is json handle, and MapKeyAsString
  800. cbor bool // is cbor handle
  801. bytes bool // is bytes reader
  802. bufio bool // is this a bufioDecReader?
  803. rb bytesDecReader
  804. ri *ioDecReader
  805. bi *bufioDecReader
  806. }
  807. // numread, track and stopTrack are always inlined, as they just check int fields, etc.
  808. // the if/else-if/else block is expensive to inline.
  809. // Each node of this construct costs a lot and dominates the budget.
  810. // Best to only do an if fast-path else block (so fast-path is inlined).
  811. // This is irrespective of inlineExtraCallCost set in $GOROOT/src/cmd/compile/internal/gc/inl.go
  812. //
  813. // In decRd methods below, we delegate all IO functions into their own methods.
  814. // This allows for the inlining of the common path when z.bytes=true.
  815. // Go 1.12+ supports inlining methods with up to 1 inlined function (or 2 if no other constructs).
  816. //
  817. // However, up through Go 1.13, decRd's readXXX, skip and unreadXXX methods are not inlined.
  818. // Consequently, there is no benefit to do the xxxIO methods for decRd at this time.
  819. // Instead, we have a if/else-if/else block so that IO calls do not have to jump through
  820. // a second unnecessary function call.
  821. //
  822. // If golang inlining gets better and bytesDecReader methods can be inlined,
  823. // then we can revert to using these 2 functions so the bytesDecReader
  824. // methods are inlined and the IO paths call out to a function.
  825. func (z *decRd) numread() uint {
  826. if z.bytes {
  827. return z.rb.numread()
  828. } else if z.bufio {
  829. return z.bi.numread()
  830. } else {
  831. return z.ri.numread()
  832. }
  833. }
  834. func (z *decRd) stopTrack() []byte {
  835. if z.bytes {
  836. return z.rb.stopTrack()
  837. } else if z.bufio {
  838. return z.bi.stopTrack()
  839. } else {
  840. return z.ri.stopTrack()
  841. }
  842. }
  843. func (z *decRd) track() {
  844. if z.bytes {
  845. z.rb.track()
  846. } else if z.bufio {
  847. z.bi.track()
  848. } else {
  849. z.ri.track()
  850. }
  851. }
  852. // func (z *decRd) unreadn1() {
  853. // if z.bytes {
  854. // z.rb.unreadn1()
  855. // } else if z.bufio {
  856. // z.bi.unreadn1()
  857. // } else {
  858. // z.ri.unreadn1()
  859. // }
  860. // }
  861. func (z *decRd) unreadn1() {
  862. if z.bytes {
  863. z.rb.unreadn1()
  864. } else {
  865. z.unreadn1IO()
  866. }
  867. }
  868. func (z *decRd) unreadn1IO() {
  869. if z.bufio {
  870. z.bi.unreadn1()
  871. } else {
  872. z.ri.unreadn1()
  873. }
  874. }
  875. func (z *decRd) readn(num uint8) [rwNLen]byte {
  876. if z.bytes {
  877. return z.rb.readn(num)
  878. } else if z.bufio {
  879. return z.bi.readn(num)
  880. } else {
  881. return z.ri.readn(num)
  882. }
  883. }
  884. func (z *decRd) readx(n uint) []byte {
  885. if z.bytes {
  886. return z.rb.readx(n)
  887. } else if z.bufio {
  888. return z.bi.readx(n)
  889. } else {
  890. return z.ri.readx(n)
  891. }
  892. }
  893. func (z *decRd) readb(s []byte) {
  894. if z.bytes {
  895. z.rb.readb(s)
  896. } else if z.bufio {
  897. z.bi.readb(s)
  898. } else {
  899. z.ri.readb(s)
  900. }
  901. }
  902. // func (z *decRd) readn1() uint8 {
  903. // if z.bytes {
  904. // return z.rb.readn1()
  905. // } else if z.bufio {
  906. // return z.bi.readn1()
  907. // } else {
  908. // return z.ri.readn1()
  909. // }
  910. // }
  911. func (z *decRd) readn1() uint8 {
  912. if z.bytes {
  913. // unfortunately, calling the function prevents inlining it here.
  914. // explicitly writing it here will allow it inline.
  915. // NOTE: Keep in sync with function implementation.
  916. //
  917. // return z.rb.readn1()
  918. z.rb.c++
  919. return z.rb.b[z.rb.c-1]
  920. }
  921. return z.readn1IO()
  922. }
  923. func (z *decRd) readn1IO() uint8 {
  924. if z.bufio {
  925. return z.bi.readn1()
  926. }
  927. return z.ri.readn1()
  928. }
  929. func (z *decRd) readn1eof() (uint8, bool) {
  930. if z.bytes {
  931. return z.rb.readn1eof()
  932. } else if z.bufio {
  933. return z.bi.readn1eof()
  934. } else {
  935. return z.ri.readn1eof()
  936. }
  937. }
  938. func (z *decRd) skipWhitespace() (token byte) {
  939. if z.bytes {
  940. return z.rb.skipWhitespace()
  941. } else if z.bufio {
  942. return z.bi.skipWhitespace()
  943. } else {
  944. return z.ri.skipWhitespace()
  945. }
  946. }
  947. // func (z *decRd) skip(accept *bitset256) (token byte) {
  948. // if z.bytes {
  949. // return z.rb.skip(accept)
  950. // } else if z.bufio {
  951. // return z.bi.skip(accept)
  952. // } else {
  953. // return z.ri.skip(accept)
  954. // }
  955. // }
  956. func (z *decRd) readTo(accept *bitset256) (out []byte) {
  957. if z.bytes {
  958. return z.rb.readTo(accept)
  959. } else if z.bufio {
  960. return z.bi.readTo(accept)
  961. } else {
  962. return z.ri.readTo(accept)
  963. }
  964. }
  965. func (z *decRd) readUntil(stop byte, includeLast bool) (out []byte) {
  966. if z.bytes {
  967. return z.rb.readUntil(stop, includeLast)
  968. } else if z.bufio {
  969. return z.bi.readUntil(stop, includeLast)
  970. } else {
  971. return z.ri.readUntil(stop, includeLast)
  972. }
  973. }
  974. /*
  975. func (z *decRd) track() {
  976. if z.bytes {
  977. z.rb.track()
  978. } else {
  979. z.trackIO()
  980. }
  981. }
  982. func (z *decRd) trackIO() {
  983. if z.bufio {
  984. z.bi.track()
  985. } else {
  986. z.ri.track()
  987. }
  988. }
  989. func (z *decRd) unreadn1() {
  990. if z.bytes {
  991. z.rb.unreadn1()
  992. } else {
  993. z.unreadn1IO()
  994. }
  995. }
  996. func (z *decRd) unreadn1IO() {
  997. if z.bufio {
  998. z.bi.unreadn1()
  999. } else {
  1000. z.ri.unreadn1()
  1001. }
  1002. }
  1003. func (z *decRd) readn(num uint8) [rwNLen]byte {
  1004. if z.bytes {
  1005. return z.rb.readn(num)
  1006. }
  1007. return z.readnIO(num)
  1008. }
  1009. func (z *decRd) readnIO(num uint8) [rwNLen]byte {
  1010. if z.bufio {
  1011. return z.bi.readn(num)
  1012. }
  1013. return z.ri.readn(num)
  1014. }
  1015. func (z *decRd) readx(n uint) []byte {
  1016. if z.bytes {
  1017. return z.rb.readx(n)
  1018. }
  1019. return z.readxIO(n)
  1020. }
  1021. func (z *decRd) readxIO(n uint) []byte {
  1022. if z.bufio {
  1023. return z.bi.readx(n)
  1024. }
  1025. return z.ri.readx(n)
  1026. }
  1027. func (z *decRd) readb(s []byte) {
  1028. if z.bytes {
  1029. z.rb.readb(s)
  1030. } else {
  1031. z.readbIO(s)
  1032. }
  1033. }
  1034. func (z *decRd) readbIO(s []byte) {
  1035. if z.bufio {
  1036. z.bi.readb(s)
  1037. } else {
  1038. z.ri.readb(s)
  1039. }
  1040. }
  1041. func (z *decRd) readn1() uint8 {
  1042. if z.bytes {
  1043. return z.rb.readn1()
  1044. }
  1045. return z.readn1IO()
  1046. }
  1047. func (z *decRd) readn1IO() uint8 {
  1048. if z.bufio {
  1049. return z.bi.readn1()
  1050. }
  1051. return z.ri.readn1()
  1052. }
  1053. func (z *decRd) readn1eof() (uint8, bool) {
  1054. if z.bytes {
  1055. return z.rb.readn1eof()
  1056. }
  1057. return z.readn1eofIO()
  1058. }
  1059. func (z *decRd) readn1eofIO() (uint8, bool) {
  1060. if z.bufio {
  1061. return z.bi.readn1eof()
  1062. }
  1063. return z.ri.readn1eof()
  1064. }
  1065. func (z *decRd) skip(accept *bitset256) (token byte) {
  1066. if z.bytes {
  1067. return z.rb.skip(accept)
  1068. }
  1069. return z.skipIO(accept)
  1070. }
  1071. func (z *decRd) skipIO(accept *bitset256) (token byte) {
  1072. if z.bufio {
  1073. return z.bi.skip(accept)
  1074. }
  1075. return z.ri.skip(accept)
  1076. }
  1077. func (z *decRd) readTo(accept *bitset256) (out []byte) {
  1078. if z.bytes {
  1079. return z.rb.readTo(accept)
  1080. }
  1081. return z.readToIO(accept)
  1082. }
  1083. func (z *decRd) readToIO(accept *bitset256) (out []byte) {
  1084. if z.bufio {
  1085. return z.bi.readTo(accept)
  1086. }
  1087. return z.ri.readTo(accept)
  1088. }
  1089. func (z *decRd) readUntil(stop byte, includeLast bool) (out []byte) {
  1090. if z.bytes {
  1091. return z.rb.readUntil(stop, includeLast)
  1092. }
  1093. return z.readUntilIO(stop, includeLast)
  1094. }
  1095. func (z *decRd) readUntilIO(stop byte, includeLast bool) (out []byte) {
  1096. if z.bufio {
  1097. return z.bi.readUntil(stop, includeLast)
  1098. }
  1099. return z.ri.readUntil(stop, includeLast)
  1100. }
  1101. */
  1102. var _ decReader = (*decRd)(nil)