Skip to content

Commit f258a39

Browse files
authored
Speed up ast.tokenLocation
1 parent 2ac0ba0 commit f258a39

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

lib/std/zig/Ast.zig

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,20 @@ pub fn tokenLocation(self: Ast, start_offset: ByteOffset, token_index: TokenInde
140140
.line_end = self.source.len,
141141
};
142142
const token_start = self.tokens.items(.start)[token_index];
143-
for (self.source[start_offset..], 0..) |c, i| {
144-
if (i + start_offset == token_start) {
145-
loc.line_end = i + start_offset;
143+
144+
// Scan to by line until we go past the token start
145+
while (std.mem.indexOfScalarPos(u8, self.source, loc.line_start, '\n')) |i| {
146+
if (i >= token_start) {
147+
break; // Went past
148+
}
149+
loc.line += 1;
150+
loc.line_start = i + 1;
151+
}
152+
153+
const offset = loc.line_start;
154+
for (self.source[offset..], 0..) |c, i| {
155+
if (i + offset == token_start) {
156+
loc.line_end = i + offset;
146157
while (loc.line_end < self.source.len and self.source[loc.line_end] != '\n') {
147158
loc.line_end += 1;
148159
}

0 commit comments

Comments
 (0)