diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index a623d0b..ed7007f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -17,15 +17,15 @@ jobs: run: echo "BRANCH=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV - name: Set up Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v4 with: go-version: ~1.22 - name: Check out code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Linters - uses: golangci/golangci-lint-action@v3 + uses: golangci/golangci-lint-action@v4 with: version: v1.57.2 working-directory: ${{ env.BRANCH }} @@ -38,12 +38,12 @@ jobs: run: echo "BRANCH=${GITHUB_REF#refs/heads/}" >> $GITHUB_ENV - name: Set up Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v4 with: go-version: ^1.22 - name: Check out code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Unit tests run: go test -v -count=1 -race -timeout=1m ./... @@ -65,12 +65,12 @@ jobs: if: contains(github.ref, 'calendar') steps: - name: Set up Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v4 with: go-version: ^1.22 - name: Check out code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: make lint run: make lint diff --git a/.golangci.yml b/.golangci.yml index d38eca5..6497dd6 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -17,6 +17,7 @@ issues: - dupl - gocyclo - gosec + - depguard linters: disable-all: true diff --git a/hw02_unpack_string/.sync b/hw02_unpack_string/.sync deleted file mode 100644 index e69de29..0000000 diff --git a/hw02_unpack_string/go.mod b/hw02_unpack_string/go.mod index 3b20310..de2e634 100644 --- a/hw02_unpack_string/go.mod +++ b/hw02_unpack_string/go.mod @@ -2,10 +2,10 @@ module github.com/fixme_my_friend/hw02_unpack_string go 1.22 -require github.com/stretchr/testify v1.7.0 +require github.com/stretchr/testify v1.9.0 require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/hw02_unpack_string/go.sum b/hw02_unpack_string/go.sum index c221f64..60ce688 100644 --- a/hw02_unpack_string/go.sum +++ b/hw02_unpack_string/go.sum @@ -1,13 +1,10 @@ -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= -github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= -gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/hw02_unpack_string/unpack.go b/hw02_unpack_string/unpack.go index 41f003d..459b026 100644 --- a/hw02_unpack_string/unpack.go +++ b/hw02_unpack_string/unpack.go @@ -2,11 +2,48 @@ package hw02unpackstring import ( "errors" + "strconv" + "strings" ) var ErrInvalidString = errors.New("invalid string") -func Unpack(_ string) (string, error) { - // Place your code here. - return "", nil +func Unpack(s string) (string, error) { + var result strings.Builder + k := false + + for i, j := 0, 1; i < len(s); i, j = i+1, j+1 { + if !k { + _, err := strconv.Atoi(string(s[i])) + if err == nil { + return "", ErrInvalidString + } + } + + b := string(s[i]) + + if b == "\\" && !k { + k = true + continue + } + + if j < len(s) { + num, err := strconv.Atoi(string(s[j])) + + if err == nil { + repstr := strings.Repeat(b, num) + result.WriteString(repstr) + i++ + j++ + } else { + result.WriteString(string(s[i])) + } + } else { + result.WriteString(string(s[i])) + } + + k = false + } + + return result.String(), nil } diff --git a/hw02_unpack_string/unpack_test.go b/hw02_unpack_string/unpack_test.go index 9799e18..c34e3b4 100644 --- a/hw02_unpack_string/unpack_test.go +++ b/hw02_unpack_string/unpack_test.go @@ -16,11 +16,15 @@ func TestUnpack(t *testing.T) { {input: "abccd", expected: "abccd"}, {input: "", expected: ""}, {input: "aaa0b", expected: "aab"}, + {input: "a0", expected: ""}, // 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`}, + {input: `a\00`, expected: `a`}, + {input: `\0\\\`, expected: `0\`}, + {input: `\0\\0`, expected: `0`}, } for _, tc := range tests { @@ -34,7 +38,7 @@ func TestUnpack(t *testing.T) { } func TestUnpackInvalidString(t *testing.T) { - invalidStrings := []string{"3abc", "45", "aaa10b"} + invalidStrings := []string{"3abc", "45", "aaa10b", "a0a0a01"} for _, tc := range invalidStrings { tc := tc t.Run(tc, func(t *testing.T) {