806) -> Optional[int]:
807 """
808 Returns the index of the code string for the given positions.
809
810 Args:
811 newlines_offsets (Sequence[int]): The offset of each newline character found in the code snippet.
812 position (SyntaxPosition): The position to search for.
813
814 Returns:
815 Optional[int]: The index of the code string for this position, or `None`
816 if the given position's line number is out of range (if it's the column that is out of range
817 we silently clamp its value so that it reaches the end of the line)
818 """
819 lines_count =
len(newlines_offsets)
820
821 line_number, column_index = position
822 if line_number > lines_count
or len(newlines_offsets) < (line_number + 1):
823 return None
824 line_index = line_number - 1
825 line_length = newlines_offsets[line_index + 1] - newlines_offsets[line_index] - 1
826
827 column_index = min(line_length, column_index)
828 return newlines_offsets[line_index] + column_index
829
830