Skip to content

reader.takeDelimiterExclusive('\n') behaves incorrectly #25597

Description

@xingyuli

Zig Version

0.15.2

Steps to Reproduce and Observed Behavior

Run this Zig program, it failed to read the file as it is previously in 0.15.1.

With file content intest_data:

123456
abcd
rer

And program in read_file.zig:

const std = @import("std");

pub fn main() !void {
    const f = try std.fs.cwd().openFile("test_data", .{});
    defer f.close();

    var buf: [1024]u8 = undefined;
    var file_reader = f.reader(&buf);

    const reader = &file_reader.interface;

    while (reader.takeDelimiterExclusive('\n')) |line| {
        std.log.debug("1: '{s}'", .{line});

        if (line.len == 0) {
            // std.log.debug("2: '{s}'", .{reader.buffered()});
            break;
        }
    } else |err| switch (err) {
        error.EndOfStream => {
            std.log.debug("eof", .{});
        },
        error.StreamTooLong, error.ReadFailed => return err,
    }
}

It simply produces wrong output:

$ rm -rf .zig-cache zig-out && zig version && zig run read_file.zig
0.15.2
debug: 1: '123456'
debug: 1: ''

Expected Behavior

$ rm -rf .zig-cache zig-out && zig version && zig run read_file.zig
0.15.1
debug: 1: '123456'
debug: 1: 'abcd'
debug: 1: 'rer'
debug: eof

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionNo questions on the issue tracker, please.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions