From 3af00fd66bff0db353df9e779213383e425af656 Mon Sep 17 00:00:00 2001 From: Andrey Ivanov Date: Wed, 10 Jun 2020 15:01:14 +0300 Subject: [PATCH] HW2 is completed --- hw02_unpack_string/unpack.go | 10 +++++----- hw02_unpack_string/unpack_test.go | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/hw02_unpack_string/unpack.go b/hw02_unpack_string/unpack.go index e5f00cb..3f5ab08 100644 --- a/hw02_unpack_string/unpack.go +++ b/hw02_unpack_string/unpack.go @@ -9,7 +9,7 @@ import ( var ErrInvalidString = errors.New("invalid string") -const slash = `\` +const slash = '\\' func Unpack(str string) (string, error) { var b strings.Builder @@ -17,17 +17,17 @@ func Unpack(str string) (string, error) { for i := 0; i < len(temp); i++ { switch { // Ловим паттерн [\][letter][digit] - case string(temp[i]) == slash && i < len(temp)-2 && unicode.IsLetter(temp[i+1]) && unicode.IsDigit(temp[i+2]): + case temp[i] == slash && i < len(temp)-2 && unicode.IsLetter(temp[i+1]) && unicode.IsDigit(temp[i+2]): c, _ := strconv.Atoi(string(temp[i+2])) - b.WriteString(strings.Repeat(slash+string(temp[i+1]), c)) + b.WriteString(strings.Repeat(string(slash)+string(temp[i+1]), c)) i += 2 // Ловим паттерн [\][digit или \][digit] - case string(temp[i]) == slash && i < len(temp)-2 && (unicode.IsDigit(temp[i+1]) || string(temp[i+1]) == slash) && unicode.IsDigit(temp[i+2]): + case temp[i] == slash && i < len(temp)-2 && (unicode.IsDigit(temp[i+1]) || temp[i+1] == slash) && unicode.IsDigit(temp[i+2]): c, _ := strconv.Atoi(string(temp[i+2])) b.WriteString(strings.Repeat(string(temp[i+1]), c)) i += 2 // Ловим паттерн [\][digit или \] - case string(temp[i]) == slash && i < len(temp)-1 && (unicode.IsDigit(temp[i+1]) || string(temp[i+1]) == slash): + case temp[i] == slash && i < len(temp)-1 && (unicode.IsDigit(temp[i+1]) || temp[i+1] == slash): b.WriteRune(temp[i+1]) i++ case unicode.IsLetter(temp[i]) && i < len(temp)-1 && unicode.IsDigit(temp[i+1]): diff --git a/hw02_unpack_string/unpack_test.go b/hw02_unpack_string/unpack_test.go index eb66ba8..dfe72a1 100644 --- a/hw02_unpack_string/unpack_test.go +++ b/hw02_unpack_string/unpack_test.go @@ -76,7 +76,6 @@ func TestUnpack(t *testing.T) { } func TestUnpackWithEscape(t *testing.T) { - //t.Skip() // Remove if task with asterisk completed for _, tst := range [...]test{ {