public static BasedSequence of(CharSequence charSequence, int start) { if (charSequence instanceof BasedSequence) return ((BasedSequence) charSequence).subSequence(start); else if (charSequence instanceof String) return CharSubSequence.of(charSequence, start); else return SubSequence.of(charSequence, start); }
@Override public BasedSequence spliceAtEnd(BasedSequence other) { if (other.isEmpty()) { return this; } else if (isEmpty()) { return other; } assert isContinuedBy(other) : "sequence[" + getStartOffset() + ", " + getEndOffset() + "] is not continued by other[" + other.getStartOffset() + ", " + other.getEndOffset() + "]"; return baseSubSequence(getStartOffset(), other.getEndOffset()); }
@Override public BasedSequence prefixOf(final BasedSequence other) { if (getBase() != other.getBase()) return SubSequence.NULL; else if (other.getStartOffset() <= getStartOffset()) return subSequence(0, 0); else if (other.getStartOffset() >= getEndOffset()) return this; else return this.baseSubSequence(getStartOffset(), other.getStartOffset()); }
@Override public boolean containsAllOf(BasedSequence other) { return getBase() == other.getBase() && other.getStartOffset() >= getStartOffset() && other.getEndOffset() <= getEndOffset(); }
public void addOriginalText(int startIndex, int endIndex) { if (isFinalized()) throw new IllegalStateException("Cannot modify finalized ReplacedTextMapper"); if (startIndex < endIndex) { BasedSequence originalSegment = original.subSequence(startIndex, endIndex); regions.add(new ReplacedTextRegion(originalSegment.getSourceRange(), new Range(startIndex, endIndex), new Range(replacedLength, replacedLength + originalSegment.length()))); replacedLength += originalSegment.length(); replacedSegments.add(originalSegment); } }
@Override public boolean isContinuedBy(BasedSequence other) { return other.length() > 0 && length() > 0 && other.getBase() == getBase() && other.getStartOffset() == getEndOffset(); }
@Override public boolean isContinuationOf(BasedSequence other) { return other.length() > 0 && length() > 0 && other.getBase() == getBase() && other.getEndOffset() == getStartOffset(); }
@Override public BasedSequence ifNullEmptyAfter(BasedSequence other) { return isNull() ? other.subSequence(other.length(), other.length()) : this; }
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; }
@Override public BasedSequence suffixOf(final BasedSequence other) { if (getBase() != other.getBase()) return SubSequence.NULL; else if (other.getEndOffset() >= getEndOffset()) return subSequence(length(), length()); else if (other.getEndOffset() <= getStartOffset()) return this; else return this.baseSubSequence(other.getEndOffset(), getEndOffset()); }
@Override public void replace(BasedSequence original, int startIndex, int endIndex, ReplacedTextMapper textMapper) { textMapper.addReplacedText(startIndex, endIndex, original.subSequence(startIndex, startIndex + 1)); } };
@Override public BasedSequence subSequence(int start, int end) { final BasedSequence baseSequence = base.subSequence(start, end); return baseSequence == base ? this : new MappedSequence(mapper, baseSequence); }
@Override public BasedSequence ifNullEmptyBefore(BasedSequence other) { return isNull() ? other.subSequence(0, 0) : this; }
public static BasedSequence of(CharSequence charSequence) { if (charSequence instanceof BasedSequence) return ((BasedSequence) charSequence); else if (charSequence instanceof String) return CharSubSequence.of(charSequence); else if (charSequence != null) return SubSequence.of(charSequence); return BasedSequence.NULL; }
@Override public SubSequence subSequence(final Range range) { return subSequence(range.getStart(), range.getEnd()); }
@Override public Range getSourceRange() { return new Range(getStartOffset(), getEndOffset()); }
public void addReplacedText(int startIndex, int endIndex, BasedSequence replacedSequence) { if (isFinalized()) throw new IllegalStateException("Cannot modify finalized ReplacedTextMapper"); regions.add(new ReplacedTextRegion(original.subSequence(startIndex, endIndex).getSourceRange(), new Range(startIndex, endIndex), new Range(replacedLength, replacedLength + replacedSequence.length()))); replacedLength += replacedSequence.length(); replacedSegments.add(replacedSequence); }
public static BasedSequence of(CharSequence charSequence, int start, int end) { if (charSequence instanceof BasedSequence) return ((BasedSequence) charSequence).subSequence(start, end); else if (charSequence instanceof String) return CharSubSequence.of(charSequence, start, end); else return SubSequence.of(charSequence, start, end); } }
@Override public void replace(BasedSequence original, int startIndex, int endIndex, ReplacedTextMapper textMapper) { textMapper.addReplacedText(startIndex, endIndex, original.subSequence(endIndex, endIndex)); } };
@Override public BasedSequence baseSubSequence(final int start, final int end) { final BasedSequence baseSequence = base.subSequence(start, end); return baseSequence == base ? this : new MappedSequence(mapper, baseSequence); }