Боремся с линтерами
parent
74a554bec3
commit
3f8a699aa4
|
@ -1,5 +1,6 @@
|
||||||
module github.com/tiburon-777/HW_OTUS/hw02_unpack_string
|
module github.com/tiburon-777/HW_OTUS/hw02_unpack_string
|
||||||
|
|
||||||
|
|
||||||
go 1.14
|
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
|
var b strings.Builder
|
||||||
temp := []rune(str)
|
temp := []rune(str)
|
||||||
for i := 0; i < len(temp); i++ {
|
for i := 0; i < len(temp); i++ {
|
||||||
if unicode.IsLetter(temp[i]) {
|
switch {
|
||||||
// Ловим паттерн [letter][digit]
|
// Ловим паттерн [\][letter][digit]
|
||||||
if i < len(temp)-1 && unicode.IsDigit(temp[i+1]) {
|
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+1]))
|
c, err := strconv.Atoi(string(temp[i+2]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
|
||||||
b.WriteString(strings.Repeat(string(temp[i]), c))
|
|
||||||
i++
|
|
||||||
// Ловим паттерн [letter]...
|
|
||||||
} else {
|
|
||||||
b.WriteRune(temp[i])
|
|
||||||
}
|
}
|
||||||
} else if string(temp[i]) == slash {
|
b.WriteString(strings.Repeat(slash+string(temp[i+1]), c))
|
||||||
// Ловим паттерн [\][letter][digit]
|
i += 2
|
||||||
if i < len(temp)-2 && unicode.IsLetter(temp[i+1]) && unicode.IsDigit(temp[i+2]) {
|
// Ловим паттерн [\][digit или \][digit]
|
||||||
c, err := strconv.Atoi(string(temp[i+2]))
|
case string(temp[i]) == slash && i < len(temp)-2 && (unicode.IsDigit(temp[i+1]) || string(temp[i+1]) == slash) && unicode.IsDigit(temp[i+2]):
|
||||||
if err != nil {
|
c, err := strconv.Atoi(string(temp[i+2]))
|
||||||
return "", err
|
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 {
|
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
|
return "", ErrInvalidString
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue