From 9a2de796bd0eb047ca9bd23940004f3b25ab6625 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 9 Apr 2021 00:19:44 -0700 Subject: stage2: clean up pretty printing compile errors --- lib/std/zig.zig | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'lib/std') diff --git a/lib/std/zig.zig b/lib/std/zig.zig index cff07a2bd2..f60b15f81b 100644 --- a/lib/std/zig.zig +++ b/lib/std/zig.zig @@ -31,21 +31,38 @@ pub fn hashSrc(src: []const u8) SrcHash { return out; } -pub fn findLineColumn(source: []const u8, byte_offset: usize) struct { line: usize, column: usize } { +pub const Loc = struct { + line: usize, + column: usize, + /// Does not include the trailing newline. + source_line: []const u8, +}; + +pub fn findLineColumn(source: []const u8, byte_offset: usize) Loc { var line: usize = 0; var column: usize = 0; - for (source[0..byte_offset]) |byte| { - switch (byte) { + var line_start: usize = 0; + var i: usize = 0; + while (i < byte_offset) : (i += 1) { + switch (source[i]) { '\n' => { line += 1; column = 0; + line_start = i + 1; }, else => { column += 1; }, } } - return .{ .line = line, .column = column }; + while (i < source.len and source[i] != '\n') { + i += 1; + } + return .{ + .line = line, + .column = column, + .source_line = source[line_start..i], + }; } pub fn lineDelta(source: []const u8, start: usize, end: usize) isize { -- cgit v1.2.3