Minor codegen improvements

This commit is contained in:
MihaZupan
2020-12-17 10:14:17 +01:00
parent f453545c02
commit ab358b5788
19 changed files with 71 additions and 44 deletions

View File

@@ -51,7 +51,7 @@ namespace Markdig.Extensions.Abbreviations
{
return BlockState.None;
}
slice.NextChar();
slice.SkipChar();
slice.Trim();

View File

@@ -107,7 +107,7 @@ namespace Markdig.Extensions.GenericAttributes
if (c == '}')
{
isValid = true;
line.NextChar(); // skip }
line.SkipChar(); // skip }
break;
}
@@ -191,7 +191,7 @@ namespace Markdig.Extensions.GenericAttributes
}
// Go to next char, skip any spaces
line.NextChar();
line.SkipChar();
line.TrimStart();
int startValue = -1;

View File

@@ -52,7 +52,7 @@ namespace Markdig.Extensions.SmartyPants
type = SmartyPantType.Quote; // We will resolve them at the end of parsing all inlines
if (slice.PeekChar() == '\'')
{
slice.NextChar();
slice.SkipChar();
type = SmartyPantType.DoubleQuote; // We will resolve them at the end of parsing all inlines
}
break;

View File

@@ -33,7 +33,7 @@ namespace Markdig.Extensions.Tables
while (c == '+')
{
var columnStart = line.Start;
line.NextChar();
line.SkipChar();
line.TrimStart();
// if we have reached the end of the line, exit
@@ -161,15 +161,16 @@ namespace Markdig.Extensions.Tables
private static bool IsRowSeperator(StringSlice slice)
{
while (slice.Length > 0)
char c = slice.CurrentChar;
do
{
if (slice.CurrentChar != '-' && slice.CurrentChar != '=' && slice.CurrentChar != ':')
if (c != '-' && c != '=' && c != ':')
{
return false;
return c == '\0';
}
slice.NextChar();
c = slice.NextChar();
}
return true;
while (true);
}
private static void TerminateCurrentRow(BlockProcessor processor, GridTableState tableState, Table gridTable, bool isLastRow)

View File

@@ -112,7 +112,7 @@ namespace Markdig.Extensions.Tables
}
tableState.LineHasPipe = true;
tableState.LineIndex = localLineIndex;
slice.NextChar(); // Skip the `|` character
slice.SkipChar(); // Skip the `|` character
tableState.ColumnAndLineDelimiters.Add(processor.Inline);
}

View File

@@ -60,7 +60,7 @@ namespace Markdig.Extensions.Tables
if (c == ':')
{
hasLeft = true;
slice.NextChar();
slice.SkipChar();
}
slice.TrimStart();
@@ -91,7 +91,7 @@ namespace Markdig.Extensions.Tables
if (c == ':')
{
hasRight = true;
slice.NextChar();
slice.SkipChar();
}
slice.TrimStart();

View File

@@ -56,7 +56,7 @@ namespace Markdig.Extensions.TaskLists
return false;
}
// Skip last ]
slice.NextChar();
slice.SkipChar();
// Create the TaskList
var taskItem = new TaskList()

View File

