Skip to content

Commit 550f5d3

Browse files
authored
Merge pull request #19088 from JuliaLang/nl/takebuf
Simplify takebuf() API
2 parents 548465b + 4b81b0c commit 550f5d3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+186
-228
lines changed

base/LineEdit.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ function refresh_multi_line(terminal::UnixTerminal, args...; kwargs...)
267267
termbuf = TerminalBuffer(outbuf)
268268
ret = refresh_multi_line(termbuf, terminal, args...;kwargs...)
269269
# Output the entire refresh at once
270-
write(terminal, takebuf_array(outbuf))
270+
write(terminal, take!(outbuf))
271271
flush(terminal)
272272
return ret
273273
end
@@ -668,7 +668,7 @@ function normalize_key(key::AbstractString)
668668
write(buf, c)
669669
end
670670
end
671-
return takebuf_string(buf)
671+
return String(take!(buf))
672672
end
673673

674674
function normalize_keys(keymap::Dict)
@@ -1501,7 +1501,7 @@ end
15011501
activate(m::ModalInterface, s::MIState, termbuf, term::TextTerminal) =
15021502
activate(s.current_mode, s, termbuf, term)
15031503

1504-
commit_changes(t::UnixTerminal, termbuf) = write(t, takebuf_array(termbuf.out_stream))
1504+
commit_changes(t::UnixTerminal, termbuf) = write(t, take!(termbuf.out_stream))
15051505
function transition(f::Function, s::MIState, mode)
15061506
if mode === :abort
15071507
s.aborted = true

