reader.go 23 KB

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