-
Notifications
You must be signed in to change notification settings - Fork 2
/
word.go
204 lines (196 loc) · 5.9 KB
/
word.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
// 2019, Georg Sauthoff <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
package main
import (
"bytes"
"io"
)
type word_split_writer struct {
out io.WriteCloser
state int
partial_word []byte
saw_newline bool
// the global default might be too large for some headers like To:
min_word_len int
}
func new_word_split_writer(mwl int, out io.WriteCloser) *word_split_writer {
w := new(word_split_writer)
w.out = out
w.partial_word = make([]byte, 0, max_word_len)
if mwl == -1 {
w.min_word_len = min_word_len
} else {
w.min_word_len = mwl
}
return w
}
func (w *word_split_writer) Write(block []byte) (int, error) {
const ( SKIP_SPACE = iota
WORD_START
IN_WORD
IGNORE_WORD
)
n := len(block)
space := []byte(" \n\t\r/'\"")
nl := []byte("\n")
for len(block) != 0 {
switch w.state {
case WORD_START:
i := index_any(block, space)
if i == -1 {
if len(block) < max_word_len {
w.partial_word = w.partial_word[:0]
w.partial_word = append(w.partial_word, block...)
w.state = IN_WORD
}
block = block[:0]
} else {
if i <= max_word_len && i >= w.min_word_len {
if _, err := w.out.Write(block[:i]); err != nil {
return 0, err
}
}
w.saw_newline = (block[i] == byte('\n'))
block = block[i+1:]
w.state = SKIP_SPACE
}
case IN_WORD:
i := index_any(block, space)
if i == -1 {
if len(w.partial_word) + len(block) < max_word_len {
w.partial_word = append(w.partial_word, block...)
} else {
w.state = IGNORE_WORD
}
block = block[:0]
} else {
l := len(w.partial_word) + i
if l >= w.min_word_len && l <= max_word_len {
w.partial_word = append(w.partial_word, block[:i]...)
if _, err := w.out.Write(w.partial_word); err != nil {
return 0, err
}
}
w.saw_newline = (block[i] == byte('\n'))
block = block[i+1:]
w.state = SKIP_SPACE
}
case IGNORE_WORD:
i := index_any(block, space)
if i == -1 {
block = block[:0]
} else {
w.saw_newline = false
block = block[i:]
w.state = SKIP_SPACE
}
case SKIP_SPACE:
var i int
for i = 0; i < len(block); i++ {
if is_space(block[i]) {
w.saw_newline = w.saw_newline || (block[i] == byte('\n'))
} else {
w.state = WORD_START
break
}
}
block = block[i:]
if w.state == WORD_START {
if w.saw_newline {
if _, err := w.out.Write(nl); err != nil {
return 0, err
}
}
}
}
}
return n, nil
}
func (w *word_split_writer) Close() error {
return w.out.Close()
}
// This writer deals with special code points that are directly
// encoded in the source charset. See also html.go which deals
// with the ones specified via html entities.
// expects full words, i.e. to be chained after split words writer
type replace_chars_writer struct {
out io.WriteCloser
}
func new_replace_chars_writer(out io.WriteCloser) *replace_chars_writer {
return &replace_chars_writer{out}
}
func (w *replace_chars_writer) Write(word []byte) (int, error) {
n := len(word)
soft_hyphen := []byte{0xc2, 0xad}
empty := []byte{}
var x []byte
if bytes.Index(word, soft_hyphen) == -1 {
x = word
} else {
x = bytes.Replace(word, soft_hyphen, empty, -1)
}
if _, err := w.out.Write(x); err != nil {
return 0, err
}
return n, nil
}
func (w *replace_chars_writer) Close() error {
return w.out.Close()
}
type word_writer struct {
out io.WriteCloser
word []byte
}
func new_word_writer(out io.WriteCloser) *word_writer {
w := new(word_writer)
w.out = out
w.word = make([]byte, 1, max_word_len)
w.word[0] = byte('{')
return w
}
func (w *word_writer) Write(block []byte) (int, error) {
if bytes.Equal(block, []byte("\n")) {
if _, err := w.out.Write([]byte("NL\n")); err != nil {
return 0, err
}
} else {
w.word = w.word[:1]
w.word = append(w.word, block...)
w.word = append(w.word, []byte("}\n")...)
if _, err := w.out.Write(w.word); err != nil {
return 0, err
}
}
return len(block), nil
}
func (w *word_writer) Close() error {
return w.out.Close()
}
type nl_writer struct {
out io.WriteCloser
word []byte
}
func new_nl_writer(out io.WriteCloser) *nl_writer {
w := new(nl_writer)
w.out = out
w.word = make([]byte, 0, max_word_len)
return w
}
func (w *nl_writer) Write(block []byte) (int, error) {
if bytes.Equal(block, []byte("\n")) {
if _, err := w.out.Write([]byte("NL\n")); err != nil {
return 0, err
}
} else {
w.word = w.word[:0]
w.word = append(w.word, block...)
w.word = append(w.word, []byte("\n")...)
if _, err := w.out.Write(w.word); err != nil {
return 0, err
}
}
return len(block), nil
}
func (w *nl_writer) Close() error {
return w.out.Close()
}