diff --git a/luhn.dart b/luhn.dart index d77849a..8641901 100644 --- a/luhn.dart +++ b/luhn.dart @@ -25,48 +25,37 @@ class Luhn { var i = 0; var digitCount = 0; var digits = []; - var matchFrom = -1; var len = s.length; - var maskOffset = -1; - var c = ''; - var cc = 0; while (i < len) { - c = s[i]; - cc = c.charCodeAt(0); + var c = s[i]; + var cc = c.charCodeAt(0); if (cc >= C_0 && cc <= C_9) { - if (matchFrom < 0) { matchFrom = i; } digitCount += 1; digits.add(Math.parseInt(c)); if (digitCount >= 14) { - for (var theLen = 14; theLen <= 16 && theLen <= digitCount; theLen++) { - var startAt = digitCount - 14; - if (theLen >= 16) { - startAt -= 2; - } else if (theLen >= 15) { - startAt -= 1; - } + for (var theLen = digitCount < 16 ? digitCount : 16; theLen >= 14; theLen--) { + var startAt = digitCount - theLen; if (testIt(digits, startAt, theLen)) { if (masked === null) { masked = s.splitChars(); } var j = i; - while (j >= matchFrom && j > maskOffset) { + var maskLen = theLen; + while (maskLen > 0) { var mc = s.charCodeAt(j); if (mc >= C_0 && mc <= C_9) { masked[j] = 'X'; + maskLen -= 1; } j -= 1; } - maskOffset = i; } } } - if (digitCount >= 16) { matchFrom += 1; } } else if (c == '-' || c == ' ') { // Keep going. } else { if (digitCount > 0) { digitCount = 0; digits = []; - matchFrom = -1; } } i += 1; diff --git a/luhn.rb b/luhn.rb index b8a2096..20c076e 100644 --- a/luhn.rb +++ b/luhn.rb @@ -22,9 +22,7 @@ def mask s i = 0 digit_count = 0 digits = [] - max_len = 0 len = s.length - c = '' while i < len c = s[i] case c @@ -32,15 +30,9 @@ def mask s digit_count += 1 digits << c.to_i if digit_count >= 14 - max_len = digit_count > 16 ? 16 : digit_count - the_len = 14 - while the_len <= max_len - start_at = digit_count - 14 - if the_len >= 16 - start_at -= 2 - elsif the_len >= 15 - start_at -= 1 - end + the_len = digit_count < 16 ? digit_count : 16 + while the_len >= 14 + start_at = digit_count - the_len if test_it(digits, start_at, the_len) masked = s[0..-1] if not masked mask_len = the_len @@ -54,7 +46,7 @@ def mask s j -= 1 end end - the_len += 1 + the_len -= 1 end end when '-', ' ' diff --git a/mask.sh b/mask.sh index 5daac07..b7eae69 100755 --- a/mask.sh +++ b/mask.sh @@ -2,5 +2,5 @@ # Call your program here instead of cat. #cat -./luhny.rb -#./luhny.dart +#./luhny.rb +./luhny.dart