Пример #1
0
        public IndexOfResult IndexOf(Byte[] eof)
        {
            IndexOfResult result = new IndexOfResult();

            result.EofData = eof;
            if (eof == null || mLength < eof.Length)
            {
                return(result);
            }
            IBuffer rbuffer = GetReadBuffer();

            if (rbuffer == null)
            {
                return(result);
            }
            result.Start        = rbuffer;
            result.StartPostion = rbuffer.Postion;
            if (IndexOf(ref result, rbuffer))
            {
                return(result);
            }
            rbuffer = rbuffer.Next;
            while (rbuffer != null)
            {
                if (IndexOf(ref result, rbuffer))
                {
                    return(result);
                }
                rbuffer = rbuffer.Next;
            }
            return(result);
        }
Пример #2
0
        public IndexOfResult IndexOf(int length)
        {
            IndexOfResult result = new IndexOfResult();

            if (mLength < length)
            {
                return(result);
            }
            IBuffer rbuffer = GetReadBuffer();

            result.Start  = rbuffer;
            result.Length = length;
            if (rbuffer != null)
            {
                result.StartPostion = rbuffer.Postion;
            }
            while (rbuffer != null)
            {
                int count = rbuffer.Length - rbuffer.Postion;
                if (count >= length)
                {
                    result.End        = rbuffer;
                    result.EndPostion = rbuffer.Postion + length - 1;
                    break;
                }
                else
                {
                    length -= count;
                }
                rbuffer = rbuffer.Next;
            }
            return(result);
        }
Пример #3
0
        private bool IndexOf(ref IndexOfResult result, IBuffer buffer)
        {
            int start  = buffer.Postion;
            int length = buffer.Length;

            byte[] data   = buffer.Data;
            byte[] eof    = result.EofData;
            int    eoflen = eof.Length;

            result.End = buffer;
            for (int i = start; i < length; i++)
            {
                result.Length++;
                if (data[i] == eof[result.EofIndex])
                {
                    result.EofIndex++;
                    result.EndPostion = i;
                    if (result.EofIndex == eoflen)
                    {
                        return(true);
                    }
                }
                else
                {
                    result.EofIndex = 0;
                }
            }
            result.End = null;
            return(false);
        }
Пример #4
0
        private bool IndexOf(ref IndexOfResult result, IBuffer buffer)
        {
            int         start  = buffer.Postion;
            int         length = buffer.Length;
            Span <byte> data   = buffer.Memory.Span;
            Span <byte> eof    = result.EofData;
            int         eoflen = eof.Length;

            result.End = buffer;
            int eofindex = result.EofIndex;
            int endpoint = 0;

            for (int i = start; i < length; i++)
            {
                result.Length++;
                if (data[i] == eof[eofindex])
                {
                    eofindex++;
                    endpoint = i;
                    if (eofindex == eoflen)
                    {
                        result.EndPostion = endpoint;
                        return(true);
                    }
                }
                else
                {
                    eofindex = 0;
                }
            }
            result.EofIndex = eofindex;
            result.End      = null;
            return(false);
        }
Пример #5
0
        private bool IndexOf(ref IndexOfResult result, IBuffer buffer)
        {
            int start  = buffer.Postion;
            int length = buffer.Length;

            byte[] data   = buffer.Data;
            byte[] eof    = result.EofData;
            int    eoflen = eof.Length;

            result.End = buffer;
            for (int i = start; i < length; i++)
            {
                result.Length++;
                if (data[i] == eof[result.EofIndex])
                {
                    var len = length - i;
                    if (len > eoflen)
                    {
                        len = eoflen;
                    }
                    result.EofIndex++;
                    result.EndPostion = i;
                    for (int k = 1; k < len; k++)
                    {
                        if (data[i + k] == eof[k])
                        {
                            result.EofIndex++;
                            result.EndPostion = i + k;
                        }
                        else
                        {
                            result.EofIndex   = 0;
                            result.End        = null;
                            result.EndPostion = -1;
                            break;
                        }
                    }
                    if (result.EofIndex == eoflen)
                    {
                        result.Length += eoflen - 1;
                        return(true);
                    }
                }
            }
            return(false);
        }
