Skip to content

Commit 58543ad

Browse files
authored
Fix the bug where prefix buffers too much (#210)
* Change `_fill` and `__fill` to better implementation and rename to `buffer!`. * Bug fix of `prefix(::BufferedInput, ::Integer)`. Change to not overbuffer. This bug fix brake the test `windows_newlines` but I think the test is incorrect. * Make it explicit that scalar addition then vector addition.
1 parent 0c8e9a1 commit 58543ad

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

src/buffered_input.jl

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,35 @@ mutable struct BufferedInput
1515
end
1616
end
1717

18-
19-
# Read and buffer n more characters
20-
function __fill(bi::BufferedInput, bi_input::IO, n::Integer)
21-
for _ in 1:n
22-
c = eof(bi_input) ? '\0' : read(bi_input, Char)
23-
i = bi.offset + bi.avail + 1
18+
# Read and buffer `n` more characters
19+
function buffer!(bi::BufferedInput, n::Integer)::Nothing
20+
for i in (bi.offset + bi.avail) .+ (1:n)
21+
c = eof(bi.input) ? '\0' : read(bi.input, Char)
2422
if i length(bi.buffer)
2523
bi.buffer[i] = c
2624
else
2725
push!(bi.buffer, c)
2826
end
29-
bi.avail += 1
3027
end
28+
bi.avail += n
29+
nothing
3130
end
3231

33-
_fill(bi::BufferedInput, n::Integer) = __fill(bi, bi.input, n)
34-
3532
# Peek the character in the i-th position relative to the current position.
3633
# (0-based)
3734
function peek(bi::BufferedInput, i::Integer=0)
3835
i1 = i + 1
3936
if bi.avail < i1
40-
_fill(bi, i1 - bi.avail)
37+
buffer!(bi, i1 - bi.avail)
4138
end
4239
bi.buffer[bi.offset + i1]
4340
end
4441

4542

4643
# Return the string formed from the first n characters from the current position
4744
# of the stream.
48-
function prefix(bi::BufferedInput, n::Integer=1)
49-
n1 = n + 1
50-
if bi.avail < n1
51-
_fill(bi, n1 - bi.avail)
52-
end
45+
function prefix(bi::BufferedInput, n::Integer=1)::String
46+
bi.avail < n && buffer!(bi, n - bi.avail)
5347
String(bi.buffer[bi.offset .+ (1:n)])
5448
end
5549

0 commit comments

Comments
 (0)