-
Notifications
You must be signed in to change notification settings - Fork 275
/
Copy pathEscaping.java
734 lines (644 loc) · 26.6 KB
/
Escaping.java
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
package com.vladsch.flexmark.util.sequence;
import com.vladsch.flexmark.util.misc.CharPredicate;
import com.vladsch.flexmark.util.misc.Utils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Escaping {
// pure chars not for pattern
final public static String ESCAPABLE_CHARS = "\"#$%&'()*+,./:;<=>?@[]\\^_`{|}~-";
final public static String ESCAPABLE = "[!" +
ESCAPABLE_CHARS
.replace("\\", "\\\\")
.replace("[", "\\[")
.replace("]", "\\]") +
"]";
final private static String ENTITY = "&(?:#x[a-f0-9]{1,8}|#[0-9]{1,8}|[a-z][a-z0-9]{1,31});";
final private static Pattern BACKSLASH_ONLY = Pattern.compile("[\\\\]");
final private static Pattern ESCAPED_CHAR =
Pattern.compile("\\\\" + ESCAPABLE, Pattern.CASE_INSENSITIVE);
final private static Pattern BACKSLASH_OR_AMP = Pattern.compile("[\\\\&]");
final private static Pattern AMP_ONLY = Pattern.compile("[\\&]");
final private static Pattern ENTITY_OR_ESCAPED_CHAR =
Pattern.compile("\\\\" + ESCAPABLE + '|' + ENTITY, Pattern.CASE_INSENSITIVE);
final private static Pattern ENTITY_ONLY =
Pattern.compile(ENTITY, Pattern.CASE_INSENSITIVE);
final private static String XML_SPECIAL = "[&<>\"]";
final private static Pattern XML_SPECIAL_RE = Pattern.compile(XML_SPECIAL);
final private static Pattern XML_SPECIAL_OR_ENTITY =
Pattern.compile(ENTITY + '|' + XML_SPECIAL, Pattern.CASE_INSENSITIVE);
// From RFC 3986 (see "reserved", "unreserved") except don't escape '[' or ']' to be compatible with JS encodeURI
final private static Pattern ESCAPE_IN_URI =
Pattern.compile("(%[a-fA-F0-9]{0,2}|[^:/?#@!$&'()*+,;=a-zA-Z0-9\\-._~])");
final private static Pattern ESCAPE_URI_DECODE =
Pattern.compile("(%[a-fA-F0-9]{2})");
static final char[] HEX_DIGITS =
new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
final private static Pattern WHITESPACE = Pattern.compile("[ \t\r\n]+");
final private static Pattern COLLAPSE_WHITESPACE = Pattern.compile("[ \t]{2,}");
final private static Replacer UNSAFE_CHAR_REPLACER = new Replacer() {
@Override
public void replace(@NotNull String s, @NotNull StringBuilder sb) {
if (s.equals("&")) {
sb.append("&");
} else if (s.equals("<")) {
sb.append("<");
} else if (s.equals(">")) {
sb.append(">");
} else if (s.equals("\"")) {
sb.append(""");
} else {
sb.append(s);
}
}
@Override
public void replace(@NotNull BasedSequence original, int startIndex, int endIndex, @NotNull ReplacedTextMapper textMapper) {
String s1 = original.subSequence(startIndex, endIndex).toString();
if (s1.equals("&")) {
textMapper.addReplacedText(startIndex, endIndex, PrefixedSubSequence.prefixOf("&", BasedSequence.NULL));
} else if (s1.equals("<")) {
textMapper.addReplacedText(startIndex, endIndex, PrefixedSubSequence.prefixOf("<", BasedSequence.NULL));
} else if (s1.equals(">")) {
textMapper.addReplacedText(startIndex, endIndex, PrefixedSubSequence.prefixOf(">", BasedSequence.NULL));
} else if (s1.equals("\"")) {
textMapper.addReplacedText(startIndex, endIndex, PrefixedSubSequence.prefixOf(""", BasedSequence.NULL));
} else {
textMapper.addOriginalText(startIndex, endIndex);
}
}
};
final private static Replacer COLLAPSE_WHITESPACE_REPLACER = new Replacer() {
@Override
public void replace(@NotNull String s, @NotNull StringBuilder sb) {
sb.append(" ");
}
@Override
public void replace(@NotNull BasedSequence original, int startIndex, int endIndex, @NotNull ReplacedTextMapper textMapper) {
textMapper.addReplacedText(startIndex, endIndex, original.subSequence(startIndex, startIndex + 1));
}
};
final private static Replacer UNESCAPE_REPLACER = new Replacer() {
@Override
public void replace(@NotNull String s, @NotNull StringBuilder sb) {
if (s.charAt(0) == '\\') {
sb.append(s, 1, s.length());
} else {
sb.append(Html5Entities.entityToString(s));
}
}
@Override
public void replace(@NotNull BasedSequence original, int startIndex, int endIndex, @NotNull ReplacedTextMapper textMapper) {
if (original.charAt(startIndex) == '\\') {
textMapper.addReplacedText(startIndex, endIndex, original.subSequence(startIndex + 1, endIndex));
} else {
textMapper.addReplacedText(startIndex, endIndex, Html5Entities.entityToSequence(original.subSequence(startIndex, endIndex)));
}
}
};
final private static Replacer REMOVE_REPLACER = new Replacer() {
@Override
public void replace(@NotNull String s, @NotNull StringBuilder sb) {
}
@Override
public void replace(@NotNull BasedSequence original, int startIndex, int endIndex, @NotNull ReplacedTextMapper textMapper) {
textMapper.addReplacedText(startIndex, endIndex, original.subSequence(endIndex, endIndex));
}
};
final private static Replacer ENTITY_REPLACER = new Replacer() {
@Override
public void replace(@NotNull String s, @NotNull StringBuilder sb) {
sb.append(Html5Entities.entityToString(s));
}
@Override
public void replace(@NotNull BasedSequence original, int startIndex, int endIndex, @NotNull ReplacedTextMapper textMapper) {
textMapper.addReplacedText(startIndex, endIndex, Html5Entities.entityToSequence(original.subSequence(startIndex, endIndex)));
}
};
final private static Replacer URL_ENCODE_REPLACER = new Replacer() {
@Override
public void replace(@NotNull String s, @NotNull StringBuilder sb) {
if (s.startsWith("%")) {
if (s.length() == 3) {
// Already percent-encoded, preserve
sb.append(s);
} else {
// %25 is the percent-encoding for %
sb.append("%25");
sb.append(s, 1, s.length());
}
} else {
byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
for (byte b : bytes) {
sb.append('%');
sb.append(HEX_DIGITS[(b >> 4) & 0xF]);
sb.append(HEX_DIGITS[b & 0xF]);
}
}
}
@Override
public void replace(@NotNull BasedSequence original, int startIndex, int endIndex, @NotNull ReplacedTextMapper textMapper) {
BasedSequence s = original.subSequence(startIndex, endIndex);
if (s.startsWith("%")) {
if (s.length() == 3) {
// Already percent-encoded, preserve
textMapper.addOriginalText(startIndex, endIndex);
} else {
// %25 is the percent-encoding for %
textMapper.addReplacedText(startIndex, startIndex + 1, PrefixedSubSequence.prefixOf("%25", BasedSequence.NULL));
textMapper.addOriginalText(startIndex + 1, endIndex);
}
} else {
byte[] bytes = s.toString().getBytes(StandardCharsets.UTF_8);
StringBuilder sbItem = new StringBuilder();
for (byte b : bytes) {
sbItem.append('%');
sbItem.append(HEX_DIGITS[(b >> 4) & 0xF]);
sbItem.append(HEX_DIGITS[b & 0xF]);
}
textMapper.addReplacedText(startIndex, endIndex, PrefixedSubSequence.prefixOf(sbItem.toString(), BasedSequence.NULL));
}
}
};
final private static Replacer URL_DECODE_REPLACER = new Replacer() {
@Override
public void replace(@NotNull String s, @NotNull StringBuilder sb) {
String urlDecoded = Utils.urlDecode(s, null);
sb.append(urlDecoded);
}
@Override
public void replace(@NotNull BasedSequence original, int startIndex, int endIndex, @NotNull ReplacedTextMapper textMapper) {
BasedSequence s = original.subSequence(startIndex, endIndex);
String decoded = Utils.urlDecode(s.toString(), null);
textMapper.addReplacedText(startIndex, endIndex, PrefixedSubSequence.prefixOf(decoded, BasedSequence.NULL));
}
};
final public static @NotNull CharPredicate AMP_BACKSLASH_SET = CharPredicate.anyOf('\\', '&');
public static String escapeHtml(@NotNull CharSequence s, boolean preserveEntities) {
Pattern p = preserveEntities ? XML_SPECIAL_OR_ENTITY : XML_SPECIAL_RE;
return replaceAll(p, s, UNSAFE_CHAR_REPLACER);
}
@NotNull
public static BasedSequence escapeHtml(@NotNull BasedSequence s, boolean preserveEntities, @NotNull ReplacedTextMapper textMapper) {
Pattern p = preserveEntities ? XML_SPECIAL_OR_ENTITY : XML_SPECIAL_RE;
return replaceAll(p, s, UNSAFE_CHAR_REPLACER, textMapper);
}
/**
* Replace entities and backslash escapes with literal characters.
*
* @param s string to un-escape
* @return un-escaped string
*/
@NotNull
public static String unescapeString(@NotNull CharSequence s) {
if (BACKSLASH_OR_AMP.matcher(s).find()) {
return replaceAll(ENTITY_OR_ESCAPED_CHAR, s, UNESCAPE_REPLACER);
} else {
return String.valueOf(s);
}
}
/**
* Replace entities and backslash escapes with literal characters.
*
* @param s string to un-escape
* @param unescapeEntities true if HTML entities are to be unescaped
* @return un-escaped string
*/
@NotNull
public static String unescapeString(@NotNull CharSequence s, boolean unescapeEntities) {
if (unescapeEntities) {
if (BACKSLASH_OR_AMP.matcher(s).find()) {
return replaceAll(ESCAPED_CHAR, s, UNESCAPE_REPLACER);
} else {
return String.valueOf(s);
}
} else {
if (BACKSLASH_ONLY.matcher(s).find()) {
return replaceAll(ENTITY_OR_ESCAPED_CHAR, s, UNESCAPE_REPLACER);
} else {
return String.valueOf(s);
}
}
}
/**
* Replace entities and backslash escapes with literal characters.
*
* @param s based sequence to un-escape
* @param textMapper replaced text mapper to update for the changed text
* @return un-escaped sequence
*/
@NotNull
public static BasedSequence unescape(@NotNull BasedSequence s, @NotNull ReplacedTextMapper textMapper) {
int indexOfAny = s.indexOfAny(AMP_BACKSLASH_SET);
if (indexOfAny != -1) {
return replaceAll(ENTITY_OR_ESCAPED_CHAR, s, UNESCAPE_REPLACER, textMapper);
} else {
return s;
}
}
/**
* Replace entities and backslash escapes with literal characters.
*
* @param s sequence being changed
* @param remove string to remove
* @param textMapper replaced text mapper to update for the changed text
* @return un-escaped sequence
*/
@NotNull
public static BasedSequence removeAll(@NotNull BasedSequence s, @NotNull CharSequence remove, @NotNull ReplacedTextMapper textMapper) {
int indexOf = s.indexOf(remove);
if (indexOf != -1) {
return replaceAll(Pattern.compile("\\Q" + remove + "\\E"), s, REMOVE_REPLACER, textMapper);
} else {
return s;
}
}
/**
* Replace entities and backslash escapes with literal characters.
*
* @param s string to un-escape
* @return un-escaped string
*/
@NotNull
public static String unescapeHtml(@NotNull CharSequence s) {
if (AMP_ONLY.matcher(s).find()) {
return replaceAll(ENTITY_ONLY, s, ENTITY_REPLACER);
} else {
return String.valueOf(s);
}
}
/**
* Replace entities and backslash escapes with literal characters.
*
* @param s based sequence to un-escape
* @param textMapper replaced text mapper to update for the changed text
* @return un-escaped sequence
*/
@NotNull
public static BasedSequence unescapeHtml(@NotNull BasedSequence s, @NotNull ReplacedTextMapper textMapper) {
int indexOfAny = s.indexOf('&');
if (indexOfAny != -1) {
return replaceAll(ENTITY_ONLY, s, ENTITY_REPLACER, textMapper);
} else {
return s;
}
}
/**
* Replace entities and backslash escapes with literal characters.
*
* @param s based sequence to un-escape
* @param textMapper replaced text mapper to update for the changed text
* @return un-escaped sequence
*/
@NotNull
public static BasedSequence unescapeHtml(@NotNull BasedSequence s, @NotNull List<Range> ranges, @NotNull ReplacedTextMapper textMapper) {
int indexOfAny = s.indexOf('&');
if (indexOfAny != -1) {
return replaceAll(ENTITY_ONLY, s, ranges, ENTITY_REPLACER, textMapper);
} else {
return s;
}
}
/**
* Normalize eol: embedded \r and \r\n are converted to \n
* <p>
* Append EOL sequence if sequence does not already end in EOL
*
* @param s sequence to convert
* @return converted sequence
*/
@NotNull
public static String normalizeEndWithEOL(@NotNull CharSequence s) {
return normalizeEOL(s, true);
}
/**
* Normalize eol: embedded \r and \r\n are converted to \n
*
* @param s sequence to convert
* @return converted sequence
*/
@NotNull
public static String normalizeEOL(@NotNull CharSequence s) {
return normalizeEOL(s, false);
}
/**
* Normalize eol: embedded \r and \r\n are converted to \n
*
* @param s sequence to convert
* @param endWithEOL true if an EOL is to be appended to the end of the sequence if not already ending with one.
* @return converted sequence
*/
@NotNull
public static String normalizeEOL(@NotNull CharSequence s, boolean endWithEOL) {
StringBuilder sb = new StringBuilder(s.length());
int iMax = s.length();
boolean hadCR = false;
boolean hadEOL = false;
for (int i = 0; i < iMax; i++) {
char c = s.charAt(i);
if (c == '\r') {
hadCR = true;
} else if (c == '\n') {
sb.append("\n");
hadCR = false;
hadEOL = true;
} else {
if (hadCR) sb.append('\n');
sb.append(c);
hadCR = false;
hadEOL = false;
}
}
if (endWithEOL && !hadEOL) sb.append('\n');
return sb.toString();
}
/**
* Normalize eol: embedded \r and \r\n are converted to \n
* <p>
* Append EOL sequence if sequence does not already end in EOL
*
* @param s sequence to convert
* @param textMapper text mapper to update for the replaced text
* @return converted sequence
*/
@NotNull
public static BasedSequence normalizeEndWithEOL(@NotNull BasedSequence s, @NotNull ReplacedTextMapper textMapper) {
return normalizeEOL(s, textMapper, true);
}
/**
* Normalize eol: embedded \r and \r\n are converted to \n
*
* @param s sequence to convert
* @param textMapper text mapper to update for the replaced text
* @return converted sequence
*/
@NotNull
public static BasedSequence normalizeEOL(@NotNull BasedSequence s, @NotNull ReplacedTextMapper textMapper) {
return normalizeEOL(s, textMapper, false);
}
/**
* Normalize eol: embedded \r and \r\n are converted to \n
* <p>
* Append EOL sequence if sequence does not already end in EOL
*
* @param s sequence to convert
* @param textMapper text mapper to update for the replaced text
* @param endWithEOL whether an EOL is to be appended to the end of the sequence if it does not already end with one.
* @return converted sequence
*/
@NotNull
public static BasedSequence normalizeEOL(@NotNull BasedSequence s, @NotNull ReplacedTextMapper textMapper, boolean endWithEOL) {
int iMax = s.length();
int lastPos = 0;
boolean hadCR = false;
boolean hadEOL = false;
if (textMapper.isModified()) textMapper.startNestedReplacement(s);
for (int i = 0; i < iMax; i++) {
char c = s.charAt(i);
if (c == '\r') {
hadCR = true;
} else if (c == '\n') {
if (hadCR) {
// previous was CR, need to take preceding chars
if (lastPos < i - 1) textMapper.addOriginalText(lastPos, i - 1);
lastPos = i;
hadCR = false;
hadEOL = true;
}
} else {
if (hadCR) {
if (lastPos < i - 1) textMapper.addOriginalText(lastPos, i + 1);
textMapper.addReplacedText(i - 1, i, BasedSequence.EOL);
lastPos = i;
hadCR = false;
hadEOL = false;
}
}
}
if (!hadCR) {
// // previous was CR, need to take preceding chars
// if (lastPos < iMax - 1) textMapper.addOriginalText(input.subSequence(lastPos, iMax - 1));
// textMapper.addReplacedText(input.subSequence(iMax - 1, iMax), SubSequence.EOL);
//} else {
if (lastPos < iMax) textMapper.addOriginalText(lastPos, iMax);
if (!hadEOL && endWithEOL) textMapper.addReplacedText(iMax - 1, iMax, BasedSequence.EOL);
}
return textMapper.getReplacedSequence();
}
/**
* @param s string to encode
* @return encoded string
*/
@NotNull
public static String percentEncodeUrl(@NotNull CharSequence s) {
return replaceAll(ESCAPE_IN_URI, s, URL_ENCODE_REPLACER);
}
/**
* @param s string to encode
* @param textMapper text mapper to update for the replaced text
* @return encoded string
*/
@NotNull
public static BasedSequence percentEncodeUrl(@NotNull BasedSequence s, @NotNull ReplacedTextMapper textMapper) {
return replaceAll(ESCAPE_IN_URI, s, URL_ENCODE_REPLACER, textMapper);
}
/**
* @param s string to encode
* @return encoded string
*/
@NotNull
public static String percentDecodeUrl(@NotNull CharSequence s) {
return replaceAll(ESCAPE_URI_DECODE, s, URL_DECODE_REPLACER);
}
/**
* @param s string to encode
* @param textMapper text mapper to update for the replaced text
* @return encoded string
*/
@NotNull
public static BasedSequence percentDecodeUrl(@NotNull BasedSequence s, @NotNull ReplacedTextMapper textMapper) {
return replaceAll(ESCAPE_URI_DECODE, s, URL_DECODE_REPLACER, textMapper);
}
/**
* Normalize the link reference id
*
* @param s sequence containing the link reference id
* @param changeCase if true then reference will be converted to lowercase
* @return normalized link reference id
*/
@NotNull
public static String normalizeReference(@NotNull CharSequence s, boolean changeCase) {
if (changeCase) return Escaping.collapseWhitespace(s.toString(), true).toLowerCase();
else return Escaping.collapseWhitespace(s.toString(), true);
}
@Nullable
private static String encode(char c) {
switch (c) {
case '&':
return "&";
case '<':
return "<";
case '>':
return ">";
case '"':
return """;
case '\'':
return "'";
}
return null;
}
private static Random random = new Random(0x2626);
/**
* e-mail obfuscation from pegdown
*
* @param email e-mail url
* @param randomize true to randomize, false for testing
* @return obfuscated e-mail url
*/
@NotNull
public static String obfuscate(@NotNull String email, boolean randomize) {
if (!randomize) random = new Random(0);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < email.length(); i++) {
char c = email.charAt(i);
switch (random.nextInt(5)) {
case 0:
case 1:
sb.append("&#").append((int) c).append(';');
break;
case 2:
case 3:
sb.append("&#x").append(Integer.toHexString(c)).append(';');
break;
case 4:
String encoded = encode(c);
if (encoded != null) sb.append(encoded);
else sb.append(c);
}
}
return sb.toString();
}
/**
* Get a normalized the link reference id from reference characters
* <p>
* Will remove leading ![ or [ and trailing ], collapse multiple whitespaces to a space and optionally convert the id to lowercase.
*
* @param s sequence containing the link reference id
* @param changeCase if true then reference will be converted to lowercase
* @return normalized link reference id
*/
@NotNull
public static String normalizeReferenceChars(@NotNull CharSequence s, boolean changeCase) {
// Strip '[' and ']', then trim and convert to lowercase
if (s.length() > 1) {
int stripEnd = s.charAt(s.length() - 1) == ':' ? 2 : 1;
int stripStart = s.charAt(0) == '!' ? 2 : 1;
return normalizeReference(s.subSequence(stripStart, s.length() - stripEnd), changeCase);
}
return String.valueOf(s);
}
/**
* Collapse regions of multiple white spaces to a single space
*
* @param s sequence to process
* @param trim true if the sequence should also be trimmed
* @return processed sequence
*/
@NotNull
public static String collapseWhitespace(@NotNull CharSequence s, boolean trim) {
StringBuilder sb = new StringBuilder(s.length());
int iMax = s.length();
boolean hadSpace = false;
for (int i = 0; i < iMax; i++) {
char c = s.charAt(i);
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') {
hadSpace = true;
} else {
if (hadSpace && (!trim || sb.length() > 0)) sb.append(' ');
sb.append(c);
hadSpace = false;
}
}
if (hadSpace && !trim) sb.append(' ');
return sb.toString();
}
@NotNull
public static BasedSequence collapseWhitespace(@NotNull BasedSequence s, @NotNull ReplacedTextMapper textMapper) {
return replaceAll(COLLAPSE_WHITESPACE, s, COLLAPSE_WHITESPACE_REPLACER, textMapper);
}
@NotNull
private static String replaceAll(@NotNull Pattern p, @NotNull CharSequence s, @NotNull Replacer replacer) {
Matcher matcher = p.matcher(s);
if (!matcher.find()) {
return String.valueOf(s);
}
StringBuilder sb = new StringBuilder(s.length() + 16);
int lastEnd = 0;
do {
sb.append(s, lastEnd, matcher.start());
replacer.replace(matcher.group(), sb);
lastEnd = matcher.end();
} while (matcher.find());
if (lastEnd != s.length()) {
sb.append(s, lastEnd, s.length());
}
return sb.toString();
}
@NotNull
private static BasedSequence replaceAll(@NotNull Pattern p, @NotNull BasedSequence s, @NotNull Replacer replacer, @NotNull ReplacedTextMapper textMapper) {
return replaceAll(p, s, 0, s.length(), replacer, textMapper);
}
@NotNull
private static BasedSequence replaceAll(@NotNull Pattern p, @NotNull BasedSequence s, int startOffset, int endOffset, @NotNull Replacer replacer, @NotNull ReplacedTextMapper textMapper) {
Matcher matcher = p.matcher(s);
matcher.region(startOffset, endOffset);
matcher.useTransparentBounds(false);
if (textMapper.isModified()) {
textMapper.startNestedReplacement(s);
}
if (!matcher.find()) {
textMapper.addOriginalText(0, s.length());
return s;
}
int lastEnd = 0;
do {
textMapper.addOriginalText(lastEnd, matcher.start());
replacer.replace(s, matcher.start(), matcher.end(), textMapper);
lastEnd = matcher.end();
} while (matcher.find());
if (lastEnd < s.length()) {
textMapper.addOriginalText(lastEnd, s.length());
}
return textMapper.getReplacedSequence();
}
@NotNull
private static BasedSequence replaceAll(@NotNull Pattern p, @NotNull BasedSequence s, @NotNull List<Range> ranges, @NotNull Replacer replacer, @NotNull ReplacedTextMapper textMapper) {
Matcher matcher = p.matcher(s);
matcher.useTransparentBounds(false);
if (textMapper.isModified()) {
textMapper.startNestedReplacement(s);
}
int lastEnd = 0;
for (Range range : ranges) {
int start = Utils.rangeLimit(range.getStart(), lastEnd, s.length());
int end = Utils.rangeLimit(range.getEnd(), start, s.length());
matcher.region(start, end);
while (matcher.find()) {
textMapper.addOriginalText(lastEnd, matcher.start());
replacer.replace(s, matcher.start(), matcher.end(), textMapper);
lastEnd = matcher.end();
}
}
if (lastEnd < s.length()) {
textMapper.addOriginalText(lastEnd, s.length());
}
return textMapper.getReplacedSequence();
}
interface Replacer {
void replace(@NotNull String s, @NotNull StringBuilder sb);
void replace(@NotNull BasedSequence s, int startIndex, int endIndex, @NotNull ReplacedTextMapper replacedTextMapper);
}
}