feat: string-unpack
This commit is contained in:
54
string-unpack/unpack.go
Normal file
54
string-unpack/unpack.go
Normal file
@ -0,0 +1,54 @@
|
||||
package stringunpack
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var ErrIncorrectString = errors.New("incorrect string")
|
||||
|
||||
func Unpack(s string) (string, error) {
|
||||
if s == "" {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
var sb strings.Builder
|
||||
sb.Grow(len(s))
|
||||
|
||||
var curSymbol rune
|
||||
for _, r := range s {
|
||||
isNumber := false
|
||||
number := 0
|
||||
if n, err := strconv.Atoi(string(r)); err == nil {
|
||||
number = n
|
||||
isNumber = true
|
||||
}
|
||||
|
||||
if curSymbol == rune(0) {
|
||||
switch {
|
||||
case isNumber:
|
||||
return "", ErrIncorrectString
|
||||
case !isNumber:
|
||||
curSymbol = r
|
||||
}
|
||||
} else {
|
||||
switch {
|
||||
case !isNumber:
|
||||
sb.WriteRune(curSymbol)
|
||||
curSymbol = r
|
||||
case isNumber:
|
||||
for i := 0; i < number; i++ {
|
||||
sb.WriteRune(curSymbol)
|
||||
}
|
||||
curSymbol = rune(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if curSymbol != rune(0) {
|
||||
sb.WriteRune(curSymbol)
|
||||
}
|
||||
|
||||
return sb.String(), nil
|
||||
}
|
44
string-unpack/unpack_test.go
Normal file
44
string-unpack/unpack_test.go
Normal file
@ -0,0 +1,44 @@
|
||||
package stringunpack_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
stringunpack "git.grachevko.ru/grachevko/h/string-unpack"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestUnpack(t *testing.T) {
|
||||
testcases := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{input: "a4bc2d5e", expected: "aaaabccddddde"},
|
||||
{input: "abcd", expected: "abcd"},
|
||||
{input: "aaa0b", expected: "aab"},
|
||||
{input: "", expected: ""},
|
||||
{input: "d\n5abc", expected: "d\n\n\n\n\nabc"},
|
||||
}
|
||||
|
||||
for _, tc := range testcases {
|
||||
t.Run(tc.input, func(t *testing.T) {
|
||||
result, err := stringunpack.Unpack(tc.input)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, tc.expected, result)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnpackIncorrectString(t *testing.T) {
|
||||
testcases := []string{
|
||||
"3abc",
|
||||
"45",
|
||||
"aaa10b",
|
||||
}
|
||||
|
||||
for _, input := range testcases {
|
||||
t.Run(input, func(t *testing.T) {
|
||||
_, err := stringunpack.Unpack(input)
|
||||
assert.Equal(t, stringunpack.ErrIncorrectString, err)
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user