reader.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. // Copyright (c) 2012-2020 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. // readx will return a view of the []byte if decoding from a []byte, OR
  9. // read into the implementation scratch buffer if possible i.e. n < len(scratchbuf), OR
  10. // create a new []byte and read into that
  11. readx(n uint) []byte
  12. readb([]byte)
  13. readn1() byte
  14. readn2() [2]byte
  15. // readn3 will read 3 bytes into the top-most elements of a 4-byte array
  16. readn3() [4]byte
  17. readn4() [4]byte
  18. readn8() [8]byte
  19. // readn1eof() (v uint8, eof bool)
  20. // // read up to 8 bytes at a time
  21. // readn(num uint8) (v [8]byte)
  22. numread() uint // number of bytes read
  23. // skip any whitespace characters, and return the first non-matching byte
  24. skipWhitespace() (token byte)
  25. // jsonReadNum will include last read byte in first element of slice,
  26. // and continue numeric characters until it sees a non-numeric char
  27. // or EOF. If it sees a non-numeric character, it will unread that.
  28. jsonReadNum() []byte
  29. // jsonReadAsisChars will read json plain characters (anything but " or \)
  30. // and return a slice terminated by a non-json asis character.
  31. jsonReadAsisChars() []byte
  32. // skip will skip any byte that matches, and return the first non-matching byte
  33. // skip(accept *bitset256) (token byte)
  34. // readTo will read any byte that matches, stopping once no-longer matching.
  35. // readTo(accept *bitset256) (out []byte)
  36. // readUntil will read, only stopping once it matches the 'stop' byte (which it excludes).
  37. readUntil(stop byte) (out []byte)
  38. }
  39. // ------------------------------------------------
  40. type unreadByteStatus uint8
  41. // unreadByteStatus goes from
  42. // undefined (when initialized) -- (read) --> canUnread -- (unread) --> canRead ...
  43. const (
  44. unreadByteUndefined unreadByteStatus = iota
  45. unreadByteCanRead
  46. unreadByteCanUnread
  47. )
  48. // --------------------
  49. type ioDecReaderCommon struct {
  50. r io.Reader // the reader passed in
  51. n uint // num read
  52. l byte // last byte
  53. ls unreadByteStatus // last byte status
  54. b [6]byte // tiny buffer for reading single bytes
  55. blist *bytesFreelist
  56. bufr []byte // buffer for readTo/readUntil
  57. }
  58. func (z *ioDecReaderCommon) reset(r io.Reader, blist *bytesFreelist) {
  59. z.blist = blist
  60. z.r = r
  61. z.ls = unreadByteUndefined
  62. z.l, z.n = 0, 0
  63. z.bufr = z.blist.check(z.bufr, 256)
  64. }
  65. func (z *ioDecReaderCommon) numread() uint {
  66. return z.n
  67. }
  68. // ------------------------------------------
  69. // ioDecReader is a decReader that reads off an io.Reader.
  70. //
  71. // It also has a fallback implementation of ByteScanner if needed.
  72. type ioDecReader struct {
  73. ioDecReaderCommon
  74. br io.ByteScanner
  75. x [64 + 48]byte // for: get struct field name, swallow valueTypeBytes, etc
  76. }
  77. func (z *ioDecReader) reset(r io.Reader, blist *bytesFreelist) {
  78. z.ioDecReaderCommon.reset(r, blist)
  79. z.br, _ = r.(io.ByteScanner)
  80. }
  81. func (z *ioDecReader) Read(p []byte) (n int, err error) {
  82. if len(p) == 0 {
  83. return
  84. }
  85. var firstByte bool
  86. if z.ls == unreadByteCanRead {
  87. z.ls = unreadByteCanUnread
  88. p[0] = z.l
  89. if len(p) == 1 {
  90. n = 1
  91. return
  92. }
  93. firstByte = true
  94. p = p[1:]
  95. }
  96. n, err = z.r.Read(p)
  97. if n > 0 {
  98. if err == io.EOF && n == len(p) {
  99. err = nil // read was successful, so postpone EOF (till next time)
  100. }
  101. z.l = p[n-1]
  102. z.ls = unreadByteCanUnread
  103. }
  104. if firstByte {
  105. n++
  106. }
  107. return
  108. }
  109. func (z *ioDecReader) ReadByte() (c byte, err error) {
  110. if z.br != nil {
  111. c, err = z.br.ReadByte()
  112. if err == nil {
  113. z.l = c
  114. z.ls = unreadByteCanUnread
  115. }
  116. return
  117. }
  118. n, err := z.Read(z.b[:1])
  119. if n == 1 {
  120. c = z.b[0]
  121. if err == io.EOF {
  122. err = nil // read was successful, so postpone EOF (till next time)
  123. }
  124. }
  125. return
  126. }
  127. func (z *ioDecReader) UnreadByte() (err error) {
  128. if z.br != nil {
  129. err = z.br.UnreadByte()
  130. if err == nil {
  131. z.ls = unreadByteCanRead
  132. }
  133. return
  134. }
  135. switch z.ls {
  136. case unreadByteCanUnread:
  137. z.ls = unreadByteCanRead
  138. case unreadByteCanRead:
  139. err = errDecUnreadByteLastByteNotRead
  140. case unreadByteUndefined:
  141. err = errDecUnreadByteNothingToRead
  142. default:
  143. err = errDecUnreadByteUnknown
  144. }
  145. return
  146. }
  147. func (z *ioDecReader) readn2() (bs [2]byte) {
  148. z.readb(bs[:])
  149. return
  150. }
  151. func (z *ioDecReader) readn3() (bs [4]byte) {
  152. z.readb(bs[1:])
  153. return
  154. }
  155. func (z *ioDecReader) readn4() (bs [4]byte) {
  156. z.readb(bs[:])
  157. return
  158. }
  159. func (z *ioDecReader) readn8() (bs [8]byte) {
  160. z.readb(bs[:])
  161. return
  162. }
  163. func (z *ioDecReader) readx(n uint) (bs []byte) {
  164. if n == 0 {
  165. return
  166. }
  167. if n < uint(len(z.x)) {
  168. bs = z.x[:n]
  169. } else {
  170. bs = make([]byte, n)
  171. }
  172. _, err := readFull(z.r, bs)
  173. halt.onerror(err)
  174. z.n += uint(len(bs))
  175. return
  176. }
  177. func (z *ioDecReader) readb(bs []byte) {
  178. if len(bs) == 0 {
  179. return
  180. }
  181. _, err := readFull(z.r, bs)
  182. halt.onerror(err)
  183. z.n += uint(len(bs))
  184. }
  185. func (z *ioDecReader) readn1() (b uint8) {
  186. b, err := z.ReadByte()
  187. halt.onerror(err)
  188. z.n++
  189. return
  190. }
  191. func (z *ioDecReader) readn1eof() (b uint8, eof bool) {
  192. b, err := z.ReadByte()
  193. if err == nil {
  194. z.n++
  195. } else if err == io.EOF {
  196. eof = true
  197. } else {
  198. halt.onerror(err)
  199. }
  200. return
  201. }
  202. func (z *ioDecReader) jsonReadNum() (bs []byte) {
  203. z.unreadn1()
  204. z.bufr = z.bufr[:0]
  205. LOOP:
  206. i, eof := z.readn1eof()
  207. if eof {
  208. return z.bufr
  209. }
  210. if isNumberChar(i) {
  211. z.bufr = append(z.bufr, i)
  212. goto LOOP
  213. }
  214. z.unreadn1()
  215. return z.bufr
  216. }
  217. func (z *ioDecReader) jsonReadAsisChars() (bs []byte) {
  218. z.bufr = z.bufr[:0]
  219. LOOP:
  220. i := z.readn1()
  221. z.bufr = append(z.bufr, i)
  222. if i == '"' || i == '\\' {
  223. return z.bufr
  224. }
  225. goto LOOP
  226. }
  227. func (z *ioDecReader) skipWhitespace() (token byte) {
  228. LOOP:
  229. token = z.readn1()
  230. if isWhitespaceChar(token) {
  231. goto LOOP
  232. }
  233. return
  234. }
  235. func (z *ioDecReader) readUntil(stop byte) []byte {
  236. z.bufr = z.bufr[:0]
  237. LOOP:
  238. token := z.readn1()
  239. z.bufr = append(z.bufr, token)
  240. if token == stop {
  241. return z.bufr[:len(z.bufr)-1]
  242. }
  243. goto LOOP
  244. }
  245. func (z *ioDecReader) unreadn1() {
  246. err := z.UnreadByte()
  247. halt.onerror(err)
  248. z.n--
  249. }
  250. // ------------------------------------
  251. type bufioDecReader struct {
  252. ioDecReaderCommon
  253. c uint // cursor
  254. buf []byte
  255. }
  256. func (z *bufioDecReader) reset(r io.Reader, bufsize int, blist *bytesFreelist) {
  257. z.ioDecReaderCommon.reset(r, blist)
  258. z.c = 0
  259. if cap(z.buf) < bufsize {
  260. z.buf = blist.get(bufsize)
  261. } else {
  262. z.buf = z.buf[:0]
  263. }
  264. }
  265. func (z *bufioDecReader) readb(p []byte) {
  266. var n = uint(copy(p, z.buf[z.c:]))
  267. z.n += n
  268. z.c += n
  269. if len(p) != int(n) {
  270. z.readbFill(p, n, true, false)
  271. }
  272. }
  273. func readbFillHandleErr(err error, must, eof bool) (isEOF bool) {
  274. if err == io.EOF {
  275. isEOF = true
  276. }
  277. if must && !(eof && isEOF) {
  278. halt.onerror(err)
  279. }
  280. return
  281. }
  282. func (z *bufioDecReader) readbFill(p0 []byte, n uint, must, eof bool) (isEOF bool, err error) {
  283. // at this point, there's nothing in z.buf to read (z.buf is fully consumed)
  284. var p []byte
  285. if p0 != nil {
  286. p = p0[n:]
  287. }
  288. var n2 uint
  289. if len(p) > cap(z.buf) {
  290. n2, err = readFull(z.r, p)
  291. if err != nil {
  292. isEOF = readbFillHandleErr(err, must, eof)
  293. return
  294. }
  295. n += n2
  296. z.n += n2
  297. // always keep last byte in z.buf
  298. z.buf = z.buf[:1]
  299. z.buf[0] = p[len(p)-1]
  300. z.c = 1
  301. return
  302. }
  303. // z.c is now 0, and len(p) <= cap(z.buf)
  304. var n1 int
  305. LOOP:
  306. // for len(p) > 0 && z.err == nil {
  307. z.buf = z.buf[0:cap(z.buf)]
  308. n1, err = z.r.Read(z.buf)
  309. n2 = uint(n1)
  310. if n2 == 0 && err != nil {
  311. isEOF = readbFillHandleErr(err, must, eof)
  312. return
  313. }
  314. err = nil
  315. z.buf = z.buf[:n2]
  316. z.c = 0
  317. if len(p) > 0 {
  318. n2 = uint(copy(p, z.buf))
  319. z.c = n2
  320. n += n2
  321. z.n += n2
  322. p = p[n2:]
  323. if len(p) > 0 {
  324. goto LOOP
  325. }
  326. if z.c == 0 {
  327. z.buf = z.buf[:1]
  328. z.buf[0] = p[len(p)-1]
  329. z.c = 1
  330. }
  331. }
  332. return
  333. }
  334. func (z *bufioDecReader) readn1() (b byte) {
  335. if z.c >= uint(len(z.buf)) {
  336. z.readbFill(nil, 0, true, false)
  337. }
  338. b = z.buf[z.c]
  339. z.c++
  340. z.n++
  341. return
  342. }
  343. func (z *bufioDecReader) readn1eof() (b byte, eof bool) {
  344. if z.c >= uint(len(z.buf)) {
  345. eof, _ = z.readbFill(nil, 0, true, true)
  346. if eof {
  347. return
  348. }
  349. }
  350. b = z.buf[z.c]
  351. z.c++
  352. z.n++
  353. return
  354. }
  355. func (z *bufioDecReader) unreadn1() {
  356. if z.c == 0 {
  357. halt.onerror(errDecUnreadByteNothingToRead)
  358. }
  359. z.c--
  360. z.n--
  361. }
  362. func (z *bufioDecReader) readn2() (bs [2]byte) {
  363. z.readb(bs[:])
  364. return
  365. }
  366. func (z *bufioDecReader) readn3() (bs [4]byte) {
  367. z.readb(bs[1:])
  368. return
  369. }
  370. func (z *bufioDecReader) readn4() (bs [4]byte) {
  371. z.readb(bs[:])
  372. return
  373. }
  374. func (z *bufioDecReader) readn8() (bs [8]byte) {
  375. z.readb(bs[:])
  376. return
  377. }
  378. func (z *bufioDecReader) readx(n uint) (bs []byte) {
  379. if n == 0 {
  380. // return
  381. } else if z.c+n <= uint(len(z.buf)) {
  382. bs = z.buf[z.c : z.c+n]
  383. z.n += n
  384. z.c += n
  385. } else {
  386. bs = make([]byte, n)
  387. // n no longer used - can reuse
  388. n = uint(copy(bs, z.buf[z.c:]))
  389. z.n += n
  390. z.c += n
  391. z.readbFill(bs, n, true, false)
  392. }
  393. return
  394. }
  395. func (z *bufioDecReader) jsonReadNum() (bs []byte) {
  396. z.unreadn1()
  397. z.bufr = z.bufr[:0]
  398. LOOP:
  399. i, eof := z.readn1eof()
  400. if eof {
  401. return z.bufr
  402. }
  403. if isNumberChar(i) {
  404. z.bufr = append(z.bufr, i)
  405. goto LOOP
  406. }
  407. z.unreadn1()
  408. return z.bufr
  409. }
  410. func (z *bufioDecReader) jsonReadAsisChars() (bs []byte) {
  411. z.bufr = z.bufr[:0]
  412. LOOP:
  413. i := z.readn1()
  414. z.bufr = append(z.bufr, i)
  415. if i == '"' || i == '\\' {
  416. return z.bufr
  417. }
  418. goto LOOP
  419. }
  420. func (z *bufioDecReader) skipWhitespace() (token byte) {
  421. i := z.c
  422. LOOP:
  423. if i < uint(len(z.buf)) {
  424. // inline z.skipLoopFn(i) and refactor, so cost is within inline budget
  425. token = z.buf[i]
  426. i++
  427. if isWhitespaceChar(token) {
  428. goto LOOP
  429. }
  430. z.n += i - 2 - z.c
  431. z.c = i
  432. return
  433. }
  434. return z.skipFillWhitespace()
  435. }
  436. func (z *bufioDecReader) skipFillWhitespace() (token byte) {
  437. z.n += uint(len(z.buf)) - z.c
  438. var i, n2 int
  439. var err error
  440. for {
  441. z.c = 0
  442. z.buf = z.buf[0:cap(z.buf)]
  443. n2, err = z.r.Read(z.buf)
  444. if n2 == 0 {
  445. halt.onerror(err)
  446. }
  447. z.buf = z.buf[:n2]
  448. for i, token = range z.buf {
  449. if !isWhitespaceChar(token) {
  450. z.n += (uint(i) - z.c) - 1
  451. z.loopFn(uint(i + 1))
  452. return
  453. }
  454. }
  455. z.n += uint(n2)
  456. }
  457. }
  458. func (z *bufioDecReader) loopFn(i uint) {
  459. z.c = i
  460. }
  461. func (z *bufioDecReader) readUntil(stop byte) (out []byte) {
  462. i := z.c
  463. LOOP:
  464. if i < uint(len(z.buf)) {
  465. if z.buf[i] == stop {
  466. z.n += (i - z.c) - 1
  467. i++
  468. out = z.buf[z.c:i]
  469. z.c = i
  470. goto FINISH
  471. }
  472. i++
  473. goto LOOP
  474. }
  475. out = z.readUntilFill(stop)
  476. FINISH:
  477. return out[:len(out)-1]
  478. }
  479. func (z *bufioDecReader) readUntilFill(stop byte) []byte {
  480. z.bufr = z.bufr[:0]
  481. z.n += uint(len(z.buf)) - z.c
  482. z.bufr = append(z.bufr, z.buf[z.c:]...)
  483. for {
  484. z.c = 0
  485. z.buf = z.buf[0:cap(z.buf)]
  486. n1, err := z.r.Read(z.buf)
  487. if n1 == 0 {
  488. halt.onerror(err)
  489. }
  490. n2 := uint(n1)
  491. z.buf = z.buf[:n2]
  492. for i, token := range z.buf {
  493. if token == stop {
  494. z.n += (uint(i) - z.c) - 1
  495. z.bufr = append(z.bufr, z.buf[z.c:i+1]...)
  496. z.loopFn(uint(i + 1))
  497. return z.bufr
  498. }
  499. }
  500. z.bufr = append(z.bufr, z.buf...)
  501. z.n += n2
  502. }
  503. }
  504. // ------------------------------------
  505. // bytesDecReader is a decReader that reads off a byte slice with zero copying
  506. //
  507. // Note: we do not try to convert index'ing out of bounds to an io.EOF.
  508. // instead, we let it bubble up to the exported Encode/Decode method
  509. // and recover it as an io.EOF.
  510. //
  511. // see panicValToErr(...) function in helper.go.
  512. type bytesDecReader struct {
  513. b []byte // data
  514. c uint // cursor
  515. }
  516. func (z *bytesDecReader) reset(in []byte) {
  517. z.b = in[:len(in):len(in)] // reslicing must not go past capacity
  518. z.c = 0
  519. }
  520. func (z *bytesDecReader) numread() uint {
  521. return z.c
  522. }
  523. // Note: slicing from a non-constant start position is more expensive,
  524. // as more computation is required to decipher the pointer start position.
  525. // However, we do it only once, and it's better than reslicing both z.b and return value.
  526. func (z *bytesDecReader) readx(n uint) (bs []byte) {
  527. x := z.c + n
  528. bs = z.b[z.c:x]
  529. z.c = x
  530. return
  531. }
  532. func (z *bytesDecReader) readb(bs []byte) {
  533. copy(bs, z.readx(uint(len(bs))))
  534. }
  535. // MARKER: do not use this - as it calls into memmove (as the size of data to move is unknown)
  536. // func (z *bytesDecReader) readnn(bs []byte, n uint) {
  537. // x := z.c
  538. // copy(bs, z.b[x:x+n])
  539. // z.c += n
  540. // }
  541. // func (z *bytesDecReader) readn(num uint8) (bs [8]byte) {
  542. // x := z.c + uint(num)
  543. // copy(bs[:], z.b[z.c:x]) // slice z.b completely, so we get bounds error if past
  544. // z.c = x
  545. // return
  546. // }
  547. // func (z *bytesDecReader) readn1() uint8 {
  548. // z.c++
  549. // return z.b[z.c-1]
  550. // }
  551. func (z *bytesDecReader) readn1() (v uint8) {
  552. v = z.b[z.c]
  553. z.c++
  554. return
  555. }
  556. // MARKER: for readn{2,3,4,8}, ensure you slice z.b completely so we get bounds error if past end.
  557. func (z *bytesDecReader) readn2() (bs [2]byte) {
  558. // copy(bs[:], z.b[z.c:z.c+2])
  559. bs[1] = z.b[z.c+1]
  560. bs[0] = z.b[z.c]
  561. z.c += 2
  562. return
  563. }
  564. func (z *bytesDecReader) readn3() (bs [4]byte) {
  565. // copy(bs[1:], z.b[z.c:z.c+3])
  566. bs = okBytes3(z.b[z.c : z.c+3])
  567. z.c += 3
  568. return
  569. }
  570. func (z *bytesDecReader) readn4() (bs [4]byte) {
  571. // copy(bs[:], z.b[z.c:z.c+4])
  572. bs = okBytes4(z.b[z.c : z.c+4])
  573. z.c += 4
  574. return
  575. }
  576. func (z *bytesDecReader) readn8() (bs [8]byte) {
  577. // copy(bs[:], z.b[z.c:z.c+8])
  578. bs = okBytes8(z.b[z.c : z.c+8])
  579. z.c += 8
  580. return
  581. }
  582. func (z *bytesDecReader) jsonReadNum() []byte {
  583. z.c--
  584. i := z.c
  585. LOOP:
  586. if i < uint(len(z.b)) && isNumberChar(z.b[i]) {
  587. i++
  588. goto LOOP
  589. }
  590. z.c, i = i, z.c
  591. return z.b[i:z.c]
  592. }
  593. func (z *bytesDecReader) jsonReadAsisChars() []byte {
  594. i := z.c
  595. LOOP:
  596. token := z.b[i]
  597. i++
  598. if token == '"' || token == '\\' {
  599. z.c, i = i, z.c
  600. return z.b[i:z.c]
  601. }
  602. goto LOOP
  603. }
  604. func (z *bytesDecReader) skipWhitespace() (token byte) {
  605. i := z.c
  606. LOOP:
  607. token = z.b[i]
  608. if isWhitespaceChar(token) {
  609. i++
  610. goto LOOP
  611. }
  612. z.c = i + 1
  613. return
  614. }
  615. func (z *bytesDecReader) readUntil(stop byte) (out []byte) {
  616. i := z.c
  617. LOOP:
  618. if z.b[i] == stop {
  619. out = z.b[z.c:i]
  620. z.c = i + 1
  621. return
  622. }
  623. i++
  624. goto LOOP
  625. }
  626. // --------------
  627. type decRd struct {
  628. mtr bool // is maptype a known type?
  629. str bool // is slicetype a known type?
  630. be bool // is binary encoding
  631. js bool // is json handle
  632. jsms bool // is json handle, and MapKeyAsString
  633. cbor bool // is cbor handle
  634. bytes bool // is bytes reader
  635. bufio bool // is this a bufioDecReader?
  636. rb bytesDecReader
  637. ri *ioDecReader
  638. bi *bufioDecReader
  639. decReader
  640. }
  641. // From out benchmarking, we see the following in terms of performance:
  642. //
  643. // - interface calls
  644. // - branch that can inline what it calls
  645. //
  646. // the if/else-if/else block is expensive to inline.
  647. // Each node of this construct costs a lot and dominates the budget.
  648. // Best to only do an if fast-path else block (so fast-path is inlined).
  649. // This is irrespective of inlineExtraCallCost set in $GOROOT/src/cmd/compile/internal/gc/inl.go
  650. //
  651. // In decRd methods below, we delegate all IO functions into their own methods.
  652. // This allows for the inlining of the common path when z.bytes=true.
  653. // Go 1.12+ supports inlining methods with up to 1 inlined function (or 2 if no other constructs).
  654. //
  655. // However, up through Go 1.13, decRd's readXXX, skip and unreadXXX methods are not inlined.
  656. // Consequently, there is no benefit to do the xxxIO methods for decRd at this time.
  657. // Instead, we have a if/else-if/else block so that IO calls do not have to jump through
  658. // a second unnecessary function call.
  659. //
  660. // If golang inlining gets better and bytesDecReader methods can be inlined,
  661. // then we can revert to using these 2 functions so the bytesDecReader
  662. // methods are inlined and the IO paths call out to a function.
  663. //
  664. // decRd is designed to embed a decReader, and then re-implement some of the decReader
  665. // methods using a conditional branch. We only override the ones that have a bytes version
  666. // that is small enough to be inlined. We use ./run.sh -z to check.
  667. // Right now, only numread and readn1 can be inlined.
  668. func (z *decRd) numread() uint {
  669. if z.bytes {
  670. return z.rb.numread()
  671. } else if z.bufio {
  672. return z.bi.numread()
  673. } else {
  674. return z.ri.numread()
  675. }
  676. }
  677. func (z *decRd) readn1() (v uint8) {
  678. if z.bytes {
  679. // MARKER: manually inline, else this function is not inlined.
  680. // Keep in sync with bytesDecReader.readn1
  681. // return z.rb.readn1()
  682. v = z.rb.b[z.rb.c]
  683. z.rb.c++
  684. } else {
  685. v = z.readn1IO()
  686. }
  687. return
  688. }
  689. func (z *decRd) readn1IO() uint8 {
  690. if z.bufio {
  691. return z.bi.readn1()
  692. }
  693. return z.ri.readn1()
  694. }
  695. type devNullReader struct{}
  696. func (devNullReader) Read(p []byte) (int, error) { return 0, io.EOF }
  697. func (devNullReader) Close() error { return nil }
  698. func readFull(r io.Reader, bs []byte) (n uint, err error) {
  699. var nn int
  700. for n < uint(len(bs)) && err == nil {
  701. nn, err = r.Read(bs[n:])
  702. if nn > 0 {
  703. if err == io.EOF {
  704. // leave EOF for next time
  705. err = nil
  706. }
  707. n += uint(nn)
  708. }
  709. }
  710. // do not do this below - it serves no purpose
  711. // if n != len(bs) && err == io.EOF { err = io.ErrUnexpectedEOF }
  712. return
  713. }
  714. var _ decReader = (*decRd)(nil)