private static BasedSequence replaceAll(Pattern p, BasedSequence s, Replacer replacer, ReplacedTextMapper textMapper) { Matcher matcher = p.matcher(s); 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(); }
@Override public void replace(BasedSequence original, int startIndex, int endIndex, 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.of("%25", BasedSequence.NULL)); textMapper.addOriginalText(startIndex + 1, endIndex); } } else { byte[] bytes = s.toString().getBytes(Charset.forName("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.of(sbItem.toString(), BasedSequence.NULL)); } } };
@Override public void replace(BasedSequence original, int startIndex, int endIndex, ReplacedTextMapper textMapper) { String s1 = original.subSequence(startIndex, endIndex).toString(); if (s1.equals("&")) { textMapper.addReplacedText(startIndex, endIndex, PrefixedSubSequence.of("&", BasedSequence.NULL)); } else if (s1.equals("<")) { textMapper.addReplacedText(startIndex, endIndex, PrefixedSubSequence.of("<", BasedSequence.NULL)); } else if (s1.equals(">")) { textMapper.addReplacedText(startIndex, endIndex, PrefixedSubSequence.of(">", BasedSequence.NULL)); } else if (s1.equals("\"")) { textMapper.addReplacedText(startIndex, endIndex, PrefixedSubSequence.of(""", BasedSequence.NULL)); } else { textMapper.addOriginalText(startIndex, endIndex); } } };
if (hadCR) { if (lastPos < i - 1) textMapper.addOriginalText(lastPos, i - 1); lastPos = i; hadCR = false; if (lastPos < i - 1) textMapper.addOriginalText(lastPos, i + 1); textMapper.addReplacedText(i - 1, i, BasedSequence.EOL); lastPos = i; if (lastPos < iMax) textMapper.addOriginalText(lastPos, iMax); if (!hadEOL && endWithEOL) textMapper.addReplacedText(iMax - 1, iMax, BasedSequence.EOL);
private static BasedSequence replaceAll(Pattern p, BasedSequence s, Replacer replacer, ReplacedTextMapper textMapper) { Matcher matcher = p.matcher(s); 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(); }
@Override public void replace(BasedSequence original, int startIndex, int endIndex, 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.of("%25", BasedSequence.NULL)); textMapper.addOriginalText(startIndex + 1, endIndex); } } else { byte[] bytes = s.toString().getBytes(Charset.forName("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.of(sbItem.toString(), BasedSequence.NULL)); } } };
@Override public void replace(BasedSequence original, int startIndex, int endIndex, ReplacedTextMapper textMapper) { String s1 = original.subSequence(startIndex, endIndex).toString(); if (s1.equals("&")) { textMapper.addReplacedText(startIndex, endIndex, PrefixedSubSequence.of("&", BasedSequence.NULL)); } else if (s1.equals("<")) { textMapper.addReplacedText(startIndex, endIndex, PrefixedSubSequence.of("<", BasedSequence.NULL)); } else if (s1.equals(">")) { textMapper.addReplacedText(startIndex, endIndex, PrefixedSubSequence.of(">", BasedSequence.NULL)); } else if (s1.equals("\"")) { textMapper.addReplacedText(startIndex, endIndex, PrefixedSubSequence.of(""", BasedSequence.NULL)); } else { textMapper.addOriginalText(startIndex, endIndex); } } };
if (hadCR) { if (lastPos < i - 1) textMapper.addOriginalText(lastPos, i - 1); lastPos = i; hadCR = false; if (lastPos < i - 1) textMapper.addOriginalText(lastPos, i + 1); textMapper.addReplacedText(i - 1, i, BasedSequence.EOL); lastPos = i; if (lastPos < iMax) textMapper.addOriginalText(lastPos, iMax); if (!hadEOL && endWithEOL) textMapper.addReplacedText(iMax - 1, iMax, BasedSequence.EOL);