diff --git a/X10D/src/IO/TextWriterExtensions.WriteLineNoAlloc.Int32.cs b/X10D/src/IO/TextWriterExtensions.WriteLineNoAlloc.Int32.cs new file mode 100644 index 0000000..8836d63 --- /dev/null +++ b/X10D/src/IO/TextWriterExtensions.WriteLineNoAlloc.Int32.cs @@ -0,0 +1,83 @@ +using System.Globalization; + +namespace X10D.IO; + +public static partial class TextWriterExtensions +{ + /// + /// Writes the text representation of a 4-byte signed integer to the text stream, followed by a line terminator, without + /// allocating a string. + /// + /// The to write to. + /// The 4-byte signed integer to write. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + 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); + } + + /// + /// Writes the text representation of a 4-byte signed integer to the text stream, followed by a line terminator, without + /// allocating a string. + /// + /// The to write to. + /// The 4-byte signed integer to write. + /// A standard or custom numeric format string. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + public static void WriteLineNoAlloc(this TextWriter writer, int value, ReadOnlySpan 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); + } + + /// + /// Writes the text representation of a 4-byte signed integer to the text stream, followed by a line terminator, without + /// allocating a string. + /// + /// The to write to. + /// The 4-byte signed integer to write. + /// A standard or custom numeric format string. + /// An object that supplies culture-specific formatting information. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + public static void WriteLineNoAlloc(this TextWriter writer, int value, ReadOnlySpan 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(); + } +} diff --git a/X10D/src/IO/TextWriterExtensions.WriteLineNoAlloc.Int64.cs b/X10D/src/IO/TextWriterExtensions.WriteLineNoAlloc.Int64.cs new file mode 100644 index 0000000..83deadd --- /dev/null +++ b/X10D/src/IO/TextWriterExtensions.WriteLineNoAlloc.Int64.cs @@ -0,0 +1,85 @@ +using System.Globalization; + +namespace X10D.IO; + +public static partial class TextWriterExtensions +{ + /// + /// Writes the text representation of an 8-byte signed integer to the text stream, followed by a line terminator, without + /// allocating a string. + /// + /// The to write to. + /// The 8-byte signed integer to write. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + 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); + } + + /// + /// Writes the text representation of an 8-byte signed integer to the text stream, followed by a line terminator, without + /// allocating a string. + /// + /// The to write to. + /// The 8-byte signed integer to write. + /// A standard or custom numeric format string. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + public static void WriteLineNoAlloc(this TextWriter writer, long value, ReadOnlySpan 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); + } + + /// + /// Writes the text representation of an 8-byte signed integer to the text stream, followed by a line terminator, without + /// allocating a string. + /// + /// The to write to. + /// The 8-byte signed integer to write. + /// A standard or custom numeric format string. + /// An object that supplies culture-specific formatting information. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + public static void WriteLineNoAlloc(this TextWriter writer, + long value, + ReadOnlySpan 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(); + } +} diff --git a/X10D/src/IO/TextWriterExtensions.WriteLineNoAlloc.UInt32.cs b/X10D/src/IO/TextWriterExtensions.WriteLineNoAlloc.UInt32.cs new file mode 100644 index 0000000..98e9d42 --- /dev/null +++ b/X10D/src/IO/TextWriterExtensions.WriteLineNoAlloc.UInt32.cs @@ -0,0 +1,88 @@ +using System.Globalization; + +namespace X10D.IO; + +public static partial class TextWriterExtensions +{ + /// + /// Writes the text representation of a 4-byte unsigned integer to the text stream, followed by a line terminator, without + /// allocating a string. + /// + /// The to write to. + /// The 4-byte unsigned integer to write. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + [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); + } + + /// + /// Writes the text representation of a 4-byte unsigned integer to the text stream, followed by a line terminator, without + /// allocating a string. + /// + /// The to write to. + /// The 4-byte unsigned integer to write. + /// A standard or custom numeric format string. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + [CLSCompliant(false)] + public static void WriteLineNoAlloc(this TextWriter writer, uint value, ReadOnlySpan 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); + } + + /// + /// Writes the text representation of a 4-byte unsigned integer to the text stream, followed by a line terminator, without + /// allocating a string. + /// + /// The to write to. + /// The 4-byte unsigned integer to write. + /// A standard or custom numeric format string. + /// An object that supplies culture-specific formatting information. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + [CLSCompliant(false)] + public static void WriteLineNoAlloc(this TextWriter writer, + uint value, + ReadOnlySpan 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(); + } +} diff --git a/X10D/src/IO/TextWriterExtensions.WriteLineNoAlloc.UInt64.cs b/X10D/src/IO/TextWriterExtensions.WriteLineNoAlloc.UInt64.cs new file mode 100644 index 0000000..68f397f --- /dev/null +++ b/X10D/src/IO/TextWriterExtensions.WriteLineNoAlloc.UInt64.cs @@ -0,0 +1,88 @@ +using System.Globalization; + +namespace X10D.IO; + +public static partial class TextWriterExtensions +{ + /// + /// Writes the text representation of an 8-byte unsigned integer to the text stream, followed by a line terminator, + /// without allocating a string. + /// + /// The to write to. + /// The 8-byte unsigned integer to write. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + [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); + } + + /// + /// Writes the text representation of an 8-byte unsigned integer to the text stream, followed by a line terminator, + /// without allocating a string. + /// + /// The to write to. + /// The 8-byte unsigned integer to write. + /// A standard or custom numeric format string. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + [CLSCompliant(false)] + public static void WriteLineNoAlloc(this TextWriter writer, ulong value, ReadOnlySpan 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); + } + + /// + /// Writes the text representation of an 8-byte unsigned integer to the text stream, followed by a line terminator, + /// without allocating a string. + /// + /// The to write to. + /// The 8-byte unsigned integer to write. + /// A standard or custom numeric format string. + /// An object that supplies culture-specific formatting information. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + [CLSCompliant(false)] + public static void WriteLineNoAlloc(this TextWriter writer, + ulong value, + ReadOnlySpan 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(); + } +} diff --git a/X10D/src/IO/TextWriterExtensions.WriteNoAlloc.Int32.cs b/X10D/src/IO/TextWriterExtensions.WriteNoAlloc.Int32.cs new file mode 100644 index 0000000..c2366bf --- /dev/null +++ b/X10D/src/IO/TextWriterExtensions.WriteNoAlloc.Int32.cs @@ -0,0 +1,92 @@ +using System.Globalization; +using X10D.Math; + +namespace X10D.IO; + +public static partial class TextWriterExtensions +{ + /// + /// Writes the text representation of a 4-byte signed integer to the text stream, without allocating a string. + /// + /// The to write to. + /// The 4-byte signed integer to write. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + 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); + } + + /// + /// Writes the text representation of a 4-byte signed integer to the text stream, without allocating a string. + /// + /// The to write to. + /// The 4-byte signed integer to write. + /// A standard or custom numeric format string. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + public static void WriteNoAlloc(this TextWriter writer, int value, ReadOnlySpan 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); + } + + /// + /// Writes the text representation of a 4-byte signed integer to the text stream, without allocating a string. + /// + /// The to write to. + /// The 4-byte signed integer to write. + /// A standard or custom numeric format string. + /// An object that supplies culture-specific formatting information. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + public static void WriteNoAlloc(this TextWriter writer, int value, ReadOnlySpan 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 buffer = stackalloc char[System.Math.Max(value < 0 ? digitCount + 1 : digitCount, 100)]; + if (value.TryFormat(buffer, out int charsWritten, format, formatProvider)) + { + Span truncated = buffer[..charsWritten]; + for (var index = 0; index < truncated.Length; index++) + { + writer.Write(truncated[index]); + } + } + else + { + writer.Write(value.ToString(format.ToString(), formatProvider)); + } + } +} diff --git a/X10D/src/IO/TextWriterExtensions.WriteNoAlloc.Int64.cs b/X10D/src/IO/TextWriterExtensions.WriteNoAlloc.Int64.cs new file mode 100644 index 0000000..049d891 --- /dev/null +++ b/X10D/src/IO/TextWriterExtensions.WriteNoAlloc.Int64.cs @@ -0,0 +1,95 @@ +using System.Globalization; +using X10D.Math; + +namespace X10D.IO; + +public static partial class TextWriterExtensions +{ + /// + /// Writes the text representation of an 8-byte signed integer to the text stream, without allocating a string. + /// + /// The to write to. + /// The 8-byte signed integer to write. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + 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); + } + + /// + /// Writes the text representation of an 8-byte signed integer to the text stream, without allocating a string. + /// + /// The to write to. + /// The 8-byte signed integer to write. + /// A standard or custom numeric format string. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + public static void WriteNoAlloc(this TextWriter writer, long value, ReadOnlySpan 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); + } + + /// + /// Writes the text representation of an 8-byte signed integer to the text stream, without allocating a string. + /// + /// The to write to. + /// The 8-byte signed integer to write. + /// A standard or custom numeric format string. + /// An object that supplies culture-specific formatting information. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + public static void WriteNoAlloc(this TextWriter writer, + long value, + ReadOnlySpan 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 buffer = stackalloc char[System.Math.Max(value < 0 ? digitCount + 1 : digitCount, 100)]; + if (value.TryFormat(buffer, out int charsWritten, format, formatProvider)) + { + Span truncated = buffer[..charsWritten]; + for (var index = 0; index < truncated.Length; index++) + { + writer.Write(truncated[index]); + } + } + else + { + writer.Write(value.ToString(format.ToString(), formatProvider)); + } + } +} diff --git a/X10D/src/IO/TextWriterExtensions.WriteNoAlloc.UInt32.cs b/X10D/src/IO/TextWriterExtensions.WriteNoAlloc.UInt32.cs new file mode 100644 index 0000000..f8d32a2 --- /dev/null +++ b/X10D/src/IO/TextWriterExtensions.WriteNoAlloc.UInt32.cs @@ -0,0 +1,98 @@ +using System.Globalization; +using X10D.Math; + +namespace X10D.IO; + +public static partial class TextWriterExtensions +{ + /// + /// Writes the text representation of a 4-byte unsigned integer to the text stream, without allocating a string. + /// + /// The to write to. + /// The 4-byte unsigned integer to write. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + [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); + } + + /// + /// Writes the text representation of a 4-byte unsigned integer to the text stream, without allocating a string. + /// + /// The to write to. + /// The 4-byte unsigned integer to write. + /// A standard or custom numeric format string. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + [CLSCompliant(false)] + public static void WriteNoAlloc(this TextWriter writer, uint value, ReadOnlySpan 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); + } + + /// + /// Writes the text representation of a 4-byte unsigned integer to the text stream, without allocating a string. + /// + /// The to write to. + /// The 4-byte unsigned integer to write. + /// A standard or custom numeric format string. + /// An object that supplies culture-specific formatting information. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + [CLSCompliant(false)] + public static void WriteNoAlloc(this TextWriter writer, + uint value, + ReadOnlySpan 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 buffer = stackalloc char[System.Math.Max(digitCount, 100)]; + if (value.TryFormat(buffer, out int charsWritten, format, formatProvider)) + { + Span truncated = buffer[..charsWritten]; + for (var index = 0; index < truncated.Length; index++) + { + writer.Write(truncated[index]); + } + } + else + { + writer.Write(value.ToString(format.ToString(), formatProvider)); + } + } +} diff --git a/X10D/src/IO/TextWriterExtensions.WriteNoAlloc.UInt64.cs b/X10D/src/IO/TextWriterExtensions.WriteNoAlloc.UInt64.cs new file mode 100644 index 0000000..5e266e4 --- /dev/null +++ b/X10D/src/IO/TextWriterExtensions.WriteNoAlloc.UInt64.cs @@ -0,0 +1,98 @@ +using System.Globalization; +using X10D.Math; + +namespace X10D.IO; + +public static partial class TextWriterExtensions +{ + /// + /// Writes the text representation of an 8-byte unsigned integer to the text stream, without allocating a string. + /// + /// The to write to. + /// The 8-byte unsigned integer to write. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + [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); + } + + /// + /// Writes the text representation of an 8-byte signed integer to the text stream, without allocating a string. + /// + /// The to write to. + /// The 8-byte unsigned integer to write. + /// A standard or custom numeric format string. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + [CLSCompliant(false)] + public static void WriteNoAlloc(this TextWriter writer, ulong value, ReadOnlySpan 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); + } + + /// + /// Writes the text representation of an 8-byte signed integer to the text stream, without allocating a string. + /// + /// The to write to. + /// The 8-byte unsigned integer to write. + /// A standard or custom numeric format string. + /// An object that supplies culture-specific formatting information. + /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. + /// is . + /// The is closed. + /// An I/O error occurs. + [CLSCompliant(false)] + public static void WriteNoAlloc(this TextWriter writer, + ulong value, + ReadOnlySpan 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 buffer = stackalloc char[System.Math.Max(digitCount, 100)]; + if (value.TryFormat(buffer, out int charsWritten, format, formatProvider)) + { + Span truncated = buffer[..charsWritten]; + for (var index = 0; index < truncated.Length; index++) + { + writer.Write(truncated[index]); + } + } + else + { + writer.Write(value.ToString(format.ToString(), formatProvider)); + } + } +} diff --git a/X10D/src/IO/TextWriterExtensions.cs b/X10D/src/IO/TextWriterExtensions.cs index 7d1b3c5..f590205 100644 --- a/X10D/src/IO/TextWriterExtensions.cs +++ b/X10D/src/IO/TextWriterExtensions.cs @@ -1,685 +1,8 @@ -using System.Globalization; -using X10D.Math; - -namespace X10D.IO; +namespace X10D.IO; /// /// IO-related extension methods for . /// -public static class TextWriterExtensions +public static partial class TextWriterExtensions { - /// - /// Writes the text representation of a 4-byte signed integer to the text stream, without allocating a string. - /// - /// The to write to. - /// The 4-byte signed integer to write. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - 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); - } - - /// - /// Writes the text representation of a 4-byte signed integer to the text stream, without allocating a string. - /// - /// The to write to. - /// The 4-byte signed integer to write. - /// A standard or custom numeric format string. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - public static void WriteNoAlloc(this TextWriter writer, int value, ReadOnlySpan 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); - } - - /// - /// Writes the text representation of a 4-byte signed integer to the text stream, without allocating a string. - /// - /// The to write to. - /// The 4-byte signed integer to write. - /// A standard or custom numeric format string. - /// An object that supplies culture-specific formatting information. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - public static void WriteNoAlloc(this TextWriter writer, int value, ReadOnlySpan 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 buffer = stackalloc char[System.Math.Max(value < 0 ? digitCount + 1 : digitCount, 1000)]; - if (value.TryFormat(buffer, out int charsWritten, format, formatProvider)) - { - Span truncated = buffer[..charsWritten]; - for (var index = 0; index < truncated.Length; index++) - { - writer.Write(truncated[index]); - } - } - else - { - writer.Write(value.ToString(format.ToString(), formatProvider)); - } - } - - /// - /// Writes the text representation of a 4-byte unsigned integer to the text stream, without allocating a string. - /// - /// The to write to. - /// The 4-byte unsigned integer to write. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - [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); - } - - /// - /// Writes the text representation of a 4-byte unsigned integer to the text stream, without allocating a string. - /// - /// The to write to. - /// The 4-byte unsigned integer to write. - /// A standard or custom numeric format string. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - [CLSCompliant(false)] - public static void WriteNoAlloc(this TextWriter writer, uint value, ReadOnlySpan 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); - } - - /// - /// Writes the text representation of a 4-byte unsigned integer to the text stream, without allocating a string. - /// - /// The to write to. - /// The 4-byte unsigned integer to write. - /// A standard or custom numeric format string. - /// An object that supplies culture-specific formatting information. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - [CLSCompliant(false)] - public static void WriteNoAlloc(this TextWriter writer, - uint value, - ReadOnlySpan 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 buffer = stackalloc char[System.Math.Max(digitCount, 1000)]; - if (value.TryFormat(buffer, out int charsWritten, format, formatProvider)) - { - Span truncated = buffer[..charsWritten]; - for (var index = 0; index < truncated.Length; index++) - { - writer.Write(truncated[index]); - } - } - else - { - writer.Write(value.ToString(format.ToString(), formatProvider)); - } - } - - /// - /// Writes the text representation of a 8-byte signed integer to the text stream, without allocating a string. - /// - /// The to write to. - /// The 8-byte signed integer to write. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - 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); - } - - /// - /// Writes the text representation of a 8-byte signed integer to the text stream, without allocating a string. - /// - /// The to write to. - /// The 8-byte signed integer to write. - /// A standard or custom numeric format string. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - public static void WriteNoAlloc(this TextWriter writer, long value, ReadOnlySpan 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); - } - - /// - /// Writes the text representation of a 8-byte signed integer to the text stream, without allocating a string. - /// - /// The to write to. - /// The 8-byte signed integer to write. - /// A standard or custom numeric format string. - /// An object that supplies culture-specific formatting information. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - public static void WriteNoAlloc(this TextWriter writer, - long value, - ReadOnlySpan 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 buffer = stackalloc char[System.Math.Max(value < 0 ? digitCount + 1 : digitCount, 1000)]; - if (value.TryFormat(buffer, out int charsWritten, format, formatProvider)) - { - Span truncated = buffer[..charsWritten]; - for (var index = 0; index < truncated.Length; index++) - { - writer.Write(truncated[index]); - } - } - else - { - writer.Write(value.ToString(format.ToString(), formatProvider)); - } - } - - /// - /// Writes the text representation of a 8-byte unsigned integer to the text stream, without allocating a string. - /// - /// The to write to. - /// The 8-byte unsigned integer to write. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - [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); - } - - /// - /// Writes the text representation of a 8-byte signed integer to the text stream, without allocating a string. - /// - /// The to write to. - /// The 8-byte unsigned integer to write. - /// A standard or custom numeric format string. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - [CLSCompliant(false)] - public static void WriteNoAlloc(this TextWriter writer, ulong value, ReadOnlySpan 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); - } - - /// - /// Writes the text representation of a 8-byte signed integer to the text stream, without allocating a string. - /// - /// The to write to. - /// The 8-byte unsigned integer to write. - /// A standard or custom numeric format string. - /// An object that supplies culture-specific formatting information. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - [CLSCompliant(false)] - public static void WriteNoAlloc(this TextWriter writer, - ulong value, - ReadOnlySpan 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 buffer = stackalloc char[System.Math.Max(digitCount, 1000)]; - if (value.TryFormat(buffer, out int charsWritten, format, formatProvider)) - { - Span truncated = buffer[..charsWritten]; - for (var index = 0; index < truncated.Length; index++) - { - writer.Write(truncated[index]); - } - } - else - { - writer.Write(value.ToString(format.ToString(), formatProvider)); - } - } - - /// - /// Writes the text representation of a 4-byte signed integer to the text stream, followed by a line terminator, without - /// allocating a string. - /// - /// The to write to. - /// The 4-byte signed integer to write. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - 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); - } - - /// - /// Writes the text representation of a 4-byte signed integer to the text stream, followed by a line terminator, without - /// allocating a string. - /// - /// The to write to. - /// The 4-byte signed integer to write. - /// A standard or custom numeric format string. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - public static void WriteLineNoAlloc(this TextWriter writer, int value, ReadOnlySpan 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); - } - - /// - /// Writes the text representation of a 4-byte signed integer to the text stream, followed by a line terminator, without - /// allocating a string. - /// - /// The to write to. - /// The 4-byte signed integer to write. - /// A standard or custom numeric format string. - /// An object that supplies culture-specific formatting information. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - public static void WriteLineNoAlloc(this TextWriter writer, int value, ReadOnlySpan 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(); - } - - /// - /// Writes the text representation of a 4-byte unsigned integer to the text stream, followed by a line terminator, without - /// allocating a string. - /// - /// The to write to. - /// The 4-byte unsigned integer to write. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - [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); - } - - /// - /// Writes the text representation of a 4-byte unsigned integer to the text stream, followed by a line terminator, without - /// allocating a string. - /// - /// The to write to. - /// The 4-byte unsigned integer to write. - /// A standard or custom numeric format string. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - [CLSCompliant(false)] - public static void WriteLineNoAlloc(this TextWriter writer, uint value, ReadOnlySpan 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); - } - - /// - /// Writes the text representation of a 4-byte unsigned integer to the text stream, followed by a line terminator, without - /// allocating a string. - /// - /// The to write to. - /// The 4-byte unsigned integer to write. - /// A standard or custom numeric format string. - /// An object that supplies culture-specific formatting information. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - [CLSCompliant(false)] - public static void WriteLineNoAlloc(this TextWriter writer, - uint value, - ReadOnlySpan 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(); - } - - /// - /// Writes the text representation of a 8-byte signed integer to the text stream, followed by a line terminator, without - /// allocating a string. - /// - /// The to write to. - /// The 8-byte signed integer to write. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - 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); - } - - /// - /// Writes the text representation of a 8-byte signed integer to the text stream, followed by a line terminator, without - /// allocating a string. - /// - /// The to write to. - /// The 8-byte signed integer to write. - /// A standard or custom numeric format string. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - public static void WriteLineNoAlloc(this TextWriter writer, long value, ReadOnlySpan 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); - } - - /// - /// Writes the text representation of a 8-byte signed integer to the text stream, followed by a line terminator, without - /// allocating a string. - /// - /// The to write to. - /// The 8-byte signed integer to write. - /// A standard or custom numeric format string. - /// An object that supplies culture-specific formatting information. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - public static void WriteLineNoAlloc(this TextWriter writer, - long value, - ReadOnlySpan 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(); - } - - /// - /// Writes the text representation of a 8-byte unsigned integer to the text stream, followed by a line terminator, without - /// allocating a string. - /// - /// The to write to. - /// The 8-byte unsigned integer to write. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - [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); - } - - /// - /// Writes the text representation of a 8-byte signed integer to the text stream, followed by a line terminator, without - /// allocating a string. - /// - /// The to write to. - /// The 8-byte unsigned integer to write. - /// A standard or custom numeric format string. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - [CLSCompliant(false)] - public static void WriteLineNoAlloc(this TextWriter writer, ulong value, ReadOnlySpan 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); - } - - /// - /// Writes the text representation of a 8-byte signed integer to the text stream, followed by a line terminator, without - /// allocating a string. - /// - /// The to write to. - /// The 8-byte unsigned integer to write. - /// A standard or custom numeric format string. - /// An object that supplies culture-specific formatting information. - /// This method may still allocate if the integer is too large to fit in a stack-allocated buffer. - /// is . - /// The is closed. - /// An I/O error occurs. - [CLSCompliant(false)] - public static void WriteLineNoAlloc(this TextWriter writer, - ulong value, - ReadOnlySpan 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(); - } }