This is technically still `O(source_string.length)`, although it will certainly have a much smaller constant than iterating through the whole string. It's worth investigating making this `O(1)` by precomputing an array that maps source positions to the position of the preceding newline. Then you could just return `newline_positions[index]` here and be done with it.