Class LrcWriter
- Namespace
- ModernLrc
- Assembly
- ModernLrc.dll
Entry-point for writing an LrcDocument. All overloads converge on a single char renderer; byte sinks encode its output with Encoding.
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).
Methods
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.
Exceptions
- ArgumentNullException
documentis null.
ValidateForWrite(LrcDocument, LrcWriteOptions?)
Pre-flight diagnostics (LRC0090 unrepresentable voice transitions). No rendering.
public static ImmutableArray<LrcDiagnostic> ValidateForWrite(LrcDocument document, LrcWriteOptions? options = null)
Parameters
documentLrcDocumentoptionsLrcWriteOptions
Returns
Write(LrcDocument, LrcWriteOptions?)
Render to a string.
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 into an IBufferWriter<T> of bytes encoded with Encoding.
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.
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, no BOM unless EmitByteOrderMark is set).
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)
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 write.
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. 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.