1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 14:28:47 +00:00

style: move Write/WriteLine overloads to files

This change should help to organise this entire class a bit better, due to the number of overloads that exist.
This commit is contained in:
Oliver Booth 2023-04-11 22:23:12 +01:00
parent e6025b1a4c
commit f13907a4d0
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
9 changed files with 729 additions and 679 deletions

View File

@ -0,0 +1,83 @@
using System.Globalization;
namespace X10D.IO;
public static partial class TextWriterExtensions
{
/// <summary>
/// Writes the text representation of a 4-byte signed integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte signed integer to write.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteLineNoAlloc(this TextWriter writer, int value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteLineNoAlloc(value, "N0".AsSpan(), CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 4-byte signed integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte signed integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteLineNoAlloc(this TextWriter writer, int value, ReadOnlySpan<char> format)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteLineNoAlloc(writer, (long)value, format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 4-byte signed integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte signed integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteLineNoAlloc(this TextWriter writer, int value, ReadOnlySpan<char> format,
IFormatProvider? formatProvider)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteNoAlloc(value, format, formatProvider);
writer.WriteLine();
}
}

View File

@ -0,0 +1,85 @@
using System.Globalization;
namespace X10D.IO;
public static partial class TextWriterExtensions
{
/// <summary>
/// Writes the text representation of an 8-byte signed integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte signed integer to write.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteLineNoAlloc(this TextWriter writer, long value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteLineNoAlloc(value, "N0".AsSpan(), CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of an 8-byte signed integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte signed integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteLineNoAlloc(this TextWriter writer, long value, ReadOnlySpan<char> format)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteLineNoAlloc(value, format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of an 8-byte signed integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte signed integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteLineNoAlloc(this TextWriter writer,
long value,
ReadOnlySpan<char> format,
IFormatProvider? formatProvider)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteNoAlloc(value, format, formatProvider);
writer.WriteLine();
}
}

View File

@ -0,0 +1,88 @@
using System.Globalization;
namespace X10D.IO;
public static partial class TextWriterExtensions
{
/// <summary>
/// Writes the text representation of a 4-byte unsigned integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte unsigned integer to write.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteLineNoAlloc(this TextWriter writer, uint value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteLineNoAlloc(value, "N0".AsSpan(), CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 4-byte unsigned integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte unsigned integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteLineNoAlloc(this TextWriter writer, uint value, ReadOnlySpan<char> format)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteLineNoAlloc(writer, (long)value, format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 4-byte unsigned integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte unsigned integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteLineNoAlloc(this TextWriter writer,
uint value,
ReadOnlySpan<char> format,
IFormatProvider? formatProvider)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteNoAlloc(value, format, formatProvider);
writer.WriteLine();
}
}

View File

@ -0,0 +1,88 @@
using System.Globalization;
namespace X10D.IO;
public static partial class TextWriterExtensions
{
/// <summary>
/// Writes the text representation of an 8-byte unsigned integer to the text stream, followed by a line terminator,
/// without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte unsigned integer to write.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteLineNoAlloc(this TextWriter writer, ulong value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteLineNoAlloc(value, "N0".AsSpan(), CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of an 8-byte unsigned integer to the text stream, followed by a line terminator,
/// without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte unsigned integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteLineNoAlloc(this TextWriter writer, ulong value, ReadOnlySpan<char> format)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteLineNoAlloc(value, format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of an 8-byte unsigned integer to the text stream, followed by a line terminator,
/// without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte unsigned integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteLineNoAlloc(this TextWriter writer,
ulong value,
ReadOnlySpan<char> format,
IFormatProvider? formatProvider)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteNoAlloc(value, format, formatProvider);
writer.WriteLine();
}
}

View File

