reader.go 23 KB

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