Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Add SequenceReader<T> #33288

Merged
merged 10 commits into from
Nov 10, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/Microsoft.Private.PackageBaseline/packageIndex.json
Original file line number Diff line number Diff line change
Expand Up @@ -2543,6 +2543,7 @@
],
"InboxOn": {
"netcoreapp2.1": "4.1.0.0",
"netcoreapp3.0": "4.2.0.0",
JeremyKuhne marked this conversation as resolved.
Show resolved Hide resolved
"monoandroid10": "Any",
"monotouch10": "Any",
"uap10.0.16300": "4.1.0.0",
Expand All @@ -2553,7 +2554,8 @@
},
"AssemblyVersionInPackageVersion": {
"4.0.1.0": "4.5.0",
"4.1.0.0": "4.6.0"
"4.1.0.0": "4.6.0",
"4.2.0.0": "4.7.0"
JeremyKuhne marked this conversation as resolved.
Show resolved Hide resolved
}
},
"System.Messaging": {
Expand Down
2 changes: 1 addition & 1 deletion src/System.Memory/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<PropertyGroup>
<!-- System.Memory has forwarded types into the runtime on netcoreapp/uap
It must win over assemblies versioned at 4.0.* -->
<AssemblyVersion>4.1.0.0</AssemblyVersion>
<AssemblyVersion>4.2.0.0</AssemblyVersion>
JeremyKuhne marked this conversation as resolved.
Show resolved Hide resolved
<StrongNameKeyId>Open</StrongNameKeyId>
<IsNETCoreApp>true</IsNETCoreApp>
<IsUAP>true</IsUAP>
Expand Down
49 changes: 45 additions & 4 deletions src/System.Memory/ref/System.Memory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,10 @@ public static void Reverse<T>(this System.Span<T> span) { }
private readonly object _dummy;
private readonly int _dummyPrimitive;
public SequencePosition(object @object, int integer) { throw null; }
[System.ComponentModel.EditorBrowsableAttribute((System.ComponentModel.EditorBrowsableState)(1))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did this end up getting removed? I would revert this change, build the ref and src, and then run the generate command from within the ref directory. Please let me know if you are still seeing such side effects/unintended changes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The build target did it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Manually added them back for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Plz revert.

public override bool Equals(object obj) { throw null; }
public bool Equals(System.SequencePosition other) { throw null; }
[System.ComponentModel.EditorBrowsableAttribute((System.ComponentModel.EditorBrowsableState)(1))]
public override int GetHashCode() { throw null; }
[System.ComponentModel.EditorBrowsableAttribute((System.ComponentModel.EditorBrowsableState)(1))]
public int GetInteger() { throw null; }
[System.ComponentModel.EditorBrowsableAttribute((System.ComponentModel.EditorBrowsableState)(1))]
public object GetObject() { throw null; }
}
}
Expand Down Expand Up @@ -184,6 +180,51 @@ public partial struct Enumerator
public bool MoveNext() { throw null; }
}
}
public static partial class SequenceReaderExtensions
{
public static bool TryReadBigEndian(this ref System.Buffers.SequenceReader<byte> reader, out short value) { throw null; }
public static bool TryReadBigEndian(this ref System.Buffers.SequenceReader<byte> reader, out int value) { throw null; }
public static bool TryReadBigEndian(this ref System.Buffers.SequenceReader<byte> reader, out long value) { throw null; }
public static bool TryReadLittleEndian(this ref System.Buffers.SequenceReader<byte> reader, out short value) { throw null; }
public static bool TryReadLittleEndian(this ref System.Buffers.SequenceReader<byte> reader, out int value) { throw null; }
public static bool TryReadLittleEndian(this ref System.Buffers.SequenceReader<byte> reader, out long value) { throw null; }
public static bool TryRead<T>(this ref System.Buffers.SequenceReader<byte> reader, out T value) where T : struct { throw null; }
JeremyKuhne marked this conversation as resolved.
Show resolved Hide resolved
}
public ref partial struct SequenceReader<T> where T : struct, System.IEquatable<T>
{
public SequenceReader(System.Buffers.ReadOnlySequence<T> buffer) { throw null; }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regenerate based on new sources so that arg names match.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs fixing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, sorry, missed this comment. I'll fix shortly.

public long Consumed { get { throw null; } }
JeremyKuhne marked this conversation as resolved.
Show resolved Hide resolved
public System.ReadOnlySpan<T> CurrentSpan { get { throw null; } }
public int CurrentSpanIndex { get { throw null; } }
public bool End { get { throw null; } }
public long Length { get { throw null; } }
public System.SequencePosition Position { get { throw null; } }
public long Remaining { get { throw null; } }
public System.Buffers.ReadOnlySequence<T> Sequence { get { throw null; } }
public System.ReadOnlySpan<T> UnreadSpan { get { throw null; } }
public void Advance(long count) { }
public long AdvancePast(T value) { throw null; }
public long AdvancePastAny(System.ReadOnlySpan<T> values) { throw null; }
public long AdvancePastAny(T value0, T value1) { throw null; }
public long AdvancePastAny(T value0, T value1, T value2) { throw null; }
public long AdvancePastAny(T value0, T value1, T value2, T value3) { throw null; }
public bool IsNext(System.ReadOnlySpan<T> next, bool advancePast = false) { throw null; }
public bool IsNext(T next, bool advancePast = false) { throw null; }
public void Rewind(long count) { }
public bool TryAdvanceTo(T delimiter, bool advancePastDelimiter = true) { throw null; }
public bool TryAdvanceToAny(System.ReadOnlySpan<T> delimiters, bool advancePastDelimiter = true) { throw null; }
public bool TryCopyTo(System.Span<T> destination) { throw null; }
public bool TryPeek(out T value) { throw null; }
public bool TryRead(out T value) { throw null; }
public bool TryReadTo(out System.Buffers.ReadOnlySequence<T> sequence, System.ReadOnlySpan<T> delimiter, bool advancePastDelimiter = true) { throw null; }
public bool TryReadTo(out System.Buffers.ReadOnlySequence<T> sequence, T delimiter, bool advancePastDelimiter = true) { throw null; }
public bool TryReadTo(out System.Buffers.ReadOnlySequence<T> sequence, T delimiter, T delimiterEscape, bool advancePastDelimiter = true) { throw null; }
public bool TryReadTo(out System.ReadOnlySpan<T> span, T delimiter) { throw null; }
public bool TryReadTo(out System.ReadOnlySpan<T> span, T delimiter, bool advancePastDelimiter) { throw null; }
public bool TryReadTo(out System.ReadOnlySpan<T> span, T delimiter, T delimiterEscape, bool advancePastDelimiter = true) { throw null; }
public bool TryReadToAny(out System.Buffers.ReadOnlySequence<T> sequence, System.ReadOnlySpan<T> delimiters, bool advancePastDelimiter = true) { throw null; }
public bool TryReadToAny(out System.ReadOnlySpan<T> span, System.ReadOnlySpan<T> delimiters, bool advancePastDelimiter = true) { throw null; }
}
public readonly partial struct StandardFormat : System.IEquatable<System.Buffers.StandardFormat>
{
private readonly int _dummyPrimitive;
Expand Down
5 changes: 4 additions & 1 deletion src/System.Memory/src/System.Memory.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<ProjectGuid>{4BBC8F69-D03E-4432-AA8A-D458FA5B235A}</ProjectGuid>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
Expand All @@ -20,6 +20,9 @@
<Compile Include="System\Buffers\ReadOnlySequenceDebugView.cs" />
<Compile Include="System\Buffers\ReadOnlySequenceSegment.cs" />
<Compile Include="System\Buffers\ReadOnlySequence_helpers.cs" />
<Compile Include="System\Buffers\SequenceReader.cs" />
<Compile Include="System\Buffers\SequenceReader_search.cs" />
<Compile Include="System\Buffers\SequenceReaderExtensions_binary.cs" />
<Compile Include="System\Buffers\Text\Base64Decoder.cs" />
<Compile Include="System\Buffers\Text\Base64Encoder.cs" />
<Compile Include="System\Runtime\InteropServices\SequenceMarshal.cs" />
Expand Down
77 changes: 77 additions & 0 deletions src/System.Memory/src/System/Buffers/ReadOnlySequence_helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Internal.Runtime.CompilerServices;

namespace System.Buffers
Expand Down Expand Up @@ -467,5 +468,81 @@ private static bool InRange(ulong value, ulong start, ulong end)
// Equivalent to: return (start <= value && value <= start)
return (value - start) <= (end - start);
}

