Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove overhead from RomanNumeralExtensions #1508

Merged
merged 1 commit into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 41 additions & 53 deletions src/Humanizer/RomanNumeralExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,60 +1,50 @@
// Done by Jesse Slicer https://github.com/jslicer

using System.Diagnostics;

namespace Humanizer;

/// <summary>
/// Contains extension methods for changing a number to Roman representation (ToRoman) and from Roman representation back to the number (FromRoman)
/// </summary>
public static class RomanNumeralExtensions
{
static readonly Dictionary<string, int> RomanNumerals =
new(StringComparer.OrdinalIgnoreCase)
static readonly KeyValuePair<string, int>[] RomanNumeralsSequence =
[
new KeyValuePair<string, int>("M", 1000),
new KeyValuePair<string, int>("CM", 900),
new KeyValuePair<string, int>("D", 500 ),
new KeyValuePair<string, int>("CD", 400),
new KeyValuePair<string, int>("C", 100 ),
new KeyValuePair<string, int>("XC", 90 ),
new KeyValuePair<string, int>("L", 50 ),
new KeyValuePair<string, int>("XL", 40 ),
new KeyValuePair<string, int>("X", 10 ),
new KeyValuePair<string, int>("IX", 9 ),
new KeyValuePair<string, int>("V", 5 ),
new KeyValuePair<string, int>("IV", 4 ),
new KeyValuePair<string, int>("I", 1 ),
];

static int GetRomanNumeralCharValue(char c)
{
Debug.Assert(char.ToLowerInvariant(c) is 'M' or 'D' or 'C' or 'L' or 'X' or 'V' or 'I', "Invalid Roman numeral character");
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
return (c & ~0x20) switch
{
{
"M", 1000
},
{
"CM", 900
},
{
"D", 500
},
{
"CD", 400
},
{
"C", 100
},
{
"XC", 90
},
{
"L", 50
},
{
"XL", 40
},
{
"X", 10
},
{
"IX", 9
},
{
"V", 5
},
{
"IV", 4
},
{
"I", 1
}
'M' => 1000,
'D' => 500,
'C' => 100,
'L' => 50,
'X' => 10,
'V' => 5,
_ => 1,
};
}

static readonly Regex ValidRomanNumeral =
new(
"^(?i:(?=[MDCLXVI])((M{0,3})((C[DM])|(D?C{0,3}))?((X[LC])|(L?XX{0,2})|L)?((I[VX])|(V?(II{0,2}))|V)?))$",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
RegexOptions.Compiled | RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

/// <summary>
/// Converts Roman numbers into integer
Expand Down Expand Up @@ -92,14 +82,10 @@ public static int FromRoman(CharSpan input)

while (i > 0)
{
var digit = RomanNumerals[input[--i]
.ToString()];

var digit = GetRomanNumeralCharValue(input[--i]);
if (i > 0)
{
var previousDigit = RomanNumerals[input[i - 1]
.ToString()];

var previousDigit = GetRomanNumeralCharValue(input[i - 1]);
if (previousDigit < digit)
{
digit -= previousDigit;
Expand Down Expand Up @@ -130,18 +116,20 @@ public static string ToRoman(this int input)
throw new ArgumentOutOfRangeException();
}

var builder = new StringBuilder(maxRomanNumeralLength);
Span<char> builder = stackalloc char[maxRomanNumeralLength];
var pos = 0;

foreach (var pair in RomanNumerals)
foreach (var pair in RomanNumeralsSequence)
{
while (input / pair.Value > 0)
while (input >= pair.Value)
{
builder.Append(pair.Key);
pair.Key.AsSpan().CopyTo(builder.Slice(pos));
pos += pair.Key.Length;
input -= pair.Value;
}
}

return builder.ToString();
return builder.Slice(0, pos).ToString();
}

static bool IsInvalidRomanNumeral(CharSpan input) =>
Expand Down