Skip to content

Commit

Permalink
fix regex serialization
Browse files Browse the repository at this point in the history
also:
- add regex comparison method and serialization test
- define hash for regex
- fix date parsing
  • Loading branch information
tanmaykm committed Jun 25, 2015
1 parent 78760e2 commit 124f496
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion base/dates/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function DateFormat(f::AbstractString,locale::AbstractString="english")
for (i,k) in enumerate(s)
k == "" && break
tran = i >= endof(s) ? r"$" : match(Regex("(?<=$(s[i])).*(?=$(s[i+1]))"),f).match
slot = tran == "" || tran == r"$" ? FixedWidthSlot : DelimitedSlot
slot = tran == "" ? FixedWidthSlot : DelimitedSlot
width = length(k)
typ = 'E' in k ? DayOfWeekSlot : 'e' in k ? DayOfWeekSlot :
'y' in k ? Year : 'm' in k ? Month :
Expand Down
15 changes: 15 additions & 0 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,18 @@ function eachmatch(re::Regex, str::AbstractString, ovr::Bool=false)
end

eachmatch(re::Regex, str::AbstractString) = RegexMatchIterator(re,str)

## comparison ##

function ==(a::Regex, b::Regex)
a.pattern == b.pattern && a.compile_options == b.compile_options && a.match_options == b.match_options
end

## hash ##
const hashre_seed = UInt === UInt64 ? 0x67e195eb8555e72d : 0xe32373e4
function hash(r::Regex, h::UInt)
h += hashre_seed
h = hash(r.pattern, h)
h = hash(r.compile_options, h)
h = hash(r.match_options, h)
end
8 changes: 5 additions & 3 deletions base/serialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ end
function serialize(s::SerializationState, r::Regex)
serialize_type(s, typeof(r))
serialize(s, r.pattern)
serialize(s, r.options)
serialize(s, r.compile_options)
serialize(s, r.match_options)
end

function serialize(s::SerializationState, n::BigInt)
Expand Down Expand Up @@ -706,8 +707,9 @@ deserialize(s::SerializationState, ::Type{BigInt}) = get(GMP.tryparse_internal(B

function deserialize(s::SerializationState, t::Type{Regex})
pattern = deserialize(s)
options = deserialize(s)
Regex(pattern, options)
compile_options = deserialize(s)
match_options = deserialize(s)
Regex(pattern, compile_options, match_options)
end

end
9 changes: 9 additions & 0 deletions test/serialize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,12 @@ create_serialization_stream() do s
@test length(b) == 5
@test isa(b,Vector{Any})
end

# Regex
create_serialization_stream() do s
r1 = r"a?b.*"
serialize(s, r1)
seekstart(s)
r2 = deserialize(s)
@test r1 == r2
end

0 comments on commit 124f496

Please sign in to comment.