Тесты проходят. Теперь бы линтеры победить..
parent
e370d8acba
commit
3dfd240682
|
@ -1,4 +1,4 @@
|
|||
module github.com/fixme_my_friend/hw02_unpack_string
|
||||
module github.com/tiburon-777/hw02_unpack_string
|
||||
|
||||
go 1.14
|
||||
|
||||
|
|
|
@ -2,11 +2,59 @@ package hw02_unpack_string //nolint:golint,stylecheck
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
)
|
||||
|
||||
var ErrInvalidString = errors.New("invalid string")
|
||||
|
||||
func Unpack(_ string) (string, error) {
|
||||
// Place your code here
|
||||
return "", nil
|
||||
const slash = `\`
|
||||
|
||||
func Unpack(str string) (string, error) {
|
||||
var b strings.Builder
|
||||
temp := []rune(str)
|
||||
for i := 0; i < len(temp); i++ {
|
||||
if unicode.IsLetter(temp[i]) {
|
||||
// Ловим паттерн [letter][digit]
|
||||
if i < len(temp)-1 && unicode.IsDigit(temp[i+1]) {
|
||||
c, err := strconv.Atoi(string(temp[i+1]))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
b.WriteString(strings.Repeat(string(temp[i]), c))
|
||||
i++
|
||||
// Ловим паттерн [letter]...
|
||||
} else {
|
||||
b.WriteRune(temp[i])
|
||||
}
|
||||
} else if string(temp[i]) == slash {
|
||||
// Ловим паттерн [\][letter][digit]
|
||||
if i < len(temp)-2 && unicode.IsLetter(temp[i+1]) && unicode.IsDigit(temp[i+2]) {
|
||||
c, err := strconv.Atoi(string(temp[i+2]))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
b.WriteString(strings.Repeat(slash+string(temp[i+1]), c))
|
||||
i += 2
|
||||
// Ловим паттерн [\][digit или \][digit]
|
||||
} else if i < len(temp)-2 && (unicode.IsDigit(temp[i+1]) || string(temp[i+1]) == slash) && unicode.IsDigit(temp[i+2]) {
|
||||
c, err := strconv.Atoi(string(temp[i+2]))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
b.WriteString(strings.Repeat(string(temp[i+1]), c))
|
||||
i += 2
|
||||
// Ловим паттерн [\][digit или \]
|
||||
} else if i < len(temp)-1 && (unicode.IsDigit(temp[i+1]) || string(temp[i+1]) == slash) {
|
||||
b.WriteRune(temp[i+1])
|
||||
i++
|
||||
} else {
|
||||
return "", ErrInvalidString
|
||||
}
|
||||
} else {
|
||||
return "", ErrInvalidString
|
||||
}
|
||||
}
|
||||
return b.String(), nil
|
||||
}
|
||||
|
|
|
@ -45,6 +45,29 @@ func TestUnpack(t *testing.T) {
|
|||
input: "aaa0b",
|
||||
expected: "aab",
|
||||
},
|
||||
{
|
||||
input: "fftcwerv ervev sva5scs",
|
||||
expected: "",
|
||||
err: ErrInvalidString,
|
||||
},
|
||||
{
|
||||
input: `||\||(8^`,
|
||||
expected: "",
|
||||
err: ErrInvalidString,
|
||||
},
|
||||
{
|
||||
input: "ис3куРАкс2еу",
|
||||
expected: "иссскуРАкссеу",
|
||||
},
|
||||
{
|
||||
input: "рлрплотчрв",
|
||||
expected: "рлрплотчрв",
|
||||
},
|
||||
{
|
||||
input: string([]rune{1234, 67, 45, 2324, 899, 353, 2345, 65}),
|
||||
expected: "",
|
||||
err: ErrInvalidString,
|
||||
},
|
||||
} {
|
||||
result, err := Unpack(tst.input)
|
||||
require.Equal(t, tst.err, err)
|
||||
|
@ -53,7 +76,7 @@ func TestUnpack(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestUnpackWithEscape(t *testing.T) {
|
||||
t.Skip() // Remove if task with asterisk completed
|
||||
//t.Skip() // Remove if task with asterisk completed
|
||||
|
||||
for _, tst := range [...]test{
|
||||
{
|
||||
|
@ -72,6 +95,11 @@ func TestUnpackWithEscape(t *testing.T) {
|
|||
input: `qwe\\\3`,
|
||||
expected: `qwe\3`,
|
||||
},
|
||||
{
|
||||
input: `3\\cds\4`,
|
||||
expected: ``,
|
||||
err: ErrInvalidString,
|
||||
},
|
||||
} {
|
||||
result, err := Unpack(tst.input)
|
||||
require.Equal(t, tst.err, err)
|
||||
|
|
Loading…
Reference in New Issue