Skip to content

Commit

Permalink
Fixed the Dart version as per the recently updated tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedrosa committed Nov 21, 2011
1 parent c931c7c commit cba798a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 32 deletions.
25 changes: 7 additions & 18 deletions luhn.dart
Expand Up @@ -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;
Expand Down
16 changes: 4 additions & 12 deletions luhn.rb
Expand Up @@ -22,25 +22,17 @@ def mask s
i = 0
digit_count = 0
digits = []
max_len = 0
len = s.length
c = ''
while i < len
c = s[i]
case c
when '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
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
Expand All @@ -54,7 +46,7 @@ def mask s
j -= 1
end
end
the_len += 1
the_len -= 1
end
end
when '-', ' '
Expand Down
4 changes: 2 additions & 2 deletions mask.sh
Expand Up @@ -2,5 +2,5 @@

# Call your program here instead of cat.
#cat
./luhny.rb
#./luhny.dart
#./luhny.rb
./luhny.dart

0 comments on commit cba798a

Please sign in to comment.