Пример #6
0
        public bool TryReadWith(byte[] eof, out string value, bool returnEof = false)
        {
            value = null;
            IndexOfResult result = IndexOf(eof);
            int           length = result.Length;

            if (result.End != null)
            {
                if (result.Start.ID == result.End.ID)
                {
                    char[] charSpan = mCharCacheBlock;
                    if (result.Length < mCacheBlockLen)
                    {
                        var len = Encoding.GetChars(result.Start.Data, result.StartPostion, length, charSpan, 0);
                        if (returnEof)
                        {
                            value = new string(charSpan, 0, len);
                        }
                        else
                        {
                            value = new string(charSpan, 0, len - eof.Length);
                        }
                        ReadFree(length);
                    }
                    else
                    {
                        value = ReadString(result.Length);
                    }
                }
                else
                {
                    if (returnEof)
                    {
                        value = ReadString(result.Length);
                    }
                    else
                    {
                        value = ReadString(result.Length - eof.Length);
                        Read(eof, 0, eof.Length);
                    }
                }

                return(true);
            }
            return(false);
        }
Пример #7
0
        public IndexOfResult End()
        {
            IndexOfResult result = new IndexOfResult();

            result.Start        = mStartBuffer;
            result.StartPostion = mStartPostion;
            int length = this.CacheLength - mStartCacheLength;

            result.Length = length;
            IBuffer nextbuf = mStartBuffer;

            while (nextbuf != null)
            {
                if (nextbuf == mStartBuffer)
                {
                    if (mStartBuffer.Postion - result.StartPostion >= length)
                    {
                        result.End        = nextbuf;
                        result.EndPostion = nextbuf.Postion - 1;
                        break;
                    }
                    else
                    {
                        length -= (mStartBuffer.Postion - result.StartPostion);
                    }
                }
                else
                {
                    if (nextbuf.Postion >= length)
                    {
                        result.End        = nextbuf;
                        result.EndPostion = nextbuf.Postion - 1;
                        break;
                    }
                    else
                    {
                        length -= nextbuf.Postion;
                    }
                }
                nextbuf = nextbuf.Next;
            }
            mStartBuffer = null;
            return(result);
        }
Пример #8
0
        public IndexOfResult IndexOf(Byte[] eof)
        {
            int           eoflen = eof.Length;
            int           index  = 0;
            int           len    = 0;
            IndexOfResult result = new IndexOfResult();

            if (mLength < eoflen)
            {
                return(result);
            }
            IBuffer rbuffer = GetReadBuffer();

            result.Start = rbuffer;
            if (rbuffer != null)
            {
                result.StartPostion = rbuffer.Postion;
            }
            while (rbuffer != null)
            {
                ReadOnlySpan <byte> data = rbuffer.Memory.Span;
                for (int i = rbuffer.Postion; i < rbuffer.Length; i++)
                {
                    len++;
                    if (data[i] == eof[index])
                    {
                        index++;
                    }
                    else
                    {
                        index = 0;
                    }
                    if (index == eoflen)
                    {
                        result.EndPostion = i;
                        result.End        = rbuffer;
                        result.Length     = len;
                        return(result);
                    }
                }
                rbuffer = rbuffer.Next;
            }
            return(result);
        }
Пример #9
0
 public bool TryReadWith(byte[] eof, out string value, bool returnEof = false)
 {
     value = null;
     IndexOfResult result = IndexOf(eof);
     if (result.Length > 0)
     {
         if (returnEof)
         {
             value = ReadString(result.Length);
         }
         else
         {
             value = ReadString(result.Length - eof.Length);
             //ReadString(eof.Length);
             Read(eof, 0, eof.Length);
         }
         return true;
     }
     return false;
 }
Пример #10
0
        public bool TryReadWith(byte[] eof, out CharBlock value, Encoding encoding, bool returnEof = false)
        {
            IndexOfResult result = IndexOf(eof);
            int           length = result.Length;

            if (result.End != null)
            {
                if (returnEof)
                {
                    value = ReadChars(length, encoding);
                }
                else
                {
                    value = ReadChars(length - eof.Length, encoding);
                    ReadFree(eof.Length);
                }
                return(true);
            }
            value = default;
            return(false);
        }
