Боремся с линтерами

pull/2/head
Andrey Ivanov 2020-06-07 19:41:24 +03:00 committed by Andrey Ivanov
parent 74a554bec3
commit 3f8a699aa4
2 changed files with 32 additions and 37 deletions

View File

@ -1,5 +1,6 @@
module github.com/tiburon-777/HW_OTUS/hw02_unpack_string
go 1.14
require github.com/stretchr/testify v1.5.0
require github.com/stretchr/testify v1.5.0

View File

@ -15,44 +15,38 @@ 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])
switch {
// Ловим паттерн [\][letter][digit]
case string(temp[i]) == slash && 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
}
} 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
b.WriteString(strings.Repeat(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]):
c, err := strconv.Atoi(string(temp[i+2]))
if err != nil {
return "", err
}
} else {
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):
b.WriteRune(temp[i+1])
i++
case unicode.IsLetter(temp[i]) && 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]...
case unicode.IsLetter(temp[i]):
b.WriteRune(temp[i])
default:
return "", ErrInvalidString
}
}