reader.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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] // cost 7
  684. z.c++ // cost 4
  685. return
  686. }
  687. // func (z *bytesDecReader) readn1() uint8 {
  688. // z.c++
  689. // return z.b[z.c-1]
  690. // }
  691. // func (z *bytesDecReader) readn1() uint8 {
  692. // v = z.b[z.c] // cost 7
  693. // c := z.c // cost 6
  694. // z.c++ // cost 4
  695. // z.c = z.c + 1 // cost 7
  696. // return z.b[z.c-1]
  697. // }
  698. func (z *bytesDecReader) readn1eof() (v uint8, eof bool) {
  699. if z.c >= uint(len(z.b)) {
  700. eof = true
  701. } else {
  702. v = z.b[z.c]
  703. z.c++
  704. }
  705. return
  706. }
  707. func (z *bytesDecReader) readn(num uint8) (bs [rwNLen]byte) {
  708. // if z.c >= uint(len(z.b)) || z.c+uint(num) >= uint(len(z.b)) {
  709. // panic(io.EOF)
  710. // }
  711. // for bounds-check elimination, reslice z.b and ensure bs is within len
  712. // bb := z.b[z.c:][:num]
  713. bb := z.b[z.c : z.c+uint(num)]
  714. _ = bs[len(bb)-1]
  715. var i int
  716. LOOP:
  717. if i < len(bb) {
  718. bs[i] = bb[i]
  719. i++
  720. goto LOOP
  721. }
  722. z.c += uint(num)
  723. return
  724. }
  725. // func (z *bytesDecReader) skip(accept *bitset256) (token byte) {
  726. // i := z.c
  727. // LOOP:
  728. // // if i < uint(len(z.b)) {
  729. // token = z.b[i]
  730. // i++
  731. // if accept.isset(token) {
  732. // goto LOOP
  733. // }
  734. // z.c = i
  735. // return
  736. // }
  737. func (z *bytesDecReader) skipWhitespace() (token byte) {
  738. i := z.c
  739. LOOP:
  740. // if i < uint(len(z.b)) {
  741. token = z.b[i]
  742. i++
  743. if isWhitespace(token) {
  744. goto LOOP
  745. }
  746. z.c = i
  747. return
  748. }
  749. func (z *bytesDecReader) readTo(accept *bitset256) (out []byte) {
  750. i := z.c
  751. LOOP:
  752. if i < uint(len(z.b)) {
  753. if accept.isset(z.b[i]) {
  754. i++
  755. goto LOOP
  756. }
  757. }
  758. out = z.b[z.c:i]
  759. z.c = i
  760. return // z.b[c:i]
  761. }
  762. func (z *bytesDecReader) readUntil(stop byte, includeLast bool) (out []byte) {
  763. i := z.c
  764. LOOP:
  765. // if i < uint(len(z.b)) {
  766. if z.b[i] == stop {
  767. i++
  768. if includeLast {
  769. out = z.b[z.c:i]
  770. } else {
  771. out = z.b[z.c : i-1]
  772. }
  773. // z.a -= (i - z.c)
  774. z.c = i
  775. return
  776. }
  777. i++
  778. goto LOOP
  779. // }
  780. // panic(io.EOF)
  781. }
  782. func (z *bytesDecReader) track() {
  783. z.t = z.c
  784. }
  785. func (z *bytesDecReader) stopTrack() (bs []byte) {
  786. return z.b[z.t:z.c]
  787. }
  788. // --------------
  789. type decRd struct {
  790. mtr bool // is maptype a known type?
  791. str bool // is slicetype a known type?
  792. be bool // is binary encoding
  793. js bool // is json handle
  794. jsms bool // is json handle, and MapKeyAsString
  795. cbor bool // is cbor handle
  796. bytes bool // is bytes reader
  797. bufio bool // is this a bufioDecReader?
  798. rb bytesDecReader
  799. ri *ioDecReader
  800. bi *bufioDecReader
  801. }
  802. // numread, track and stopTrack are always inlined, as they just check int fields, etc.
  803. // the if/else-if/else block is expensive to inline.
  804. // Each node of this construct costs a lot and dominates the budget.
  805. // Best to only do an if fast-path else block (so fast-path is inlined).
  806. // This is irrespective of inlineExtraCallCost set in $GOROOT/src/cmd/compile/internal/gc/inl.go
  807. //
  808. // In decRd methods below, we delegate all IO functions into their own methods.
  809. // This allows for the inlining of the common path when z.bytes=true.
  810. // Go 1.12+ supports inlining methods with up to 1 inlined function (or 2 if no other constructs).
  811. //
  812. // However, up through Go 1.13, decRd's readXXX, skip and unreadXXX methods are not inlined.
  813. // Consequently, there is no benefit to do the xxxIO methods for decRd at this time.
  814. // Instead, we have a if/else-if/else block so that IO calls do not have to jump through
  815. // a second unnecessary function call.
  816. //
  817. // If golang inlining gets better and bytesDecReader methods can be inlined,
  818. // then we can revert to using these 2 functions so the bytesDecReader
  819. // methods are inlined and the IO paths call out to a function.
  820. func (z *decRd) numread() uint {
  821. if z.bytes {
  822. return z.rb.numread()
  823. } else if z.bufio {
  824. return z.bi.numread()
  825. } else {
  826. return z.ri.numread()
  827. }
  828. }
  829. func (z *decRd) stopTrack() []byte {
  830. if z.bytes {
  831. return z.rb.stopTrack()
  832. } else if z.bufio {
  833. return z.bi.stopTrack()
  834. } else {
  835. return z.ri.stopTrack()
  836. }
  837. }
  838. func (z *decRd) track() {
  839. if z.bytes {
  840. z.rb.track()
  841. } else if z.bufio {
  842. z.bi.track()
  843. } else {
  844. z.ri.track()
  845. }
  846. }
  847. // func (z *decRd) unreadn1() {
  848. // if z.bytes {
  849. // z.rb.unreadn1()
  850. // } else if z.bufio {
  851. // z.bi.unreadn1()
  852. // } else {
  853. // z.ri.unreadn1()
  854. // }
  855. // }
  856. func (z *decRd) unreadn1() {
  857. if z.bytes {
  858. z.rb.unreadn1()
  859. } else {
  860. z.unreadn1IO()
  861. }
  862. }
  863. func (z *decRd) unreadn1IO() {
  864. if z.bufio {
  865. z.bi.unreadn1()
  866. } else {
  867. z.ri.unreadn1()
  868. }
  869. }
  870. func (z *decRd) readn(num uint8) [rwNLen]byte {
  871. if z.bytes {
  872. return z.rb.readn(num)
  873. } else if z.bufio {
  874. return z.bi.readn(num)
  875. } else {
  876. return z.ri.readn(num)
  877. }
  878. }
  879. func (z *decRd) readx(n uint) []byte {
  880. if z.bytes {
  881. return z.rb.readx(n)
  882. } else if z.bufio {
  883. return z.bi.readx(n)
  884. } else {
  885. return z.ri.readx(n)
  886. }
  887. }
  888. func (z *decRd) readb(s []byte) {
  889. if z.bytes {
  890. z.rb.readb(s)
  891. } else if z.bufio {
  892. z.bi.readb(s)
  893. } else {
  894. z.ri.readb(s)
  895. }
  896. }
  897. // func (z *decRd) readn1() uint8 {
  898. // if z.bytes {
  899. // return z.rb.readn1()
  900. // } else if z.bufio {
  901. // return z.bi.readn1()
  902. // } else {
  903. // return z.ri.readn1()
  904. // }
  905. // }
  906. func (z *decRd) readn1() uint8 {
  907. if z.bytes {
  908. // unfortunately, calling the function prevents inlining it here.
  909. // explicitly writing it here will allow it inline.
  910. // NOTE: Keep in sync with function implementation.
  911. //
  912. // return z.rb.readn1()
  913. z.rb.c++
  914. return z.rb.b[z.rb.c-1]
  915. }
  916. return z.readn1IO()
  917. }
  918. func (z *decRd) readn1IO() uint8 {
  919. if z.bufio {
  920. return z.bi.readn1()
  921. }
  922. return z.ri.readn1()
  923. }
  924. func (z *decRd) readn1eof() (uint8, bool) {
  925. if z.bytes {
  926. return z.rb.readn1eof()
  927. } else if z.bufio {
  928. return z.bi.readn1eof()
  929. } else {
  930. return z.ri.readn1eof()
  931. }
  932. }
  933. func (z *decRd) skipWhitespace() (token byte) {
  934. if z.bytes {
  935. return z.rb.skipWhitespace()
  936. } else if z.bufio {
  937. return z.bi.skipWhitespace()
  938. } else {
  939. return z.ri.skipWhitespace()
  940. }
  941. }
  942. // func (z *decRd) skip(accept *bitset256) (token byte) {
  943. // if z.bytes {
  944. // return z.rb.skip(accept)
  945. // } else if z.bufio {
  946. // return z.bi.skip(accept)
  947. // } else {
  948. // return z.ri.skip(accept)
  949. // }
  950. // }
  951. func (z *decRd) readTo(accept *bitset256) (out []byte) {
  952. if z.bytes {
  953. return z.rb.readTo(accept)
  954. } else if z.bufio {
  955. return z.bi.readTo(accept)
  956. } else {
  957. return z.ri.readTo(accept)
  958. }
  959. }
  960. func (z *decRd) readUntil(stop byte, includeLast bool) (out []byte) {
  961. if z.bytes {
  962. return z.rb.readUntil(stop, includeLast)
  963. } else if z.bufio {
  964. return z.bi.readUntil(stop, includeLast)
  965. } else {
  966. return z.ri.readUntil(stop, includeLast)
  967. }
  968. }
  969. /*
  970. func (z *decRd) track() {
  971. if z.bytes {
  972. z.rb.track()
  973. } else {
  974. z.trackIO()
  975. }
  976. }
  977. func (z *decRd) trackIO() {
  978. if z.bufio {
  979. z.bi.track()
  980. } else {
  981. z.ri.track()
  982. }
  983. }
  984. func (z *decRd) unreadn1() {
  985. if z.bytes {
  986. z.rb.unreadn1()
  987. } else {
  988. z.unreadn1IO()
  989. }
  990. }
  991. func (z *decRd) unreadn1IO() {
  992. if z.bufio {
  993. z.bi.unreadn1()
  994. } else {
  995. z.ri.unreadn1()
  996. }
  997. }
  998. func (z *decRd) readn(num uint8) [rwNLen]byte {
  999. if z.bytes {
  1000. return z.rb.readn(num)
  1001. }
  1002. return z.readnIO(num)
  1003. }
  1004. func (z *decRd) readnIO(num uint8) [rwNLen]byte {
  1005. if z.bufio {
  1006. return z.bi.readn(num)
  1007. }
  1008. return z.ri.readn(num)
  1009. }
  1010. func (z *decRd) readx(n uint) []byte {
  1011. if z.bytes {
  1012. return z.rb.readx(n)
  1013. }
  1014. return z.readxIO(n)
  1015. }
  1016. func (z *decRd) readxIO(n uint) []byte {
  1017. if z.bufio {
  1018. return z.bi.readx(n)
  1019. }
  1020. return z.ri.readx(n)
  1021. }
  1022. func (z *decRd) readb(s []byte) {
  1023. if z.bytes {
  1024. z.rb.readb(s)
  1025. } else {
  1026. z.readbIO(s)
  1027. }
  1028. }
  1029. func (z *decRd) readbIO(s []byte) {
  1030. if z.bufio {
  1031. z.bi.readb(s)
  1032. } else {
  1033. z.ri.readb(s)
  1034. }
  1035. }
  1036. func (z *decRd) readn1() uint8 {
  1037. if z.bytes {
  1038. return z.rb.readn1()
  1039. }
  1040. return z.readn1IO()
  1041. }
  1042. func (z *decRd) readn1IO() uint8 {
  1043. if z.bufio {
  1044. return z.bi.readn1()
  1045. }
  1046. return z.ri.readn1()
  1047. }
  1048. func (z *decRd) skip(accept *bitset256) (token byte) {
  1049. if z.bytes {
  1050. return z.rb.skip(accept)
  1051. }
  1052. return z.skipIO(accept)
  1053. }
  1054. func (z *decRd) skipIO(accept *bitset256) (token byte) {
  1055. if z.bufio {
  1056. return z.bi.skip(accept)
  1057. }
  1058. return z.ri.skip(accept)
  1059. }
  1060. func (z *decRd) readTo(accept *bitset256) (out []byte) {
  1061. if z.bytes {
  1062. return z.rb.readTo(accept)
  1063. }
  1064. return z.readToIO(accept)
  1065. }
  1066. func (z *decRd) readToIO(accept *bitset256) (out []byte) {
  1067. if z.bufio {
  1068. return z.bi.readTo(accept)
  1069. }
  1070. return z.ri.readTo(accept)
  1071. }
  1072. func (z *decRd) readUntil(stop byte, includeLast bool) (out []byte) {
  1073. if z.bytes {
  1074. return z.rb.readUntil(stop, includeLast)
  1075. }
  1076. return z.readUntilIO(stop, includeLast)
  1077. }
  1078. func (z *decRd) readUntilIO(stop byte, includeLast bool) (out []byte) {
  1079. if z.bufio {
  1080. return z.bi.readUntil(stop, includeLast)
  1081. }
  1082. return z.ri.readUntil(stop, includeLast)
  1083. }
  1084. */
  1085. var _ decReader = (*decRd)(nil)