forked from processing/processing4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextTransform.java
More file actions
327 lines (254 loc) · 9.17 KB
/
TextTransform.java
File metadata and controls
327 lines (254 loc) · 9.17 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package processing.mode.java;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ListIterator;
import java.util.stream.Collectors;
import processing.core.PApplet;
public class TextTransform {
private static final Comparator<Edit> INPUT_OFFSET_COMP =
(o1, o2) -> Integer.compare(o1.fromOffset, o2.fromOffset);
private static final Comparator<Edit> OUTPUT_OFFSET_COMP =
(o1, o2) -> Integer.compare(o1.toOffset, o2.toOffset);
private CharSequence input;
private List<Edit> edits = new ArrayList<>();
private List<Edit> inMap = new ArrayList<>();
private List<Edit> outMap = new ArrayList<>();
private boolean built;
private int builtForLength;
TextTransform(CharSequence input) {
this.input = input;
}
public void add(Edit edit) {
edits.add(edit);
built = false;
}
public void addAll(Collection<Edit> edits) {
this.edits.addAll(edits);
built = false;
}
public String apply() {
final int inLength = input.length();
final StringBuilder output = new StringBuilder(inLength);
buildIfNeeded(inLength);
outMap.stream()
// Filter out Delete edits
.filter(outEdit -> outEdit.toLength > 0)
.forEach(outEdit -> {
if (outEdit.outputText != null) {
// Insert or Replace edit
output.append(outEdit.outputText);
} else {
// Move edit
output.append(input, outEdit.fromOffset, outEdit.fromOffset + outEdit.fromLength);
}
});
return output.toString();
}
public OffsetMapper getMapper() {
int inLength = input.length();
buildIfNeeded(inLength);
return new SimpleOffsetMapper(inMap, outMap);
}
private void buildIfNeeded(int inLength) {
if (built && inLength == builtForLength) return;
// Make copies of Edits to preserve original edits
List<Edit> inEdits = edits.stream().map(Edit::new).collect(Collectors.toList());
List<Edit> outEdits = new ArrayList<>(inEdits);
// Edits sorted by input offsets
Collections.sort(inEdits, INPUT_OFFSET_COMP);
// Edits sorted by output offsets
Collections.sort(outEdits, OUTPUT_OFFSET_COMP);
// TODO: add some validation
// Input
ListIterator<Edit> inIt = inEdits.listIterator();
Edit inEdit = inIt.hasNext() ? inIt.next() : null;
int inEditOff = inEdit == null ? inLength : inEdit.fromOffset;
// Output
ListIterator<Edit> outIt = outEdits.listIterator();
Edit outEdit = outIt.hasNext() ? outIt.next() : null;
int outEditOff = outEdit == null ? inLength : outEdit.toOffset;
int inOffset = 0;
int outOffset = 0;
inMap.clear();
outMap.clear();
// Walk through the input, apply changes, create mapping
while (inOffset < inLength || inEdit != null || outEdit != null) {
{ // Create mapping for unmodified portion of the input
int nextEditOffset = Math.min(inEditOff, outEditOff);
{ // Insert move block to have mapping for unmodified portions too
int length = nextEditOffset - inOffset;
if (length > 0) {
Edit ch = Edit.move(inOffset, length, outOffset);
inMap.add(ch);
outMap.add(ch);
}
}
// Move offsets accordingly
outOffset += nextEditOffset - inOffset;
inOffset = nextEditOffset;
}
// Process encountered input edits
while (inEdit != null && inOffset >= inEditOff) {
inOffset += inEdit.fromLength;
if (inEdit.fromLength > 0) inMap.add(inEdit);
inEdit = inIt.hasNext() ? inIt.next() : null;
inEditOff = inEdit != null ? inEdit.fromOffset : inLength;
}
// Process encountered output edits
while (outEdit != null && inOffset >= outEditOff) {
outEdit.toOffset = outOffset;
if (outEdit.toLength > 0) outMap.add(outEdit);
outOffset += outEdit.toLength;
outEdit = outIt.hasNext() ? outIt.next() : null;
outEditOff = outEdit != null ? outEdit.toOffset : inLength;
}
}
built = true;
builtForLength = inLength;
}
@Override
public String toString() {
return "SourceMapping{" +
"edits=" + edits +
'}';
}
public static class Edit {
public static Edit insert(int offset, String text) {
return new Edit(offset, 0, offset, text.length(), text);
}
public static Edit replace(int offset, int length, String text) {
return new Edit(offset, length, offset, text.length(), text);
}
public static Edit move(int fromOffset, int length, int toOffset) {
Edit result = new Edit(fromOffset, length, toOffset, length, null);
result.toOffset = toOffset;
return result;
}
public static Edit delete(int position, int length) {
return new Edit(position, length, position, 0, null);
}
Edit(Edit edit) {
this.fromOffset = edit.fromOffset;
this.fromLength = edit.fromLength;
this.toOffset = edit.toOffset;
this.toLength = edit.toLength;
this.outputText = edit.outputText;
}
Edit(int fromOffset, int fromLength, int toOffset, int toLength, String text) {
this.fromOffset = fromOffset;
this.fromLength = fromLength;
this.toOffset = toOffset;
this.toLength = toLength;
this.outputText = text;
}
private final int fromOffset;
private final int fromLength;
private int toOffset;
private final int toLength;
private final String outputText;
@Override
public String toString() {
return "Edit{" +
"from=" + fromOffset + ":" + fromLength +
", to=" + toOffset + ":" + toLength +
((outputText != null) ? (", text='" + outputText + '\'') : "") +
'}';
}
}
protected interface OffsetMapper {
int getInputOffset(int outputOffset);
int getOutputOffset(int inputOffset);
OffsetMapper thenMapping(OffsetMapper mapper);
OffsetMapper EMPTY_MAPPER = CompositeOffsetMapper.of();
}
private static class SimpleOffsetMapper implements OffsetMapper {
private List<Edit> inMap = new ArrayList<>();
private List<Edit> outMap = new ArrayList<>();
private int outputOffsetOfInputStart;
private int inputOffsetOfOutputStart;
private SimpleOffsetMapper(List<Edit> inMap, List<Edit> outMap) {
this.inMap.addAll(inMap);
this.outMap.addAll(outMap);
Edit inStart = null;
for (Edit in : this.inMap) {
inStart = in;
if (in.fromLength > 0) break;
}
outputOffsetOfInputStart = inStart == null ? 0 : inStart.toOffset;
Edit outStart = null;
for (Edit out : this.inMap) {
outStart = out;
if (out.toLength > 0) break;
}
inputOffsetOfOutputStart = outStart == null ? 0 : outStart.fromOffset;
}
@Override
public int getInputOffset(int outputOffset) {
if (outputOffset < outputOffsetOfInputStart) return -1;
Edit searchKey = new Edit(0, 0, outputOffset, Integer.MAX_VALUE, null);
int i = Collections.binarySearch(outMap, searchKey, OUTPUT_OFFSET_COMP);
if (i < 0) {
i = -(i + 1);
i -= 1;
}
i = PApplet.constrain(i, 0, outMap.size()-1);
Edit edit = outMap.get(i);
int diff = outputOffset - edit.toOffset;
return edit.fromOffset + Math.min(diff, Math.max(0, edit.fromLength - 1));
}
@Override
public int getOutputOffset(int inputOffset) {
if (inputOffset < inputOffsetOfOutputStart) return -1;
Edit searchKey = new Edit(inputOffset, Integer.MAX_VALUE, 0, 0, null);
int i = Collections.binarySearch(inMap, searchKey, INPUT_OFFSET_COMP);
if (i < 0) {
i = -(i + 1);
i -= 1;
}
i = PApplet.constrain(i, 0, inMap.size()-1);
Edit edit = inMap.get(i);
int diff = inputOffset - edit.fromOffset;
return edit.toOffset + Math.min(diff, Math.max(0, edit.toLength - 1));
}
@Override
public OffsetMapper thenMapping(OffsetMapper mapper) {
return CompositeOffsetMapper.of(this, mapper);
}
}
private static class CompositeOffsetMapper implements OffsetMapper {
private List<OffsetMapper> mappers = new ArrayList<>();
public static CompositeOffsetMapper of(OffsetMapper... inMappers) {
CompositeOffsetMapper composite = new CompositeOffsetMapper();
// Add mappers one by one, unwrap if Composite
for (OffsetMapper mapper : inMappers) {
if (mapper instanceof CompositeOffsetMapper) {
composite.mappers.addAll(((CompositeOffsetMapper) mapper).mappers);
} else {
composite.mappers.add(mapper);
}
}
return composite;
}
@Override
public int getInputOffset(int outputOffset) {
for (int i = mappers.size() - 1; i >= 0; i--) {
outputOffset = mappers.get(i).getInputOffset(outputOffset);
}
return outputOffset;
}
@Override
public int getOutputOffset(int inputOffset) {
for (OffsetMapper mapper : mappers) {
inputOffset = mapper.getOutputOffset(inputOffset);
}
return inputOffset;
}
@Override
public OffsetMapper thenMapping(OffsetMapper mapper) {
return CompositeOffsetMapper.of(this, mapper);
}
}
}