reader.go 24 KB

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