diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-04-09 00:20:26 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-04-09 00:20:26 -0700 |
| commit | cc525bc002e456cc735f66bc4d2c5267a10b6016 (patch) | |
| tree | 473870b356590dc0631e9a809403e180190d23e1 /lib/std | |
| parent | c28d1fe1733eb150f956b1eb0d01d79496e7378c (diff) | |
| parent | 9a2de796bd0eb047ca9bd23940004f3b25ab6625 (diff) | |
| download | zig-cc525bc002e456cc735f66bc4d2c5267a10b6016.tar.gz zig-cc525bc002e456cc735f66bc4d2c5267a10b6016.zip | |
Merge pretty printing compile errors branch
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/debug.zig | 2 | ||||
| -rw-r--r-- | lib/std/zig.zig | 25 |
2 files changed, 22 insertions, 5 deletions
diff --git a/lib/std/debug.zig b/lib/std/debug.zig index a7badf7ed1..c84a0e0f18 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -462,7 +462,7 @@ pub const TTY = struct { // TODO give this a payload of file handle windows_api, - fn setColor(conf: Config, out_stream: anytype, color: Color) void { + pub fn setColor(conf: Config, out_stream: anytype, color: Color) void { nosuspend switch (conf) { .no_color => return, .escape_codes => switch (color) { 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 { |