@@ -127,7 +127,7 @@ namespace Markdig.Helpers
case '\0':
return false;
case '>':
text.NextChar();
text.SkipChar();
builder.Append(c);
return true;
case '/':
@@ -137,7 +137,7 @@ namespace Markdig.Helpers
{
return false;
}
text.NextChar();
text.SkipChar();
builder.Append('>');
return true;
case '=':
@@ -269,7 +269,7 @@ namespace Markdig.Helpers
if (c == '>')
{
text.NextChar();
text.SkipChar();
builder.Append('>');
return true;
}
@@ -304,7 +304,7 @@ namespace Markdig.Helpers
if (c == '>')
{
builder.Append('>');
text.NextChar();
text.SkipChar();
return true;
}
@@ -337,7 +337,7 @@ namespace Markdig.Helpers
c = text.NextChar();
if (c == '>')
{
text.NextChar();
text.SkipChar();
builder.Append('>');
return true;
}
@@ -392,7 +392,7 @@ namespace Markdig.Helpers
if (c == '>')
{
builder.Append('>');
text.NextChar();
text.SkipChar();
return true;
}
return false;
@@ -417,7 +417,7 @@ namespace Markdig.Helpers
if (c == '>' && prevChar == '?')
{
builder.Append('>');
text.NextChar();
text.SkipChar();
return true;
}
prevChar = c;
@@ -532,7 +532,7 @@ namespace Markdig.Helpers
c = slice.PeekChar();
if (c == 'x' || c == 'X')
{
c = slice.NextChar(); // skip #
slice.SkipChar(); // skip #
// expect 1-6 hex digits starting from pos+3
while (c != '\0')
{

View File

@@ -31,12 +31,23 @@ namespace Markdig.Helpers
/// <returns>The next character. `\0` is end of the iteration.</returns>
char NextChar();
/// <summary>
/// Goes to the next character, incrementing the <see cref="Start" /> position.
/// </summary>
void SkipChar();
/// <summary>
/// Peeks at the next character, without incrementing the <see cref="Start"/> position.
/// </summary>
/// <returns>The next character. `\0` is end of the iteration.</returns>
char PeekChar();
/// <summary>
/// Peeks at the next character, without incrementing the <see cref="Start"/> position.
/// </summary>
/// <param name="offset"></param>
/// <returns>The next character. `\0` is end of the iteration.</returns>
char PeekChar(int offset = 1);
char PeekChar(int offset);
/// <summary>
/// Gets a value indicating whether this instance is empty.

View File

@@ -246,7 +246,7 @@ namespace Markdig.Helpers
break;
}
text.NextChar();
text.SkipChar();
link = builder.ToString();
builder.Length = 0;
return true;
@@ -294,7 +294,7 @@ namespace Markdig.Helpers
if (c == '>')
{
text.NextChar();
text.SkipChar();
link = builder.ToString();
builder.Length = 0;
return true;
@@ -355,7 +355,7 @@ namespace Markdig.Helpers
// 1. An inline link consists of a link text followed immediately by a left parenthesis (,
if (c == '(')
{
text.NextChar();
text.SkipChar();
text.TrimStart();
var pos = text.Start;
@@ -407,7 +407,7 @@ namespace Markdig.Helpers
if (isValid)
{
// Skip ')'
text.NextChar();
text.SkipChar();
title ??= string.Empty;
}
@@ -466,7 +466,7 @@ namespace Markdig.Helpers
}
// Skip last quote
text.NextChar();
text.SkipChar();
isValid = true;
break;
}
@@ -527,7 +527,7 @@ namespace Markdig.Helpers
c = text.NextChar();
if (!hasEscape && c == '>')
{
text.NextChar();
text.SkipChar();
isValid = true;
break;
}
@@ -743,7 +743,7 @@ namespace Markdig.Helpers
label = null;
return false;
}
text.NextChar(); // Skip ':'
text.SkipChar(); // Skip ':'
// Skip any whitespace before the url
text.TrimStart();
@@ -873,7 +873,7 @@ namespace Markdig.Helpers
if (c == ']')
{
lines.NextChar(); // Skip ]
lines.SkipChar(); // Skip ]
if (allowEmpty || hasNonWhiteSpace)
{
// Remove trailing spaces

View File

@@ -223,7 +223,7 @@ namespace Markdig.Helpers
{
End += lines.Lines[i].Slice.Length + 1; // Add chars
}
NextChar();
SkipChar();
}
public int Start { get; private set; }
@@ -287,7 +287,11 @@ namespace Markdig.Helpers
return CurrentChar;
}
public readonly char PeekChar(int offset = 1)
public void SkipChar() => NextChar();
public readonly char PeekChar() => PeekChar(1);
public readonly char PeekChar(int offset)
{
if (offset < 0) ThrowHelper.ArgumentOutOfRangeException("Negative offset are not supported for StringLineGroup", nameof(offset));

View File

@@ -119,6 +119,17 @@ namespace Markdig.Helpers
return Text[start];
}
/// <summary>
/// Goes to the next character, incrementing the <see cref="Start" /> position.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SkipChar()
{
int start = Start;
if (start <= End)
Start = start + 1;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal int CountAndSkipChar(char matchChar)
{

View File

@@ -212,7 +212,7 @@ namespace Markdig.Parsers
}
else
{
Line.NextChar();
Line.SkipChar();
Column++;
}
}

