Skip to content

Commit 7f09fd4

Browse files
committed
Merge pull request #2 from ScottPJones/spj/update
Further improvements, addition of HTML names for characters, and full Unicode name table.
2 parents 6f53ac3 + 1e46b11 commit 7f09fd4

File tree

11 files changed

+61588
-214
lines changed

11 files changed

+61588
-214
lines changed

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,28 @@ the $ character in them, which is rather frequent in some applications.
1111
Also, Unicode sequences are represented as in Swift, i.e. as `\u{hexdigits}`, where there
1212
can be from 1 to 6 hex digits. This syntax eliminates having to worry about always outputting
1313
4 or 8 hex digits, to prevent problems with 0-9,A-F,a-f characters immediately following.
14-
Finally, I have added two new ways of representing characters in the literal string,
15-
`\:emojiname:` and `\{latexname}`.
14+
Finally, I have added four ways of representing characters in the literal string,
15+
`\:emojiname:`, `\<latexname>`, `\&htmlname;` and `\N{UnicodeName}`.
1616
This makes life a lot easier when you want to keep the text of a program in ASCII, and
1717
also to be able to write programs using those characters that might not even display
1818
correctly in their editor.
1919

2020
This now has some initial formatting capability, based on Tom Breloff's wonderful PR #10 to the
2121
JuliaLang/Formatting.jl package (by Dahua Lin and other contributors).
22+
2223
`\%(arguments)` is interpolated as a call to fmt(arguments).
24+
This is especially useful when defaults have been set for the type of the first argument.
25+
26+
`fmt_default!{T}(::Type{T}, syms::Symbol...; kwargs...)` sets the defaults for a particular type.
27+
`fmt_default!(syms::Symbol...; kwargs...)` sets the defaults for all types.
28+
Symbols that can currently be used are: `:ljust` or `:left`, `:rjust` or `:right`, `:commas`,
29+
`:zpad` or `:zeropad`, and `:ipre` or `:prefix`.
30+
`reset!{T}(::Type{T})` resets the defaults for a particular type.
31+
`defaultSpec(x)` will return the defaults for the type of x, and
32+
`defaultSpec{T}(::Type{T})` will return the defaults for the given type.
2333

24-
I also plan on adding support for `\%c(arguments)`, where c is a C style formatting character.
25-
I'm debating if I should make it take the full C style syntax, with leading 0, width/precision,
26-
etc, before the single character.
34+
This also adds support for `\%ccc(arguments)`, where ccc is one or more characters of a
35+
single C style format specification, which is interpolated as a call to cfmt("ccc", arguments).
2736

37+
I'm also experimenting with adding Python style syntax (as much as possible), as
38+
u"\{pythonformat}"

data/UnicodeData.txt

Lines changed: 29215 additions & 0 deletions
Large diffs are not rendered by default.

src/StringUtils.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ export s_unescape_string, s_escape_string, s_print_unescaped, s_print_escaped
1010

1111
include("literals.jl")
1212
include("unicodenames.jl")
13+
include("htmlnames.jl")
1314

1415
# From Formatting.jl
1516
import Base.show
1617

17-
export cfmt, fmt, fmt_default, fmt_default!
18+
export FormatSpec, FormatExpr, printfmt, printfmtln, format, generate_formatter
19+
export pyfmt, cfmt, fmt, fmt_default, fmt_default!, reset!, defaultSpec
1820

1921
include("cformat.jl" )
2022
include("fmtspec.jl")

src/cformat.jl

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

