diff --git a/hw02_unpack_string/go.mod b/hw02_unpack_string/go.mod index 1c9fc9b..5588f22 100644 --- a/hw02_unpack_string/go.mod +++ b/hw02_unpack_string/go.mod @@ -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 \ No newline at end of file diff --git a/hw02_unpack_string/unpack.go b/hw02_unpack_string/unpack.go index 0b3650f..f7da01d 100644 --- a/hw02_unpack_string/unpack.go +++ b/hw02_unpack_string/unpack.go @@ -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 } }