Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring of src/buffered_input.jl #156

Merged
merged 5 commits into from
Jun 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/buffered_input.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ mutable struct BufferedInput
avail::UInt64

function BufferedInput(input::IO)
return new(input, Vector{Char}(undef, 0), 0, 0)
return new(input, Char[], 0, 0)
end
end


# Read and buffer n more characters
function __fill(bi::BufferedInput, bi_input::IO, n::Integer)
for i in 1:n
for _ in 1:n
c = eof(bi_input) ? '\0' : read(bi_input, Char)
if bi.offset + bi.avail + 1 <= length(bi.buffer)
bi.buffer[bi.offset + bi.avail + 1] = c
i = bi.offset + bi.avail + 1
if i ≤ length(bi.buffer)
bi.buffer[i] = c
else
push!(bi.buffer, c)
end
Expand All @@ -34,20 +35,22 @@ _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)
if bi.avail < i + 1
_fill(bi, i + 1 - bi.avail)
i1 = i + 1
if bi.avail < i1
_fill(bi, i1 - bi.avail)
end
return bi.buffer[bi.offset + i + 1]
bi.buffer[bi.offset + i1]
end


# Return the string formed from the first n characters from the current position
# of the stream.
function prefix(bi::BufferedInput, n::Integer=1)
if bi.avail < n + 1
_fill(bi, n + 1 - bi.avail)
n1 = n + 1
if bi.avail < n1
_fill(bi, n1 - bi.avail)
end
return string(bi.buffer[(bi.offset + 1):(bi.offset + n)]...)
String(bi.buffer[bi.offset .+ (1:n)])
end


Expand Down
Loading