From 14973c2365d954c023a874de5f9f49b3576a05d9 Mon Sep 17 00:00:00 2001 From: DimVlas Date: Sat, 13 Apr 2024 12:13:41 +0300 Subject: [PATCH] =?UTF-8?q?hw02=5Fext=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=8D=D0=BA=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hw02_unpack_string/unpack.go | 14 ++++++++++++++ hw02_unpack_string/unpack_test.go | 10 +++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/hw02_unpack_string/unpack.go b/hw02_unpack_string/unpack.go index f5746c1..9b25c67 100644 --- a/hw02_unpack_string/unpack.go +++ b/hw02_unpack_string/unpack.go @@ -14,6 +14,8 @@ var ( nums = []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'} numZero = '0' + + bslash = '\\' ) // True - если руна является цифрой. @@ -40,6 +42,18 @@ func Unpack(text string) (string, error) { return "", ErrInvalidString } + if runes[i] == bslash { // текущий символ слэш + if i == lenRunes-1 { // это последний символ + return "", ErrInvalidString + } + + if !IsDigit(runes[i+1]) && runes[i+1] != bslash { // следующий символ не цифра ине слэш + return "", ErrInvalidString + } + + i++ // нужно обработать следующий символ как обычный + } + if i == lenRunes-1 { // это последний символ res.WriteRune(runes[i]) break diff --git a/hw02_unpack_string/unpack_test.go b/hw02_unpack_string/unpack_test.go index 73dfe8c..25b678d 100644 --- a/hw02_unpack_string/unpack_test.go +++ b/hw02_unpack_string/unpack_test.go @@ -20,10 +20,10 @@ func TestUnpack(t *testing.T) { {input: "", expected: ""}, {input: "aaa0b", expected: "aab"}, // uncomment if task with asterisk completed - // {input: `qwe\4\5`, expected: `qwe45`}, - // {input: `qwe\45`, expected: `qwe44444`}, - // {input: `qwe\\5`, expected: `qwe\\\\\`}, - // {input: `qwe\\\3`, expected: `qwe\3`}, + {input: `qwe\4\5`, expected: `qwe45`}, + {input: `qwe\45`, expected: `qwe44444`}, + {input: `qwe\\5`, expected: `qwe\\\\\`}, + {input: `qwe\\\3`, expected: `qwe\3`}, } for _, tc := range tests { @@ -37,7 +37,7 @@ func TestUnpack(t *testing.T) { } func TestUnpackInvalidString(t *testing.T) { - invalidStrings := []string{"3abc", "45", "aaa10b", "aaa+b10"} + invalidStrings := []string{"3abc", "45", "aaa10b", "aaa+b10", `aaaa\`, `aaa\\\b`} for _, tc := range invalidStrings { tc := tc t.Run(tc, func(t *testing.T) {