Class LrcWriter
- Namespace
- ModernLrc
- Assembly
- ModernLrc.dll
Entry-point for writing an LrcDocument. All overloads converge on the canonical Write(LrcDocument, LrcWriteOptions?).
public static class LrcWriter
- Inheritance
-
LrcWriter
- Inherited Members
Examples
using ModernLrc;
using ModernLrc.Model;
var doc = new LrcDocumentBuilder()
.WithTitle("My Song")
.AddLine("00:12.00", "first line")
.AddLine("00:14.20", "second line", LrcVoice.Female)
.Build();
string lrc = LrcWriter.Write(doc);
// [ti:My Song]
//
// [00:12.00]first line
// [00:14.20]F: second line
Remarks
Sinks: string, TextWriter, Stream,
IBufferWriter<T> (chars or encoded bytes), fixed Span<T>
(via the TryWrite family), and a path on disk
(WriteFile(LrcDocument, string, LrcWriteOptions?), atomic via temp + rename).
The IBufferWriter<T> char sink and UTF-8 byte sinks are zero-allocation beyond consumer-buffer growth.
Methods
EstimateByteSize(LrcDocument, LrcWriteOptions?)
O(lines + words + tags) byte-length estimate (UTF-8 worst-case: up to 4 bytes per char + headroom).
[Pure]
public static int EstimateByteSize(LrcDocument document, LrcWriteOptions? options = null)
Parameters
documentLrcDocumentoptionsLrcWriteOptions
Returns
EstimateSize(LrcDocument, LrcWriteOptions?)
O(lines + words + tags) char-length estimate. Pure.
[Pure]
public static int EstimateSize(LrcDocument document, LrcWriteOptions? options = null)
Parameters
documentLrcDocumentoptionsLrcWriteOptions
Returns
TryWrite(LrcDocument, Span<byte>, out int, LrcWriteOptions?)
Try-render into a fixed byte buffer using Encoding.
public static bool TryWrite(LrcDocument document, Span<byte> destination, out int bytesWritten, LrcWriteOptions? options = null)
Parameters
documentLrcDocumentThe document to render.
destinationSpan<byte>Destination span.
bytesWrittenintNumber of bytes written on success; 0 on failure.
optionsLrcWriteOptionsWrite options.
Returns
- bool
falsewhendestinationis too small;trueon success.
Exceptions
- ArgumentNullException
documentis null.
TryWrite(LrcDocument, Span<char>, out int, LrcWriteOptions?)
Try-render into a fixed char buffer.
public static bool TryWrite(LrcDocument document, Span<char> destination, out int charsWritten, LrcWriteOptions? options = null)
Parameters
documentLrcDocumentThe document to render.
destinationSpan<char>Destination span.
charsWrittenintNumber of chars written on success; 0 on failure.
optionsLrcWriteOptionsWrite options.
Returns
- bool
falsewhendestinationis too small;trueon success.
Remarks
The implementation renders to a staging buffer first to know the exact size,
then copies into destination. For zero-allocation rendering use the
IBufferWriter<T> overload instead.
Exceptions
- ArgumentNullException
documentis null.
ValidateForWrite(LrcDocument, LrcWriteOptions?)
Pre-flight diagnostics (LRC0090 unrepresentable voice transitions). Pure — no rendering.
[Pure]
public static ImmutableArray<LrcDiagnostic> ValidateForWrite(LrcDocument document, LrcWriteOptions? options = null)
Parameters
documentLrcDocumentoptionsLrcWriteOptions
Returns
Write(LrcDocument, LrcWriteOptions?)
Render to a string. Internally renders into an ArrayBufferWriter<T> of chars then materializes the final string. The buffer is pre-sized via EstimateSize(LrcDocument, LrcWriteOptions?) so large documents don't accumulate copy-on-grow waste.
public static string Write(LrcDocument document, LrcWriteOptions? options = null)
Parameters
documentLrcDocumentThe document to render.
optionsLrcWriteOptionsWrite options. Defaults to Default.
Returns
- string
The fully-rendered LRC text.
Exceptions
- ArgumentNullException
documentis null.
Write(LrcDocument, IBufferWriter<byte>, LrcWriteOptions?)
Render directly into an IBufferWriter<T> of encoded bytes. The UTF-8 path has zero allocations beyond consumer buffer growth.
public static void Write(LrcDocument document, IBufferWriter<byte> writer, LrcWriteOptions? options = null)
Parameters
documentLrcDocumentThe document to render.
writerIBufferWriter<byte>Destination buffer writer.
optionsLrcWriteOptionsWrite options.
Exceptions
- ArgumentNullException
documentorwriteris null.
Write(LrcDocument, IBufferWriter<char>, LrcWriteOptions?)
Render directly into an IBufferWriter<T> of chars. Zero allocations beyond consumer buffer growth.
public static void Write(LrcDocument document, IBufferWriter<char> writer, LrcWriteOptions? options = null)
Parameters
documentLrcDocumentThe document to render.
writerIBufferWriter<char>Destination buffer writer.
optionsLrcWriteOptionsWrite options.
Examples
var bw = new System.Buffers.ArrayBufferWriter<char>();
LrcWriter.Write(doc, bw);
var span = bw.WrittenSpan;
Exceptions
- ArgumentNullException
documentorwriteris null.
Write(LrcDocument, Stream, LrcWriteOptions?)
Render to a Stream using Encoding (default UTF-8 with no BOM). When the encoding is UTF-8, the writer takes a fast path that renders directly into bytes — no intermediate string copy.
public static void Write(LrcDocument document, Stream stream, LrcWriteOptions? options = null)
Parameters
documentLrcDocumentThe document to render.
streamStreamDestination stream; must be writable.
optionsLrcWriteOptionsWrite options.
Exceptions
- ArgumentNullException
documentorstreamis null.- ArgumentException
streamis not writable.
Write(LrcDocument, TextWriter, LrcWriteOptions?)
Render to a TextWriter via a single span write — no intermediate string allocation.
public static void Write(LrcDocument document, TextWriter writer, LrcWriteOptions? options = null)
Parameters
documentLrcDocumentThe document to render.
writerTextWriterDestination writer.
optionsLrcWriteOptionsWrite options.
Exceptions
- ArgumentNullException
documentorwriteris null.
WriteAsync(LrcDocument, Stream, LrcWriteOptions?, CancellationToken)
Async render to a Stream. When the encoding is UTF-8, the writer takes a fast path that renders directly into bytes — no intermediate string copy.
public static ValueTask WriteAsync(LrcDocument document, Stream stream, LrcWriteOptions? options = null, CancellationToken cancellationToken = default)
Parameters
documentLrcDocumentThe document to render.
streamStreamDestination stream; must be writable.
optionsLrcWriteOptionsWrite options.
cancellationTokenCancellationTokenHonored before render and during async write.
Returns
Examples
using var fs = File.Create("out.lrc");
await LrcWriter.WriteAsync(doc, fs, cancellationToken: ct);
Exceptions
- ArgumentNullException
documentorstreamis null.- ArgumentException
streamis not writable.- OperationCanceledException
Cancellation was requested.
WriteAsync(LrcDocument, TextWriter, LrcWriteOptions?, CancellationToken)
Async render to a TextWriter.
public static ValueTask WriteAsync(LrcDocument document, TextWriter writer, LrcWriteOptions? options = null, CancellationToken cancellationToken = default)
Parameters
documentLrcDocumentThe document to render.
writerTextWriterDestination writer.
optionsLrcWriteOptionsWrite options.
cancellationTokenCancellationTokenHonored before the synchronous render and during the asynchronous flush.
Returns
Exceptions
- ArgumentNullException
documentorwriteris null.- OperationCanceledException
Cancellation was requested.
WriteFile(LrcDocument, string, LrcWriteOptions?)
Atomic file write: writes to a temp file in the destination directory then moves it into place with overwrite. Uses None on the temp file. The temp file is cleaned up only on failure.
public static void WriteFile(LrcDocument document, string path, LrcWriteOptions? options = null)
Parameters
documentLrcDocumentThe document to render.
pathstringDestination file path. Must be non-empty.
optionsLrcWriteOptionsWrite options.
Examples
LrcWriter.WriteFile(doc, "song.lrc");
// On success: "song.lrc" exists, no temp file remains.
// On failure: any temp file in the destination dir is best-effort cleaned up.
Exceptions
- ArgumentNullException
documentis null.- ArgumentException
pathis null, empty, or whitespace.- IOException
A filesystem error occurred. The temp file is cleaned up best-effort.
- UnauthorizedAccessException
The destination cannot be written.
WriteFileAsync(LrcDocument, string, LrcWriteOptions?, CancellationToken)
Async atomic file write: writes to a temp file in the destination directory and then moves it into place with overwrite. The temp file is cleaned up only on failure.
public static ValueTask WriteFileAsync(LrcDocument document, string path, LrcWriteOptions? options = null, CancellationToken cancellationToken = default)
Parameters
documentLrcDocumentThe document to render.
pathstringDestination file path. Must be non-empty.
optionsLrcWriteOptionsWrite options.
cancellationTokenCancellationTokenHonored before opening and during writes.
Returns
Exceptions
- ArgumentNullException
documentis null.- ArgumentException
pathis null, empty, or whitespace.- IOException
A filesystem error occurred. The temp file is cleaned up best-effort.
- UnauthorizedAccessException
The destination cannot be written.
- OperationCanceledException
Cancellation was requested.