From e14538257092f83959760e84cf170af56c50191c Mon Sep 17 00:00:00 2001 From: KKlochko Date: Wed, 1 Nov 2023 15:02:44 +0200 Subject: [PATCH] Add int-string? to check if a string is a number. --- src/cipher_analytical_machine/parsers/parsers.clj | 6 ++++++ .../parsers/parsers_test.clj | 12 ++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/cipher_analytical_machine/parsers/parsers.clj b/src/cipher_analytical_machine/parsers/parsers.clj index df268a8..0d5d454 100644 --- a/src/cipher_analytical_machine/parsers/parsers.clj +++ b/src/cipher_analytical_machine/parsers/parsers.clj @@ -8,6 +8,12 @@ (if (re-matches #"\d+" str) true false)) +(defn int-string? + "Return true if the string is an signed integer." + [str] + (if (re-matches #"[+-]?\b\d+\b" str) + true false)) + (defn parse-unsigned-int "Return an integer if the argument is an integer." [str-or-int] diff --git a/test/cipher_analytical_machine/parsers/parsers_test.clj b/test/cipher_analytical_machine/parsers/parsers_test.clj index 46d20e5..1d027f3 100644 --- a/test/cipher_analytical_machine/parsers/parsers_test.clj +++ b/test/cipher_analytical_machine/parsers/parsers_test.clj @@ -16,6 +16,18 @@ "-10" false "abc" false))) +(deftest int-string?-test + (testing "The function return true only for an singned integer as a string." + (are [str expected] + (= expected + (int-string? str)) + "9" true + "10" true + "-10" true + "asd -10 " false + " -10 " false + "abc" false))) + (deftest parse-unsigned-int-test (testing "The function parse the integer if the string is an integer." (are [str-or-int expected]