@@ -52,6 +52,17 @@ internal static class LuhnAlgorithm
52
52
/// </summary>
53
53
private const int AsciiCodeForZero = 48 ;
54
54
55
+ /// <summary>
56
+ /// Represents a delegate used for processing digits within the Luhn algorithm context.
57
+ /// </summary>
58
+ /// <param name="digit">The original value of the digit before doubling.</param>
59
+ /// <param name="doubleDigit">The value of the digit after being doubled.</param>
60
+ /// <param name="index">The index of the digit in the sequence, starting from the rightmost end.</param>
61
+ /// <returns>
62
+ /// A <see cref="System.UInt32"/> representing the processed result for the current digit.
63
+ /// </returns>
64
+ private delegate uint DigitProcessor ( uint digit , uint doubleDigit , int index ) ;
65
+
55
66
/// <summary>
56
67
/// Doubles every second digit in the given sequence based on the context of validation or computation.
57
68
/// </summary>
@@ -70,25 +81,47 @@ internal static uint DoubleEverySecondDigit(this string number, bool forValidati
70
81
#endif
71
82
{
72
83
uint sum = 0 ;
84
+ DigitProcessor digitProcessor = forValidation ? ProcessDigitForValidation : ProcessDigitForComputation ;
73
85
for ( int index = 0 ; index < number . Length ; index ++ )
74
86
{
75
- uint digit = number [ number . Length - index - 1 ] . ToDigit ( ) ;
87
+ uint digit = number [ number . Length - index - 1 ] . ToUnsignedIntegerDigit ( ) ;
76
88
uint doubleDigit = digit << 1 ;
77
- if ( forValidation )
78
- {
79
- doubleDigit = doubleDigit > 9 ? doubleDigit - 9 : doubleDigit ;
80
- sum += index . IsEven ( ) ? digit : doubleDigit ;
81
- }
82
- else
83
- {
84
- doubleDigit = doubleDigit / Modulus + doubleDigit % Modulus ;
85
- sum += ! index . IsEven ( ) ? digit : doubleDigit ;
86
- }
89
+ sum += digitProcessor ( digit , doubleDigit , index ) ;
87
90
}
88
91
89
92
return sum % Modulus ;
90
93
}
91
94
95
+ /// <summary>
96
+ /// Processes a digit during the computation phase of the Luhn algorithm.
97
+ /// Depending on the index, either the doubled digit's sum of digits or the original digit is returned.
98
+ /// </summary>
99
+ /// <param name="digit">The current digit being processed from the numeric input.</param>
100
+ /// <param name="doubleDigit">The value of the current digit multiplied by 2.</param>
101
+ /// <param name="index">The position of the digit in the sequence, considering rightmost digit as index 0.</param>
102
+ /// <returns>
103
+ /// A <see cref="System.UInt32"/> value representing the transformed digit based on its position.
104
+ /// </returns>
105
+ private static uint ProcessDigitForComputation ( uint digit , uint doubleDigit , int index )
106
+ {
107
+ return ! index . IsEven ( ) ? digit : doubleDigit / Modulus + doubleDigit % Modulus ;
108
+ }
109
+
110
+ /// <summary>
111
+ /// Processes a digit for validation based on the rules defined within the Luhn algorithm.
112
+ /// </summary>
113
+ /// <param name="digit">The numeric value of the digit being processed.</param>
114
+ /// <param name="doubleDigit">The value of the digit multiplied by two.</param>
115
+ /// <param name="index">The position index of the digit in the sequence, starting from zero.</param>
116
+ /// <returns>
117
+ /// A <see cref="System.UInt32"/> value representing the result of the processing based on the digit's index.
118
+ /// </returns>
119
+ private static uint ProcessDigitForValidation ( uint digit , uint doubleDigit , int index )
120
+ {
121
+ doubleDigit = doubleDigit > 9 ? doubleDigit - 9 : doubleDigit ;
122
+ return index . IsEven ( ) ? digit : doubleDigit ;
123
+ }
124
+
92
125
/// <summary>
93
126
/// Checks whether the <paramref name="value"/> is even.
94
127
/// </summary>
@@ -98,9 +131,9 @@ internal static uint DoubleEverySecondDigit(this string number, bool forValidati
98
131
private static bool IsEven ( this int value ) => value % 2 == 0 ;
99
132
100
133
/// <summary>
101
- /// Converts a character representing a numeric digit to its integer value.
134
+ /// Converts a character representing a numeric digit to its unsigned integer value.
102
135
/// </summary>
103
136
/// <param name="character">The character to convert. Must represent a numeric digit.</param>
104
137
/// <returns>The numeric value of the character as an unsigned integer.</returns>
105
- private static uint ToDigit ( this char character ) => ( uint ) character - AsciiCodeForZero ;
138
+ private static uint ToUnsignedIntegerDigit ( this char character ) => ( uint ) character - AsciiCodeForZero ;
106
139
}
0 commit comments