From 5cca5bfde98947b4d335e7ca6b61e25d945b6b39 Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Thu, 19 Sep 2024 15:54:59 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20IsInt()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- micro/microfunctions.go | 17 +++++++++++++++++ micro/microfunctions_test.go | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/micro/microfunctions.go b/micro/microfunctions.go index 6ed3d52c..a4fb12cd 100644 --- a/micro/microfunctions.go +++ b/micro/microfunctions.go @@ -961,3 +961,20 @@ func StringFromMassInt64(A []int64, delim string) string { return buffer.String() } + +// IsInt - проверяет, является ли строка целым числом +func IsInt(s string) bool { + Otvet := false + if s == "" { + return Otvet + } + + for _, c := range s { + if !unicode.IsDigit(c) { + return Otvet + } + } + + Otvet = true + return Otvet +} diff --git a/micro/microfunctions_test.go b/micro/microfunctions_test.go index aef321d8..521b8de3 100644 --- a/micro/microfunctions_test.go +++ b/micro/microfunctions_test.go @@ -853,3 +853,23 @@ func TestStringFromMassInt64(t *testing.T) { t.Errorf("Expected '%s', but got: %s", expectedResult, multipleResult) } } + +func TestIsInt(t *testing.T) { + // Test with an empty string + emptyResult := IsInt("") + if emptyResult != false { + t.Errorf("Expected false for empty string, but got: %v", emptyResult) + } + + // Test with a string containing only digits + digitResult := IsInt("12345") + if digitResult != true { + t.Errorf("Expected true for string containing only digits, but got: %v", digitResult) + } + + // Test with a string containing non-digit characters + nonDigitResult := IsInt("abc123") + if nonDigitResult != false { + t.Errorf("Expected false for string containing non-digit characters, but got: %v", nonDigitResult) + } +}