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

Commit

Permalink
Cleaned up Reader/Writer Project (#2156)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofCwalina authored Mar 12, 2018
1 parent fd72e1a commit d2264af
Show file tree
Hide file tree
Showing 32 changed files with 103 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Buffers;
using System.Buffers.Cryptography;
using System.Buffers.Text;
using System.Buffers.Writer;
using System.Text.Encodings.Web.Utf8;
using System.Text.Utf8;
using static System.Buffers.Text.Encodings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
using System.Buffers.Cryptography;
using System.Text.Utf8;
using System.Binary.Base64Experimental;
using System.Buffers.Text;
using System.Buffers;
using System.Buffers.Transformations;
using System.Buffers.Operations;
using System.Buffers.Writer;

namespace System.Azure.Authentication
{
Expand All @@ -20,7 +20,7 @@ public struct StorageAuthorizationHeader : IWritable
public ReadOnlyMemory<byte> HttpVerb;
public string AccountName;
public string CanonicalizedResource;
public WritableBytes CanonicalizedHeaders;
public ReadOnlyMemory<byte> CanonicalizedHeaders;
public long ContentLength;

public bool TryWrite(Span<byte> buffer, out int written, StandardFormat format = default)
Expand Down
1 change: 1 addition & 0 deletions src/System.Binary.Base64/Base64Decoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// 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;
using System.Buffers.Operations;
using System.Buffers.Text;

namespace System.Binary.Base64Experimental
Expand Down
1 change: 1 addition & 0 deletions src/System.Binary.Base64/Base64Encoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Buffers;
using System.Buffers.Operations;
using System.Buffers.Text;
using System.Diagnostics;
using System.Runtime.CompilerServices;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Runtime.CompilerServices;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Buffers.Operations;

namespace System.Buffers
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Buffers
namespace System.Buffers.Operations
{
public interface IBufferOperation
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Buffers
namespace System.Buffers.Operations
{
public interface IBufferTransformation : IBufferOperation
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Sequences;
using System.Runtime.CompilerServices;

namespace System.Buffers
namespace System.Buffers.Reader
{
public class BufferReader
{
public static ByteBufferReader Create(ReadOnlySequence<byte> buffer)
{
return new ByteBufferReader(buffer);
}

public static int Peek(ByteBufferReader reader, Span<byte> destination)
=> ByteBufferReader.Peek(reader, destination);
}

public ref struct ByteBufferReader
public ref struct BufferReader
{
private ReadOnlySpan<byte> _currentSpan;
private int _index;
Expand All @@ -29,7 +17,7 @@ public ref struct ByteBufferReader
private int _consumedBytes;
private bool _end;

public ByteBufferReader(ReadOnlySequence<byte> buffer)
public BufferReader(ReadOnlySequence<byte> buffer)
{
_end = false;
_index = 0;
Expand All @@ -41,6 +29,11 @@ public ByteBufferReader(ReadOnlySequence<byte> buffer)
MoveNext();
}

public static BufferReader Create(ReadOnlySequence<byte> buffer)
{
return new BufferReader(buffer);
}

public bool End => _end;

public int CurrentSegmentIndex => _index;
Expand Down Expand Up @@ -134,7 +127,7 @@ public void Advance(int byteCount)
}
}

internal static int Peek(ByteBufferReader bytes, Span<byte> destination)
internal static int Peek(BufferReader bytes, Span<byte> destination)
{
var first = bytes.UnreadSegment;
if (first.Length > destination.Length)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Buffers.Binary;
using System.Collections.Sequences;

namespace System.Buffers
namespace System.Buffers.Reader
{
public static partial class BufferReaderExtensions
{
public static bool TryRead(ref ByteBufferReader reader, out int value, bool littleEndian = false)
public static bool TryRead(ref BufferReader reader, out int value, bool littleEndian = false)
{
var unread = reader.UnreadSegment;
if (littleEndian)
Expand All @@ -26,7 +25,7 @@ public static bool TryRead(ref ByteBufferReader reader, out int value, bool litt
}

Span<byte> tempSpan = stackalloc byte[4];
var copied = BufferReader.Peek(reader, tempSpan);
var copied = BufferReaderExtensions.Peek(reader, tempSpan);
if (copied < 4)
{
value = default;
Expand All @@ -45,4 +44,10 @@ public static bool TryRead(ref ByteBufferReader reader, out int value, bool litt
return true;
}
}

public static partial class BufferReaderExtensions
{
public static int Peek(BufferReader reader, Span<byte> destination)
=> BufferReader.Peek(reader, destination);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

using System.Collections.Sequences;

namespace System.Buffers
namespace System.Buffers.Reader
{
// TODO: the TryReadUntill methods are very inneficient. We need to fix that.
public static partial class BufferReaderExtensions
{
public static bool TryReadUntill(ref ByteBufferReader reader, out ReadOnlySequence<byte> bytes, byte delimiter)
public static bool TryReadUntill(ref BufferReader reader, out ReadOnlySequence<byte> bytes, byte delimiter)
{
var copy = reader;
var start = reader.Position;
Expand All @@ -26,7 +26,7 @@ public static bool TryReadUntill(ref ByteBufferReader reader, out ReadOnlySequen
return false;
}

public static bool TryReadUntill(ref ByteBufferReader reader, out ReadOnlySequence<byte> bytes, ReadOnlySpan<byte> delimiter)
public static bool TryReadUntill(ref BufferReader reader, out ReadOnlySequence<byte> bytes, ReadOnlySpan<byte> delimiter)
{
if (delimiter.Length == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Buffers.Text;
using System.Collections.Sequences;

namespace System.Buffers
namespace System.Buffers.Reader
{
public static partial class BufferReaderExtensions
{
public static bool TryParse(ref ByteBufferReader reader, out bool value)
public static bool TryParse(ref BufferReader reader, out bool value)
{
var unread = reader.UnreadSegment;
if (Utf8Parser.TryParse(unread, out value, out int consumed))
Expand All @@ -21,7 +20,7 @@ public static bool TryParse(ref ByteBufferReader reader, out bool value)
}

Span<byte> tempSpan = stackalloc byte[5];
var copied = BufferReader.Peek(reader, tempSpan);
var copied = BufferReaderExtensions.Peek(reader, tempSpan);
if (Utf8Parser.TryParse(tempSpan.Slice(0, copied), out value, out consumed))
{
reader.Advance(consumed);
Expand All @@ -31,7 +30,7 @@ public static bool TryParse(ref ByteBufferReader reader, out bool value)
return false;
}

public static bool TryParse(ref ByteBufferReader reader, out int value)
public static bool TryParse(ref BufferReader reader, out int value)
{
var unread = reader.UnreadSegment;
if (Utf8Parser.TryParse(unread, out value, out int consumed))
Expand All @@ -44,7 +43,7 @@ public static bool TryParse(ref ByteBufferReader reader, out int value)
}

Span<byte> tempSpan = stackalloc byte[15];
var copied = BufferReader.Peek(reader, tempSpan);
var copied = BufferReaderExtensions.Peek(reader, tempSpan);
if (Utf8Parser.TryParse(tempSpan.Slice(0, copied), out value, out consumed))
{
reader.Advance(consumed);
Expand All @@ -54,7 +53,7 @@ public static bool TryParse(ref ByteBufferReader reader, out int value)
return false;
}

public static bool TryParse(ref ByteBufferReader reader, out ulong value)
public static bool TryParse(ref BufferReader reader, out ulong value)
{
var unread = reader.UnreadSegment;
if (Utf8Parser.TryParse(unread, out value, out int consumed))
Expand All @@ -67,7 +66,7 @@ public static bool TryParse(ref ByteBufferReader reader, out ulong value)
}

Span<byte> tempSpan = stackalloc byte[30];
var copied = BufferReader.Peek(reader, tempSpan);
var copied = BufferReaderExtensions.Peek(reader, tempSpan);
if (Utf8Parser.TryParse(tempSpan.Slice(0, copied), out value, out consumed))
{
reader.Advance(consumed);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,8 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace System.Buffers.Transformations
namespace System.Buffers.Operations
{
public struct WritableBytes : IWritable
{
readonly ReadOnlyMemory<byte> _bytes;

public WritableBytes(ReadOnlyMemory<byte> bytes)
{
_bytes = bytes;
}

public bool TryWrite(Span<byte> buffer, out int written, StandardFormat format = default)
{
if (format != default) throw new InvalidOperationException();

if (!_bytes.Span.TryCopyTo(buffer))
{
written = 0;
return false;
}
written = _bytes.Length;
return true;
}
}

public class RemoveTransformation : IBufferTransformation
{
byte _value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// See the LICENSE file in the project root for more information.


namespace System.Buffers.Text
using System.Buffers.Text;

namespace System.Buffers.Writer
{
public ref partial struct BufferWriter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Buffers.Text
using System.Buffers.Text;

namespace System.Buffers.Writer
{
public ref partial struct BufferWriter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Buffers.Text
using System.Buffers.Text;

namespace System.Buffers.Writer
{
public ref partial struct BufferWriter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// 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.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace System.Buffers.Text
namespace System.Buffers.Writer
{
public ref struct BufferWriter<TOutput> where TOutput : IBufferWriter<byte>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
// 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.Text.Utf8;

namespace System.Buffers.Text
namespace System.Buffers.Writer
{
public ref partial struct BufferWriter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
// See the LICENSE file in the project root for more information.


namespace System.Buffers.Text
using System.Buffers.Text;

namespace System.Buffers.Writer
{
public ref partial struct BufferWriter
{
Expand Down Expand Up @@ -35,6 +37,28 @@ public bool TryWriteBytes(ReadOnlyMemory<byte> bytes)
public void WriteBytes(ReadOnlyMemory<byte> bytes)
=> WriteBytes(bytes.Span);

public bool TryWriteBytes(ReadOnlyMemory<byte> bytes, TransformationFormat format)
{
if (!TryWriteBytes(bytes.Span))
{
return false;
}

int written = bytes.Length;
if (format.TryTransform(Free, ref written))
{
_written += written;
return true;
}

return false;
}

public void WriteBytes(ReadOnlyMemory<byte> bytes, TransformationFormat format)
{
while (!TryWriteBytes(bytes, format)) Resize();
}

#endregion

#region IWritable
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace System.Buffers
namespace System.Buffers.Writer
{
public static class OutputExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

using System.Runtime.CompilerServices;

namespace System.Buffers
namespace System.Buffers.Writer
{
public static class OutputWriter
{
Expand Down
Loading

0 comments on commit d2264af

Please sign in to comment.