-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrle.go
44 lines (40 loc) · 1.2 KB
/
rle.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
package compress
import "strconv"
const (
NULL = "\u0000"
)
func RLE(encode string) string {
var o string // output string
var l int // last index
for i, c := range encode {
if i != 0 && encode[i] != encode[i-1] && string(c) != NULL {
o += strconv.Itoa(i-l) + NULL + string(encode[i-1]) + NULL // format: amount of times character seen + NULL + respective character + NULL
l = i
}
}
if l != len(encode) { // the last few characters need to be added because they'll go undetected otherwise
o += strconv.Itoa(len(encode)-l) + NULL + string(encode[len(encode)-1]) + NULL
}
return o
}
func DeRLE(decode string) string {
var (
o string // output string
b string // buffer
l int
)
for i, c := range decode {
if string(c) == NULL && len(b) > 0 { // this gets respective character and adds it output string
m, _ := strconv.Atoi(b)
for n := 0; n < m; n++ { // add to output string amount of times necessary
o += decode[l:i]
}
l = i + 1 // must add one otherwise NULL will be included
b = ""
} else if string(c) == NULL && len(b) == 0 { // this gets amount of times character is seen
b = decode[l:i]
l = i + 1 // must add one otherwise NULL will be included
}
}
return o
}