예제 #1
0
        public void ReadLine(LineInfo line)
        {
            line.LineOffset = _previousBufferLength + _bufferPosition;
            line.LineNumber++;
            line.OffsetCount = 0;
            line.Line        = null;
            var tabIncreaseCount = 0;

            if (_bufferPosition == _bufferLength && !ReadBuffer())
            {
                return;
            }

            bool useBuilder = false;
            int  num;
            char c;

            while (true)
            {
                num = _bufferPosition;
                do
                {
                    c = _buffer[num];
                    if (c == '\r' || c == '\n')
                    {
                        goto IL_4A;
                    }

                    if (c == '\0')
                    {
                        _buffer[num] = '\uFFFD';
                    }

                    num++;
                } while (num < _bufferLength);

                num = _bufferLength - _bufferPosition;
                if (!useBuilder)
                {
                    useBuilder      = true;
                    _builder.Length = 0;
                }

                _builder.Append(_buffer, _bufferPosition, num);
                if (!ReadBuffer())
                {
                    _builder.Append('\n');
                    line.Line = _builder.ToString();
                    return;
                }
            }

IL_4A:
            string result;

            _buffer[num] = '\n';
            if (useBuilder)
            {
                _builder.Append(_buffer, _bufferPosition, num - _bufferPosition + 1);
                result = _builder.ToString();
            }
            else
            {
                result = new string(_buffer, _bufferPosition, num - _bufferPosition + 1);
            }

            _bufferPosition = num + 1;

            if (c == '\r' && (_bufferPosition < _bufferLength || ReadBuffer()) && _buffer[_bufferPosition] == '\n')
            {
                if (line.IsTrackingPositions)
                {
                    line.AddOffset(_previousBufferLength + _bufferPosition - 1 + tabIncreaseCount, 1);
                }

                _bufferPosition++;
            }

            line.Line = result;
        }
예제 #2
0
        public void AddOffset(LineInfo line, int startIndex, int length)
        {
            if (OffsetCount + line.OffsetCount + 2 >= Offsets.Length)
            {
                Array.Resize(ref Offsets, Offsets.Length + line.OffsetCount + 20);
            }

            PositionOffset po1, po2;

            if (startIndex > 0)
            {
                po1 = new PositionOffset(
                    CalculateOrigin(line.Offsets, line.OffsetCount, line.LineOffset, false, true),
                    startIndex);
            }
            else
            {
                po1 = EmptyPositionOffset;
            }

            if (line.Line.Length - startIndex - length > 0)
            {
                po2 = new PositionOffset(
                    CalculateOrigin(line.Offsets, line.OffsetCount, line.LineOffset + startIndex + length, false, true),
                    line.Line.Length - startIndex - length);
            }
            else
            {
                po2 = EmptyPositionOffset;
            }

            var indexAfterLastCopied = 0;

            if (po1.Offset == 0)
            {
                if (po2.Offset == 0)
                {
                    goto FINTOTAL;
                }

                po1 = po2;
                po2 = EmptyPositionOffset;
            }

            for (var i = 0; i < line.OffsetCount; i++)
            {
                var pc = line.Offsets[i];
                if (pc.Position > po1.Position)
                {
                    if (i > indexAfterLastCopied)
                    {
                        Array.Copy(line.Offsets, indexAfterLastCopied, Offsets, OffsetCount, i - indexAfterLastCopied);
                        OffsetCount         += i - indexAfterLastCopied;
                        indexAfterLastCopied = i;
                    }

                    Offsets[OffsetCount++] = po1;

                    po1 = po2;

                    if (po1.Offset == 0)
                    {
                        goto FIN;
                    }

                    po2 = EmptyPositionOffset;
                }
            }

FIN:
            if (po1.Offset != 0)
            {
                Offsets[OffsetCount++] = po1;
            }

            if (po2.Offset != 0)
            {
                Offsets[OffsetCount++] = po2;
            }

FINTOTAL:
            Array.Copy(line.Offsets, indexAfterLastCopied, Offsets, OffsetCount, line.OffsetCount - indexAfterLastCopied);
            OffsetCount += line.OffsetCount - indexAfterLastCopied;
        }