Skip to content

Commit

Permalink
Merge pull request #51 from JuliaString/spj/irrational
Browse files Browse the repository at this point in the history
Support Irrational, Complex, and other types for default formatting
  • Loading branch information
ScottPJones authored Dec 18, 2019
2 parents c4d2f61 + 075158f commit 13b184d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ keywords = ["Strings", "Formatting"]
license = "MIT"
name = "Format"
uuid = "1fa38f19-a742-5d3f-a2b9-30dd87b9d5f8"
version = "1.0.1"
version = "1.1.0"

[deps]
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Expand Down
42 changes: 34 additions & 8 deletions src/fmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,19 @@

mutable struct DefaultSpec
typechar::Char
kwargs::Any
fspec::FormatSpec
DefaultSpec(c::AbstractChar) = new(Char(c), FormatSpec(c))
end

DefaultSpec(c::AbstractChar) = DefaultSpec(Char(c), (), FormatSpec(c))
function DefaultSpec(c::AbstractChar, syms...; kwargs...)
# otherwise update the spec
if isempty(syms)
DefaultSpec(Char(c), kwargs, FormatSpec(c; kwargs...))
else
kw = _add_kwargs_from_symbols(kwargs, syms...)
DefaultSpec(Char(c), tuple(kw...), FormatSpec(c; kw...))
end
end

const DEFAULT_FORMATTERS = Dict{DataType, DefaultSpec}()
Expand All @@ -28,17 +39,17 @@ default_spec!(::Type{T}, c::AbstractChar) where {T} =
default_spec!(::Type{T}, ::Type{K}) where {T,K} =
(DEFAULT_FORMATTERS[T] = DEFAULT_FORMATTERS[K]; nothing)

# seed it with some basic default formatters
for (t, c) in [(Integer,'d'), (AbstractFloat,'f'), (AbstractChar,'c'), (AbstractString,'s')]
default_spec!(t, c)
end
default_spec!(::Type{T}, c::AbstractChar, syms...; kwargs...) where {T} =
(DEFAULT_FORMATTERS[T] = DefaultSpec(c, syms...; kwargs...); nothing)

reset!(::Type{T}) where {T} =
(dspec = default_spec(T); dspec.fspec = FormatSpec(dspec.typechar); nothing)
function reset!(::Type{T}) where {T}
dspec = default_spec(T)
dspec.fspec = FormatSpec(dspec.typechar; dspec.kwargs...)
nothing
end

# --------------------------------------------------------------------------------------------------


function _add_kwargs_from_symbols(kwargs, syms::Symbol...)
d = Dict{Symbol, Any}(kwargs)
for s in syms
Expand Down Expand Up @@ -66,6 +77,8 @@ default_spec(::Type{<:Integer}) = DEFAULT_FORMATTERS[Integer]
default_spec(::Type{<:AbstractFloat}) = DEFAULT_FORMATTERS[AbstractFloat]
default_spec(::Type{<:AbstractString}) = DEFAULT_FORMATTERS[AbstractString]
default_spec(::Type{<:AbstractChar}) = DEFAULT_FORMATTERS[AbstractChar]
default_spec(::Type{<:AbstractIrrational}) = DEFAULT_FORMATTERS[AbstractIrrational]
default_spec(::Type{<:Number}) = DEFAULT_FORMATTERS[Number]

default_spec(::Type{T}) where {T} =
get(DEFAULT_FORMATTERS, T) do
Expand Down Expand Up @@ -200,3 +213,16 @@ function fmt(x, syms::Symbol...; kwargs...)
d = _add_kwargs_from_symbols(kwargs, syms...)
fmt(x; d...)
end

# --------------------------------------------------------------------------------------------------

# seed it with some basic default formatters
for (t, c) in [(Integer,'d'),
(AbstractFloat,'f'),
(AbstractChar,'c'),
(AbstractString,'s')]
default_spec!(t, c)
end

default_spec!(Number, 's', :right)
default_spec!(AbstractIrrational, 's', :right)
5 changes: 3 additions & 2 deletions test/fmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ i = 1234567
@test fmt(i) == "1234567"
@test fmt(i,:commas) == "1,234,567"

@test_throws ErrorException fmt_default(Real)
@test_throws ErrorException fmt_default(Complex)
# These are not handled
#@test_throws ErrorException fmt_default(Real)
#@test_throws ErrorException fmt_default(Complex)

fmt_default!(Int, :commas, width = 12)
@test fmt(i) == " 1,234,567"
Expand Down

2 comments on commit 13b184d

@ScottPJones
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/6908

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v1.1.0 -m "<description of version>" 13b184dba998de1a8e1fecb3ec1b3c531bad2d42
git push origin v1.1.0

Please sign in to comment.