-
Notifications
You must be signed in to change notification settings - Fork 0
/
readcode.go
56 lines (48 loc) · 924 Bytes
/
readcode.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
package tmpl
import (
"bytes"
"unicode/utf8"
)
// readCode takes a string such as:
//
// ".Apple}} more stuff"
//
// and returns:
//
// return ".Apple", " more stuff", nil
func readCode(s string) (string, string, error) {
var code bytes.Buffer
index := 0
//Loop: for 0 < len(s[index:]) {
Loop: for index < len(s) {
r, length := utf8.DecodeRuneInString(s[index:])
if utf8.RuneError == r {
return s, "", errRuneError
}
index += length
switch r {
case '}':
if len(s) < index {
return s, "", errInternalError
}
r2, length2 := utf8.DecodeRuneInString(s[index:])
if utf8.RuneError == r {
return s, "", errRuneError
}
index += length2
switch r2 {
case '}':
break Loop
default:
code.WriteRune(r)
code.WriteRune(r2)
}
default:
code.WriteRune(r)
}
}
if len(s) < index {
return s, "", errInternalError
}
return s[index:], code.String(), nil
}