Skip to content

Commit 3db1d19

Browse files
authored
add missing clamp function for IOBuffer (JuliaLang#55424)
The `clamp` function was defined in Base.Math, but required to be in Base now, so move it to intfuncs with other similar functions Fixes JuliaLang#55279
2 parents ac9558c + fc6047b commit 3db1d19

File tree

4 files changed

+105
-104
lines changed

4 files changed

+105
-104
lines changed

base/intfuncs.jl

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,3 +1237,102 @@ function binomial(x::Number, k::Integer)
12371237
# and instead divide each term by i, to avoid spurious overflow.
12381238
return prod(i -> (x-(i-1))/i, OneTo(k), init=oneunit(x)/one(k))
12391239
end
1240+
1241+
"""
1242+
clamp(x, lo, hi)
1243+
1244+
Return `x` if `lo <= x <= hi`. If `x > hi`, return `hi`. If `x < lo`, return `lo`. Arguments
1245+
are promoted to a common type.
1246+
1247+
See also [`clamp!`](@ref), [`min`](@ref), [`max`](@ref).
1248+
1249+
!!! compat "Julia 1.3"
1250+
`missing` as the first argument requires at least Julia 1.3.
1251+
1252+
# Examples
1253+
```jldoctest
1254+
julia> clamp.([pi, 1.0, big(10)], 2.0, 9.0)
1255+
3-element Vector{BigFloat}:
1256+
3.141592653589793238462643383279502884197169399375105820974944592307816406286198
1257+
2.0
1258+
9.0
1259+
1260+
julia> clamp.([11, 8, 5], 10, 6) # an example where lo > hi
1261+
3-element Vector{Int64}:
1262+
6
1263+
6
1264+
10
1265+
```
1266+
"""
1267+
function clamp(x::X, lo::L, hi::H) where {X,L,H}
1268+
T = promote_type(X, L, H)
1269+
return (x > hi) ? convert(T, hi) : (x < lo) ? convert(T, lo) : convert(T, x)
1270+
end
1271+
1272+
"""
1273+
clamp(x, T)::T
1274+
1275+
Clamp `x` between `typemin(T)` and `typemax(T)` and convert the result to type `T`.
1276+
1277+
See also [`trunc`](@ref).
1278+
1279+
# Examples
1280+
```jldoctest
1281+
julia> clamp(200, Int8)
1282+
127
1283+
1284+
julia> clamp(-200, Int8)
1285+
-128
1286+
1287+
julia> trunc(Int, 4pi^2)
1288+
39
1289+
```
1290+
"""
1291+
function clamp(x, ::Type{T}) where {T<:Integer}
1292+
# delegating to clamp(x, typemin(T), typemax(T)) would promote types
1293+
# this way, we avoid unnecessary conversions
1294+
# think of, e.g., clamp(big(2) ^ 200, Int16)
1295+
lo = typemin(T)
1296+
hi = typemax(T)
1297+
return (x > hi) ? hi : (x < lo) ? lo : convert(T, x)
1298+
end
1299+
1300+
1301+
"""
1302+
clamp!(array::AbstractArray, lo, hi)
1303+
1304+
Restrict values in `array` to the specified range, in-place.
1305+
See also [`clamp`](@ref).
1306+
1307+
!!! compat "Julia 1.3"
1308+
`missing` entries in `array` require at least Julia 1.3.
1309+
1310+
# Examples
1311+
```jldoctest
1312+
julia> row = collect(-4:4)';
1313+
1314+
julia> clamp!(row, 0, Inf)
1315+
1×9 adjoint(::Vector{Int64}) with eltype Int64:
1316+
0 0 0 0 0 1 2 3 4
1317+
1318+
julia> clamp.((-4:4)', 0, Inf)
1319+
1×9 Matrix{Float64}:
1320+
0.0 0.0 0.0 0.0 0.0 1.0 2.0 3.0 4.0
1321+
```
1322+
"""
1323+
function clamp!(x::AbstractArray, lo, hi)
1324+
@inbounds for i in eachindex(x)
1325+
x[i] = clamp(x[i], lo, hi)
1326+
end
1327+
x
1328+
end
1329+
1330+
"""
1331+
clamp(x::Integer, r::AbstractUnitRange)
1332+
1333+
Clamp `x` to lie within range `r`.
1334+
1335+
!!! compat "Julia 1.6"
1336+
This method requires at least Julia 1.6.
1337+
"""
1338+
clamp(x::Integer, r::AbstractUnitRange{<:Integer}) = clamp(x, first(r), last(r))

base/io.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,8 +543,8 @@ julia> rm("my_file.txt")
543543
```
544544
"""
545545
readuntil(filename::AbstractString, delim; kw...) = open(io->readuntil(io, delim; kw...), convert(String, filename)::String)
546-
readuntil(stream::IO, delim::UInt8; kw...) = _unsafe_take!(copyuntil(IOBuffer(sizehint=70), stream, delim; kw...))
547-
readuntil(stream::IO, delim::Union{AbstractChar, AbstractString}; kw...) = String(_unsafe_take!(copyuntil(IOBuffer(sizehint=70), stream, delim; kw...)))
546+
readuntil(stream::IO, delim::UInt8; kw...) = _unsafe_take!(copyuntil(IOBuffer(sizehint=16), stream, delim; kw...))
547+
readuntil(stream::IO, delim::Union{AbstractChar, AbstractString}; kw...) = String(_unsafe_take!(copyuntil(IOBuffer(sizehint=16), stream, delim; kw...)))
548548
readuntil(stream::IO, delim::T; keep::Bool=false) where T = _copyuntil(Vector{T}(), stream, delim, keep)
549549

550550

@@ -617,7 +617,7 @@ Logan
617617
readline(filename::AbstractString; keep::Bool=false) =
618618
open(io -> readline(io; keep), filename)
619619
readline(s::IO=stdin; keep::Bool=false) =
620-
String(_unsafe_take!(copyline(IOBuffer(sizehint=70), s; keep)))
620+
String(_unsafe_take!(copyline(IOBuffer(sizehint=16), s; keep)))
621621

622622
"""
623623
copyline(out::IO, io::IO=stdin; keep::Bool=false)
@@ -1111,7 +1111,7 @@ function copyuntil(out::IO, io::IO, target::AbstractString; keep::Bool=false)
11111111
end
11121112

11131113
function readuntil(io::IO, target::AbstractVector{T}; keep::Bool=false) where T
1114-
out = (T === UInt8 ? resize!(StringVector(70), 0) : Vector{T}())
1114+
out = (T === UInt8 ? resize!(StringVector(16), 0) : Vector{T}())
11151115
readuntil_vector!(io, target, keep, out)
11161116
return out
11171117
end

base/math.jl

Lines changed: 1 addition & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import .Base: log, exp, sin, cos, tan, sinh, cosh, tanh, asin,
2323
using .Base: sign_mask, exponent_mask, exponent_one,
2424
exponent_half, uinttype, significand_mask,
2525
significand_bits, exponent_bits, exponent_bias,
26-
exponent_max, exponent_raw_max
26+
exponent_max, exponent_raw_max, clamp, clamp!
2727

2828
using Core.Intrinsics: sqrt_llvm
2929

@@ -69,104 +69,6 @@ end
6969
return Txy, T(xy-Txy)
7070
end
7171

72-
"""
73-
clamp(x, lo, hi)
74-
75-
Return `x` if `lo <= x <= hi`. If `x > hi`, return `hi`. If `x < lo`, return `lo`. Arguments
76-
are promoted to a common type.
77-
78-
See also [`clamp!`](@ref), [`min`](@ref), [`max`](@ref).
79-
80-
!!! compat "Julia 1.3"
81-
`missing` as the first argument requires at least Julia 1.3.
82-
83-
# Examples
84-
```jldoctest
85-
julia> clamp.([pi, 1.0, big(10)], 2.0, 9.0)
86-
3-element Vector{BigFloat}:
87-
3.141592653589793238462643383279502884197169399375105820974944592307816406286198
88-
2.0
89-
9.0
90-
91-
julia> clamp.([11, 8, 5], 10, 6) # an example where lo > hi
92-
3-element Vector{Int64}:
93-
6
94-
6
95-
10
96-
```
97-
"""
98-
function clamp(x::X, lo::L, hi::H) where {X,L,H}
99-
T = promote_type(X, L, H)
100-
return (x > hi) ? convert(T, hi) : (x < lo) ? convert(T, lo) : convert(T, x)
101-
end
102-
103-
"""
104-
clamp(x, T)::T
105-
106-
Clamp `x` between `typemin(T)` and `typemax(T)` and convert the result to type `T`.
107-
108-
See also [`trunc`](@ref).
109-
110-
# Examples
111-
```jldoctest
112-
julia> clamp(200, Int8)
113-
127
114-
115-
julia> clamp(-200, Int8)
116-
-128
117-
118-
julia> trunc(Int, 4pi^2)
119-
39
120-
```
121-
"""
122-
function clamp(x, ::Type{T}) where {T<:Integer}
123-
# delegating to clamp(x, typemin(T), typemax(T)) would promote types
124-
# this way, we avoid unnecessary conversions
125-
# think of, e.g., clamp(big(2) ^ 200, Int16)
126-
lo = typemin(T)
127-
hi = typemax(T)
128-
return (x > hi) ? hi : (x < lo) ? lo : convert(T, x)
129-
end
130-
131-
132-
"""
133-
clamp!(array::AbstractArray, lo, hi)
134-
135-
Restrict values in `array` to the specified range, in-place.
136-
See also [`clamp`](@ref).
137-
138-
!!! compat "Julia 1.3"
139-
`missing` entries in `array` require at least Julia 1.3.
140-
141-
# Examples
142-
```jldoctest
143-
julia> row = collect(-4:4)';
144-
145-
julia> clamp!(row, 0, Inf)
146-
1×9 adjoint(::Vector{Int64}) with eltype Int64:
147-
0 0 0 0 0 1 2 3 4
148-
149-
julia> clamp.((-4:4)', 0, Inf)
150-
1×9 Matrix{Float64}:
151-
0.0 0.0 0.0 0.0 0.0 1.0 2.0 3.0 4.0
152-
```
153-
"""
154-
function clamp!(x::AbstractArray, lo, hi)
155-
@inbounds for i in eachindex(x)
156-
x[i] = clamp(x[i], lo, hi)
157-
end
158-
x
159-
end
160-
161-
"""
162-
clamp(x::Integer, r::AbstractUnitRange)
163-
164-
Clamp `x` to lie within range `r`.
165-
166-
!!! compat "Julia 1.6"
167-
This method requires at least Julia 1.6.
168-
"""
169-
clamp(x::Integer, r::AbstractUnitRange{<:Integer}) = clamp(x, first(r), last(r))
17072

17173
"""
17274
evalpoly(x, p)
@@ -1690,7 +1592,6 @@ end
16901592

16911593
exp2(x::AbstractFloat) = 2^x
16921594
exp10(x::AbstractFloat) = 10^x
1693-
clamp(::Missing, lo, hi) = missing
16941595
fourthroot(::Missing) = missing
16951596

16961597
end # module

base/missing.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ min(::Any, ::Missing) = missing
135135
max(::Missing, ::Missing) = missing
136136
max(::Missing, ::Any) = missing
137137
max(::Any, ::Missing) = missing
138+
clamp(::Missing, lo, hi) = missing
138139

139140
missing_conversion_msg(@nospecialize T) =
140141
LazyString("cannot convert a missing value to type ", T, ": use Union{", T, ", Missing} instead")

0 commit comments

Comments
 (0)