forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprintf.j
773 lines (721 loc) · 22 KB
/
printf.j
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
### printf formatter generation ###
function _jl_printf_gen(s::String)
args = {:(out::IOStream)}
blk = expr(:block, :(local neg, pt, len, exp))
for x in _jl_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
arg, ex = f(x...)
push(args, arg)
push(blk.args, ex)
end
end
push(blk.args, :nothing)
return args, blk
end
### printf format string parsing ###
function _jl_printf_parse(s::String)
# parse format string in to stings and format tuples
list = {}
i = j = start(s)
while !done(s,j)
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)
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")
push(list, conversion == '%' ? "%" : (flags,width,precision,conversion))
i = j = k
else
j = k
end
end
isempty(s[i:]) || push(list, utf8(unescape_string(s[i:])))
# coalesce adjacent strings
i = 1
while i < length(list)
if isa(list[i],String)
for j = i+1:length(list)
if !isa(list[j],String)
j -= 1
break
end
list[i] *= list[j]
end
del(list,i+1:j)
end
i += 1
end
return list
end
## parse a single printf specifier ##
# printf specifiers:
# % # start
# (\d+\$)? # arg (not supported)
# [\-\+#0' ]* # flags
# (\d+)? # width
# (\.\d*)? # precision
# (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) :
error("invalid printf format string: ", show_to_string(s))
function _jl_printf_parse1(s::String, k::Integer)
j = k
width = 0
precision = -1
c, k = _jl_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)
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)
end
# parse precision
if c == '.'
c, k = _jl_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)
end
end
end
# parse length modifer (ignored)
if c == 'h' || c == 'l'
prev = c
c, k = _jl_next_or_die(s,k)
if c == prev
c, k = _jl_next_or_die(s,k)
end
elseif contains("Ljqtz",c)
c, k = _jl_next_or_die(s,k)
end
# validate conversion
if !contains("diouxXDOUeEfFgGaAcCsSpn", c)
error("invalid printf format string: ", show_to_string(s))
end
# TODO: warn about silly flag/conversion combinations
flags, width, precision, c, k
end
### printf formatter generation ###
function _jl_special_handler(flags::ASCIIString, width::Int)
x = gensym()
blk = expr(:block)
pad = contains(flags,'-') ? rpad : lpad
pos = contains(flags,'+') ? "+" :
contains(flags,' ') ? " " : ""
abn = quote
isnan($x) ? $(cstring(pad("NaN", width))) :
$x < 0 ? $(cstring(pad("-Inf", width))) :
$(cstring(pad("$(pos)Inf", width)))
end
ex = :(isfinite($x) ? $blk : write(out, $abn))
x, ex, blk
end
function _jl_printf_pad(m::Int, n, c::Char)
if m <= 1
:($n > 0 && write(out,$c))
else
i = gensym()
quote
$i = $n
while $i > 0
write(out,$c)
$i -= 1
end
end
end
end
function _jl_print_fixed(out, precision)
pdigits = pointer(_jl_digits)
ndigits = _jl_length[1]
pt = _jl_point[1]
if pt <= 0
# 0.0dddd0
write(out, '0')
write(out, '.')
precision += pt
while pt < 0
write(out, '0')
pt += 1
end
write(out, pdigits, ndigits)
precision -= ndigits
elseif ndigits <= pt
# dddd000.000000
write(out, pdigits, ndigits)
while ndigits < pt
write(out, '0')
ndigits += 1
end
write(out, '.')
else # 0 < pt < ndigits
# dd.dd0000
ndigits -= pt
write(out, pdigits, pt)
write(out, '.')
write(out, pdigits+pt, ndigits)
precision -= ndigits
end
while precision > 0
write(out, '0')
precision -= 1
end
end
function _jl_print_exp(out, exp)
write(out, exp < 0 ? '-' : '+')
exp = abs(exp)
d = div(exp,100)
d > 0 && write(out, char('0'+d))
exp = rem(exp,100)
write(out, char('0'+div(exp,10)))
write(out, char('0'+rem(exp,10)))
end
function _jl_printf_d(flags::ASCIIString, width::Int, precision::Int, c::Char)
# print integer:
# [dDiu]: print decimal digits
# [o]: print octal digits
# [x]: print hex digits, lowercase
# [X]: print hex digits, uppercase
#
# flags:
# (#): prefix hex with 0x/0X; octal leads with 0
# (0): pad left with zeros
# (-): left justify
# ( ): precede non-negative values with " "
# (+): precede non-negative values with "+"
#
x, ex, blk = _jl_special_handler(flags,width)
# interpret the number
prefix = ""
if lc(c)=='o'
f = contains(flags,'#') ? :_jl_int_0ct : :_jl_int_oct
push(blk.args, :(($f)($x)))
elseif c=='x'
if contains(flags,'#'); prefix = "0x"; end
push(blk.args, :(_jl_int_hex($x)))
elseif c=='X'
if contains(flags,'#'); prefix = "0X"; end
push(blk.args, :(_jl_int_HEX($x)))
else
push(blk.args, :(_jl_int_dec($x)))
end
push(blk.args, :(neg = _jl_neg[1]))
push(blk.args, :(pt = _jl_point[1]))
# calculate padding
width -= strlen(prefix)
space_pad = width > max(1,precision) && contains(flags,'-') ||
precision < 0 && width > 1 && !contains(flags,'0') ||
precision >= 0 && width > precision
padding = nothing
if precision < 1; precision = 1; end
if space_pad
if contains(flags,'+') || contains(flags,' ')
width -= 1
if width > precision
padding = :($width-(pt > $precision ? pt : $precision))
end
else
if width > precision
padding = :($width-neg-(pt > $precision ? pt : $precision))
end
end
end
# print space padding
if padding != nothing && !contains(flags,'-')
push(blk.args, _jl_printf_pad(width-precision, padding, ' '))
end
# print sign
contains(flags,'+') ? push(blk.args, :(write(out, neg?'-':'+'))) :
contains(flags,' ') ? push(blk.args, :(write(out, neg?'-':' '))) :
push(blk.args, :(neg && write(out, '-')))
# print prefix
for ch in prefix
push(blk.args, :(write(out, $ch)))
end
# print zero padding & leading zeros
if space_pad && precision > 1
push(blk.args, _jl_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'))
end
# print integer
push(blk.args, :(write(out, pointer(_jl_digits), pt)))
# print padding
if padding != nothing && contains(flags,'-')
push(blk.args, _jl_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)
# print to fixed trailing precision
# [fF]: the only choice
#
# flags
# (#): always print a decimal point
# (0): pad left with zeros
# (-): left justify
# ( ): precede non-negative values with " "
# (+): precede non-negative values with "+"
#
x, ex, blk = _jl_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]))
# calculate padding
padding = nothing
if precision > 0 || contains(flags,'#')
width -= precision+1
end
if contains(flags,'+') || contains(flags,' ')
width -= 1
if width > 1
padding = :($width-pt)
end
else
if width > 1
padding = :($width-pt-neg)
end
end
# print space padding
if padding != nothing && !contains(flags,'-') && !contains(flags,'0')
push(blk.args, _jl_printf_pad(width-1, padding, ' '))
end
# print sign
contains(flags,'+') ? push(blk.args, :(write(out, neg?'-':'+'))) :
contains(flags,' ') ? push(blk.args, :(write(out, neg?'-':' '))) :
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'))
end
# print digits
if precision > 0
push(blk.args, :(_jl_print_fixed(out,$precision)))
else
push(blk.args, :(write(out, pointer(_jl_digits), pt)))
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, ' '))
end
# return arg, expr
:(($x)::Real), ex
end
function _jl_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
#
# flags:
# (#): always print a decimal point
# (0): pad left with zeros
# (-): left justify
# ( ): precede non-negative values with " "
# (+): precede non-negative values with "+"
#
x, ex, blk = _jl_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))
expmark = c=='E' ? "E" : "e"
if precision==0 && contains(flags,'#')
expmark = strcat(".",expmark)
end
# calculate padding
padding = nothing
width -= precision+strlen(expmark)+(precision>0)+4
# 4 = leading + expsign + 2 exp digits
if contains(flags,'+') || contains(flags,' ')
width -= 1 # for the sign indicator
if width > 0
padding = :($width-((exp<=-100)|(100<=exp)))
end
else
if width > 0
padding = :($width-((exp<=-100)|(100<=exp))-neg)
end
end
# print space padding
if padding != nothing && !contains(flags,'-') && !contains(flags,'0')
push(blk.args, _jl_printf_pad(width, padding, ' '))
end
# print sign
contains(flags,'+') ? push(blk.args, :(write(out, neg?'-':'+'))) :
contains(flags,' ') ? push(blk.args, :(write(out, neg?'-':' '))) :
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'))
end
# print digits
push(blk.args, :(write(out, _jl_digits[1])))
if precision > 0
push(blk.args, :(write(out, '.')))
push(blk.args, :(write(out, pointer(_jl_digits)+1, $(ndigits-1))))
if ndigits < precision+1
n = precision+1-ndigits
push(blk.args, _jl_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)))
# print space padding
if padding != nothing && contains(flags,'-')
push(blk.args, _jl_printf_pad(width, padding, ' '))
end
# return arg, expr
:(($x)::Real), ex
end
function _jl_printf_c(flags::ASCIIString, width::Int, precision::Int, c::Char)
# print a character:
# [cC]: both the same for us (Unicode)
#
# flags:
# (0): pad left with zeros
# (-): left justify
#
x = gensym()
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))
end
push(blk.args, :(write(out, $x)))
if width > 1 && contains(flags,'-')
push(blk.args, _jl_printf_pad(width-1, :($width-charwidth($x)), ' '))
end
:(($x)::Integer), blk
end
function _jl_printf_s(flags::ASCIIString, width::Int, precision::Int, c::Char)
# print a string:
# [sS]: both the same for us (Unicode)
#
# flags:
# (0): pad left with zeros
# (-): left justify
#
x = gensym()
blk = expr(:block)
if width > 0
if !contains(flags,'#')
push(blk.args, :($x = string($x)))
else
push(blk.args, :($x = show_to_string($x)))
end
if !contains(flags,'-')
push(blk.args, _jl_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)), ' '))
end
else
if !contains(flags,'#')
push(blk.args, :(print($x)))
else
push(blk.args, :(show($x)))
end
end
:(($x)::Any), blk
end
# TODO: faster pointer printing.
function _jl_printf_p(flags::ASCIIString, width::Int, precision::Int, c::Char)
# print pointer:
# [p]: the only option
#
x = gensym()
blk = expr(:block)
ptrwidth = WORD_SIZE>>2
width -= ptrwidth+2
if width > 0 && !contains(flags,'-')
push(blk.args, _jl_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, ' '))
end
:(($x)::Ptr), blk
end
### core unsigned integer decoding functions ###
macro handle_zero()
quote
if x == 0
_jl_point[1] = 1
_jl_digits[1] = '0'
return
end
end
end
function _jl_decode_oct(x::Unsigned)
@handle_zero
_jl_point[1] = i = div((sizeof(x)<<3)-leading_zeros(x)+2,3)
while i > 0
_jl_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)
while i > 0
_jl_digits[i] = '0'+(x&0x7)
x >>= 3
i -= 1
end
end
function _jl_decode_dec(x::Unsigned)
@handle_zero
_jl_point[1] = i = ndigits0z(x)
while i > 0
_jl_digits[i] = '0'+rem(x,10)
x = div(x,10)
i -= 1
end
end
function _jl_decode_hex(x::Unsigned, symbols::Array{Uint8,1})
@handle_zero
_jl_point[1] = i = (sizeof(x)<<1)-(leading_zeros(x)>>2)
while i > 0
_jl_digits[i] = symbols[(x&0xf)+1]
x >>= 4
i -= 1
end
end
const _jl_hex_symbols = "0123456789abcdef".data
const _jl_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)
### 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
# alternate versions:
# _jl_*_0ct(x,n) => ensure that the first octal digits is zero
# _jl_*_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]
#
_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))
macro handle_negative()
quote
if x < 0
_jl_neg[1] = true
x = -x
else
_jl_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)))
_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.
function _jl_int_dec(x::Float)
if x == 0.0
_jl_neg[1] = false
_jl_point[1] = 1
_jl_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'
else
for i = _jl_length[1]+1:_jl_point[1]
_jl_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
#
_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)
function _jl_fix_dec(x::Float, n::Int)
if n > 17; n = 17; end
@grisu_ccall x GRISU_FIXED n
if _jl_length[1] == 0
_jl_neg[1] = false
_jl_point[1] = 1
_jl_digits[1] = '0'
end
end
## ini decoding functions ##
#
# - sets _jl_neg[1]
# - sets _jl_point[1]
# - implies _jl_length[1] = n (requested digits)
#
function _jl_ini_dec(x::Unsigned, n::Int)
k = ndigits(x)
if k <= n
_jl_point[1] = k
for i = k:-1:1
_jl_digits[i] = '0'+rem(x,10)
x = div(x,10)
end
for i = k+1:n
_jl_digits[i] = '0'
end
else
p = _jl_powers_of_ten[k-n+1]
r = rem(x,p)
if r >= (p>>1)
x += p
if x >= _jl_powers_of_ten[k+1]
p *= 10
k += 1
end
end
_jl_point[1] = k
x = div(x,p)
for i = n:-1:1
_jl_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)
function _jl_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),
pointer(_jl_digits), int32('0'), n)
else
@grisu_ccall x GRISU_PRECISION n
end
end
## sig decoding functions ##
#
# - sets _jl_neg[1]
# - sets _jl_point[1]
# - sets _jl_length[1]
#
function _jl_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'
return
end
k = ndigits0z(x)
if k <= n
_jl_point[1] = k
for i = k:-1:1
_jl_digits[i] = '0'+rem(x,10)
x = div(x,10)
end
while _jl_digits[k] == '0'
k -= 1
end
_jl_length[1] = k
else
p = _jl_powers_of_ten[k-n+1]
r = rem(x,p)
if r >= (p>>1)
x += p
if x >= _jl_powers_of_ten[k+1]
p *= 10
k += 1
end
end
_jl_point[1] = k
x = div(x,p)
for i = n:-1:1
_jl_digits[i] = '0'+rem(x,10)
x = div(x,10)
end
while _jl_digits[n] == '0'
n -= 1
end
_jl_length[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)
function _jl_sig_dec(x::Float, n::Int)
@grisu_ccall x GRISU_PRECISION n
if x == 0.0; return; end
while _jl_digits[n] == '0'
n -= 1
end
_jl_length[1] = n
end
### external printf interface ###
function f_str_f(f)
args, blk = _jl_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)
if length(args) != length(exps)
error("printf: wrong number of arguments")
end
for i = length(args):-1:1
arg = args[i].args[1]
unshift(blk.args, :($arg = $(exps[i])))
end
blk
end
fprintf(s::IOStream, f::Function, args...) = f(s, args...)
fprintf(s::IOStream, fmt::String, args...) = fprintf(s, eval(f_str_f(fmt)), args...)
printf(f::Union(Function,String), args...) = fprintf(current_output_stream(), f, args...)
sprintf(f::Union(Function,String), args...) = print_to_string(printf, f, args...)