/** * 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 */ public static BasedSequence unescapeHtml(BasedSequence s, 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 sequence being changed * @param remove string to remove * @param textMapper replaced text mapper to update for the changed text * * @return un-escaped sequence */ public static BasedSequence removeAll(BasedSequence s, CharSequence remove, ReplacedTextMapper textMapper) { int indexOf = s.indexOf(remove); if (indexOf != -1) { return replaceAll(Pattern.compile("\\Q" + remove + "\\E"), s, REMOVE_REPLACER, textMapper); } else { return s; } }
@SuppressWarnings("WeakerAccess") public static int indexOfValue(final CharSequence value, final CharSequence valueName, final char valueListDelimiter, final char valueNameDelimiter) { if (valueName.length() == 0 || value.length() == 0) return -1; if (valueListDelimiter == NUL) { return value.equals(valueName) ? 0 : -1; } else { int lastPos = 0; final BasedSequence subSeq = SubSequence.of(value); while (lastPos < value.length()) { int pos = subSeq.indexOf(valueName, lastPos); if (pos == -1) break; // see if it is 0 or preceded by a space, or at the end or followed by a space int endPos = pos + valueName.length(); if (pos == 0 || value.charAt(pos - 1) == valueListDelimiter || valueNameDelimiter != NUL && value.charAt(pos - 1) == valueNameDelimiter) { if (endPos >= value.length() || value.charAt(endPos) == valueListDelimiter || valueNameDelimiter != NUL && value.charAt(endPos) == valueNameDelimiter) { return pos; } } lastPos = endPos + 1; } } return -1; }
public static BasedSequence stripIndent(BasedSequence input, CharSequence sourceIndent) { BasedSequence result = input; if (sourceIndent.length() != 0) { // strip out indent to test how segmented input parses ArrayList<BasedSequence> segments = new ArrayList<>(); int lastPos = 0; int length = input.length(); while (lastPos < length) { int pos = input.indexOf(sourceIndent, lastPos); int end = pos == -1 ? length : pos; if (lastPos < end && (pos <= 0 || input.charAt(pos - 1) == '\n')) { segments.add(input.subSequence(lastPos, end)); } lastPos = end + sourceIndent.length(); } result = SegmentedSequence.of(segments); } return result; }
private int getMaxRepeatedChars(final CharSequence text, final char c, int minCount) { BasedSequence chars = BasedSequenceImpl.of(text); int lastPos = 0; while (lastPos < chars.length()) { int pos = chars.indexOf(c, lastPos); if (pos < 0) break; int count = chars.countChars(c, pos); if (minCount <= count) minCount = count + 1; lastPos = pos + count; } return minCount; }
private static void renderReferenceText(BasedSequence text, NodeFormatterContext context, MarkdownWriter markdown) { if (!text.isEmpty()) { BasedSequence valueChars = text; int pos = valueChars.indexOf(':'); String category; String id = null; if (pos == -1) { category = text.toString(); } else { category = valueChars.subSequence(0, pos).toString(); id = valueChars.subSequence(pos + 1).toString(); } String encoded = AttributesNodeFormatter.getEncodedIdAttribute(category, id, context, markdown); markdown.append(encoded); } }
@Override public int indexOfAny(CharSequence s, int fromIndex, int endIndex) { switch (s.length()) { case 0: return fromIndex; case 1: return indexOf(s.charAt(0), fromIndex, endIndex); case 2: return indexOfAny(s.charAt(0), s.charAt(1), fromIndex, endIndex); case 3: return indexOfAny(s.charAt(0), s.charAt(1), s.charAt(2), fromIndex, endIndex); default: BasedSequence sequence = of(s); if (fromIndex < 0) fromIndex = 0; if (endIndex > length()) endIndex = length(); for (int i = fromIndex; i < endIndex; i++) { char c = charAt(i); if (sequence.indexOf(c) != -1) return i; } } return -1; }
@Override public int indexOfAnyNot(CharSequence s, int fromIndex, int endIndex) { switch (s.length()) { case 0: return fromIndex; case 1: return indexOfNot(s.charAt(0), fromIndex, endIndex); case 2: return indexOfAnyNot(s.charAt(0), s.charAt(1), fromIndex, endIndex); case 3: return indexOfAnyNot(s.charAt(0), s.charAt(1), s.charAt(2), fromIndex, endIndex); default: BasedSequence sequence = of(s); if (fromIndex < 0) fromIndex = 0; if (endIndex > length()) endIndex = length(); for (int i = fromIndex; i < endIndex; i++) { char c = charAt(i); if (sequence.indexOf(c) == -1) return i; } } return -1; }
@Override public int lastIndexOfAny(CharSequence s, int startIndex, int fromIndex) { switch (s.length()) { case 0: return startIndex; case 1: return lastIndexOf(s.charAt(0), startIndex, fromIndex); case 2: return lastIndexOfAny(s.charAt(0), s.charAt(1), startIndex, fromIndex); case 3: return lastIndexOfAny(s.charAt(0), s.charAt(1), s.charAt(2), startIndex, fromIndex); default: BasedSequence sequence = of(s); if (startIndex < 0) startIndex = 0; if (fromIndex >= length()) fromIndex = length(); else fromIndex++; for (int i = fromIndex; i-- > startIndex; ) { final char c = charAt(i); if (sequence.indexOf(c) != -1) return i; } } return -1; }
@Override public int lastIndexOfAnyNot(CharSequence s, int startIndex, int fromIndex) { switch (s.length()) { case 0: return startIndex; case 1: return lastIndexOfNot(s.charAt(0), startIndex, fromIndex); case 2: return lastIndexOfAnyNot(s.charAt(0), s.charAt(1), startIndex, fromIndex); case 3: return lastIndexOfAnyNot(s.charAt(0), s.charAt(1), s.charAt(2), startIndex, fromIndex); default: BasedSequence sequence = of(s); if (startIndex < 0) startIndex = 0; if (fromIndex >= length()) fromIndex = length(); else fromIndex++; for (int i = fromIndex; i-- > startIndex; ) { final char c = charAt(i); if (sequence.indexOf(c) == -1) return i; } } return -1; }
public TocBlockBase(BasedSequence chars, BasedSequence styleChars, boolean closingSimToc) { super(chars); openingMarker = chars.subSequence(0, 1); tocKeyword = chars.subSequence(1, 4); if (styleChars != null) { style = styleChars; } int closingPos = chars.indexOf(']', 4); if (closingSimToc && !(closingPos != -1 && closingPos + 1 < chars.length() && chars.charAt(closingPos + 1) == ':')) { throw new IllegalStateException("Invalid TOC block sequence"); } closingMarker = chars.subSequence(closingPos, closingPos + (closingSimToc ? 2 : 1)); }
public TocBlock(BasedSequence chars, BasedSequence styleChars, boolean closingSimToc) { super(chars); openingMarker = chars.subSequence(0, 1); tocKeyword = chars.subSequence(1, 4); if (styleChars != null) { style = styleChars; } int closingPos = chars.indexOf(']', 4); if (closingSimToc && !(closingPos != -1 && closingPos + 1 < chars.length() && chars.charAt(closingPos + 1) == ':')) { throw new IllegalStateException("Invalid TOC block sequence"); } closingMarker = chars.subSequence(closingPos, closingPos + (closingSimToc ? 2 : 1)); }
public SimTocBlock(BasedSequence chars, BasedSequence styleChars, BasedSequence titleChars) { super(chars, styleChars, true); int anchorPos = chars.indexOf('#', closingMarker.getEndOffset() - chars.getStartOffset()); if (anchorPos == -1) { throw new IllegalStateException("Invalid TOC block sequence"); } anchorMarker = chars.subSequence(anchorPos, anchorPos + 1); if (titleChars != null) { if (titleChars.length() < 2) { throw new IllegalStateException("Invalid TOC block title sequence"); } openingTitleMarker = titleChars.subSequence(0, 1); title = titleChars.midSequence(1, -1).nullIfEmpty(); closingTitleMarker = titleChars.endSequence(1); } }
public void setLink(BasedSequence linkChars, boolean allowAnchors, boolean canEscapeAnchor) { // now parse out the # from the link this.link = linkChars; if (!allowAnchors) { this.pageRef = linkChars; } else { int pos = -1; do { pos = linkChars.indexOf('#', pos + 1); } while (pos != -1 && canEscapeAnchor && (pos > 0 && linkChars.charAt(pos - 1) == '\\' && (linkChars.subSequence(0, pos).countTrailing('\\') & 1) == 1)); if (pos < 0) { this.pageRef = linkChars; } else { this.pageRef = linkChars.subSequence(0, pos); this.anchorMarker = linkChars.subSequence(pos, pos + 1); this.anchorRef = linkChars.subSequence(pos + 1); } } }
int pos = valueChars.indexOf(':'); if (pos == -1) { String encodedOriginal = getEncodedOriginalId(attributeNode.getChars().toString(), context);
public void setUrlChars(BasedSequence url) { if (url != null && url != BasedSequence.NULL) { // strip off <> wrapping if (url.startsWith("<") && url.endsWith(">")) { urlOpeningMarker = url.subSequence(0, 1); this.url = url.subSequence(1, url.length() - 1); urlClosingMarker = url.subSequence(url.length() - 1); } else { this.url = url; } // parse out the anchor marker and ref int pos = this.url.indexOf('#'); if (pos < 0) { this.pageRef = this.url; } else { this.pageRef = this.url.subSequence(0, pos); this.anchorMarker = this.url.subSequence(pos, pos + 1); this.anchorRef = this.url.subSequence(pos + 1); } } else { this.urlOpeningMarker = BasedSequence.NULL; this.url = BasedSequence.NULL; this.urlClosingMarker = BasedSequence.NULL; } }
pos = -1; do { pos = linkChars.indexOf('|', pos + 1); } while (pos != -1 && canEscapePipe && (pos > 0 && linkChars.charAt(pos - 1) == '\\' && (linkChars.subSequence(0, pos).countTrailing('\\') & 1) == 1));