Skip to content

Commit e814159

Browse files
committed
Use takestring! in Base and docs
This commit switches the usage of the pattern `String(take!(::IOBuffer))` to use the new `takestring!` or `unsafe_takestring!` functions across Base, and in doc- strings. Not all occurrences in e.g. tests are switched over, as this would consistute a lot of code churn for no real purpose.
1 parent a7c4296 commit e814159

File tree

32 files changed

+77
-77
lines changed

32 files changed

+77
-77
lines changed

Compiler/src/ssair/show.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ function compute_ir_line_annotations(code::IRCode)
329329
loc_method = string(" "^printing_depth, loc_method)
330330
last_stack = stack
331331
end
332-
push!(loc_annotations, String(take!(buf)))
332+
push!(loc_annotations, takestring!(buf))
333333
push!(loc_lineno, (lineno != 0 && lineno != last_lineno) ? string(lineno) : "")
334334
push!(loc_methods, loc_method)
335335
(lineno != 0) && (last_lineno = lineno)

base/errorshow.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ function showerror(io::IO, ex::MethodError)
307307
iob = IOContext(buf, io) # for type abbreviation as in #49795; some, like `convert(T, x)`, should not abbreviate
308308
show_signature_function(iob, Core.Typeof(f))
309309
show_tuple_as_call(iob, :function, arg_types; hasfirst=false, kwargs = isempty(kwargs) ? nothing : kwargs)
310-
str = String(take!(buf))
310+
str = takestring!(buf)
311311
str = type_limited_string_from_context(io, str)
312312
print(io, str)
313313
end
@@ -600,7 +600,7 @@ function show_method_candidates(io::IO, ex::MethodError, kwargs=[])
600600
m = parentmodule_before_main(method)
601601
modulecolor = get!(() -> popfirst!(STACKTRACE_MODULECOLORS), STACKTRACE_FIXEDCOLORS, m)
602602
print_module_path_file(iob, m, string(file), line; modulecolor, digit_align_width = 3)
603-
push!(lines, String(take!(buf)))
603+
push!(lines, takestring!(buf))
604604
push!(line_score, -(right_matches * 2 + (length(arg_types_param) < 2 ? 1 : 0)))
605605
end
606606
end

base/filesystem.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ import .Base:
141141
bytesavailable, position, read, read!, readbytes!, readavailable, seek, seekend, show,
142142
skip, stat, unsafe_read, unsafe_write, write, transcode, uv_error, _uv_error,
143143
setup_stdio, rawhandle, OS_HANDLE, INVALID_OS_HANDLE, windowserror, filesize,
144-
isexecutable, isreadable, iswritable, MutableDenseArrayType, truncate
144+
isexecutable, isreadable, iswritable, MutableDenseArrayType, truncate, unsafe_takestring!
145145

146146
import .Base.RefValue
147147

