SourceCodePositioner { public int lineNumberFromOffset(int offset) { int search = Arrays.binarySearch(lineOffsets, offset); int lineNumber; if (search >= 0) { lineNumber = search; } else { int insertionPoint = search; insertionPoint += 1; insertionPoint *= -1; lineNumber = insertionPoint - 1; } return lineNumber + 1; } SourceCodePositioner(String sourceCode); int lineNumberFromOffset(int offset); int columnFromOffset(int lineNumber, int offset); int getLastLine(); int getLastLineColumn();  }
@Test public void testLineNumberFromOffset() { SourceCodePositioner positioner = new SourceCodePositioner(SOURCE_CODE); int offset; offset = SOURCE_CODE.indexOf('a'); assertEquals(1, positioner.lineNumberFromOffset(offset)); assertEquals(1, positioner.columnFromOffset(1, offset)); offset = SOURCE_CODE.indexOf('b'); assertEquals(1, positioner.lineNumberFromOffset(offset)); assertEquals(2, positioner.columnFromOffset(1, offset)); offset = SOURCE_CODE.indexOf('e'); assertEquals(2, positioner.lineNumberFromOffset(offset)); assertEquals(2, positioner.columnFromOffset(2, offset)); offset = SOURCE_CODE.indexOf('q'); assertEquals(5, positioner.lineNumberFromOffset(offset)); assertEquals(3, positioner.columnFromOffset(5, offset)); }