/
line-iter.go
285 lines (245 loc) · 6.03 KB
/
line-iter.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright (C) 2023 Takayuki Sato. All Rights Reserved.
// This program is free software under MIT License.
// See the file LICENSE in this distribution for more details.
package linebreak
import (
"strings"
"text/scanner"
"unicode"
"golang.org/x/text/width"
)
// Line break opprtunity type
type lboType int
const (
lbo_never lboType = iota
lbo_before
lbo_after
lbo_both
lbo_break
lbo_space
)
type lboState struct {
lboType lboType
lboPrev lboType
openApos int8 // 0:not, 1:opened, 2:opened inside "..."
openQuot int8 // 0:not, 1:opened, 2:opened inside '...'
}
// LineIter is the struct that outputs the given string line by line.
// This struct can control the overall line width and the indentation from any
// desired line.
type LineIter struct {
scanner *scanner.Scanner
buffer runeBuffer
width [2]int /* 0: width before lbo, 1: width after lbo */
lboPos int
limit int
indent string
openQuot int8
openApos int8
}
// New is the function that creates a LineIter instance which outputs the given
// string line by line.
// The second arguument is the width of the output lines.
func New(text string, lineWidth int) LineIter {
sc := new(scanner.Scanner)
sc.Init(strings.NewReader(text))
iter := LineIter{}
iter.scanner = sc
iter.buffer = newRuneBuffer(lineWidth)
iter.limit = lineWidth
return iter
}
// SetIndent is the method to set an indentation for the subsequent lines.
func (iter *LineIter) SetIndent(indent string) {
iter.indent = indent
}
// Init is the method to re-initialize with an argument string for reusing this
// instance.
func (iter *LineIter) Init(text string) {
iter.scanner.Init(strings.NewReader(text))
iter.buffer.length = 0
iter.width[0] = 0
iter.width[1] = 0
iter.lboPos = 0
iter.openQuot = 0
iter.openApos = 0
}
// Next is the method that returns a string of the next line and a bool which
// indicates whether there are more next lines or not.
func (iter *LineIter) Next() (string, bool) {
limit := iter.limit - len(iter.indent)
var line string
var state lboState
state.openQuot = iter.openQuot
state.openApos = iter.openApos
for r := iter.scanner.Next(); r != scanner.EOF; r = iter.scanner.Next() {
lineBreakOpportunity(r, &state)
if state.lboType == lbo_break {
line = string(trimRight(iter.buffer.full()))
iter.buffer.length = 0
iter.width[0] = 0
iter.width[1] = 0
iter.openQuot = 0
iter.openApos = 0
iter.lboPos = 0
if len(line) > 0 {
line = iter.indent + line
}
return line, true
}
if iter.buffer.length == 0 && state.lboType == lbo_space {
continue
}
runeW := RuneWidth(r)
lboPos := iter.lboPos
if (iter.width[0] + iter.width[1] + runeW) > limit {
if state.lboPrev == lbo_before {
line := string(trimRight(iter.buffer.runes[0:lboPos]))
iter.buffer.cr(lboPos)
iter.buffer.add(r)
iter.width[0] = iter.width[1] + runeW
iter.width[1] = 0
iter.lboPos = iter.buffer.length
iter.openQuot = state.openQuot
iter.openApos = state.openApos
if len(line) > 0 {
line = iter.indent + line
}
return line, true
}
switch state.lboType {
case lbo_before, lbo_both, lbo_space:
lboPos = iter.buffer.length
}
// break forcely when no lbo in the current line.
if lboPos == 0 {
iter.width[0] += iter.width[1]
iter.width[1] = 0
lboPos = iter.buffer.length
}
line := string(trimRight(iter.buffer.runes[0:lboPos]))
iter.buffer.cr(lboPos)
switch state.lboType {
case lbo_space:
iter.width[0] = 0
iter.width[1] = 0
iter.lboPos = 0
case lbo_before, lbo_both:
iter.buffer.add(r)
iter.width[0] = runeW
iter.width[1] = 0
iter.lboPos = 0
case lbo_after:
iter.buffer.add(r)
iter.width[0] = iter.width[1] + runeW
iter.width[1] = 0
iter.lboPos = iter.buffer.length
default:
iter.buffer.add(r)
iter.width[0] = iter.width[1] + runeW
iter.width[1] = 0
iter.lboPos = 0
}
iter.openQuot = state.openQuot
iter.openApos = state.openApos
if len(line) > 0 {
line = iter.indent + line
}
return line, true
}
if runeW > 0 {
iter.buffer.add(r)
}
switch state.lboType {
case lbo_before:
if state.lboPrev != lbo_before {
iter.lboPos = iter.buffer.length - 1
}
iter.width[0] += iter.width[1]
iter.width[1] = runeW
case lbo_both:
iter.lboPos = iter.buffer.length - 1
iter.width[0] += iter.width[1]
iter.width[1] = runeW
case lbo_after, lbo_space:
iter.lboPos = iter.buffer.length
iter.width[0] += iter.width[1] + runeW
iter.width[1] = 0
default:
iter.width[1] += runeW
}
}
line = string(trimRight(iter.buffer.full()))
iter.buffer.length = 0
if len(line) > 0 {
line = iter.indent + line
}
return line, false
}
func lineBreakOpportunity(r rune, state *lboState) {
state.lboPrev = state.lboType
switch r {
case 0x22: // "
if state.openQuot == 0 { // open
state.openQuot = state.openApos + 1
state.lboType = lbo_before
} else { // close
if state.openQuot < state.openApos {
state.openApos = 0
}
state.openQuot = 0
state.lboType = lbo_after
}
return
case 0x27: // '
if state.openApos == 0 { // open
state.openApos = state.openQuot + 1
state.lboType = lbo_before
} else { // close
if state.openApos < state.openQuot {
state.openQuot = 0
}
state.openApos = 0
state.lboType = lbo_after
}
return
}
if contains(lboBreaks, r) {
state.lboType = lbo_break
return
}
if contains(lboBefores, r) {
state.lboType = lbo_before
return
}
if contains(lboAfters, r) {
state.lboType = lbo_after
return
}
if unicode.IsSpace(r) {
state.lboType = lbo_space
return
}
switch width.LookupRune(r).Kind() {
case width.EastAsianWide, width.EastAsianFullwidth:
state.lboType = lbo_both
return
}
state.lboType = lbo_never
}
func contains(candidates []rune, r rune) bool {
for _, e := range candidates {
if e == r {
return true
}
}
return false
}
func trimRight(runes []rune) []rune {
for i := len(runes) - 1; i >= 0; i-- {
if !unicode.IsSpace(runes[i]) {
return runes[0 : i+1]
}
}
return []rune{}
}