reader.go 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  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 [rwNLen]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, includeLast bool) (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 [rwNLen]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, includeLast bool) []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. if includeLast {
  294. return z.bufr
  295. }
  296. return z.bufr[:len(z.bufr)-1]
  297. }
  298. goto LOOP
  299. }
  300. //go:noinline
  301. func (z *ioDecReader) unreadn1() {
  302. err := z.br.UnreadByte()
  303. if err != nil {
  304. panic(err)
  305. }
  306. z.n--
  307. if z.trb {
  308. if l := len(z.tr) - 1; l >= 0 {
  309. z.tr = z.tr[:l]
  310. }
  311. }
  312. }
  313. // ------------------------------------
  314. type bufioDecReader struct {
  315. ioDecReaderCommon
  316. // _ uint64 // padding (cache-aligned)
  317. c uint // cursor
  318. buf []byte
  319. // bp bytesBufSlicePooler
  320. // err error
  321. }
  322. func (z *bufioDecReader) reset(r io.Reader, bufsize int, blist *bytesFreelist) {
  323. z.ioDecReaderCommon.reset(r, blist)
  324. z.c = 0
  325. if cap(z.buf) < bufsize {
  326. // z.bp.get(bufsize)
  327. // z.buf = make([]byte, 0, bufsize)
  328. z.buf = blist.get(bufsize)
  329. }
  330. z.buf = z.buf[:0]
  331. }
  332. // func (z *bufioDecReader) release() {
  333. // z.ioDecReaderCommon.release()
  334. // // z.bp.end()
  335. // }
  336. func (z *bufioDecReader) readb(p []byte) {
  337. var n = uint(copy(p, z.buf[z.c:]))
  338. z.n += n
  339. z.c += n
  340. if len(p) == int(n) {
  341. if z.trb {
  342. z.tr = append(z.tr, p...)
  343. }
  344. } else {
  345. z.readbFill(p, n)
  346. }
  347. }
  348. func (z *bufioDecReader) readbFill(p0 []byte, n uint) {
  349. // at this point, there's nothing in z.buf to read (z.buf is fully consumed)
  350. p := p0[n:]
  351. var n2 uint
  352. var err error
  353. if len(p) > cap(z.buf) {
  354. n2, err = decReadFull(z.r, p)
  355. if err != nil {
  356. panic(err)
  357. }
  358. n += n2
  359. z.n += n2
  360. // always keep last byte in z.buf
  361. z.buf = z.buf[:1]
  362. z.buf[0] = p[len(p)-1]
  363. z.c = 1
  364. if z.trb {
  365. z.tr = append(z.tr, p0[:n]...)
  366. }
  367. return
  368. }
  369. // z.c is now 0, and len(p) <= cap(z.buf)
  370. LOOP:
  371. // for len(p) > 0 && z.err == nil {
  372. if len(p) > 0 {
  373. z.buf = z.buf[0:cap(z.buf)]
  374. var n1 int
  375. n1, err = z.r.Read(z.buf)
  376. n2 = uint(n1)
  377. if n2 == 0 && err != nil {
  378. panic(err)
  379. }
  380. z.buf = z.buf[:n2]
  381. n2 = uint(copy(p, z.buf))
  382. z.c = n2
  383. n += n2
  384. z.n += n2
  385. p = p[n2:]
  386. goto LOOP
  387. }
  388. if z.c == 0 {
  389. z.buf = z.buf[:1]
  390. z.buf[0] = p[len(p)-1]
  391. z.c = 1
  392. }
  393. if z.trb {
  394. z.tr = append(z.tr, p0[:n]...)
  395. }
  396. }
  397. func (z *bufioDecReader) readn1() (b byte) {
  398. // fast-path, so we elide calling into Read() most of the time
  399. if z.c < uint(len(z.buf)) {
  400. b = z.buf[z.c]
  401. z.c++
  402. z.n++
  403. if z.trb {
  404. z.tr = append(z.tr, b)
  405. }
  406. } else { // meaning z.c == len(z.buf) or greater ... so need to fill
  407. z.readbFill(z.b[:1], 0)
  408. b = z.b[0]
  409. }
  410. return
  411. }
  412. func (z *bufioDecReader) unreadn1() {
  413. if z.c == 0 {
  414. panic(errDecUnreadByteNothingToRead)
  415. }
  416. z.c--
  417. z.n--
  418. if z.trb {
  419. z.tr = z.tr[:len(z.tr)-1]
  420. }
  421. }
  422. func (z *bufioDecReader) readn(num uint8) (bs [rwNLen]byte) {
  423. z.readb(bs[:num])
  424. // copy(bs[:], z.readx(uint(num)))
  425. return
  426. }
  427. func (z *bufioDecReader) readx(n uint) (bs []byte) {
  428. if n == 0 {
  429. // return
  430. } else if z.c+n <= uint(len(z.buf)) {
  431. bs = z.buf[z.c : z.c+n]
  432. z.n += n
  433. z.c += n
  434. if z.trb {
  435. z.tr = append(z.tr, bs...)
  436. }
  437. } else {
  438. bs = make([]byte, n)
  439. // n no longer used - can reuse
  440. n = uint(copy(bs, z.buf[z.c:]))
  441. z.n += n
  442. z.c += n
  443. z.readbFill(bs, n)
  444. }
  445. return
  446. }
  447. // func (z *bufioDecReader) doTrack(y uint) {
  448. // z.tr = append(z.tr, z.buf[z.c:y]...) // cost=14???
  449. // }
  450. // func (z *bufioDecReader) skipLoopFn(i uint) {
  451. // z.n += (i - z.c) - 1
  452. // i++
  453. // if z.trb {
  454. // // z.tr = append(z.tr, z.buf[z.c:i]...)
  455. // z.doTrack(i)
  456. // }
  457. // z.c = i
  458. // }
  459. func (z *bufioDecReader) skip(accept *bitset256) (token byte) {
  460. // token, _ = z.search(nil, accept, 0, 1); return
  461. // for i := z.c; i < len(z.buf); i++ {
  462. // if token = z.buf[i]; !accept.isset(token) {
  463. // z.skipLoopFn(i)
  464. // return
  465. // }
  466. // }
  467. i := z.c
  468. LOOP:
  469. if i < uint(len(z.buf)) {
  470. // inline z.skipLoopFn(i) and refactor, so cost is within inline budget
  471. token = z.buf[i]
  472. i++
  473. if accept.isset(token) {
  474. goto LOOP
  475. }
  476. z.n += i - 2 - z.c
  477. if z.trb {
  478. z.tr = append(z.tr, z.buf[z.c:i]...) // z.doTrack(i)
  479. }
  480. z.c = i
  481. return
  482. }
  483. return z.skipFill(accept)
  484. }
  485. func (z *bufioDecReader) skipFill(accept *bitset256) (token byte) {
  486. // defer func() { xdebugf("skipFill '%c'", token) }()
  487. z.n += uint(len(z.buf)) - z.c
  488. if z.trb {
  489. z.tr = append(z.tr, z.buf[z.c:]...)
  490. }
  491. var i, n2 int
  492. var err error
  493. for {
  494. z.c = 0
  495. z.buf = z.buf[0:cap(z.buf)]
  496. n2, err = z.r.Read(z.buf)
  497. if n2 == 0 && err != nil {
  498. panic(err)
  499. }
  500. z.buf = z.buf[:n2]
  501. for i, token = range z.buf {
  502. // if !accept.isset(token) {
  503. if accept.check(token) == 0 {
  504. z.n += (uint(i) - z.c) - 1
  505. z.loopFn(uint(i + 1))
  506. return
  507. }
  508. }
  509. // for i := 0; i < n2; i++ {
  510. // if token = z.buf[i]; !accept.isset(token) {
  511. // z.skipLoopFn(i)
  512. // return
  513. // }
  514. // }
  515. z.n += uint(n2)
  516. if z.trb {
  517. z.tr = append(z.tr, z.buf...)
  518. }
  519. }
  520. }
  521. // func (z *bufioDecReader) readLoopFn(i uint, out0 []byte) (out []byte) {
  522. // out = appendPool(out0, z.buf[z.c:i]...)
  523. // z.loopFn(i)
  524. // return
  525. // }
  526. func (z *bufioDecReader) loopFn(i uint) {
  527. if z.trb {
  528. z.tr = append(z.tr, z.buf[z.c:i]...) // z.doTrack(i)
  529. }
  530. z.c = i
  531. }
  532. // func (z *bufioDecReader) readToLoopFn(i uint, out0 []byte) (out []byte) {
  533. // // out0 is never nil
  534. // z.n += (i - z.c) - 1
  535. // return z.readLoopFn(i, out0)
  536. // }
  537. func (z *bufioDecReader) readTo(accept *bitset256) (out []byte) {
  538. // defer func() { xdebug2f("bufio: readTo: %s", out) }()
  539. // _, out = z.search(in, accept, 0, 2); return
  540. // for i := z.c; i < len(z.buf); i++ {
  541. // if !accept.isset(z.buf[i]) {
  542. // return z.readToLoopFn(i, nil)
  543. // }
  544. // }
  545. i := z.c
  546. LOOP:
  547. if i < uint(len(z.buf)) {
  548. // if !accept.isset(z.buf[i]) {
  549. if accept.check(z.buf[i]) == 0 {
  550. // return z.readToLoopFn(i, nil)
  551. // inline readToLoopFn here (for performance)
  552. z.n += (i - z.c) - 1
  553. out = z.buf[z.c:i]
  554. if z.trb {
  555. z.tr = append(z.tr, z.buf[z.c:i]...) // z.doTrack(i)
  556. }
  557. z.c = i
  558. return
  559. }
  560. i++
  561. goto LOOP
  562. }
  563. return z.readToFill(accept)
  564. }
  565. func (z *bufioDecReader) readToFill(accept *bitset256) []byte {
  566. z.bufr = z.blist.check(z.bufr, 256)[:0]
  567. z.n += uint(len(z.buf)) - z.c
  568. z.bufr = append(z.bufr, z.buf[z.c:]...)
  569. if z.trb {
  570. z.tr = append(z.tr, z.buf[z.c:]...)
  571. }
  572. var n2 int
  573. var err error
  574. for {
  575. z.c = 0
  576. z.buf = z.buf[:cap(z.buf)]
  577. n2, err = z.r.Read(z.buf)
  578. if n2 == 0 && err != nil {
  579. if err == io.EOF {
  580. return z.bufr // readTo should read until it matches or end is reached
  581. }
  582. panic(err)
  583. }
  584. z.buf = z.buf[:n2]
  585. for i, token := range z.buf {
  586. // if !accept.isset(token) {
  587. if accept.check(token) == 0 {
  588. z.n += (uint(i) - z.c) - 1
  589. z.bufr = append(z.bufr, z.buf[z.c:i]...)
  590. z.loopFn(uint(i))
  591. return z.bufr
  592. }
  593. }
  594. // for i := 0; i < n2; i++ {
  595. // if !accept.isset(z.buf[i]) {
  596. // return z.readToLoopFn(i, out)
  597. // }
  598. // }
  599. z.bufr = append(z.bufr, z.buf...)
  600. z.n += uint(n2)
  601. if z.trb {
  602. z.tr = append(z.tr, z.buf...)
  603. }
  604. }
  605. }
  606. // func (z *bufioDecReader) readUntilLoopFn(i uint, out0 []byte) (out []byte) {
  607. // z.n += (i - z.c) - 1
  608. // return z.readLoopFn(i+1, out0)
  609. // }
  610. func (z *bufioDecReader) readUntil(stop byte, includeLast bool) (out []byte) {
  611. // defer func() { xdebug2f("bufio: readUntil: %s", out) }()
  612. // _, out = z.search(in, nil, stop, 4); return
  613. // for i := z.c; i < len(z.buf); i++ {
  614. // if z.buf[i] == stop {
  615. // return z.readUntilLoopFn(i, nil)
  616. // }
  617. // }
  618. i := z.c
  619. LOOP:
  620. if i < uint(len(z.buf)) {
  621. if z.buf[i] == stop {
  622. // inline readUntilLoopFn
  623. // return z.readUntilLoopFn(i, nil)
  624. z.n += (i - z.c) - 1
  625. i++
  626. out = z.buf[z.c:i]
  627. if z.trb {
  628. z.tr = append(z.tr, z.buf[z.c:i]...) // z.doTrack(i)
  629. }
  630. z.c = i
  631. goto FINISH
  632. }
  633. i++
  634. goto LOOP
  635. }
  636. out = z.readUntilFill(stop)
  637. FINISH:
  638. if includeLast {
  639. return
  640. }
  641. return out[:len(out)-1]
  642. }
  643. func (z *bufioDecReader) readUntilFill(stop byte) []byte {
  644. z.bufr = z.blist.check(z.bufr, 256)[:0]
  645. z.n += uint(len(z.buf)) - z.c
  646. z.bufr = append(z.bufr, z.buf[z.c:]...)
  647. if z.trb {
  648. z.tr = append(z.tr, z.buf[z.c:]...)
  649. }
  650. for {
  651. z.c = 0
  652. z.buf = z.buf[0:cap(z.buf)]
  653. n1, err := z.r.Read(z.buf)
  654. if n1 == 0 && err != nil {
  655. panic(err)
  656. }
  657. n2 := uint(n1)
  658. z.buf = z.buf[:n2]
  659. for i, token := range z.buf {
  660. if token == stop {
  661. z.n += (uint(i) - z.c) - 1
  662. z.bufr = append(z.bufr, z.buf[z.c:i+1]...)
  663. z.loopFn(uint(i + 1))
  664. return z.bufr
  665. }
  666. }
  667. // for i := 0; i < n2; i++ {
  668. // if z.buf[i] == stop {
  669. // return z.readUntilLoopFn(i, out)
  670. // }
  671. // }
  672. z.bufr = append(z.bufr, z.buf...)
  673. z.n += n2
  674. if z.trb {
  675. z.tr = append(z.tr, z.buf...)
  676. }
  677. }
  678. }
  679. // ------------------------------------
  680. // bytesDecReader is a decReader that reads off a byte slice with zero copying
  681. type bytesDecReader struct {
  682. b []byte // data
  683. c uint // cursor
  684. t uint // track start
  685. // a int // available
  686. }
  687. func (z *bytesDecReader) reset(in []byte) {
  688. z.b = in
  689. // z.a = len(in)
  690. z.c = 0
  691. z.t = 0
  692. }
  693. func (z *bytesDecReader) numread() uint {
  694. return z.c
  695. }
  696. func (z *bytesDecReader) unreadn1() {
  697. if z.c == 0 || len(z.b) == 0 {
  698. panic(errBytesDecReaderCannotUnread)
  699. }
  700. z.c--
  701. // z.a++
  702. }
  703. func (z *bytesDecReader) readx(n uint) (bs []byte) {
  704. // slicing from a non-constant start position is more expensive,
  705. // as more computation is required to decipher the pointer start position.
  706. // However, we do it only once, and it's better than reslicing both z.b and return value.
  707. // if n <= 0 {
  708. // } else if z.a == 0 {
  709. // panic(io.EOF)
  710. // } else if n > z.a {
  711. // panic(io.ErrUnexpectedEOF)
  712. // } else {
  713. // c0 := z.c
  714. // z.c = c0 + n
  715. // z.a = z.a - n
  716. // bs = z.b[c0:z.c]
  717. // }
  718. // return
  719. // if n == 0 {
  720. // return
  721. // }
  722. z.c += n
  723. // if z.c > uint(len(z.b)) {
  724. // z.c = uint(len(z.b))
  725. // panic(io.EOF)
  726. // }
  727. // bs = z.b[z.c-n : z.c]
  728. // return
  729. return z.b[z.c-n : z.c]
  730. // if n == 0 {
  731. // } else if z.c+n > uint(len(z.b)) {
  732. // z.c = uint(len(z.b))
  733. // panic(io.EOF)
  734. // } else {
  735. // z.c += n
  736. // bs = z.b[z.c-n : z.c]
  737. // }
  738. // return
  739. // if n == 0 {
  740. // return
  741. // }
  742. // if z.c == uint(len(z.b)) {
  743. // panic(io.EOF)
  744. // }
  745. // if z.c+n > uint(len(z.b)) {
  746. // panic(io.ErrUnexpectedEOF)
  747. // }
  748. // // z.a -= n
  749. // z.c += n
  750. // return z.b[z.c-n : z.c]
  751. }
  752. func (z *bytesDecReader) readb(bs []byte) {
  753. copy(bs, z.readx(uint(len(bs))))
  754. }
  755. func (z *bytesDecReader) readn1() (v uint8) {
  756. // if z.c >= uint(len(z.b)) {
  757. // panic(io.EOF)
  758. // }
  759. v = z.b[z.c]
  760. z.c++
  761. // v = z.b[z.c] // cost = 7
  762. // z.c++ // cost = 4
  763. // z.a--
  764. return
  765. }
  766. func (z *bytesDecReader) readn(num uint8) (bs [rwNLen]byte) {
  767. // if z.c >= uint(len(z.b)) || z.c+uint(num) >= uint(len(z.b)) {
  768. // panic(io.EOF)
  769. // }
  770. copy(bs[:], z.b[z.c:z.c+uint(num)])
  771. z.c += uint(num)
  772. return
  773. }
  774. // func (z *bytesDecReader) readn4() (bs [4]byte) {
  775. // // if z.c+3 >= uint(len(z.b)) {
  776. // // panic(io.EOF)
  777. // // }
  778. // // copy(bs[:], z.b[z.c:z.c+4])
  779. // bs[3] = z.b[z.c+3]
  780. // bs[2] = z.b[z.c+2]
  781. // bs[1] = z.b[z.c+1]
  782. // bs[0] = z.b[z.c]
  783. // z.c += 4
  784. // return
  785. // }
  786. // func (z *bytesDecReader) readn1eof() (v uint8, eof bool) {
  787. // if z.a == 0 {
  788. // eof = true
  789. // return
  790. // }
  791. // v = z.b[z.c]
  792. // z.c++
  793. // z.a--
  794. // return
  795. // }
  796. func (z *bytesDecReader) skip(accept *bitset256) (token byte) {
  797. i := z.c
  798. // if i == len(z.b) {
  799. // goto END
  800. // // panic(io.EOF)
  801. // }
  802. // Replace loop with goto construct, so that this can be inlined
  803. // for i := z.c; i < blen; i++ {
  804. // if !accept.isset(z.b[i]) {
  805. // token = z.b[i]
  806. // i++
  807. // z.a -= (i - z.c)
  808. // z.c = i
  809. // return
  810. // }
  811. // }
  812. // i := z.c
  813. LOOP:
  814. // if i < uint(len(z.b)) {
  815. token = z.b[i]
  816. i++
  817. if accept.isset(token) {
  818. goto LOOP
  819. }
  820. // z.a -= (i - z.c)
  821. z.c = i
  822. return
  823. // }
  824. // panic(io.EOF)
  825. // END:
  826. // // z.a = 0
  827. // z.c = blen
  828. // return
  829. }
  830. func (z *bytesDecReader) readTo(accept *bitset256) (out []byte) {
  831. i := z.c
  832. // if i == uint(len(z.b)) {
  833. // panic(io.EOF)
  834. // }
  835. // Replace loop with goto construct, so that this can be inlined
  836. // for i := z.c; i < blen; i++ {
  837. // if !accept.isset(z.b[i]) {
  838. // out = z.b[z.c:i]
  839. // z.a -= (i - z.c)
  840. // z.c = i
  841. // return
  842. // }
  843. // }
  844. // out = z.b[z.c:]
  845. // z.a, z.c = 0, blen
  846. // return
  847. // i := z.c
  848. // LOOP:
  849. // if i < blen {
  850. // if accept.isset(z.b[i]) {
  851. // i++
  852. // goto LOOP
  853. // }
  854. // out = z.b[z.c:i]
  855. // z.a -= (i - z.c)
  856. // z.c = i
  857. // return
  858. // }
  859. // out = z.b[z.c:]
  860. // // z.a, z.c = 0, blen
  861. // z.a = 0
  862. // z.c = blen
  863. // return
  864. // c := i
  865. LOOP:
  866. if i < uint(len(z.b)) {
  867. if accept.isset(z.b[i]) {
  868. i++
  869. goto LOOP
  870. }
  871. }
  872. out = z.b[z.c:i]
  873. // z.a -= (i - z.c)
  874. z.c = i
  875. return // z.b[c:i]
  876. // z.c, i = i, z.c
  877. // return z.b[i:z.c]
  878. }
  879. func (z *bytesDecReader) readUntil(stop byte, includeLast bool) (out []byte) {
  880. i := z.c
  881. // if i == len(z.b) {
  882. // panic(io.EOF)
  883. // }
  884. // Replace loop with goto construct, so that this can be inlined
  885. // for i := z.c; i < blen; i++ {
  886. // if z.b[i] == stop {
  887. // i++
  888. // out = z.b[z.c:i]
  889. // z.a -= (i - z.c)
  890. // z.c = i
  891. // return
  892. // }
  893. // }
  894. LOOP:
  895. // if i < uint(len(z.b)) {
  896. if z.b[i] == stop {
  897. i++
  898. if includeLast {
  899. out = z.b[z.c:i]
  900. } else {
  901. out = z.b[z.c : i-1]
  902. }
  903. // z.a -= (i - z.c)
  904. z.c = i
  905. return
  906. }
  907. i++
  908. goto LOOP
  909. // }
  910. // panic(io.EOF)
  911. // z.a = 0
  912. // z.c = blen
  913. }
  914. func (z *bytesDecReader) track() {
  915. z.t = z.c
  916. }
  917. func (z *bytesDecReader) stopTrack() (bs []byte) {
  918. return z.b[z.t:z.c]
  919. }
  920. // --------------
  921. type decRd struct {
  922. // esep bool // has elem separators
  923. mtr bool // is maptype a known type?
  924. str bool // is slicetype a known type?
  925. be bool // is binary encoding
  926. js bool // is json handle
  927. jsms bool // is json handle, and MapKeyAsString
  928. // typ entryType
  929. bytes bool // is bytes reader
  930. bufio bool // is this a bufioDecReader?
  931. rb bytesDecReader
  932. ri *ioDecReader
  933. bi *bufioDecReader
  934. }
  935. // numread, track and stopTrack are always inlined, as they just check int fields, etc.
  936. // the if/else-if/else block is expensive to inline.
  937. // Each node of this construct costs a lot and dominates the budget.
  938. // Best to only do an if fast-path else block (so fast-path is inlined).
  939. // This is irrespective of inlineExtraCallCost set in $GOROOT/src/cmd/compile/internal/gc/inl.go
  940. //
  941. // In decRd methods below, we delegate all IO functions into their own methods.
  942. // This allows for the inlining of the common path when z.bytes=true.
  943. // Go 1.12+ supports inlining methods with up to 1 inlined function (or 2 if no other constructs).
  944. //
  945. // However, up through Go 1.13, decRd's readXXX, skip and unreadXXX methods are not inlined.
  946. // Consequently, there is no benefit to do the xxxIO methods for decRd at this time.
  947. // Instead, we have a if/else-if/else block so that IO calls do not have to jump through
  948. // a second unnecessary function call.
  949. // func (z *decRd) release() {
  950. // if z.bytes {
  951. // } else if z.bufio {
  952. // z.bi.release()
  953. // } else {
  954. // z.ri.release()
  955. // }
  956. // }
  957. func (z *decRd) numread() uint {
  958. if z.bytes {
  959. return z.rb.numread()
  960. } else if z.bufio {
  961. return z.bi.numread()
  962. } else {
  963. return z.ri.numread()
  964. }
  965. }
  966. func (z *decRd) track() {
  967. if z.bytes {
  968. z.rb.track()
  969. } else if z.bufio {
  970. z.bi.track()
  971. } else {
  972. z.ri.track()
  973. }
  974. }
  975. func (z *decRd) stopTrack() []byte {
  976. if z.bytes {
  977. return z.rb.stopTrack()
  978. } else if z.bufio {
  979. return z.bi.stopTrack()
  980. } else {
  981. return z.ri.stopTrack()
  982. }
  983. }
  984. func (z *decRd) unreadn1() {
  985. if z.bytes {
  986. z.rb.unreadn1()
  987. } else if z.bufio {
  988. z.bi.unreadn1()
  989. } else {
  990. z.ri.unreadn1() // not inlined
  991. }
  992. }
  993. func (z *decRd) readn(num uint8) [rwNLen]byte {
  994. if z.bytes {
  995. return z.rb.readn(num)
  996. }
  997. if z.bufio {
  998. return z.bi.readn(num)
  999. }
  1000. return z.ri.readn(num)
  1001. }
  1002. func (z *decRd) readx(n uint) []byte {
  1003. if z.bytes {
  1004. return z.rb.readx(n)
  1005. }
  1006. if z.bufio {
  1007. return z.bi.readx(n)
  1008. }
  1009. return z.ri.readx(n)
  1010. }
  1011. func (z *decRd) readb(s []byte) {
  1012. if z.bytes {
  1013. z.rb.readb(s)
  1014. } else if z.bufio {
  1015. z.bi.readb(s)
  1016. } else {
  1017. z.ri.readb(s)
  1018. }
  1019. }
  1020. func (z *decRd) readn1() uint8 {
  1021. if z.bytes {
  1022. return z.rb.readn1()
  1023. }
  1024. if z.bufio {
  1025. return z.bi.readn1()
  1026. }
  1027. return z.ri.readn1()
  1028. }
  1029. func (z *decRd) skip(accept *bitset256) (token byte) {
  1030. if z.bytes {
  1031. return z.rb.skip(accept)
  1032. }
  1033. if z.bufio {
  1034. return z.bi.skip(accept)
  1035. }
  1036. return z.ri.skip(accept)
  1037. }
  1038. func (z *decRd) readTo(accept *bitset256) (out []byte) {
  1039. if z.bytes {
  1040. return z.rb.readTo(accept)
  1041. }
  1042. if z.bufio {
  1043. return z.bi.readTo(accept)
  1044. }
  1045. return z.ri.readTo(accept)
  1046. }
  1047. func (z *decRd) readUntil(stop byte, includeLast bool) (out []byte) {
  1048. if z.bytes {
  1049. return z.rb.readUntil(stop, includeLast)
  1050. }
  1051. if z.bufio {
  1052. return z.bi.readUntil(stop, includeLast)
  1053. }
  1054. return z.ri.readUntil(stop, includeLast)
  1055. }
  1056. /*
  1057. func (z *decRd) unreadn1() {
  1058. if z.bytes {
  1059. z.rb.unreadn1()
  1060. } else {
  1061. z.unreadn1IO()
  1062. }
  1063. }
  1064. func (z *decRd) unreadn1IO() {
  1065. if z.bufio {
  1066. z.bi.unreadn1()
  1067. } else {
  1068. z.ri.unreadn1()
  1069. }
  1070. }
  1071. func (z *decRd) readn(num uint8) [rwNLen]byte {
  1072. if z.bytes {
  1073. return z.rb.readn(num)
  1074. }
  1075. return z.readnIO(num)
  1076. }
  1077. func (z *decRd) readnIO(num uint8) [rwNLen]byte {
  1078. if z.bufio {
  1079. return z.bi.readn(num)
  1080. }
  1081. return z.ri.readn(num)
  1082. }
  1083. func (z *decRd) readx(n uint) []byte {
  1084. if z.bytes {
  1085. return z.rb.readx(n)
  1086. }
  1087. return z.readxIO(n)
  1088. }
  1089. func (z *decRd) readxIO(n uint) []byte {
  1090. if z.bufio {
  1091. return z.bi.readx(n)
  1092. }
  1093. return z.ri.readx(n)
  1094. }
  1095. func (z *decRd) readb(s []byte) {
  1096. if z.bytes {
  1097. z.rb.readb(s)
  1098. } else {
  1099. z.readbIO(s)
  1100. }
  1101. }
  1102. func (z *decRd) readbIO(s []byte) {
  1103. if z.bufio {
  1104. z.bi.readb(s)
  1105. } else {
  1106. z.ri.readb(s)
  1107. }
  1108. }
  1109. func (z *decRd) readn1() uint8 {
  1110. if z.bytes {
  1111. return z.rb.readn1()
  1112. }
  1113. return z.readn1IO()
  1114. }
  1115. func (z *decRd) readn1IO() uint8 {
  1116. if z.bufio {
  1117. return z.bi.readn1()
  1118. }
  1119. return z.ri.readn1()
  1120. }
  1121. func (z *decRd) skip(accept *bitset256) (token byte) {
  1122. if z.bytes {
  1123. return z.rb.skip(accept)
  1124. }
  1125. return z.skipIO(accept)
  1126. }
  1127. func (z *decRd) skipIO(accept *bitset256) (token byte) {
  1128. if z.bufio {
  1129. return z.bi.skip(accept)
  1130. }
  1131. return z.ri.skip(accept)
  1132. }
  1133. func (z *decRd) readTo(accept *bitset256) (out []byte) {
  1134. if z.bytes {
  1135. return z.rb.readTo(accept)
  1136. }
  1137. return z.readToIO(accept)
  1138. }
  1139. func (z *decRd) readToIO(accept *bitset256) (out []byte) {
  1140. if z.bufio {
  1141. return z.bi.readTo(accept)
  1142. }
  1143. return z.ri.readTo(accept)
  1144. }
  1145. func (z *decRd) readUntil(stop byte) (out []byte) {
  1146. if z.bytes {
  1147. return z.rb.readUntil(stop)
  1148. }
  1149. return z.readUntilIO(stop)
  1150. }
  1151. func (z *decRd) readUntilIO(stop byte) (out []byte) {
  1152. if z.bufio {
  1153. return z.bi.readUntil(stop)
  1154. }
  1155. return z.ri.readUntil(stop)
  1156. }
  1157. */
  1158. /*
  1159. func (z *decRd) numread() int {
  1160. switch z.typ {
  1161. case entryTypeBytes:
  1162. return z.rb.numread()
  1163. case entryTypeIo:
  1164. return z.ri.numread()
  1165. default:
  1166. return z.bi.numread()
  1167. }
  1168. }
  1169. func (z *decRd) track() {
  1170. switch z.typ {
  1171. case entryTypeBytes:
  1172. z.rb.track()
  1173. case entryTypeIo:
  1174. z.ri.track()
  1175. default:
  1176. z.bi.track()
  1177. }
  1178. }
  1179. func (z *decRd) stopTrack() []byte {
  1180. switch z.typ {
  1181. case entryTypeBytes:
  1182. return z.rb.stopTrack()
  1183. case entryTypeIo:
  1184. return z.ri.stopTrack()
  1185. default:
  1186. return z.bi.stopTrack()
  1187. }
  1188. }
  1189. func (z *decRd) unreadn1() {
  1190. switch z.typ {
  1191. case entryTypeBytes:
  1192. z.rb.unreadn1()
  1193. case entryTypeIo:
  1194. z.ri.unreadn1()
  1195. default:
  1196. z.bi.unreadn1()
  1197. }
  1198. }
  1199. func (z *decRd) readx(n int) []byte {
  1200. switch z.typ {
  1201. case entryTypeBytes:
  1202. return z.rb.readx(n)
  1203. case entryTypeIo:
  1204. return z.ri.readx(n)
  1205. default:
  1206. return z.bi.readx(n)
  1207. }
  1208. }
  1209. func (z *decRd) readb(s []byte) {
  1210. switch z.typ {
  1211. case entryTypeBytes:
  1212. z.rb.readb(s)
  1213. case entryTypeIo:
  1214. z.ri.readb(s)
  1215. default:
  1216. z.bi.readb(s)
  1217. }
  1218. }
  1219. func (z *decRd) readn1() uint8 {
  1220. switch z.typ {
  1221. case entryTypeBytes:
  1222. return z.rb.readn1()
  1223. case entryTypeIo:
  1224. return z.ri.readn1()
  1225. default:
  1226. return z.bi.readn1()
  1227. }
  1228. }
  1229. func (z *decRd) skip(accept *bitset256) (token byte) {
  1230. switch z.typ {
  1231. case entryTypeBytes:
  1232. return z.rb.skip(accept)
  1233. case entryTypeIo:
  1234. return z.ri.skip(accept)
  1235. default:
  1236. return z.bi.skip(accept)
  1237. }
  1238. }
  1239. func (z *decRd) readTo(accept *bitset256) (out []byte) {
  1240. switch z.typ {
  1241. case entryTypeBytes:
  1242. return z.rb.readTo(accept)
  1243. case entryTypeIo:
  1244. return z.ri.readTo(accept)
  1245. default:
  1246. return z.bi.readTo(accept)
  1247. }
  1248. }
  1249. func (z *decRd) readUntil(stop byte) (out []byte) {
  1250. switch z.typ {
  1251. case entryTypeBytes:
  1252. return z.rb.readUntil(stop)
  1253. case entryTypeIo:
  1254. return z.ri.readUntil(stop)
  1255. default:
  1256. return z.bi.readUntil(stop)
  1257. }
  1258. }
  1259. */
  1260. var _ decReader = (*decRd)(nil)
  1261. // // register these here, so that staticcheck stops barfing
  1262. // var _ = (*bytesDecReader).readUntil