String CodeDocument::getTextBetween (const Position& start, const Position& end) const { if (end.getPosition() <= start.getPosition()) return {}; auto startLine = start.getLineNumber(); auto endLine = end.getLineNumber(); if (startLine == endLine) { if (auto* line = lines [startLine]) return line->line.substring (start.getIndexInLine(), end.getIndexInLine()); return {}; } MemoryOutputStream mo; mo.preallocate ((size_t) (end.getPosition() - start.getPosition() + 4)); auto maxLine = jmin (lines.size() - 1, endLine); for (int i = jmax (0, startLine); i <= maxLine; ++i) { auto& line = *lines.getUnchecked(i); auto len = line.lineLength; if (i == startLine) { auto index = start.getIndexInLine(); mo << line.line.substring (index, len); } else if (i == endLine) { len = end.getIndexInLine(); mo << line.line.substring (0, len); } else { mo << line.line; } } return mo.toUTF8(); }
String CodeDocument::getTextBetween (const Position& start, const Position& end) const { if (end.getPosition() <= start.getPosition()) return String::empty; const int startLine = start.getLineNumber(); const int endLine = end.getLineNumber(); if (startLine == endLine) { CodeDocumentLine* const line = lines [startLine]; return (line == nullptr) ? String::empty : line->line.substring (start.getIndexInLine(), end.getIndexInLine()); } MemoryOutputStream mo; mo.preallocate (end.getPosition() - start.getPosition() + 4); const int maxLine = jmin (lines.size() - 1, endLine); for (int i = jmax (0, startLine); i <= maxLine; ++i) { const CodeDocumentLine* line = lines.getUnchecked(i); int len = line->lineLength; if (i == startLine) { const int index = start.getIndexInLine(); mo << line->line.substring (index, len); } else if (i == endLine) { len = end.getIndexInLine(); mo << line->line.substring (0, len); } else { mo << line->line; } } return mo.toString(); }