@Override public BasedSequence append(final CharSequence... others) { if (others.length > 0) { int total = 0; for (CharSequence other : others) total += other.length(); StringBuilder sb = new StringBuilder(length() + total); appendTo(sb); for (CharSequence other : others) { if (other instanceof BasedSequence) ((BasedSequence) other).appendTo(sb); else sb.append(other); } return CharSubSequence.of(sb); } return this; }
@Override public BasedSequence replace(final CharSequence find, final CharSequence replace) { int[] indices = indexOfAll(find); if (indices.length == 0) return this; final int iMax = indices.length; StringBuilder sb = new StringBuilder(length() + (replace.length() - find.length()) * iMax); final BasedSequence basedReplace = SubSequence.of(replace); int i = 0; int lastPos = 0; while (i < iMax) { int pos = indices[i++]; if (lastPos < pos) appendTo(sb, lastPos, pos); lastPos = pos + find.length(); basedReplace.appendTo(sb); } if (lastPos < length()) { appendTo(sb, lastPos, length()); } return CharSubSequence.of(sb); }
@Override public String toString() { int total = 0; StringBuilder sb = new StringBuilder(); BasedSequence last = null; for (BasedSequence s : segments) { if (s.isEmpty()) continue; if (last != null && last.getEndOffset() < s.getStartOffset() && (BasedSequence.WHITESPACE_CHARS.indexOf(last.charAt(last.length() - 1)) == -1) && BasedSequence.WHITESPACE_CHARS.indexOf(s.charAt(0)) == -1 && s.baseSubSequence(last.getEndOffset(), s.getStartOffset()).endsWith(" ") ) { sb.append(' '); } s.appendTo(sb); last = s; } return sb.toString(); } }
@Override public BasedSequence append(final CharSequence... others) { if (others.length > 0) { int total = 0; for (CharSequence other : others) total += other.length(); StringBuilder sb = new StringBuilder(length() + total); appendTo(sb); for (CharSequence other : others) { if (other instanceof BasedSequence) ((BasedSequence) other).appendTo(sb); else sb.append(other); } return CharSubSequence.of(sb); } return this; }
@Override public BasedSequence replace(final CharSequence find, final CharSequence replace) { int[] indices = indexOfAll(find); if (indices.length == 0) return this; final int iMax = indices.length; StringBuilder sb = new StringBuilder(length() + (replace.length() - find.length()) * iMax); final BasedSequence basedReplace = SubSequence.of(replace); int i = 0; int lastPos = 0; while (i < iMax) { int pos = indices[i++]; if (lastPos < pos) appendTo(sb, lastPos, pos); lastPos = pos + find.length(); basedReplace.appendTo(sb); } if (lastPos < length()) { appendTo(sb, lastPos, length()); } return CharSubSequence.of(sb); }
@Override public String toString() { int total = 0; StringBuilder sb = new StringBuilder(); BasedSequence last = null; for (BasedSequence s : segments) { if (s.isEmpty()) continue; if (last != null && last.getEndOffset() < s.getStartOffset() && (BasedSequence.WHITESPACE_CHARS.indexOf(last.charAt(last.length() - 1)) == -1) && BasedSequence.WHITESPACE_CHARS.indexOf(s.charAt(0)) == -1 && s.baseSubSequence(last.getEndOffset(), s.getStartOffset()).endsWith(" ") ) { sb.append(' '); } s.appendTo(sb); last = s; } return sb.toString(); } }