Skip to content
Browse files

modularization: split off Grisu and Printf modules.

For some reason @printf doesn't get imported correctly into Main.
  • Loading branch information...
1 parent 73d8ca4 commit 0213f82c0cea8808945622713a4d30c69f02c6fc @StefanKarpinski StefanKarpinski committed
Showing with 237 additions and 223 deletions.
  1. +39 −33 base/grisu.jl
  2. +193 −185 base/printf.jl
  3. +1 −1 base/start_image.jl
  4. +1 −1 base/stat.jl
  5. +2 −2 base/sysimg.jl
  6. +1 −1 test/perf/perf.jl
View
72 base/grisu.jl
@@ -1,25 +1,29 @@
-_jl_libgrisu = dlopen("libgrisu")
+libgrisu = dlopen("libgrisu")
-const _jl_neg = Array(Bool,1)
-const _jl_digits = Array(Uint8,309+17)
-const _jl_buflen = int32(length(_jl_digits)+1)
-const _jl_length = Array(Int32,1)
-const _jl_point = Array(Int32,1)
+module Grisu
+import Base.*
+export @grisu_ccall, _neg, _digits, _buflen, _len, _point
@JeffBezanson The Julia Language member

Exporting identifiers starting with _?

@StefanKarpinski The Julia Language member

