/** * Advance the position in the post-translation file. e.g. (1, 3) -> * advanceByTextSize(blah\nblah\nblah) -> (3, 4) NB: slower than specifying directly using * advanceInLine or advanceToNewLine */ public void advanceByTextSize(String text) { TextPosition eofPos = LengthScanner.getLength(text); if (eofPos.getLine() == 1) { advanceInLine(eofPos.getColumn() - 1); } else { advanceToNewLine(eofPos.getLine() - 1, eofPos.getColumn()); } }
/** Record a change to the cumulative offset, beginning at the current position. */ public void recordOffsetChange(String text, int startPos, boolean insert) { TextPosition eofPos = LengthScanner.getLength(text); int lineOffsetChange = -1; int colOffsetChange = -1; if (eofPos.getLine() == 1) { lineOffsetChange = 0; colOffsetChange = eofPos.getColumn() - 1; } else { lineOffsetChange = eofPos.getLine() - 1; colOffsetChange = eofPos.getColumn() - startPos; } if (insert) { recordOffsetChange(-1 * lineOffsetChange, -1 * colOffsetChange); } else { recordOffsetChange(lineOffsetChange, colOffsetChange); } }
TextPosition reverseOffset(TextPosition original) { return new TextPosition(original.getLine() - lineOffset, original.getColumn() - colOffset); }
/** * Advance the position in the post-translation file. e.g. (1, 3) -> advanceInLine(2) -> (1, 5) */ public void advanceInLine(int numCols) { if (numCols < 0) { throw new IllegalArgumentException("Attempted to move backwards in line: " + numCols); } currPos = new TextPosition(currPos.getLine(), currPos.getColumn() + numCols); }
TextPosition forwardOffset(TextPosition original) { return new TextPosition(original.getLine() + lineOffset, original.getColumn() + colOffset); }