Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
Cleaned up IOutput based on API review (#1987)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofCwalina authored Dec 18, 2017
1 parent f531829 commit 8c9bfd6
Show file tree
Hide file tree
Showing 20 changed files with 80 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@ public TcpConnectionFormatter(TcpConnection connection, int bufferSize = 4096)

public SymbolTable SymbolTable => SymbolTable.InvariantUtf8;

public Span<byte> Buffer {
get {
var buffer = _buffer.AsSpan().Slice(ChunkPrefixSize + _written);
if (buffer.Length > 2) return buffer.Slice(0, buffer.Length - 2);
return Span<byte>.Empty;
}
public Span<byte> GetSpan() {
var buffer = _buffer.AsSpan().Slice(ChunkPrefixSize + _written);
if (buffer.Length > 2) return buffer.Slice(0, buffer.Length - 2);
return Span<byte>.Empty;
}

public void Advance(int bytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> outputSpan = destination.Buffer;
Span<byte> outputSpan = destination.GetSpan();
ReadOnlySpan<byte> sourceSpan = sourceBuffer.Span;

if (!remainder.IsEmpty)
Expand All @@ -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)
{
Expand All @@ -186,7 +186,7 @@ public static void Pipe(this IBufferOperation transformation, ReadOnlyBytes sour
afterMergeSlice = bytesConsumed - remainder.Length;
remainder = Span<byte>.Empty;
destination.Advance(bytesWritten);
outputSpan = destination.Buffer;
outputSpan = destination.GetSpan();
}
}

Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -219,7 +219,7 @@ private Span<byte> 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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/System.Buffers.Primitives/System/Buffers/IOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace System.Buffers
{
public interface IOutput
{
Span<byte> Buffer { get; }
Span<byte> GetSpan();
void Advance(int bytes);

/// <summary>desiredBufferLength == 0 means "i don't care"</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public static Position Create<T>(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;
Expand All @@ -56,7 +55,5 @@ private Position(int index, object item)
_item = item;
_index = index;
}

private Position Offset(int index) => new Position(Index + index, _item);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 3 additions & 2 deletions src/System.IO.Pipelines.Extensions/WritableBufferOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public WritableBufferOutput(WritableBuffer writer) : this()
_writer = writer;
}

public Span<byte> Buffer => _writer.Buffer.Span;
public Span<byte> GetSpan() => _writer.Buffer.Span;

public void Advance(int bytes)
{
Expand All @@ -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;
}
}
Expand Down
10 changes: 3 additions & 7 deletions src/System.IO.Pipelines.Text.Primitives/PipelineTextOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,10 @@ public PipelineTextOutput(IPipeWriter writer, SymbolTable symbolTable)

public SymbolTable SymbolTable { get; }

public Span<byte> Buffer
public Span<byte> GetSpan()
{
get
{
EnsureBuffer();

return _writableBuffer.Buffer.Span;
}
EnsureBuffer();
return _writableBuffer.Buffer.Span;
}

public void Advance(int bytes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ public static void Format<TFormatter, T0, T1, T2, T3, T4>(this TFormatter format
// TODO: this should be removed and an ability to append substrings should be added
static void Append<TFormatter>(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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void Clear() {

public SymbolTable SymbolTable => _symbolTable;

public Span<byte> Buffer => Free.AsSpan();
public Span<byte> GetSpan() => Free.AsSpan();

public void Enlarge(int desiredBufferLength = 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public OutputFormatter(TOutput output) : this(output, SymbolTable.InvariantUtf8)
{
}

public Span<byte> Buffer => _output.Buffer;
public Span<byte> GetSpan() => _output.GetSpan();

public SymbolTable SymbolTable => _symbolTable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,8 @@ public SequenceFormatter(TSequence buffers, SymbolTable symbolTable)
_previousWrittenBytes = -1;
}

Span<byte> IOutput.Buffer
{
get {
return Current.Span.Slice(_currentWrittenBytes);
}
}
Span<byte> IOutput.GetSpan()
=> Current.Span.Slice(_currentWrittenBytes);

private Memory<byte> Current {
get {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,13 @@ public StreamFormatter(Stream stream, SymbolTable symbolTable, ArrayPool<byte> p
_stream = stream;
}

Span<byte> IOutput.Buffer
Span<byte> IOutput.GetSpan()
{
get
if (_buffer == null)
{
if (_buffer == null)
{
_buffer = _pool.Rent(256);
}
return new Span<byte>(_buffer);
_buffer = _pool.Rent(256);
}
return new Span<byte>(_buffer);
}

void IOutput.Enlarge(int desiredBufferLength)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override string ToString()
return text;
}

Span<byte> IOutput.Buffer => _buffer.Free.AsSpan();
Span<byte> IOutput.GetSpan() => _buffer.Free.AsSpan();

void IOutput.Enlarge(int desiredBufferLength)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static void Append<TFormatter, T>(this TFormatter formatter, T value, Sym

public static bool TryAppend<TFormatter, T>(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;
}
Expand Down Expand Up @@ -65,7 +65,7 @@ public static void Append<TFormatter>(this TFormatter formatter, ReadOnlySpan<ch

public static bool TryAppend<TFormatter>(this TFormatter formatter, ReadOnlySpan<char> 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);

Expand Down Expand Up @@ -97,7 +97,7 @@ public static void Append<TFormatter>(this TFormatter formatter, Utf8Span value,

public static bool TryAppend<TFormatter>(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;
}
Expand All @@ -114,7 +114,7 @@ public static void Append<TFormatter>(this TFormatter formatter, uint value, Sym

public static bool TryAppend<TFormatter>(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;
}
Expand All @@ -131,7 +131,7 @@ public static void Append<TFormatter>(this TFormatter formatter, ulong value, Sy

public static bool TryAppend<TFormatter>(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;
}
Expand All @@ -148,7 +148,7 @@ public static void Append<TFormatter>(this TFormatter formatter, int value, Symb

public static bool TryAppend<TFormatter>(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;
}
Expand All @@ -165,7 +165,7 @@ public static void Append<TFormatter>(this TFormatter formatter, long value, Sym

public static bool TryAppend<TFormatter>(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;
}
Expand All @@ -182,7 +182,7 @@ public static void Append<TFormatter>(this TFormatter formatter, byte value, Sym

public static bool TryAppend<TFormatter>(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;
}
Expand All @@ -199,7 +199,7 @@ public static void Append<TFormatter>(this TFormatter formatter, sbyte value, Sy

public static bool TryAppend<TFormatter>(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;
}
Expand All @@ -216,7 +216,7 @@ public static void Append<TFormatter>(this TFormatter formatter, ushort value, S

public static bool TryAppend<TFormatter>(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;
}
Expand All @@ -233,7 +233,7 @@ public static void Append<TFormatter>(this TFormatter formatter, short value, Sy

public static bool TryAppend<TFormatter>(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;
}
Expand All @@ -250,7 +250,7 @@ public static void Append<TFormatter>(this TFormatter formatter, Guid value, Sym

public static bool TryAppend<TFormatter>(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;
}
Expand All @@ -267,7 +267,7 @@ public static void Append<TFormatter>(this TFormatter formatter, DateTime value,

public static bool TryAppend<TFormatter>(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;
}
Expand All @@ -284,7 +284,7 @@ public static void Append<TFormatter>(this TFormatter formatter, DateTimeOffset

public static bool TryAppend<TFormatter>(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;
}
Expand All @@ -301,7 +301,7 @@ public static void Append<TFormatter>(this TFormatter formatter, TimeSpan value,

public static bool TryAppend<TFormatter>(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;
}
Expand All @@ -318,7 +318,7 @@ public static void Append<TFormatter>(this TFormatter formatter, float value, Sy

public static bool TryAppend<TFormatter>(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;
}
Expand All @@ -335,7 +335,7 @@ public static void Append<TFormatter>(this TFormatter formatter, double value, S

public static bool TryAppend<TFormatter>(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;
}
Expand Down
Loading

0 comments on commit 8c9bfd6

Please sign in to comment.