Skip to content

Commit

Permalink
Minor CharHelper optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
MihaZupan committed Oct 14, 2019
1 parent c99f7dd commit 9af96ba
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/Markdig/Helpers/CharHelper.cs
Expand Up @@ -36,7 +36,8 @@
using System.Globalization;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

using System.Diagnostics;

namespace Markdig.Helpers
{
/// <summary>
Expand Down Expand Up @@ -130,9 +131,9 @@ public static int RomanToArabic(string text)
int result = 0;
for (int i = 0; i < text.Length; i++)
{
var character = Char.ToUpperInvariant(text[i]);
var character = char.ToUpperInvariant(text[i]);
var candidate = romanMap[character];
if (i + 1 < text.Length && candidate < romanMap[Char.ToUpperInvariant(text[i + 1])])
if (i + 1 < text.Length && candidate < romanMap[char.ToUpperInvariant(text[i + 1])])
{
result -= candidate;
}
Expand All @@ -147,7 +148,9 @@ public static int RomanToArabic(string text)
[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
public static int AddTab(int column)
{
return ((column + TabSize) / TabSize) * TabSize;
// return ((column + TabSize) / TabSize) * TabSize;
Debug.Assert(TabSize == 4, "Change the AddTab implementation if TabSize is no longer a power of 2");
return TabSize + (column & ~(TabSize - 1));
}

[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
Expand Down Expand Up @@ -180,7 +183,7 @@ public static bool IsWhitespace(this char c)
[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
public static bool IsControl(this char c)
{
return c < ' ' || Char.IsControl(c);
return c < ' ' || char.IsControl(c);
}

[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
Expand All @@ -193,7 +196,7 @@ public static bool IsEscapableSymbol(this char c)
//[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
public static bool IsWhiteSpaceOrZero(this char c)
{
return IsWhitespace(c) || IsZero(c);
return IsZero(c) || IsWhitespace(c);
}

// Note that we are not considering the character & as a punctuation in HTML
Expand Down Expand Up @@ -294,14 +297,14 @@ public static char EscapeInsecure(this char c)

[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
public static bool IsAlphaUpper(this char c)
{
return c >= 'A' && c <= 'Z';
{
return (uint)(c - 'A') <= ('Z' - 'A');
}

[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
public static bool IsAlpha(this char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
return ((uint)(c - 'a') <= ('z' - 'a')) || ((uint)(c - 'A') <= ('Z' - 'A'));
}

[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
Expand All @@ -313,7 +316,7 @@ public static bool IsAlphaNumeric(this char c)
[MethodImpl(MethodImplOptionPortable.AggressiveInlining)]
public static bool IsDigit(this char c)
{
return c >= '0' && c <= '9';
return (uint)(c - '0') <= ('9' - '0');
}

public static bool IsAsciiPunctuation(this char c)
Expand Down

0 comments on commit 9af96ba

Please sign in to comment.