HW2 is completed
parent
dfb22256a1
commit
3af00fd66b
|
@ -9,7 +9,7 @@ import (
|
||||||
|
|
||||||
var ErrInvalidString = errors.New("invalid string")
|
var ErrInvalidString = errors.New("invalid string")
|
||||||
|
|
||||||
const slash = `\`
|
const slash = '\\'
|
||||||
|
|
||||||
func Unpack(str string) (string, error) {
|
func Unpack(str string) (string, error) {
|
||||||
var b strings.Builder
|
var b strings.Builder
|
||||||
|
@ -17,17 +17,17 @@ func Unpack(str string) (string, error) {
|
||||||
for i := 0; i < len(temp); i++ {
|
for i := 0; i < len(temp); i++ {
|
||||||
switch {
|
switch {
|
||||||
// Ловим паттерн [\][letter][digit]
|
// Ловим паттерн [\][letter][digit]
|
||||||
case string(temp[i]) == slash && i < len(temp)-2 && unicode.IsLetter(temp[i+1]) && unicode.IsDigit(temp[i+2]):
|
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]))
|
c, _ := strconv.Atoi(string(temp[i+2]))
|
||||||
b.WriteString(strings.Repeat(slash+string(temp[i+1]), c))
|
b.WriteString(strings.Repeat(string(slash)+string(temp[i+1]), c))
|
||||||
i += 2
|
i += 2
|
||||||
// Ловим паттерн [\][digit или \][digit]
|
// Ловим паттерн [\][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]):
|
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]))
|
c, _ := strconv.Atoi(string(temp[i+2]))
|
||||||
b.WriteString(strings.Repeat(string(temp[i+1]), c))
|
b.WriteString(strings.Repeat(string(temp[i+1]), c))
|
||||||
i += 2
|
i += 2
|
||||||
// Ловим паттерн [\][digit или \]
|
// Ловим паттерн [\][digit или \]
|
||||||
case string(temp[i]) == slash && i < len(temp)-1 && (unicode.IsDigit(temp[i+1]) || string(temp[i+1]) == slash):
|
case temp[i] == slash && i < len(temp)-1 && (unicode.IsDigit(temp[i+1]) || temp[i+1] == slash):
|
||||||
b.WriteRune(temp[i+1])
|
b.WriteRune(temp[i+1])
|
||||||
i++
|
i++
|
||||||
case unicode.IsLetter(temp[i]) && i < len(temp)-1 && unicode.IsDigit(temp[i+1]):
|
case unicode.IsLetter(temp[i]) && i < len(temp)-1 && unicode.IsDigit(temp[i+1]):
|
||||||
|
|
|
@ -76,7 +76,6 @@ func TestUnpack(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUnpackWithEscape(t *testing.T) {
|
func TestUnpackWithEscape(t *testing.T) {
|
||||||
//t.Skip() // Remove if task with asterisk completed
|
|
||||||
|
|
||||||
for _, tst := range [...]test{
|
for _, tst := range [...]test{
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue