Skip to content

Commit 450defe

Browse files
authored
add Printf support (#201)
* add Printf support * fix test for older Julia versions
1 parent c513799 commit 450defe

File tree

4 files changed

+51
-5
lines changed

4 files changed

+51
-5
lines changed

Project.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
88
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
99

1010
[weakdeps]
11+
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
1112
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1213
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
1314
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1415

1516
[extensions]
17+
IntervalSetsPrintfExt = "Printf"
1618
IntervalSetsRandomExt = "Random"
1719
IntervalSetsRecipesBaseExt = "RecipesBase"
1820
IntervalSetsStatisticsExt = "Statistics"
@@ -22,6 +24,7 @@ Aqua = "0.8"
2224
Dates = "1"
2325
OffsetArrays = "1"
2426
Plots = "1"
27+
Printf = "1"
2528
Random = "1"
2629
RecipesBase = "1"
2730
Statistics = "1"
@@ -34,11 +37,12 @@ Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
3437
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
3538
OffsetArrays = "6fe1bfb0-de20-5000-8ca7-80f57d26f881"
3639
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
40+
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
3741
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
3842
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
3943
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
4044
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
4145
Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
4246

4347
[targets]
44-
test = ["Aqua", "Dates", "Test", "Plots", "Random", "RecipesBase", "OffsetArrays", "Statistics", "Unitful"]
48+
test = ["Aqua", "Dates", "Test", "Plots", "Printf", "Random", "RecipesBase", "OffsetArrays", "Statistics", "Unitful"]

ext/IntervalSetsPrintfExt.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module IntervalSetsPrintfExt
2+
3+
using Printf
4+
using IntervalSets
5+
using IntervalSets: _show_suffix
6+
7+
INTERVAL_SEPARATOR = " .. "
8+
9+
Printf.plength(f::Printf.Spec{<:Printf.Ints}, x::Interval) =
10+
Printf.plength(f, leftendpoint(x)) + Printf.plength(f, rightendpoint(x)) +
11+
ncodeunits(INTERVAL_SEPARATOR) + ncodeunits(_show_suffix(x))
12+
13+
# separate methods for disambiguation
14+
Printf.fmt(buf, pos, arg::Interval, spec::Printf.Spec{<:Printf.Floats}) = _fmt(buf, pos, arg, spec)
15+
Printf.fmt(buf, pos, arg::Interval, spec::Printf.Spec{<:Printf.Ints}) = _fmt(buf, pos, arg, spec)
16+
17+
function _fmt(buf, pos, arg, spec)
18+
pos = Printf.fmt(buf, pos, leftendpoint(arg), spec)
19+
buf[pos:pos+ncodeunits(INTERVAL_SEPARATOR)-1] .= codeunits(INTERVAL_SEPARATOR)
20+
pos += ncodeunits(INTERVAL_SEPARATOR)
21+
pos = Printf.fmt(buf, pos, rightendpoint(arg), spec)
22+
23+
suf = _show_suffix(arg)
24+
buf[pos:pos+ncodeunits(suf)-1] .= codeunits(suf)
25+
pos += ncodeunits(suf)
26+
27+
return pos
28+
end
29+
30+
end

src/interval.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,12 @@ julia> 3 ± 2
156156
±(x, y) = ClosedInterval(x - y, x + y)
157157
±(x::CartesianIndex, y::CartesianIndex) = ClosedInterval(x-y, x+y)
158158

159-
show(io::IO, I::ClosedInterval) = print(io, leftendpoint(I), " .. ", rightendpoint(I))
160-
show(io::IO, I::OpenInterval) = print(io, leftendpoint(I), " .. ", rightendpoint(I), " (open)")
161-
show(io::IO, I::Interval{:open,:closed}) = print(io, leftendpoint(I), " .. ", rightendpoint(I), " (open-closed)")
162-
show(io::IO, I::Interval{:closed,:open}) = print(io, leftendpoint(I), " .. ", rightendpoint(I), " (closed-open)")
159+
_show_suffix(::ClosedInterval) = ""
160+
_show_suffix(::OpenInterval) = " (open)"
161+
_show_suffix(::Interval{:open,:closed}) = " (open-closed)"
162+
_show_suffix(::Interval{:closed,:open}) = " (closed-open)"
163+
164+
show(io::IO, I::Interval) = print(io, leftendpoint(I), " .. ", rightendpoint(I), _show_suffix(I))
163165

164166
# The following are not typestable for mixed endpoint types
165167
_left_intersect_type(::Type{Val{:open}}, ::Type{Val{L2}}, a1, a2) where L2 = a1 < a2 ? (a2,L2) : (a1,:open)

test/runtests.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Statistics: mean
66
using Random
77
using Unitful
88
using Plots
9+
using Printf
910

1011
import IntervalSets: Domain, endpoints, closedendpoints, TypedEndpointsInterval
1112

@@ -443,6 +444,15 @@ struct IncompleteInterval <: AbstractInterval{Int} end
443444
@test_throws MethodError 2 in I
444445
end
445446

447+
VERSION v"1.9" && @testset "stringify" begin
448+
@test string(0..1) == "0 .. 1"
449+
@test string(iv"[0,1)") == "0 .. 1 (closed-open)"
450+
@test @sprintf("%d", 0..1) == "0 .. 1"
451+
@test @sprintf("%.2f", 0..1) == "0.00 .. 1.00"
452+
@test @sprintf("%.2f", iv"[0,1)") == "0.00 .. 1.00 (closed-open)"
453+
@test @sprintf("%.2f", 0u"m"..1u"m") == "0.00 m .. 1.00 m"
454+
end
455+
446456
include("base_methods.jl")
447457
include("setoperations.jl")
448458
include("findall.jl")

0 commit comments

Comments
 (0)