a
    i‚j	  ã                   @   sh   d Z ddlmZ ddlZddlmZ dZdd„ Zeƒ Ze	dkrdd	Z
eee
ƒee
ƒee
ƒee
ƒ fƒ dS )
a£  Run-time calculation of offset into Python string structure

Does a scan to find the digits of pi in a string structure
in order to produce an offset that can be used to produce 
data-pointers from Python strings.

Porting note:
    
    Currently this uses id( str a ) to get the base address
    of the Python string.  Python implementations where id( a )
    is *not* the memory address of the string will not work!
é    )Úprint_functionN)ÚbytesZ31415926535897931c            	         sš   d‰ t } t| ƒ}t tj¡}tdƒD ]J}t || |¡}d}t| ƒD ]\}}|| |krDd} qbqD|r$|‰  qpq$ˆ durŽ‡ fdd„}ˆ |_|S t	dƒ‚dS )a/  Calculates the data-pointer offset for strings
    
    This does a sequential scan for 100 bytes from the id
    of a string to find special data-value stored in the
    string (the digits of PI).  It produces a dataPointer
    function which adds that offset to the id of the 
    passed strings.
    Néd   TFc                    s(   t | tƒstdt| ƒf ƒ‚t| ƒˆ  S )zÞReturn the data-pointer from the array using calculated offset
            
            data -- a Python string
            
            Returns the raw data-pointer to the internal buffer of the passed string
            z5This function can only handle Python strings!  Got %s)Ú
isinstancer   Ú	TypeErrorÚtypeÚid)Údata©ZfinalOffset© úT/home/ghrups/robot-bench/.venv/lib/python3.9/site-packages/OpenGL/arrays/_strings.pyÚdataPointer+   s    
ÿÿz$calculateOffset.<locals>.dataPointerz3Unable to determine dataPointer offset for strings!)
Ú	PI_DIGITSr   ÚctypesZPOINTERZc_charÚrangeÚcastÚ	enumerateÚoffsetÚRuntimeError)	ÚaÚinitialZ
targetTyper   ZvectorZ
allMatchedÚindexÚdigitr   r   r
   r   ÚcalculateOffset   s*    	ÿr   Ú__main__Úthis)Ú__doc__Ú
__future__r   r   ZOpenGL._bytesr   r   r   r   Ú__name__r   Úprintr   r   r   r   r   Ú<module>   s   .