Sobeston / zig.guide

Repo for https://zig.guide content. Get up to speed with Zig quickly.
https://zig.guide
MIT License
689 stars 177 forks source link

[Chapter 2] A common usecase for readers is to read until the next line (e.g. for user input) #102

Closed maxxnino closed 3 years ago

maxxnino commented 3 years ago
fn nextLine(reader: anytype, buffer: []u8) !?[]const u8 {
    var line = (try reader.readUntilDelimiterOrEof(
        buffer,
        '\n',
    )) orelse return null;
    // trim annoying windows-only carriage return character
    if (std.builtin.Target.current.os.tag == .windows) {
        line = std.mem.trimRight(u8, line, "\r");
    }
    return line;
}

std.mem.trimRight return []const u8 but line variable is not const, so this can't compile on window

line = std.mem.trimRight(u8, line, "\r");

I dont't know much about zig so my dirty fix is

if (std.builtin.Target.current.os.tag == .windows) {
   return std.mem.trimRight(u8, line, "\r");
}

zig version 0.7.1

Sobeston commented 3 years ago

std.mem.trimRight return []const u8 but line variable is not const, so this can't compile on window

Const pointers aren't the same thing as const values; you can reassign variables of const pointer types.

This compiles with Zig master; Zig 0.7.1 isn't supported.