Skip to content

Instantly share code, notes, and snippets.

@Makman2
Created October 17, 2015 22:56
Show Gist options
  • Save Makman2/7519a41dbb7c933e3327 to your computer and use it in GitHub Desktop.
Save Makman2/7519a41dbb7c933e3327 to your computer and use it in GitHub Desktop.
Converts absolute text position to line/column position
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