commit
5b1035be8a
|
@ -1,4 +1,5 @@
|
|||
## Домашнее задание №2 «Распаковка строки»
|
||||
[](https://travis-ci.com/tiburon-777/HW_OTUS)
|
||||
|
||||
Необходимо написать Go функцию, осуществляющую примитивную распаковку строки,
|
||||
содержащую повторяющиеся символы/руны, например:
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
module github.com/fixme_my_friend/hw02_unpack_string
|
||||
module github.com/tiburon-777/HW_OTUS/hw02_unpack_string
|
||||
|
||||
go 1.14
|
||||
|
||||
|
|
|
@ -5,6 +5,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
|
|||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.5.0 h1:DMOzIV76tmoDNE9pX6RSN0aDtCYeCg5VueieJaAo1uw=
|
||||
github.com/stretchr/testify v1.5.0/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
|
|
@ -2,11 +2,44 @@ 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++ {
|
||||
switch {
|
||||
// Ловим паттерн [\][letter][digit]
|
||||
case temp[i] == slash && i < len(temp)-2 && unicode.IsLetter(temp[i+1]) && unicode.IsDigit(temp[i+2]):
|
||||
c, _ := strconv.Atoi(string(temp[i+2]))
|
||||
b.WriteString(strings.Repeat(string(slash)+string(temp[i+1]), c))
|
||||
i += 2
|
||||
// Ловим паттерн [\][digit или \][digit]
|
||||
case temp[i] == slash && i < len(temp)-2 && (unicode.IsDigit(temp[i+1]) || temp[i+1] == slash) && unicode.IsDigit(temp[i+2]):
|
||||
c, _ := strconv.Atoi(string(temp[i+2]))
|
||||
b.WriteString(strings.Repeat(string(temp[i+1]), c))
|
||||
i += 2
|
||||
// Ловим паттерн [\][digit или \]
|
||||
case temp[i] == slash && i < len(temp)-1 && (unicode.IsDigit(temp[i+1]) || 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, _ := strconv.Atoi(string(temp[i+1]))
|
||||
b.WriteString(strings.Repeat(string(temp[i]), c))
|
||||
i++
|
||||
// Ловим паттерн [letter]...
|
||||
case unicode.IsLetter(temp[i]):
|
||||
b.WriteRune(temp[i])
|
||||
default:
|
||||
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,6 @@ func TestUnpack(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestUnpackWithEscape(t *testing.T) {
|
||||
t.Skip() // Remove if task with asterisk completed
|
||||
|
||||
for _, tst := range [...]test{
|
||||
{
|
||||
|
@ -72,6 +94,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