Пример #11
0
        public IndexOfResult IndexOf(Byte[] eof)
        {
            IndexOfResult result = new IndexOfResult();

            result.EofData = eof;
            if (eof == null || mLength < eof.Length)
            {
                return(result);
            }
            IBuffer rbuffer = GetReadBuffer();

            if (rbuffer == null)
            {
                return(result);
            }
            result.Start        = rbuffer;
            result.StartPostion = rbuffer.Postion;
            int bufferLen = 0;
            int eofOffset = 0;

            byte[] bufferdata = null;
            while (rbuffer != null)
            {
                bufferLen  = rbuffer.Length;
                bufferdata = rbuffer.Data;
                int?offset = null;
                while (true)
                {
                    var index = rbuffer.IndexOf(eof[0], offset);
                    if (index != -1)
                    {
                        offset = index + 1;
                        if (eof.Length == 1)
                        {
                            result.End        = rbuffer;
                            result.EndPostion = index;
                            result.Length     = GetLength((IBuffer)result.Start, (IBuffer)result.End, result.EndPostion);
                            return(result);
                        }
                        eofOffset = 1;
                        for (int i = offset.Value; i < bufferLen; i++)
                        {
                            if (eof[eofOffset] == bufferdata[i])
                            {
                                eofOffset++;
                                if (eofOffset == eof.Length)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                eofOffset = -1;
                                break;
                            }
                        }
                        if (eofOffset == eof.Length)
                        {
                            result.End        = rbuffer;
                            result.EndPostion = index + eofOffset - 1;
                            result.Length     = GetLength((IBuffer)result.Start, (IBuffer)result.End, result.EndPostion);
                            return(result);
                        }
                        else if (eofOffset > 0)
                        {
                            if (rbuffer.Next != null)
                            {
                                var startW = rbuffer.Next.StartWith(eof, eofOffset);
                                if (startW != -1)
                                {
                                    result.End        = rbuffer.Next;
                                    result.EndPostion = startW;
                                    result.Length     = GetLength((IBuffer)result.Start, (IBuffer)result.End, result.EndPostion);
                                    return(result);
                                }
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                rbuffer = rbuffer.Next;
            }
            return(result);
        }
Пример #12
0
        public IndexOfResult IndexOfLine()
        {
            IndexOfResult result = new IndexOfResult();

            result.EofData = mLineEof;
            if (mLength < 2)
            {
                return(result);
            }
            IBuffer rbuffer = GetReadBuffer();

            if (rbuffer == null)
            {
                return(result);
            }
            result.Start        = rbuffer;
            result.StartPostion = rbuffer.Postion;
            int bufferLen = 0;

            byte[] bufferdata = null;
            while (rbuffer != null)
            {
                bufferLen  = rbuffer.Length;
                bufferdata = rbuffer.Data;
                int?offset = null;
                while (true)
                {
                    var index = rbuffer.IndexOf(13, offset);
                    if (index != -1)
                    {
                        offset = index + 1;
                        if (index + 1 >= bufferLen)
                        {
                            if (rbuffer.Next == null)
                            {
                                return(result);
                            }
                            if (rbuffer.Next.Data[0] == 10)
                            {
                                result.End        = rbuffer.Next;
                                result.EndPostion = 0;
                                result.Length     = GetLength((IBuffer)result.Start, (IBuffer)result.End, result.EndPostion);
                                return(result);
                            }
                        }
                        else
                        {
                            if (bufferdata[index + 1] == 10)
                            {
                                result.End        = rbuffer;
                                result.EndPostion = index + 1;
                                result.Length     = GetLength((IBuffer)result.Start, (IBuffer)result.End, result.EndPostion);
                                return(result);
                            }
                        }
                    }
                    else
                    {
                        break;
                    }
                }
                rbuffer = rbuffer.Next;
            }
            return(result);
        }