Боремся с линтерами
parent
74a554bec3
commit
3f8a699aa4
|
@ -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
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue