Skip to content

Commit 7d87847

Browse files
committed
Properly account for pointer offset for non IOBuffers when parsing Strings. Fixes JuliaData/CSV.jl#345
1 parent 6b26654 commit 7d87847

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/strings.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ Base.isequal(x::Tuple{Ptr{UInt8}, Int}, y::String) = hash(x) === hash(y)
2525
Base.convert(::Type{String}, x::Tuple{Ptr{UInt8}, Int}) = unsafe_string(x[1], x[2])
2626

2727
const BUF = IOBuffer()
28-
getptr(io::IO, pos) = pointer(BUF.data, 1)
29-
getptr(io::IOBuffer, pos) = pointer(io.data, pos+1)
28+
getptr(io::IO, pos, ptroff) = pointer(BUF.data, 1)
29+
getptr(io::IOBuffer, pos, ptroff) = pointer(io.data, pos+1) + ptroff
3030
incr(io::IO, b) = Base.write(BUF, b)
3131
incr(io::IOBuffer, b) = 1
3232

@@ -155,7 +155,7 @@ incr(io::IOBuffer, b) = 1
155155
end
156156
# @debug "node=$node"
157157
eof(io) && (code |= EOF)
158-
ptr = getptr(io, pos) + ptroff
158+
ptr = getptr(io, pos, ptroff)
159159
if match!(node, ptr, len)
160160
code |= SENTINEL
161161
setfield!(r, 1, missing)

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,20 @@ let io=IOBuffer("1,2,null,4"), layers=Parsers.Delimited(Parsers.Quoted(Parsers.S
649649
@test r.pos == 9
650650
end
651651

652+
# https://github.com/JuliaData/CSV.jl/issues/345
653+
open("temp", "w+") do io
654+
write(io, "\"DALLAS BLACK DANCE THEATRE\",")
655+
end
656+
657+
let layers=Parsers.Delimited(Parsers.Quoted(Parsers.Sentinel(["null"])))
658+
open("temp") do io
659+
r = Parsers.parse(layers, io, String)
660+
@test r.result == "DALLAS BLACK DANCE THEATRE"
661+
@test r.code === OK | QUOTED | DELIMITED | EOF
662+
end
663+
end
664+
rm("temp")
665+
652666
# https://github.com/JuliaData/CSV.jl/issues/344
653667
open("temp", "w+") do io
654668
write(io, "1,2,null,4")

0 commit comments

Comments
 (0)