-
Notifications
You must be signed in to change notification settings - Fork 16
Support Strings (multiple chars) as 'quotechars' #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,8 +41,8 @@ end | |
| * `sentinel=nothing`: valid values include: `nothing` meaning don't check for sentinel values; `missing` meaning an "empty field" should be considered a sentinel value; or a `Vector{String}` of the various string values that should each be checked as a sentinel value. Note that sentinels will always be checked longest to shortest, with the longest valid match taking precedence. | ||
| * `wh1=' '`: the first ascii character to be considered when ignoring leading/trailing whitespace in value parsing | ||
| * `wh2='\t'`: the second ascii character to be considered when ignoring leading/trailing whitespace in value parsing | ||
| * `openquotechar='"'`: the ascii character that signals a "quoted" field while parsing; subsequent characters will be treated as non-significant until a valid `closequotechar` is detected | ||
| * `closequotechar='"'`: the ascii character that signals the end of a quoted field | ||
| * `openquotechar='"'`: the ascii character or string that signals a "quoted" field while parsing; subsequent characters will be treated as non-significant until a valid `closequotechar` is detected | ||
| * `closequotechar='"'`: the ascii character or string that signals the end of a quoted field | ||
| * `escapechar='"'`: an ascii character used to "escape" a `closequotechar` within a quoted field | ||
| * `delim=nothing`: if `nothing`, no delimiter will be checked for; if a `Char` or `String`, a delimiter will be checked for directly after parsing a value or `closequotechar`; a newline (`\n`), return (`\r`), or CRLF (`"\r\n"`) are always considered "delimiters", in addition to EOF | ||
| * `decimal='.'`: an ascii character to be used when parsing float values that separates a decimal value | ||
|
|
@@ -65,8 +65,8 @@ struct Options | |
| wh1::UInt8 | ||
| wh2::UInt8 | ||
| quoted::Bool | ||
| oq::UInt8 | ||
| cq::UInt8 | ||
| oq::Union{UInt8, PtrLen} | ||
| cq::Union{UInt8, PtrLen} | ||
| e::UInt8 | ||
| delim::Union{Nothing, UInt8, PtrLen} | ||
| decimal::UInt8 | ||
|
|
@@ -83,11 +83,11 @@ asciival(c::Char) = isascii(c) | |
| asciival(b::UInt8) = b < 0x80 | ||
|
|
||
| function Options( | ||
| sentinel::Union{Nothing, Missing, Vector{String}}, | ||
| sentinel::Union{Nothing, Missing, Vector{String}}, | ||
| wh1::Union{UInt8, Char}, | ||
| wh2::Union{UInt8, Char}, | ||
| oq::Union{UInt8, Char}, | ||
| cq::Union{UInt8, Char}, | ||
| oq::Union{UInt8, Char, String}, | ||
| cq::Union{UInt8, Char, String}, | ||
| e::Union{UInt8, Char}, | ||
| delim::Union{Nothing, UInt8, Char, String}, | ||
| decimal::Union{UInt8, Char}, | ||
|
|
@@ -96,15 +96,20 @@ function Options( | |
| dateformat::Union{Nothing, String, Dates.DateFormat, Format}, | ||
| ignorerepeated, ignoreemptylines, comment, quoted, debug, stripwhitespace=false, stripquoted=false) | ||
| asciival(wh1) && asciival(wh2) || throw(ArgumentError("whitespace characters must be ASCII")) | ||
| asciival(oq) && asciival(cq) && asciival(e) || throw(ArgumentError("openquotechar, closequotechar, and escapechar must be ASCII characters")) | ||
| (oq isa String || asciival(oq)) && (cq isa String || asciival(cq)) && asciival(e) || throw(ArgumentError("openquotechar, closequotechar, and escapechar must be ASCII characters")) | ||
| (oq == delim) || (cq == delim) || (e == delim) && throw(ArgumentError("delim argument must be different than openquotechar, closequotechar, and escapechar arguments")) | ||
| if sentinel isa Vector{String} | ||
| for sent in sentinel | ||
| if startswith(sent, string(Char(wh1))) || startswith(sent, string(Char(wh2))) | ||
| throw(ArgumentError("sentinel value isn't allowed to start with wh1 or wh2 characters")) | ||
| end | ||
| if startswith(sent, string(Char(oq))) || startswith(sent, string(Char(cq))) | ||
| if ( | ||
| ((oq isa UInt8 || oq isa Char) && startswith(sent, string(Char(oq)))) || | ||
| ((cq isa UInt8 || cq isa Char) && startswith(sent, string(Char(cq)))) | ||
| ) | ||
| throw(ArgumentError("sentinel value isn't allowed to start with openquotechar, closequotechar, or escapechar characters")) | ||
| elseif (oq isa String && startswith(sent, oq)) || (cq isa String && startswith(sent, cq)) | ||
| throw(ArgumentError("sentinel value isn't allowed to start with openquote or closequote string")) | ||
| end | ||
| if (delim isa UInt8 || delim isa Char) && startswith(sent, string(Char(delim))) | ||
| throw(ArgumentError("sentinel value isn't allowed to start with a delimiter character")) | ||
|
|
@@ -117,6 +122,8 @@ function Options( | |
| refs = [""] | ||
| end | ||
| sent = sentinel === nothing || sentinel === missing ? sentinel : prepare(sentinel) | ||
| openq = oq isa String ? ptrlen(oq) : oq % UInt8 | ||
| closeq = cq isa String ? ptrlen(cq) : cq % UInt8 | ||
| del = delim === nothing ? nothing : delim isa String ? ptrlen(delim) : delim % UInt8 | ||
| if del isa UInt8 | ||
| ((wh1 % UInt8) == del || (wh2 % UInt8) == del) && throw(ArgumentError("whitespace characters (`wh1=' '` and `wh2='\\t'` default keyword arguments) must be different than delim argument")) | ||
|
|
@@ -136,15 +143,15 @@ function Options( | |
| cmt = ptrlen(comment) | ||
| end | ||
| df = dateformat === nothing ? nothing : dateformat isa String ? Format(dateformat) : dateformat isa Dates.DateFormat ? Format(dateformat) : dateformat | ||
| return Options(refs, sent, ignorerepeated, ignoreemptylines, wh1 % UInt8, wh2 % UInt8, quoted, oq % UInt8, cq % UInt8, e % UInt8, del, decimal % UInt8, trues, falses, df, cmt, stripwhitespace || stripquoted, stripquoted) | ||
| return Options(refs, sent, ignorerepeated, ignoreemptylines, wh1 % UInt8, wh2 % UInt8, quoted, openq, closeq, e % UInt8, del, decimal % UInt8, trues, falses, df, cmt, stripwhitespace || stripquoted, stripquoted) | ||
| end | ||
|
|
||
| Options(; | ||
| sentinel::Union{Nothing, Missing, Vector{String}}=nothing, | ||
| wh1::Union{UInt8, Char}=UInt8(' '), | ||
| wh2::Union{UInt8, Char}=UInt8('\t'), | ||
| openquotechar::Union{UInt8, Char}=UInt8('"'), | ||
| closequotechar::Union{UInt8, Char}=UInt8('"'), | ||
| openquotechar::Union{UInt8, Char, String}=UInt8('"'), | ||
| closequotechar::Union{UInt8, Char, String}=UInt8('"'), | ||
| escapechar::Union{UInt8, Char}=UInt8('"'), | ||
| delim::Union{Nothing, UInt8, Char, String}=nothing, | ||
| decimal::Union{UInt8, Char}=UInt8('.'), | ||
|
|
@@ -209,7 +216,7 @@ A [`Parsers.Result`](@ref) struct is returned, with the following fields: | |
| function xparse end | ||
|
|
||
| # for testing purposes only, it's much too slow to dynamically create Options for every xparse call | ||
| function xparse(::Type{T}, buf::Union{AbstractVector{UInt8}, AbstractString, IO}; pos::Integer=1, len::Integer=buf isa IO ? 0 : sizeof(buf), sentinel=nothing, wh1::Union{UInt8, Char}=UInt8(' '), wh2::Union{UInt8, Char}=UInt8('\t'), quoted::Bool=true, openquotechar::Union{UInt8, Char}=UInt8('"'), closequotechar::Union{UInt8, Char}=UInt8('"'), escapechar::Union{UInt8, Char}=UInt8('"'), ignorerepeated::Bool=false, ignoreemptylines::Bool=false, delim::Union{UInt8, Char, PtrLen, AbstractString, Nothing}=UInt8(','), decimal::Union{UInt8, Char}=UInt8('.'), comment=nothing, trues=nothing, falses=nothing, dateformat::Union{Nothing, String, Dates.DateFormat}=nothing, debug::Bool=false, stripwhitespace::Bool=false, stripquoted::Bool=false) where {T} | ||
| function xparse(::Type{T}, buf::Union{AbstractVector{UInt8}, AbstractString, IO}; pos::Integer=1, len::Integer=buf isa IO ? 0 : sizeof(buf), sentinel=nothing, wh1::Union{UInt8, Char}=UInt8(' '), wh2::Union{UInt8, Char}=UInt8('\t'), quoted::Bool=true, openquotechar::Union{UInt8, Char, String}=UInt8('"'), closequotechar::Union{UInt8, Char, String}=UInt8('"'), escapechar::Union{UInt8, Char}=UInt8('"'), ignorerepeated::Bool=false, ignoreemptylines::Bool=false, delim::Union{UInt8, Char, PtrLen, AbstractString, Nothing}=UInt8(','), decimal::Union{UInt8, Char}=UInt8('.'), comment=nothing, trues=nothing, falses=nothing, dateformat::Union{Nothing, String, Dates.DateFormat}=nothing, debug::Bool=false, stripwhitespace::Bool=false, stripquoted::Bool=false) where {T} | ||
| options = Options(sentinel, wh1, wh2, openquotechar, closequotechar, escapechar, delim, decimal, trues, falses, dateformat, ignorerepeated, ignoreemptylines, comment, quoted, debug, stripwhitespace, stripquoted) | ||
| return xparse(T, buf, pos, len, options) | ||
| end | ||
|
|
@@ -286,12 +293,12 @@ function xparse(::Type{T}, source::Union{AbstractVector{UInt8}, IO}, pos, len, o | |
| end | ||
| # check for start of quoted field | ||
| if options.quoted | ||
| quoted = b == options.oq | ||
| preqpos = pos | ||
| pos = checkquote(source, pos, len, options.oq) | ||
| quoted = pos > preqpos | ||
| if quoted | ||
| code = QUOTED | ||
| pos += 1 | ||
| vstartpos = pos | ||
| incr!(source) | ||
| if eof(source, pos, len) | ||
| code |= INVALID_QUOTED_FIELD | ||
| @goto donedone | ||
|
|
@@ -359,9 +366,9 @@ function xparse(::Type{T}, source::Union{AbstractVector{UInt8}, IO}, pos, len, o | |
| same = options.cq == options.e | ||
| first = true | ||
| while true | ||
| pos += 1 | ||
| incr!(source) | ||
| if same && b == options.e | ||
| pos += 1 | ||
| incr!(source) | ||
| if eof(source, pos, len) | ||
| code |= EOF | ||
| if !first | ||
|
|
@@ -378,6 +385,8 @@ function xparse(::Type{T}, source::Union{AbstractVector{UInt8}, IO}, pos, len, o | |
| pos += 1 | ||
| incr!(source) | ||
| elseif b == options.e | ||
| pos += 1 | ||
| incr!(source) | ||
| if eof(source, pos, len) | ||
| code |= INVALID_QUOTED_FIELD | EOF | ||
| @goto donedone | ||
|
|
@@ -386,6 +395,8 @@ function xparse(::Type{T}, source::Union{AbstractVector{UInt8}, IO}, pos, len, o | |
| pos += 1 | ||
| incr!(source) | ||
| elseif b == options.cq | ||
| pos += 1 | ||
| incr!(source) | ||
| if !first | ||
| code |= INVALID | ||
| end | ||
|
|
@@ -394,6 +405,22 @@ function xparse(::Type{T}, source::Union{AbstractVector{UInt8}, IO}, pos, len, o | |
| @goto donedone | ||
| end | ||
| break | ||
| else | ||
| preqpos = pos | ||
| pos = checkquote(source, pos, len, options.cq) | ||
| if pos > preqpos | ||
| if !first | ||
| code |= INVALID | ||
| end | ||
| if eof(source, pos, len) | ||
| code |= EOF | ||
| @goto donedone | ||
| end | ||
| break | ||
| else | ||
| pos += 1 | ||
| incr!(source) | ||
| end | ||
| end | ||
| if eof(source, pos, len) | ||
| code |= INVALID_QUOTED_FIELD | EOF | ||
|
|
@@ -765,7 +792,23 @@ end | |
| end | ||
| end | ||
|
|
||
| function checkdelim!(buf, pos, len, options::Options) | ||
| function checkquote(source, pos, len, q::PtrLen) | ||
| eof(source, pos, len) && return pos | ||
| return checkdelim(source, pos, len, q) # calls `incr!` if `q` matches. | ||
| end | ||
|
|
||
| function checkquote(source, pos, len, q::UInt8) | ||
| eof(source, pos, len) && return pos | ||
| b = peekbyte(source, pos) | ||
| if b == q | ||
| pos += 1 | ||
| incr!(source) | ||
| end | ||
| return pos | ||
| end | ||
|
|
||
| # Used by CSV.jl | ||
| function checkdelim!(buf::AbstractVector{UInt8}, pos, len, options::Options) | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added comment and type annotation here because i ended up being led astray by this function... and then wondered why we had it at all 😂 |
||
| pos > len && return pos | ||
| delim = options.delim | ||
| @inbounds b = buf[pos] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to think about how
delimandoq/cqinteract e.g. whendelim::Char in oq::StringThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should probably just check the
delimandoq/cq(i) are not equal, (ii) none of the chars in one are in the other