/// <summary>
/// Helper to efficiently prepare the <see cref="SequenceReader{T}"/>
/// </summary>
/// <param name="first">The first span in the sequence.</param>
/// <param name="next">The next position.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void GetFirstSpan(out ReadOnlySpan<T> first, out SequencePosition next)
{
first = default;
next = default;
SequencePosition start = Start;
int startIndex = start.GetInteger();
object startObject = start.GetObject();

if (startObject != null)
{
SequencePosition end = End;
int endIndex = end.GetInteger();
bool hasMultipleSegments = startObject != end.GetObject();

if (startIndex >= 0)
{
if (endIndex >= 0)
{
// Positive start and end index == ReadOnlySequenceSegment<T>
ReadOnlySequenceSegment<T> segment = (ReadOnlySequenceSegment<T>)startObject;
next = new SequencePosition(segment.Next, 0);
first = segment.Memory.Span;
if (hasMultipleSegments)
{
first = first.Slice(startIndex);
}
else
{
first = first.Slice(startIndex, endIndex - startIndex);
}
}
else
{
// Positive start and negative end index == T[]
if (hasMultipleSegments)
ThrowHelper.ThrowInvalidOperationException_EndPositionNotReached();

first = new ReadOnlySpan<T>((T[])startObject, startIndex, (endIndex & ReadOnlySequence.IndexBitMask) - startIndex);
}
}
else
{
first = GetFirstSpanSlow(startObject, startIndex, endIndex, hasMultipleSegments);
}
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static ReadOnlySpan<T> GetFirstSpanSlow(object startObject, int startIndex, int endIndex, bool hasMultipleSegments)
{
Debug.Assert(startIndex < 0);
if (hasMultipleSegments)
ThrowHelper.ThrowInvalidOperationException_EndPositionNotReached();

// The type == char check here is redundant. However, we still have it to allow
// the JIT to see when that the code is unreachable and eliminate it.
if (typeof(T) == typeof(char) && endIndex < 0)
{
// Negative start and negative end index == string
ReadOnlySpan<char> spanOfChar = ((string)startObject).AsSpan(startIndex & ReadOnlySequence.IndexBitMask, endIndex - startIndex);
return MemoryMarshal.CreateSpan(ref Unsafe.As<char, T>(ref MemoryMarshal.GetReference(spanOfChar)), spanOfChar.Length);
}
else
{
// Negative start and positive end index == MemoryManager<T>
startIndex &= ReadOnlySequence.IndexBitMask;
return ((MemoryManager<T>)startObject).Memory.Span.Slice(startIndex, endIndex - startIndex);
}
}
}
}
Loading