Fix string/nth instruction

This changes `string/nth` to return `:ignore-instruction` when the string is empty. Otherwise it would throw an error due to dividing by zero.
This commit is contained in:
Erik Rauer 2021-03-29 21:05:35 -05:00
parent 8e361225ae
commit 695e08506e

View File

@ -158,7 +158,13 @@
:string_nth
^{:stacks #{:char :integer :string}}
(fn [state]
(make-instruction state #(nth %2 (mod %1 (count %2))) [:integer :string] :char)))
(make-instruction state
#(let [str-length (count %2)]
(if (= 0 str-length)
:ignore-instruction
(nth %2 (mod %1 str-length))))
[:integer :string]
:char)))
;; Pushes the number of times the top CHAR occurs in the top STRING onto the
;; INTEGER stack