Skip to content

Commit 8e62f89

Browse files
committed
Fix JuliaData/CSV.jl#306 by ensuring sentinel values get matched before trying to parse type values. Turns out we were already doing this for Dates/Strings, just not other type parsers
1 parent 7ca4166 commit 8e62f89

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

src/Parsers.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,17 +488,19 @@ Sentinel(sentinels::Union{String, Vector{String}}) = Sentinel(defaultparser, Tri
488488
@inline function parse!(s::Sentinel, io::IO, r::Result{T}; kwargs...) where {T}
489489
# @debug "xparse Sentinel - $T"
490490
pos = position(io)
491+
if !isempty(s.sentinels.leaves)
492+
if match!(s.sentinels, io, r)
493+
setfield!(r, 3, Int64(pos))
494+
r.code &= ~INVALID
495+
return r
496+
end
497+
end
491498
parse!(s.next, io, r; kwargs...)
492499
# @debug "Sentinel - $T: result.code=$(result.code), result.result=$(result.result)"
493500
if !ok(r.code)
494501
if isempty(s.sentinels.leaves) && position(io) == pos
495502
r.code &= ~INVALID
496503
r.code |= (SENTINEL | ifelse(eof(io), EOF, SUCCESS))
497-
else
498-
fastseek!(io, pos)
499-
if match!(s.sentinels, io, r)
500-
r.code &= ~INVALID
501-
end
502504
end
503505
end
504506
return r

test/bools.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ r = Parsers.parse(Parsers.Sentinel(["NA"]), IOBuffer("NA"), Bool)
100100
@test r.code == SENTINEL | EOF
101101
@test r.pos == 0
102102
r = Parsers.parse(Parsers.Sentinel(["false"]), IOBuffer("false"), Bool)
103-
@test r.result === false
104-
@test r.code == OK | EOF
103+
@test r.result === missing
104+
@test r.code == SENTINEL | EOF
105105
@test r.pos == 0
106106
r = Parsers.parse(Parsers.Sentinel(["fals"]), IOBuffer("falsee"), Bool)
107-
@test r.result === false
108-
@test r.code == OK
107+
@test r.result === missing
108+
@test r.code == SENTINEL
109109
@test r.pos == 0
110110
r = Parsers.parse(Parsers.Sentinel(["fals"]), IOBuffer("fals"), Bool)
111111
@test r.result === missing

test/runtests.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,15 @@ end
663663
@test Parsers.tryparse(int2str, SubString("101"), Int) === 101
664664
@test Parsers.tryparse(int2str, IOBuffer(SubString("101")), Int) === 101
665665

666+
# https://github.com/JuliaData/CSV.jl/issues/306
667+
# ensure sentinels are matched before trying to parse type values
668+
r = Parsers.parse(Parsers.Delimited(Parsers.Quoted(Parsers.Sentinel(["1"]))), IOBuffer("1"), Int)
669+
@test r.result === missing
670+
@test r.code === SENTINEL | EOF
671+
r = Parsers.parse(Parsers.Delimited(Parsers.Quoted(Parsers.Sentinel(["1"]))), IOBuffer("1"), String)
672+
@test r.result === missing
673+
@test r.code === SENTINEL | EOF
674+
666675
end # @testset
667676

668677
end # @testset

0 commit comments

Comments
 (0)