Skip to content

Commit

Permalink
Revert "Cleaned up Reader/Writer Project (dotnet#2156)"
Browse files Browse the repository at this point in the history
This reverts commit d2264af.
  • Loading branch information
ahsonkhan committed Mar 16, 2018
1 parent 291c36d commit 4f02651
Show file tree
Hide file tree
Showing 32 changed files with 83 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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.Operations;
using System.Buffers.Writer;
using System.Buffers.Transformations;

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 ReadOnlyMemory<byte> CanonicalizedHeaders;
public WritableBytes CanonicalizedHeaders;
public long ContentLength;

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

namespace System.Binary.Base64Experimental
Expand Down
1 change: 0 additions & 1 deletion src/System.Binary.Base64/Base64Encoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// 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,7 +6,6 @@
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.Operations
namespace System.Buffers
{
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.Operations
namespace System.Buffers
{
public interface IBufferTransformation : IBufferOperation
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
// 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.Reader
namespace System.Buffers
{
public static partial class BufferReaderExtensions
{
public static bool TryRead(ref BufferReader reader, out int value, bool littleEndian = false)
public static bool TryRead(ref ByteBufferReader reader, out int value, bool littleEndian = false)
{
var unread = reader.UnreadSegment;
if (littleEndian)
Expand All @@ -25,7 +26,7 @@ public static bool TryRead(ref BufferReader reader, out int value, bool littleEn
}

Span<byte> tempSpan = stackalloc byte[4];
var copied = BufferReaderExtensions.Peek(reader, tempSpan);
var copied = BufferReader.Peek(reader, tempSpan);
if (copied < 4)
{
value = default;
Expand All @@ -44,10 +45,4 @@ public static bool TryRead(ref BufferReader reader, out int value, bool littleEn
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.Reader
namespace System.Buffers
{
// TODO: the TryReadUntill methods are very inneficient. We need to fix that.
public static partial class BufferReaderExtensions
{
public static bool TryReadUntill(ref BufferReader reader, out ReadOnlySequence<byte> bytes, byte delimiter)
public static bool TryReadUntill(ref ByteBufferReader 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 BufferReader reader, out ReadOnlySequence<b
return false;
}

public static bool TryReadUntill(ref BufferReader reader, out ReadOnlySequence<byte> bytes, ReadOnlySpan<byte> delimiter)
public static bool TryReadUntill(ref ByteBufferReader 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,12 +2,13 @@
// 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.Reader
namespace System.Buffers
{
public static partial class BufferReaderExtensions
{
public static bool TryParse(ref BufferReader reader, out bool value)
public static bool TryParse(ref ByteBufferReader reader, out bool value)
{
var unread = reader.UnreadSegment;
if (Utf8Parser.TryParse(unread, out value, out int consumed))
Expand All @@ -20,7 +21,7 @@ public static bool TryParse(ref BufferReader reader, out bool value)
}

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

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

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

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

Span<byte> tempSpan = stackalloc byte[30];
var copied = BufferReaderExtensions.Peek(reader, tempSpan);
var copied = BufferReader.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,11 +1,23 @@
// 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.Reader
namespace System.Buffers
{
public ref struct BufferReader
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
{
private ReadOnlySpan<byte> _currentSpan;
private int _index;
Expand All @@ -17,7 +29,7 @@ public ref struct BufferReader
private int _consumedBytes;
private bool _end;

public BufferReader(ReadOnlySequence<byte> buffer)
public ByteBufferReader(ReadOnlySequence<byte> buffer)
{
_end = false;
_index = 0;
Expand All @@ -29,11 +41,6 @@ public BufferReader(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 @@ -127,7 +134,7 @@ public void Advance(int byteCount)
}
}

internal static int Peek(BufferReader bytes, Span<byte> destination)
internal static int Peek(ByteBufferReader 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
@@ -1,8 +1,31 @@
// 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.Operations
namespace System.Buffers.Transformations
{
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,9 +3,7 @@
// See the LICENSE file in the project root for more information.


using System.Buffers.Text;

namespace System.Buffers.Writer
namespace System.Buffers.Text
{
public ref partial struct BufferWriter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +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.Text;

namespace System.Buffers.Writer
namespace System.Buffers.Text
{
public ref partial struct BufferWriter
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +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.Text;

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

using System.Buffers.Text;
using System.Text.Utf8;

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


using System.Buffers.Text;

namespace System.Buffers.Writer
namespace System.Buffers.Text
{
public ref partial struct BufferWriter
{
Expand Down Expand Up @@ -37,28 +35,6 @@ 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.Writer
namespace System.Buffers
{
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.Writer
namespace System.Buffers
{
public static class OutputWriter
{
Expand Down
Loading

0 comments on commit 4f02651

Please sign in to comment.