Skip to content
This repository has been archived by the owner on Mar 5, 2020. It is now read-only.

Commit

Permalink
Created a mask for the Ruby version by avoiding regexp altogether.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpedrosa committed Nov 19, 2011
1 parent 06ed903 commit 0201554
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 4 deletions.
53 changes: 52 additions & 1 deletion luhn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,63 @@ def mask_with_gsub s
end
end

def mask_with_case s
masked = nil
i = 0
digit_count = 0
digit_offset = 0
digits = []
match_from = -1
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'
match_from = i if match_from < 0
digit_count += 1
digits << c.to_i
max_len = digit_count - digit_offset
if max_len >= 14
found = test_it(digits, digit_offset, max_len)
if found
masked = s[0..-1] if not masked
j = i
while j >= match_from
case s[j]
when '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
masked[j] = 'X'
end
j -= 1
end
end
end
if max_len >= 16
digit_offset += 1
match_from += 1
end
when '-', ' '
# Keep going.
else
if digit_count > 0
digit_count = 0
digits = []
match_from = -1
digit_offset = 0
end
end
i += 1
end
masked || s
end

def tap_stdin
n_repeats = ARGV[0] ? ARGV[0].to_i : 1
lines = STDIN.readlines
n_repeats.times do
lines.each do |s|
puts mask(s)
puts mask_with_case(s)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions luhny.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@


def sample_test
[ '56613959932537',
[ #'56613959932537',
# '508733740140655',
# '6853371389452376',
# '49536290423965',
Expand All @@ -20,14 +20,14 @@ def sample_test
# '0' * 1000,
# '4352 7211 4223 5131',
# '7288-8379-3639-2755',
# 'java.lang.FakeException: 7230 3161 3748 4124 is a card #.',
'java.lang.FakeException: 7230 3161 3748 4124 is a card #.',
# "4111 1111 1111 111 doesn't have enough digits.",
# '56613959932535089 has too many digits.',
#'0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
#'5212843322137064296101908962963186202766554788983704435814703972605368416396055678877682488862234546927406211307521284332213706429610190896296318620276655478898370443581470397260536841639605567887768248886223454692740621130752128433221370642961019089629631862027665547889837044358147039726053684163960556788776824888622345469274062113075212843322137064296101908962963186202766554788983704435814703972605368416396055678877682488862234546927406211307521284332213706429610190896296318620276655478898370443581470397260536841639605567887768248886223454692740621130752128433221370642961019089629631862027665547889837044358147039726053684163960556788776824888622345469274062113075212843322137064296101908962963186202766554788983704435814703972605368416396055678877682488862234546927406211307521284332213706429610190896296318620276655478898370443581470397260536841639605567887768248886223454692740621130752128433221370642961019089629631862027665547889837044358147039726053684163960556788776824888622345469274',
#'5451496852732996063216961135925002811586537152199011985874232493633063047918301881385483284586533476253043731721256291647129524137724321728426184434461211703740649863341542579718271551110706936707319896126135944655506777360650140073402696573847382312143994860950153547889826890506187544774005026327396239056283010290981735778560515623251759619833225650753259593746554508212002384743816147220901767420098517594528110348433559626620298669171000062321471778438988210772771125375553564585320157635817785646893772472227467874437527001732836456864256454316370375336790286880557855773092293464498480234269658315323895080609167720971257548045968322939533404152970558280322501801922337656360557826932953501196791996392141409716242024358132638936168824328539157595191336754405365165167449323818836023303930252215414173373588852488517005006682091203000533131570369087503070508446066122223326556254899598241462739083519446495374791938334509806165682356091342533772992235963750210739883141610937988149208758059239',
].each do |s|
puts Luhn.new.mask(s)
puts Luhn.new.mask_with_case(s)
end
end

Expand Down

0 comments on commit 0201554

Please sign in to comment.