View File

@@ -49,7 +49,7 @@ namespace Markdig.Parsers
var line = state.Line;
var startPosition = line.Start;
line.NextChar();
line.SkipChar();
var result = TryParseTagType16(state, line, state.ColumnBeforeIndent, startPosition);
// HTML blocks of type 7 cannot interrupt a paragraph:

View File

@@ -144,7 +144,7 @@ namespace Markdig.Parsers.Inlines
char pc = (char)0;
if (processor.Inline is HtmlEntityInline htmlEntityInline)
{
if (htmlEntityInline.Transcoded.Length > 0)
if (!htmlEntityInline.Transcoded.IsEmpty)
{
pc = htmlEntityInline.Transcoded[htmlEntityInline.Transcoded.End];
}

View File

@@ -36,7 +36,7 @@ namespace Markdig.Parsers.Inlines
IsFirstCharacterEscaped = true,
};
processor.Inline.Span.End = processor.Inline.Span.Start + 1;
slice.NextChar();
slice.SkipChar();
return true;
}
@@ -52,7 +52,7 @@ namespace Markdig.Parsers.Inlines
Column = column
};
processor.Inline.Span.End = processor.Inline.Span.Start + 1;
slice.NextChar();
slice.SkipChar();
return true;
}
return false;

View File

@@ -37,7 +37,7 @@ namespace Markdig.Parsers.Inlines
var startPosition = slice.Start;
var hasDoubleSpacesBefore = slice.PeekCharExtra(-1).IsSpace() && slice.PeekCharExtra(-2).IsSpace();
slice.NextChar(); // Skip \n
slice.SkipChar(); // Skip \n
processor.Inline = new LineBreakInline
{

View File

@@ -60,7 +60,7 @@ namespace Markdig.Parsers.Inlines
slice = saved;
// Else we insert a LinkDelimiter
slice.NextChar();
slice.SkipChar();
processor.Inline = new LinkDelimiterInline(this)
{
Type = DelimiterType.Open,
@@ -74,7 +74,7 @@ namespace Markdig.Parsers.Inlines
return true;
case ']':
slice.NextChar();
slice.SkipChar();
if (processor.Inline != null)
{
if (TryProcessLinkOrImage(processor, ref slice))
@@ -252,8 +252,8 @@ namespace Markdig.Parsers.Inlines
label = openParent.Label;
labelSpan = openParent.LabelSpan;
isLabelSpanLocal = false;
text.NextChar(); // Skip [
text.NextChar(); // Skip ]
text.SkipChar(); // Skip [
text.SkipChar(); // Skip ]
}
}
else

View File

@@ -15,7 +15,7 @@ namespace Markdig.Renderers.Normalize.Inlines
{
protected override void Write(NormalizeRenderer renderer, LiteralInline obj)
{
if (obj.IsFirstCharacterEscaped && obj.Content.Length > 0 && obj.Content[obj.Content.Start].IsAsciiPunctuation())
if (obj.IsFirstCharacterEscaped && !obj.Content.IsEmpty && obj.Content[obj.Content.Start].IsAsciiPunctuation())
{
renderer.Write('\\');
}