diff --git a/awk/atbash-cipher/atbash-cipher.awk b/awk/atbash-cipher/atbash-cipher.awk index e422723f..a08887e4 100644 --- a/awk/atbash-cipher/atbash-cipher.awk +++ b/awk/atbash-cipher/atbash-cipher.awk @@ -34,26 +34,20 @@ function atbash_cipher(input) { return "error: plain-text can't be empty" } - spacer = "" # is this needed here? - for (i = 1; i <= length(filtered); i++) { letter = tolower(letters[i]) #print "plain letter: [" letter "]" shifted_letter = shift_letter(letter) #print "shifted letter: [" shifted_letter "]" - if (direction == "encode") { - if ((i - 1) % 5 == 0) { - spacer = " " - } else { - spacer = "" - } - } - if (length(cipher_text) == 0) { cipher_text = shifted_letter } else { - cipher_text = cipher_text spacer shifted_letter + cipher_text = cipher_text shifted_letter + } + + if (direction == "encode") { + cipher_text = gensub(/[a-z0-9]{5}\B/, "& ", "g", cipher_text) } } diff --git a/awk/atbash-cipher/run-tests-awk.txt b/awk/atbash-cipher/run-tests-awk.txt index 87580136..c343603a 100644 --- a/awk/atbash-cipher/run-tests-awk.txt +++ b/awk/atbash-cipher/run-tests-awk.txt @@ -25,6 +25,7 @@ awkunit.so gawk --lint --file=./atbash-cipher.awk < /dev/null > /dev/null gawk: ./atbash-cipher.awk:3: warning: `load' is a gawk extension +gawk: ./atbash-cipher.awk:50: warning: `gensub' is a gawk extension real 0m0.005s user 0m0.002s @@ -37,16 +38,16 @@ gawk: warning: function `assert' defined but never called directly gawk: ./awkunit.awk:26: warning: reference to uninitialized variable `_assert_exit' real 0m0.006s -user 0m0.003s -sys 0m0.002s +user 0m0.002s +sys 0m0.004s gawk --lint --file=./test-cases.awk < /dev/null > /dev/null gawk: ./test-cases.awk:8: warning: subscript of array `encode_cases' is null string gawk: ./test-cases.awk:22: warning: subscript of array `decode_cases' is null string -real 0m0.004s -user 0m0.002s -sys 0m0.002s +real 0m0.005s +user 0m0.001s +sys 0m0.003s exit 0 @@ -69,9 +70,9 @@ ok 12 decode all the letters ok 13 decode with too many spaces ok 14 decode with no spaces -real 0m0.479s -user 0m0.319s -sys 0m0.187s +real 0m0.597s +user 0m0.394s +sys 0m0.231s exit 0 @@ -238,9 +239,9 @@ Test 26: 26 out of 26 tests passed! -real 0m0.009s +real 0m0.008s user 0m0.003s -sys 0m0.006s +sys 0m0.005s Tests Passed! @@ -250,9 +251,9 @@ exit 0 Running: misspell . -real 0m0.033s +real 0m0.042s user 0m0.041s -sys 0m0.009s +sys 0m0.022s ===============================================================================