reader.go 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  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. // c := z.c
  755. // z.c += uint(len(bs))
  756. // copy(bs, z.b[c:z.c])
  757. }
  758. func (z *bytesDecReader) readn1() (v uint8) {
  759. // if z.c >= uint(len(z.b)) {
  760. // panic(io.EOF)
  761. // }
  762. v = z.b[z.c]
  763. z.c++
  764. // v = z.b[z.c] // cost = 7
  765. // z.c++ // cost = 4
  766. // z.a--
  767. return
  768. }
  769. func (z *bytesDecReader) readn(num uint8) (bs [rwNLen]byte) {
  770. // if z.c >= uint(len(z.b)) || z.c+uint(num) >= uint(len(z.b)) {
  771. // panic(io.EOF)
  772. // }
  773. // _ = z.b[z.c:z.c+uint(num)]
  774. // _ = bs[0]
  775. // _ = bs[num-1]
  776. // _ = z.b[z.c]
  777. // _ = z.b[z.c+uint(num-1)]
  778. // for bounds-check elimination, reslice z.b and ensure bs is within len
  779. // bb := z.b[z.c:][:num]
  780. bb := z.b[z.c : z.c+uint(num)]
  781. _ = bs[len(bb)-1]
  782. for i := 0; i < len(bb); i++ {
  783. // for i := uint(0); i < uint(len(bb)); i++ {
  784. bs[i] = bb[i]
  785. }
  786. // for i := uint8(0); i < num; i++ {
  787. // bs[i] = z.b[z.c+uint(i)]
  788. // }
  789. // for i := num; i > 0; i-- {
  790. // // xdebugf("i: %d", i)
  791. // bs[i-1] = z.b[z.c+uint(i-1)]
  792. // }
  793. // copy(bs[:], z.b[z.c:z.c+uint(num)])
  794. z.c += uint(num)
  795. return
  796. }
  797. // func (z *bytesDecReader) readn4() (bs [4]byte) {
  798. // // if z.c+3 >= uint(len(z.b)) {
  799. // // panic(io.EOF)
  800. // // }
  801. // // copy(bs[:], z.b[z.c:z.c+4])
  802. // bs[3] = z.b[z.c+3]
  803. // bs[2] = z.b[z.c+2]
  804. // bs[1] = z.b[z.c+1]
  805. // bs[0] = z.b[z.c]
  806. // z.c += 4
  807. // return
  808. // }
  809. // func (z *bytesDecReader) readn1eof() (v uint8, eof bool) {
  810. // if z.a == 0 {
  811. // eof = true
  812. // return
  813. // }
  814. // v = z.b[z.c]
  815. // z.c++
  816. // z.a--
  817. // return
  818. // }
  819. func (z *bytesDecReader) skip(accept *bitset256) (token byte) {
  820. i := z.c
  821. // if i == len(z.b) {
  822. // goto END
  823. // // panic(io.EOF)
  824. // }
  825. // Replace loop with goto construct, so that this can be inlined
  826. // for i := z.c; i < blen; i++ {
  827. // if !accept.isset(z.b[i]) {
  828. // token = z.b[i]
  829. // i++
  830. // z.a -= (i - z.c)
  831. // z.c = i
  832. // return
  833. // }
  834. // }
  835. // i := z.c
  836. LOOP:
  837. // if i < uint(len(z.b)) {
  838. token = z.b[i]
  839. i++
  840. if accept.isset(token) {
  841. goto LOOP
  842. }
  843. // z.a -= (i - z.c)
  844. z.c = i
  845. return
  846. // }
  847. // panic(io.EOF)
  848. // END:
  849. // // z.a = 0
  850. // z.c = blen
  851. // return
  852. }
  853. func (z *bytesDecReader) readTo(accept *bitset256) (out []byte) {
  854. i := z.c
  855. // if i == uint(len(z.b)) {
  856. // panic(io.EOF)
  857. // }
  858. // Replace loop with goto construct, so that this can be inlined
  859. // for i := z.c; i < blen; i++ {
  860. // if !accept.isset(z.b[i]) {
  861. // out = z.b[z.c:i]
  862. // z.a -= (i - z.c)
  863. // z.c = i
  864. // return
  865. // }
  866. // }
  867. // out = z.b[z.c:]
  868. // z.a, z.c = 0, blen
  869. // return
  870. // i := z.c
  871. // LOOP:
  872. // if i < blen {
  873. // if accept.isset(z.b[i]) {
  874. // i++
  875. // goto LOOP
  876. // }
  877. // out = z.b[z.c:i]
  878. // z.a -= (i - z.c)
  879. // z.c = i
  880. // return
  881. // }
  882. // out = z.b[z.c:]
  883. // // z.a, z.c = 0, blen
  884. // z.a = 0
  885. // z.c = blen
  886. // return
  887. // c := i
  888. LOOP:
  889. if i < uint(len(z.b)) {
  890. if accept.isset(z.b[i]) {
  891. i++
  892. goto LOOP
  893. }
  894. }
  895. out = z.b[z.c:i]
  896. // z.a -= (i - z.c)
  897. z.c = i
  898. return // z.b[c:i]
  899. // z.c, i = i, z.c
  900. // return z.b[i:z.c]
  901. }
  902. func (z *bytesDecReader) readUntil(stop byte, includeLast bool) (out []byte) {
  903. i := z.c
  904. // if i == len(z.b) {
  905. // panic(io.EOF)
  906. // }
  907. // Replace loop with goto construct, so that this can be inlined
  908. // for i := z.c; i < blen; i++ {
  909. // if z.b[i] == stop {
  910. // i++
  911. // out = z.b[z.c:i]
  912. // z.a -= (i - z.c)
  913. // z.c = i
  914. // return
  915. // }
  916. // }
  917. LOOP:
  918. // if i < uint(len(z.b)) {
  919. if z.b[i] == stop {
  920. i++
  921. if includeLast {
  922. out = z.b[z.c:i]
  923. } else {
  924. out = z.b[z.c : i-1]
  925. }
  926. // z.a -= (i - z.c)
  927. z.c = i
  928. return
  929. }
  930. i++
  931. goto LOOP
  932. // }
  933. // panic(io.EOF)
  934. // z.a = 0
  935. // z.c = blen
  936. }
  937. func (z *bytesDecReader) track() {
  938. z.t = z.c
  939. }
  940. func (z *bytesDecReader) stopTrack() (bs []byte) {
  941. return z.b[z.t:z.c]
  942. }
  943. // --------------
  944. type decRd struct {
  945. // esep bool // has elem separators
  946. mtr bool // is maptype a known type?
  947. str bool // is slicetype a known type?
  948. be bool // is binary encoding
  949. js bool // is json handle
  950. jsms bool // is json handle, and MapKeyAsString
  951. // typ entryType
  952. bytes bool // is bytes reader
  953. bufio bool // is this a bufioDecReader?
  954. rb bytesDecReader
  955. ri *ioDecReader
  956. bi *bufioDecReader
  957. }
  958. // numread, track and stopTrack are always inlined, as they just check int fields, etc.
  959. // the if/else-if/else block is expensive to inline.
  960. // Each node of this construct costs a lot and dominates the budget.
  961. // Best to only do an if fast-path else block (so fast-path is inlined).
  962. // This is irrespective of inlineExtraCallCost set in $GOROOT/src/cmd/compile/internal/gc/inl.go
  963. //
  964. // In decRd methods below, we delegate all IO functions into their own methods.
  965. // This allows for the inlining of the common path when z.bytes=true.
  966. // Go 1.12+ supports inlining methods with up to 1 inlined function (or 2 if no other constructs).
  967. //
  968. // However, up through Go 1.13, decRd's readXXX, skip and unreadXXX methods are not inlined.
  969. // Consequently, there is no benefit to do the xxxIO methods for decRd at this time.
  970. // Instead, we have a if/else-if/else block so that IO calls do not have to jump through
  971. // a second unnecessary function call.
  972. // func (z *decRd) release() {
  973. // if z.bytes {
  974. // } else if z.bufio {
  975. // z.bi.release()
  976. // } else {
  977. // z.ri.release()
  978. // }
  979. // }
  980. func (z *decRd) numread() uint {
  981. if z.bytes {
  982. return z.rb.numread()
  983. } else if z.bufio {
  984. return z.bi.numread()
  985. } else {
  986. return z.ri.numread()
  987. }
  988. }
  989. func (z *decRd) track() {
  990. if z.bytes {
  991. z.rb.track()
  992. } else if z.bufio {
  993. z.bi.track()
  994. } else {
  995. z.ri.track()
  996. }
  997. }
  998. func (z *decRd) stopTrack() []byte {
  999. if z.bytes {
  1000. return z.rb.stopTrack()
  1001. } else if z.bufio {
  1002. return z.bi.stopTrack()
  1003. } else {
  1004. return z.ri.stopTrack()
  1005. }
  1006. }
  1007. func (z *decRd) unreadn1() {
  1008. if z.bytes {
  1009. z.rb.unreadn1()
  1010. } else if z.bufio {
  1011. z.bi.unreadn1()
  1012. } else {
  1013. z.ri.unreadn1() // not inlined
  1014. }
  1015. }
  1016. func (z *decRd) readn(num uint8) [rwNLen]byte {
  1017. if z.bytes {
  1018. return z.rb.readn(num)
  1019. }
  1020. if z.bufio {
  1021. return z.bi.readn(num)
  1022. }
  1023. return z.ri.readn(num)
  1024. }
  1025. func (z *decRd) readx(n uint) []byte {
  1026. if z.bytes {
  1027. return z.rb.readx(n)
  1028. }
  1029. if z.bufio {
  1030. return z.bi.readx(n)
  1031. }
  1032. return z.ri.readx(n)
  1033. }
  1034. func (z *decRd) readb(s []byte) {
  1035. if z.bytes {
  1036. z.rb.readb(s)
  1037. } else if z.bufio {
  1038. z.bi.readb(s)
  1039. } else {
  1040. z.ri.readb(s)
  1041. }
  1042. }
  1043. func (z *decRd) readn1() uint8 {
  1044. if z.bytes {
  1045. return z.rb.readn1()
  1046. }
  1047. if z.bufio {
  1048. return z.bi.readn1()
  1049. }
  1050. return z.ri.readn1()
  1051. }
  1052. func (z *decRd) skip(accept *bitset256) (token byte) {
  1053. if z.bytes {
  1054. return z.rb.skip(accept)
  1055. }
  1056. if z.bufio {
  1057. return z.bi.skip(accept)
  1058. }
  1059. return z.ri.skip(accept)
  1060. }
  1061. func (z *decRd) readTo(accept *bitset256) (out []byte) {
  1062. if z.bytes {
  1063. return z.rb.readTo(accept)
  1064. }
  1065. if z.bufio {
  1066. return z.bi.readTo(accept)
  1067. }
  1068. return z.ri.readTo(accept)
  1069. }
  1070. func (z *decRd) readUntil(stop byte, includeLast bool) (out []byte) {
  1071. if z.bytes {
  1072. return z.rb.readUntil(stop, includeLast)
  1073. }
  1074. if z.bufio {
  1075. return z.bi.readUntil(stop, includeLast)
  1076. }
  1077. return z.ri.readUntil(stop, includeLast)
  1078. }
  1079. /*
  1080. func (z *decRd) unreadn1() {
  1081. if z.bytes {
  1082. z.rb.unreadn1()
  1083. } else {
  1084. z.unreadn1IO()
  1085. }
  1086. }
  1087. func (z *decRd) unreadn1IO() {
  1088. if z.bufio {
  1089. z.bi.unreadn1()
  1090. } else {
  1091. z.ri.unreadn1()
  1092. }
  1093. }
  1094. func (z *decRd) readn(num uint8) [rwNLen]byte {
  1095. if z.bytes {
  1096. return z.rb.readn(num)
  1097. }
  1098. return z.readnIO(num)
  1099. }
  1100. func (z *decRd) readnIO(num uint8) [rwNLen]byte {
  1101. if z.bufio {
  1102. return z.bi.readn(num)
  1103. }
  1104. return z.ri.readn(num)
  1105. }
  1106. func (z *decRd) readx(n uint) []byte {
  1107. if z.bytes {
  1108. return z.rb.readx(n)
  1109. }
  1110. return z.readxIO(n)
  1111. }
  1112. func (z *decRd) readxIO(n uint) []byte {
  1113. if z.bufio {
  1114. return z.bi.readx(n)
  1115. }
  1116. return z.ri.readx(n)
  1117. }
  1118. func (z *decRd) readb(s []byte) {
  1119. if z.bytes {
  1120. z.rb.readb(s)
  1121. } else {
  1122. z.readbIO(s)
  1123. }
  1124. }
  1125. func (z *decRd) readbIO(s []byte) {
  1126. if z.bufio {
  1127. z.bi.readb(s)
  1128. } else {
  1129. z.ri.readb(s)
  1130. }
  1131. }
  1132. func (z *decRd) readn1() uint8 {
  1133. if z.bytes {
  1134. return z.rb.readn1()
  1135. }
  1136. return z.readn1IO()
  1137. }
  1138. func (z *decRd) readn1IO() uint8 {
  1139. if z.bufio {
  1140. return z.bi.readn1()
  1141. }
  1142. return z.ri.readn1()
  1143. }
  1144. func (z *decRd) skip(accept *bitset256) (token byte) {
  1145. if z.bytes {
  1146. return z.rb.skip(accept)
  1147. }
  1148. return z.skipIO(accept)
  1149. }
  1150. func (z *decRd) skipIO(accept *bitset256) (token byte) {
  1151. if z.bufio {
  1152. return z.bi.skip(accept)
  1153. }
  1154. return z.ri.skip(accept)
  1155. }
  1156. func (z *decRd) readTo(accept *bitset256) (out []byte) {
  1157. if z.bytes {
  1158. return z.rb.readTo(accept)
  1159. }
  1160. return z.readToIO(accept)
  1161. }
  1162. func (z *decRd) readToIO(accept *bitset256) (out []byte) {
  1163. if z.bufio {
  1164. return z.bi.readTo(accept)
  1165. }
  1166. return z.ri.readTo(accept)
  1167. }
  1168. func (z *decRd) readUntil(stop byte) (out []byte) {
  1169. if z.bytes {
  1170. return z.rb.readUntil(stop)
  1171. }
  1172. return z.readUntilIO(stop)
  1173. }
  1174. func (z *decRd) readUntilIO(stop byte) (out []byte) {
  1175. if z.bufio {
  1176. return z.bi.readUntil(stop)
  1177. }
  1178. return z.ri.readUntil(stop)
  1179. }
  1180. */
  1181. /*
  1182. func (z *decRd) numread() int {
  1183. switch z.typ {
  1184. case entryTypeBytes:
  1185. return z.rb.numread()
  1186. case entryTypeIo:
  1187. return z.ri.numread()
  1188. default:
  1189. return z.bi.numread()
  1190. }
  1191. }
  1192. func (z *decRd) track() {
  1193. switch z.typ {
  1194. case entryTypeBytes:
  1195. z.rb.track()
  1196. case entryTypeIo:
  1197. z.ri.track()
  1198. default:
  1199. z.bi.track()
  1200. }
  1201. }
  1202. func (z *decRd) stopTrack() []byte {
  1203. switch z.typ {
  1204. case entryTypeBytes:
  1205. return z.rb.stopTrack()
  1206. case entryTypeIo:
  1207. return z.ri.stopTrack()
  1208. default:
  1209. return z.bi.stopTrack()
  1210. }
  1211. }
  1212. func (z *decRd) unreadn1() {
  1213. switch z.typ {
  1214. case entryTypeBytes:
  1215. z.rb.unreadn1()
  1216. case entryTypeIo:
  1217. z.ri.unreadn1()
  1218. default:
  1219. z.bi.unreadn1()
  1220. }
  1221. }
  1222. func (z *decRd) readx(n int) []byte {
  1223. switch z.typ {
  1224. case entryTypeBytes:
  1225. return z.rb.readx(n)
  1226. case entryTypeIo:
  1227. return z.ri.readx(n)
  1228. default:
  1229. return z.bi.readx(n)
  1230. }
  1231. }
  1232. func (z *decRd) readb(s []byte) {
  1233. switch z.typ {
  1234. case entryTypeBytes:
  1235. z.rb.readb(s)
  1236. case entryTypeIo:
  1237. z.ri.readb(s)
  1238. default:
  1239. z.bi.readb(s)
  1240. }
  1241. }
  1242. func (z *decRd) readn1() uint8 {
  1243. switch z.typ {
  1244. case entryTypeBytes:
  1245. return z.rb.readn1()
  1246. case entryTypeIo:
  1247. return z.ri.readn1()
  1248. default:
  1249. return z.bi.readn1()
  1250. }
  1251. }
  1252. func (z *decRd) skip(accept *bitset256) (token byte) {
  1253. switch z.typ {
  1254. case entryTypeBytes:
  1255. return z.rb.skip(accept)
  1256. case entryTypeIo:
  1257. return z.ri.skip(accept)
  1258. default:
  1259. return z.bi.skip(accept)
  1260. }
  1261. }
  1262. func (z *decRd) readTo(accept *bitset256) (out []byte) {
  1263. switch z.typ {
  1264. case entryTypeBytes:
  1265. return z.rb.readTo(accept)
  1266. case entryTypeIo:
  1267. return z.ri.readTo(accept)
  1268. default:
  1269. return z.bi.readTo(accept)
  1270. }
  1271. }
  1272. func (z *decRd) readUntil(stop byte) (out []byte) {
  1273. switch z.typ {
  1274. case entryTypeBytes:
  1275. return z.rb.readUntil(stop)
  1276. case entryTypeIo:
  1277. return z.ri.readUntil(stop)
  1278. default:
  1279. return z.bi.readUntil(stop)
  1280. }
  1281. }
  1282. */
  1283. var _ decReader = (*decRd)(nil)
  1284. // // register these here, so that staticcheck stops barfing
  1285. // var _ = (*bytesDecReader).readUntil