/
string.go
209 lines (182 loc) · 3.97 KB
/
string.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package pdf
import "bytes"
import "fmt"
import "unicode"
import "unicode/utf16"
// PDF "String" object
// Implements:
// pdf.Object
type ProtectString interface {
Object
Bytes() []byte
}
type String interface {
ProtectString
SetSerializer(func(String,Writer))
}
type stringImpl struct {
value []byte
serializer func(s String, w Writer)
}
// Constructor for Name object
func NewTextString(s string) String {
// If PDFDocEncoding works, use that
result,ok := PDFDocEncoding ([]rune(s))
// Otherwise use UTF16-BE
if !ok {
utf16result := utf16.Encode([]rune(s))
result = make([]byte,0)
result = append(result, 0xfe, 0xff)
for _,w := range utf16result {
result = append(result, byte(w>>8), byte(w&0xff))
}
}
return &stringImpl{result, NormalStringSerializer}
}
func NewBinaryString(s []byte) String {
return &stringImpl{s, NormalStringSerializer}
}
func (s *stringImpl) Bytes() (result []byte) {
newSlice := make([]byte, len(s.value))
copy (newSlice, s.value)
return newSlice
}
func (s *stringImpl) Clone() Object {
newString := *s
return &newString
}
func (s *stringImpl) Dereference() Object {
return s
}
// Return value of Protect() can safely be cast to ProtectString
// but not String.
func (s *stringImpl) Protect() Object {
return &readOnlyString{s}
}
// Return value of Unprotect() can safely be cast to String or
// ProtectString.
func (s *stringImpl) Unprotect() Object {
return s
}
func (s *stringImpl) Serialize(w Writer, file ...File) {
s.serializer(s, w)
}
type readOnlyString struct {
s String
}
func (ros readOnlyString) Bytes() []byte {
return ros.s.Bytes()
}
func (ros readOnlyString) Clone() Object {
return ros
}
func (ros readOnlyString) Dereference() Object {
return ros
}
// Return value of Protect() can safely be cast to ProtectString
// but not String.
func (ros readOnlyString) Protect() Object {
return ros
}
// Return value of Unprotect() can safely be cast to String or
// ProtectString.
func (ros readOnlyString) Unprotect() Object {
return ros.s.Clone()
}
func (ros readOnlyString) Serialize(w Writer, file ...File) {
ros.s.Serialize(w, file...)
}
func stringMinimalEscapeByte(b byte) (result []byte) {
switch b {
case '(', ')', '\\':
result = []byte{'\\', b}
default:
result = []byte{b}
}
return result
}
func NormalStringSerializer(s String, w Writer) {
w.WriteByte('(')
for _, b := range s.Bytes() {
w.Write(stringMinimalEscapeByte(b))
}
w.WriteByte(')')
return
}
func stringAsciiEscapeByte(b byte) (result []byte) {
switch b {
case '(', ')', '\\':
result = []byte{'\\', b}
default:
if b < 128 && unicode.IsPrint(rune(b)) {
result = []byte{b}
} else {
var buffer bytes.Buffer
buffer.WriteByte('\\')
switch b {
case '\n':
buffer.WriteByte('n')
case '\r':
buffer.WriteByte('r')
case '\t':
buffer.WriteByte('t')
case '\b':
buffer.WriteByte('b')
case '\f':
buffer.WriteByte('f')
default:
fmt.Fprintf(&buffer, "%03o", b)
}
result = buffer.Bytes()
}
}
return result
}
func GeneralAsciiEscapeByte(b byte) (result []byte) {
switch b {
case '\\':
result = []byte{'\\', b}
default:
if b < 128 && unicode.IsPrint(rune(b)) {
result = []byte{b}
} else {
var buffer bytes.Buffer
buffer.WriteByte('\\')
switch b {
case '\n':
buffer.WriteByte('n')
case '\r':
buffer.WriteByte('r')
case '\t':
buffer.WriteByte('t')
case '\b':
buffer.WriteByte('b')
case '\f':
buffer.WriteByte('f')
default:
fmt.Fprintf(&buffer, "%03o", b)
}
result = buffer.Bytes()
}
}
return result
}
func AsciiStringSerializer(s String, w Writer) {
w.WriteByte('(')
for _, b := range s.Bytes() {
w.Write(stringAsciiEscapeByte(b))
}
w.WriteByte(')')
return
}
func HexStringSerializer(s String, w Writer) {
w.WriteByte('<')
for _, b := range s.Bytes() {
w.WriteByte(HexDigit(b / 16))
w.WriteByte(HexDigit(b % 16))
}
w.WriteByte('>')
}
func (s *stringImpl) SetSerializer (serializer func(String,Writer)) {
s.serializer = serializer
}