read reads the next rune from the reader.
()
| 409 | |
| 410 | // read reads the next rune from the reader. |
| 411 | func (r *reader) read() (ch rune, pos Pos) { |
| 412 | // If we have unread characters then read them off the buffer first. |
| 413 | if r.n > 0 { |
| 414 | r.n-- |
| 415 | return r.curr() |
| 416 | } |
| 417 | |
| 418 | // Read next rune from underlying reader. |
| 419 | // Any error (including io.EOF) should return as EOF. |
| 420 | ch, _, err := r.r.ReadRune() |
| 421 | if err != nil { |
| 422 | ch = eof |
| 423 | } else if ch == '\r' { |
| 424 | if ch, _, err := r.r.ReadRune(); err != nil { |
| 425 | // nop |
| 426 | } else if ch != '\n' { |
| 427 | _ = r.r.UnreadRune() |
| 428 | } |
| 429 | ch = '\n' |
| 430 | } |
| 431 | |
| 432 | // Save character and position to the buffer. |
| 433 | r.i = (r.i + 1) % len(r.buf) |
| 434 | buf := &r.buf[r.i] |
| 435 | buf.ch, buf.pos = ch, r.pos |
| 436 | |
| 437 | // Update position. |
| 438 | // Only count EOF once. |
| 439 | if ch == '\n' { |
| 440 | r.pos.Line++ |
| 441 | r.pos.Char = 0 |
| 442 | } else if !r.eof { |
| 443 | r.pos.Char++ |
| 444 | } |
| 445 | |
| 446 | // Mark the reader as EOF. |
| 447 | // This is used so we don't double count EOF characters. |
| 448 | if ch == eof { |
| 449 | r.eof = true |
| 450 | } |
| 451 | |
| 452 | return r.curr() |
| 453 | } |
| 454 | |
| 455 | // unread pushes the previously read rune back onto the buffer. |
| 456 | func (r *reader) unread() { |
no test coverage detected