From 927c33af1fae061b2ec6f26d0a14f629d401e0f7 Mon Sep 17 00:00:00 2001 From: linbergil Date: Sun, 5 Nov 2023 05:22:54 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=B5=D1=89=D1=91=20=D0=B1=D0=BE=D0=BB?= =?UTF-8?q?=D1=8C=D1=88=D0=B5=20=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- unpack.go | 14 +++++++------- unpack_test.go | 6 +++++- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/unpack.go b/unpack.go index ea9a1bd..6c2cd51 100644 --- a/unpack.go +++ b/unpack.go @@ -38,6 +38,13 @@ func Unpack(str string) (string, error) { if unicode.IsDigit(char) { currentState = number numOfRepeat, _ = strconv.Atoi(string(char)) + repStr, err := repeatRune(runeArray[i-1], numOfRepeat) + if err != nil { + res := result.String() + result.Reset() + result.WriteString(res[0 : len(res)-1]) + } + result.WriteString(repStr) } else if char == '\\' { currentState = escape } else { @@ -48,13 +55,6 @@ func Unpack(str string) (string, error) { if unicode.IsDigit(char) { return "", ErrInvalidString } - repStr, err := repeatRune(runeArray[i-2], numOfRepeat) - if err != nil { - res := result.String() - result.Reset() - result.WriteString(res[0 : len(res)-1]) - } - result.WriteString(repStr) currentState = start result.WriteRune(char) case escape: diff --git a/unpack_test.go b/unpack_test.go index fb26729..62d46a8 100644 --- a/unpack_test.go +++ b/unpack_test.go @@ -16,6 +16,10 @@ func TestUnpack(t *testing.T) { {input: "abccd", expected: "abccd"}, {input: "", expected: ""}, {input: "aaa0b", expected: "aab"}, + {input: "a2", expected: "aa"}, + {input: "aa", expected: "aa"}, + {input: "a", expected: "a"}, + {input: "d\n5abc", expected: "d\n\n\n\n\nabc"}, {input: `qwe\4\5`, expected: `qwe45`}, {input: `qwe\45`, expected: `qwe44444`}, {input: `qwe\\5`, expected: `qwe\\\\\`}, @@ -33,7 +37,7 @@ func TestUnpack(t *testing.T) { } func TestUnpackInvalidString(t *testing.T) { - invalidStrings := []string{"3abc", "45", "aaa10b"} + invalidStrings := []string{"3abc", "45", "aaa10b", "1"} for _, tc := range invalidStrings { tc := tc t.Run(tc, func(t *testing.T) { From be1ea53d9711f24256b7ccba5ee219bb7ab7ebe2 Mon Sep 17 00:00:00 2001 From: linbergil Date: Sun, 5 Nov 2023 05:26:52 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D0=B5=D1=89=D1=91=20=D0=B1=D0=BE=D0=BB?= =?UTF-8?q?=D1=8C=D1=88=D0=B5=20=D1=82=D0=B5=D1=81=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- unpack.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/unpack.go b/unpack.go index 6c2cd51..8412ea1 100644 --- a/unpack.go +++ b/unpack.go @@ -21,9 +21,6 @@ func Unpack(str string) (string, error) { var currentState state = start var result strings.Builder numOfRepeat := 1 - //if firstCheckString(&str) == true { - // return "", ErrInvalidString - //} var runeArray = []rune(str) @@ -59,7 +56,7 @@ func Unpack(str string) (string, error) { result.WriteRune(char) case escape: result.WriteRune(char) - + currentState = start } }