@ -0,0 +1,92 @@
using System.Globalization;
using X10D.Math;
namespace X10D.IO;
public static partial class TextWriterExtensions
{
/// <summary>
/// Writes the text representation of a 4-byte signed integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte signed integer to write.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteNoAlloc(this TextWriter writer, int value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteNoAlloc(writer, value, "N0".AsSpan(), CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 4-byte signed integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte signed integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteNoAlloc(this TextWriter writer, int value, ReadOnlySpan<char> format)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteNoAlloc(writer, value, format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 4-byte signed integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte signed integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteNoAlloc(this TextWriter writer, int value, ReadOnlySpan<char> format, IFormatProvider? formatProvider)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
int digitCount = value.CountDigits();
Span<char> buffer = stackalloc char[System.Math.Max(value < 0 ? digitCount + 1 : digitCount, 100)];
if (value.TryFormat(buffer, out int charsWritten, format, formatProvider))
{
Span<char> truncated = buffer[..charsWritten];
for (var index = 0; index < truncated.Length; index++)
{
writer.Write(truncated[index]);
}
}
else
{
writer.Write(value.ToString(format.ToString(), formatProvider));
}
}
}

View File

@ -0,0 +1,95 @@
using System.Globalization;
using X10D.Math;
namespace X10D.IO;
public static partial class TextWriterExtensions
{
/// <summary>
/// Writes the text representation of an 8-byte signed integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte signed integer to write.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteNoAlloc(this TextWriter writer, long value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteNoAlloc(writer, value, "N0".AsSpan(), CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of an 8-byte signed integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte signed integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteNoAlloc(this TextWriter writer, long value, ReadOnlySpan<char> format)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteNoAlloc(writer, value, format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of an 8-byte signed integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte signed integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteNoAlloc(this TextWriter writer,
long value,
ReadOnlySpan<char> format,
IFormatProvider? formatProvider)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
int digitCount = value.CountDigits();
Span<char> buffer = stackalloc char[System.Math.Max(value < 0 ? digitCount + 1 : digitCount, 100)];
if (value.TryFormat(buffer, out int charsWritten, format, formatProvider))
{
Span<char> truncated = buffer[..charsWritten];
for (var index = 0; index < truncated.Length; index++)
{
writer.Write(truncated[index]);
}
}
else
{
writer.Write(value.ToString(format.ToString(), formatProvider));
}
}
}

View File

@ -0,0 +1,98 @@
using System.Globalization;
using X10D.Math;
namespace X10D.IO;
public static partial class TextWriterExtensions
{
/// <summary>
/// Writes the text representation of a 4-byte unsigned integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte unsigned integer to write.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteNoAlloc(this TextWriter writer, uint value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteNoAlloc(writer, value, "N0".AsSpan(), CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 4-byte unsigned integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte unsigned integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteNoAlloc(this TextWriter writer, uint value, ReadOnlySpan<char> format)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteNoAlloc(writer, value, format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 4-byte unsigned integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte unsigned integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteNoAlloc(this TextWriter writer,
uint value,
ReadOnlySpan<char> format,
IFormatProvider? formatProvider)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
int digitCount = value.CountDigits();
Span<char> buffer = stackalloc char[System.Math.Max(digitCount, 100)];
if (value.TryFormat(buffer, out int charsWritten, format, formatProvider))
{
Span<char> truncated = buffer[..charsWritten];
for (var index = 0; index < truncated.Length; index++)
{
writer.Write(truncated[index]);
}
}
else
{
writer.Write(value.ToString(format.ToString(), formatProvider));
}
}
}

View File

@ -0,0 +1,98 @@
using System.Globalization;
using X10D.Math;
namespace X10D.IO;
public static partial class TextWriterExtensions
{
/// <summary>
/// Writes the text representation of an 8-byte unsigned integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte unsigned integer to write.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteNoAlloc(this TextWriter writer, ulong value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteNoAlloc(writer, value, "N0".AsSpan(), CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of an 8-byte signed integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte unsigned integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteNoAlloc(this TextWriter writer, ulong value, ReadOnlySpan<char> format)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteNoAlloc(writer, value, format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of an 8-byte signed integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte unsigned integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteNoAlloc(this TextWriter writer,
ulong value,
ReadOnlySpan<char> format,
IFormatProvider? formatProvider)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
int digitCount = value.CountDigits();
Span<char> buffer = stackalloc char[System.Math.Max(digitCount, 100)];
if (value.TryFormat(buffer, out int charsWritten, format, formatProvider))
{
Span<char> truncated = buffer[..charsWritten];
for (var index = 0; index < truncated.Length; index++)
{
writer.Write(truncated[index]);
}
}
else
{
writer.Write(value.ToString(format.ToString(), formatProvider));
}
}
}

