reader.go 24 KB

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