base/REPL.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ function respond(f, repl, main; pass_empty = false)
646646
if !ok
647647
return transition(s, :abort)
648648
end
649-
line = takebuf_string(buf)
649+
line = String(take!(buf))
650650
if !isempty(line) || pass_empty
651651
reset(repl)
652652
val, bt = send_to_backend(f(line), backend(repl))
@@ -840,7 +840,7 @@ function setup_interface(repl::LineEditREPL; hascolor = repl.hascolor, extra_rep
840840
return
841841
end
842842
edit_insert(sbuffer, input)
843-
input = takebuf_string(sbuffer)
843+
input = String(take!(sbuffer))
844844
oldpos = start(input)
845845
firstline = true
846846
isprompt_paste = false

base/REPLCompletions.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ function complete_methods(ex_org::Expr)
331331
# Check if the method's type signature intersects the input types
332332
if typeintersect(Tuple{method.sig.parameters[1 : min(na, end)]...}, t_in) != Union{}
333333
show(io, method, kwtype=kwtype)
334-
push!(out, takebuf_string(io))
334+
push!(out, String(take!(io)))
335335
end
336336
end
337337
return out

base/base64.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ function base64encode(f::Function, args...)
176176
b = Base64EncodePipe(s)
177177
f(b, args...)
178178
close(b)
179-
takebuf_string(s)
179+
String(take!(s))
180180
end
181181
base64encode(x...) = base64encode(write, x...)
182182

base/c.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ transcode{T<:Union{Int32,UInt32}}(::Type{T}, src::Vector{UInt8}) = transcode(T,
167167
function transcode{S<:Union{Int32,UInt32}}(::Type{UInt8}, src::Vector{S})
168168
buf = IOBuffer()
169169
for c in src; print(buf, Char(c)); end
170-
takebuf_array(buf)
170+
take!(buf)
171171
end
172172
transcode(::Type{String}, src::String) = src
173173
transcode(T, src::String) = transcode(T, src.data)

base/datafmt.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -643,9 +643,9 @@ function writedlm(io::IO, a::AbstractMatrix, dlm; opts...)
643643
writedlm_cell(pb, a[i, j], dlm, quotes)
644644
j == lastc ? write(pb,'\n') : print(pb,dlm)
645645
end
646-
(nb_available(pb) > (16*1024)) && write(io, takebuf_array(pb))
646+
(nb_available(pb) > (16*1024)) && write(io, take!(pb))
647647
end
648-
write(io, takebuf_array(pb))
648+
write(io, take!(pb))
649649
nothing
650650
end
651651

@@ -677,9 +677,9 @@ function writedlm(io::IO, itr, dlm; opts...)
677677
pb = PipeBuffer()
678678
for row in itr
679679
writedlm_row(pb, row, dlm, quotes)
680-
(nb_available(pb) > (16*1024)) && write(io, takebuf_array(pb))
680+
(nb_available(pb) > (16*1024)) && write(io, take!(pb))
681681
end
682-
write(io, takebuf_array(pb))
682+
write(io, take!(pb))
683683
nothing
684684
end
685685

base/deprecated.jl

+4
Original file line numberDiff line numberDiff line change
@@ -1112,4 +1112,8 @@ end)
11121112

11131113
Filesystem.stop_watching(stream::Filesystem._FDWatcher) = depwarn("stop_watching(::_FDWatcher) should not be used", :stop_watching)
11141114

1115+
# #19088
1116+
@deprecate takebuf_array take!
1117+
@deprecate takebuf_string(b) String(take!(b))
1118+
11151119
# End deprecations scheduled for 0.6

base/docs/helpdb/Base.jl

-8
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,6 @@ Compute the inverse sine of `x`, where the output is in radians.
5252
"""
5353
asin
5454

55-
"""
56-
takebuf_array(b::IOBuffer)
57-
58-
Obtain the contents of an `IOBuffer` as an array, without copying. Afterwards, the
59-
`IOBuffer` is reset to its initial state.
60-
"""
61-
takebuf_array
62-
6355
"""
6456
pointer(array [, index])
6557

base/exports.jl

+1-2
Original file line numberDiff line numberDiff line change
@@ -1166,8 +1166,7 @@ export
11661166
serialize,
11671167
skip,
11681168
skipchars,
1169-
takebuf_array,
1170-
takebuf_string,
1169+
take!,
11711170
truncate,
11721171
unmark,
11731172
watch_file,

base/io.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ function readuntil(s::IO, delim::Char)
400400
break
401401
end
402402
end
403-
return takebuf_string(out)
403+
return String(take!(out))
404404
end
405405

406406
function readuntil{T}(s::IO, delim::T)
@@ -445,7 +445,7 @@ function readuntil(s::IO, t::AbstractString)
445445
break
446446
end
447447
end
448-
return takebuf_string(out)
448+
return String(take!(out))
449449
end
450450

451451
readline() = readline(STDIN)

base/iobuffer.jl

+8-10
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,13 @@ function String(io::AbstractIOBuffer)
253253
return String(copy!(Array{UInt8}(io.size), 1, io.data, 1, io.size))
254254
end
255255

256-
function takebuf_array(io::AbstractIOBuffer)
256+
"""
257+
take!(b::IOBuffer)
258+
259+
Obtain the contents of an `IOBuffer` as an array, without copying. Afterwards, the
260+
`IOBuffer` is reset to its initial state.
261+
"""
262+
function take!(io::AbstractIOBuffer)
257263
ismarked(io) && unmark(io)
258264
if io.seekable
259265
nbytes = io.size
@@ -268,7 +274,7 @@ function takebuf_array(io::AbstractIOBuffer)
268274
end
269275
return data
270276
end
271-
function takebuf_array(io::IOBuffer)
277+
function take!(io::IOBuffer)
272278
ismarked(io) && unmark(io)
273279
if io.seekable
274280
data = io.data
@@ -291,14 +297,6 @@ function takebuf_array(io::IOBuffer)
291297
return data
292298
end
293299

294-
"""
295-
takebuf_string(b::IOBuffer)
296-
297-
Obtain the contents of an `IOBuffer` as a string, without copying.
298-
Afterwards, the `IOBuffer` is reset to its initial state.
299-
"""
300-
takebuf_string(io::AbstractIOBuffer) = String(takebuf_array(io))
301-
302300
function write(to::AbstractIOBuffer, from::AbstractIOBuffer)
303301
if to === from
304302
from.ptr = from.size + 1

base/iostream.jl

+3-12
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type IOStream <: IO
1313
IOStream(name::AbstractString, buf::Array{UInt8,1}) = new(pointer(buf), buf, name, -1)
1414
end
1515
# TODO: delay adding finalizer, e.g. for memio with a small buffer, or
16-
# in the case where we takebuf it.
16+
# in the case where we take! it.
1717
function IOStream(name::AbstractString, finalize::Bool)
1818
buf = zeros(UInt8,sizeof_ios_t)
1919
x = IOStream(name, buf)
@@ -218,17 +218,8 @@ function write(s::IOStream, c::Char)
218218
end
219219
read(s::IOStream, ::Type{Char}) = Char(ccall(:jl_getutf8, UInt32, (Ptr{Void},), s.ios))
220220

221-
takebuf_string(s::IOStream) =
222-
ccall(:jl_takebuf_string, Ref{String}, (Ptr{Void},), s.ios)
223-
224-
takebuf_array(s::IOStream) =
225-
ccall(:jl_takebuf_array, Vector{UInt8}, (Ptr{Void},), s.ios)
226-
227-
function takebuf_raw(s::IOStream)
228-
sz = position(s)
229-
buf = ccall(:jl_takebuf_raw, Ptr{UInt8}, (Ptr{Void},), s.ios)
230-
return buf, sz
231-
end
221+
take!(s::IOStream) =
222+
ccall(:jl_take_buffer, Vector{UInt8}, (Ptr{Void},), s.ios)
232223

233224
function readuntil(s::IOStream, delim::UInt8)
234225
ccall(:jl_readuntil, Array{UInt8,1}, (Ptr{Void}, UInt8), s.ios, delim)

base/markdown/Common/block.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ function indentcode(stream::IO, block::MD)
115115
break
116116
end
117117
end
118-
code = takebuf_string(buffer)
118+
code = String(take!(buffer))
119119
!isempty(code) && (push!(block, Code(rstrip(code))); return true)
120120
return false
121121
end
@@ -179,7 +179,7 @@ function blockquote(stream::IO, block::MD)
179179
end
180180
empty && return false
181181

182-
md = takebuf_string(buffer)
182+
md = String(take!(buffer))
183183
push!(block, BlockQuote(parse(md, flavor = config(block)).content))
184184
return true
185185
end
@@ -237,7 +237,7 @@ function admonition(stream::IO, block::MD)
237237
end
238238
end
239239
# Parse the nested block as markdown and create a new Admonition block.
240-
nested = parse(takebuf_string(buffer), flavor = config(block))
240+
nested = parse(String(take!(buffer)), flavor = config(block))
241241
push!(block, Admonition(category, title, nested.content))
242242
return true
243243
end
@@ -326,7 +326,7 @@ function list(stream::IO, block::MD)
326326
return true
327327
end
328328
end
329-
pushitem!(list, buffer) = push!(list.items, parse(takebuf_string(buffer)).content)
329+
pushitem!(list, buffer) = push!(list.items, parse(String(take!(buffer))).content)
330330

331331
# ––––––––––––––
332332
# HorizontalRule

base/markdown/GitHub/GitHub.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ function fencedcode(stream::IO, block::MD)
2121
if startswith(stream, string(ch) ^ n)
2222
if !startswith(stream, string(ch))
2323
if flavor == "math"
24-
push!(block, LaTeX(takebuf_string(buffer) |> chomp))
24+
push!(block, LaTeX(String(take!(buffer)) |> chomp))
2525
else
26-
push!(block, Code(flavor, takebuf_string(buffer) |> chomp))
26+
push!(block, Code(flavor, String(take!(buffer)) |> chomp))
2727
end
2828
return true
2929
else

base/markdown/parse/parse.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ function parseinline(stream::IO, md::MD, config::Config)
5656
char = Char(peek(stream))
5757
if haskey(config.inner, char) &&
5858
(inner = parseinline(stream, md, config.inner[char])) !== nothing
59-
c = takebuf_string(buffer)
59+
c = String(take!(buffer))
6060
!isempty(c) && push!(content, c)
6161
buffer = IOBuffer()
6262
push!(content, inner)
6363
else
6464
write(buffer, read(stream, Char))
6565
end
6666
end
67-
c = takebuf_string(buffer)
67+
c = String(take!(buffer))
6868
!isempty(c) && push!(content, c)
6969
return content
7070
end

base/markdown/parse/util.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ function readuntil(stream::IO, delimiter; newlines = false, match = nothing)
143143
while !eof(stream)
144144
if startswith(stream, delimiter)
145145
if count == 0
146-
return takebuf_string(buffer)
146+
return String(take!(buffer))
147147
else
148148
count -= 1
149149
write(buffer, delimiter)
@@ -190,7 +190,7 @@ function parse_inline_wrapper(stream::IO, delimiter::AbstractString; rep = false
190190
if !(char in whitespace || char == '\n' || char in delimiter) && startswith(stream, delimiter^n)
191191
trailing = 0
192192
while startswith(stream, delimiter); trailing += 1; end
193-
trailing == 0 && return takebuf_string(buffer)
193+
trailing == 0 && return String(take!(buffer))
194194
write(buffer, delimiter ^ (n + trailing))
195195
end
196196
end

base/multimedia.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ _textreprmime(m::MIME"text/plain", x::AbstractString) =
6363
function _binreprmime(m::MIME, x)
6464
s = IOBuffer()
6565
verbose_show(s, m, x)
66-
takebuf_array(s)
66+
take!(s)
6767
end
6868
_binreprmime(m::MIME, x::Vector{UInt8}) = x
6969

base/precompile.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ precompile(Base.systemerror, (Symbol, Bool))
389389
precompile(Base.take!, (Base.RemoteValue,))
390390
precompile(Base.take!, (RemoteChannel,))
391391
precompile(Base.take_ref, (Tuple{Int,Int},))
392-
precompile(Base.takebuf_string, (IOBuffer,))
392+
precompile(Base.take!, (IOBuffer,))
393393
precompile(Base.task_local_storage, ())
394394
precompile(Base.terminate_all_workers, ())
395395
precompile(Base.try_include, (String,))

base/printf.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -723,7 +723,7 @@ function gen_g(flags::String, width::Int, precision::Int, c::Char)
723723
# need to compute value before left-padding since trailing zeros are elided
724724
push!(blk.args, :(tmpout = IOBuffer()))
725725
push!(blk.args, :(print_fixed(tmpout,fprec,pt,len,$('#' in flags))))
726-
push!(blk.args, :(tmpstr = takebuf_string(tmpout)))
726+
push!(blk.args, :(tmpstr = String(take!(tmpout))))
727727
push!(blk.args, :(width -= length(tmpstr)))
728728
if '+' in flags || ' ' in flags
729729
push!(blk.args, :(width -= 1))
@@ -1111,7 +1111,7 @@ function bigfloat_printf(out, d, flags::String, width::Int, precision::Int, c::C
11111111
write(fmt, 'R')
11121112
write(fmt, c)
11131113
write(fmt, UInt8(0))
1114-
printf_fmt = takebuf_array(fmt)
1114+
printf_fmt = take!(fmt)
11151115
@assert length(printf_fmt) == fmt_len
11161116
bufsiz = length(DIGITS) - 1
11171117
lng = ccall((:mpfr_snprintf,:libmpfr), Int32, (Ptr{UInt8}, Culong, Ptr{UInt8}, Ptr{BigFloat}...), DIGITS, bufsiz, printf_fmt, &d)
@@ -1221,7 +1221,7 @@ macro sprintf(args...)
12211221
isa(args[1], AbstractString) || is_str_expr(args[1]) ||
12221222
throw(ArgumentError("@sprintf: first argument must be a format string"))
12231223
letexpr = _printf("@sprintf", :(IOBuffer()), args[1], args[2:end])
1224-
push!(letexpr.args[1].args, :(takebuf_string(out)))
1224+
push!(letexpr.args[1].args, :(String(take!(out))))
12251225
letexpr
12261226
end
12271227

base/replutil.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ function show_method_candidates(io::IO, ex::MethodError, kwargs::Vector=Any[])
553553
break
554554
end
555555
i += 1
556-
print(io, takebuf_string(line[1]))
556+
print(io, String(take!(line[1])))
557557
end
558558
end
559559
end

base/stream.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ end
733733

734734
function read(stream::LibuvStream)
735735
wait_readnb(stream, typemax(Int))
736-
return takebuf_array(stream.buffer)
736+
return take!(stream.buffer)
737737
end
738738

739739
function unsafe_read(s::LibuvStream, p::Ptr{UInt8}, nb::UInt)
@@ -778,7 +778,7 @@ function readavailable(this::LibuvStream)
778778
wait_readnb(this, 1)
779779
buf = this.buffer
780780
@assert buf.seekable == false
781-
return takebuf_array(buf)
781+
return take!(buf)
782782
end
783783

784784
function readuntil(this::LibuvStream, c::UInt8)
@@ -838,7 +838,7 @@ function flush(s::LibuvStream)
838838
end
839839
buf = get(s.sendbuf)
840840
if nb_available(buf) > 0
841-
arr = takebuf_array(buf) # Array of UInt8s
841+
arr = take!(buf) # Array of UInt8s
842842
uv_write(s, arr)
843843
end
844844
return

base/strings/basic.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ function map(f, s::AbstractString)
442442
end
443443
write(out, c2::Char)
444444
end
445-
String(takebuf_array(out))
445+
String(take!(out))
446446
end
447447

448448
function filter(f, s::AbstractString)
@@ -453,5 +453,5 @@ function filter(f, s::AbstractString)
453453
write(out, c)
454454
end
455455
end
456-
takebuf_string(out)
456+
String(take!(out))
457457
end

0 commit comments

Comments
 (0)