StringWriter is a subclass of OutStream with additional member functions for writing text.

The OutStream and StringWriter classes are, in fact, interchangeable. You can cast an OutStream to a StringWriter at any time by calling OutStream::strWriter(). The main reason why OutStream and StringWriter are separate classes is to help express intention in the code. OutStreams are mainly intended to write binary data, and StringWriters are mainly intended to write text encoded in an 8-bit format compatible with ASCII, such as UTF-8, ISO 8859-1, Windows-1252 or ASCII itself.

Use TextFormat::createExporter() to automatically convert UTF-8-encoded text to other formats.

Just like OutStream, you can create a StringWriter directly from an OutPipe such as stdOut. However, such a StringWriter will not perform automatic newline conversion:

StringWriter sw{stdOut};

For more information, see Unicode Support.

Header File

#include <ply-runtime/io/text/StringWriter.h>

Also included from <ply-runtime/Base.h>.

Member Functions

StringWriter(u32 chunkSizeExp = OutStream) [code]

Constructs a StringWriter that writes to a chunk list in memory.

StringWriter(StringWriter&& other) [code]

Move constructor. other is reset to a null stream. This constructor is safe to call even when other is an instance of MemOutStream or ViewOutStream.

StringWriter(OptionallyOwned<OutPipe>&& outPipe, u32 chunkSizeExp = DefaultChunkSizeExp) [code]

Constructs an OutStream that writes to an OutPipe. If outPipe is an owned pointer, the OutStream takes ownership of the OutPipe and will automatically destroy it in its destructor. If outPipe is a borrowed pointer, the OutStream does not take ownership of the OutPipe. See OptionallyOwned.

void operator=(StringWriter&& other) [code]

Move assignment operator. other is reset to a null stream.

String moveToString() [code]

If the StringWriter writes to a chunk list in memory, this function returns a String containing all the data that was written. The StringWriter is reset to a null stream as result of this call, which means that no further data can be written.

For small strings (default fewer than 4096 bytes), no new memory is allocated and no data is copied as a result of this call; instead, the memory allocation containing the output buffer is resized and the returned String takes ownership of it directly.

template <typename Args>
void format(StringView fmt, const Args& args) [code]

Template function that expands the format string fmt using the given arguments and writes the result to the output stream.

StringWriter sw;
sw.format("The answer is {}.\n", 42);
return sw.moveToString();

For more information, see Converting Values to Text.

template <typename T>
StringWriter& operator<<(const T& value) [code]

Template function that writes the the default text representation of value to the output stream.

StringWriter sw;
sw << "The answer is " << 42 << ".\n";
return sw.moveToString();

For more information, see Converting Values to Text.

Members Inherited From ply::OutStream

  • u8* curByte
  • u8* endByte
  • OutStream(OutStream&& other)
  • OutStream(OptionallyOwned<OutPipe>&& outPipe, u32 chunkSizeExp = DefaultChunkSizeExp)
  • ~OutStream()
  • void operator=(OutStream&& other)
  • bool atEOF() const
  • u32 numBytesAvailable() const
  • BufferView viewAvailable()
  • bool anyBytesAvailable() const
  • u64 getSeekPos() const
  • bool flush(bool toDevice = true)
  • void flushMem()
  • u32 tryMakeBytesAvailable(u32 numBytes = 1)
  • void makeBytesAvailable(u32 numBytes = 1)
  • bool writeByte(u8 byte)
  • bool write(ConstBufferView src)
  • StringWriter* strWriter()