1- VERSION >= v " 0.4.0-dev+6641 " && __precompile__ ()
1+ __precompile__ ()
22
33module Showoff
44
55using Compat
6- if VERSION >= v " 0.6.0-dev.1015"
7- import Base. Iterators: drop
8- end
6+ import Compat. Iterators: drop
97
108export showoff
119
1715
1816
1917function grisu (v:: AbstractFloat , mode, requested_digits)
20- if VERSION < v " 0.4-dev"
21- if isa (v, Float32) && mode == Base. Grisu. SHORTEST
22- mode = Base. Grisu. SHORTEST_SINGLE
23- end
24- @grisu_ccall v mode requested_digits
25- return Base. Grisu. LEN[1 ], Base. Grisu. POINT[1 ], Base. Grisu. NEG, Base. Grisu. DIGITS
26- elseif VERSION < v " 0.5.0-dev+2094"
27- return Base. Grisu. grisu (v, mode, requested_digits)
28- else
29- return tuple (Base. Grisu. grisu (v, mode, requested_digits)... , Base. Grisu. DIGITS)
30- end
18+ return tuple (Base. Grisu. grisu (v, mode, requested_digits)... , Base. Grisu. DIGITS)
3119end
3220
3321
3422# Fallback
3523function showoff (xs:: AbstractArray , style= :none )
36- result = Array (AbstractString, length (xs))
24+ result = Vector {String} ( length (xs))
3725 buf = IOBuffer ()
3826 for (i, x) in enumerate (xs)
3927 show (buf, x)
4836
4937function concrete_minimum (xs)
5038 if done (xs, start (xs))
51- error ( " argument must not be empty" )
39+ throw ( ArgumentError ( " argument must not be empty" ) )
5240 end
5341
5442 x_min = first (xs)
7058
7159function concrete_maximum (xs)
7260 if done (xs, start (xs))
73- error ( " argument must not be empty" )
61+ throw ( ArgumentError ( " argument must not be empty" ) )
7462 end
7563
7664 x_max = first (xs)
10391
10492function scientific_precision_heuristic {T <: AbstractFloat} (xs:: AbstractArray{T} )
10593 ys = [x == 0.0 ? 0.0 : x / 10.0 ^ floor (log10 (abs (x)))
106- for x in filter (isfinite, xs )]
94+ for x in xs if isfinite (x )]
10795 return plain_precision_heuristic (ys) + 1
10896end
10997
@@ -115,7 +103,7 @@ function showoff{T <: AbstractFloat}(xs::AbstractArray{T}, style=:auto)
115103 x_max = Float64 (Float32 (x_max))
116104
117105 if ! isfinite (x_min) || ! isfinite (x_max)
118- error ( " At least one finite value must be provided to formatter." )
106+ throw ( ArgumentError ( " At least one finite value must be provided to formatter." ) )
119107 end
120108
121109 if style == :auto
@@ -128,17 +116,17 @@ function showoff{T <: AbstractFloat}(xs::AbstractArray{T}, style=:auto)
128116
129117 if style == :plain
130118 precision = plain_precision_heuristic (xs)
131- return AbstractString [format_fixed (x, precision) for x in xs]
119+ return String [format_fixed (x, precision) for x in xs]
132120 elseif style == :scientific
133121 precision = scientific_precision_heuristic (xs)
134- return AbstractString [format_fixed_scientific (x, precision, false )
122+ return String [format_fixed_scientific (x, precision, false )
135123 for x in xs]
136124 elseif style == :engineering
137125 precision = scientific_precision_heuristic (xs)
138- return AbstractString [format_fixed_scientific (x, precision, true )
126+ return String [format_fixed_scientific (x, precision, true )
139127 for x in xs]
140128 else
141- error ( " $(style) is not a recongnized number format" )
129+ throw ( ArgumentError ( " $(style) is not a recongnized number format" ) )
142130 end
143131end
144132
@@ -274,91 +262,88 @@ function format_fixed_scientific(x::AbstractFloat, precision::Integer,
274262end
275263
276264
277-
278- if VERSION >= v " 0.4-dev"
279- function showoff {T <: (Union{Date, DateTime})} (ds:: AbstractArray{T} , style= :none )
280- years = Set ()
281- months = Set ()
282- days = Set ()
283- hours = Set ()
284- minutes = Set ()
285- seconds = Set ()
286- for d in ds
287- push! (years, Dates. year (d))
288- push! (months, Dates. month (d))
289- push! (days, Dates. day (d))
290- push! (hours, Dates. hour (d))
291- push! (minutes, Dates. minute (d))
292- push! (seconds, Dates. second (d))
293- end
294- all_same_year = length (years) == 1
295- all_one_month = length (months) == 1 && 1 in months
296- all_one_day = length (days) == 1 && 1 in days
297- all_zero_hour = length (hours) == 1 && 0 in hours
298- all_zero_minute = length (minutes) == 1 && 0 in minutes
299- all_zero_seconds = length (minutes) == 1 && 0 in minutes
300- all_zero_milliseconds = length (minutes) == 1 && 0 in minutes
301-
302- # first label format
303- label_months = false
304- label_days = false
305- f1 = " u d, yyyy"
306- f2 = " "
307- if ! all_zero_seconds
308- f2 = " HH:MM:SS.sss"
309- elseif ! all_zero_seconds
310- f2 = " HH:MM:SS"
311- elseif ! all_zero_hour || ! all_zero_minute
312- f2 = " HH:MM"
313- else
314- if ! all_one_day
315- first_label_format = " u d yyyy"
316- elseif ! all_one_month
317- first_label_format = " u yyyy"
318- elseif ! all_one_day
319- first_label_format = " yyyy"
320- end
321- end
322- if f2 != " "
323- first_label_format = string (f1, " " , f2)
324- else
325- first_label_format = f1
265+ function showoff {T <: (Union{Date, DateTime})} (ds:: AbstractArray{T} , style= :none )
266+ years = Set ()
267+ months = Set ()
268+ days = Set ()
269+ hours = Set ()
270+ minutes = Set ()
271+ seconds = Set ()
272+ for d in ds
273+ push! (years, Dates. year (d))
274+ push! (months, Dates. month (d))
275+ push! (days, Dates. day (d))
276+ push! (hours, Dates. hour (d))
277+ push! (minutes, Dates. minute (d))
278+ push! (seconds, Dates. second (d))
279+ end
280+ all_same_year = length (years) == 1
281+ all_one_month = length (months) == 1 && 1 in months
282+ all_one_day = length (days) == 1 && 1 in days
283+ all_zero_hour = length (hours) == 1 && 0 in hours
284+ all_zero_minute = length (minutes) == 1 && 0 in minutes
285+ all_zero_seconds = length (minutes) == 1 && 0 in minutes
286+ all_zero_milliseconds = length (minutes) == 1 && 0 in minutes
287+
288+ # first label format
289+ label_months = false
290+ label_days = false
291+ f1 = " u d, yyyy"
292+ f2 = " "
293+ if ! all_zero_seconds
294+ f2 = " HH:MM:SS.sss"
295+ elseif ! all_zero_seconds
296+ f2 = " HH:MM:SS"
297+ elseif ! all_zero_hour || ! all_zero_minute
298+ f2 = " HH:MM"
299+ else
300+ if ! all_one_day
301+ first_label_format = " u d yyyy"
302+ elseif ! all_one_month
303+ first_label_format = " u yyyy"
304+ elseif ! all_one_day
305+ first_label_format = " yyyy"
326306 end
307+ end
308+ if f2 != " "
309+ first_label_format = string (f1, " " , f2)
310+ else
311+ first_label_format = f1
312+ end
327313
328- labels = Array (AbstractString, length (ds))
329- labels[1 ] = Dates. format (ds[1 ], first_label_format)
330- d_last = ds[1 ]
331- for (i, d) in enumerate (ds[2 : end ])
332- if Dates. year (d) != Dates. year (d_last)
333- if all_one_day && all_one_month
334- f1 = " yyyy"
335- elseif all_one_day && ! all_one_month
336- f1 = " u yyyy"
337- else
338- f1 = " u d, yyyy"
339- end
340- elseif Dates. month (d) != Dates. month (d_last)
341- f1 = all_one_day ? " u" : " u d"
342- elseif Dates. day (d) != Dates. day (d_last)
343- f1 = " d"
344- else
345- f1 = " "
346- end
347-
348- if f2 != " "
349- f = string (f1, " " , f2)
350- elseif f1 != " "
351- f = f1
314+ labels = Vector {String} (length (ds))
315+ labels[1 ] = Dates. format (ds[1 ], first_label_format)
316+ d_last = ds[1 ]
317+ for (i, d) in enumerate (ds[2 : end ])
318+ if Dates. year (d) != Dates. year (d_last)
319+ if all_one_day && all_one_month
320+ f1 = " yyyy"
321+ elseif all_one_day && ! all_one_month
322+ f1 = " u yyyy"
352323 else
353- f = first_label_format
324+ f1 = " u d, yyyy "
354325 end
326+ elseif Dates. month (d) != Dates. month (d_last)
327+ f1 = all_one_day ? " u" : " u d"
328+ elseif Dates. day (d) != Dates. day (d_last)
329+ f1 = " d"
330+ else
331+ f1 = " "
332+ end
355333
356- labels[i+ 1 ] = Dates. format (d, f)
357- d_last = d
334+ if f2 != " "
335+ f = string (f1, " " , f2)
336+ elseif f1 != " "
337+ f = f1
338+ else
339+ f = first_label_format
358340 end
359341
360- return labels
342+ labels[i+ 1 ] = Dates. format (d, f)
343+ d_last = d
361344 end
345+
346+ return labels
362347end
363348
364349
0 commit comments