diff --git a/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/TcpConnectionFormatter.cs b/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/TcpConnectionFormatter.cs index b1b45fe0252..b8c9cbc0a51 100644 --- a/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/TcpConnectionFormatter.cs +++ b/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/TcpConnectionFormatter.cs @@ -30,12 +30,10 @@ public TcpConnectionFormatter(TcpConnection connection, int bufferSize = 4096) public SymbolTable SymbolTable => SymbolTable.InvariantUtf8; - public Span Buffer { - get { - var buffer = _buffer.AsSpan().Slice(ChunkPrefixSize + _written); - if (buffer.Length > 2) return buffer.Slice(0, buffer.Length - 2); - return Span.Empty; - } + public Span GetSpan() { + var buffer = _buffer.AsSpan().Slice(ChunkPrefixSize + _written); + if (buffer.Length > 2) return buffer.Slice(0, buffer.Length - 2); + return Span.Empty; } public void Advance(int bytes) diff --git a/src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs b/src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs index 244f67c88ce..4c3dfca3d0e 100644 --- a/src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs +++ b/src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs @@ -137,7 +137,7 @@ public static void Pipe(this IBufferOperation transformation, ReadOnlyBytes sour Position poisition = default; while (source.TryGet(ref poisition, out var sourceBuffer)) { - Span outputSpan = destination.Buffer; + Span outputSpan = destination.GetSpan(); ReadOnlySpan sourceSpan = sourceBuffer.Span; if (!remainder.IsEmpty) @@ -160,7 +160,7 @@ public static void Pipe(this IBufferOperation transformation, ReadOnlyBytes sour if (status == OperationStatus.DestinationTooSmall) { destination.Enlarge(); // output buffer is too small - outputSpan = destination.Buffer; + outputSpan = destination.GetSpan(); if (outputSpan.Length - bytesWritten < 3) { @@ -186,7 +186,7 @@ public static void Pipe(this IBufferOperation transformation, ReadOnlyBytes sour afterMergeSlice = bytesConsumed - remainder.Length; remainder = Span.Empty; destination.Advance(bytesWritten); - outputSpan = destination.Buffer; + outputSpan = destination.GetSpan(); } } @@ -202,7 +202,7 @@ public static void Pipe(this IBufferOperation transformation, ReadOnlyBytes sour if (result == OperationStatus.DestinationTooSmall) { destination.Enlarge(); // output buffer is too small - outputSpan = destination.Buffer; + outputSpan = destination.GetSpan(); if (outputSpan.Length - written < 3) { return; // no more output space, user decides what to do. diff --git a/src/System.Buffers.Experimental/System/Buffers/Text/BufferWriter_sequence.cs b/src/System.Buffers.Experimental/System/Buffers/Text/BufferWriter_sequence.cs index d9ff5b7a320..21d7a33df50 100644 --- a/src/System.Buffers.Experimental/System/Buffers/Text/BufferWriter_sequence.cs +++ b/src/System.Buffers.Experimental/System/Buffers/Text/BufferWriter_sequence.cs @@ -17,14 +17,14 @@ namespace System.Buffers.Text public BufferWriter(TOutput output) { _output = output; - _buffer = _output.Buffer; + _buffer = _output.GetSpan(); _written = 0; } public void Flush() { _output.Advance(_written); - _buffer = _output.Buffer; + _buffer = _output.GetSpan(); _written = 0; } @@ -219,7 +219,7 @@ private Span Enlarge(int desiredBufferSize = 0) if (_buffer.Length > before) return _buffer; _output.Enlarge(desiredBufferSize); - _buffer = _output.Buffer; + _buffer = _output.GetSpan(); Debug.Assert(_written == 0); // ensure still 0 return _buffer; } diff --git a/src/System.Buffers.Primitives/System/Buffers/IOutput.cs b/src/System.Buffers.Primitives/System/Buffers/IOutput.cs index 948b8b7e2b9..4f552282c1b 100644 --- a/src/System.Buffers.Primitives/System/Buffers/IOutput.cs +++ b/src/System.Buffers.Primitives/System/Buffers/IOutput.cs @@ -5,7 +5,7 @@ namespace System.Buffers { public interface IOutput { - Span Buffer { get; } + Span GetSpan(); void Advance(int bytes); /// desiredBufferLength == 0 means "i don't care" diff --git a/src/System.Collections.Sequences/System/Collections/Sequences/Position.cs b/src/System.Collections.Sequences/System/Collections/Sequences/Position.cs index 2259f926787..ba2c5a495c2 100644 --- a/src/System.Collections.Sequences/System/Collections/Sequences/Position.cs +++ b/src/System.Collections.Sequences/System/Collections/Sequences/Position.cs @@ -33,8 +33,7 @@ public static Position Create(T item, int index = 0) where T : class public static bool operator ==(Position left, Position right) => left.Index == right.Index && left._item == right._item; public static bool operator !=(Position left, Position right) => left.Index != right.Index || left._item != right._item; - public static Position operator +(Position position, int offset) => position.Offset(offset); - public static Position operator -(Position position, int offset) => position.Offset(-offset); + public static Position operator +(Position position, int offset) => new Position(position.Index + offset, position._item); [EditorBrowsable(EditorBrowsableState.Never)] public bool Equals(Position position) => this == position; @@ -56,7 +55,5 @@ private Position(int index, object item) _item = item; _index = index; } - - private Position Offset(int index) => new Position(Index + index, _item); } } diff --git a/src/System.Collections.Sequences/System/Collections/Sequences/ResizableArray.cs b/src/System.Collections.Sequences/System/Collections/Sequences/ResizableArray.cs index acd573082bb..4f5fd755363 100644 --- a/src/System.Collections.Sequences/System/Collections/Sequences/ResizableArray.cs +++ b/src/System.Collections.Sequences/System/Collections/Sequences/ResizableArray.cs @@ -116,7 +116,7 @@ public bool TryGet(ref Position position, out T item, bool advance = true) } item = default; - position = Position.End; + position = default; return false; } diff --git a/src/System.IO.Pipelines.Extensions/WritableBufferOutput.cs b/src/System.IO.Pipelines.Extensions/WritableBufferOutput.cs index 0e56750a5ec..5444dc35b0b 100644 --- a/src/System.IO.Pipelines.Extensions/WritableBufferOutput.cs +++ b/src/System.IO.Pipelines.Extensions/WritableBufferOutput.cs @@ -14,7 +14,7 @@ public WritableBufferOutput(WritableBuffer writer) : this() _writer = writer; } - public Span Buffer => _writer.Buffer.Span; + public Span GetSpan() => _writer.Buffer.Span; public void Advance(int bytes) { @@ -29,7 +29,8 @@ public void Enlarge(int desiredBufferLength = 0) private int ComputeActualSize(int desiredBufferLength) { if (desiredBufferLength < 256) desiredBufferLength = 256; - if (desiredBufferLength < Buffer.Length) desiredBufferLength = Buffer.Length * 2; + var length = GetSpan().Length; + if (desiredBufferLength < length) desiredBufferLength = length * 2; return desiredBufferLength; } } diff --git a/src/System.IO.Pipelines.Text.Primitives/PipelineTextOutput.cs b/src/System.IO.Pipelines.Text.Primitives/PipelineTextOutput.cs index 81165e1e3bc..9d5eca9a18c 100644 --- a/src/System.IO.Pipelines.Text.Primitives/PipelineTextOutput.cs +++ b/src/System.IO.Pipelines.Text.Primitives/PipelineTextOutput.cs @@ -21,14 +21,10 @@ public PipelineTextOutput(IPipeWriter writer, SymbolTable symbolTable) public SymbolTable SymbolTable { get; } - public Span Buffer + public Span GetSpan() { - get - { - EnsureBuffer(); - - return _writableBuffer.Buffer.Span; - } + EnsureBuffer(); + return _writableBuffer.Buffer.Span; } public void Advance(int bytes) diff --git a/src/System.Text.Formatting/System/Text/Formatting/CompositeFormat.cs b/src/System.Text.Formatting/System/Text/Formatting/CompositeFormat.cs index 837280207fb..3aabd333d92 100644 --- a/src/System.Text.Formatting/System/Text/Formatting/CompositeFormat.cs +++ b/src/System.Text.Formatting/System/Text/Formatting/CompositeFormat.cs @@ -128,12 +128,12 @@ public static void Format(this TFormatter format // TODO: this should be removed and an ability to append substrings should be added static void Append(this TFormatter formatter, string whole, int index, int count) where TFormatter : ITextOutput { - var buffer = formatter.Buffer; + var buffer = formatter.GetSpan(); var maxBytes = count << 4; // this is the worst case, i.e. 4 bytes per char while(buffer.Length < maxBytes) { formatter.Enlarge(maxBytes); - buffer = formatter.Buffer; + buffer = formatter.GetSpan(); } // this should be optimized using fixed pointer to substring, but I will wait with this till we design proper substring diff --git a/src/System.Text.Formatting/System/Text/Formatting/Formatters/ArrayFormatter.cs b/src/System.Text.Formatting/System/Text/Formatting/Formatters/ArrayFormatter.cs index 4cf46e292db..bb2582e3655 100644 --- a/src/System.Text.Formatting/System/Text/Formatting/Formatters/ArrayFormatter.cs +++ b/src/System.Text.Formatting/System/Text/Formatting/Formatters/ArrayFormatter.cs @@ -32,7 +32,7 @@ public void Clear() { public SymbolTable SymbolTable => _symbolTable; - public Span Buffer => Free.AsSpan(); + public Span GetSpan() => Free.AsSpan(); public void Enlarge(int desiredBufferLength = 0) { diff --git a/src/System.Text.Formatting/System/Text/Formatting/Formatters/OutputFormatter.cs b/src/System.Text.Formatting/System/Text/Formatting/Formatters/OutputFormatter.cs index c02a1c99dad..ad9257cdac7 100644 --- a/src/System.Text.Formatting/System/Text/Formatting/Formatters/OutputFormatter.cs +++ b/src/System.Text.Formatting/System/Text/Formatting/Formatters/OutputFormatter.cs @@ -22,7 +22,7 @@ public OutputFormatter(TOutput output) : this(output, SymbolTable.InvariantUtf8) { } - public Span Buffer => _output.Buffer; + public Span GetSpan() => _output.GetSpan(); public SymbolTable SymbolTable => _symbolTable; diff --git a/src/System.Text.Formatting/System/Text/Formatting/Formatters/SequenceFormatter.cs b/src/System.Text.Formatting/System/Text/Formatting/Formatters/SequenceFormatter.cs index a97a1b29cc9..a604940a875 100644 --- a/src/System.Text.Formatting/System/Text/Formatting/Formatters/SequenceFormatter.cs +++ b/src/System.Text.Formatting/System/Text/Formatting/Formatters/SequenceFormatter.cs @@ -34,12 +34,8 @@ public SequenceFormatter(TSequence buffers, SymbolTable symbolTable) _previousWrittenBytes = -1; } - Span IOutput.Buffer - { - get { - return Current.Span.Slice(_currentWrittenBytes); - } - } + Span IOutput.GetSpan() + => Current.Span.Slice(_currentWrittenBytes); private Memory Current { get { diff --git a/src/System.Text.Formatting/System/Text/Formatting/Formatters/StreamFormatter.cs b/src/System.Text.Formatting/System/Text/Formatting/Formatters/StreamFormatter.cs index c409c796d04..79d548f0d34 100644 --- a/src/System.Text.Formatting/System/Text/Formatting/Formatters/StreamFormatter.cs +++ b/src/System.Text.Formatting/System/Text/Formatting/Formatters/StreamFormatter.cs @@ -30,16 +30,13 @@ public StreamFormatter(Stream stream, SymbolTable symbolTable, ArrayPool p _stream = stream; } - Span IOutput.Buffer + Span IOutput.GetSpan() { - get + if (_buffer == null) { - if (_buffer == null) - { - _buffer = _pool.Rent(256); - } - return new Span(_buffer); + _buffer = _pool.Rent(256); } + return new Span(_buffer); } void IOutput.Enlarge(int desiredBufferLength) diff --git a/src/System.Text.Formatting/System/Text/Formatting/Formatters/StringFormatter.cs b/src/System.Text.Formatting/System/Text/Formatting/Formatters/StringFormatter.cs index 0dd4f0aeae7..03e3288dd3a 100644 --- a/src/System.Text.Formatting/System/Text/Formatting/Formatters/StringFormatter.cs +++ b/src/System.Text.Formatting/System/Text/Formatting/Formatters/StringFormatter.cs @@ -60,7 +60,7 @@ public override string ToString() return text; } - Span IOutput.Buffer => _buffer.Free.AsSpan(); + Span IOutput.GetSpan() => _buffer.Free.AsSpan(); void IOutput.Enlarge(int desiredBufferLength) { diff --git a/src/System.Text.Formatting/System/Text/Formatting/IOutputExtensions.cs b/src/System.Text.Formatting/System/Text/Formatting/IOutputExtensions.cs index 281b2d02d1a..1f4a609a130 100644 --- a/src/System.Text.Formatting/System/Text/Formatting/IOutputExtensions.cs +++ b/src/System.Text.Formatting/System/Text/Formatting/IOutputExtensions.cs @@ -18,7 +18,7 @@ public static void Append(this TFormatter formatter, T value, Sym public static bool TryAppend(this TFormatter formatter, T value, SymbolTable symbolTable, StandardFormat format = default) where T : IBufferFormattable where TFormatter : IOutput { - if (!value.TryFormat(formatter.Buffer, out int bytesWritten, format, symbolTable)) + if (!value.TryFormat(formatter.GetSpan(), out int bytesWritten, format, symbolTable)) { return false; } @@ -65,7 +65,7 @@ public static void Append(this TFormatter formatter, ReadOnlySpan(this TFormatter formatter, ReadOnlySpan value, SymbolTable symbolTable) where TFormatter : IOutput { - var result = symbolTable.TryEncode(value, formatter.Buffer, out int consumed, out int written); + var result = symbolTable.TryEncode(value, formatter.GetSpan(), out int consumed, out int written); if (result) formatter.Advance(written); @@ -97,7 +97,7 @@ public static void Append(this TFormatter formatter, Utf8Span value, public static bool TryAppend(this TFormatter formatter, Utf8Span value, SymbolTable symbolTable) where TFormatter : IOutput { - if (!symbolTable.TryEncode(value, formatter.Buffer, out int consumed, out int bytesWritten)) + if (!symbolTable.TryEncode(value, formatter.GetSpan(), out int consumed, out int bytesWritten)) { return false; } @@ -114,7 +114,7 @@ public static void Append(this TFormatter formatter, uint value, Sym public static bool TryAppend(this TFormatter formatter, uint value, SymbolTable symbolTable, StandardFormat format = default) where TFormatter : IOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, symbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, symbolTable)) { return false; } @@ -131,7 +131,7 @@ public static void Append(this TFormatter formatter, ulong value, Sy public static bool TryAppend(this TFormatter formatter, ulong value, SymbolTable symbolTable, StandardFormat format = default) where TFormatter : IOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, symbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, symbolTable)) { return false; } @@ -148,7 +148,7 @@ public static void Append(this TFormatter formatter, int value, Symb public static bool TryAppend(this TFormatter formatter, int value, SymbolTable symbolTable, StandardFormat format = default) where TFormatter : IOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, symbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, symbolTable)) { return false; } @@ -165,7 +165,7 @@ public static void Append(this TFormatter formatter, long value, Sym public static bool TryAppend(this TFormatter formatter, long value, SymbolTable symbolTable, StandardFormat format = default) where TFormatter : IOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, symbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, symbolTable)) { return false; } @@ -182,7 +182,7 @@ public static void Append(this TFormatter formatter, byte value, Sym public static bool TryAppend(this TFormatter formatter, byte value, SymbolTable symbolTable, StandardFormat format = default) where TFormatter : IOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, symbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, symbolTable)) { return false; } @@ -199,7 +199,7 @@ public static void Append(this TFormatter formatter, sbyte value, Sy public static bool TryAppend(this TFormatter formatter, sbyte value, SymbolTable symbolTable, StandardFormat format = default) where TFormatter : IOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, symbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, symbolTable)) { return false; } @@ -216,7 +216,7 @@ public static void Append(this TFormatter formatter, ushort value, S public static bool TryAppend(this TFormatter formatter, ushort value, SymbolTable symbolTable, StandardFormat format = default) where TFormatter : IOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, symbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, symbolTable)) { return false; } @@ -233,7 +233,7 @@ public static void Append(this TFormatter formatter, short value, Sy public static bool TryAppend(this TFormatter formatter, short value, SymbolTable symbolTable, StandardFormat format = default) where TFormatter : IOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, symbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, symbolTable)) { return false; } @@ -250,7 +250,7 @@ public static void Append(this TFormatter formatter, Guid value, Sym public static bool TryAppend(this TFormatter formatter, Guid value, SymbolTable symbolTable, StandardFormat format = default) where TFormatter : IOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, symbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, symbolTable)) { return false; } @@ -267,7 +267,7 @@ public static void Append(this TFormatter formatter, DateTime value, public static bool TryAppend(this TFormatter formatter, DateTime value, SymbolTable symbolTable, StandardFormat format = default) where TFormatter : IOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, symbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, symbolTable)) { return false; } @@ -284,7 +284,7 @@ public static void Append(this TFormatter formatter, DateTimeOffset public static bool TryAppend(this TFormatter formatter, DateTimeOffset value, SymbolTable symbolTable, StandardFormat format = default) where TFormatter : IOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, symbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, symbolTable)) { return false; } @@ -301,7 +301,7 @@ public static void Append(this TFormatter formatter, TimeSpan value, public static bool TryAppend(this TFormatter formatter, TimeSpan value, SymbolTable symbolTable, StandardFormat format = default) where TFormatter : IOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, symbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, symbolTable)) { return false; } @@ -318,7 +318,7 @@ public static void Append(this TFormatter formatter, float value, Sy public static bool TryAppend(this TFormatter formatter, float value, SymbolTable symbolTable, StandardFormat format = default) where TFormatter : IOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, symbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, symbolTable)) { return false; } @@ -335,7 +335,7 @@ public static void Append(this TFormatter formatter, double value, S public static bool TryAppend(this TFormatter formatter, double value, SymbolTable symbolTable, StandardFormat format = default) where TFormatter : IOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, symbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, symbolTable)) { return false; } diff --git a/src/System.Text.Formatting/System/Text/Formatting/ITextOutputExtensions.cs b/src/System.Text.Formatting/System/Text/Formatting/ITextOutputExtensions.cs index 202d3faeccc..54164648378 100644 --- a/src/System.Text.Formatting/System/Text/Formatting/ITextOutputExtensions.cs +++ b/src/System.Text.Formatting/System/Text/Formatting/ITextOutputExtensions.cs @@ -18,7 +18,7 @@ public static void Append(this TFormatter formatter, T value, Sta public static bool TryAppend(this TFormatter formatter, T value, StandardFormat format = default) where T : IBufferFormattable where TFormatter : ITextOutput { - if (!value.TryFormat(formatter.Buffer, out int bytesWritten, format, formatter.SymbolTable)) + if (!value.TryFormat(formatter.GetSpan(), out int bytesWritten, format, formatter.SymbolTable)) { return false; } @@ -35,7 +35,7 @@ public static void Append(this TFormatter formatter, byte value, Sta public static bool TryAppend(this TFormatter formatter, byte value, StandardFormat format = default) where TFormatter : ITextOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, formatter.SymbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, formatter.SymbolTable)) { return false; } @@ -52,7 +52,7 @@ public static void Append(this TFormatter formatter, sbyte value, St public static bool TryAppend(this TFormatter formatter, sbyte value, StandardFormat format = default) where TFormatter : ITextOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, formatter.SymbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, formatter.SymbolTable)) { return false; } @@ -69,7 +69,7 @@ public static void Append(this TFormatter formatter, ushort value, S public static bool TryAppend(this TFormatter formatter, ushort value, StandardFormat format = default) where TFormatter : ITextOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, formatter.SymbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, formatter.SymbolTable)) { return false; } @@ -86,7 +86,7 @@ public static void Append(this TFormatter formatter, short value, St public static bool TryAppend(this TFormatter formatter, short value, StandardFormat format = default) where TFormatter : ITextOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, formatter.SymbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, formatter.SymbolTable)) { return false; } @@ -103,7 +103,7 @@ public static void Append(this TFormatter formatter, uint value, Sta public static bool TryAppend(this TFormatter formatter, uint value, StandardFormat format = default) where TFormatter : ITextOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, formatter.SymbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, formatter.SymbolTable)) { return false; } @@ -120,7 +120,7 @@ public static void Append(this TFormatter formatter, int value, Stan public static bool TryAppend(this TFormatter formatter, int value, StandardFormat format = default) where TFormatter : ITextOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, formatter.SymbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, formatter.SymbolTable)) { return false; } @@ -137,7 +137,7 @@ public static void Append(this TFormatter formatter, ulong value, St public static bool TryAppend(this TFormatter formatter, ulong value, StandardFormat format = default) where TFormatter : ITextOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, formatter.SymbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, formatter.SymbolTable)) { return false; } @@ -154,7 +154,7 @@ public static void Append(this TFormatter formatter, long value, Sta public static bool TryAppend(this TFormatter formatter, long value, StandardFormat format = default) where TFormatter : ITextOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, formatter.SymbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, formatter.SymbolTable)) { return false; } @@ -207,7 +207,7 @@ public static void Append(this TFormatter formatter, Utf8Span value) public static bool TryAppend(this TFormatter formatter, Utf8Span value) where TFormatter : ITextOutput { - if (!formatter.SymbolTable.TryEncode(value, formatter.Buffer, out int consumed, out int bytesWritten)) + if (!formatter.SymbolTable.TryEncode(value, formatter.GetSpan(), out int consumed, out int bytesWritten)) { return false; } @@ -224,7 +224,7 @@ public static void Append(this TFormatter formatter, Guid value, Sta public static bool TryAppend(this TFormatter formatter, Guid value, StandardFormat format = default) where TFormatter : ITextOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, formatter.SymbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, formatter.SymbolTable)) { return false; } @@ -241,7 +241,7 @@ public static void Append(this TFormatter formatter, DateTime value, public static bool TryAppend(this TFormatter formatter, DateTime value, StandardFormat format = default) where TFormatter : ITextOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, formatter.SymbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, formatter.SymbolTable)) { return false; } @@ -258,7 +258,7 @@ public static void Append(this TFormatter formatter, DateTimeOffset public static bool TryAppend(this TFormatter formatter, DateTimeOffset value, StandardFormat format = default) where TFormatter : ITextOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, formatter.SymbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, formatter.SymbolTable)) { return false; } @@ -275,7 +275,7 @@ public static void Append(this TFormatter formatter, TimeSpan value, public static bool TryAppend(this TFormatter formatter, TimeSpan value, StandardFormat format = default) where TFormatter : ITextOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, formatter.SymbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, formatter.SymbolTable)) { return false; } @@ -292,7 +292,7 @@ public static void Append(this TFormatter formatter, float value, St public static bool TryAppend(this TFormatter formatter, float value, StandardFormat format = default) where TFormatter : ITextOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, formatter.SymbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, formatter.SymbolTable)) { return false; } @@ -309,7 +309,7 @@ public static void Append(this TFormatter formatter, double value, S public static bool TryAppend(this TFormatter formatter, double value, StandardFormat format = default) where TFormatter : ITextOutput { - if (!CustomFormatter.TryFormat(value, formatter.Buffer, out int bytesWritten, format, formatter.SymbolTable)) + if (!CustomFormatter.TryFormat(value, formatter.GetSpan(), out int bytesWritten, format, formatter.SymbolTable)) { return false; } diff --git a/src/System.Text.Http/System/Text/Http/IFormatterHttpExtensions.cs b/src/System.Text.Http/System/Text/Http/IFormatterHttpExtensions.cs index 2ffecdec8d0..265a93617f0 100644 --- a/src/System.Text.Http/System/Text/Http/IFormatterHttpExtensions.cs +++ b/src/System.Text.Http/System/Text/Http/IFormatterHttpExtensions.cs @@ -110,10 +110,10 @@ public static void AppendHttpHeader(this TFormatter formatter, strin public static void AppendHttpNewLine(this TFormatter formatter) where TFormatter : ITextOutput { - var buffer = formatter.Buffer; + var buffer = formatter.GetSpan(); while(buffer.Length < 2) { formatter.Enlarge(2); - buffer = formatter.Buffer; + buffer = formatter.GetSpan(); } buffer[0] = 13; buffer[1] = 10; @@ -124,7 +124,7 @@ static void AppendBytes(this TFormatter formatter, byte[] bytes) whe { while (true) { - var buffer = formatter.Buffer; + var buffer = formatter.GetSpan(); if (bytes.Length > buffer.Length) { formatter.Enlarge(bytes.Length); diff --git a/src/System.Text.Json/System/Text/Json/JsonWriter.cs b/src/System.Text.Json/System/Text/Json/JsonWriter.cs index 780ec04cc36..89e23049952 100644 --- a/src/System.Text.Json/System/Text/Json/JsonWriter.cs +++ b/src/System.Text.Json/System/Text/Json/JsonWriter.cs @@ -344,7 +344,7 @@ private void WriteControl(byte value) } else { - var buffer = _output.Buffer; + var buffer = _output.GetSpan(); int written; while (!_output.SymbolTable.TryEncode(value, buffer, out written)) buffer = EnsureBuffer(); @@ -365,7 +365,7 @@ private void WriteQuotedString(string value) [MethodImpl(MethodImplOptions.AggressiveInlining)] private void WriteNumber(long value) { - var buffer = _output.Buffer; + var buffer = _output.GetSpan(); int written; while (!CustomFormatter.TryFormat(value, buffer, out written, JsonConstants.NumberFormat, _output.SymbolTable)) buffer = EnsureBuffer(); @@ -376,7 +376,7 @@ private void WriteNumber(long value) [MethodImpl(MethodImplOptions.AggressiveInlining)] private void WriteNumber(ulong value) { - var buffer = _output.Buffer; + var buffer = _output.GetSpan(); int written; while (!CustomFormatter.TryFormat(value, buffer, out written, JsonConstants.NumberFormat, _output.SymbolTable)) buffer = EnsureBuffer(); @@ -387,7 +387,7 @@ private void WriteNumber(ulong value) [MethodImpl(MethodImplOptions.AggressiveInlining)] private void WriteDateTime(DateTime value) { - var buffer = _output.Buffer; + var buffer = _output.GetSpan(); int written; while (!CustomFormatter.TryFormat(value, buffer, out written, JsonConstants.DateTimeFormat, _output.SymbolTable)) buffer = EnsureBuffer(); @@ -398,7 +398,7 @@ private void WriteDateTime(DateTime value) [MethodImpl(MethodImplOptions.AggressiveInlining)] private void WriteDateTimeOffset(DateTimeOffset value) { - var buffer = _output.Buffer; + var buffer = _output.GetSpan(); int written; while (!CustomFormatter.TryFormat(value, buffer, out written, JsonConstants.DateTimeFormat, _output.SymbolTable)) buffer = EnsureBuffer(); @@ -409,7 +409,7 @@ private void WriteDateTimeOffset(DateTimeOffset value) [MethodImpl(MethodImplOptions.AggressiveInlining)] private void WriteGuid(Guid value) { - var buffer = _output.Buffer; + var buffer = _output.GetSpan(); int written; while (!CustomFormatter.TryFormat(value, buffer, out written, JsonConstants.GuidFormat, _output.SymbolTable)) buffer = EnsureBuffer(); @@ -424,7 +424,7 @@ private void Write(ReadOnlySpan value) if (UseFastUtf8) { - Span destination = _output.Buffer; + Span destination = _output.GetSpan(); while (true) { @@ -453,7 +453,7 @@ private void Write(ReadOnlySpan value) } else { - Span destination = _output.Buffer; + Span destination = _output.GetSpan(); if (!_output.SymbolTable.TryEncode(source, destination, out int consumed, out int written)) destination = EnsureBuffer(); @@ -464,7 +464,7 @@ private void Write(ReadOnlySpan value) [MethodImpl(MethodImplOptions.AggressiveInlining)] private void WriteJsonValue(ReadOnlySpan values) { - var buffer = _output.Buffer; + var buffer = _output.GetSpan(); int written; while (!_output.SymbolTable.TryEncode(values, buffer, out int consumed, out written)) buffer = EnsureBuffer(); @@ -571,13 +571,13 @@ private Span EnsureBuffer(int needed = 256) // larger than we are likely to need. const int BufferEnlargeCount = 1024; - var buffer = _output.Buffer; + var buffer = _output.GetSpan(); var currentSize = buffer.Length; if (currentSize >= needed) return buffer; _output.Enlarge(BufferEnlargeCount); - buffer = _output.Buffer; + buffer = _output.GetSpan(); int newSize = buffer.Length; if (newSize < needed || newSize <= currentSize) diff --git a/tests/System.Binary.Base64.Tests/BasicUnitTests.cs b/tests/System.Binary.Base64.Tests/BasicUnitTests.cs index 5776eff8997..7bb22f57be5 100644 --- a/tests/System.Binary.Base64.Tests/BasicUnitTests.cs +++ b/tests/System.Binary.Base64.Tests/BasicUnitTests.cs @@ -96,7 +96,7 @@ class TestOutput : IOutput public Span GetBuffer => _buffer; - public Span Buffer => _buffer.AsSpan().Slice(_written); + public Span GetSpan() => _buffer.AsSpan().Slice(_written); public void Advance(int bytes) { diff --git a/tests/System.Buffers.Experimental.Tests/BufferWriterTests_sequence.cs b/tests/System.Buffers.Experimental.Tests/BufferWriterTests_sequence.cs index 1e0cd2e2a05..c550c323a24 100644 --- a/tests/System.Buffers.Experimental.Tests/BufferWriterTests_sequence.cs +++ b/tests/System.Buffers.Experimental.Tests/BufferWriterTests_sequence.cs @@ -48,7 +48,7 @@ class Output : IOutput byte[] _current = new byte[0]; List _commited = new List(); - public Span Buffer => _current; + public Span GetSpan() => _current; public void Advance(int bytes) {