Остання активність 1720079875

sotolf2 ревизій цього gist 1720079874. До ревизії

1 file changed, 78 insertions

Parse_words.zig(файл створено)

@@ -0,0 +1,78 @@
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 + }
Новіше Пізніше