Skip to content

Commit

Permalink
Cache string representations of numbers 0-26
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan committed Apr 5, 2020
1 parent 84ec832 commit bdfaa27
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/Markdig/Extensions/ListExtras/ListExtraItemParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,24 @@ public override bool TryParse(BlockProcessor state, char pendingBulletType, out
if ((isRomanLow || isRomanUp) && (pendingBulletType == '\0' || pendingBulletType == 'i' || pendingBulletType == 'I'))
{
int startChar = state.Start;
int endChar = 0;
// With a roman, we can have multiple characters
// Note that we don't validate roman numbers
while (isRomanLow ? CharHelper.IsRomanLetterLowerPartial(c) : CharHelper.IsRomanLetterUpperPartial(c))
do
{
endChar = state.Start;
c = state.NextChar();
}
while (isRomanLow ? CharHelper.IsRomanLetterLowerPartial(c) : CharHelper.IsRomanLetterUpperPartial(c));

result.OrderedStart = CharHelper.RomanToArabic(state.Line.Text.AsSpan(startChar, endChar - startChar + 1)).ToString();
int orderValue = CharHelper.RomanToArabic(state.Line.Text.AsSpan(startChar, state.Start - startChar));
result.OrderedStart = CharHelper.SmallNumberToString(orderValue);
result.BulletType = isRomanLow ? 'i' : 'I';
result.DefaultOrderedStart = isRomanLow ? "i" : "I";
}
else
{
// otherwise we expect a regular alpha lettered list with a single character.
var isUpper = c.IsAlphaUpper();
result.OrderedStart = (Char.ToUpperInvariant(c) - 64).ToString();
result.OrderedStart = CharHelper.SmallNumberToString((c | 0x20) - 'a' + 1);
result.BulletType = isUpper ? 'A' : 'a';
result.DefaultOrderedStart = isUpper ? "A" : "a";
state.NextChar();
Expand Down
18 changes: 18 additions & 0 deletions src/Markdig/Helpers/CharHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,24 @@ public static bool IsLeftToRight(int c)
c == 0x002128 || c == 0x002395 ||
c == 0x01D4A2 || c == 0x01D4BB ||
c == 0x01D546;
}

// Used by ListExtraItemParser to format numbers from 1 - 26
private static readonly string[] smallNumberStringCache = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
"20", "21", "22", "23", "24", "25", "26",
};

internal static string SmallNumberToString(int number)
{
string[] cache = smallNumberStringCache;
if ((uint)number < (uint)cache.Length)
{
return cache[number];
}

return number.ToString();
}
}
}

0 comments on commit bdfaa27

Please sign in to comment.