From eb0f3027f4372ed44fcc44051af231efd32e927d Mon Sep 17 00:00:00 2001 From: davidanthoff Date: Wed, 11 Aug 2021 17:12:25 +0000 Subject: [PATCH] Format files using DocumentFormat --- benchmark/benchmarks.jl | 16 ++-- docs/make.jl | 10 +- src/VectorBackedStrings.jl | 4 +- src/field.jl | 146 +++++++++++++++--------------- src/guesstype.jl | 14 +-- src/lib/date-tryparse-internal.jl | 36 ++++---- src/record.jl | 30 +++--- src/utf8optimizations.jl | 140 ++++++++++++++-------------- src/util.jl | 97 ++++++++++---------- test/test_vectorbackedstrings.jl | 56 ++++++------ 10 files changed, 274 insertions(+), 275 deletions(-) diff --git a/benchmark/benchmarks.jl b/benchmark/benchmarks.jl index 0839a4c..80d9ebf 100644 --- a/benchmark/benchmarks.jl +++ b/benchmark/benchmarks.jl @@ -65,14 +65,14 @@ datetimestr = "1970-02-02 02:20:20" datetimestrlen = our_lastindex(datetimestr) SUITE["util"]["tryparsenext"] = BenchmarkGroup() -SUITE["util"]["tryparsenext"]["NumericFloat64"] = @benchmarkable TextParse.tryparsenext($(TextParse.Numeric(Float64)), $float64str,1,$float64strlen) -SUITE["util"]["tryparsenext"]["LongNumericFloat64"] = @benchmarkable TextParse.tryparsenext($(TextParse.Numeric(Float64)), $longfloat64str,1,$longfloat64strlen) -SUITE["util"]["tryparsenext"]["UInt64"] = @benchmarkable TextParse.tryparsenext($(TextParse.Numeric(UInt64)), $intstr,1,$intstrlen) -SUITE["util"]["tryparsenext"]["NegInt64"] = @benchmarkable TextParse.tryparsenext($(TextParse.Numeric(Int64)), $negintstr,1,$negintstrlen) -SUITE["util"]["tryparsenext"]["Percentage"] = @benchmarkable TextParse.tryparsenext($(TextParse.Percentage()), $percentagestring,1,$percentagestringlen, TextParse.default_opts) -SUITE["util"]["tryparsenext"]["StringToken"] = @benchmarkable TextParse.tryparsenext($(TextParse.StringToken(String)), $somestring,1,$somestringlen, TextParse.default_opts) -SUITE["util"]["tryparsenext"]["DateTimeToken"] = @benchmarkable TextParse.tryparsenext($tok, $datetimestr,1,$datetimestrlen, $opts) -SUITE["util"]["tryparsenext"]["QuotedStringToken"] = @benchmarkable TextParse.tryparsenext($(Quoted(String,quotechar='"', escapechar='"')), $somequotedstring) +SUITE["util"]["tryparsenext"]["NumericFloat64"] = @benchmarkable TextParse.tryparsenext($(TextParse.Numeric(Float64)), $float64str, 1, $float64strlen) +SUITE["util"]["tryparsenext"]["LongNumericFloat64"] = @benchmarkable TextParse.tryparsenext($(TextParse.Numeric(Float64)), $longfloat64str, 1, $longfloat64strlen) +SUITE["util"]["tryparsenext"]["UInt64"] = @benchmarkable TextParse.tryparsenext($(TextParse.Numeric(UInt64)), $intstr, 1, $intstrlen) +SUITE["util"]["tryparsenext"]["NegInt64"] = @benchmarkable TextParse.tryparsenext($(TextParse.Numeric(Int64)), $negintstr, 1, $negintstrlen) +SUITE["util"]["tryparsenext"]["Percentage"] = @benchmarkable TextParse.tryparsenext($(TextParse.Percentage()), $percentagestring, 1, $percentagestringlen, TextParse.default_opts) +SUITE["util"]["tryparsenext"]["StringToken"] = @benchmarkable TextParse.tryparsenext($(TextParse.StringToken(String)), $somestring, 1, $somestringlen, TextParse.default_opts) +SUITE["util"]["tryparsenext"]["DateTimeToken"] = @benchmarkable TextParse.tryparsenext($tok, $datetimestr, 1, $datetimestrlen, $opts) +SUITE["util"]["tryparsenext"]["QuotedStringToken"] = @benchmarkable TextParse.tryparsenext($(Quoted(String, quotechar='"', escapechar='"')), $somequotedstring) somefieldstring = " 12,3" f = TextParse.fromtype(Int) diff --git a/docs/make.jl b/docs/make.jl index dcaf5b4..ac7d0fb 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -1,12 +1,12 @@ using Documenter, TextParse makedocs( - modules = [TextParse], - sitename = "TextParse.jl", - authors = "Shashi Gowda", - pages = Any["Home" => "index.md"], + modules=[TextParse], + sitename="TextParse.jl", + authors="Shashi Gowda", + pages=Any["Home" => "index.md"], ) deploydocs( - repo = "github.com/JuliaComputing/TextParse.jl.git" + repo="github.com/JuliaComputing/TextParse.jl.git" ) diff --git a/src/VectorBackedStrings.jl b/src/VectorBackedStrings.jl index 3b26e39..b53dce7 100644 --- a/src/VectorBackedStrings.jl +++ b/src/VectorBackedStrings.jl @@ -36,7 +36,7 @@ Base.@propagate_inbounds function Base.iterate(s::VectorBackedUTF8String, i::Int i > ncodeunits(s) && return nothing b = codeunit(s, i) u = UInt32(b) << 24 - Base.between(b, 0x80, 0xf7) || return reinterpret(Char, u), i+1 + Base.between(b, 0x80, 0xf7) || return reinterpret(Char, u), i + 1 return our_next_continued(s, i, u) end @@ -58,7 +58,7 @@ function our_next_continued(s::VectorBackedUTF8String, i::Int, u::UInt32) @inbounds b = codeunit(s, i) b & 0xc0 == 0x80 || @goto ret u |= UInt32(b); i += 1 -@label ret + @label ret return reinterpret(Char, u), i end diff --git a/src/field.jl b/src/field.jl index 1ec8a20..aafeb3b 100644 --- a/src/field.jl +++ b/src/field.jl @@ -5,7 +5,7 @@ export CustomParser, Quoted abstract type AbstractToken{T} end fieldtype(::AbstractToken{T}) where {T} = T fieldtype(::Type{AbstractToken{T}}) where {T} = T -fieldtype(::Type{T}) where {T<:AbstractToken} = fieldtype(supertype(T)) +fieldtype(::Type{T}) where {T <: AbstractToken} = fieldtype(supertype(T)) """ `tryparsenext{T}(tok::AbstractToken{T}, str, i, till, localopts)` @@ -29,7 +29,7 @@ Options local to the token currently being parsed. - `includequotes`: whether to include quotes while parsing - `includenewlines`: whether to include newlines while parsing """ -struct LocalOpts{T_ENDCHAR<:Union{Char,UInt8}, T_QUOTECHAR<:Union{Char,UInt8}, T_ESCAPECHAR<:Union{Char,UInt8}} +struct LocalOpts{T_ENDCHAR <: Union{Char,UInt8},T_QUOTECHAR <: Union{Char,UInt8},T_ESCAPECHAR <: Union{Char,UInt8}} endchar::T_ENDCHAR # End parsing at this char spacedelim::Bool quotechar::T_QUOTECHAR # Quote char @@ -49,12 +49,12 @@ end tryparsenext(tok, str, i, len) end -struct WrapLocalOpts{T, X<:AbstractToken} <: AbstractToken{T} +struct WrapLocalOpts{T,X <: AbstractToken} <: AbstractToken{T} opts::LocalOpts inner::X end -WrapLocalOpts(opts, inner) = WrapLocalOpts{fieldtype(inner), typeof(inner)}(opts, inner) +WrapLocalOpts(opts, inner) = WrapLocalOpts{fieldtype(inner),typeof(inner)}(opts, inner) @inline function tryparsenext(tok::WrapLocalOpts, str, i, len, opts::LocalOpts=default_opts) tryparsenext(tok.inner, str, i, len, tok.opts) @@ -69,7 +69,7 @@ function tryparsenext(::Unknown, str, i, len, opts) nullableNA, i end show(io::IO, ::Unknown) = print(io, "") -struct CustomParser{T, F} <: AbstractToken{T} +struct CustomParser{T,F} <: AbstractToken{T} f::Function end @@ -114,33 +114,33 @@ end show(io::IO, c::Numeric{T}) where {T} = print(io, "<$T>") Numeric(::Type{T}, decimal='.', thousands=',') where {T} = Numeric{T}(decimal, thousands) -fromtype(::Type{N}) where {N<:Number} = Numeric(N) +fromtype(::Type{N}) where {N <: Number} = Numeric(N) ### Unsigned integers -function tryparsenext(::Numeric{T}, str, i, len) where {T<:Signed} +function tryparsenext(::Numeric{T}, str, i, len) where {T <: Signed} R = Nullable{T} @chk2 sign, i = tryparsenext_sign(str, i, len) @chk2 x, i = tryparsenext_base10(T, str, i, len) @label done - return R(convert(T, sign*x)), i + return R(convert(T, sign * x)), i @label error return R(), i end -@inline function tryparsenext(::Numeric{T}, str, i, len) where {T<:Unsigned} - tryparsenext_base10(T,str, i, len) +@inline function tryparsenext(::Numeric{T}, str, i, len) where {T <: Unsigned} + tryparsenext_base10(T, str, i, len) end -@inline _is_e(str, i) = str[i]=='e' || str[i]=='E' +@inline _is_e(str, i) = str[i] == 'e' || str[i] == 'E' -@inline _is_negative(str, i) = str[i]=='-' +@inline _is_negative(str, i) = str[i] == '-' -@inline _is_positive(str, i) = str[i]=='+' +@inline _is_positive(str, i) = str[i] == '+' -const pre_comp_exp_double = Double64[Double64(10.0)^i for i=0:308] +const pre_comp_exp_double = Double64[Double64(10.0)^i for i = 0:308] @inline function convert_to_double(f1::Int64, exp::Int) f = Float64(f1) @@ -151,30 +151,30 @@ const pre_comp_exp_double = Double64[Double64(10.0)^i for i=0:308] minexp = -256 if exp >= 0 - x *= pre_comp_exp_double[exp+1] + x *= pre_comp_exp_double[exp + 1] else if exp < minexp # not sure why this is a good choice, but it seems to be! - x /= pre_comp_exp_double[-minexp+1] + x /= pre_comp_exp_double[-minexp + 1] x /= pre_comp_exp_double[-exp + minexp + 1] else - x /= pre_comp_exp_double[-exp+1] + x /= pre_comp_exp_double[-exp + 1] end end return Float64(x) end -@inline function tryparsenext(::Numeric{F}, str, i, len) where {F<:AbstractFloat} +@inline function tryparsenext(::Numeric{F}, str, i, len) where {F <: AbstractFloat} R = Nullable{F} y1 = iterate(str, i) - y1===nothing && @goto error + y1 === nothing && @goto error negate = false c = y1[1] - if c=='-' + if c == '-' negate = true i = y1[2] - elseif c=='+' + elseif c == '+' i = y1[2] end @@ -190,8 +190,8 @@ end # next thing must be dec pt. y2 = iterate(str, i) - if y2!==nothing && y2[1]=='.' - i =y2[2] + if y2 !== nothing && y2[1] == '.' + i = y2[2] f1, rval2, ie = parse_uint_and_stop(str, i, len, f1) # TODO This is incorrect for string types where a digit takes up # more than one codeunit, we need to return the number of digits @@ -209,11 +209,11 @@ end eval::Int32 = 0 y3 = iterate(str, i) - if y3!==nothing && _is_e(str, i) + if y3 !== nothing && _is_e(str, i) i = y3[2] y4 = iterate(str, i) - if y4!==nothing + if y4 !== nothing enegate = false if _is_negative(str, i) enegate = true @@ -235,9 +235,9 @@ end if frac_digits <= 15 && -22 <= exp <= 22 if exp >= 0 - f = F(f1)*10.0^exp + f = F(f1) * 10.0^exp else - f = F(f1)/10.0^(-exp) + f = F(f1) / 10.0^(-exp) end else f = convert_to_double(f1, exp) @@ -266,7 +266,7 @@ function tryparsenext(::Percentage, str, i, len, opts) # parse away the % char ii = eatwhitespaces(str, ii, len) y = iterate(str, ii) - if y===nothing + if y === nothing return Nullable{Float64}(), ii # failed to parse % else c = y[1]; k = y[2] @@ -276,7 +276,7 @@ function tryparsenext(::Percentage, str, i, len, opts) return Nullable{Float64}(num.value / 100.0), k # the point after % end end - end +end end """ @@ -292,7 +292,7 @@ function StringToken(t::Type{T}) where T end show(io::IO, c::StringToken) = print(io, "") -fromtype(::Type{S}) where {S<:AbstractString} = StringToken(S) +fromtype(::Type{S}) where {S <: AbstractString} = StringToken(S) function tryparsenext(s::StringToken{T}, str, i, len, opts) where {T} inside_quoted_strong = Char(opts.endchar) == Char(opts.quotechar) @@ -302,7 +302,7 @@ function tryparsenext(s::StringToken{T}, str, i, len, opts) where {T} i0 = i if opts.includequotes y = iterate(str, i) - if y!==nothing + if y !== nothing c = y[1]; ii = y[2] if c == Char(opts.quotechar) i = ii # advance counter so that @@ -312,15 +312,15 @@ function tryparsenext(s::StringToken{T}, str, i, len, opts) where {T} end y2 = iterate(str, i) - while y2!==nothing + while y2 !== nothing c = y2[1]; ii = y2[2] - if inside_quoted_strong && p==Char(opts.escapechar) + if inside_quoted_strong && p == Char(opts.escapechar) escapecount += 1 end if opts.spacedelim && (c == ' ' || c == '\t') - break + break elseif !opts.spacedelim && c == Char(opts.endchar) if inside_quoted_strong # this means we're inside a quoted string @@ -328,9 +328,9 @@ function tryparsenext(s::StringToken{T}, str, i, len, opts) where {T} # sometimes the quotechar is the escapechar # in that case we need to see the next char y3 = iterate(str, ii) - if y3===nothing + if y3 === nothing if opts.includequotes - i=ii + i = ii end break else @@ -357,7 +357,7 @@ function tryparsenext(s::StringToken{T}, str, i, len, opts) where {T} end break elseif (!opts.includenewlines && isnewline(c)) - break + break end i = ii p = c @@ -365,15 +365,15 @@ function tryparsenext(s::StringToken{T}, str, i, len, opts) where {T} y2 = iterate(str, i) end - return R(_substring(T, str, i0, i-1, escapecount, opts)), i + return R(_substring(T, str, i0, i - 1, escapecount, opts)), i end @inline function _substring(::Type{String}, str, i, j, escapecount, opts) if escapecount > 0 - buf = IOBuffer(sizehint=j-i+1-escapecount) + buf = IOBuffer(sizehint=j - i + 1 - escapecount) cur_i = i c = str[cur_i] - if opts.includequotes && c==Char(opts.quotechar) + if opts.includequotes && c == Char(opts.quotechar) print(buf, c) cur_i = nextind(str, cur_i) end @@ -394,11 +394,11 @@ end end return String(take!(buf)) else - return unsafe_string(pointer(str, i), j-i+1) + return unsafe_string(pointer(str, i), j - i + 1) end end -@inline function _substring(::Type{T}, str, i, j, escapecount, opts) where {T<:SubString} +@inline function _substring(::Type{T}, str, i, j, escapecount, opts) where {T <: SubString} escapecount > 0 && error("Not yet handled.") T(str, i, thisind(j)) end @@ -420,7 +420,7 @@ end export Quoted -struct Quoted{T, S<:AbstractToken, T_QUOTECHAR<:Union{Char,UInt8}, T_ESCAPECHAR<:Union{Char,UInt8}} <: AbstractToken{T} +struct Quoted{T,S <: AbstractToken,T_QUOTECHAR <: Union{Char,UInt8},T_ESCAPECHAR <: Union{Char,UInt8}} <: AbstractToken{T} inner::S required::Bool stripwhitespaces::Bool @@ -451,9 +451,9 @@ end function Quoted(inner::S, quotechar::T_QUOTECHAR, escapechar::T_ESCAPECHAR; required=false, - stripwhitespaces=fieldtype(S)<:Number, + stripwhitespaces=fieldtype(S) <: Number, includequotes=false, - includenewlines=true) where {S<:AbstractToken,T_QUOTECHAR,T_ESCAPECHAR} + includenewlines=true) where {S <: AbstractToken,T_QUOTECHAR,T_ESCAPECHAR} T = fieldtype(S) Quoted{T,S,T_QUOTECHAR,T_ESCAPECHAR}(inner, required, stripwhitespaces, includequotes, @@ -464,7 +464,7 @@ Quoted(t::Type, quotechar, escapechar; kwargs...) = Quoted(fromtype(t), quotecha function tryparsenext(q::Quoted{T,S,T_QUOTECHAR,T_ESCAPECHAR}, str, i, len, opts) where {T,S,T_QUOTECHAR,T_ESCAPECHAR} y1 = iterate(str, i) - if y1===nothing + if y1 === nothing q.required && @goto error # check to see if inner thing is ok with an empty field @chk2 x, i = tryparsenext(q.inner, str, i, len, opts) error @@ -504,7 +504,7 @@ function tryparsenext(q::Quoted{T,S,T_QUOTECHAR,T_ESCAPECHAR}, str, i, len, opts i = eatwhitespaces(str, i, len) end y2 = iterate(str, i) - y2===nothing && error("Internal error.") + y2 === nothing && error("Internal error.") c = y2[1]; ii = y2[2] if quotestarted && !q.includequotes @@ -521,7 +521,7 @@ function tryparsenext(q::Quoted{T,S,T_QUOTECHAR,T_ESCAPECHAR}, str, i, len, opts end ## Date and Time -struct DateTimeToken{T,S<:DateFormat} <: AbstractToken{T} +struct DateTimeToken{T,S <: DateFormat} <: AbstractToken{T} format::S end @@ -531,8 +531,8 @@ end Parse a date time string of format `fmt` into type `T` which is either `Date`, `Time` or `DateTime`. """ -DateTimeToken(T::Type, df::S) where {S<:DateFormat} = DateTimeToken{T, S}(df) -DateTimeToken(df::S) where {S<:DateFormat} = DateTimeToken{DateTime, S}(df) +DateTimeToken(T::Type, df::S) where {S <: DateFormat} = DateTimeToken{T,S}(df) +DateTimeToken(df::S) where {S <: DateFormat} = DateTimeToken{DateTime,S}(df) fromtype(df::DateFormat) = DateTimeToken(DateTime, df) fromtype(::Type{DateTime}) = DateTimeToken(DateTime, ISODateTimeFormat) fromtype(::Type{Date}) = DateTimeToken(Date, ISODateFormat) @@ -555,7 +555,7 @@ const nastrings_upcase = ["NA", "NULL", "N/A","#N/A", "#N/A N/A", "#NA", const NA_STRINGS = sort!(vcat(nastrings_upcase, map(lowercase, nastrings_upcase))) -struct NAToken{T, S<:AbstractToken} <: AbstractToken{T} +struct NAToken{T,S <: AbstractToken} <: AbstractToken{T} inner::S emptyisna::Bool nastrings::Vector{String} @@ -577,7 +577,7 @@ function NAToken( , nastrings=NA_STRINGS) where S T = fieldtype(inner) - NAToken{UnionMissing{T}, S}(inner, emptyisna, nastrings) + NAToken{UnionMissing{T},S}(inner, emptyisna, nastrings) end function show(io::IO, na::NAToken) @@ -588,8 +588,8 @@ end function tryparsenext(na::NAToken{T}, str, i, len, opts) where {T} R = Nullable{T} i = eatwhitespaces(str, i, len) - y1 = iterate(str,i) - if y1===nothing + y1 = iterate(str, i) + if y1 === nothing if na.emptyisna @goto null else @@ -597,7 +597,7 @@ function tryparsenext(na::NAToken{T}, str, i, len, opts) where {T} end end - c = y1[1]; ii=y1[2] + c = y1[1]; ii = y1[2] if (c == Char(opts.endchar) || isnewline(c)) && na.emptyisna @goto null end @@ -605,7 +605,7 @@ function tryparsenext(na::NAToken{T}, str, i, len, opts) where {T} if isa(na.inner, Unknown) @goto maybe_null end - @chk2 x,ii = tryparsenext(na.inner, str, i, len, opts) maybe_null + @chk2 x, ii = tryparsenext(na.inner, str, i, len, opts) maybe_null @label done return R(convert(T, x)), ii @@ -615,7 +615,7 @@ function tryparsenext(na::NAToken{T}, str, i, len, opts) where {T} opts.escapechar, false, opts.includenewlines) @chk2 nastr, ii = tryparsenext(StringToken(WeakRefString{UInt8}), str, i, len, naopts) if !isempty(searchsorted(na.nastrings, nastr)) - i=ii + i = ii i = eatwhitespaces(str, i, len) @goto null end @@ -625,13 +625,13 @@ function tryparsenext(na::NAToken{T}, str, i, len, opts) where {T} return R(missing), i @label error - return R(), i +return R(), i end fromtype(::Type{Union{Missing,T}}) where T = NAToken(fromtype(T)) struct SkipToken{S} <: AbstractToken{Nothing} - inner::S +inner::S end function tryparsenext(f::SkipToken, str, i, len, opts) @@ -648,7 +648,7 @@ end abstract type AbstractField{T} <: AbstractToken{T} end # A rocord is a collection of abstract fields -struct Field{T,S<:AbstractToken} <: AbstractField{T} +struct Field{T,S <: AbstractToken} <: AbstractField{T} inner::S ignore_init_whitespace::Bool ignore_end_whitespace::Bool @@ -669,7 +669,7 @@ function Field(f::Field; inner=f.inner, ignore_init_whitespace=f.ignore_init_whi end function swapinner(f::Field, inner::AbstractToken; - ignore_init_whitespace= f.ignore_end_whitespace + ignore_init_whitespace=f.ignore_end_whitespace , ignore_end_whitespace=f.ignore_end_whitespace , eoldelim=f.eoldelim ) @@ -686,7 +686,7 @@ function tryparsenext(f::Field{T}, str, i, len, opts) where {T} i > len && @goto error if f.ignore_init_whitespace y1 = iterate(str, i) - while y1!==nothing + while y1 !== nothing c = y1[1]; ii = y1[2] !isspace(c) && break i = ii @@ -698,9 +698,9 @@ function tryparsenext(f::Field{T}, str, i, len, opts) where {T} if f.ignore_end_whitespace i0 = i y2 = iterate(str, i) - while y2!==nothing + while y2 !== nothing c = y2[1]; ii = y2[2] - !opts.spacedelim && Char(opts.endchar) == '\t' && c == '\t' && (i =ii; @goto done) + !opts.spacedelim && Char(opts.endchar) == '\t' && c == '\t' && (i = ii; @goto done) !isspace(c) && c != '\t' && break i = ii y2 = iterate(str, i) @@ -719,29 +719,29 @@ function tryparsenext(f::Field{T}, str, i, len, opts) where {T} end y3 = iterate(str, i) - y3===nothing && error("Internal error.") + y3 === nothing && error("Internal error.") c = y3[1]; ii = y3[2] - opts.spacedelim && (isspace(c) || c == '\t') && (i=ii; @goto done) - !opts.spacedelim && Char(opts.endchar) == c && (i=ii; @goto done) + opts.spacedelim && (isspace(c) || c == '\t') && (i = ii; @goto done) + !opts.spacedelim && Char(opts.endchar) == c && (i = ii; @goto done) if f.eoldelim if c == '\r' - i=ii + i = ii y4 = iterate(str, i) - if y4!==nothing + if y4 !== nothing c = y4[1]; ii = y4[2] if c == '\n' - i=ii + i = ii end end @goto done elseif c == '\n' - i=ii + i = ii y5 = iterate(str, i) - if y5!==nothing + if y5 !== nothing c = y5[1]; ii = y5[2] if c == '\r' - i=ii + i = ii end end @goto done diff --git a/src/guesstype.jl b/src/guesstype.jl index f1d1beb..b75faf8 100644 --- a/src/guesstype.jl +++ b/src/guesstype.jl @@ -52,7 +52,7 @@ function getquotechar(x) return '\0' end -function guesstoken(x, opts, prevent_quote_wrap, @nospecialize(prev_guess=Unknown()), nastrings=NA_STRINGS, stringarraytype=StringArray) +function guesstoken(x, opts, prevent_quote_wrap, @nospecialize(prev_guess = Unknown()), nastrings=NA_STRINGS, stringarraytype=StringArray) q = getquotechar(x) if isa(prev_guess, StringToken) @@ -66,9 +66,9 @@ function guesstoken(x, opts, prevent_quote_wrap, @nospecialize(prev_guess=Unknow prev_inner = prev_guess end inner_string = strip(strip(x, q)) - if inner_string=="" + if inner_string == "" # If we come across a "", we classify it as a string column no matter what - return Quoted(StringToken(stringarraytype<:StringArray ? StrRange : String), opts.quotechar, opts.escapechar) + return Quoted(StringToken(stringarraytype <: StringArray ? StrRange : String), opts.quotechar, opts.escapechar) else inner_token = guesstoken(inner_string, opts, true, prev_inner, nastrings, stringarraytype) return Quoted(inner_token, opts.quotechar, opts.escapechar) @@ -99,7 +99,7 @@ function guesstoken(x, opts, prevent_quote_wrap, @nospecialize(prev_guess=Unknow # prev_guess is not a NAToken or a StringToken ispercent = strip(x)[end] == '%' if ispercent - x = x[1:end-1] + x = x[1:end - 1] end if tryparse(Int, x) !== nothing || tryparse(Float64, x) !== nothing T = tryparse(Int, x) === nothing ? Float64 : Int @@ -114,19 +114,19 @@ function guesstoken(x, opts, prevent_quote_wrap, @nospecialize(prev_guess=Unknow return Numeric(promote_type(T, fieldtype(prev_guess))) else # something like a date turned into a single number? - y1 = StringToken(stringarraytype<:StringArray ? StrRange : String) + y1 = StringToken(stringarraytype <: StringArray ? StrRange : String) return prevent_quote_wrap ? y1 : Quoted(y1, opts.quotechar, opts.escapechar) end else # fast-path if length(filter(isnumeric, x)) < 4 - y2 = StringToken(stringarraytype<:StringArray ? StrRange : String) + y2 = StringToken(stringarraytype <: StringArray ? StrRange : String) return prevent_quote_wrap ? y2 : Quoted(y2, opts.quotechar, opts.escapechar) end maybedate = guessdateformat(x) if maybedate === nothing - y3 = StringToken(stringarraytype<:StringArray ? StrRange : String) + y3 = StringToken(stringarraytype <: StringArray ? StrRange : String) return prevent_quote_wrap ? y3 : Quoted(y3, opts.quotechar, opts.escapechar) else return maybedate diff --git a/src/lib/date-tryparse-internal.jl b/src/lib/date-tryparse-internal.jl index 0831293..e3a9ea9 100644 --- a/src/lib/date-tryparse-internal.jl +++ b/src/lib/date-tryparse-internal.jl @@ -16,7 +16,7 @@ Returns a 2-element tuple `(values, pos)`: """ @generated function tryparsenext_internal( ::Type{T}, str::AbstractString, pos::Int, len::Int, df::DateFormat, endchar=UInt('\0'), raise::Bool=false, -) where {T<:TimeType} +) where {T <: TimeType} letters = character_codes(df) tokens = Type[CONVERSION_SPECIFIERS[letter] for letter in letters] @@ -32,8 +32,8 @@ Returns a 2-element tuple `(values, pos)`: # of the required variables. assign_defaults = Expr[ quote - $name = $default - end + $name = $default + end for (name, default) in zip(output_names, output_defaults) ] @@ -43,7 +43,7 @@ Returns a 2-element tuple `(values, pos)`: assign_value_till = Expr[ quote ($i <= num_parsed) && ($name = unsafe_val[$i]) - end for (i,name) in enumerate(value_names)] + end for (i, name) in enumerate(value_names)] quote values, pos, num_parsed = tryparsenext_core(str, pos, len, df, raise) @@ -94,38 +94,38 @@ Returns a 3-element tuple `(values, pos, num_parsed)`: # unassigned variables. assign_defaults = Expr[ quote - $name = $default - end + $name = $default + end for (name, default) in zip(value_names, value_defaults) ] vi = 1 parsers = Expr[ if directives[i] <: DatePart - name = value_names[vi] - nullable = Symbol(:nullable_, name) - vi += 1 - quote - pos > len && @goto done + name = value_names[vi] + nullable = Symbol(:nullable_, name) + vi += 1 + quote + pos > len && @goto done nothingable_tuple = tryparsenext(directives[$i], str, pos, len, locale) - nothingable_tuple===nothing && @goto error + nothingable_tuple === nothing && @goto error $name = nothingable_tuple[1] next_pos = nothingable_tuple[2] pos = next_pos num_parsed += 1 directive_index += 1 - end - else - quote - pos > len && @goto done + end + else + quote + pos > len && @goto done nothingable_tuple = tryparsenext(directives[$i], str, pos, len, locale) - nothingable_tuple===nothing && @goto error + nothingable_tuple === nothing && @goto error nullable_delim = nothingable_tuple[1] next_pos = nothingable_tuple[2] pos = next_pos directive_index += 1 - end end + end for i in 1:length(directives) ] diff --git a/src/record.jl b/src/record.jl index 05939e4..8c1b01f 100644 --- a/src/record.jl +++ b/src/record.jl @@ -1,22 +1,22 @@ -struct Record{Tf<:Tuple, To} +struct Record{Tf <: Tuple,To} fields::Tf end -function Record(t::T) where T<:Tuple +function Record(t::T) where T <: Tuple To = Tuple{map(fieldtype, t)...} - #Tov = Tuple{map(s->Vector{s},map(fieldtype, t))...} - Record{T, To}(t) + # Tov = Tuple{map(s->Vector{s},map(fieldtype, t))...} + Record{T,To}(t) end # for dispatch on N include_string(TextParse, "const RecN{N,U} = Record{T,U} where T<:NTuple{N, Any}") -@generated function tryparsenext(r::RecN{N, To}, str, i, len, opts=default_opts) where {N, To} +@generated function tryparsenext(r::RecN{N,To}, str, i, len, opts=default_opts) where {N,To} quote R = Nullable{To} i > len && @goto error - Base.@nexprs $N j->begin + Base.@nexprs $N j -> begin @chk2 (val_j, i) = tryparsenext(r.fields[j], str, i, len, opts) end @@ -54,7 +54,7 @@ end ft = fldtypes[j] ct = coltypes[j] rl = 1 - for k = j+1:N + for k = j + 1:N if fldtypes[k] == ft && coltypes[k] == ct rl += 1 else @@ -65,15 +65,15 @@ end body = gen_1parsesetindex(:jj, :(r.fields[jj]::($ft)), :(columns[jj]::($ct))) push!(fieldparsers, quote - for jj = $j:$(j+rl-1); $body; end - end) + for jj = $j:$(j + rl - 1); $body; end + end) j += rl else push!(fieldparsers, gen_1parsesetindex(j, :(r.fields[$j]), :(columns[$j]))) j += 1 end end - R = Result{Int, Tuple{Int,Int,Int,UInt8}} + R = Result{Int,Tuple{Int,Int,Int,UInt8}} quote err_field = 1 ii = i @@ -107,7 +107,7 @@ end # Weird hybrid of records and fields -struct UseOne{T,R<:Record,use} <: AbstractToken{T} +struct UseOne{T,R <: Record,use} <: AbstractToken{T} record::R end @@ -115,7 +115,7 @@ fieldtype(::UseOne{T}) where {T} = T function UseOne(fields::Tuple, use) r = Record(fields) - UseOne{fieldtype(fields[use]), typeof(r), use}(r) + UseOne{fieldtype(fields[use]),typeof(r),use}(r) end getthing(x, ::Type{Val{n}}) where {n} = x[n] function tryparsenext(f::UseOne{T,S,use}, str, i, len, opts=default_opts) where {T,S,use} @@ -130,11 +130,11 @@ function tryparsenext(f::UseOne{T,S,use}, str, i, len, opts=default_opts) where end -struct Repeated{F, T, N} +struct Repeated{F,T,N} field::F end -Repeated(f::F, n) where {F} = Repeated{F, fieldtype(f), n}(f) +Repeated(f::F, n) where {F} = Repeated{F,fieldtype(f),n}(f) fieldtype(::Repeated{F,T,N}) where {F,T,N} = NTuple{N,T} @@ -144,7 +144,7 @@ fieldtype(::Repeated{F,T,N}) where {F,T,N} = NTuple{N,T} i > len && @goto error # pefect candidate for #11902 - Base.@nexprs $N j->begin + Base.@nexprs $N j -> begin @chk2 (val_j, i) = tryparsenext(f.field, str, i, len, opts) end diff --git a/src/utf8optimizations.jl b/src/utf8optimizations.jl index 59dda15..bbdb1b6 100644 --- a/src/utf8optimizations.jl +++ b/src/utf8optimizations.jl @@ -1,8 +1,8 @@ -@inline function eatwhitespaces(str::Union{VectorBackedUTF8String, String}, i=1, len=lastindex(str)) - while i<=len +@inline function eatwhitespaces(str::Union{VectorBackedUTF8String,String}, i=1, len=lastindex(str)) + while i <= len @inbounds b = codeunit(str, i) - if b==0x20 # This is ' ' + if b == 0x20 # This is ' ' i += 1 else break @@ -11,13 +11,13 @@ return i end -@inline function eatnewlines(str::Union{VectorBackedUTF8String, String}, i=1, len=lastindex(str)) +@inline function eatnewlines(str::Union{VectorBackedUTF8String,String}, i=1, len=lastindex(str)) count = 0 - while i<=len + while i <= len @inbounds b = codeunit(str, i) if b == 0xd # '\r' i += 1 - if i<=len + if i <= len @inbounds b = codeunit(str, i) if b == 0xa # '\n' i += 1 @@ -26,7 +26,7 @@ end count += 1 elseif b == 0xa i += 1 - if i<=len + if i <= len @inbounds b = codeunit(str, i) if b == 0xd i += 1 @@ -41,26 +41,26 @@ end return i, count end -@inline function tryparsenext_sign(str::Union{VectorBackedUTF8String, String}, i, len) +@inline function tryparsenext_sign(str::Union{VectorBackedUTF8String,String}, i, len) i > len && return Nullable{Int}(), i @inbounds b = codeunit(str, i) - if b==0x2d - return Nullable{Int}(-1), i+1 - elseif b==0x2b - return Nullable{Int}(1), i+1 + if b == 0x2d + return Nullable{Int}(-1), i + 1 + elseif b == 0x2b + return Nullable{Int}(1), i + 1 else return Nullable{Int}(1), i end end -@inline function tryparsenext_base10_digit(T,str::Union{VectorBackedUTF8String, String},i, len) +@inline function tryparsenext_base10_digit(T, str::Union{VectorBackedUTF8String,String}, i, len) i > len && @goto error - @inbounds b = codeunit(str,i) - diff = b-0x30 + @inbounds b = codeunit(str, i) + diff = b - 0x30 diff >= UInt8(10) && @goto error - return convert(T, diff), i+1 + return convert(T, diff), i + 1 @label error return nothing @@ -68,13 +68,13 @@ end @inline _isdigit(b::UInt8) = ( (0x30 ≤ b) & (b ≤ 0x39) ) -@inline function parse_uint_and_stop(str::Union{VectorBackedUTF8String, String}, i, len, n::T) where {T <: Integer} +@inline function parse_uint_and_stop(str::Union{VectorBackedUTF8String,String}, i, len, n::T) where {T <: Integer} ten = T(10) # specialize handling of the first digit so we can return an error - max_without_overflow = div(typemax(T)-9,10) # the larg + max_without_overflow = div(typemax(T) - 9, 10) # the larg i <= len || return n, false, i @inbounds b = codeunit(str, i) - diff = b-0x30 + diff = b - 0x30 if diff < UInt8(10) && n <= max_without_overflow n *= ten n += T(diff) @@ -85,7 +85,7 @@ end while i <= len && n <= max_without_overflow @inbounds b = codeunit(str, i) - diff = b-0x30 + diff = b - 0x30 if diff < UInt8(10) n *= ten n += T(diff) @@ -97,7 +97,7 @@ end return n, true, i end -@inline function read_digits(str::Union{VectorBackedUTF8String, String}, i, len) +@inline function read_digits(str::Union{VectorBackedUTF8String,String}, i, len) # slurp up extra digits while i <= len @inbounds b = codeunit(str, i) @@ -109,35 +109,35 @@ end return i end -@inline function _is_e(str::Union{VectorBackedUTF8String, String}, i) +@inline function _is_e(str::Union{VectorBackedUTF8String,String}, i) @inbounds b = codeunit(str, i) - return (b==0x65) | (b==0x45) + return (b == 0x65) | (b == 0x45) end -@inline function _is_negative(str::Union{VectorBackedUTF8String, String}, i) +@inline function _is_negative(str::Union{VectorBackedUTF8String,String}, i) @inbounds b = codeunit(str, i) - return b==0x2d + return b == 0x2d end -@inline function _is_positive(str::Union{VectorBackedUTF8String, String}, i) +@inline function _is_positive(str::Union{VectorBackedUTF8String,String}, i) @inbounds b = codeunit(str, i) - return b==0x2b + return b == 0x2b end -const pre_comp_exp = Float64[10.0^i for i=0:22] +const pre_comp_exp = Float64[10.0^i for i = 0:22] -@inline function tryparsenext(::Numeric{F}, str::Union{VectorBackedUTF8String, String}, i, len) where {F<:AbstractFloat} +@inline function tryparsenext(::Numeric{F}, str::Union{VectorBackedUTF8String,String}, i, len) where {F <: AbstractFloat} R = Nullable{F} - i>len && @goto error + i > len && @goto error negate = false @inbounds b = codeunit(str, i) - if b==0x2d # '-' + if b == 0x2d # '-' negate = true i += 1 - elseif b==0x2b # '+' - i +=1 + elseif b == 0x2b # '+' + i += 1 end f1::Int64 = 0 @@ -169,7 +169,7 @@ const pre_comp_exp = Float64[10.0^i for i=0:22] i += 1 enegate = false - if i<=len + if i <= len if _is_negative(str, i) enegate = true i += 1 @@ -190,12 +190,12 @@ const pre_comp_exp = Float64[10.0^i for i=0:22] if frac_digits <= 15 && -22 <= exp <= 22 if exp >= 0 - f = F(f1)*pre_comp_exp[exp+1] + f = F(f1) * pre_comp_exp[exp + 1] else - f = F(f1)/pre_comp_exp[-exp+1] + f = F(f1) / pre_comp_exp[-exp + 1] end else - f = convert_to_double(f1, exp) + f = convert_to_double(f1, exp) end if negate @@ -209,7 +209,7 @@ const pre_comp_exp = Float64[10.0^i for i=0:22] return R(), i end -function tryparsenext(f::Field{T}, str::Union{VectorBackedUTF8String, String}, i, len, opts::LocalOpts{T_ENDCHAR}) where {T, T_ENDCHAR<:UInt8} +function tryparsenext(f::Field{T}, str::Union{VectorBackedUTF8String,String}, i, len, opts::LocalOpts{T_ENDCHAR}) where {T,T_ENDCHAR <: UInt8} R = Nullable{T} i > len && @goto error if f.ignore_init_whitespace @@ -220,13 +220,13 @@ function tryparsenext(f::Field{T}, str::Union{VectorBackedUTF8String, String}, i if f.ignore_end_whitespace i0 = i - while i<=len + while i <= len @inbounds b = codeunit(str, i) - !opts.spacedelim && opts.endchar == 0x09 && b == 0x09 && (i = i+1; @goto done) # 0x09 is \t + !opts.spacedelim && opts.endchar == 0x09 && b == 0x09 && (i = i + 1; @goto done) # 0x09 is \t - b!=0x20 && b!=0x09 && break - i=i+1 + b != 0x20 && b != 0x09 && break + i = i + 1 end opts.spacedelim && i > i0 && @goto done @@ -241,27 +241,27 @@ function tryparsenext(f::Field{T}, str::Union{VectorBackedUTF8String, String}, i end end - i>len && error("Internal error.") + i > len && error("Internal error.") @inbounds b = codeunit(str, i) - opts.spacedelim && (b!=0x20 || b!=0x09) && (i+=1; @goto done) - !opts.spacedelim && opts.endchar == b && (i+=1; @goto done) + opts.spacedelim && (b != 0x20 || b != 0x09) && (i += 1; @goto done) + !opts.spacedelim && opts.endchar == b && (i += 1; @goto done) if f.eoldelim if b == 0x0d # '\r' - i+=1 - if i<=len + i += 1 + if i <= len @inbounds b = codeunit(str, i) if b == 0x0a # '\n' - i+=1 + i += 1 end end @goto done elseif b == 0x0a # '\n' - i+=1 - if i<=len + i += 1 + if i <= len @inbounds b = codeunit(str, i) if b == 0x0d # '\r' - i+=1 + i += 1 end end @goto done @@ -275,15 +275,15 @@ function tryparsenext(f::Field{T}, str::Union{VectorBackedUTF8String, String}, i return R(convert(T, res)), i end -function tryparsenext(q::Quoted{T,S,<:UInt8,<:UInt8}, str::Union{VectorBackedUTF8String, String}, i, len, opts::LocalOpts{<:UInt8,<:UInt8,<:UInt8}) where {T,S} - if i>len +function tryparsenext(q::Quoted{T,S,<:UInt8,<:UInt8}, str::Union{VectorBackedUTF8String,String}, i, len, opts::LocalOpts{<:UInt8,<:UInt8,<:UInt8}) where {T,S} + if i > len q.required && @goto error # check to see if inner thing is ok with an empty field @chk2 x, i = tryparsenext(q.inner, str, i, len, opts) error @goto done end @inbounds b = codeunit(str, i) - ii = i+1 + ii = i + 1 quotestarted = false if q.quotechar == b quotestarted = true @@ -316,7 +316,7 @@ function tryparsenext(q::Quoted{T,S,<:UInt8,<:UInt8}, str::Union{VectorBackedUTF if q.stripwhitespaces i = eatwhitespaces(str, i, len) end - i>len && error("Internal error.") + i > len && error("Internal error.") @inbounds b = codeunit(str, i) ii = i + 1 @@ -337,7 +337,7 @@ end b == UInt8(10) || b == UInt8(13) end -function tryparsenext(s::StringToken{T}, str::Union{VectorBackedUTF8String, String}, i, len, opts::LocalOpts{<:UInt8,<:UInt8,<:UInt8}) where {T} +function tryparsenext(s::StringToken{T}, str::Union{VectorBackedUTF8String,String}, i, len, opts::LocalOpts{<:UInt8,<:UInt8,<:UInt8}) where {T} len = ncodeunits(str) inside_quoted_strong = opts.endchar == opts.quotechar escapecount = 0 @@ -345,9 +345,9 @@ function tryparsenext(s::StringToken{T}, str::Union{VectorBackedUTF8String, Stri p = UInt8(0) i0 = i if opts.includequotes - if i<=len + if i <= len @inbounds b = codeunit(str, i) - if b==opts.quotechar + if b == opts.quotechar # advance counter so that # the while loop doesn't react to opening quote i += 1 @@ -355,11 +355,11 @@ function tryparsenext(s::StringToken{T}, str::Union{VectorBackedUTF8String, Stri end end - while i<=len + while i <= len @inbounds b = codeunit(str, i) ii = i + 1 - if inside_quoted_strong && p==opts.escapechar + if inside_quoted_strong && p == opts.escapechar escapecount += 1 end @@ -373,7 +373,7 @@ function tryparsenext(s::StringToken{T}, str::Union{VectorBackedUTF8String, Stri # in that case we need to see the next char if ii > len if opts.includequotes - i=ii + i = ii end break else @@ -404,16 +404,16 @@ function tryparsenext(s::StringToken{T}, str::Union{VectorBackedUTF8String, Stri p = b end - return R(_substring(T, str, i0, i-1, escapecount, opts)), i + return R(_substring(T, str, i0, i - 1, escapecount, opts)), i end -@inline function _substring(::Type{String}, str::Union{VectorBackedUTF8String, String}, i, j, escapecount, opts::LocalOpts{<:UInt8,<:UInt8,<:UInt8}) +@inline function _substring(::Type{String}, str::Union{VectorBackedUTF8String,String}, i, j, escapecount, opts::LocalOpts{<:UInt8,<:UInt8,<:UInt8}) if escapecount > 0 - buffer = Vector{UInt8}(undef, j-i+1-escapecount) + buffer = Vector{UInt8}(undef, j - i + 1 - escapecount) cur_i = i cur_buffer_i = 1 @inbounds c = codeunit(str, cur_i) - if opts.includequotes && c==opts.quotechar + if opts.includequotes && c == opts.quotechar @inbounds buffer[cur_buffer_i] = c cur_i += 1 cur_buffer_i += 1 @@ -441,11 +441,11 @@ end end return String(buffer) else - return unsafe_string(pointer(str, i), j-i+1) + return unsafe_string(pointer(str, i), j - i + 1) end end -function tryparsenext(na::NAToken{T}, str::Union{VectorBackedUTF8String, String}, i, len, opts::LocalOpts{<:UInt8,<:UInt8,<:UInt8}) where {T} +function tryparsenext(na::NAToken{T}, str::Union{VectorBackedUTF8String,String}, i, len, opts::LocalOpts{<:UInt8,<:UInt8,<:UInt8}) where {T} R = Nullable{T} i = eatwhitespaces(str, i, len) if i > len @@ -459,13 +459,13 @@ function tryparsenext(na::NAToken{T}, str::Union{VectorBackedUTF8String, String} @inbounds b = codeunit(str, i) ii = i + 1 if (b == opts.endchar || isnewline(b)) && na.emptyisna - @goto null + @goto null end if isa(na.inner, Unknown) @goto maybe_null end - @chk2 x,ii = tryparsenext(na.inner, str, i, len, opts) maybe_null + @chk2 x, ii = tryparsenext(na.inner, str, i, len, opts) maybe_null @label done return R(convert(T, x)), ii @@ -475,7 +475,7 @@ function tryparsenext(na::NAToken{T}, str::Union{VectorBackedUTF8String, String} opts.escapechar, false, opts.includenewlines) @chk2 nastr, ii = tryparsenext(StringToken(WeakRefString{UInt8}), str, i, len, naopts) if !isempty(searchsorted(na.nastrings, nastr)) - i=ii + i = ii i = eatwhitespaces(str, i, len) @goto null end diff --git a/src/util.jl b/src/util.jl index 4199d20..1eae88a 100644 --- a/src/util.jl +++ b/src/util.jl @@ -2,7 +2,7 @@ include("lib/result.jl") -#= +#= A relic from the past macro chk1(expr,label=:error) @@ -14,10 +14,9 @@ macro chk1(expr,label=:error) x[1].value, x[2] end end -end -=# +end =# -macro chk2(expr,label=:error) +macro chk2(expr, label=:error) @assert expr.head == :(=) lhs, rhs = expr.args @@ -39,24 +38,24 @@ end @inline function parse_uint_and_stop(str, i, len, n::T) where {T <: Integer} ten = T(10) # specialize handling of the first digit so we can return an error - max_without_overflow = div(typemax(T)-9,10) # the larg + max_without_overflow = div(typemax(T) - 9, 10) # the larg y1 = iterate(str, i) - y1===nothing && return n, false, i + y1 === nothing && return n, false, i c = y1[1] if _isdigit(c) && n <= max_without_overflow n *= ten - n += T(c-'0') + n += T(c - '0') else return n, false, i end i = y1[2] y2 = iterate(str, i) - while y2!==nothing && n <= max_without_overflow + while y2 !== nothing && n <= max_without_overflow c = y2[1] if _isdigit(c) n *= ten - n += T(c-'0') + n += T(c - '0') else return n, true, i end @@ -70,7 +69,7 @@ end # slurp up extra digits @inline function read_digits(str, i, len) y = iterate(str, i) - while y!==nothing + while y !== nothing c = y[1] if !_isdigit(c) # do nothing return i @@ -81,30 +80,30 @@ end return i end -@inline function tryparsenext_base10_digit(T,str,i, len) - y = iterate(str,i) - y===nothing && @goto error +@inline function tryparsenext_base10_digit(T, str, i, len) + y = iterate(str, i) + y === nothing && @goto error c = y[1]; ii = y[2] '0' <= c <= '9' || @goto error - return convert(T, c-'0'), ii + return convert(T, c - '0'), ii @label error return nothing end Base.@pure maxdigits(::Type{T}) where {T} = ndigits(typemax(T)) -Base.@pure min_with_max_digits(::Type{T}) where {T} = convert(T, T(10)^(maxdigits(T)-1)) +Base.@pure min_with_max_digits(::Type{T}) where {T} = convert(T, T(10)^(maxdigits(T) - 1)) -@inline function tryparsenext_base10(T, str,i,len) +@inline function tryparsenext_base10(T, str, i, len) i0 = i R = Nullable{T} - y = tryparsenext_base10_digit(T,str,i, len) - y===nothing && return R(), i + y = tryparsenext_base10_digit(T, str, i, len) + y === nothing && return R(), i r = y[1]; i = y[2] # Eat zeros - while r==0 - y2 = tryparsenext_base10_digit(T,str,i, len) + while r == 0 + y2 = tryparsenext_base10_digit(T, str, i, len) y2 === nothing && return R(convert(T, 0)), i r = y2[1]; i = y2[2] end @@ -112,11 +111,11 @@ Base.@pure min_with_max_digits(::Type{T}) where {T} = convert(T, T(10)^(maxdigit digits = 1 ten = T(10) while true - y2 = tryparsenext_base10_digit(T,str,i,len) - y2===nothing && break + y2 = tryparsenext_base10_digit(T, str, i, len) + y2 === nothing && break digits += 1 d = y2[1]; i = y2[2] - r = r*ten + d + r = r * ten + d end max_digits = maxdigits(T) @@ -141,7 +140,7 @@ end R = Nullable{Int} y = iterate(str, i) - if y===nothing + if y === nothing return return R(), i else c = y[1]; ii = y[2] @@ -165,10 +164,10 @@ end @inline function eatwhitespaces(str, i=1, l=lastindex(str)) y = iterate(str, i) - while y!==nothing + while y !== nothing c = y[1]; ii = y[2] if isspace(c) - i=ii + i = ii else break end @@ -181,27 +180,27 @@ end function eatnewlines(str, i=1, l=lastindex(str)) count = 0 y = iterate(str, i) - while y!==nothing + while y !== nothing c = y[1]; ii = y[2] if c == '\r' - i=ii + i = ii y2 = iterate(str, i) - if y2!==nothing + if y2 !== nothing c = y2[1] ii = y2[2] if c == '\n' - i=ii + i = ii end end count += 1 elseif c == '\n' - i=ii + i = ii y3 = iterate(str, i) - if y3!==nothing + if y3 !== nothing c = y3[1] ii = y3[2] if c == '\r' - i=ii + i = ii end end count += 1 @@ -216,7 +215,7 @@ end # Move past consecutive lines that start with commentchar. # Return a tuple of the new pos in str and the amount of comment lines moved past. -function eatcommentlines(str, i=1, l=lastindex(str), commentchar::Union{Char, Nothing}=nothing) +function eatcommentlines(str, i=1, l=lastindex(str), commentchar::Union{Char,Nothing}=nothing) commentchar === nothing && return i, 0 count = 0 @@ -238,7 +237,7 @@ end function getlineend(str, i=1, l=lastindex(str)) y = iterate(str, i) - while y!==nothing + while y !== nothing c = y[1]; ii = y[2] isnewline(c) && break i = ii @@ -254,44 +253,44 @@ function getrowend(str, i, len, opts, delim) i0 = i i = eatwhitespaces(str, i, len) y = iterate(str, i) - while y!==nothing + while y !== nothing c = y[1]; i = y[2] - if c==Char(opts.quotechar) + if c == Char(opts.quotechar) # We are now inside a quoted field y2 = iterate(str, i) - while y2!==nothing + while y2 !== nothing c = y2[1]; i = y2[2] - if c==Char(opts.escapechar) + if c == Char(opts.escapechar) y3 = iterate(str, i) - if y3===nothing - if c==Char(opts.quotechar) + if y3 === nothing + if c == Char(opts.quotechar) return prevind(str, i) else error("Parsing error, quoted string never terminated.") end else c2 = y3[1]; ii = y3[2] - if c2==Char(opts.quotechar) + if c2 == Char(opts.quotechar) i = ii - elseif c==Char(opts.quotechar) + elseif c == Char(opts.quotechar) break end end - elseif c==Char(opts.quotechar) + elseif c == Char(opts.quotechar) break; end y2 = iterate(str, i) - if y2===nothing + if y2 === nothing error("Parsing error, quoted string never terminated.") end end i = eatwhitespaces(str, i, len) y4 = iterate(str, i) - if y4!==nothing + if y4 !== nothing c = y4[1]; i4 = y4[2] if isnewline(c) return prevind(str, i) - elseif c!=Char(delim) + elseif c != Char(delim) error("Invalid line") end else @@ -299,9 +298,9 @@ function getrowend(str, i, len, opts, delim) end else # We are now inside a non quoted field - while y!==nothing + while y !== nothing c = y[1]; i = y[2] - if c==Char(delim) + if c == Char(delim) i = eatwhitespaces(str, i, len) break elseif isnewline(c) diff --git a/test/test_vectorbackedstrings.jl b/test/test_vectorbackedstrings.jl index 4f7bc85..d84f5b1 100644 --- a/test/test_vectorbackedstrings.jl +++ b/test/test_vectorbackedstrings.jl @@ -1,51 +1,51 @@ using Test -using TextParse: VectorBackedUTF8String +using TextParse:VectorBackedUTF8String @testset "VectorBackedStrings" begin -buffer = UInt8['T', 'e', 's', 't'] + buffer = UInt8['T', 'e', 's', 't'] -s = VectorBackedUTF8String(buffer) + s = VectorBackedUTF8String(buffer) -@test s == VectorBackedUTF8String(copy(buffer)) + @test s == VectorBackedUTF8String(copy(buffer)) -@test pointer(s) == pointer(buffer) + @test pointer(s) == pointer(buffer) -@test pointer(s, 2) == pointer(buffer, 2) + @test pointer(s, 2) == pointer(buffer, 2) -@test ncodeunits(s) == length(buffer) + @test ncodeunits(s) == length(buffer) -@test codeunit(s) <: UInt8 + @test codeunit(s) <: UInt8 -@test codeunit(s, 2) == UInt8('e') + @test codeunit(s, 2) == UInt8('e') -@test thisind(s, 2) == 2 + @test thisind(s, 2) == 2 -@test isvalid(s, 2) == true + @test isvalid(s, 2) == true -@test iterate(s) == ('T', 2) + @test iterate(s) == ('T', 2) -@test iterate(s, 2) == ('e', 3) + @test iterate(s, 2) == ('e', 3) -@test iterate(s, 5) == nothing + @test iterate(s, 5) == nothing -@test string(s) == "Test" + @test string(s) == "Test" -sub_s = SubString(s, 2:3) + sub_s = SubString(s, 2:3) -@test sub_s == "es" + @test sub_s == "es" -@test pointer(sub_s, 1) == pointer(s, 2) -@test pointer(sub_s, 2) == pointer(s, 3) + @test pointer(sub_s, 1) == pointer(s, 2) + @test pointer(sub_s, 2) == pointer(s, 3) -@test_throws ErrorException s == "Test" -@test_throws ErrorException "Test" == s -@test_throws ErrorException hash(s, UInt(1)) -@test_throws ErrorException print(s) -@test_throws ErrorException textwidth(s) -@test_throws ErrorException convert(VectorBackedUTF8String, "foo") -@test_throws ErrorException convert(String, s) -@test_throws ErrorException String(s) -@test_throws ErrorException Symbol(s) + @test_throws ErrorException s == "Test" + @test_throws ErrorException "Test" == s + @test_throws ErrorException hash(s, UInt(1)) + @test_throws ErrorException print(s) + @test_throws ErrorException textwidth(s) + @test_throws ErrorException convert(VectorBackedUTF8String, "foo") + @test_throws ErrorException convert(String, s) + @test_throws ErrorException String(s) + @test_throws ErrorException Symbol(s) end