mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-09 16:35:42 +00:00
feat: add TextWriter.WriteNoAlloc/WriteLineNoAlloc
Allows writing of integer types without allocating a string.
This commit is contained in:
parent
ce35f8676e
commit
9d26f3da60
@ -12,6 +12,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- X10D: Added math-related extension methods for `BigInteger`.
|
||||
- X10D: Added `Span<T>.Replace(T, T)`.
|
||||
- X10D: Added `CountDigits` for integer types.
|
||||
- X10D: Added `TextWriter.WriteNoAlloc(int[, ReadOnlySpan<char>[, IFormatProvider]])`.
|
||||
- X10D: Added `TextWriter.WriteNoAlloc(uint[, ReadOnlySpan<char>[, IFormatProvider]])`.
|
||||
- X10D: Added `TextWriter.WriteNoAlloc(long[, ReadOnlySpan<char>[, IFormatProvider]])`.
|
||||
- X10D: Added `TextWriter.WriteNoAlloc(ulong[, ReadOnlySpan<char>[, IFormatProvider]])`.
|
||||
- X10D: Added `TextWriter.WriteLineNoAlloc(int[, ReadOnlySpan<char>[, IFormatProvider]])`.
|
||||
- X10D: Added `TextWriter.WriteLineNoAlloc(uint[, ReadOnlySpan<char>[, IFormatProvider]])`.
|
||||
- X10D: Added `TextWriter.WriteLineNoAlloc(long[, ReadOnlySpan<char>[, IFormatProvider]])`.
|
||||
- X10D: Added `TextWriter.WriteLineNoAlloc(ulong[, ReadOnlySpan<char>[, IFormatProvider]])`.
|
||||
|
||||
### Changed
|
||||
- X10D: `DateTime.Age(DateTime)` and `DateTimeOffset.Age(DateTimeOffset)` parameter renamed from `asOf` to `referenceDate`.
|
||||
|
131
X10D.Tests/src/IO/TextWriterTests.Int32.cs
Normal file
131
X10D.Tests/src/IO/TextWriterTests.Int32.cs
Normal file
@ -0,0 +1,131 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using X10D.IO;
|
||||
|
||||
namespace X10D.Tests.IO;
|
||||
|
||||
public partial class TextWriterTests
|
||||
{
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldThrowArgumentNullException_GivenInt32_AndNullWriter()
|
||||
{
|
||||
TextWriter writer = null!;
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteNoAlloc(420));
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteNoAlloc(420, "N0"));
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteNoAlloc(420, "N0", null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldThrowObjectDisposedException_GivenInt32_AndDisposedStream()
|
||||
{
|
||||
var stream = new MemoryStream();
|
||||
var writer = new StreamWriter(stream, Encoding.UTF8);
|
||||
writer.Dispose();
|
||||
stream.Dispose();
|
||||
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteNoAlloc(420));
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteNoAlloc(420, "N0"));
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteNoAlloc(420, "N0", null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldThrowArgumentNullException_GivenInt32_AndNullWriter()
|
||||
{
|
||||
TextWriter writer = null!;
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteLineNoAlloc(420));
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteLineNoAlloc(420, "N0"));
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteLineNoAlloc(420, "N0", null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldThrowObjectDisposedException_GivenInt32_AndDisposedStream()
|
||||
{
|
||||
var stream = new MemoryStream();
|
||||
var writer = new StreamWriter(stream, Encoding.UTF8);
|
||||
writer.Dispose();
|
||||
stream.Dispose();
|
||||
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteLineNoAlloc(420));
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteLineNoAlloc(420, "N0"));
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteLineNoAlloc(420, "N0", null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldWriteTextValue_GivenInt32()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteNoAlloc(420);
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
const string expected = "420";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldWriteTextValue_GivenInt32_AndFormatString()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteNoAlloc(420, "N0");
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
const string expected = "420";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldWriteTextValue_GivenInt32_AndFormatString_AndCultureInfo()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteNoAlloc(420, "N0", CultureInfo.CurrentCulture);
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
const string expected = "420";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldWriteTextValue_GivenInt32()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteLineNoAlloc(420);
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
var expected = $"420{Environment.NewLine}";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldWriteTextValue_GivenInt32_AndFormatString()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteLineNoAlloc(420, "N0");
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
var expected = $"420{Environment.NewLine}";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldWriteTextValue_GivenInt32_AndFormatString_AndCultureInfo()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteLineNoAlloc(420, "N0", CultureInfo.CurrentCulture);
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
var expected = $"420{Environment.NewLine}";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
131
X10D.Tests/src/IO/TextWriterTests.Int64.cs
Normal file
131
X10D.Tests/src/IO/TextWriterTests.Int64.cs
Normal file
@ -0,0 +1,131 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using X10D.IO;
|
||||
|
||||
namespace X10D.Tests.IO;
|
||||
|
||||
public partial class TextWriterTests
|
||||
{
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldThrowArgumentNullException_GivenInt64_AndNullWriter()
|
||||
{
|
||||
TextWriter writer = null!;
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteNoAlloc(420L));
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteNoAlloc(420L, "N0"));
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteNoAlloc(420L, "N0", null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldThrowObjectDisposedException_GivenInt64_AndDisposedStream()
|
||||
{
|
||||
var stream = new MemoryStream();
|
||||
var writer = new StreamWriter(stream, Encoding.UTF8);
|
||||
writer.Dispose();
|
||||
stream.Dispose();
|
||||
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteNoAlloc(420L));
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteNoAlloc(420L, "N0"));
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteNoAlloc(420L, "N0", null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldThrowArgumentNullException_GivenInt64_AndNullWriter()
|
||||
{
|
||||
TextWriter writer = null!;
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteLineNoAlloc(420L));
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteLineNoAlloc(420L, "N0"));
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteLineNoAlloc(420L, "N0", null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldThrowObjectDisposedException_GivenInt64_AndDisposedStream()
|
||||
{
|
||||
var stream = new MemoryStream();
|
||||
var writer = new StreamWriter(stream, Encoding.UTF8);
|
||||
writer.Dispose();
|
||||
stream.Dispose();
|
||||
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteLineNoAlloc(420L));
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteLineNoAlloc(420L, "N0"));
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteLineNoAlloc(420L, "N0", null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldWriteTextValue_GivenInt64()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteNoAlloc(420L);
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
const string expected = "420";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldWriteTextValue_GivenInt64_AndFormatString()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteNoAlloc(420L, "N0");
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
const string expected = "420";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldWriteTextValue_GivenInt64_AndFormatString_AndCultureInfo()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteNoAlloc(420L, "N0", CultureInfo.CurrentCulture);
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
const string expected = "420";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldWriteTextValue_GivenInt64()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteLineNoAlloc(420L);
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
var expected = $"420{Environment.NewLine}";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldWriteTextValue_GivenInt64_AndFormatString()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteLineNoAlloc(420L, "N0");
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
var expected = $"420{Environment.NewLine}";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldWriteTextValue_GivenInt64_AndFormatString_AndCultureInfo()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteLineNoAlloc(420L, "N0", CultureInfo.CurrentCulture);
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
var expected = $"420{Environment.NewLine}";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
131
X10D.Tests/src/IO/TextWriterTests.UInt32.cs
Normal file
131
X10D.Tests/src/IO/TextWriterTests.UInt32.cs
Normal file
@ -0,0 +1,131 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using X10D.IO;
|
||||
|
||||
namespace X10D.Tests.IO;
|
||||
|
||||
public partial class TextWriterTests
|
||||
{
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldThrowArgumentNullException_GivenUInt32_AndNullWriter()
|
||||
{
|
||||
TextWriter writer = null!;
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteNoAlloc(420U));
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteNoAlloc(420U, "N0"));
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteNoAlloc(420U, "N0", null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldThrowObjectDisposedException_GivenUInt32_AndDisposedStream()
|
||||
{
|
||||
var stream = new MemoryStream();
|
||||
var writer = new StreamWriter(stream, Encoding.UTF8);
|
||||
writer.Dispose();
|
||||
stream.Dispose();
|
||||
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteNoAlloc(420U));
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteNoAlloc(420U, "N0"));
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteNoAlloc(420U, "N0", null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldThrowArgumentNullException_GivenUInt32_AndNullWriter()
|
||||
{
|
||||
TextWriter writer = null!;
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteLineNoAlloc(420U));
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteLineNoAlloc(420U, "N0"));
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteLineNoAlloc(420U, "N0", null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldThrowObjectDisposedException_GivenUInt32_AndDisposedStream()
|
||||
{
|
||||
var stream = new MemoryStream();
|
||||
var writer = new StreamWriter(stream, Encoding.UTF8);
|
||||
writer.Dispose();
|
||||
stream.Dispose();
|
||||
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteLineNoAlloc(420U));
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteLineNoAlloc(420U, "N0"));
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteLineNoAlloc(420U, "N0", null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldWriteTextValue_GivenUInt32()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteNoAlloc(420U);
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
const string expected = "420";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldWriteTextValue_GivenUInt32_AndFormatString()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteNoAlloc(420U, "N0");
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
const string expected = "420";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldWriteTextValue_GivenUInt32_AndFormatString_AndCultureInfo()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteNoAlloc(420U, "N0", CultureInfo.CurrentCulture);
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
const string expected = "420";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldWriteTextValue_GivenUInt32()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteLineNoAlloc(420U);
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
var expected = $"420{Environment.NewLine}";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldWriteTextValue_GivenUInt32_AndFormatString()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteLineNoAlloc(420U, "N0");
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
var expected = $"420{Environment.NewLine}";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldWriteTextValue_GivenUInt32_AndFormatString_AndCultureInfo()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteLineNoAlloc(420U, "N0", CultureInfo.CurrentCulture);
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
var expected = $"420{Environment.NewLine}";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
131
X10D.Tests/src/IO/TextWriterTests.UInt64.cs
Normal file
131
X10D.Tests/src/IO/TextWriterTests.UInt64.cs
Normal file
@ -0,0 +1,131 @@
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using X10D.IO;
|
||||
|
||||
namespace X10D.Tests.IO;
|
||||
|
||||
public partial class TextWriterTests
|
||||
{
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldThrowArgumentNullException_GivenUInt64_AndNullWriter()
|
||||
{
|
||||
TextWriter writer = null!;
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteNoAlloc(420UL));
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteNoAlloc(420UL, "N0"));
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteNoAlloc(420UL, "N0", null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldThrowObjectDisposedException_GivenUInt64_AndDisposedStream()
|
||||
{
|
||||
var stream = new MemoryStream();
|
||||
var writer = new StreamWriter(stream, Encoding.UTF8);
|
||||
writer.Dispose();
|
||||
stream.Dispose();
|
||||
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteNoAlloc(420UL));
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteNoAlloc(420UL, "N0"));
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteNoAlloc(420UL, "N0", null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldThrowArgumentNullException_GivenUInt64_AndNullWriter()
|
||||
{
|
||||
TextWriter writer = null!;
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteLineNoAlloc(420UL));
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteLineNoAlloc(420UL, "N0"));
|
||||
Assert.Throws<ArgumentNullException>(() => writer.WriteLineNoAlloc(420UL, "N0", null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldThrowObjectDisposedException_GivenUInt64_AndDisposedStream()
|
||||
{
|
||||
var stream = new MemoryStream();
|
||||
var writer = new StreamWriter(stream, Encoding.UTF8);
|
||||
writer.Dispose();
|
||||
stream.Dispose();
|
||||
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteLineNoAlloc(420UL));
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteLineNoAlloc(420UL, "N0"));
|
||||
Assert.Throws<ObjectDisposedException>(() => writer.WriteLineNoAlloc(420UL, "N0", null));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldWriteTextValue_GivenUInt64()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteNoAlloc(420UL);
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
const string expected = "420";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldWriteTextValue_GivenUInt64_AndFormatString()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteNoAlloc(420UL, "N0");
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
const string expected = "420";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteNoAlloc_ShouldWriteTextValue_GivenUInt64_AndFormatString_AndCultureInfo()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteNoAlloc(420UL, "N0", CultureInfo.CurrentCulture);
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
const string expected = "420";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldWriteTextValue_GivenUInt64()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteLineNoAlloc(420UL);
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
var expected = $"420{Environment.NewLine}";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldWriteTextValue_GivenUInt64_AndFormatString()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteLineNoAlloc(420UL, "N0");
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
var expected = $"420{Environment.NewLine}";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void WriteLineNoAlloc_ShouldWriteTextValue_GivenUInt64_AndFormatString_AndCultureInfo()
|
||||
{
|
||||
Assert.That(_stream.Length, Is.Zero);
|
||||
_writer.WriteLineNoAlloc(420UL, "N0", CultureInfo.CurrentCulture);
|
||||
_writer.Flush();
|
||||
|
||||
string actual = Encoding.UTF8.GetString(_stream.ToArray());
|
||||
var expected = $"420{Environment.NewLine}";
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
46
X10D.Tests/src/IO/TextWriterTests.cs
Normal file
46
X10D.Tests/src/IO/TextWriterTests.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace X10D.Tests.IO;
|
||||
|
||||
[TestFixture]
|
||||
public partial class TextWriterTests
|
||||
{
|
||||
private MemoryStream _stream = null!;
|
||||
private StreamWriter _writer = null!;
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void OneTimeSetup()
|
||||
{
|
||||
_stream = new MemoryStream();
|
||||
_writer = new StreamWriter(_stream, Encoding.UTF8);
|
||||
_writer.Write(' ');
|
||||
_writer.Flush();
|
||||
_stream.SetLength(0);
|
||||
_stream.Position = 0;
|
||||
|
||||
Trace.Listeners.Add(new ConsoleTraceListener());
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void OneTimeTearDown()
|
||||
{
|
||||
_writer.Dispose();
|
||||
_stream.Dispose();
|
||||
|
||||
Trace.Flush();
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_stream.SetLength(0);
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void TearDown()
|
||||
{
|
||||
_stream.SetLength(0);
|
||||
}
|
||||
}
|
685
X10D/src/IO/TextWriterExtensions.cs
Normal file
685
X10D/src/IO/TextWriterExtensions.cs
Normal file
@ -0,0 +1,685 @@
|
||||
using System.Globalization;
|
||||
using X10D.Math;
|
||||
|
||||
namespace X10D.IO;
|
||||
|
||||
/// <summary>
|
||||
/// IO-related extension methods for <see cref="TextWriter" />.
|
||||
/// </summary>
|
||||
public static 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user