Skip to content

Commit

Permalink
hw02_ext добавление экранирования
Browse files Browse the repository at this point in the history
  • Loading branch information
DimVlas authored and DimVlas committed Apr 13, 2024
1 parent 48a1e12 commit 14973c2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
14 changes: 14 additions & 0 deletions hw02_unpack_string/unpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ var (
nums = []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}

numZero = '0'

bslash = '\\'
)

// True - если руна является цифрой.
Expand All @@ -40,6 +42,18 @@ func Unpack(text string) (string, error) {
return "", ErrInvalidString
}

if runes[i] == bslash { // текущий символ слэш
if i == lenRunes-1 { // это последний символ
return "", ErrInvalidString
}

if !IsDigit(runes[i+1]) && runes[i+1] != bslash { // следующий символ не цифра ине слэш
return "", ErrInvalidString
}

i++ // нужно обработать следующий символ как обычный
}

if i == lenRunes-1 { // это последний символ
res.WriteRune(runes[i])
break
Expand Down
10 changes: 5 additions & 5 deletions hw02_unpack_string/unpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ func TestUnpack(t *testing.T) {
{input: "", expected: ""},
{input: "aaa0b", expected: "aab"},
// uncomment if task with asterisk completed
// {input: `qwe\4\5`, expected: `qwe45`},
// {input: `qwe\45`, expected: `qwe44444`},
// {input: `qwe\\5`, expected: `qwe\\\\\`},
// {input: `qwe\\\3`, expected: `qwe\3`},
{input: `qwe\4\5`, expected: `qwe45`},
{input: `qwe\45`, expected: `qwe44444`},
{input: `qwe\\5`, expected: `qwe\\\\\`},
{input: `qwe\\\3`, expected: `qwe\3`},
}

for _, tc := range tests {
Expand All @@ -37,7 +37,7 @@ func TestUnpack(t *testing.T) {
}

func TestUnpackInvalidString(t *testing.T) {
invalidStrings := []string{"3abc", "45", "aaa10b", "aaa+b10"}
invalidStrings := []string{"3abc", "45", "aaa10b", "aaa+b10", `aaaa\`, `aaa\\\b`}
for _, tc := range invalidStrings {
tc := tc
t.Run(tc, func(t *testing.T) {
Expand Down

0 comments on commit 14973c2

Please sign in to comment.