From: Brendan Hansen Date: Tue, 6 Apr 2021 04:39:44 +0000 (-0500) Subject: tiny updates to stream reader X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=9b1b39811ce973f71dbec39aa0ff3dab0f3f74ae;p=onyx.git tiny updates to stream reader --- diff --git a/core/io/reader.onyx b/core/io/reader.onyx index 347511aa..8ab638a0 100644 --- a/core/io/reader.onyx +++ b/core/io/reader.onyx @@ -53,7 +53,7 @@ read_u64 :: (use reader: ^Reader) -> u64 { return n; } -read_line :: (use reader: ^Reader, allocator := context.allocator) -> str { +read_line :: (use reader: ^Reader, consume_newline := true, allocator := context.allocator) -> str { _, curr_pos := stream_tell(stream); count := 0; @@ -65,6 +65,8 @@ read_line :: (use reader: ^Reader, allocator := context.allocator) -> str { if err != Error.None do break; } + if consume_newline do count += 1; + stream_seek(stream, curr_pos, SeekFrom.Start); out := str.{ diff --git a/tests/aoc-2020/day24.onyx b/tests/aoc-2020/day24.onyx index 57c4b38a..099d3414 100644 --- a/tests/aoc-2020/day24.onyx +++ b/tests/aoc-2020/day24.onyx @@ -1,7 +1,6 @@ #load "core/std" use package core -use package core.string.reader as reader Vec2 :: struct { x: i32 = 0; @@ -30,13 +29,14 @@ Cell :: struct { main :: (args: [] cstr) { contents := #file_contents "./tests/aoc-2020/input/day24.txt"; - file := reader.make(contents); + file_stream := io.string_stream_make(contents); + file := io.reader_make(^file_stream); grid := map.make(Vec2, Cell, .{}, 1021); // `true` is black defer map.free(^grid); - while !reader.empty(^file) { - line := reader.read_line(^file); + while !io.stream_end_of_file(^file_stream) { + line := io.read_line(^file); loc := Vec2.{ 0, 0 }; s := 0; diff --git a/tests/string_stream_test.onyx b/tests/string_stream_test.onyx index 8fe127b2..b18e0d35 100644 --- a/tests/string_stream_test.onyx +++ b/tests/string_stream_test.onyx @@ -13,7 +13,7 @@ main :: proc (args: [] cstr) { println(word); } - println(io.read_line(^sreader, context.temp_allocator)); + println(io.read_line(^sreader, consume_newline=false, allocator=context.temp_allocator)); a := io.read_u32(^sreader); b := io.read_u32(^sreader);