commit
c45e5c6e0e
19
unpack.go
19
unpack.go
|
@ -21,9 +21,6 @@ func Unpack(str string) (string, error) {
|
||||||
var currentState state = start
|
var currentState state = start
|
||||||
var result strings.Builder
|
var result strings.Builder
|
||||||
numOfRepeat := 1
|
numOfRepeat := 1
|
||||||
//if firstCheckString(&str) == true {
|
|
||||||
// return "", ErrInvalidString
|
|
||||||
//}
|
|
||||||
|
|
||||||
var runeArray = []rune(str)
|
var runeArray = []rune(str)
|
||||||
|
|
||||||
|
@ -38,6 +35,13 @@ func Unpack(str string) (string, error) {
|
||||||
if unicode.IsDigit(char) {
|
if unicode.IsDigit(char) {
|
||||||
currentState = number
|
currentState = number
|
||||||
numOfRepeat, _ = strconv.Atoi(string(char))
|
numOfRepeat, _ = strconv.Atoi(string(char))
|
||||||
|
repStr, err := repeatRune(runeArray[i-1], numOfRepeat)
|
||||||
|
if err != nil {
|
||||||
|
res := result.String()
|
||||||
|
result.Reset()
|
||||||
|
result.WriteString(res[0 : len(res)-1])
|
||||||
|
}
|
||||||
|
result.WriteString(repStr)
|
||||||
} else if char == '\\' {
|
} else if char == '\\' {
|
||||||
currentState = escape
|
currentState = escape
|
||||||
} else {
|
} else {
|
||||||
|
@ -48,18 +52,11 @@ func Unpack(str string) (string, error) {
|
||||||
if unicode.IsDigit(char) {
|
if unicode.IsDigit(char) {
|
||||||
return "", ErrInvalidString
|
return "", ErrInvalidString
|
||||||
}
|
}
|
||||||
repStr, err := repeatRune(runeArray[i-2], numOfRepeat)
|
|
||||||
if err != nil {
|
|
||||||
res := result.String()
|
|
||||||
result.Reset()
|
|
||||||
result.WriteString(res[0 : len(res)-1])
|
|
||||||
}
|
|
||||||
result.WriteString(repStr)
|
|
||||||
currentState = start
|
currentState = start
|
||||||
result.WriteRune(char)
|
result.WriteRune(char)
|
||||||
case escape:
|
case escape:
|
||||||
result.WriteRune(char)
|
result.WriteRune(char)
|
||||||
|
currentState = start
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,10 @@ func TestUnpack(t *testing.T) {
|
||||||
{input: "abccd", expected: "abccd"},
|
{input: "abccd", expected: "abccd"},
|
||||||
{input: "", expected: ""},
|
{input: "", expected: ""},
|
||||||
{input: "aaa0b", expected: "aab"},
|
{input: "aaa0b", expected: "aab"},
|
||||||
|
{input: "a2", expected: "aa"},
|
||||||
|
{input: "aa", expected: "aa"},
|
||||||
|
{input: "a", expected: "a"},
|
||||||
|
{input: "d\n5abc", expected: "d\n\n\n\n\nabc"},
|
||||||
{input: `qwe\4\5`, expected: `qwe45`},
|
{input: `qwe\4\5`, expected: `qwe45`},
|
||||||
{input: `qwe\45`, expected: `qwe44444`},
|
{input: `qwe\45`, expected: `qwe44444`},
|
||||||
{input: `qwe\\5`, expected: `qwe\\\\\`},
|
{input: `qwe\\5`, expected: `qwe\\\\\`},
|
||||||
|
@ -33,7 +37,7 @@ func TestUnpack(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnpackInvalidString(t *testing.T) {
|
func TestUnpackInvalidString(t *testing.T) {
|
||||||
invalidStrings := []string{"3abc", "45", "aaa10b"}
|
invalidStrings := []string{"3abc", "45", "aaa10b", "1"}
|
||||||
for _, tc := range invalidStrings {
|
for _, tc := range invalidStrings {
|
||||||
tc := tc
|
tc := tc
|
||||||
t.Run(tc, func(t *testing.T) {
|
t.Run(tc, func(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue