diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter.cs index ffbe09f43c9..50e579bc78b 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter.cs @@ -12,8 +12,8 @@ public ref partial struct BufferWriter Span _buffer; int _written; - public ReadOnlySpan NewLine; - public Func> Enlarge; + public ReadOnlySpan NewLine { get; set; } + public Func> Enlarge { get; set; } static byte[] s_defaultNewline = new byte[] { (byte)'\n' }; diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriterT.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriterT.cs index 36a944dc0e5..617186fe9dd 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriterT.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriterT.cs @@ -19,7 +19,7 @@ public BufferWriter(T output) _span = output.GetSpan(); } - public ReadOnlySpan NewLine; + public ReadOnlySpan NewLine { get; set; } public Span Buffer => _span; diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriterT_ints.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriterT_ints.cs index db82a67d754..48b6fda1fc3 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriterT_ints.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriterT_ints.cs @@ -9,6 +9,18 @@ namespace System.Buffers.Writer { public ref partial struct BufferWriter where T : IBufferWriter { + #region Int32 + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Write(int value) + { + int written; + while (!Utf8Formatter.TryFormat(value, Buffer, out written, default)) + { + Enlarge(); + } + Advance(written); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Write(int value, StandardFormat format = default) { @@ -20,6 +32,27 @@ public void Write(int value, StandardFormat format = default) Advance(written); } + public void Write(int value, TransformationFormat format) + { + int written; + while (true) + { + Span buffer = Buffer; + while (!Utf8Formatter.TryFormat(value, Buffer, out written, format.Format)) + { + Enlarge(); + } + if (format.TryTransform(buffer, ref written)) + { + Advance(written); + return; + } + Enlarge(); + } + } + #endregion + + #region UInt64 [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Write(ulong value, StandardFormat format = default) { @@ -30,5 +63,6 @@ public void Write(ulong value, StandardFormat format = default) } Advance(written); } + #endregion } } diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriterT_writable.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriterT_writable.cs index f05a756f9c3..3c3e00cbe5e 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriterT_writable.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriterT_writable.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Buffers.Text; using System.Runtime.CompilerServices; namespace System.Buffers.Writer diff --git a/tests/System.Buffers.ReaderWriter.Tests/BasicUnitTests.cs b/tests/System.Buffers.ReaderWriter.Tests/BasicUnitTests.cs index 09c1998fb67..95b06ae60fd 100644 --- a/tests/System.Buffers.ReaderWriter.Tests/BasicUnitTests.cs +++ b/tests/System.Buffers.ReaderWriter.Tests/BasicUnitTests.cs @@ -99,6 +99,19 @@ public void WriteLineUtf8String() var result = _sink.ToString(); Assert.Equal("hello worldXY!XY", result); } + + [Fact] + public void WriteInt32Transformed() + { + TransformationFormat widen = new TransformationFormat(new AsciiToUtf16()); + _sink.Reset(); + var writer = BufferWriter.Create(_sink); + writer.Write(255, widen); + writer.Flush(); + + var result = _sink.ToStringAssumingUtf16Buffer(); + Assert.Equal("255", result); + } } class Sink : IBufferWriter @@ -125,10 +138,17 @@ public Memory GetMemory(int sizeHint = 0) public Span GetSpan(int sizeHint = 0) => _buffer.AsSpan(_written, _buffer.Length - _written); + public Span WrittenBytes => _buffer.AsSpan(0, _written); + public override string ToString() { return Encoding.UTF8.GetString(_buffer, 0, _written); } + + public string ToStringAssumingUtf16Buffer() + { + return Encoding.Unicode.GetString(_buffer, 0, _written); + } } static class DateHeader @@ -170,4 +190,24 @@ public static void SetDateValues(DateTimeOffset value) } } } + + class AsciiToUtf16 : IBufferTransformation + { + public OperationStatus Execute(ReadOnlySpan input, Span output, out int consumed, out int written) + { + throw new NotImplementedException(); + } + + public OperationStatus Transform(Span buffer, int dataLength, out int written) + { + written = dataLength * 2; + if (buffer.Length < written) return OperationStatus.DestinationTooSmall; + for(int i = written - 1; i > 0; i-=2) + { + buffer[i] = 0; + buffer[i - 1] = buffer[i / 2]; + } + return OperationStatus.Done; + } + } }