Skip to content

Commit

Permalink
Merge pull request #144 from omus/cv/skip
Browse files Browse the repository at this point in the history
Add support for `skip` method / More population triggers
  • Loading branch information
rofinn authored Oct 26, 2021
2 parents 647f26c + 92e4fc3 commit 97146a9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FilePathsBase"
uuid = "48062228-2e41-5def-b9a4-89aafe57970f"
authors = ["Rory Finnegan"]
version = "0.9.12"
version = "0.9.13"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
14 changes: 5 additions & 9 deletions src/buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,19 @@ end

Base.isreadable(buffer::FileBuffer) = buffer.read
Base.iswritable(buffer::FileBuffer) = buffer.write
Base.seek(buffer::FileBuffer, n::Integer) = seek(buffer.io, n)
Base.seek(buffer::FileBuffer, n::Integer) = (_read(buffer); seek(buffer.io, n))
Base.skip(buffer::FileBuffer, n::Integer) = (_read(buffer); skip(buffer.io, n))
Base.seekstart(buffer::FileBuffer) = seekstart(buffer.io)
Base.seekend(buffer::FileBuffer) = seekend(buffer.io)
Base.seekend(buffer::FileBuffer) = (_read(buffer); seekend(buffer.io))
Base.position(buffer::FileBuffer) = position(buffer.io)
function Base.eof(buffer::FileBuffer)
if position(buffer) == 0
_read(buffer)
seekstart(buffer)
end
return eof(buffer.io)
end
Base.eof(buffer::FileBuffer) = (_read(buffer); eof(buffer.io))

function _read(buffer::FileBuffer)
# If our IOBuffer is empty then populate it with the
# filepath contents
if buffer.io.size == 0
write(buffer.io, read(buffer.path))
seekstart(buffer.io)
end
end

Expand Down
34 changes: 34 additions & 0 deletions test/buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,40 @@ using FilePathsBase: FileBuffer
end
end

@testset "seek" begin
p = p"../README.md"
io = FileBuffer(p)
try
@test position(io) == 0
@test position(seekend(io)) > 0
@test position(seekstart(io)) == 0
@test position(seek(io, 5)) == 5
@test position(skip(io, 10)) == 15
finally
close(io)
end
end

@testset "populate" begin
funcs = (
:seek => io -> seek(io, 1),
:skip => io -> skip(io, 1),
:seekend => seekend,
:eof => eof,
)

@testset "$name" for (name, f) in funcs
p = p"../README.md"
buffer = FileBuffer(p)
try
f(buffer)
@test buffer.io.size > 0
finally
close(buffer)
end
end
end

@testset "write" begin
mktmpdir() do d
p1 = absolute(p"../README.md")
Expand Down

2 comments on commit 97146a9

@rofinn
Copy link
Owner Author

@rofinn rofinn commented on 97146a9 Oct 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/47558

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.9.13 -m "<description of version>" 97146a942bb337dc439790677bf52053767f8c82
git push origin v0.9.13

Please sign in to comment.