String () { String original = myStringBuilder.toString(); if (myLineToOffsetMapping == null || myLineToOffsetMapping.isEmpty()) { if (myLineMapping != null) { return addOriginalLineNumbers(); } return original; } else { StringBuilder res = new StringBuilder(); String[] srcLines = original.split(myLineSeparator); int currentLineStartOffset = 0; int currentLine = 0; int previousMarkLine = 0; int dumpedLines = 0; ArrayList<Integer> linesWithMarks = new ArrayList<>(myLineToOffsetMapping.keySet()); Collections.sort(linesWithMarks); for (Integer markLine : linesWithMarks) { Integer markOffset = myLineToOffsetMapping.get(markLine); while (currentLine < srcLines.length) { String line = srcLines[currentLine]; int lineEnd = currentLineStartOffset + line.length() + myLineSeparator.length(); if (markOffset <= lineEnd) { int requiredLine = markLine - 1; int linesToAdd = requiredLine - dumpedLines; dumpedLines = requiredLine; appendLines(res, srcLines, previousMarkLine, currentLine, linesToAdd); previousMarkLine = currentLine; break; } currentLineStartOffset = lineEnd; currentLine++; } } if (previousMarkLine < srcLines.length) { appendLines(res, srcLines, previousMarkLine, srcLines.length, srcLines.length - previousMarkLine); } return res.toString(); } }