3-
function sprintf1( fmt::ASCIIString, x )
3+
function cfmt( fmt::ASCIIString, x )
44
global formatters
55
f = generate_formatter( fmt )
66
f( x )
@@ -159,7 +159,7 @@ function format{T<:Real}( x::T;
159159
)
160160
checkwidth = commas
161161
if conversion == ""
162-
if T <: FloatingPoint || T <: Rational && precision != -1
162+
if T <: AbstractFloat || T <: Rational && precision != -1
163163
actualconv = "f"
164164
elseif T <: Unsigned
165165
actualconv = "x"
@@ -178,7 +178,7 @@ function format{T<:Real}( x::T;
178178
if T <: Rational && conversion == "s"
179179
stripzeros = false
180180
end
181-
if ( T <: FloatingPoint && actualconv == "f" || T <: Integer ) && autoscale != :none
181+
if ( T <: AbstractFloat && actualconv == "f" || T <: Integer ) && autoscale != :none
182182
actualconv = "f"
183183
if autoscale == :metric
184184
scales = [
@@ -198,7 +198,7 @@ function format{T<:Real}( x::T;
198198
break
199199
end
200200
end
201-
elseif T <: FloatingPoint
201+
elseif T <: AbstractFloat
202202
smallscales = [
203203
( 1e-12, "p" ),
204204
( 1e-9, "n" ),
@@ -254,7 +254,7 @@ function format{T<:Real}( x::T;
254254
actualx = x
255255
end
256256
end
257-
s = sprintf1( generate_format_string( width=width,
257+
s = cfmt( generate_format_string( width=width,
258258
precision=precision,
259259
leftjustified=leftjustified,
260260
zeropadding=zeropadding,

src/fmt.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
21
# interface proposal by Tom Breloff (@tbreloff)... comments welcome
3-
# This uses the more basic formatting based on FormatSpec and the cfmt method (formerly called fmt, which I repurposed)
2+
# This uses the more basic formatting based on FormatSpec and the pyfmt method
3+
# (formerly called fmt, which I repurposed)
44

55
# TODO: swap out FormatSpec for something that is able to use the "format" method, which has more options for units, prefixes, etc
66
# TODO: support rational numbers, autoscale, etc as in "format"
@@ -112,7 +112,7 @@ end
112112

113113
# --------------------------------------------------------------------------------------------------
114114

115-
# TODO: get rid of this entire hack by moving commas into cfmt
115+
# TODO: get rid of this entire hack by moving commas into pyfmt
116116

117117
function optionalCommas(x::Real, s::AbstractString, fspec::FormatSpec)
118118
dpos = findfirst(s, '.')
@@ -146,12 +146,12 @@ optionalCommas(x, s::AbstractString, fspec::FormatSpec) = s
146146

147147
# TODO: do more caching to optimize repeated calls
148148

149-
# creates a new FormatSpec by overriding the defaults and passes it to cfmt
149+
# creates a new FormatSpec by overriding the defaults and passes it to pyfmt
150150
# note: adding kwargs is only appropriate for one-off formatting.
151151
# normally it will be much faster to change the fmt_default formatting as needed
152152
function fmt(x; kwargs...)
153153
fspec = isempty(kwargs) ? fmt_default(x) : FormatSpec(fmt_default(x); kwargs...)
154-
s = cfmt(fspec, x)
154+
s = pyfmt(fspec, x)
155155

156156
# add the commas now... I was confused as to when this is done currently
157157
if fspec.tsep

src/fmtspec.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ immutable FormatSpec
3939
tsep::Bool # whether to use thousand-separator
4040

4141
function FormatSpec(typ::Char;
42-
fill::Char=' ',
42+
fill::Char=' ',
4343
align::Char='\0',
4444
sign::Char='-',
4545
width::Int=-1,
@@ -181,6 +181,10 @@ _srepr(x) = repr(x)
181181
_srepr(x::AbstractString) = x
182182
_srepr(x::Char) = string(x)
183183

184+
if isdefined(:Enum)
185+
_srepr(x::Enum) = string(x)
186+
end
187+
184188
function printfmt(io::IO, fs::FormatSpec, x)
185189
cls = fs.cls
186190
ty = fs.typ
@@ -209,5 +213,5 @@ end
209213

210214
printfmt(fs::FormatSpec, x) = printfmt(STDOUT, fs, x)
211215

212-
cfmt(fs::FormatSpec, x) = (buf = IOBuffer(); printfmt(buf, fs, x); bytestring(buf))
213-
cfmt(spec::AbstractString, x) = cfmt(FormatSpec(spec), x)
216+
pyfmt(fs::FormatSpec, x) = (buf = IOBuffer(); printfmt(buf, fs, x); bytestring(buf))
217+
pyfmt(spec::AbstractString, x) = pyfmt(FormatSpec(spec), x)

src/formatexpr.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ immutable ArgSpec
1313
end
1414
end
1515

16-
getarg(args, sp::ArgSpec) =
16+
getarg(args, sp::ArgSpec) =
1717
(a = args[sp.argidx]; sp.hasfilter ? sp.filter(a) : a)
1818

1919
# pos > 0: must not have iarg in expression (use pos+1), return (entry, pos + 1)
@@ -166,6 +166,3 @@ printfmt(fe::StringOrFE, args...) = printfmt(STDOUT, fe, args...)
166166
printfmtln(io::IO, fe::StringOrFE, args...) = (printfmt(io, fe, args...); println(io))
167167
printfmtln(fe::StringOrFE, args...) = printfmtln(STDOUT, fe, args...)
168168

169-
format(fe::StringOrFE, args...) =
170-
(buf = IOBuffer(); printfmt(buf, fe, args...); bytestring(buf))
171-

0 commit comments

Comments
 (0)