-
Notifications
You must be signed in to change notification settings - Fork 1
/
guacutils.go
53 lines (48 loc) · 1.17 KB
/
guacutils.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
// copy this file into your project
package main
import (
"errors"
"strings"
"strconv"
)
func GuacEncode(src []string) string {
// if src is empty, return empty string
if len(src) == 0 { return "" }
ret := ""
for _, str := range src {
// ret += <length>.<str>,
ret += strconv.Itoa(len(str)) + "." + str + ","
}
// end with a ; and not with a ,
ret = strings.TrimSuffix(ret, ",") + ";"
return ret
}
func GuacDecode(src_ string) ([]string, error) {
// if src is empty, return empty slice
if len(src_) == 0 { return make([]string, 0), nil }
var ret []string
src := src_
for i := 0; i < len(src); {
lenEnd := strings.Index(src, ".")
if lenEnd == -1 {
return nil, errors.New("Invalid guac string (can't find '.')")
}
lenStr := src[i:lenEnd]
lenInt, err := strconv.Atoi(lenStr)
if err != nil {
return nil, err
}
i = lenEnd + 1
segment := src[i:i+lenInt]
ret = append(ret, segment)
i += lenInt
switch []rune(src)[i] {
case ';': break
case ',':
default: return nil, errors.New("Invalid guac string (can't find ',' or ';')")
}
src = src[i+1:]
i = 0
}
return ret, nil
}