a
    lc                     @   sF   d dl Z d dlZd dlmZmZmZ edejZG dd de	Z
dS )    N)DictListTuple^c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )LineNumbersaJ  
  Class to convert between character offsets in a text string, and pairs (line, column) of 1-based
  line and 0-based column numbers, as used by tokens and AST nodes.

  This class expects unicode for input and stores positions in unicode. But it supports
  translating to and from utf8 offsets, which are used by ast parsing.
  c                 C   s0   dd t |D | _|| _t|| _i | _d S )Nc                 S   s   g | ]}| d qS )r   )start).0m r
   R/var/www/html/django/DPS/env/lib/python3.9/site-packages/asttokens/line_numbers.py
<listcomp>        z(LineNumbers.__init__.<locals>.<listcomp>)_line_start_refinditer_line_offsets_textlen	_text_len_utf8_offset_cache)selftextr
   r
   r   __init__   s    
zLineNumbers.__init__c                 C   s   | j |}|du rt|t| jk r,| j| n| j}| j| j|d  | }dd t|D }|t| || j |< |tdt	t|d | S )z`
    Given a 1-based line number and 0-based utf8 column, returns a 0-based unicode column.
    N   c                 S   s$   g | ]\}}| d D ]}|qqS )utf8)encode)r   icbyter
   r
   r   r   /   r   z-LineNumbers.from_utf8_col.<locals>.<listcomp>r   )
r   getr   r   r   r   	enumerateappendmaxmin)r   lineZutf8_columnoffsetsZ
end_offsetZ	line_textr
   r
   r   from_utf8_col%   s    
zLineNumbers.from_utf8_colc                 C   sH   |d8 }|t | jkr| jS |dk r(dS t| j| td| | jS dS )z`
    Converts 1-based line number and 0-based column to 0-based character offset into text.
    r   r   N)r   r   r   r"   r!   )r   r#   columnr
   r
   r   line_to_offset5   s    zLineNumbers.line_to_offsetc                 C   s:   t dt| j|}t| j|d }|d || j|  fS )zo
    Converts 0-based character offset to pair (line, col) of 1-based line and 0-based column
    numbers.
    r   r   )r!   r"   r   bisectbisect_rightr   )r   offset
line_indexr
   r
   r   offset_to_lineB   s    zLineNumbers.offset_to_lineN)__name__
__module____qualname____doc__r   r%   r'   r,   r
   r
   r
   r   r      s
   r   )r(   retypingr   r   r   compileMr   objectr   r
   r
   r
   r   <module>   s   