From aa04daecaa261e51ca78b993ee6e9363cd51183f Mon Sep 17 00:00:00 2001 From: Koki Fushimi Date: Mon, 17 Jun 2024 17:12:53 +0900 Subject: [PATCH 1/3] Change `_fill` and `__fill` to better implementation and rename to `buffer!`. --- src/buffered_input.jl | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/buffered_input.jl b/src/buffered_input.jl index dfa7b5a..af1ec0d 100644 --- a/src/buffered_input.jl +++ b/src/buffered_input.jl @@ -15,29 +15,26 @@ mutable struct BufferedInput end end - -# Read and buffer n more characters -function __fill(bi::BufferedInput, bi_input::IO, n::Integer) - for _ in 1:n - c = eof(bi_input) ? '\0' : read(bi_input, Char) - i = bi.offset + bi.avail + 1 +# Read and buffer `n` more characters +function buffer!(bi::BufferedInput, n::Integer)::Nothing + for i in bi.offset + bi.avail .+ (1:n) + c = eof(bi.input) ? '\0' : read(bi.input, Char) if i ≤ length(bi.buffer) bi.buffer[i] = c else push!(bi.buffer, c) end - bi.avail += 1 end + bi.avail += n + nothing end -_fill(bi::BufferedInput, n::Integer) = __fill(bi, bi.input, n) - # Peek the character in the i-th position relative to the current position. # (0-based) function peek(bi::BufferedInput, i::Integer=0) i1 = i + 1 if bi.avail < i1 - _fill(bi, i1 - bi.avail) + buffer!(bi, i1 - bi.avail) end bi.buffer[bi.offset + i1] end @@ -48,7 +45,7 @@ end function prefix(bi::BufferedInput, n::Integer=1) n1 = n + 1 if bi.avail < n1 - _fill(bi, n1 - bi.avail) + buffer!(bi, n1 - bi.avail) end String(bi.buffer[bi.offset .+ (1:n)]) end From d2014f8454ad29e10e491752514ac4e808c2a7cf Mon Sep 17 00:00:00 2001 From: Koki Fushimi Date: Mon, 17 Jun 2024 17:20:14 +0900 Subject: [PATCH 2/3] Refactoring of `peek` functions. --- src/buffered_input.jl | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/buffered_input.jl b/src/buffered_input.jl index af1ec0d..109993d 100644 --- a/src/buffered_input.jl +++ b/src/buffered_input.jl @@ -29,16 +29,14 @@ function buffer!(bi::BufferedInput, n::Integer)::Nothing nothing end -# Peek the character in the i-th position relative to the current position. -# (0-based) -function peek(bi::BufferedInput, i::Integer=0) - i1 = i + 1 - if bi.avail < i1 - buffer!(bi, i1 - bi.avail) - end - bi.buffer[bi.offset + i1] +# Peek the character in the `i`-th position relative to the current position. +function peek1(bi::BufferedInput, i::Integer=1)::Char + bi.avail < i && buffer!(bi, i - bi.avail) + bi.buffer[bi.offset + i] end +# peek function for 0-based indices +peek(bi::BufferedInput, i::Integer=0) = peek1(bi, i + 1) # Return the string formed from the first n characters from the current position # of the stream. From 5d6632c71e35d1ae75f81aaeae1667b74032624a Mon Sep 17 00:00:00 2001 From: Koki Fushimi Date: Tue, 18 Jun 2024 13:31:45 +0900 Subject: [PATCH 3/3] Make it explicit that scalar addition then vector addition. --- src/buffered_input.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/buffered_input.jl b/src/buffered_input.jl index af1ec0d..41d6ca4 100644 --- a/src/buffered_input.jl +++ b/src/buffered_input.jl @@ -17,7 +17,7 @@ end # Read and buffer `n` more characters function buffer!(bi::BufferedInput, n::Integer)::Nothing - for i in bi.offset + bi.avail .+ (1:n) + for i in (bi.offset + bi.avail) .+ (1:n) c = eof(bi.input) ? '\0' : read(bi.input, Char) if i ≤ length(bi.buffer) bi.buffer[i] = c