X-Git-Url: http://nitlanguage.org diff --git a/lib/standard/stream.nit b/lib/standard/stream.nit index d26d95f..d65d377 100644 --- a/lib/standard/stream.nit +++ b/lib/standard/stream.nit @@ -98,6 +98,35 @@ interface OStream fun is_writable: Bool is abstract end +# Things that can be efficienlty writen to a OStream +# +# The point of this interface it to allow is instance to be efficenty +# writen into a OStream without having to allocate a big String object +# +# ready-to-save documents usually provide this interface. +interface Streamable + # Write itself to a `stream` + # The specific logic it let to the concrete subclasses + fun write_to(stream: OStream) is abstract + + # Like `write_to` but return a new String (may be quite large) + # + # This funtionnality is anectodical, since the point + # of streamable object to to be efficienlty written to a + # stream without having to allocate and concatenate strings + fun write_to_string: String + do + var stream = new StringOStream + write_to(stream) + return stream.to_s + end +end + +redef class String + super Streamable + redef fun write_to(stream) do stream.write(self) +end + # Input streams with a buffer abstract class BufferedIStream super IStream