Prev: Module io: File and Stream Input/Output
Module memio: In-Memory Streams and Buffers
The module provides in-memory streams which can be used as temporary data buffers. Once created, an in-memory stream can be used like any other stream, or like a temporary file created with
io.create.
In addition to the standard I/O functions, an in-memory stream also supports the functions memio.delete and memio.insert which allow to efficiently edit and "shuffle" data.
Assembling large strings via in-memory streams is significantly more efficient (both in time and in space) than assembling by repeated string concatenation.
Please note that stream positions match character positions only with the io.raw encoding scheme (CES). With io.utf16le and io.utf16be encoding schemes, character positions and stream positions differ by a factor of two. With the io.utf8 encoding scheme, there is no simple correspondence between stream and character positions.
memio.clear
• function clear(stream) → null
Deletes all data in the buffer of the stream stream. io.size(stream) will return 0 afterwards.
memio.delete
• function delete(stream, at, length=io.size(stream)-at) → null
Deletes the region at position at with length length from the stream's buffer, moving the following data up. After deleting, the current stream position points to the same byte of data as before deleting. If the byte at the current position was deleted, the new current position is at the end of the deleted region.
Throws ExcValueOutOfRange if the region is not completely part of the buffer.
s=memio.new(); // io.raw encoding
io.write(s, "This is some text");
memio.delete(s, 8, 5);
print memio.get(s)
→ This is text
|
memio.get
• function get(stream, at=0, chars=io.size(stream)-at) → String
Get the chars characters starting at (stream) position at from the stream's buffer, or as many as are available. This is a convenience function roughly equivalent to but more efficient than
function get(s)
old=io.seek(s, at);
t=io.read(s, length);
io.seek(s, old);
return t
end
|
Throws ExcValueOutOfRange if at is not a valid stream position, or if chars is negative.
memio.insert
• function insert(stream, at, data) → null
Inserts the string data into the stream's buffer at (stream) position at. After inserting, the current stream position points to the same byte of data as before inserting.
s=memio.new(); // io.raw encoding
io.write(s, "This is some text");
memio.insert(s, 13, "longer ");
print memio.get(s)
→ This is some longer text
|
Throws ExcValueOutOfRange if at is not a valid stream position.
memio.new
• function new(ces=io.raw) → Native Object
Create a new in-memory stream with character encoding scheme ces. The stream can be used like any other stream, i.e. the functions of module io can be used with it.
Next: Module system: System Related Functions© 2004-2011 airbit AG, CH-8008 Zürich
Document AB-M-LIB-888