Skip to content

Commit 5e42882

Browse files
committed
Refactor Luhn validation methods for clarity and reusability
Replaced `IsNumber` with `ValidateAndTrimNumber` to improve readability and better communicate its purpose. Introduced `AsciiCodeForZero` constant for cleaner ASCII code calculations. Updated method references and comments to reflect these changes, enhancing maintainability. Resolves: No entry
1 parent a470296 commit 5e42882

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/Luhn.cs

+17-12
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public static class Luhn
5252
/// </summary>
5353
private const int Modulus = 10;
5454

55+
/// <summary>
56+
/// Represents the ASCII code for the character '0'.
57+
/// </summary>
58+
private const int AsciiCodeForZero = 48;
59+
5560
#if NET8_0_OR_GREATER
5661
/// <summary>
5762
/// Computes the Luhn check digit
@@ -63,7 +68,7 @@ public static class Luhn
6368
[SuppressMessage("ReSharper", "UnusedMember.Global")]
6469
[SuppressMessage("ReSharper", "HeapView.ObjectAllocation")]
6570
public static byte ComputeLuhnCheckDigit(this ReadOnlySpan<char> number) =>
66-
(byte)((Modulus - number.IsNumber().GetDigits().DoubleEverySecondDigit(false).SumDigits()) % Modulus);
71+
(byte)((Modulus - number.ValidateAndTrimNumber().GetDigits().DoubleEverySecondDigit(false).SumDigits()) % Modulus);
6772
#endif
6873

6974
/// <summary>
@@ -79,7 +84,7 @@ public static byte ComputeLuhnCheckDigit(this string number) =>
7984
#if NET8_0_OR_GREATER
8085
number.AsSpan().ComputeLuhnCheckDigit();
8186
#else
82-
(byte)((Modulus - number.IsNumber().GetDigits().DoubleEverySecondDigit(false).SumDigits()) % Modulus);
87+
(byte)((Modulus - number.ValidateAndTrimNumber().GetDigits().DoubleEverySecondDigit(false).SumDigits()) % Modulus);
8388
#endif
8489

8590
#if NET8_0_OR_GREATER
@@ -136,7 +141,7 @@ public static string ComputeLuhnNumber(this string number)
136141
[SuppressMessage("ReSharper", "UnusedMember.Global")]
137142
[SuppressMessage("ReSharper", "HeapView.ObjectAllocation")]
138143
public static bool IsValidLuhnNumber(this ReadOnlySpan<char> luhnNumber) =>
139-
luhnNumber.IsNumber().GetDigits().DoubleEverySecondDigit(true).SumDigits() == 0;
144+
luhnNumber.ValidateAndTrimNumber().GetDigits().DoubleEverySecondDigit(true).SumDigits() == 0;
140145
#endif
141146

142147
/// <summary>
@@ -155,7 +160,7 @@ public static bool IsValidLuhnNumber(this string luhnNumber) =>
155160
#if NET8_0_OR_GREATER
156161
luhnNumber.AsSpan().IsValidLuhnNumber();
157162
#else
158-
luhnNumber.IsNumber().GetDigits().DoubleEverySecondDigit(true).SumDigits() == 0;
163+
luhnNumber.ValidateAndTrimNumber().GetDigits().DoubleEverySecondDigit(true).SumDigits() == 0;
159164
#endif
160165

161166
#if NET8_0_OR_GREATER
@@ -184,7 +189,7 @@ public static bool IsValidLuhnCheckDigit(this byte checkDigit, ReadOnlySpan<char
184189

185190
return string.Concat(number.Trim(), checkDigit.ToString(CultureInfo.InvariantCulture))
186191
.AsSpan()
187-
.IsNumber()
192+
.ValidateAndTrimNumber()
188193
.GetDigits()
189194
.DoubleEverySecondDigit(true)
190195
.SumDigits() == 0;
@@ -222,7 +227,7 @@ public static bool IsValidLuhnCheckDigit(this byte checkDigit, string number)
222227
"{0}{1}",
223228
number.Trim(),
224229
checkDigit.ToString(CultureInfo.InvariantCulture))
225-
.IsNumber()
230+
.ValidateAndTrimNumber()
226231
.GetDigits()
227232
.DoubleEverySecondDigit(true)
228233
.SumDigits() == 0;
@@ -294,12 +299,12 @@ public static string ConvertAlphaNumericToNumeric(this string alphaNumeric)
294299
#endif
295300

296301
/// <summary>
297-
/// Doubling of every second digit.
302+
/// Doubles every second digit of the <paramref name="digits"/> enumeration.
298303
/// </summary>
299304
/// <param name="digits">The digits represent a number w/ or w/o check digit.</param>
300305
/// <param name="forValidation"><see langword="true"/> if the <paramref name="digits"/> represent
301306
/// a Luhn number including a check digit; otherwise <see langword="false"/></param>
302-
/// <returns></returns>
307+
/// <returns>Enumeration of digits</returns>
303308
private static IEnumerable<uint> DoubleEverySecondDigit(this IEnumerable<uint> digits, bool forValidation)
304309
{
305310
int index = 0;
@@ -336,7 +341,7 @@ private static IEnumerable<uint> DoubleEverySecondDigit(this IEnumerable<uint> d
336341
/// <param name="number">An identification number</param>
337342
/// <returns>The trimmed identification number if valid</returns>
338343
/// <exception cref="ArgumentException"><paramref name="number"/> is not a valid number</exception>
339-
private static string IsNumber(this string number)
344+
private static string ValidateAndTrimNumber(this string number)
340345
{
341346
string trimmedNumber = number?.Trim();
342347
if (string.IsNullOrWhiteSpace(trimmedNumber) || !Regex.IsMatch(trimmedNumber, @"^\d+$"))
@@ -355,7 +360,7 @@ private static string IsNumber(this string number)
355360
/// <param name="number">An identification number</param>
356361
/// <returns>The trimmed identification number if valid</returns>
357362
/// <exception cref="ArgumentException"><paramref name="number"/> is not a valid number</exception>
358-
private static ReadOnlySpan<char> IsNumber(this ReadOnlySpan<char> number)
363+
private static ReadOnlySpan<char> ValidateAndTrimNumber(this ReadOnlySpan<char> number)
359364
{
360365
var trimmedNumber = number.Trim();
361366
if (trimmedNumber.Length == 0 || !trimmedNumber.IsDigits())
@@ -397,7 +402,7 @@ private static IEnumerable<uint> GetDigits(this ReadOnlySpan<char> number)
397402
uint[] digits = new uint[number.Length];
398403
for (int i = 0; i < number.Length; i++)
399404
{
400-
digits[number.Length - i - 1] = (uint)number[i] - 48;
405+
digits[number.Length - i - 1] = (uint)number[i] - AsciiCodeForZero;
401406
}
402407

403408
return digits;
@@ -407,7 +412,7 @@ private static IEnumerable<uint> GetDigits(this string number)
407412
{
408413
for (int i = number.Length - 1; i >= 0; i--)
409414
{
410-
yield return (uint)number[i] - 48;
415+
yield return (uint)number[i] - AsciiCodeForZero;
411416
}
412417
}
413418
#endif

0 commit comments

Comments
 (0)