int lineNumber = 1;
int lineStart = 0;
if (!fCachedMatches.isEmpty()) {
// match on same line as last?
FileMatch last = (FileMatch) fCachedMatches.get(fCachedMatches.size() - 1);
LineElement lineElement = last.getLineElement();
if (lineElement.contains(offset)) {
return lineElement;
}
// start with the offset and line information from the last match
lineStart = lineElement.getOffset() + lineElement.getLength();
lineNumber = lineElement.getLine() + 1;
}
if (offset < lineStart) {
return null; // offset before the last line
}
int i = lineStart;
int contentLength = matchRequestor.getFileContentLength();
while (i < contentLength) {
char ch = matchRequestor.getFileContentChar(i++);
if (ch == '\n' || ch == '\r') {
if (ch == '\r' && i < contentLength && matchRequestor.getFileContentChar(i) == '\n') {
i++;
}
if (offset < i) {
String lineContent = getContents(matchRequestor, lineStart, i); // include line delimiter
return new LineElement(matchRequestor.getFile(), lineNumber, lineStart, lineContent);
}
lineNumber++;
lineStart = i;
}
}
if (offset < i) {
String lineContent = getContents(matchRequestor, lineStart, i); // until end of file
return new LineElement(matchRequestor.getFile(), lineNumber, lineStart, lineContent);
}
return null; // offset outside of range
}