diff --git a/src/Markdig/Helpers/StringLineGroup.cs b/src/Markdig/Helpers/StringLineGroup.cs index ec8d693cb..6752dc978 100644 --- a/src/Markdig/Helpers/StringLineGroup.cs +++ b/src/Markdig/Helpers/StringLineGroup.cs @@ -69,16 +69,13 @@ public void Clear() /// The index. public void RemoveAt(int index) { - if (Count - 1 == index) - { - Count--; - } - else + if (index != Count - 1) { Array.Copy(Lines, index + 1, Lines, index, Count - index - 1); - Lines[Count - 1] = new StringLine(); - Count--; } + + Lines[Count - 1] = new StringLine(); + Count--; } /// @@ -115,22 +112,19 @@ public readonly override string ToString() /// A single slice concatenating the lines of this instance public readonly StringSlice ToSlice(List lineOffsets = null) { - // Optimization case when no lines - if (Count == 0) - { - return new StringSlice(string.Empty); - } - // Optimization case for a single line. if (Count == 1) { - if (lineOffsets != null) - { - lineOffsets.Add(new LineOffset(Lines[0].Position, Lines[0].Column, Lines[0].Slice.Start - Lines[0].Position, Lines[0].Slice.Start, Lines[0].Slice.End + 1)); - } + lineOffsets?.Add(new LineOffset(Lines[0].Position, Lines[0].Column, Lines[0].Slice.Start - Lines[0].Position, Lines[0].Slice.Start, Lines[0].Slice.End + 1)); return Lines[0]; } + // Optimization case when no lines + if (Count == 0) + { + return new StringSlice(string.Empty); + } + if (lineOffsets != null && lineOffsets.Capacity < lineOffsets.Count + Count) { lineOffsets.Capacity = Math.Max(lineOffsets.Count + Count, lineOffsets.Capacity * 2); @@ -143,21 +137,16 @@ public readonly StringSlice ToSlice(List lineOffsets = null) { if (i > 0) { - if (lineOffsets != null) - { - lineOffsets.Add(new LineOffset(Lines[i - 1].Position, Lines[i - 1].Column, Lines[i - 1].Slice.Start - Lines[i - 1].Position, previousStartOfLine, builder.Length)); - } builder.Append('\n'); previousStartOfLine = builder.Length; } - if (!Lines[i].Slice.IsEmpty) + ref var line = ref Lines[i]; + if (!line.Slice.IsEmpty) { - builder.Append(Lines[i].Slice.Text, Lines[i].Slice.Start, Lines[i].Slice.Length); + builder.Append(line.Slice.Text, line.Slice.Start, line.Slice.Length); } - } - if (lineOffsets != null) - { - lineOffsets.Add(new LineOffset(Lines[Count - 1].Position, Lines[Count - 1].Column, Lines[Count - 1].Slice.Start - Lines[Count - 1].Position, previousStartOfLine, builder.Length)); + + lineOffsets?.Add(new LineOffset(line.Position, line.Column, line.Slice.Start - line.Position, previousStartOfLine, builder.Length)); } return new StringSlice(builder.GetStringAndReset()); } @@ -268,10 +257,10 @@ public StringLineGroup Remaining() public char NextChar() { Start++; - _offset++; if (Start <= End) { var slice = _lines.Lines[SliceIndex].Slice; + _offset++; if (_offset < slice.Length) { CurrentChar = slice[slice.Start + _offset]; @@ -288,7 +277,6 @@ public char NextChar() CurrentChar = '\0'; Start = End + 1; SliceIndex = _lines.Count; - _offset--; } return CurrentChar; }