From d2264af73d5ee7275d636cf48e7ca5ac4331900c Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Mon, 12 Mar 2018 10:41:09 -0700 Subject: [PATCH] Cleaned up Reader/Writer Project (#2156) --- .../Azure/CosmosDbAuthorizationHeader.cs | 1 + .../System/Azure/StorageAccessSignature.cs | 6 ++--- src/System.Binary.Base64/Base64Decoder.cs | 1 + src/System.Binary.Base64/Base64Encoder.cs | 1 + .../System/Buffers/BufferExtensions.cs | 1 + .../System/Buffers/IBufferOperation.cs | 2 +- .../System/Buffers/IBufferTransformation.cs | 2 +- ...eadOnlyBufferReader.cs => BufferReader.cs} | 25 +++++++----------- .../Buffers/Reader/BufferReader_binary.cs | 13 +++++++--- .../Buffers/Reader/BufferReader_search.cs | 6 ++--- .../Buffers/Reader/BufferReader_text.cs | 15 +++++------ .../Transformations/RemoveTranstomation.cs | 25 +----------------- .../System/Buffers/Writer/BufferWriter.cs | 4 ++- .../Buffers/Writer/BufferWriter_datetime.cs | 4 ++- .../Buffers/Writer/BufferWriter_ints.cs | 4 ++- .../Buffers/Writer/BufferWriter_sequence.cs | 3 ++- .../Buffers/Writer/BufferWriter_strings.cs | 3 ++- .../Buffers/Writer/BufferWriter_writable.cs | 26 ++++++++++++++++++- .../System/Buffers/Writer/OutputExtensions.cs | 2 +- .../System/Buffers/Writer/OutputWriter.cs | 2 +- .../Buffers/Writer/TransformationFormat.cs | 3 ++- .../Utf8BufferEncoder.cs | 1 + src/System.Text.Http.Parser/HttpParser.cs | 3 ++- .../Text/Http/BufferWriterHttpExtensions.cs | 2 +- .../Text/Encoders/Ascii/Ascii_casing.cs | 2 ++ tests/Benchmarks/BytesReaderBench.cs | 1 + .../BufferWriterTests.cs | 7 ++--- .../BufferWriterTests_sequence.cs | 11 ++++---- .../BytesReaderTests.cs | 3 +-- .../ReadableBufferReaderFacts.cs | 5 ++-- .../PipeThroughput.cs | 1 + .../ReadCursorOperationsThroughput.cs | 1 + 32 files changed, 103 insertions(+), 83 deletions(-) rename src/System.Buffers.ReaderWriter/System/Buffers/Reader/{ReadOnlyBufferReader.cs => BufferReader.cs} (89%) diff --git a/src/System.Azure.Experimental/System/Azure/CosmosDbAuthorizationHeader.cs b/src/System.Azure.Experimental/System/Azure/CosmosDbAuthorizationHeader.cs index 1ec105a6f6c..e2728cd0ec5 100644 --- a/src/System.Azure.Experimental/System/Azure/CosmosDbAuthorizationHeader.cs +++ b/src/System.Azure.Experimental/System/Azure/CosmosDbAuthorizationHeader.cs @@ -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; diff --git a/src/System.Azure.Experimental/System/Azure/StorageAccessSignature.cs b/src/System.Azure.Experimental/System/Azure/StorageAccessSignature.cs index 00b0cf0f925..50fd5b97b09 100644 --- a/src/System.Azure.Experimental/System/Azure/StorageAccessSignature.cs +++ b/src/System.Azure.Experimental/System/Azure/StorageAccessSignature.cs @@ -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 { @@ -20,7 +20,7 @@ public struct StorageAuthorizationHeader : IWritable public ReadOnlyMemory HttpVerb; public string AccountName; public string CanonicalizedResource; - public WritableBytes CanonicalizedHeaders; + public ReadOnlyMemory CanonicalizedHeaders; public long ContentLength; public bool TryWrite(Span buffer, out int written, StandardFormat format = default) diff --git a/src/System.Binary.Base64/Base64Decoder.cs b/src/System.Binary.Base64/Base64Decoder.cs index 31c1bc76521..f0192208043 100644 --- a/src/System.Binary.Base64/Base64Decoder.cs +++ b/src/System.Binary.Base64/Base64Decoder.cs @@ -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 diff --git a/src/System.Binary.Base64/Base64Encoder.cs b/src/System.Binary.Base64/Base64Encoder.cs index 892fe61480c..70b45c52800 100644 --- a/src/System.Binary.Base64/Base64Encoder.cs +++ b/src/System.Binary.Base64/Base64Encoder.cs @@ -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; diff --git a/src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs b/src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs index afa19bfb2b3..fcba4da829f 100644 --- a/src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs +++ b/src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs @@ -6,6 +6,7 @@ using System.Runtime.CompilerServices; using System.Numerics; using System.Runtime.InteropServices; +using System.Buffers.Operations; namespace System.Buffers { diff --git a/src/System.Buffers.Primitives/System/Buffers/IBufferOperation.cs b/src/System.Buffers.Primitives/System/Buffers/IBufferOperation.cs index 8b4e113c973..82dd2a0086f 100644 --- a/src/System.Buffers.Primitives/System/Buffers/IBufferOperation.cs +++ b/src/System.Buffers.Primitives/System/Buffers/IBufferOperation.cs @@ -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 { diff --git a/src/System.Buffers.Primitives/System/Buffers/IBufferTransformation.cs b/src/System.Buffers.Primitives/System/Buffers/IBufferTransformation.cs index bce7cfb8355..ac9072b8190 100644 --- a/src/System.Buffers.Primitives/System/Buffers/IBufferTransformation.cs +++ b/src/System.Buffers.Primitives/System/Buffers/IBufferTransformation.cs @@ -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 { diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Reader/ReadOnlyBufferReader.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader.cs similarity index 89% rename from src/System.Buffers.ReaderWriter/System/Buffers/Reader/ReadOnlyBufferReader.cs rename to src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader.cs index fb8d006d0b9..3b6fb62eaa4 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Reader/ReadOnlyBufferReader.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader.cs @@ -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 buffer) - { - return new ByteBufferReader(buffer); - } - - public static int Peek(ByteBufferReader reader, Span destination) - => ByteBufferReader.Peek(reader, destination); - } - - public ref struct ByteBufferReader + public ref struct BufferReader { private ReadOnlySpan _currentSpan; private int _index; @@ -29,7 +17,7 @@ public ref struct ByteBufferReader private int _consumedBytes; private bool _end; - public ByteBufferReader(ReadOnlySequence buffer) + public BufferReader(ReadOnlySequence buffer) { _end = false; _index = 0; @@ -41,6 +29,11 @@ public ByteBufferReader(ReadOnlySequence buffer) MoveNext(); } + public static BufferReader Create(ReadOnlySequence buffer) + { + return new BufferReader(buffer); + } + public bool End => _end; public int CurrentSegmentIndex => _index; @@ -134,7 +127,7 @@ public void Advance(int byteCount) } } - internal static int Peek(ByteBufferReader bytes, Span destination) + internal static int Peek(BufferReader bytes, Span destination) { var first = bytes.UnreadSegment; if (first.Length > destination.Length) diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader_binary.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader_binary.cs index 66a046762f7..43cb7b48e53 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader_binary.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader_binary.cs @@ -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) @@ -26,7 +25,7 @@ public static bool TryRead(ref ByteBufferReader reader, out int value, bool litt } Span tempSpan = stackalloc byte[4]; - var copied = BufferReader.Peek(reader, tempSpan); + var copied = BufferReaderExtensions.Peek(reader, tempSpan); if (copied < 4) { value = default; @@ -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 destination) + => BufferReader.Peek(reader, destination); + } } diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader_search.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader_search.cs index a4af2048177..f734935f0c1 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader_search.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader_search.cs @@ -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 bytes, byte delimiter) + public static bool TryReadUntill(ref BufferReader reader, out ReadOnlySequence bytes, byte delimiter) { var copy = reader; var start = reader.Position; @@ -26,7 +26,7 @@ public static bool TryReadUntill(ref ByteBufferReader reader, out ReadOnlySequen return false; } - public static bool TryReadUntill(ref ByteBufferReader reader, out ReadOnlySequence bytes, ReadOnlySpan delimiter) + public static bool TryReadUntill(ref BufferReader reader, out ReadOnlySequence bytes, ReadOnlySpan delimiter) { if (delimiter.Length == 0) { diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader_text.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader_text.cs index 990284b4afe..e788d9c2c11 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader_text.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Reader/BufferReader_text.cs @@ -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)) @@ -21,7 +20,7 @@ public static bool TryParse(ref ByteBufferReader reader, out bool value) } Span 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); @@ -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)) @@ -44,7 +43,7 @@ public static bool TryParse(ref ByteBufferReader reader, out int value) } Span 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); @@ -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)) @@ -67,7 +66,7 @@ public static bool TryParse(ref ByteBufferReader reader, out ulong value) } Span 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); diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Transformations/RemoveTranstomation.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Transformations/RemoveTranstomation.cs index 48b54fc4646..03ffa7ba6ec 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Transformations/RemoveTranstomation.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Transformations/RemoveTranstomation.cs @@ -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 _bytes; - - public WritableBytes(ReadOnlyMemory bytes) - { - _bytes = bytes; - } - - public bool TryWrite(Span 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; diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter.cs index 5942fea190d..08b7be670fd 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter.cs @@ -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 { diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_datetime.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_datetime.cs index d3ef09e1111..feb668d6c34 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_datetime.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_datetime.cs @@ -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 { diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_ints.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_ints.cs index 08680f9c196..1887e4001ca 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_ints.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_ints.cs @@ -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 { diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_sequence.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_sequence.cs index d4fe4faf0db..97678ad1469 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_sequence.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_sequence.cs @@ -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 where TOutput : IBufferWriter { diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_strings.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_strings.cs index e6e8a84e09e..dfabf4d6daf 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_strings.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_strings.cs @@ -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 { diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_writable.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_writable.cs index 5ad309229c5..77c1d501a43 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_writable.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/BufferWriter_writable.cs @@ -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 { @@ -35,6 +37,28 @@ public bool TryWriteBytes(ReadOnlyMemory bytes) public void WriteBytes(ReadOnlyMemory bytes) => WriteBytes(bytes.Span); + public bool TryWriteBytes(ReadOnlyMemory 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 bytes, TransformationFormat format) + { + while (!TryWriteBytes(bytes, format)) Resize(); + } + #endregion #region IWritable diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/OutputExtensions.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/OutputExtensions.cs index f09068f370f..f0e898453b1 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/OutputExtensions.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/OutputExtensions.cs @@ -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 { diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/OutputWriter.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/OutputWriter.cs index 234d90e9563..f25bdfae7b1 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/OutputWriter.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/OutputWriter.cs @@ -4,7 +4,7 @@ using System.Runtime.CompilerServices; -namespace System.Buffers +namespace System.Buffers.Writer { public static class OutputWriter { diff --git a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/TransformationFormat.cs b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/TransformationFormat.cs index 694826eb466..2a2b3fe790a 100644 --- a/src/System.Buffers.ReaderWriter/System/Buffers/Writer/TransformationFormat.cs +++ b/src/System.Buffers.ReaderWriter/System/Buffers/Writer/TransformationFormat.cs @@ -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.Operations; using System.Runtime.CompilerServices; -namespace System.Buffers +namespace System.Buffers.Writer { public readonly struct TransformationFormat { diff --git a/src/System.Text.Encodings.Web.Utf8/Utf8BufferEncoder.cs b/src/System.Text.Encodings.Web.Utf8/Utf8BufferEncoder.cs index c7216c5d9c9..0a83260cc33 100644 --- a/src/System.Text.Encodings.Web.Utf8/Utf8BufferEncoder.cs +++ b/src/System.Text.Encodings.Web.Utf8/Utf8BufferEncoder.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Buffers; +using System.Buffers.Operations; namespace System.Text.Encodings.Web { diff --git a/src/System.Text.Http.Parser/HttpParser.cs b/src/System.Text.Http.Parser/HttpParser.cs index dd4c275ec29..49e6e3dba03 100644 --- a/src/System.Text.Http.Parser/HttpParser.cs +++ b/src/System.Text.Http.Parser/HttpParser.cs @@ -2,6 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Buffers; +using System.Buffers.Reader; using System.Buffers.Text; using System.Numerics; using System.Runtime.CompilerServices; @@ -242,7 +243,7 @@ public unsafe bool ParseHeaders(T handler, in ReadOnlySequence buffer, var bufferEnd = buffer.End; var reader = BufferReader.Create(buffer); - var start = default(ByteBufferReader); + var start = default(BufferReader); var done = false; try diff --git a/src/System.Text.Http/System/Text/Http/BufferWriterHttpExtensions.cs b/src/System.Text.Http/System/Text/Http/BufferWriterHttpExtensions.cs index 303516a37b5..c837a541993 100644 --- a/src/System.Text.Http/System/Text/Http/BufferWriterHttpExtensions.cs +++ b/src/System.Text.Http/System/Text/Http/BufferWriterHttpExtensions.cs @@ -2,7 +2,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.Buffers; -using System.Buffers.Text; +using System.Buffers.Writer; namespace System.Text.Http.Formatter { diff --git a/src/System.Text.Primitives/System/Text/Encoders/Ascii/Ascii_casing.cs b/src/System.Text.Primitives/System/Text/Encoders/Ascii/Ascii_casing.cs index 0f2645e6c02..21e96f2d30b 100644 --- a/src/System.Text.Primitives/System/Text/Encoders/Ascii/Ascii_casing.cs +++ b/src/System.Text.Primitives/System/Text/Encoders/Ascii/Ascii_casing.cs @@ -2,6 +2,8 @@ // 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.Operations; + namespace System.Buffers.Text { public static partial class Encodings diff --git a/tests/Benchmarks/BytesReaderBench.cs b/tests/Benchmarks/BytesReaderBench.cs index c990676f421..980c0887531 100644 --- a/tests/Benchmarks/BytesReaderBench.cs +++ b/tests/Benchmarks/BytesReaderBench.cs @@ -5,6 +5,7 @@ using Microsoft.Xunit.Performance; using System; using System.Buffers; +using System.Buffers.Reader; using System.Buffers.Text; using System.Text; diff --git a/tests/System.Buffers.Experimental.Tests/BufferWriterTests.cs b/tests/System.Buffers.Experimental.Tests/BufferWriterTests.cs index 8dc19e97050..29c1ceee755 100644 --- a/tests/System.Buffers.Experimental.Tests/BufferWriterTests.cs +++ b/tests/System.Buffers.Experimental.Tests/BufferWriterTests.cs @@ -3,6 +3,7 @@ using System.Binary.Base64Experimental; using System.Buffers.Text; +using System.Buffers.Writer; using System.Text; using Xunit; using static System.Buffers.Binary.BinaryPrimitives; @@ -17,7 +18,7 @@ public partial class BufferWriterTests public void Basics() { Span buffer = stackalloc byte[256]; - var writer = Text.BufferWriter.Create(buffer); + var writer = BufferWriter.Create(buffer); writer.WrittenCount = 0; writer.Write("AaBc", new TransformationFormat(Encodings.Ascii.ToLowercase, Encodings.Ascii.ToUppercase)); @@ -44,7 +45,7 @@ public void Basics() public void Writable() { Span buffer = stackalloc byte[256]; - var writer = Text.BufferWriter.Create(buffer); + var writer = BufferWriter.Create(buffer); var ulonger = new UInt128(); ulonger.Lower = ulong.MaxValue; @@ -65,7 +66,7 @@ public void WriteDateTime() { var now = DateTime.UtcNow; Span buffer = stackalloc byte[256]; - var writer = Text.BufferWriter.Create(buffer); + var writer = BufferWriter.Create(buffer); writer.WriteLine(now, 'R'); var result = Encodings.Utf8.ToString(writer.Written); Assert.Equal(string.Format("{0:R}\n", now), result); diff --git a/tests/System.Buffers.Experimental.Tests/BufferWriterTests_sequence.cs b/tests/System.Buffers.Experimental.Tests/BufferWriterTests_sequence.cs index f3dda11979e..3a787e0055e 100644 --- a/tests/System.Buffers.Experimental.Tests/BufferWriterTests_sequence.cs +++ b/tests/System.Buffers.Experimental.Tests/BufferWriterTests_sequence.cs @@ -3,6 +3,7 @@ using System.Binary.Base64Experimental; using System.Buffers.Text; +using System.Buffers.Writer; using System.Collections.Generic; using System.Text; using Xunit; @@ -16,8 +17,8 @@ public partial class GenericBufferWriterTests [Fact] public void Bytes() { - IBufferWriter bufferWriter = new BufferWriter(); - var writer = Text.BufferWriter.Create(bufferWriter); + IBufferWriter bufferWriter = new TestBufferWriter(); + var writer = BufferWriter.Create(bufferWriter); writer.WriteBytes(Encoding.UTF8.GetBytes("Hello")); writer.WriteBytes(Encoding.UTF8.GetBytes(" World!")); writer.Flush(); @@ -27,8 +28,8 @@ public void Bytes() [Fact] public void Writable() { - IBufferWriter bufferWriter = new BufferWriter(); - var writer = Text.BufferWriter.Create(bufferWriter); + IBufferWriter bufferWriter = new TestBufferWriter(); + var writer = BufferWriter.Create(bufferWriter); var ulonger = new UInt128(); ulonger.Lower = ulong.MaxValue; @@ -43,7 +44,7 @@ public void Writable() } } - class BufferWriter : IBufferWriter + class TestBufferWriter : IBufferWriter { byte[] _current = new byte[0]; List _commited = new List(); diff --git a/tests/System.Buffers.Experimental.Tests/BytesReaderTests.cs b/tests/System.Buffers.Experimental.Tests/BytesReaderTests.cs index a228dea1766..be8c114a5b7 100644 --- a/tests/System.Buffers.Experimental.Tests/BytesReaderTests.cs +++ b/tests/System.Buffers.Experimental.Tests/BytesReaderTests.cs @@ -1,9 +1,8 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System.Buffers; +using System.Buffers.Reader; using System.Collections.Generic; -using System.Collections.Sequences; using System.Text; using Xunit; diff --git a/tests/System.Buffers.Primitives.Tests/ReadableBufferReaderFacts.cs b/tests/System.Buffers.Primitives.Tests/ReadableBufferReaderFacts.cs index 60485e95aa4..aa8ed945b0c 100644 --- a/tests/System.Buffers.Primitives.Tests/ReadableBufferReaderFacts.cs +++ b/tests/System.Buffers.Primitives.Tests/ReadableBufferReaderFacts.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Buffers; +using System.Buffers.Reader; using System.IO.Pipelines.Testing; using Xunit; @@ -310,7 +311,7 @@ public void CopyToLargerBufferWorks() for (int i = 0; i < content.Length; i++) { - int copied = BufferReader.Peek(reader, buffer); + int copied = BufferReaderExtensions.Peek(reader, buffer); Assert.Equal(content.Length - i, copied); Assert.True(buffer.Slice(0, copied).SequenceEqual(content.AsSpan().Slice(i))); @@ -341,7 +342,7 @@ public void CopyToSmallerBufferWorks() { var bufferSlice = buffer.Slice(0, j); bufferSlice.Clear(); - int copied = BufferReader.Peek(reader, bufferSlice); + int copied = BufferReaderExtensions.Peek(reader, bufferSlice); Assert.Equal(Math.Min(bufferSlice.Length, content.Length - i), copied); Assert.True(bufferSlice.Slice(0, copied).SequenceEqual(content.AsSpan().Slice(i, j))); diff --git a/tests/System.IO.Pipelines.Performance.Tests/PipeThroughput.cs b/tests/System.IO.Pipelines.Performance.Tests/PipeThroughput.cs index f3dde48bd20..b4e3cf6f851 100644 --- a/tests/System.IO.Pipelines.Performance.Tests/PipeThroughput.cs +++ b/tests/System.IO.Pipelines.Performance.Tests/PipeThroughput.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Buffers; +using System.Buffers.Writer; using System.Threading.Tasks; using BenchmarkDotNet.Attributes; diff --git a/tests/System.IO.Pipelines.Performance.Tests/ReadCursorOperationsThroughput.cs b/tests/System.IO.Pipelines.Performance.Tests/ReadCursorOperationsThroughput.cs index 9fb32ccb6fe..7a9e359e847 100644 --- a/tests/System.IO.Pipelines.Performance.Tests/ReadCursorOperationsThroughput.cs +++ b/tests/System.IO.Pipelines.Performance.Tests/ReadCursorOperationsThroughput.cs @@ -1,4 +1,5 @@ using System.Buffers; +using System.Buffers.Reader; using System.Collections.Generic; using System.IO.Pipelines.Testing; using System.Linq;