Skip to content

Commit befc01c

Browse files
committed
apply changes to cfmt() and add tests
1 parent 82b9b43 commit befc01c

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/cformat.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
formatters = Dict{ ASCIIStr, Function }()
22

3-
cfmt( fmt::ASCIIStr, x ) = m_eval(Expr(:call, generate_formatter( fmt ), x))
3+
cfmt( fmt::ASCIIStr, x::Union{<:AbstractString,<:Real} ) = m_eval(Expr(:call, generate_formatter( fmt ), x))
4+
5+
function cfmt( fmt_str::ASCIIStr, x::Number )
6+
#remove width information
7+
new_fmt_str = replace(fmt_str, r"(%(\d+\$)?[\-\+#0' ]*)(\d+)?"=>s"\1")
8+
s = fmt_Number(x, x->m_eval(Expr(:call, generate_formatter( new_fmt_str ), AbstractFloat(x))))
9+
# extract width information
10+
m = match(r"%(\d+\$)?[\-\+#0' ]*(\d+)?", fmt_str)
11+
width = m[2] == nothing ? 0 : parse(Int, m[2])
12+
fmt(s, width, occursin("-", fmt_str) ? :left : :right)
13+
end
414

515
function checkfmt(fmt)
616
test = PF.parse( fmt )

test/fmt.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ i = 1234567
2020
@test fmt(i) == "1234567"
2121
@test fmt(i,:commas) == "1,234,567"
2222

23-
# These are not handled
23+
# All Number types are now handled, default is right aligned string
2424
#@test_throws ErrorException fmt_default(Real)
2525
#@test_throws ErrorException fmt_default(Complex)
26+
fmt_default(Real) == fmt_default(Number) == FormatSpec('s', align = '>')
2627

2728
@test fmt(2 - 3im, 10) == " 2 - 3im"
2829
@test fmt(pi - 3im, 15, 2) == " 3.14 - 3.00im"

0 commit comments

Comments
 (0)