Yeah, yeah. I committed this before we discussed that! I need to rework this whole business. These are basically shared private state that's only shared between Printf and Grisu — Grisu isn't really meant to be imported from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+const _neg = Array(Bool,1)
+const _digits = Array(Uint8,309+17)
+const _buflen = int32(length(_digits)+1)
+const _len = Array(Int32,1)
+const _point = Array(Int32,1)
macro grisu_ccall(value, mode, ndigits)
quote
- ccall(dlsym(_jl_libgrisu, :grisu), Void,
+ ccall(dlsym(Base.libgrisu, :grisu), Void,
(Float64, Int32, Int32, Ptr{Uint8}, Int32,
Ptr{Bool}, Ptr{Int32}, Ptr{Int32}),
$value, $mode, $ndigits,
- _jl_digits, _jl_buflen, _jl_neg, _jl_length, _jl_point)
+ _digits, _buflen, _neg, _len, _point)
end
end
-const GRISU_SHORTEST = int32(0) # shortest exact representation for doubles
-const GRISU_SHORTEST_SINGLE = int32(1) # shortest exact representation for singles
-const GRISU_FIXED = int32(2) # fixed number of trailing decimal points
-const GRISU_PRECISION = int32(3) # fixed precision regardless of magnitude
+const SHORTEST = int32(0) # shortest exact representation for doubles
+const SHORTEST_SINGLE = int32(1) # shortest exact representation for singles
+const FIXED = int32(2) # fixed number of trailing decimal points
+const PRECISION = int32(3) # fixed precision regardless of magnitude
# wrapper for the core grisu function, primarily for debugging
global grisu, grisu_fix, grisu_sig
@@ -27,32 +31,32 @@ function grisu(x::Float64, mode::Integer, ndigits::Integer)
if !isfinite(x); error("non-finite value: $x"); end
if ndigits < 0; error("negative digits requested"); end
@grisu_ccall x mode ndigits
- _jl_neg[1], _jl_digits[1:_jl_length[1]], _jl_point[1]
+ _neg[1], _digits[1:_len[1]], _point[1]
end
-grisu(x::Float64) = grisu(x, GRISU_SHORTEST, int32(0))
-grisu(x::Float32) = grisu(float64(x), GRISU_SHORTEST_SINGLE, int32(0))
+grisu(x::Float64) = grisu(x, SHORTEST, int32(0))
+grisu(x::Float32) = grisu(float64(x), SHORTEST_SINGLE, int32(0))
grisu(x::Real) = grisu(float(x))
function grisu_fix(x::Real, n::Integer)
if n > 17; n = 17; end
- grisu(float64(x), GRISU_FIXED, int32(n))
+ grisu(float64(x), FIXED, int32(n))
end
function grisu_sig(x::Real, n::Integer)
if n > 309; n = 309; end
- grisu(float64(x), GRISU_PRECISION, int32(n))
+ grisu(float64(x), PRECISION, int32(n))
end
function _show(io, x::Float, mode::Int32, n::Int)
if isnan(x); return write(io, "NaN"); end
if isinf(x); return write(io, x < 0 ? "-Inf" : "Inf"); end
@grisu_ccall x mode n
- pdigits = pointer(_jl_digits)
- neg = _jl_neg[1]
- len = _jl_length[1]
- pt = _jl_point[1]
- if mode == GRISU_PRECISION
- while len > 1 && _jl_digits[len] == '0'
+ pdigits = pointer(_digits)
+ neg = _neg[1]
+ len = _len[1]
+ pt = _point[1]
+ if mode == PRECISION
+ while len > 1 && _digits[len] == '0'
len -= 1
end
end
@@ -94,9 +98,9 @@ function _show(io, x::Float, mode::Int32, n::Int)
nothing
end
-show(io, x::Float64) = _show(io, x, GRISU_SHORTEST, 0)
-show(io, x::Float32) = _show(io, x, GRISU_SHORTEST_SINGLE, 0)
-showcompact(io, x::Float) = _show(io, x, GRISU_PRECISION, 6)
+show(io, x::Float64) = _show(io, x, SHORTEST, 0)
+show(io, x::Float32) = _show(io, x, SHORTEST_SINGLE, 0)
+showcompact(io, x::Float) = _show(io, x, PRECISION, 6)
# normal:
# 0 < pt < len ####.#### len+1
@@ -107,14 +111,14 @@ showcompact(io, x::Float) = _show(io, x, GRISU_PRECISION, 6)
# pt <= 0 ########e-### len+k+2
# 0 < pt ########e### len+k+1
-function _jl_print_shortest(io, x::Float, dot::Bool, mode::Int32)
+function _print_shortest(io, x::Float, dot::Bool, mode::Int32)
if isnan(x); return write(io, "NaN"); end
if isinf(x); return write(io, x < 0 ? "-Inf" : "Inf"); end
@grisu_ccall x mode 0
- pdigits = pointer(_jl_digits)
- neg = _jl_neg[1]
- len = _jl_length[1]
- pt = _jl_point[1]
+ pdigits = pointer(_digits)
+ neg = _neg[1]
+ len = _len[1]
+ pt = _point[1]
if neg
write(io, '-')
end
@@ -151,6 +155,8 @@ function _jl_print_shortest(io, x::Float, dot::Bool, mode::Int32)
nothing
end
-print_shortest(io, x::Float64, dot::Bool) = _jl_print_shortest(io, x, dot, GRISU_SHORTEST)
-print_shortest(io, x::Float32, dot::Bool) = _jl_print_shortest(io, x, dot, GRISU_SHORTEST_SINGLE)
+print_shortest(io, x::Float64, dot::Bool) = _print_shortest(io, x, dot, SHORTEST)
+print_shortest(io, x::Float32, dot::Bool) = _print_shortest(io, x, dot, SHORTEST_SINGLE)
print_shortest(io, x::Union(Float,Integer)) = print_shortest(io, float(x), false)
+
+end # module
View
378 base/printf.jl
@@ -1,20 +1,24 @@
+module Printf
+import Base.*
+import Base.Grisu.*
+
### printf formatter generation ###
-function _jl_printf_gen(s::String)
+function printf_gen(s::String)
args = {:(out::IOStream)}
blk = expr(:block, :(local neg, pt, len, exp))
- for x in _jl_printf_parse(s)
+ for x in printf_parse(s)
if isa(x,String)
push(blk.args, :(write(out, $(strlen(x)==1 ? x[1] : x))))
else
c = lc(x[end])
- f = c=='f' ? _jl_printf_f :
- c=='e' ? _jl_printf_e :
- c=='g' ? _jl_printf_g :
- c=='c' ? _jl_printf_c :
- c=='s' ? _jl_printf_s :
- c=='p' ? _jl_printf_p :
- _jl_printf_d
+ f = c=='f' ? printf_f :
+ c=='e' ? printf_e :
+ c=='g' ? printf_g :
+ c=='c' ? printf_c :
+ c=='s' ? printf_s :
+ c=='p' ? printf_p :
+ printf_d
arg, ex = f(x...)
push(args, arg)
push(blk.args, ex)
@@ -26,7 +30,7 @@ end
### printf format string parsing ###
-function _jl_printf_parse(s::String)
+function printf_parse(s::String)
# parse format string in to stings and format tuples
list = {}
i = j = start(s)
@@ -34,7 +38,7 @@ function _jl_printf_parse(s::String)
c, k = next(s,j)
if c == '%'
isempty(s[i:j-1]) || push(list, utf8(unescape_string(s[i:j-1])))
- flags, width, precision, conversion, k = _jl_printf_parse1(s,k)
+ flags, width, precision, conversion, k = printf_parse1(s,k)
contains(flags,'\'') && error("printf format flag ' not yet supported")
conversion == 'a' && error("printf feature %a not yet supported")
conversion == 'n' && error("printf feature %n not supported")
@@ -74,48 +78,48 @@ end
# (h|hh|l|ll|L|j|t|z|q)? # modifier (ignored)
# [diouxXeEfFgGaAcCsSp%] # conversion
-_jl_next_or_die(s::String, k) = !done(s,k) ? next(s,k) :
+next_or_die(s::String, k) = !done(s,k) ? next(s,k) :
error("invalid printf format string: ", sshow(s))
-function _jl_printf_parse1(s::String, k::Integer)
+function printf_parse1(s::String, k::Integer)
j = k
width = 0
precision = -1
- c, k = _jl_next_or_die(s,k)
+ c, k = next_or_die(s,k)
# handle %%
if c == '%'
return "", width, precision, c, k
end
# parse flags
while contains("#0- + '", c)
- c, k = _jl_next_or_die(s,k)
+ c, k = next_or_die(s,k)
end
flags = ascii(s[j:k-2])
# parse width
while '0' <= c <= '9'
width = 10*width + c-'0'
- c, k = _jl_next_or_die(s,k)
+ c, k = next_or_die(s,k)
end
# parse precision
if c == '.'
- c, k = _jl_next_or_die(s,k)
+ c, k = next_or_die(s,k)
if '0' <= c <= '9'
precision = 0
while '0' <= c <= '9'
precision = 10*precision + c-'0'
- c, k = _jl_next_or_die(s,k)
+ c, k = next_or_die(s,k)
end
end
end
# parse length modifer (ignored)
if c == 'h' || c == 'l'
prev = c
- c, k = _jl_next_or_die(s,k)
+ c, k = next_or_die(s,k)
if c == prev
- c, k = _jl_next_or_die(s,k)
+ c, k = next_or_die(s,k)
end
elseif contains("Ljqtz",c)
- c, k = _jl_next_or_die(s,k)
+ c, k = next_or_die(s,k)
end
# validate conversion
if !contains("diouxXDOUeEfFgGaAcCsSpn", c)
@@ -127,7 +131,7 @@ end
### printf formatter generation ###
-function _jl_special_handler(flags::ASCIIString, width::Int)
+function special_handler(flags::ASCIIString, width::Int)
@gensym x
blk = expr(:block)
pad = contains(flags,'-') ? rpad : lpad
@@ -142,7 +146,7 @@ function _jl_special_handler(flags::ASCIIString, width::Int)
x, ex, blk
end
-function _jl_printf_pad(m::Int, n, c::Char)
+function printf_pad(m::Int, n, c::Char)
if m <= 1
:($n > 0 && write(out,$c))
else
@@ -157,10 +161,10 @@ function _jl_printf_pad(m::Int, n, c::Char)
end
end
-function _jl_print_fixed(out, precision)
- pdigits = pointer(_jl_digits)
- ndigits = _jl_length[1]
- pt = _jl_point[1]
+function print_fixed(out, precision)
+ pdigits = pointer(_digits)
+ ndigits = _len[1]
+ pt = _point[1]
if pt <= 0
# 0.0dddd0
write(out, '0')
@@ -194,7 +198,7 @@ function _jl_print_fixed(out, precision)
end
end
-function _jl_print_exp(out, exp)
+function print_exp(out, exp)
write(out, exp < 0 ? '-' : '+')
exp = abs(exp)
d = div(exp,100)
@@ -204,7 +208,7 @@ function _jl_print_exp(out, exp)
write(out, char('0'+rem(exp,10)))
end
-function _jl_printf_d(flags::ASCIIString, width::Int, precision::Int, c::Char)
+function printf_d(flags::ASCIIString, width::Int, precision::Int, c::Char)
# print integer:
# [dDiu]: print decimal digits
# [o]: print octal digits
@@ -218,23 +222,23 @@ function _jl_printf_d(flags::ASCIIString, width::Int, precision::Int, c::Char)
# ( ): precede non-negative values with " "
# (+): precede non-negative values with "+"
#
- x, ex, blk = _jl_special_handler(flags,width)
+ x, ex, blk = special_handler(flags,width)
# interpret the number
prefix = ""
if lc(c)=='o'
- f = contains(flags,'#') ? :_jl_int_0ct : :_jl_int_oct
+ f = contains(flags,'#') ? :int_0ct : :int_oct
push(blk.args, :(($f)($x)))
elseif c=='x'
if contains(flags,'#'); prefix = "0x"; end
- push(blk.args, :(_jl_int_hex($x)))
+ push(blk.args, :(int_hex($x)))
elseif c=='X'
if contains(flags,'#'); prefix = "0X"; end
- push(blk.args, :(_jl_int_HEX($x)))
+ push(blk.args, :(int_HEX($x)))
else
- push(blk.args, :(_jl_int_dec($x)))
+ push(blk.args, :(int_dec($x)))
end
- push(blk.args, :(neg = _jl_neg[1]))
- push(blk.args, :(pt = _jl_point[1]))
+ push(blk.args, :(neg = _neg[1]))
+ push(blk.args, :(pt = _point[1]))
# calculate padding
width -= strlen(prefix)
space_pad = width > max(1,precision) && contains(flags,'-') ||
@@ -256,7 +260,7 @@ function _jl_printf_d(flags::ASCIIString, width::Int, precision::Int, c::Char)
end
# print space padding
if padding != nothing && !contains(flags,'-')
- push(blk.args, _jl_printf_pad(width-precision, padding, ' '))
+ push(blk.args, printf_pad(width-precision, padding, ' '))
end
# print sign
contains(flags,'+') ? push(blk.args, :(write(out, neg?'-':'+'))) :
@@ -268,23 +272,23 @@ function _jl_printf_d(flags::ASCIIString, width::Int, precision::Int, c::Char)
end
# print zero padding & leading zeros
if space_pad && precision > 1
- push(blk.args, _jl_printf_pad(precision-1, :($precision-pt), '0'))
+ push(blk.args, printf_pad(precision-1, :($precision-pt), '0'))
elseif !space_pad && width > 1
zeros = contains(flags,'+') || contains(flags,' ') ?
:($(width-1)-pt) : :($width-neg-pt)
- push(blk.args, _jl_printf_pad(width-1, zeros, '0'))
+ push(blk.args, printf_pad(width-1, zeros, '0'))
end
# print integer
- push(blk.args, :(write(out, pointer(_jl_digits), pt)))
+ push(blk.args, :(write(out, pointer(_digits), pt)))
# print padding
if padding != nothing && contains(flags,'-')
- push(blk.args, _jl_printf_pad(width-precision, padding, ' '))
+ push(blk.args, printf_pad(width-precision, padding, ' '))
end
# return arg, expr
:(($x)::Real), ex
end
-function _jl_printf_f(flags::ASCIIString, width::Int, precision::Int, c::Char)
+function printf_f(flags::ASCIIString, width::Int, precision::Int, c::Char)
# print to fixed trailing precision
# [fF]: the only choice
#
@@ -295,13 +299,13 @@ function _jl_printf_f(flags::ASCIIString, width::Int, precision::Int, c::Char)
# ( ): precede non-negative values with " "
# (+): precede non-negative values with "+"
#
- x, ex, blk = _jl_special_handler(flags,width)
+ x, ex, blk = special_handler(flags,width)
# interpret the number
if precision < 0; precision = 6; end
- push(blk.args, :(_jl_fix_dec($x,$precision)))
- push(blk.args, :(neg = _jl_neg[1]))
- push(blk.args, :(pt = _jl_point[1]))
- push(blk.args, :(len = _jl_length[1]))
+ push(blk.args, :(fix_dec($x,$precision)))
+ push(blk.args, :(neg = _neg[1]))
+ push(blk.args, :(pt = _point[1]))
+ push(blk.args, :(len = _len[1]))
# calculate padding
padding = nothing
if precision > 0 || contains(flags,'#')
@@ -319,7 +323,7 @@ function _jl_printf_f(flags::ASCIIString, width::Int, precision::Int, c::Char)
end
# print space padding
if padding != nothing && !contains(flags,'-') && !contains(flags,'0')
- push(blk.args, _jl_printf_pad(width-1, padding, ' '))
+ push(blk.args, printf_pad(width-1, padding, ' '))
end
# print sign
contains(flags,'+') ? push(blk.args, :(write(out, neg?'-':'+'))) :
@@ -327,25 +331,25 @@ function _jl_printf_f(flags::ASCIIString, width::Int, precision::Int, c::Char)
push(blk.args, :(neg && write(out, '-')))
# print zero padding
if padding != nothing && !contains(flags,'-') && contains(flags,'0')
- push(blk.args, _jl_printf_pad(width-1, padding, '0'))
+ push(blk.args, printf_pad(width-1, padding, '0'))
end
# print digits
if precision > 0
- push(blk.args, :(_jl_print_fixed(out,$precision)))
+ push(blk.args, :(print_fixed(out,$precision)))
else
- push(blk.args, :(write(out, pointer(_jl_digits), len)))
+ push(blk.args, :(write(out, pointer(_digits), len)))
push(blk.args, :(while pt >= (len+=1) write(out,'0') end))
contains(flags,'#') && push(blk.args, :(write(out, '.')))
end
# print space padding
if padding != nothing && contains(flags,'-')
- push(blk.args, _jl_printf_pad(width-1, padding, ' '))
+ push(blk.args, printf_pad(width-1, padding, ' '))
end
# return arg, expr
:(($x)::Real), ex
end
-function _jl_printf_e(flags::ASCIIString, width::Int, precision::Int, c::Char)
+function printf_e(flags::ASCIIString, width::Int, precision::Int, c::Char)
# print float in scientific form:
# [e]: use 'e' to introduce exponent
# [E]: use 'E' to introduce exponent
@@ -357,13 +361,13 @@ function _jl_printf_e(flags::ASCIIString, width::Int, precision::Int, c::Char)
# ( ): precede non-negative values with " "
# (+): precede non-negative values with "+"
#
- x, ex, blk = _jl_special_handler(flags,width)
+ x, ex, blk = special_handler(flags,width)
# interpret the number
if precision < 0; precision = 6; end
- ndigits = min(precision+1,_jl_buflen-1)
- push(blk.args, :(_jl_ini_dec($x,$ndigits)))
- push(blk.args, :(neg = _jl_neg[1]))
- push(blk.args, :(exp = _jl_point[1]-1))
+ ndigits = min(precision+1,_buflen-1)
+ push(blk.args, :(ini_dec($x,$ndigits)))
+ push(blk.args, :(neg = _neg[1]))
+ push(blk.args, :(exp = _point[1]-1))
expmark = c=='E' ? "E" : "e"
if precision==0 && contains(flags,'#')
expmark = strcat(".",expmark)
@@ -384,7 +388,7 @@ function _jl_printf_e(flags::ASCIIString, width::Int, precision::Int, c::Char)
end
# print space padding
if padding != nothing && !contains(flags,'-') && !contains(flags,'0')
- push(blk.args, _jl_printf_pad(width, padding, ' '))
+ push(blk.args, printf_pad(width, padding, ' '))
end
# print sign
contains(flags,'+') ? push(blk.args, :(write(out, neg?'-':'+'))) :
@@ -392,31 +396,31 @@ function _jl_printf_e(flags::ASCIIString, width::Int, precision::Int, c::Char)
push(blk.args, :(neg && write(out, '-')))
# print zero padding
if padding != nothing && !contains(flags,'-') && contains(flags,'0')
- push(blk.args, _jl_printf_pad(width, padding, '0'))
+ push(blk.args, printf_pad(width, padding, '0'))
end
# print digits
- push(blk.args, :(write(out, _jl_digits[1])))
+ push(blk.args, :(write(out, _digits[1])))
if precision > 0
push(blk.args, :(write(out, '.')))
- push(blk.args, :(write(out, pointer(_jl_digits)+1, $(ndigits-1))))
+ push(blk.args, :(write(out, pointer(_digits)+1, $(ndigits-1))))
if ndigits < precision+1
n = precision+1-ndigits
- push(blk.args, _jl_printf_pad(n, n, '0'))
+ push(blk.args, printf_pad(n, n, '0'))
end
end
for ch in expmark
push(blk.args, :(write(out, $ch)))
end
- push(blk.args, :(_jl_print_exp(out, exp)))
+ push(blk.args, :(print_exp(out, exp)))
# print space padding
if padding != nothing && contains(flags,'-')
- push(blk.args, _jl_printf_pad(width, padding, ' '))
+ push(blk.args, printf_pad(width, padding, ' '))
end
# return arg, expr
:(($x)::Real), ex
end
-function _jl_printf_c(flags::ASCIIString, width::Int, precision::Int, c::Char)
+function printf_c(flags::ASCIIString, width::Int, precision::Int, c::Char)
# print a character:
# [cC]: both the same for us (Unicode)
#
@@ -428,16 +432,16 @@ function _jl_printf_c(flags::ASCIIString, width::Int, precision::Int, c::Char)
blk = expr(:block, :($x = char($x)))
if width > 1 && !contains(flags,'-')
p = contains(flags,'0') ? '0' : ' '
- push(blk.args, _jl_printf_pad(width-1, :($width-charwidth($x)), p))
+ push(blk.args, printf_pad(width-1, :($width-charwidth($x)), p))
end
push(blk.args, :(write(out, $x)))
if width > 1 && contains(flags,'-')
- push(blk.args, _jl_printf_pad(width-1, :($width-charwidth($x)), ' '))
+ push(blk.args, printf_pad(width-1, :($width-charwidth($x)), ' '))
end
:(($x)::Integer), blk
end
-function _jl_printf_s(flags::ASCIIString, width::Int, precision::Int, c::Char)
+function printf_s(flags::ASCIIString, width::Int, precision::Int, c::Char)
# print a string:
# [sS]: both the same for us (Unicode)
#
@@ -454,11 +458,11 @@ function _jl_printf_s(flags::ASCIIString, width::Int, precision::Int, c::Char)
push(blk.args, :($x = sshow($x)))
end
if !contains(flags,'-')
- push(blk.args, _jl_printf_pad(width, :($width-strwidth($x)), ' '))
+ push(blk.args, printf_pad(width, :($width-strwidth($x)), ' '))
end
push(blk.args, :(write(out, $x)))
if contains(flags,'-')
- push(blk.args, _jl_printf_pad(width, :($width-strwidth($x)), ' '))
+ push(blk.args, printf_pad(width, :($width-strwidth($x)), ' '))
end
else
if !contains(flags,'#')
@@ -472,7 +476,7 @@ end
# TODO: faster pointer printing.
-function _jl_printf_p(flags::ASCIIString, width::Int, precision::Int, c::Char)
+function printf_p(flags::ASCIIString, width::Int, precision::Int, c::Char)
# print pointer:
# [p]: the only option
#
@@ -481,13 +485,13 @@ function _jl_printf_p(flags::ASCIIString, width::Int, precision::Int, c::Char)
ptrwidth = WORD_SIZE>>2
width -= ptrwidth+2
if width > 0 && !contains(flags,'-')
- push(blk.args, _jl_printf_pad(width, width, ' '))
+ push(blk.args, printf_pad(width, width, ' '))
end
push(blk.args, :(write(out, '0')))
push(blk.args, :(write(out, 'x')))
push(blk.args, :(write(out, cstring(hex(unsigned($x), $ptrwidth)))))
if width > 0 && contains(flags,'-')
- push(blk.args, _jl_printf_pad(width, width, ' '))
+ push(blk.args, printf_pad(width, width, ' '))
end
:(($x)::Ptr), blk
end
@@ -497,266 +501,266 @@ end
macro handle_zero()
quote
if $esc(:x) == 0
- _jl_point[1] = 1
- _jl_digits[1] = '0'
+ _point[1] = 1
+ _digits[1] = '0'
return
end
end
end
-function _jl_decode_oct(x::Unsigned)
+function decode_oct(x::Unsigned)
@handle_zero
- _jl_point[1] = i = div((sizeof(x)<<3)-leading_zeros(x)+2,3)
+ _point[1] = i = div((sizeof(x)<<3)-leading_zeros(x)+2,3)
while i > 0
- _jl_digits[i] = '0'+(x&0x7)
+ _digits[i] = '0'+(x&0x7)
x >>= 3
i -= 1
end
end
-function _jl_decode_0ct(x::Unsigned)
- _jl_point[1] = i = div((sizeof(x)<<3)-leading_zeros(x)+5,3)
+function decode_0ct(x::Unsigned)
+ _point[1] = i = div((sizeof(x)<<3)-leading_zeros(x)+5,3)
while i > 0
- _jl_digits[i] = '0'+(x&0x7)
+ _digits[i] = '0'+(x&0x7)
x >>= 3
i -= 1
end
end
-function _jl_decode_dec(x::Unsigned)
+function decode_dec(x::Unsigned)
@handle_zero
- _jl_point[1] = i = ndigits0z(x)
+ _point[1] = i = ndigits0z(x)
while i > 0
- _jl_digits[i] = '0'+rem(x,10)
+ _digits[i] = '0'+rem(x,10)
x = div(x,10)
i -= 1
end
end
-function _jl_decode_hex(x::Unsigned, symbols::Array{Uint8,1})
+function decode_hex(x::Unsigned, symbols::Array{Uint8,1})
@handle_zero
- _jl_point[1] = i = (sizeof(x)<<1)-(leading_zeros(x)>>2)
+ _point[1] = i = (sizeof(x)<<1)-(leading_zeros(x)>>2)
while i > 0
- _jl_digits[i] = symbols[(x&0xf)+1]
+ _digits[i] = symbols[(x&0xf)+1]
x >>= 4
i -= 1
end
end
-const _jl_hex_symbols = "0123456789abcdef".data
-const _jl_HEX_symbols = "0123456789ABCDEF".data
+const hex_symbols = "0123456789abcdef".data
+const HEX_symbols = "0123456789ABCDEF".data
-_jl_decode_hex(x::Unsigned) = _jl_decode_hex(x,_jl_hex_symbols)
-_jl_decode_HEX(x::Unsigned) = _jl_decode_hex(x,_jl_HEX_symbols)
+decode_hex(x::Unsigned) = decode_hex(x,hex_symbols)
+decode_HEX(x::Unsigned) = decode_hex(x,HEX_symbols)
### decoding functions directly used by printf generated code ###
-# _jl_int_*(x) => fixed precision, to 0th place, filled out
-# _jl_fix_*(x,n) => fixed precision, to nth place, not filled out
-# _jl_ini_*(x,n) => n initial digits, filled out
-# _jl_sig_*(x,n) => n initial digits, zero-stripped
+# int_*(x) => fixed precision, to 0th place, filled out
+# fix_*(x,n) => fixed precision, to nth place, not filled out
+# ini_*(x,n) => n initial digits, filled out
+# sig_*(x,n) => n initial digits, zero-stripped
# alternate versions:
-# _jl_*_0ct(x,n) => ensure that the first octal digits is zero
-# _jl_*_HEX(x,n) => use uppercase digits for hexadecimal
+# *_0ct(x,n) => ensure that the first octal digits is zero
+# *_HEX(x,n) => use uppercase digits for hexadecimal
## "int" decoding functions ##
#
-# - sets _jl_neg[1]
-# - sets _jl_point[1]
-# - implies _jl_length[1] = _jl_point[1]
+# - sets neg[1]
+# - sets point[1]
+# - implies len[1] = point[1]
#
-_jl_int_oct(x::Unsigned) = (_jl_neg[1]=false; _jl_decode_oct(x))
-_jl_int_0ct(x::Unsigned) = (_jl_neg[1]=false; _jl_decode_0ct(x))
-_jl_int_dec(x::Unsigned) = (_jl_neg[1]=false; _jl_decode_dec(x))
-_jl_int_hex(x::Unsigned) = (_jl_neg[1]=false; _jl_decode_hex(x))
-_jl_int_HEX(x::Unsigned) = (_jl_neg[1]=false; _jl_decode_HEX(x))
+int_oct(x::Unsigned) = (_neg[1]=false; decode_oct(x))
+int_0ct(x::Unsigned) = (_neg[1]=false; decode_0ct(x))
+int_dec(x::Unsigned) = (_neg[1]=false; decode_dec(x))
+int_hex(x::Unsigned) = (_neg[1]=false; decode_hex(x))
+int_HEX(x::Unsigned) = (_neg[1]=false; decode_HEX(x))
macro handle_negative()
quote
if $esc(:x) < 0
- _jl_neg[1] = true
+ _neg[1] = true
$esc(:x) = -($esc(:x))
else
- _jl_neg[1] = false
+ _neg[1] = false
end
end
end
-_jl_int_oct(x::Integer) = (@handle_negative; _jl_decode_oct(unsigned(x)))
-_jl_int_0ct(x::Integer) = (@handle_negative; _jl_decode_0ct(unsigned(x)))
-_jl_int_dec(x::Integer) = (@handle_negative; _jl_decode_dec(unsigned(x)))
-_jl_int_hex(x::Integer) = (@handle_negative; _jl_decode_hex(unsigned(x)))
-_jl_int_HEX(x::Integer) = (@handle_negative; _jl_decode_HEX(unsigned(x)))
+int_oct(x::Integer) = (@handle_negative; decode_oct(unsigned(x)))
+int_0ct(x::Integer) = (@handle_negative; decode_0ct(unsigned(x)))
+int_dec(x::Integer) = (@handle_negative; decode_dec(unsigned(x)))
+int_hex(x::Integer) = (@handle_negative; decode_hex(unsigned(x)))
+int_HEX(x::Integer) = (@handle_negative; decode_HEX(unsigned(x)))
-_jl_int_oct(x::Real) = _jl_int_oct(integer(x)) # TODO: real float decoding.
-_jl_int_0ct(x::Real) = _jl_int_0ct(integer(x)) # TODO: real float decoding.
-_jl_int_dec(x::Real) = _jl_int_dec(float(x))
-_jl_int_hex(x::Real) = _jl_int_hex(integer(x)) # TODO: real float decoding.
-_jl_int_HEX(x::Real) = _jl_int_HEX(integer(x)) # TODO: real float decoding.
+int_oct(x::Real) = int_oct(integer(x)) # TODO: real float decoding.
+int_0ct(x::Real) = int_0ct(integer(x)) # TODO: real float decoding.
+int_dec(x::Real) = int_dec(float(x))
+int_hex(x::Real) = int_hex(integer(x)) # TODO: real float decoding.
+int_HEX(x::Real) = int_HEX(integer(x)) # TODO: real float decoding.
-function _jl_int_dec(x::Float)
+function int_dec(x::Float)
if x == 0.0
- _jl_neg[1] = false
- _jl_point[1] = 1
- _jl_digits[1] = '0'
+ _neg[1] = false
+ _point[1] = 1
+ _digits[1] = '0'
return
end
- @grisu_ccall x GRISU_FIXED 0
- if _jl_length[1] == 0
- _jl_neg[1] = false
- _jl_point[1] = 1
- _jl_digits[1] = '0'
+ @grisu_ccall x Grisu.FIXED 0
+ if _len[1] == 0
+ _neg[1] = false
+ _point[1] = 1
+ _digits[1] = '0'
else
- for i = _jl_length[1]+1:_jl_point[1]
- _jl_digits[i] = '0'
+ for i = _len[1]+1:_point[1]
+ _digits[i] = '0'
end
end
end
## fix decoding functions ##
#
-# - sets _jl_neg[1]
-# - sets _jl_point[1]
-# - sets _jl_length[1]; if less than _jl_point[1], trailing zeros implied
+# - sets neg[1]
+# - sets point[1]
+# - sets len[1]; if less than point[1], trailing zeros implied
#
-_jl_fix_dec(x::Integer, n::Int) = (_jl_int_dec(x); _jl_length[1]=_jl_point[1])
-_jl_fix_dec(x::Real, n::Int) = _jl_fix_dec(float(x),n)
+fix_dec(x::Integer, n::Int) = (int_dec(x); _len[1]=_point[1])
+fix_dec(x::Real, n::Int) = fix_dec(float(x),n)
-function _jl_fix_dec(x::Float, n::Int)
- if n > _jl_buflen-1; n = _jl_buflen-1; end
- @grisu_ccall x GRISU_FIXED n
- if _jl_length[1] == 0
- _jl_neg[1] = false
- _jl_point[1] = 1
- _jl_digits[1] = '0'
+function fix_dec(x::Float, n::Int)
+ if n > _buflen-1; n = _buflen-1; end
+ @grisu_ccall x Grisu.FIXED n
+ if _len[1] == 0
+ _neg[1] = false
+ _point[1] = 1
+ _digits[1] = '0'
end
end
## ini decoding functions ##
#
-# - sets _jl_neg[1]
-# - sets _jl_point[1]
-# - implies _jl_length[1] = n (requested digits)
+# - sets neg[1]
+# - sets point[1]
+# - implies len[1] = n (requested digits)
#
-function _jl_ini_dec(x::Unsigned, n::Int)
+function ini_dec(x::Unsigned, n::Int)
k = ndigits(x)
if k <= n
- _jl_point[1] = k
+ _point[1] = k
for i = k:-1:1
- _jl_digits[i] = '0'+rem(x,10)
+ _digits[i] = '0'+rem(x,10)
x = div(x,10)
end
for i = k+1:n
- _jl_digits[i] = '0'
+ _digits[i] = '0'
end
else
- p = _jl_powers_of_ten[k-n+1]
+ p = powers_of_ten[k-n+1]
r = rem(x,p)
if r >= (p>>1)
x += p
- if x >= _jl_powers_of_ten[k+1]
+ if x >= powers_of_ten[k+1]
p *= 10
k += 1
end
end
- _jl_point[1] = k
+ _point[1] = k
x = div(x,p)
for i = n:-1:1
- _jl_digits[i] = '0'+rem(x,10)
+ _digits[i] = '0'+rem(x,10)
x = div(x,10)
end
end
end
-_jl_ini_dec(x::Integer, n::Int) = (@handle_negative; _jl_ini_dec(unsigned(x),n))
-_jl_ini_dec(x::Real, n::Int) = _jl_ini_dec(float(x),n)
+ini_dec(x::Integer, n::Int) = (@handle_negative; ini_dec(unsigned(x),n))
+ini_dec(x::Real, n::Int) = ini_dec(float(x),n)
-function _jl_ini_dec(x::Float, n::Int)
+function ini_dec(x::Float, n::Int)
if x == 0.0
- _jl_point[1] = 1
- _jl_neg[1] = signbit(x)
- ccall(:memset, Void, (Ptr{Uint8}, Int32, Int), _jl_digits, '0', n)
+ _point[1] = 1
+ _neg[1] = signbit(x)
+ ccall(:memset, Void, (Ptr{Uint8}, Int32, Int), _digits, '0', n)
else
- @grisu_ccall x GRISU_PRECISION n
+ @grisu_ccall x Grisu.PRECISION n
end
end
## sig decoding functions ##
#
-# - sets _jl_neg[1]
-# - sets _jl_point[1]
-# - sets _jl_length[1]
+# - sets neg[1]
+# - sets point[1]
+# - sets len[1]
#
-function _jl_sig_dec(x::Unsigned, n::Int)
+function sig_dec(x::Unsigned, n::Int)
if x == 0
- _jl_neg[1] = false
- _jl_point[1] = 1
- _jl_length[1] = 1
- _jl_digits[1] = '0'
+ _neg[1] = false
+ _point[1] = 1
+ _len[1] = 1
+ _digits[1] = '0'
return
end
k = ndigits0z(x)
if k <= n
- _jl_point[1] = k
+ _point[1] = k
for i = k:-1:1
- _jl_digits[i] = '0'+rem(x,10)
+ _digits[i] = '0'+rem(x,10)
x = div(x,10)
end
- while _jl_digits[k] == '0'
+ while _digits[k] == '0'
k -= 1
end
- _jl_length[1] = k
+ _len[1] = k
else
- p = _jl_powers_of_ten[k-n+1]
+ p = powers_of_ten[k-n+1]
r = rem(x,p)
if r >= (p>>1)
x += p
- if x >= _jl_powers_of_ten[k+1]
+ if x >= powers_of_ten[k+1]
p *= 10
k += 1
end
end
- _jl_point[1] = k
+ _point[1] = k
x = div(x,p)
for i = n:-1:1
- _jl_digits[i] = '0'+rem(x,10)
+ _digits[i] = '0'+rem(x,10)
x = div(x,10)
end
- while _jl_digits[n] == '0'
+ while _digits[n] == '0'
n -= 1
end
- _jl_length[1] = n
+ _len[1] = n
end
end
-_jl_sig_dec(x::Integer, n::Int) = (@handle_negative; _jl_sig_dec(unsigned(x),n))
-_jl_sig_dec(x::Real, n::Int) = _jl_sig_dec(float(x),n)
+sig_dec(x::Integer, n::Int) = (@handle_negative; sig_dec(unsigned(x),n))
+sig_dec(x::Real, n::Int) = sig_dec(float(x),n)
-function _jl_sig_dec(x::Float, n::Int)
- @grisu_ccall x GRISU_PRECISION n
+function sig_dec(x::Float, n::Int)
+ @grisu_ccall x Grisu.PRECISION n
if x == 0.0; return; end
- while _jl_digits[n] == '0'
+ while _digits[n] == '0'
n -= 1
end
- _jl_length[1] = n
+ _len[1] = n
end
### external printf interface ###
function f_str_f(f)
- args, blk = _jl_printf_gen(f)
+ args, blk = printf_gen(f)
:(($expr(:tuple, args))->($blk))
end
macro f_str(f); f_str_f(f); end
macro printf(f, exps...)
- args, blk = _jl_printf_gen(f)
+ args, blk = printf_gen(f)
if length(args) != length(exps)
error("printf: wrong number of arguments")
end
@@ -771,3 +775,7 @@ fprintf(s::IOStream, f::Function, args...) = f(s, args...)
fprintf(s::IOStream, fmt::String, args...) = fprintf(s, eval(Base,f_str_f(fmt)), args...)
printf(f::Union(Function,String), args...) = fprintf(OUTPUT_STREAM, f, args...)
sprintf(f::Union(Function,String), args...) = sprint(fprintf, f, args...)
+
+export @f_str, @printf, fprintf, printf, sprintf
+end # module
+import Base.Printf.*
View
2 base/start_image.jl
@@ -7,7 +7,7 @@ _jl_lib = ccall(:jl_load_dynamic_library,Ptr{Void},(Ptr{None},),C_NULL)
# Essential libraries
libpcre = dlopen("libpcre")
-_jl_libgrisu = dlopen("libgrisu")
+libgrisu = dlopen("libgrisu")
_jl_libm = dlopen("libm")
_jl_libfdm = dlopen("libfdm")
_jl_librandom = dlopen("librandom");
View
2 base/stat.jl
@@ -28,7 +28,7 @@ Stat(buf::Vector{Uint8}) = Stat(
ccall(:jl_stat_ctime, Float64, (Ptr{Uint8},), buf),
)
-show(io::IO, st::Stat) = printf(f"Stat(mode=%06o,size=%i)\n", st.mode, st.size)
+show(io::IO, st::Stat) = println("Stat(mode=$(oct(st.mode,6)), size=$(st.size))")
# stat & lstat functions
View
4 base/sysimg.jl
@@ -1,8 +1,8 @@
module Base
export
- # Module
- Base, PCRE,
+ # Modules
+ Base,Grisu,Printf,PCRE,
# Types
AbstractMatrix,AbstractVector,Array,Associative,CharString,Chars,Cmd,Cmds,
Colon,Complex,Complex128,Complex64,ComplexPair,DArray,Dict,Dims,EachLine,
View
2 test/perf/perf.jl
@@ -137,7 +137,7 @@ function printfd(n)
for i = 1:n
# fprintf(f, f"%d %d\n", i, i)
# f"%d %d\n"(f,i,i)
- @printf "%d %d\n" f i i
+ # @printf "%d %d\n" f i i
end
close(f)
end

0 comments on commit 0213f82

Please sign in to comment.
Something went wrong with that request. Please try again.