Skip to content

Commit

Permalink
make Parsers a weakdep (JuliaStrings#78)
Browse files Browse the repository at this point in the history
* make Parsers a weakdep

* make Parsers a weakdep

* reorder the project entries a bit

* Trim some extra empty lines

---------

Co-authored-by: Alexander Plavin <[email protected]>
  • Loading branch information
KristofferC and aplavin committed Jul 12, 2024
1 parent d54fa1b commit 3458995
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 74 deletions.
21 changes: 12 additions & 9 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ version = "1.4.1"
[deps]
Parsers = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"

[extras]
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
[weakdeps]
ArrowTypes = "31f734f8-188a-4ce0-8406-c8a06bd891cd"
Parsers = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"

[compat]
Parsers = "2"
julia = "1.6"

[weakdeps]
ArrowTypes = "31f734f8-188a-4ce0-8406-c8a06bd891cd"

[extensions]
ArrowTypesExt = "ArrowTypes"
ParsersExt = "Parsers"

[extras]
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"
Parsers = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Arrow", "Test", "Random", "Serialization"]
test = ["Arrow", "Test", "Parsers", "Random", "Serialization"]
67 changes: 67 additions & 0 deletions ext/ParsersExt.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
module ParsersExt
using Parsers
using InlineStrings: InlineString, InlineString1, addcodeunit

Parsers.xparse(::Type{T}, buf::AbstractString, pos, len, options, ::Type{S}=T) where {T <: InlineString, S} =
Parsers.xparse(T, codeunits(buf), pos, len, options, S)

function Parsers.xparse(::Type{T}, source::Union{AbstractVector{UInt8}, IO}, pos, len, options::Parsers.Options, ::Type{S}=T) where {T <: InlineString, S}
res = Parsers.xparse(String, source, pos, len, options, PosLen)
code = res.code
overflowed = false
poslen = res.val
if !Parsers.valueok(code) || Parsers.sentinel(code)
x = T()
else
poslen = res.val
if T === InlineString1
if poslen.len != 1
overflowed = true
x = T()
else
Parsers.fastseek!(source, poslen.pos)
x = InlineString1(Parsers.peekbyte(source, poslen.pos))
Parsers.fastseek!(source, pos + res.tlen - 1)
end
elseif Parsers.escapedstring(code) || !(source isa AbstractVector{UInt8})
if poslen.len > (sizeof(T) - 1)
overflowed = true
x = T()
else
# manually build up InlineString
i = poslen.pos
maxi = i + poslen.len
x = T()
Parsers.fastseek!(source, i - 1)
while i < maxi
b = Parsers.peekbyte(source, i)
if b == options.e
i += 1
Parsers.incr!(source)
b = Parsers.peekbyte(source, i)
end
x, overflowed = addcodeunit(x, b)
i += 1
Parsers.incr!(source)
end
Parsers.fastseek!(source, maxi)
end
else
vlen = poslen.len
if vlen > (sizeof(T) - 1)
# @show T, vlen, sizeof(T)
overflowed = true
x = T()
else
# @show poslen.pos, vlen
x = T(source, poslen.pos, vlen)
end
end
end
if overflowed
code |= Parsers.OVERFLOW
end
return Parsers.Result{S}(code, res.tlen, x)
end

end
69 changes: 4 additions & 65 deletions src/InlineStrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ module InlineStrings

import Base: ==

using Parsers

export InlineString, InlineStringType, inlinestrings
export @inline_str

Expand Down Expand Up @@ -876,69 +874,6 @@ end
end
end

# Parsers.xparse
Parsers.xparse(::Type{T}, buf::AbstractString, pos, len, options, ::Type{S}=T) where {T <: InlineString, S} =
Parsers.xparse(T, codeunits(buf), pos, len, options, S)

function Parsers.xparse(::Type{T}, source::Union{AbstractVector{UInt8}, IO}, pos, len, options::Parsers.Options, ::Type{S}=T) where {T <: InlineString, S}
res = Parsers.xparse(String, source, pos, len, options, PosLen)
code = res.code
overflowed = false
poslen = res.val
if !Parsers.valueok(code) || Parsers.sentinel(code)
x = T()
else
poslen = res.val
if T === InlineString1
if poslen.len != 1
overflowed = true
x = T()
else
Parsers.fastseek!(source, poslen.pos)
x = InlineString1(Parsers.peekbyte(source, poslen.pos))
Parsers.fastseek!(source, pos + res.tlen - 1)
end
elseif Parsers.escapedstring(code) || !(source isa AbstractVector{UInt8})
if poslen.len > (sizeof(T) - 1)
overflowed = true
x = T()
else
# manually build up InlineString
i = poslen.pos
maxi = i + poslen.len
x = T()
Parsers.fastseek!(source, i - 1)
while i < maxi
b = Parsers.peekbyte(source, i)
if b == options.e
i += 1
Parsers.incr!(source)
b = Parsers.peekbyte(source, i)
end
x, overflowed = addcodeunit(x, b)
i += 1
Parsers.incr!(source)
end
Parsers.fastseek!(source, maxi)
end
else
vlen = poslen.len
if vlen > (sizeof(T) - 1)
# @show T, vlen, sizeof(T)
overflowed = true
x = T()
else
# @show poslen.pos, vlen
x = T(source, poslen.pos, vlen)
end
end
end
if overflowed
code |= Parsers.OVERFLOW
end
return Parsers.Result{S}(code, res.tlen, x)
end

## InlineString sorting
using Base.Sort, Base.Order

Expand Down Expand Up @@ -1136,4 +1071,8 @@ Base.Broadcast.broadcasted(::Type{InlineString}, A::AbstractArray) = inlinestrin
Base.map(::Type{InlineString}, A::AbstractArray) = inlinestrings(A)
Base.collect(::Type{InlineString}, A::AbstractArray) = inlinestrings(A)

if !isdefined(Base, :get_extension)
include("../ext/ParsersExt.jl")
end

end # module

0 comments on commit 3458995

Please sign in to comment.