authorgravatar for frmdstryr@protonmail.comfrmdstryr <frmdstryr@protonmail.com> 2023-11-08 20:45:25-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-11-09 01:45:25+00:00
logf258a391daa31b3ba2c37d879db96fadc0c058f3
tree07b32cd0d52119e8b1cf17d1679726d43ee7c54e
parent2ac0ba03a60a133e7ffbab9ad6aaf3b54f6c747b
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Speed up ast.tokenLocation


1 files changed, 14 insertions(+), 3 deletions(-)

lib/std/zig/Ast.zig+14-3
......@@ -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 }