simplify io stream argument options

also, fix mistake in text streams

closes: #3201
This commit is contained in:
payonel 2020-01-25 16:12:00 -08:00
parent 8fe92ee191
commit 5b1c60f5ed
2 changed files with 4 additions and 9 deletions

View File

@ -181,7 +181,7 @@ function text.internal.reader(txt, mode)
}, {__index=text.internal.stream_base((mode or ""):match("b"))})
process.closeOnExit(reader)
return require("buffer").new((mode:match("[rb]+") or "r"), reader)
return require("buffer").new((mode or "r"):match("[rb]+"), reader)
end
function text.internal.writer(ostream, mode, append_txt)
@ -223,7 +223,7 @@ function text.internal.writer(ostream, mode, append_txt)
}, {__index=text.internal.stream_base((mode or ""):match("b"))})
process.closeOnExit(writer)
return require("buffer").new((mode:match("[awb]+") or "w"), writer)
return require("buffer").new((mode or "w"):match("[awb]+"), writer)
end
function text.detab(value, tabWidth)

View File

@ -47,17 +47,12 @@ end
function io.stream(fd,file,mode)
checkArg(1,fd,'number')
checkArg(2, file, "table", "string", "nil")
assert(fd>=0,'fd must be >= 0. 0 is input, 1 is stdout, 2 is stderr')
local dio = require("process").info().data.io
if file then
if type(file) == "string" then
local result, reason = io.open(file, mode)
if not result then
error(reason, 2)
end
file = result
elseif not io.type(file) then
error("bad argument #1 (string or file expected, got " .. type(file) .. ")", 2)
file = assert(io.open(file, mode))
end
dio[fd] = file
end