This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite.go
150 lines (136 loc) · 2.86 KB
/
write.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package xsx
import (
"bytes"
"fmt"
"io"
"unicode"
)
// EscapeTo escapes characters in str according to the escape rules of XSX
// quoted atoms. I.e. '"' is replaces with '\"' and '\' is replaced with '\\'.
// The result is written to the dst output buffer.
func EscapeTo(str string, dst io.Writer) (numEsc int, err error) {
var res int
for _, c := range str {
switch c {
case '"':
if _, err = dst.Write([]byte("\\\"")); err != nil {
return 0, err
}
res++
case '\\':
if _, err = dst.Write([]byte("\\\\")); err != nil {
return 0, err
}
res++
default:
if _, err = dst.Write([]byte(string(c))); err != nil {
return 0, err
}
}
}
return res, nil
}
func NeedQuote(str string) bool {
if len(str) == 0 {
return true
}
for _, c := range str {
switch c {
case '"', '\\', '(', '[', '{', ' ', '\t':
return true
default:
if unicode.IsSpace(c) {
return true
}
}
}
return false
}
func QuoteTo(str string, wr io.Writer) (err error) {
if _, err = wr.Write([]byte("\"")); err != nil {
return err
}
if _, err = EscapeTo(str, wr); err != nil {
return err
}
_, err = wr.Write([]byte("\""))
return err
}
func Quoted(str string) string {
buf := bytes.NewBuffer(nil)
QuoteTo(str, buf)
return buf.String()
}
func CondQuoteTo(str string, wr io.Writer) (quoted bool, err error) {
if NeedQuote(str) {
err = QuoteTo(str, wr)
return true, err
} else {
_, err = wr.Write([]byte(str))
return false, err
}
}
func CondQuoted(str string) (string, bool) {
if NeedQuote(str) {
return Quoted(str), true
} else {
return str, false
}
}
type QuoteMode int
const (
// Quote only if needed
Qcond QuoteMode = iota
// Always quote
Qforce
// QSUPPRESS suppresses quoting of an atom. This might break XSX syntax!
QSUPPRESS
)
type B rune
type Bm rune
type printEnd int
// End is passed to the Print function to end the current structure. Print
// will choose the correct bracket to keep them balanced.
const End printEnd = printEnd(0)
type Nl struct {
Count int
Indent int
}
func Write(p Printer, token ...interface{}) (err error) {
for _, t := range token {
switch tok := t.(type) {
case Nl:
if err = p.Newline(tok.Count, tok.Indent); err != nil {
return err
}
case B:
if err = p.Begin(rune(tok), false); err != nil {
return err
}
case Bm:
if err = p.Begin(rune(tok), true); err != nil {
return err
}
case printEnd:
if err = p.End(); err != nil {
return err
}
case string:
if err = p.Atom(tok, false, Qcond); err != nil {
return err
}
case int, uint, bool, float32, float64, int8, uint8,
int16, uint16, int32, uint32, int64, uint64, uintptr:
str := fmt.Sprint(t)
if err = p.Atom(str, false, QSUPPRESS); err != nil {
return err
}
default:
str := fmt.Sprint(t)
if err = p.Atom(str, false, Qcond); err != nil {
return err
}
}
}
return nil
}