Peek returns the ith element and possibly does an allocation. Peeking past an error will panic.
(pos int)
| 65 | // Peek returns the ith element and possibly does an allocation. |
| 66 | // Peeking past an error will panic. |
| 67 | func (z *TokenBuffer) Peek(pos int) *Token { |
| 68 | pos += z.pos |
| 69 | if pos >= len(z.buf) { |
| 70 | if len(z.buf) > 0 && z.buf[len(z.buf)-1].TokenType == html.ErrorToken { |
| 71 | return &z.buf[len(z.buf)-1] |
| 72 | } |
| 73 | |
| 74 | c := cap(z.buf) |
| 75 | d := len(z.buf) - z.pos |
| 76 | p := pos - z.pos + 1 // required peek length |
| 77 | var buf []Token |
| 78 | if 2*p > c { |
| 79 | buf = make([]Token, 0, 2*c+p) |
| 80 | } else { |
| 81 | buf = z.buf |
| 82 | } |
| 83 | copy(buf[:d], z.buf[z.pos:]) |
| 84 | |
| 85 | buf = buf[:p] |
| 86 | pos -= z.pos |
| 87 | for i := d; i < p; i++ { |
| 88 | z.read(&buf[i]) |
| 89 | if buf[i].TokenType == html.ErrorToken { |
| 90 | buf = buf[:i+1] |
| 91 | pos = i |
| 92 | break |
| 93 | } |
| 94 | } |
| 95 | z.pos, z.buf = 0, buf |
| 96 | } |
| 97 | return &z.buf[pos] |
| 98 | } |
| 99 | |
| 100 | // Shift returns the first element and advances position. |
| 101 | func (z *TokenBuffer) Shift() *Token { |