Reset clears a Tokenizer's state for reuse while preserving allocated memory. This method is called automatically by PutTokenizer() and generally should not be called directly by users. It's exposed for advanced use cases where you want to reuse a tokenizer instance without going through the pool.
()
| 159 | // Performance: By preserving slice capacity, subsequent Tokenize() calls |
| 160 | // avoid reallocation of lineStarts for similarly-sized inputs. |
| 161 | func (t *Tokenizer) Reset() { |
| 162 | // Clear input reference to allow garbage collection |
| 163 | t.input = nil |
| 164 | |
| 165 | // Reset position tracking |
| 166 | t.pos = NewPosition(1, 0) |
| 167 | t.lineStart = Position{} |
| 168 | |
| 169 | // Preserve lineStarts slice capacity but reset length |
| 170 | if cap(t.lineStarts) > 0 { |
| 171 | t.lineStarts = t.lineStarts[:0] |
| 172 | t.lineStarts = append(t.lineStarts, 0) |
| 173 | } else { |
| 174 | // Initialize if not yet allocated |
| 175 | t.lineStarts = make([]int, 1, 16) // Start with reasonable capacity |
| 176 | t.lineStarts[0] = 0 |
| 177 | } |
| 178 | |
| 179 | t.line = 0 |
| 180 | |
| 181 | // Don't reset keywords as they're constant |
| 182 | t.logger = nil |
| 183 | |
| 184 | // Preserve Comments slice capacity but reset length |
| 185 | if cap(t.Comments) > 0 { |
| 186 | t.Comments = t.Comments[:0] |
| 187 | } |
| 188 | } |