base/indices.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function throw_promote_shape_mismatch(a::Tuple, b::Union{Nothing,Tuple}, i = not
132132
if i nothing
133133
print(msg, ", mismatch at dim ", i)
134134
end
135-
throw(DimensionMismatch(String(take!(msg))))
135+
throw(DimensionMismatch(takestring!(msg)))
136136
end
137137

138138
function promote_shape(a::Tuple{Int,}, b::Tuple{Int,})

base/int.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ macro big_str(s::String)
720720
is_prev_dot = (c == '.')
721721
end
722722
print(bf, s[end])
723-
s = String(take!(bf))
723+
s = unsafe_takestring!(bf)
724724
end
725725
n = tryparse(BigInt, s)
726726
n === nothing || return n

base/io.jl

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -277,13 +277,13 @@ julia> io = IOBuffer();
277277
julia> write(io, "JuliaLang is a GitHub organization.", " It has many members.")
278278
56
279279
280-
julia> String(take!(io))
280+
julia> takestring!(io)
281281
"JuliaLang is a GitHub organization. It has many members."
282282
283283
julia> write(io, "Sometimes those members") + write(io, " write documentation.")
284284
44
285285
286-
julia> String(take!(io))
286+
julia> takestring!(io)
287287
"Sometimes those members write documentation."
288288
```
289289
User-defined plain-data types without `write` methods can be written when wrapped in a `Ref`:
@@ -544,7 +544,7 @@ julia> rm("my_file.txt")
544544
"""
545545
readuntil(filename::AbstractString, delim; kw...) = open(io->readuntil(io, delim; kw...), convert(String, filename)::String)
546546
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...)))
547+
readuntil(stream::IO, delim::Union{AbstractChar, AbstractString}; kw...) = takestring!(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

@@ -566,10 +566,10 @@ Similar to [`readuntil`](@ref), which returns a `String`; in contrast,
566566
```jldoctest
567567
julia> write("my_file.txt", "JuliaLang is a GitHub organization.\\nIt has many members.\\n");
568568
569-
julia> String(take!(copyuntil(IOBuffer(), "my_file.txt", 'L')))
569+
julia> takestring!(copyuntil(IOBuffer(), "my_file.txt", 'L'))
570570
"Julia"
571571
572-
julia> String(take!(copyuntil(IOBuffer(), "my_file.txt", '.', keep = true)))
572+
julia> takestring!(copyuntil(IOBuffer(), "my_file.txt", '.', keep = true))
573573
"JuliaLang is a GitHub organization."
574574
575575
julia> rm("my_file.txt")
@@ -616,8 +616,7 @@ Logan
616616
"""
617617
readline(filename::AbstractString; keep::Bool=false) =
618618
open(io -> readline(io; keep), filename)
619-
readline(s::IO=stdin; keep::Bool=false) =
620-
String(_unsafe_take!(copyline(IOBuffer(sizehint=16), s; keep)))
619+
readline(s::IO=stdin; keep::Bool=false) = takestring!(copyline(IOBuffer(sizehint=16), s; keep))
621620

622621
"""
623622
copyline(out::IO, io::IO=stdin; keep::Bool=false)
@@ -642,10 +641,10 @@ See also [`copyuntil`](@ref) for reading until more general delimiters.
642641
```jldoctest
643642
julia> write("my_file.txt", "JuliaLang is a GitHub organization.\\nIt has many members.\\n");
644643
645-
julia> String(take!(copyline(IOBuffer(), "my_file.txt")))
644+
julia> takestring!(copyline(IOBuffer(), "my_file.txt"))
646645
"JuliaLang is a GitHub organization."
647646
648-
julia> String(take!(copyline(IOBuffer(), "my_file.txt", keep=true)))
647+
julia> takestring!(copyline(IOBuffer(), "my_file.txt", keep=true))
649648
"JuliaLang is a GitHub organization.\\n"
650649
651650
julia> rm("my_file.txt")
@@ -1290,7 +1289,7 @@ function iterate(r::Iterators.Reverse{<:EachLine}, state)
12901289
buf.size = _stripnewline(r.itr.keep, buf.size, buf.data)
12911290
empty!(chunks) # will cause next iteration to terminate
12921291
seekend(r.itr.stream) # reposition to end of stream for isdone
1293-
s = String(_unsafe_take!(buf))
1292+
s = unsafe_takestring!(buf)
12941293
else
12951294
# extract the string from chunks[ichunk][inewline+1] to chunks[jchunk][jnewline]
12961295
if ichunk == jchunk # common case: current and previous newline in same chunk
@@ -1307,7 +1306,7 @@ function iterate(r::Iterators.Reverse{<:EachLine}, state)
13071306
end
13081307
write(buf, view(chunks[jchunk], 1:jnewline))
13091308
buf.size = _stripnewline(r.itr.keep, buf.size, buf.data)
1310-
s = String(_unsafe_take!(buf))
1309+
s = unsafe_takestring!(buf)
13111310

13121311
# overwrite obsolete chunks (ichunk+1:jchunk)
13131312
i = jchunk

base/iobuffer.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ julia> io = IOBuffer();
198198
julia> write(io, "JuliaLang is a GitHub organization.", " It has many members.")
199199
56
200200
201-
julia> String(take!(io))
201+
julia> takestring!(io)
202202
"JuliaLang is a GitHub organization. It has many members."
203203
204204
julia> io = IOBuffer(b"JuliaLang is a GitHub organization.")
@@ -216,7 +216,7 @@ IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=fa
216216
julia> write(io, "JuliaLang is a GitHub organization.")
217217
34
218218
219-
julia> String(take!(io))
219+
julia> takestring!(io)
220220
"JuliaLang is a GitHub organization"
221221
222222
julia> length(read(IOBuffer(b"data", read=true, truncate=false)))

base/iostream.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ julia> write(io, "JuliaLang is a GitHub organization.")
111111
julia> truncate(io, 15)
112112
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, append=false, size=15, maxsize=Inf, ptr=16, mark=-1)
113113
114-
julia> String(take!(io))
114+
julia> takestring!(io)
115115
"JuliaLang is a "
116116
117117
julia> io = IOBuffer();
@@ -120,7 +120,7 @@ julia> write(io, "JuliaLang is a GitHub organization.");
120120
121121
julia> truncate(io, 40);
122122
123-
julia> String(take!(io))
123+
julia> takestring!(io)
124124
"JuliaLang is a GitHub organization.\\0\\0\\0\\0\\0"
125125
```
126126
"""
@@ -469,7 +469,7 @@ function readuntil_string(s::IOStream, delim::UInt8, keep::Bool)
469469
end
470470
readuntil(s::IOStream, delim::AbstractChar; keep::Bool=false) =
471471
isascii(delim) ? readuntil_string(s, delim % UInt8, keep) :
472-
String(_unsafe_take!(copyuntil(IOBuffer(sizehint=70), s, delim; keep)))
472+
takestring!(copyuntil(IOBuffer(sizehint=70), s, delim; keep))
473473

474474
function readline(s::IOStream; keep::Bool=false)
475475
@_lock_ios s ccall(:jl_readuntil, Ref{String}, (Ptr{Cvoid}, UInt8, UInt8, UInt8), s.ios, '\n', 1, keep ? 0 : 2)

base/logging/ConsoleLogger.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function handle_message(logger::ConsoleLogger, level::LogLevel, message, _module
147147
for (key, val) in kwargs
148148
key === :maxlog && continue
149149
showvalue(valio, val)
150-
vallines = split(String(take!(valbuf)), '\n')
150+
vallines = split(takestring!(valbuf), '\n')
151151
if length(vallines) == 1
152152
push!(msglines, (indent=2, msg=SubString("$key = $(vallines[1])")))
153153
else

base/pkgid.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function binpack(pkg::PkgId)
3232
uuid = pkg.uuid
3333
write(io, uuid === nothing ? UInt128(0) : UInt128(uuid))
3434
write(io, pkg.name)
35-
return String(take!(io))
35+
return unsafe_takestring!(io)
3636
end
3737

3838
function binunpack(s::String)

base/show.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -390,12 +390,12 @@ julia> io = IOBuffer();
390390
391391
julia> printstyled(IOContext(io, :color => true), "string", color=:red)
392392
393-
julia> String(take!(io))
393+
julia> takestring!(io)
394394
"\\e[31mstring\\e[39m"
395395
396396
julia> printstyled(io, "string", color=:red)
397397
398-
julia> String(take!(io))
398+
julia> takestring!(io)
399399
"string"
400400
```
401401
@@ -2649,7 +2649,7 @@ function show_tuple_as_call(out::IO, name::Symbol, sig::Type;
26492649
end
26502650
print_within_stacktrace(io, ")", bold=true)
26512651
show_method_params(io, tv)
2652-
str = String(take!(buf))
2652+
str = takestring!(buf)
26532653
str = type_limited_string_from_context(out, str)
26542654
print(out, str)
26552655
nothing
@@ -2758,7 +2758,7 @@ function type_depth_limit(str::String, n::Int; maxdepth = nothing)
27582758
end
27592759
prev = di
27602760
end
2761-
return String(take!(output))
2761+
return unsafe_takestring!(output)
27622762
end
27632763

27642764
function print_type_bicolor(io, type; kwargs...)
@@ -3193,7 +3193,7 @@ summary(io::IO, x) = print(io, typeof(x))
31933193
function summary(x)
31943194
io = IOBuffer()
31953195
summary(io, x)
3196-
String(take!(io))
3196+
takestring!(io)
31973197
end
31983198

31993199
## `summary` for AbstractArrays

base/stat.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ function filemode_string(mode)
320320
end
321321
complete && write(str, "-")
322322
end
323-
return String(take!(str))
323+
return unsafe_takestring!(str)
324324
end
325325

326326
"""

base/strings/annotated.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ function annotatedstring(xs...)
272272
print(s, x)
273273
end
274274
end
275-
str = String(take!(buf))
275+
str = takestring!(buf)
276276
AnnotatedString(str, annotations)
277277
end
278278

@@ -457,7 +457,7 @@ function annotated_chartransform(f::Function, str::AnnotatedString, state=nothin
457457
stop_offset = last(offsets[findlast(<=(stop) first, offsets)::Int])
458458
push!(annots, setindex(annot, (start + start_offset):(stop + stop_offset), :region))
459459
end
460-
AnnotatedString(String(take!(outstr)), annots)
460+
AnnotatedString(takestring!(outstr), annots)
461461
end
462462

463463
struct RegionIterator{S <: AbstractString}

base/strings/basic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ function filter(f, s::AbstractString)
678678
for c in s
679679
f(c) && write(out, c)
680680
end
681-
String(_unsafe_take!(out))
681+
takestring!(out)
682682
end
683683

684684
## string first and last ##

base/strings/io.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ julia> io = IOBuffer();
2525
2626
julia> print(io, "Hello", ' ', :World!)
2727
28-
julia> String(take!(io))
28+
julia> takestring!(io)
2929
"Hello World!"
3030
```
3131
"""
@@ -70,7 +70,7 @@ julia> io = IOBuffer();
7070
7171
julia> println(io, "Hello", ',', " world.")
7272
73-
julia> String(take!(io))
73+
julia> takestring!(io)
7474
"Hello, world.\\n"
7575
```
7676
"""
@@ -112,7 +112,7 @@ function sprint(f::Function, args...; context=nothing, sizehint::Integer=0)
112112
else
113113
f(s, args...)
114114
end
115-
String(_unsafe_take!(s))
115+
takestring!(s)
116116
end
117117

118118
function _str_sizehint(x)
@@ -146,7 +146,7 @@ function print_to_string(xs...)
146146
for x in xs
147147
print(s, x)
148148
end
149-
String(_unsafe_take!(s))
149+
takestring!(s)
150150
end
151151
setfield!(typeof(print_to_string).name.mt, :max_args, 10, :monotonic)
152152

@@ -164,7 +164,7 @@ function string_with_env(env, xs...)
164164
for x in xs
165165
print(env_io, x)
166166
end
167-
String(_unsafe_take!(s))
167+
takestring!(s)
168168
end
169169

170170
"""
@@ -294,10 +294,10 @@ Create a read-only `IOBuffer` on the data underlying the given string.
294294
```jldoctest
295295
julia> io = IOBuffer("Haho");
296296
297-
julia> String(take!(io))
297+
julia> takestring!(io)
298298
"Haho"
299299
300-
julia> String(take!(io))
300+
julia> takestring!(io)
301301
"Haho"
302302
```
303303
"""
@@ -774,7 +774,7 @@ function unindent(str::AbstractString, indent::Int; tabwidth=8)
774774
print(buf, ' ')
775775
end
776776
end
777-
String(take!(buf))
777+
takestring!(buf)
778778
end
779779

780780
function String(a::AbstractVector{Char})

base/strings/unicode.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ function titlecase(s::AbstractString; wordsep::Function = !isletter, strict::Boo
702702
end
703703
c0 = c
704704
end
705-
return String(take!(b))
705+
return takestring!(b)
706706
end
707707

708708
# TODO: improve performance characteristics, room for a ~10x improvement.

base/strings/util.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ function _replace_(str, pat_repl::NTuple{N, Pair}, count::Int) where N
10401040
return String(str)
10411041
end
10421042
out = IOBuffer(sizehint=floor(Int, 1.2sizeof(str)))
1043-
return String(take!(_replace_finish(out, str, count, e1, patterns, replaces, rs)))
1043+
return takestring!(_replace_finish(out, str, count, e1, patterns, replaces, rs))
10441044
end
10451045

10461046
"""
@@ -1272,5 +1272,5 @@ function Base.rest(s::AbstractString, st...)
12721272
for c in Iterators.rest(s, st...)
12731273
print(io, c)
12741274
end
1275-
return String(take!(io))
1275+
return takestring!(io)
12761276
end

base/task.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function show_task_exception(io::IO, t::Task; indent = true)
9595
else
9696
show_exception_stack(IOContext(b, io), stack)
9797
end
98-
str = String(take!(b))
98+
str = takestring!(b)
9999
if indent
100100
str = replace(str, "\n" => "\n ")
101101
end

base/toml_parser.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ function point_to_line(str::AbstractString, a::Int, b::Int, context)
315315
c == '\n' && break
316316
print(io1, c)
317317
end
318-
return String(take!(io1.io)), String(take!(io2.io))
318+
return takestring!(io1.io), takestring!(io2.io)
319319
end
320320

321321
function Base.showerror(io::IO, err::ParserError)

0 commit comments

Comments
 (0)