zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit f258a391daa31b3ba2c37d879db96fadc0c058f3 (tree)
parent 2ac0ba03a60a133e7ffbab9ad6aaf3b54f6c747b
Author: frmdstryr <frmdstryr@protonmail.com>
Date:   Wed,  8 Nov 2023 20:45:25 -0500

Speed up ast.tokenLocation


Diffstat:
Mlib/std/zig/Ast.zig | 17++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)

diff --git a/lib/std/zig/Ast.zig b/lib/std/zig/Ast.zig @@ -140,9 +140,20 @@ pub fn tokenLocation(self: Ast, start_offset: ByteOffset, token_index: TokenInde .line_end = self.source.len, }; const token_start = self.tokens.items(.start)[token_index]; - for (self.source[start_offset..], 0..) |c, i| { - if (i + start_offset == token_start) { - loc.line_end = i + start_offset; + + // Scan to by line until we go past the token start + while (std.mem.indexOfScalarPos(u8, self.source, loc.line_start, '\n')) |i| { + if (i >= token_start) { + break; // Went past + } + loc.line += 1; + loc.line_start = i + 1; + } + + const offset = loc.line_start; + for (self.source[offset..], 0..) |c, i| { + if (i + offset == token_start) { + loc.line_end = i + offset; while (loc.line_end < self.source.len and self.source[loc.line_end] != '\n') { loc.line_end += 1; }