Skip to content

Commit 3458995

Browse files
KristofferCaplavin
andauthored
make Parsers a weakdep (#78)
* make Parsers a weakdep * make Parsers a weakdep * reorder the project entries a bit * Trim some extra empty lines --------- Co-authored-by: Alexander Plavin <alexander@plav.in>
1 parent d54fa1b commit 3458995

File tree

3 files changed

+83
-74
lines changed

3 files changed

+83
-74
lines changed

Project.toml

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,24 @@ version = "1.4.1"
66
[deps]
77
Parsers = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
88

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

1513
[compat]
1614
Parsers = "2"
1715
julia = "1.6"
1816

19-
[weakdeps]
20-
ArrowTypes = "31f734f8-188a-4ce0-8406-c8a06bd891cd"
21-
2217
[extensions]
2318
ArrowTypesExt = "ArrowTypes"
19+
ParsersExt = "Parsers"
20+
21+
[extras]
22+
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"
23+
Parsers = "69de0a69-1ddd-5017-9359-2bf0b02dc9f0"
24+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
25+
Serialization = "9e88b42a-f829-5b0c-bbe9-9e923198166b"
26+
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2427

2528
[targets]
26-
test = ["Arrow", "Test", "Random", "Serialization"]
29+
test = ["Arrow", "Test", "Parsers", "Random", "Serialization"]

ext/ParsersExt.jl

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
module ParsersExt
2+
using Parsers
3+
using InlineStrings: InlineString, InlineString1, addcodeunit
4+
5+
Parsers.xparse(::Type{T}, buf::AbstractString, pos, len, options, ::Type{S}=T) where {T <: InlineString, S} =
6+
Parsers.xparse(T, codeunits(buf), pos, len, options, S)
7+
8+
function Parsers.xparse(::Type{T}, source::Union{AbstractVector{UInt8}, IO}, pos, len, options::Parsers.Options, ::Type{S}=T) where {T <: InlineString, S}
9+
res = Parsers.xparse(String, source, pos, len, options, PosLen)
10+
code = res.code
11+
overflowed = false
12+
poslen = res.val
13+
if !Parsers.valueok(code) || Parsers.sentinel(code)
14+
x = T()
15+
else
16+
poslen = res.val
17+
if T === InlineString1
18+
if poslen.len != 1
19+
overflowed = true
20+
x = T()
21+
else
22+
Parsers.fastseek!(source, poslen.pos)
23+
x = InlineString1(Parsers.peekbyte(source, poslen.pos))
24+
Parsers.fastseek!(source, pos + res.tlen - 1)
25+
end
26+
elseif Parsers.escapedstring(code) || !(source isa AbstractVector{UInt8})
27+
if poslen.len > (sizeof(T) - 1)
28+
overflowed = true
29+
x = T()
30+
else
31+
# manually build up InlineString
32+
i = poslen.pos
33+
maxi = i + poslen.len
34+
x = T()
35+
Parsers.fastseek!(source, i - 1)
36+
while i < maxi
37+
b = Parsers.peekbyte(source, i)
38+
if b == options.e
39+
i += 1
40+
Parsers.incr!(source)
41+
b = Parsers.peekbyte(source, i)
42+
end
43+
x, overflowed = addcodeunit(x, b)
44+
i += 1
45+
Parsers.incr!(source)
46+
end
47+
Parsers.fastseek!(source, maxi)
48+
end
49+
else
50+
vlen = poslen.len
51+
if vlen > (sizeof(T) - 1)
52+
# @show T, vlen, sizeof(T)
53+
overflowed = true
54+
x = T()
55+
else
56+
# @show poslen.pos, vlen
57+
x = T(source, poslen.pos, vlen)
58+
end
59+
end
60+
end
61+
if overflowed
62+
code |= Parsers.OVERFLOW
63+
end
64+
return Parsers.Result{S}(code, res.tlen, x)
65+
end
66+
67+
end

src/InlineStrings.jl

Lines changed: 4 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ module InlineStrings
22

33
import Base: ==
44

5-
using Parsers
6-
75
export InlineString, InlineStringType, inlinestrings
86
export @inline_str
97

@@ -876,69 +874,6 @@ end
876874
end
877875
end
878876

879-
# Parsers.xparse
880-
Parsers.xparse(::Type{T}, buf::AbstractString, pos, len, options, ::Type{S}=T) where {T <: InlineString, S} =
881-
Parsers.xparse(T, codeunits(buf), pos, len, options, S)
882-
883-
function Parsers.xparse(::Type{T}, source::Union{AbstractVector{UInt8}, IO}, pos, len, options::Parsers.Options, ::Type{S}=T) where {T <: InlineString, S}
884-
res = Parsers.xparse(String, source, pos, len, options, PosLen)
885-
code = res.code
886-
overflowed = false
887-
poslen = res.val
888-
if !Parsers.valueok(code) || Parsers.sentinel(code)
889-
x = T()
890-
else
891-
poslen = res.val
892-
if T === InlineString1
893-
if poslen.len != 1
894-
overflowed = true
895-
x = T()
896-
else
897-
Parsers.fastseek!(source, poslen.pos)
898-
x = InlineString1(Parsers.peekbyte(source, poslen.pos))
899-
Parsers.fastseek!(source, pos + res.tlen - 1)
900-
end
901-
elseif Parsers.escapedstring(code) || !(source isa AbstractVector{UInt8})
902-
if poslen.len > (sizeof(T) - 1)
903-
overflowed = true
904-
x = T()
905-
else
906-
# manually build up InlineString
907-
i = poslen.pos
908-
maxi = i + poslen.len
909-
x = T()
910-
Parsers.fastseek!(source, i - 1)
911-
while i < maxi
912-
b = Parsers.peekbyte(source, i)
913-
if b == options.e
914-
i += 1
915-
Parsers.incr!(source)
916-
b = Parsers.peekbyte(source, i)
917-
end
918-
x, overflowed = addcodeunit(x, b)
919-
i += 1
920-
Parsers.incr!(source)
921-
end
922-
Parsers.fastseek!(source, maxi)
923-
end
924-
else
925-
vlen = poslen.len
926-
if vlen > (sizeof(T) - 1)
927-
# @show T, vlen, sizeof(T)
928-
overflowed = true
929-
x = T()
930-
else
931-
# @show poslen.pos, vlen
932-
x = T(source, poslen.pos, vlen)
933-
end
934-
end
935-
end
936-
if overflowed
937-
code |= Parsers.OVERFLOW
938-
end
939-
return Parsers.Result{S}(code, res.tlen, x)
940-
end
941-
942877
## InlineString sorting
943878
using Base.Sort, Base.Order
944879

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

1074+
if !isdefined(Base, :get_extension)
1075+
include("../ext/ParsersExt.jl")
1076+
end
1077+
11391078
end # module

0 commit comments

Comments
 (0)