int (int offset, boolean greedyRight) { int delta = 0; for (Map.Entry<TextRange, CharSequence> entry : myAffectedFragments.entrySet()) { int lengthAfter = entry.getValue().length(); TextRange range = entry.getKey(); // for offsets inside affected fragments, return either start or end of the updated range if (range.containsOffset(offset)) { return range.getStartOffset() + delta + (greedyRight ? lengthAfter : 0); } if (range.getStartOffset() > offset) { break; } delta += lengthAfter - range.getLength(); } return offset + delta; }