From 695e08506ed7c864b628a0df5bba393f1264bd61 Mon Sep 17 00:00:00 2001 From: Erik Rauer Date: Mon, 29 Mar 2021 21:05:35 -0500 Subject: [PATCH] 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. --- src/propeller/push/instructions/string.cljc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/propeller/push/instructions/string.cljc b/src/propeller/push/instructions/string.cljc index 5218ff5..26398a7 100755 --- a/src/propeller/push/instructions/string.cljc +++ b/src/propeller/push/instructions/string.cljc @@ -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