public void AddOffset(LineInfo line, int startIndex, int length) { if (this.OffsetCount + line.OffsetCount + 2 >= this.Offsets.Length) { Array.Resize(ref this.Offsets, this.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, this.Offsets, this.OffsetCount, i - indexAfterLastCopied); this.OffsetCount += i - indexAfterLastCopied; indexAfterLastCopied = i; } this.Offsets[this.OffsetCount++] = po1; po1 = po2; if (po1.Offset == 0) { goto FIN; } po2 = EmptyPositionOffset; } } FIN: if (po1.Offset != 0) { this.Offsets[this.OffsetCount++] = po1; } if (po2.Offset != 0) { this.Offsets[this.OffsetCount++] = po2; } FINTOTAL: Array.Copy(line.Offsets, indexAfterLastCopied, this.Offsets, this.OffsetCount, line.OffsetCount - indexAfterLastCopied); this.OffsetCount += line.OffsetCount - indexAfterLastCopied; }
public void ReadLine(LineInfo line) { line.LineOffset = this._previousBufferLength + this._bufferPosition; line.LineNumber++; line.OffsetCount = 0; line.Line = null; var tabIncreaseCount = 0; if (this._bufferPosition == this._bufferLength && !this.ReadBuffer()) { return; } bool useBuilder = false; int num; char c; while (true) { num = this._bufferPosition; do { c = this._buffer[num]; if (c == '\r' || c == '\n') { goto IL_4A; } if (c == '\0') { this._buffer[num] = '\uFFFD'; } num++; }while (num < this._bufferLength); num = this._bufferLength - this._bufferPosition; if (!useBuilder) { useBuilder = true; this._builder.Length = 0; } this._builder.Append(this._buffer, this._bufferPosition, num); if (!this.ReadBuffer()) { this._builder.Append('\n'); line.Line = this._builder.ToString(); return; } } IL_4A: string result; this._buffer[num] = '\n'; if (useBuilder) { this._builder.Append(this._buffer, this._bufferPosition, num - this._bufferPosition + 1); result = this._builder.ToString(); } else { result = new string(this._buffer, this._bufferPosition, num - this._bufferPosition + 1); } this._bufferPosition = num + 1; if (c == '\r' && (this._bufferPosition < this._bufferLength || this.ReadBuffer()) && this._buffer[this._bufferPosition] == '\n') { if (line.IsTrackingPositions) { line.AddOffset(this._previousBufferLength + this._bufferPosition - 1 + tabIncreaseCount, 1); } this._bufferPosition++; } line.Line = result; }