Created
October 17, 2015 22:56
-
-
Save Makman2/7519a41dbb7c933e3327 to your computer and use it in GitHub Desktop.
Converts absolute text position to line/column position
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def _absolute_position_to_line_column(string, pos): | |
""" | |
Converts an absolute char-position to a line-column position. | |
:param string: The string where the position is associated to. | |
:param pos: The position to convert (starts from 0). | |
:returns: A pair with (line, column). Line and column start with 1. | |
""" | |
line = string.count("\n", 0, pos) + 1 | |
column = string.rfind("\n", 0, pos) | |
if column == -1: | |
column = 1 | |
else: | |
column = pos - column + 1 | |
return (line, column) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment