-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (76 loc) · 1.67 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"strconv"
"strings"
"unicode"
)
func main() {
var pstring, unpstring string
fmt.Print("Packed string:")
fmt.Scan(&pstring)
unpstring = Reformat(pstring)
if unpstring == "" {
fmt.Print("Некорректная строка!")
} else {
fmt.Print("Unpacked string:", unpstring)
}
}
func Reformat(str string) (out string) {
var repeat string
var prevLetter rune
var backslash bool
if _, err := strconv.Atoi(str); err == nil {
return ""
}
strrune := []rune(str)
for ind, currRune := range strrune {
if unicode.IsDigit(currRune) {
if backslash {
out = out + string(currRune)
prevLetter = currRune
backslash = false
} else {
repeat = repeat + string(currRune)
if ind+1 != len(strrune) {
if unicode.IsDigit(rune(strrune[ind+1])) {
continue
} else {
out = out + RepeatLetters(repeat, prevLetter)
}
} else {
out = out + RepeatLetters(repeat, prevLetter)
}
}
} else {
repeat = ""
if currRune == '\\' {
if backslash {
backslash = false
prevLetter = currRune
} else {
backslash = true
}
} else {
if ind+1 != len(strrune) && !unicode.IsDigit(rune(strrune[ind+1])) {
out = out + string(currRune)
fmt.Println("tr", currRune, " ", string(currRune))
fmt.Println("r", string(strrune[ind+1]))
} else {
prevLetter = currRune
}
if ind == len(strrune)-1 {
out = out + string(currRune)
}
}
}
}
return
}
func RepeatLetters(repeat string, prevLetter rune) string {
repeatnum, _ := strconv.Atoi(repeat)
if unicode.IsDigit(prevLetter) {
repeatnum--
}
return strings.Repeat(string(prevLetter), repeatnum)
}