int GetLineNumberForIndex (string fileContents, int index)
{
int line = 1;
char c;
bool foundCR = false;
for (int pos = 0; pos < index; pos++) {
c = fileContents [pos];
if (c == '\n' || foundCR) {
line++;
foundCR = false;
}
foundCR = (c == '\r');
}
return line;
}