コード例 #1
0
 public ReadOnlyBytes(ReadOnlyMemory <byte> first, IReadOnlyMemoryList <byte> rest, int length)
 {
     Debug.Assert(rest != null || length <= first.Length);
     _rest        = rest;
     _first       = first;
     _totalLength = length;
 }
コード例 #2
0
ファイル: BufferExtensions.cs プロジェクト: mjp41/corefxlab
        public static int IndexOf(this IReadOnlyMemoryList <byte> sequence, byte value)
        {
            var first = sequence.First.Span;
            var index = first.IndexOf(value);

            if (index != -1)
            {
                return(index);
            }

            var rest = sequence.Rest;

            if (rest == null)
            {
                return(-1);
            }

            index = rest.IndexOf(value);
            if (index != -1)
            {
                return(first.Length + index);
            }

            return(-1);
        }
コード例 #3
0
ファイル: BufferExtensions.cs プロジェクト: mjp41/corefxlab
        public static int IndexOf(this IReadOnlyMemoryList <byte> sequence, ReadOnlySpan <byte> value)
        {
            var first = sequence.First.Span;
            var index = first.IndexOf(value);

            if (index != -1)
            {
                return(index);
            }

            var rest = sequence.Rest;

            if (rest == null)
            {
                return(-1);
            }

            return(IndexOfStraddling(first, sequence.Rest, value));
        }
コード例 #4
0
ファイル: BytesReader.cs プロジェクト: tmds/corefxlab
        int IndexOf(IReadOnlyMemoryList <byte> sequence, byte value)
        {
            if (sequence == null)
            {
                return(-1);
            }
            var first = sequence.First;
            var index = first.Span.IndexOf(value);

            if (index > -1)
            {
                return(index);
            }

            var indexOfRest = IndexOf(sequence.Rest, value);

            if (indexOfRest < 0)
            {
                return(-1);
            }
            return(first.Length + indexOfRest);
        }
コード例 #5
0
ファイル: BufferExtensions.cs プロジェクト: mjp41/corefxlab
        // TODO (pri 3): I am pretty sure this whole routine can be written much better

        // searches values that potentially straddle between first and rest
        internal static int IndexOfStraddling(this ReadOnlySpan <byte> first, IReadOnlyMemoryList <byte> rest, ReadOnlySpan <byte> value)
        {
            Debug.Assert(rest != null);

            // we only need to search the end of the first buffer. More precisely, only up to value.Length - 1 bytes in the first buffer
            // The other bytes in first, were already search and presumably did not match
            int bytesToSkipFromFirst = 0;

            if (first.Length > value.Length - 1)
            {
                bytesToSkipFromFirst = first.Length - value.Length - 1;
            }

            // now that we know how many bytes we need to skip, create slice of first buffer with bytes that need to be searched.
            ReadOnlySpan <byte> bytesToSearchAgain;

            if (bytesToSkipFromFirst > 0)
            {
                bytesToSearchAgain = first.Slice(bytesToSkipFromFirst);
            }
            else
            {
                bytesToSearchAgain = first;
            }

            int index;

            // now combine the bytes from the end of the first buffer with bytes in the rest, and serarch the combined buffer
            // this check is a small optimization: if the first byte from the value does not exist in the bytesToSearchAgain, there is no reason to combine
            if (bytesToSearchAgain.IndexOf(value[0]) != -1)
            {
                Span <byte> combined;
                var         combinedBufferLength = value.Length << 1;
                if (combinedBufferLength < 128)
                {
                    unsafe
                    {
                        byte *temp = stackalloc byte[combinedBufferLength];
                        combined = new Span <byte>(temp, combinedBufferLength);
                    }
                }
                else
                {
                    // TODO (pri 3): I think this could be eliminated by chunking values
                    combined = new byte[combinedBufferLength];
                }

                bytesToSearchAgain.CopyTo(combined);
                int combinedLength = bytesToSearchAgain.Length + rest.CopyTo(combined.Slice(bytesToSearchAgain.Length));
                combined = combined.Slice(0, combinedLength);

                if (combined.Length < value.Length)
                {
                    return(-1);
                }

                index = combined.IndexOf(value);
                if (index != -1)
                {
                    return(index + bytesToSkipFromFirst);
                }
            }

            // try to find the bytes in _rest
            index = rest.IndexOf(value);
            if (index != -1)
            {
                return(first.Length + index);
            }

            return(-1);
        }
コード例 #6
0
ファイル: BytesReader.cs プロジェクト: tmds/corefxlab
 public BytesReader(IReadOnlyMemoryList <byte> bytes, TextEncoder encoder) : this(new ReadOnlyBytes(bytes))
 {
 }
コード例 #7
0
ファイル: BytesReader.cs プロジェクト: tmds/corefxlab
 public BytesReader(IReadOnlyMemoryList <byte> bytes) : this(bytes, TextEncoder.Utf8)
 {
 }
コード例 #8
0
ファイル: ReadOnlyBytes.cs プロジェクト: judypol/corefxlab
 public ReadOnlyBytes(IReadOnlyMemoryList <byte> segments) :
     this(segments.First, segments.Rest, Unspecified)
 {
 }
コード例 #9
0
ファイル: ReadOnlyBytes.cs プロジェクト: judypol/corefxlab
 public ReadOnlyBytes(IReadOnlyMemoryList <byte> segments, int length) :
     this(segments.First, segments.Rest, length)
 {
 }
コード例 #10
0
ファイル: ReadOnlyBytes.cs プロジェクト: judypol/corefxlab
 public ReadOnlyBytes(ReadOnlyMemory <byte> first, IReadOnlyMemoryList <byte> rest) :
     this(first, rest, Unspecified)
 {
 }
コード例 #11
0
ファイル: ReadOnlyBytes.cs プロジェクト: judypol/corefxlab
 public ReadOnlyBytes(ReadOnlyMemory <byte> first, IReadOnlyMemoryList <byte> rest, int length)
 {
     _rest   = rest;
     _first  = first;
     _length = length;
 }
コード例 #12
0
ファイル: BytesReader.cs プロジェクト: mramosMS/corefxlab
 public BytesReader(IReadOnlyMemoryList <byte> bytes, EncodingData encoding) : this(new ReadOnlyBytes(bytes))
 {
 }
コード例 #13
0
ファイル: BytesReader.cs プロジェクト: mramosMS/corefxlab
 public BytesReader(IReadOnlyMemoryList <byte> bytes) : this(bytes, EncodingData.InvariantUtf8)
 {
 }
コード例 #14
0
 public ReadOnlyBytes(ReadOnlyMemory <byte> first, IReadOnlyMemoryList <byte> rest) :
     this(first, rest, rest == null?first.Length:Unspecified)
 {
 }