Skip to content

Commit

Permalink
Added custom string reading for the Dart version to avoid issues with…
Browse files Browse the repository at this point in the history
… the default StringInputStream implementation.
  • Loading branch information
jpedrosa committed Nov 22, 2011
1 parent 4933147 commit 0f6e7e0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
31 changes: 24 additions & 7 deletions luhn.dart
Expand Up @@ -58,25 +58,42 @@ class Luhn {
return masked !== null ? Strings.concatAll(masked) : s;
}

readRawLines(fn) {
var a;
var saved;
while ((a = stdin.read(16384)) !== null) {
var startAt = 0;
var len = a.length;
for (var i = 0; i < len; i++) {
if (a[i] == 10) { // Newline, \n.
if (saved !== null) {
saved.addAll(a.getRange(startAt, i - startAt));
fn(new String.fromCharCodes(saved));
saved = null;
} else {
fn(new String.fromCharCodes(a.getRange(startAt, i - startAt)));
}
startAt = i + 1;
}
}
if (startAt < len) { saved = a.getRange(startAt, len - startAt); }
}
}

void tapStdin() {
var s = "";
var ios = new StringInputStream(stdin);
var args = new Options().arguments;
var nRepeats = args.length > 0 ? Math.parseInt(args[0]) : 1;
if (nRepeats > 1) {
var lines = [];
while ((s = ios.readLine()) !== null) {
lines.add(s);
}
readRawLines((s) => lines.add(s));
for (var i = 0; i < nRepeats; i++) {
for (s in lines) {
print(mask(s));
}
}
} else {
while ((s = ios.readLine()) !== null) {
print(mask(s));
}
readRawLines((s) => print(mask(s)));
}
exit(0);
}
Expand Down
4 changes: 2 additions & 2 deletions mask.sh
Expand Up @@ -2,6 +2,6 @@

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

0 comments on commit 0f6e7e0

Please sign in to comment.