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

Perf improvement: avoid Dictionary in spanish code #1308

Merged
merged 1 commit into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,26 @@ private static string ConvertMappedOrdinalNumber(

private static string ConvertOrdinalUnits(in int number, GrammaticalGender gender, WordForm wordForm)
{
if (number is > 0 and < 10)
if (number is <= 0 or >= 10)
{
Dictionary<GrammaticalGender, string> genderedEndingDict = new()
{
{ GrammaticalGender.Feminine, "a" },
{ GrammaticalGender.Masculine, HasOrdinalAbbreviation(number, wordForm) ? string.Empty : "o" },
};
return string.Empty;
}

genderedEndingDict.Add(GrammaticalGender.Neuter, genderedEndingDict[GrammaticalGender.Masculine]);
switch (gender)
{
case GrammaticalGender.Masculine:
case GrammaticalGender.Neuter:
if (HasOrdinalAbbreviation(number, wordForm))
{
return OrdinalsRootMap[number];
}

return OrdinalsRootMap[number] + genderedEndingDict[gender];
return OrdinalsRootMap[number] + 'o';
case GrammaticalGender.Feminine:
return OrdinalsRootMap[number] + "a";
default:
throw new ArgumentOutOfRangeException(nameof(gender), gender, null);
}

return string.Empty;
}

private static string ConvertTenths(in int number, out int remainder, GrammaticalGender gender)
Expand All @@ -198,28 +204,26 @@ private static string ConvertThousandths(in int number, out int remainder, Gramm

private static string ConvertUnits(long inputNumber, GrammaticalGender gender, WordForm wordForm = WordForm.Normal)
{
var wordPart = string.Empty;
if (inputNumber <= 0)
{
return string.Empty;
}

UnitsMap[1] = GetGenderedOne(gender, wordForm);
UnitsMap[21] = GetGenderedTwentyOne(gender, wordForm);

if (inputNumber > 0)
if (inputNumber < 30)
{
UnitsMap[1] = GetGenderedOne(gender, wordForm);
UnitsMap[21] = GetGenderedTwentyOne(gender, wordForm);
return UnitsMap[inputNumber];
}

if (inputNumber < 30)
{
wordPart = UnitsMap[inputNumber];
}
else
{
wordPart = TensMap[inputNumber / 10];
if (inputNumber % 10 > 0)
{
wordPart += $" y {UnitsMap[inputNumber % 10]}";
}
}
var wordPart = TensMap[inputNumber / 10];
if (inputNumber % 10 <= 0)
{
return wordPart;
}

return wordPart;
return wordPart + $" y {UnitsMap[inputNumber % 10]}";
}

private static IReadOnlyList<string> GetGenderedHundredsMap(GrammaticalGender gender)
Expand All @@ -238,26 +242,30 @@ private static IReadOnlyList<string> GetGenderedHundredsMap(GrammaticalGender ge

private static string GetGenderedOne(GrammaticalGender gender, WordForm wordForm = WordForm.Normal)
{
var genderedOne = new Dictionary<GrammaticalGender, string>()
switch (gender)
{
{ GrammaticalGender.Feminine, "una" },
{ GrammaticalGender.Masculine, wordForm == WordForm.Abbreviation ? "un" : "uno" }
};

genderedOne.Add(GrammaticalGender.Neuter, genderedOne[GrammaticalGender.Masculine]);
return genderedOne[gender];
case GrammaticalGender.Masculine:
case GrammaticalGender.Neuter:
return wordForm == WordForm.Abbreviation ? "un" : "uno";
case GrammaticalGender.Feminine:
return "una";
default:
throw new ArgumentOutOfRangeException(nameof(gender), gender, null);
}
}

private static string GetGenderedTwentyOne(GrammaticalGender gender, WordForm wordForm = WordForm.Normal)
{
var genderedtwentyOne = new Dictionary<GrammaticalGender, string>()
switch (gender)
{
{ GrammaticalGender.Feminine, "veintiuna" },
{ GrammaticalGender.Masculine, wordForm == WordForm.Abbreviation ? "veintiún" : "veintiuno" }
};

genderedtwentyOne.Add(GrammaticalGender.Neuter, genderedtwentyOne[GrammaticalGender.Masculine]);
return genderedtwentyOne[gender];
case GrammaticalGender.Masculine:
case GrammaticalGender.Neuter:
return wordForm == WordForm.Abbreviation ? "veintiún" : "veintiuno";
case GrammaticalGender.Feminine:
return "veintiuna";
default:
throw new ArgumentOutOfRangeException(nameof(gender), gender, null);
}
}

private static bool HasOrdinalAbbreviation(int number, WordForm wordForm)
Expand Down
21 changes: 12 additions & 9 deletions src/Humanizer/Localisation/Ordinalizers/SpanishOrdinalizer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Globalization;

namespace Humanizer.Localisation.Ordinalizers
Expand All @@ -17,13 +18,6 @@ public override string Convert(int number, string numberString, GrammaticalGende

public override string Convert(int number, string numberString, GrammaticalGender gender, WordForm wordForm)
{
var genderMap = new Dictionary<GrammaticalGender, string>()
{
{ GrammaticalGender.Feminine, ".ª" },
{ GrammaticalGender.Masculine, GetWordForm(number, wordForm) },
{ GrammaticalGender.Neuter, GetWordForm(number, wordForm) }
};

// N/A in Spanish
if (number == 0 || number == int.MinValue)
{
Expand All @@ -35,7 +29,16 @@ public override string Convert(int number, string numberString, GrammaticalGende
return Convert(-number, GetNumberString(-number), gender);
}

return $"{numberString}{genderMap[gender]}";
switch (gender)
{
case GrammaticalGender.Masculine:
case GrammaticalGender.Neuter:
return numberString + GetWordForm(number, wordForm);
case GrammaticalGender.Feminine:
return numberString + ".ª";
default:
throw new ArgumentOutOfRangeException(nameof(gender), gender, null);
}
}

private static string GetNumberString(int number)
Expand Down