View File

@ -1,685 +1,8 @@
using System.Globalization;
using X10D.Math;
namespace X10D.IO;
namespace X10D.IO;
/// <summary>
/// IO-related extension methods for <see cref="TextWriter" />.
/// </summary>
public static class TextWriterExtensions
public static partial class TextWriterExtensions
{
/// <summary>
/// Writes the text representation of a 4-byte signed integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte signed integer to write.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteNoAlloc(this TextWriter writer, int value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteNoAlloc(writer, value, "N0".AsSpan(), CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 4-byte signed integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte signed integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteNoAlloc(this TextWriter writer, int value, ReadOnlySpan<char> format)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteNoAlloc(writer, value, format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 4-byte signed integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte signed integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteNoAlloc(this TextWriter writer, int value, ReadOnlySpan<char> format, IFormatProvider? formatProvider)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
int digitCount = value.CountDigits();
Span<char> buffer = stackalloc char[System.Math.Max(value < 0 ? digitCount + 1 : digitCount, 1000)];
if (value.TryFormat(buffer, out int charsWritten, format, formatProvider))
{
Span<char> truncated = buffer[..charsWritten];
for (var index = 0; index < truncated.Length; index++)
{
writer.Write(truncated[index]);
}
}
else
{
writer.Write(value.ToString(format.ToString(), formatProvider));
}
}
/// <summary>
/// Writes the text representation of a 4-byte unsigned integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte unsigned integer to write.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteNoAlloc(this TextWriter writer, uint value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteNoAlloc(writer, value, "N0".AsSpan(), CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 4-byte unsigned integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte unsigned integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteNoAlloc(this TextWriter writer, uint value, ReadOnlySpan<char> format)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteNoAlloc(writer, value, format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 4-byte unsigned integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte unsigned integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteNoAlloc(this TextWriter writer,
uint value,
ReadOnlySpan<char> format,
IFormatProvider? formatProvider)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
int digitCount = value.CountDigits();
Span<char> buffer = stackalloc char[System.Math.Max(digitCount, 1000)];
if (value.TryFormat(buffer, out int charsWritten, format, formatProvider))
{
Span<char> truncated = buffer[..charsWritten];
for (var index = 0; index < truncated.Length; index++)
{
writer.Write(truncated[index]);
}
}
else
{
writer.Write(value.ToString(format.ToString(), formatProvider));
}
}
/// <summary>
/// Writes the text representation of a 8-byte signed integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte signed integer to write.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteNoAlloc(this TextWriter writer, long value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteNoAlloc(writer, value, "N0".AsSpan(), CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 8-byte signed integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte signed integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteNoAlloc(this TextWriter writer, long value, ReadOnlySpan<char> format)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteNoAlloc(writer, value, format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 8-byte signed integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte signed integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteNoAlloc(this TextWriter writer,
long value,
ReadOnlySpan<char> format,
IFormatProvider? formatProvider)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
int digitCount = value.CountDigits();
Span<char> buffer = stackalloc char[System.Math.Max(value < 0 ? digitCount + 1 : digitCount, 1000)];
if (value.TryFormat(buffer, out int charsWritten, format, formatProvider))
{
Span<char> truncated = buffer[..charsWritten];
for (var index = 0; index < truncated.Length; index++)
{
writer.Write(truncated[index]);
}
}
else
{
writer.Write(value.ToString(format.ToString(), formatProvider));
}
}
/// <summary>
/// Writes the text representation of a 8-byte unsigned integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte unsigned integer to write.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteNoAlloc(this TextWriter writer, ulong value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteNoAlloc(writer, value, "N0".AsSpan(), CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 8-byte signed integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte unsigned integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteNoAlloc(this TextWriter writer, ulong value, ReadOnlySpan<char> format)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
WriteNoAlloc(writer, value, format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 8-byte signed integer to the text stream, without allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte unsigned integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteNoAlloc(this TextWriter writer,
ulong value,
ReadOnlySpan<char> format,
IFormatProvider? formatProvider)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
int digitCount = value.CountDigits();
Span<char> buffer = stackalloc char[System.Math.Max(digitCount, 1000)];
if (value.TryFormat(buffer, out int charsWritten, format, formatProvider))
{
Span<char> truncated = buffer[..charsWritten];
for (var index = 0; index < truncated.Length; index++)
{
writer.Write(truncated[index]);
}
}
else
{
writer.Write(value.ToString(format.ToString(), formatProvider));
}
}
/// <summary>
/// Writes the text representation of a 4-byte signed integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte signed integer to write.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteLineNoAlloc(this TextWriter writer, int value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteLineNoAlloc(value, "N0".AsSpan(), CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 4-byte signed integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte signed integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteLineNoAlloc(this TextWriter writer, int value, ReadOnlySpan<char> format)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteLineNoAlloc(value, format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 4-byte signed integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte signed integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteLineNoAlloc(this TextWriter writer, int value, ReadOnlySpan<char> format,
IFormatProvider? formatProvider)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteNoAlloc(value, format, formatProvider);
writer.WriteLine();
}
/// <summary>
/// Writes the text representation of a 4-byte unsigned integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte unsigned integer to write.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteLineNoAlloc(this TextWriter writer, uint value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteLineNoAlloc(value, "N0".AsSpan(), CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 4-byte unsigned integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte unsigned integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteLineNoAlloc(this TextWriter writer, uint value, ReadOnlySpan<char> format)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteLineNoAlloc(value, format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 4-byte unsigned integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 4-byte unsigned integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteLineNoAlloc(this TextWriter writer,
uint value,
ReadOnlySpan<char> format,
IFormatProvider? formatProvider)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteNoAlloc(value, format, formatProvider);
writer.WriteLine();
}
/// <summary>
/// Writes the text representation of a 8-byte signed integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte signed integer to write.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteLineNoAlloc(this TextWriter writer, long value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteLineNoAlloc(value, "N0".AsSpan(), CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 8-byte signed integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte signed integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteLineNoAlloc(this TextWriter writer, long value, ReadOnlySpan<char> format)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteLineNoAlloc(value, format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 8-byte signed integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte signed integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
public static void WriteLineNoAlloc(this TextWriter writer,
long value,
ReadOnlySpan<char> format,
IFormatProvider? formatProvider)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteNoAlloc(value, format, formatProvider);
writer.WriteLine();
}
/// <summary>
/// Writes the text representation of a 8-byte unsigned integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte unsigned integer to write.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteLineNoAlloc(this TextWriter writer, ulong value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteLineNoAlloc(value, "N0".AsSpan(), CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 8-byte signed integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte unsigned integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteLineNoAlloc(this TextWriter writer, ulong value, ReadOnlySpan<char> format)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteLineNoAlloc(value, format, CultureInfo.CurrentCulture);
}
/// <summary>
/// Writes the text representation of a 8-byte signed integer to the text stream, followed by a line terminator, without
/// allocating a string.
/// </summary>
/// <param name="writer">The <see cref="TextWriter" /> to write to.</param>
/// <param name="value">The 8-byte unsigned integer to write.</param>
/// <param name="format">A standard or custom numeric format string.</param>
/// <param name="formatProvider">An object that supplies culture-specific formatting information.</param>
/// <remarks>This method may still allocate if the integer is too large to fit in a stack-allocated buffer.</remarks>
/// <exception cref="ArgumentNullException"><paramref name="writer" /> is <see langword="null" />.</exception>
/// <exception cref="ObjectDisposedException">The <see cref="TextWriter" /> is closed.</exception>
/// <exception cref="IOException">An I/O error occurs.</exception>
[CLSCompliant(false)]
public static void WriteLineNoAlloc(this TextWriter writer,
ulong value,
ReadOnlySpan<char> format,
IFormatProvider? formatProvider)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(writer);
#else
if (writer is null)
{
throw new ArgumentNullException(nameof(writer));
}
#endif
writer.WriteNoAlloc(value, format, formatProvider);
writer.WriteLine();
}
}