Parse_words.zig
· 2.0 KiB · Zig
Raw
const std = @import("std");
const mem = std.mem;
const fs = std.fs;
const print = std.debug.print;
const ParseError = error{
RanOutOfWords,
};
const Parser = struct {
it: mem.SplitIterator(u8, .scalar),
fn fromString(str: []const u8) Parser {
return Parser{
.it = mem.splitScalar(u8, str, ' '),
};
}
fn take(self: *Parser) ![]const u8 {
const wordo = self.it.next();
if (wordo) |word| {
return word;
} else {
return ParseError.RanOutOfWords;
}
}
fn skip(self: *Parser, cnt: usize) void {
for (0..cnt) |_| {
_ = self.it.next();
}
}
fn takeAsNumber(self: *Parser, Type: type) !Type {
const wordo = self.it.next();
if (wordo) |word| {
return std.fmt.parseInt(Type, word, 10);
} else {
return ParseError.RanOutOfWords;
}
}
};
const Raindeer = struct {
speed: usize,
flight: usize,
rest: usize,
};
fn getLines(path: []const u8, alloc: mem.Allocator) !std.ArrayList(Raindeer) {
const file = fs.cwd().openFile(path, .{}) catch |err| {
std.log.err("Failed to open file {s}", .{@errorName(err)});
return err;
};
defer file.close();
var raindeer = std.ArrayList(Raindeer).init(alloc);
while (file.reader().readUntilDelimiterOrEofAlloc(alloc, '\n', std.math.maxInt(usize)) catch |err| {
std.log.err("Failed to read line: {s}", .{@errorName(err)});
return err;
}) |line| {
defer alloc.free(line);
var words = Parser.fromString(line);
words.skip(3);
const speed = try words.takeAsNumber(usize);
words.skip(2);
const flight = try words.takeAsNumber(usize);
words.skip(6);
const rest = try words.takeAsNumber(usize);
try raindeer.append(.{
.speed = speed,
.flight = flight,
.rest = rest,
});
}
return raindeer;
}
1 | const std = @import("std"); |
2 | const mem = std.mem; |
3 | const fs = std.fs; |
4 | const print = std.debug.print; |
5 | |
6 | const ParseError = error{ |
7 | RanOutOfWords, |
8 | }; |
9 | |
10 | const Parser = struct { |
11 | it: mem.SplitIterator(u8, .scalar), |
12 | |
13 | fn fromString(str: []const u8) Parser { |
14 | return Parser{ |
15 | .it = mem.splitScalar(u8, str, ' '), |
16 | }; |
17 | } |
18 | |
19 | fn take(self: *Parser) ![]const u8 { |
20 | const wordo = self.it.next(); |
21 | if (wordo) |word| { |
22 | return word; |
23 | } else { |
24 | return ParseError.RanOutOfWords; |
25 | } |
26 | } |
27 | |
28 | fn skip(self: *Parser, cnt: usize) void { |
29 | for (0..cnt) |_| { |
30 | _ = self.it.next(); |
31 | } |
32 | } |
33 | |
34 | fn takeAsNumber(self: *Parser, Type: type) !Type { |
35 | const wordo = self.it.next(); |
36 | if (wordo) |word| { |
37 | return std.fmt.parseInt(Type, word, 10); |
38 | } else { |
39 | return ParseError.RanOutOfWords; |
40 | } |
41 | } |
42 | }; |
43 | |
44 | const Raindeer = struct { |
45 | speed: usize, |
46 | flight: usize, |
47 | rest: usize, |
48 | }; |
49 | |
50 | fn getLines(path: []const u8, alloc: mem.Allocator) !std.ArrayList(Raindeer) { |
51 | const file = fs.cwd().openFile(path, .{}) catch |err| { |
52 | std.log.err("Failed to open file {s}", .{@errorName(err)}); |
53 | return err; |
54 | }; |
55 | defer file.close(); |
56 | |
57 | var raindeer = std.ArrayList(Raindeer).init(alloc); |
58 | |
59 | while (file.reader().readUntilDelimiterOrEofAlloc(alloc, '\n', std.math.maxInt(usize)) catch |err| { |
60 | std.log.err("Failed to read line: {s}", .{@errorName(err)}); |
61 | return err; |
62 | }) |line| { |
63 | defer alloc.free(line); |
64 | var words = Parser.fromString(line); |
65 | words.skip(3); |
66 | const speed = try words.takeAsNumber(usize); |
67 | words.skip(2); |
68 | const flight = try words.takeAsNumber(usize); |
69 | words.skip(6); |
70 | const rest = try words.takeAsNumber(usize); |
71 | try raindeer.append(.{ |
72 | .speed = speed, |
73 | .flight = flight, |
74 | .rest = rest, |
75 | }); |
76 | } |
77 | return raindeer; |
78 | } |
79 |