Skip to content

Commit

Permalink
Merge pull request #12 from rick2047/fix_bytes_single_byte
Browse files Browse the repository at this point in the history
Fix failing case of Bytes parser when there is only 1 byte in the stream
  • Loading branch information
gkappler authored Nov 4, 2020
2 parents 9e57b77 + b24a2b8 commit 7f382a5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/CombinedParsers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -494,8 +494,8 @@ end
If available before end of sequence, parse `N` bytes successfully with `result_type` `T`, fail otherwise.
"""
Bytes(N::Integer, T::Type=Vector{UInt8}) = Bytes{T}(N)
_iterate(parser::Bytes, sequence, till, posi, next_i, state::Nothing) =
posi+parser.N <= till ? (nextind(sequence,posi,parser.N), MatchState()) : nothing
_iterate(parser::Bytes, sequence, till, posi, next_i, state::Nothing) =
posi+parser.N <= till+1 ? (nextind(sequence,posi,parser.N), MatchState()) : nothing
_iterate(parser::Bytes, sequence, till, posi, next_i, state::MatchState) =
nothing
regex_string_(x::Bytes{N}) where N = ".{$(N)}"
Expand Down
9 changes: 7 additions & 2 deletions test/test-parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,14 @@ end
dat = 'a'^(new_Repeat_max)*'b';
@test String(parse(Repeat_stop('a','b';max=new_Repeat_max), dat))==dat[1:end-1]
end
end


@testset "Bytes" begin
# simple test for binary parsing
@test parse(Bytes(1,UInt8),[0x33]) == 0x33
@test parse(Bytes(2,UInt16),[0x33,0x66]) == 0x6633
@test parse(Bytes(4,Float32),[0x55,0x77,0x33,0x66]) == reinterpret(Float32,0x66337755)
end
end

@testset "FlatMap" begin
@test parse(
Expand Down

0 comments on commit 7f382a5

Please sign in to comment.