Skip to content

Commit

Permalink
Merge pull request #17 from anthonyclays/hh-parse
Browse files Browse the repository at this point in the history
parsing improvements by gpapini-it
  • Loading branch information
hhaensel authored Nov 30, 2021
2 parents 0d4cba7 + b93f810 commit 653fed5
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/RomanNumerals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module RomanNumerals
# Defines the exception the struct RomanNumeral
include("types.jl")

# Defines the functions toroman and parse(RomanNumeral, x), to convert from
# Roman-formatted string to int and vice versa
include("roman_conversion.jl")
# Defines the functions toroman, fromroman and parse(RomanNumeral, x), to
# convert from Roman-formatted string to int and vice versa
include("parsing.jl")

# Defines new method dispatching specific to Roman numerals
include("arithmetic.jl")
Expand Down
9 changes: 6 additions & 3 deletions src/roman_conversion.jl → src/parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ const NUMERAL_MAP = [
(1, "I")
]

import Base: parse
function parse(::Type{RomanNumeral}, str::AbstractString)
function fromroman(str::AbstractString)
m = match(VALID_ROMAN_PATTERN, str)
m nothing && throw(Meta.ParseError(str * " is not a valid roman numeral"))
# Strip whitespace and make uppercase
Expand All @@ -58,9 +57,13 @@ function parse(::Type{RomanNumeral}, str::AbstractString)
i += numlen
end
end
val
return val
end

import Base: parse
parse(::Type{RomanNumeral}, s::AbstractString) = RomanNumeral(fromroman(s))

# using Compat: @warn
function toroman(val::Integer)
val <= 0 && throw(DomainError(val, "in ancient Rome there were only strictly positive numbers"))
val > maximum(first(t) for t in NUMERAL_MAP) && @warn("Roman numerals do not handle large numbers well") # instead if maximum, use NUMERAL_MAP[1][1]?
Expand Down
2 changes: 1 addition & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ struct RomanNumeral <: Integer
str::String
RomanNumeral(int::Integer) = new(int, toroman(int))
function RomanNumeral(str::AbstractString)
num = parse(RomanNumeral, str)
num = fromroman(str)
new(num, toroman(num))
end
end
Expand Down

1 comment on commit 653fed5

@hhaensel
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.