1const std = @import("std");
2const builtin = @import("builtin");
3const extras = @import("extras");
4const nio = @import("./nio.zig");
5
6pub fn SkipReader(comptime ReaderType: type) type {
7 return struct {
8 source_reader: ReaderType,
9 needles: []const u8,
10
11 const Self = @This();
12
13 pub fn init(source_reader: ReaderType, needles: []const u8) Self {
14 return .{
15 .source_reader = source_reader,
16 .needles = needles,
17 };
18 }
19
20 pub fn from(source_reader: anytype, needles: []const u8) SkipReader(@TypeOf(source_reader)) {
21 return .init(source_reader, needles);
22 }
23
24 const R = nio.Readable(@This(), ._var);
25 pub const readAll = R.readAll;
26 pub const readAtLeast = R.readAtLeast;
27 pub const readNoEof = R.readNoEof;
28 pub const readAllAlloc = R.readAllAlloc;
29 pub const readArray = R.readArray;
30 pub const readByte = R.readByte;
31 pub const readUntilDelimiterArrayList = R.readUntilDelimiterArrayList;
32 pub const readUntilDelimiterAlloc = R.readUntilDelimiterAlloc;
33 pub const readUntilDelimiterOrEofAlloc = R.readUntilDelimiterOrEofAlloc;
34 pub const readUntilDelimitersBuf = R.readUntilDelimitersBuf;
35 pub const readUntilDelimitersArrayList = R.readUntilDelimitersArrayList;
36 pub const readAlloc = R.readAlloc;
37 pub const readInt = R.readInt;
38 pub const readUntilDelimitersAlloc = R.readUntilDelimitersAlloc;
39 pub const readUntilDelimiter = R.readUntilDelimiter;
40 pub const readUntilDelimiterOrEof = R.readUntilDelimiterOrEof;
41 pub const readExpected = R.readExpected;
42 pub const readType = R.readType;
43 pub const skipBytes = R.skipBytes;
44 pub const skipUntilDelimiterOrEof = R.skipUntilDelimiterOrEof;
45 pub const pipeTo = R.pipeTo;
46
47 pub const ReadError = extras.Pointee(ReaderType).ReadError;
48 pub fn read(self: *Self, dest: []u8) ReadError!usize {
49 var i: usize = 0;
50 while (i < dest.len) {
51 dest[i] = self.source_reader.readByte() catch |err| switch (err) {
52 error.EndOfStream => break,
53 else => |e| return e,
54 };
55 if (std.mem.findScalar(u8, self.needles, dest[i]) != null) continue;
56 i += 1;
57 }
58 return i;
59 }
60
61 pub fn anyReadable(self: *Self) nio.AnyReadable {
62 const S = struct {
63 fn read(s: *allowzero anyopaque, buffer: []u8) anyerror!usize {
64 const br: *Self = @ptrCast(@alignCast(s));
65 return br.read(buffer);
66 }
67 };
68 return .{
69 .vtable = &.{ .read = S.read },
70 .state = @ptrCast(self),
71 };
72 }
73 };
74}