Exemplo n.º 1
0
 public ReadOnlyBytes(ReadOnlyBuffer <byte> first, IReadOnlyBufferList <byte> rest, int length)
 {
     Debug.Assert(rest != null || length <= first.Length);
     _rest        = rest;
     _first       = first;
     _totalLength = length;
 }
Exemplo n.º 2
0
        public static int IndexOf(this IReadOnlyBufferList <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);
        }
Exemplo n.º 3
0
 private ReadOnlyBytes(ReadOnlyMemory <byte> first, IReadOnlyBufferList <byte> all, long length)
 {
     // TODO: add assert that first overlaps all (once we have Overlap on Span)
     _first       = first;
     _all         = all;
     _totalLength = length;
 }
Exemplo n.º 4
0
        public static int IndexOf(this IReadOnlyBufferList <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));
        }
Exemplo n.º 5
0
        int IndexOf(IReadOnlyBufferList <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);
        }
Exemplo n.º 6
0
        // 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, IReadOnlyBufferList <byte> rest, ReadOnlySpan <byte> value)
        {
            Debug.Assert(first.IndexOf(value) == -1);
            if (rest == null)
            {
                return(-1);
            }

            // 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)
            {
                var combinedBufferLength = value.Length << 1;
                var combined             = combinedBufferLength < 128 ?
                                           stackalloc byte[combinedBufferLength] :
                                           // TODO (pri 3): I think this could be eliminated by chunking values
                                           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);
        }
Exemplo n.º 7
0
        private static unsafe void ReadNextTwoChars(byte *pBuffer, int remaining, int index, out int ch1, out int ch2, IReadOnlyBufferList <byte> rest)
        {
            if (remaining == 1)
            {
                ch1 = pBuffer[index];
                if (rest == null)
                {
                    ch2 = -1;
                }
                else
                {
                    ch2 = rest.First.Span[0];
                }
            }
            else
            {
                ch1 = -1;
                ch2 = -1;
                if (rest != null)
                {
                    var nextSpan = rest.First.Span;
                    if (nextSpan.Length > 0)
                    {
                        ch1 = nextSpan[0];

                        if (nextSpan.Length > 1)
                        {
                            ch2 = nextSpan[1];
                        }
                        else
                        {
                            ch2 = -1;
                        }
                    }
                    else
                    {
                        ch2 = -1;
                    }
                }
            }
        }
Exemplo n.º 8
0
 public BytesReader(IReadOnlyBufferList <byte> bytes, SymbolTable encoder) : this(new ReadOnlyBytes(bytes))
 {
 }
Exemplo n.º 9
0
 public BytesReader(IReadOnlyBufferList <byte> bytes) : this(bytes, SymbolTable.InvariantUtf8)
 {
 }
Exemplo n.º 10
0
 public ReadOnlyBytes(IReadOnlyBufferList <byte> segments) :
     this(segments.First, segments.Rest, Unspecified)
 {
 }
Exemplo n.º 11
0
 public ReadOnlyBytes(IReadOnlyBufferList <byte> segments, int length) :
     this(segments.First, segments.Rest, length)
 {
 }
Exemplo n.º 12
0
 public ReadOnlyBytes(ReadOnlyBuffer <byte> first, IReadOnlyBufferList <byte> rest) :
     this(first, rest, rest == null?first.Length:Unspecified)
 {
 }
Exemplo n.º 13
0
 public ReadOnlyBytes(IReadOnlyBufferList <byte> segments, long length)
 {
     _first       = segments.First;
     _all         = segments;
     _totalLength = length;
 }
Exemplo n.º 14
0
 public ReadOnlyBytes(ReadOnlyMemory <byte> buffer)
 {
     _first       = buffer;
     _all         = null;
     _totalLength = _first.Length;
 }
Exemplo n.º 15
0
 public BytesReader(IReadOnlyBufferList <byte> bytes, TextEncoder encoder) : this(new ReadOnlyBytes(bytes))
 {
 }
Exemplo n.º 16
0
 public BytesReader(IReadOnlyBufferList <byte> bytes) : this(bytes, TextEncoder.Utf8)
 {
 }