Skip to content

Commit

Permalink
Use unsafe version of takestring more rarely
Browse files Browse the repository at this point in the history
Specifically, do not use it when a value of an unknown type prints to the
buffer that is being mutated, since any user may define printing of these values
to store references to the buffer somewhere else.
  • Loading branch information
jakobnissen committed Jul 31, 2024
1 parent be1e354 commit 8a7667f
Show file tree
Hide file tree
Showing 12 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion base/indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function throw_promote_shape_mismatch(a::Tuple, b::Union{Nothing,Tuple}, i = not
if i nothing
print(msg, ", mismatch at dim ", i)
end
throw(DimensionMismatch(unsafe_takestring!(msg)))
throw(DimensionMismatch(takestring!(msg)))
end

function promote_shape(a::Tuple{Int,}, b::Tuple{Int,})
Expand Down
4 changes: 2 additions & 2 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ julia> rm("my_file.txt")
"""
readuntil(filename::AbstractString, delim; kw...) = open(io->readuntil(io, delim; kw...), convert(String, filename)::String)
readuntil(stream::IO, delim::UInt8; kw...) = _unsafe_take!(copyuntil(IOBuffer(sizehint=70), stream, delim; kw...))
readuntil(stream::IO, delim::Union{AbstractChar, AbstractString}; kw...) = unsafe_takestring!(copyuntil(IOBuffer(sizehint=70), stream, delim; kw...))
readuntil(stream::IO, delim::Union{AbstractChar, AbstractString}; kw...) = takestring!(copyuntil(IOBuffer(sizehint=70), stream, delim; kw...))
readuntil(stream::IO, delim::T; keep::Bool=false) where T = _copyuntil(Vector{T}(), stream, delim, keep)


Expand Down Expand Up @@ -616,7 +616,7 @@ Logan
"""
readline(filename::AbstractString; keep::Bool=false) =
open(io -> readline(io; keep), filename)
readline(s::IO=stdin; keep::Bool=false) = unsafe_takestring!(copyline(IOBuffer(sizehint=70), s; keep))
readline(s::IO=stdin; keep::Bool=false) = takestring!(copyline(IOBuffer(sizehint=70), s; keep))

"""
copyline(out::IO, io::IO=stdin; keep::Bool=false)
Expand Down
2 changes: 1 addition & 1 deletion base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ function readuntil_string(s::IOStream, delim::UInt8, keep::Bool)
end
readuntil(s::IOStream, delim::AbstractChar; keep::Bool=false) =
isascii(delim) ? readuntil_string(s, delim % UInt8, keep) :
unsafe_takestring!(copyuntil(IOBuffer(sizehint=70), s, delim; keep))
takestring!(copyuntil(IOBuffer(sizehint=70), s, delim; keep))

function readline(s::IOStream; keep::Bool=false)
@_lock_ios s ccall(:jl_readuntil, Ref{String}, (Ptr{Cvoid}, UInt8, UInt8, UInt8), s.ios, '\n', 1, keep ? 0 : 2)
Expand Down
2 changes: 1 addition & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3158,7 +3158,7 @@ summary(io::IO, x) = print(io, typeof(x))
function summary(x)
io = IOBuffer()
summary(io, x)
unsafe_takestring!(io)
takestring!(io)
end

## `summary` for AbstractArrays
Expand Down
4 changes: 2 additions & 2 deletions base/strings/annotated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ function annotatedstring(xs...)
print(s, x)
end
end
str = unsafe_takestring!(buf)
str = takestring!(buf)
AnnotatedString(str, annotations)
end

Expand Down Expand Up @@ -457,7 +457,7 @@ function annotated_chartransform(f::Function, str::AnnotatedString, state=nothin
stop_offset = last(offsets[findlast(<=(stop) first, offsets)::Int])
push!(annots, ((start + start_offset):(stop + stop_offset), value))
end
AnnotatedString(unsafe_takestring!(outstr), annots)
AnnotatedString(takestring!(outstr), annots)
end

## AnnotatedIOBuffer
Expand Down
2 changes: 1 addition & 1 deletion base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ function filter(f, s::AbstractString)
for c in s
f(c) && write(out, c)
end
unsafe_takestring!(out)
takestring!(out)
end

## string first and last ##
Expand Down
8 changes: 4 additions & 4 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function sprint(f::Function, args...; context=nothing, sizehint::Integer=0)
else
f(s, args...)
end
unsafe_takestring!(s)
takestring!(s)
end

function _str_sizehint(x)
Expand Down Expand Up @@ -147,7 +147,7 @@ function print_to_string(xs...)
for x in xs
print(s, x)
end
unsafe_takestring!(s)
takestring!(s)
end

function string_with_env(env, xs...)
Expand All @@ -164,7 +164,7 @@ function string_with_env(env, xs...)
for x in xs
print(env_io, x)
end
unsafe_takestring!(s)
takestring!(s)
end

"""
Expand Down Expand Up @@ -756,7 +756,7 @@ function unindent(str::AbstractString, indent::Int; tabwidth=8)
print(buf, ' ')
end
end
unsafe_takestring!(buf)
takestring!(buf)
end

function String(a::AbstractVector{Char})
Expand Down
2 changes: 1 addition & 1 deletion base/strings/unicode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ function titlecase(s::AbstractString; wordsep::Function = !isletter, strict::Boo
end
c0 = c
end
return unsafe_takestring!(b)
return takestring!(b)
end

# TODO: improve performance characteristics, room for a ~10x improvement.
Expand Down
2 changes: 1 addition & 1 deletion base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1112,5 +1112,5 @@ function Base.rest(s::AbstractString, st...)
for c in Iterators.rest(s, st...)
print(io, c)
end
return unsafe_takestring!(io)
return takestring!(io)
end
2 changes: 1 addition & 1 deletion base/task.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function show_task_exception(io::IO, t::Task; indent = true)
else
show_exception_stack(IOContext(b, io), stack)
end
str = unsafe_takestring!(b)
str = takestring!(b)
if indent
str = replace(str, "\n" => "\n ")
end
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Base64/src/encode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,6 @@ function base64encode(f::Function, args...; context=nothing)
f(IOContext(b, context), args...)
end
close(b)
return unsafe_takestring!(s)
return takestring!(s)
end
base64encode(args...; context=nothing) = base64encode(write, args...; context=context)
2 changes: 1 addition & 1 deletion stdlib/Markdown/src/parse/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ function readuntil(stream::IO, delimiter; newlines = false, match = nothing)
while !eof(stream)
if startswith(stream, delimiter)
if count == 0
return unsafe_takestring!(buffer)
return takestring!(buffer)
else
count -= 1
write(buffer, delimiter)
Expand Down

0 comments on commit 8a7667f

Please sign in to comment.