o
    •çè[¯	 ã                   @   sT   d dl mZ 	 	 dZd dlZd dlZd dlZd dlZG dd„ dƒZG dd„ dƒZdS )é    )Údivisionzfraser@google.com (Neil Fraser)Nc                   @   sD  e Zd ZdZdd„ ZdZdZdZdMd	d
„Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Ze d#¡Ze d$¡Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Z d5d6„ Z!d7d8„ Z"d9d:„ Z#d;d<„ Z$d=d>„ Z%dNd?d@„Z&dAdB„ Z'dCdD„ Z(dEdF„ Z)dGdH„ Z*dIdJ„ Z+dKdL„ Z,dS )OÚdiff_match_patchz_Class containing the diff, match and patch methods.

  Also contains the behaviour settings.
  c                 C   s.   d| _ d| _d| _d| _d| _d| _d| _dS )zxInits a diff_match_patch object with default settings.
    Redefine these in your program to override the defaults.
    ç      ð?é   g      à?iè  é    N)ÚDiff_TimeoutÚDiff_EditCostÚMatch_ThresholdÚMatch_DistanceÚPatch_DeleteThresholdÚPatch_MarginÚMatch_MaxBits©Úself© r   úW/var/www/html/env/lib/python3.10/site-packages/diff_match_patch/diff_match_patch_py2.pyÚ__init__*   s   
zdiff_match_patch.__init__éÿÿÿÿé   r   TNc           	      C   s  |dkr| j dkrtj}nt ¡ | j  }|dks|dkr tdƒ‚||kr.|r,| j|fgS g S |  ||¡}|d|… }||d… }||d… }|  ||¡}|dkrSd}n|| d… }|d| … }|d| … }|  ||||¡}|r|| j|fg|dd…< |r†| 	| j|f¡ |  
|¡ |S )a  Find the differences between two texts.  Simplifies the problem by
      stripping any common prefix or suffix off the texts before diffing.

    Args:
      text1: Old string to be diffed.
      text2: New string to be diffed.
      checklines: Optional speedup flag.  If present and false, then don't run
        a line-level diff first to identify the changed areas.
        Defaults to true, which does a faster, slightly less optimal diff.
      deadline: Optional time when the diff should be complete by.  Used
        internally for recursive calls.  Users should set DiffTimeout instead.

    Returns:
      Array of changes.
    Nr   zNull inputs. (diff_main)Ú )r   ÚsysÚmaxintÚtimeÚ
ValueErrorÚ
DIFF_EQUALÚdiff_commonPrefixÚdiff_commonSuffixÚdiff_computeÚappendÚdiff_cleanupMerge)	r   Útext1Útext2Ú
checklinesÚdeadlineÚcommonlengthÚcommonprefixÚcommonsuffixÚdiffsr   r   r   Ú	diff_mainP   s6   

zdiff_match_patch.diff_mainc                 C   sv  |s| j |fgS |s| j|fgS t|ƒt|ƒkr||}}n||}}| |¡}|dkrf| j |d|… f| j|f| j ||t|ƒ d… fg}t|ƒt|ƒkrd| j|d d f|d< | j|d d f|d< |S t|ƒdkrv| j|f| j |fgS |  ||¡}	|	rŸ|	\}
}}}}|  |
|||¡}|  ||||¡}|| j|fg | S |r´t|ƒdkr´t|ƒdkr´|  |||¡S |  |||¡S )aã  Find the differences between two texts.  Assumes that the texts do not
      have any common prefix or suffix.

    Args:
      text1: Old string to be diffed.
      text2: New string to be diffed.
      checklines: Speedup flag.  If false, then don't run a line-level diff
        first to identify the changed areas.
        If true, then run a faster, slightly less optimal diff.
      deadline: Time when the diff should be complete by.

    Returns:
      Array of changes.
    r   Nr   r   é   éd   )	ÚDIFF_INSERTÚDIFF_DELETEÚlenÚfindr   Údiff_halfMatchr(   Údiff_lineModeÚdiff_bisect)r   r    r!   r"   r#   ÚlongtextÚ	shorttextÚir'   ÚhmÚtext1_aÚtext1_bÚtext2_aÚtext2_bÚ
mid_commonÚdiffs_aÚdiffs_br   r   r   r   Œ   s6   

ÿzdiff_match_patch.diff_computec                 C   sJ  |   ||¡\}}}|  ||d|¡}|  ||¡ |  |¡ | | jdf¡ d}d}d}d}	d}
|t|ƒk rŸ|| d | jkrJ|d7 }|
|| d 7 }
nK|| d | jkr`|d7 }|	|| d 7 }	n5|| d | jkr•|dkr�|dkr�|  |	|
d|¡}|||| | |…< || | t|ƒ }d}d}d}	d}
|d7 }|t|ƒk s4| 	¡  |S )aK  Do a quick line-level diff on both strings, then rediff the parts for
      greater accuracy.
      This speedup can produce non-minimal diffs.

    Args:
      text1: Old string to be diffed.
      text2: New string to be diffed.
      deadline: Time when the diff should be complete by.

    Returns:
      Array of changes.
    Fr   r   r   )
Údiff_linesToCharsr(   Údiff_charsToLinesÚdiff_cleanupSemanticr   r   r-   r+   r,   Úpop)r   r    r!   r#   Ú	linearrayr'   ÚpointerÚcount_deleteÚcount_insertÚtext_deleteÚtext_insertÚsubDiffr   r   r   r0   Ç   s<   
ízdiff_match_patch.diff_lineModec              	   C   sŒ  t |ƒ}t |ƒ}|| d d }|}d| }dg| }	d|	|d < |	dd… }
|| }|d dk}d}d}d}d}t|ƒD �]}}t ¡ |krI �nst| | |d | dƒD ]™}|| }|| ksq||krx|	|d  |	|d  k rx|	|d  }n|	|d  d }|| }||k r¬||k r¬|| || kr¬|d7 }|d7 }||k r¬||k r¬|| || ks”||	|< ||kr¹|d7 }qV||krÂ|d7 }qV|rï|| | }|dkrï||k rï|
| dkrï||
|  }||krï|  |||||¡    S qVt| | |d | dƒD ]½}|| }|| k�s||k�r"|
|d  |
|d  k �r"|
|d  }n|
|d  d }|| }||k �rh||k �rh|| d  || d  k�rh|d7 }|d7 }||k �rh||k �rh|| d  || d  k�sG||
|< ||k�rv|d7 }qý||k�r€|d7 }qý|�sº|| | }|dk�rº||k �rº|	| dk�rº|	| }|| | }|| }||k�rº|  |||||¡    S qýq=| j|f| j|fgS )a}  Find the 'middle snake' of a diff, split the problem in two
      and return the recursively constructed diff.
      See Myers 1986 paper: An O(ND) Difference Algorithm and Its Variations.

    Args:
      text1: Old string to be diffed.
      text2: New string to be diffed.
      deadline: Time at which to bail if not yet complete.

    Returns:
      Array of diff tuples.
    r   r)   r   r   N)r-   Úxranger   Údiff_bisectSplitr,   r+   )r   r    r!   r#   Útext1_lengthÚtext2_lengthÚmax_dÚv_offsetÚv_lengthÚv1Úv2ÚdeltaÚfrontÚk1startÚk1endÚk2startÚk2endÚdÚk1Ú	k1_offsetÚx1Úy1Ú	k2_offsetÚx2Úk2Úy2r   r   r   r1      sŽ   
ý

€ý



"
€ä zdiff_match_patch.diff_bisectc                 C   sX   |d|… }|d|… }||d… }||d… }	|   ||d|¡}
|   ||	d|¡}|
| S )ag  Given the location of the 'middle snake', split the diff in two parts
    and recurse.

    Args:
      text1: Old string to be diffed.
      text2: New string to be diffed.
      x: Index of split point in text1.
      y: Index of split point in text2.
      deadline: Time at which to bail if not yet complete.

    Returns:
      Array of diff tuples.
    NF)r(   )r   r    r!   ÚxÚyr#   Útext1aÚtext2aÚtext1bÚtext2br'   Údiffsbr   r   r   rI   f  s   z!diff_match_patch.diff_bisectSplitc                    sD   g ‰ i ‰ˆ   d¡ ‡ ‡‡fdd„}d‰||ƒ}d‰||ƒ}||ˆ fS )aŸ  Split two texts into an array of strings.  Reduce the texts to a string
    of hashes where each Unicode character represents one line.

    Args:
      text1: First string.
      text2: Second string.

    Returns:
      Three element tuple, containing the encoded text1, the encoded text2 and
      the array of unique strings.  The zeroth element of the array of unique
      strings is intentionally blank.
    r   c                    sÚ   g }d}d}|t | ƒd k rh|  d|¡}|dkrt | ƒd }| ||d … }|ˆv r4| tˆ| ƒ¡ n(t ˆ ƒˆkrD| |d… }t | ƒ}ˆ  |¡ t ˆ ƒd ˆ|< | tt ˆ ƒd ƒ¡ |d }|t | ƒd k sd |¡S )a!  Split a text into an array of strings.  Reduce the texts to a string
      of hashes where each Unicode character represents one line.
      Modifies linearray and linehash through being a closure.

      Args:
        text: String to encode.

      Returns:
        Encoded string.
      r   r   r   Ú
Nr   )r-   r.   r   ÚunichrÚjoin)ÚtextÚcharsÚ	lineStartÚlineEndÚline©Ú	lineArrayÚlineHashÚmaxLinesr   r   Údiff_linesToCharsMunge“  s&   
ð
zBdiff_match_patch.diff_linesToChars.<locals>.diff_linesToCharsMungei@œ  iÿÿ  )r   )r   r    r!   rs   Úchars1Úchars2r   ro   r   r=     s   
%
z"diff_match_patch.diff_linesToCharsc                 C   sX   t t|ƒƒD ]#}g }|| d D ]}| |t|ƒ ¡ q|| d d |¡f||< qdS )z±Rehydrate the text in a diff from a string of line hashes to real lines
    of text.

    Args:
      diffs: Array of diff tuples.
      lineArray: Array of unique strings.
    r   r   r   N)rH   r-   r   Úordri   )r   r'   rp   r4   rj   Úcharr   r   r   r>   ¾  s   üz"diff_match_patch.diff_charsToLinesc                 C   s„   |r|r|d |d krdS d}t t|ƒt|ƒƒ}|}d}||k r@|||… |||… kr2|}|}n|}|| d | }||k s!|S )zÃDetermine the common prefix of two strings.

    Args:
      text1: First string.
      text2: Second string.

    Returns:
      The number of characters common to the start of each string.
    r   r)   ©Úminr-   )r   r    r!   Ú
pointerminÚ
pointermaxÚ
pointermidÚpointerstartr   r   r   r   Ì  s   úz"diff_match_patch.diff_commonPrefixc                 C   s˜   |r|r|d |d krdS d}t t|ƒt|ƒƒ}|}d}||k rJ|| t|ƒ| … || t|ƒ| … kr<|}|}n|}|| d | }||k s!|S )zÁDetermine the common suffix of two strings.

    Args:
      text1: First string.
      text2: Second string.

    Returns:
      The number of characters common to the end of each string.
    r   r   r)   rx   )r   r    r!   rz   r{   r|   Ú
pointerendr   r   r   r   è  s    ÿùz"diff_match_patch.diff_commonSuffixc           
      C   sÌ   t |ƒ}t |ƒ}|dks|dkrdS ||kr|| d… }n
||k r(|d|… }t||ƒ}||kr3|S d}d}	 || d… }| |¡}	|	dkrJ|S ||	7 }|	dks_|| d… |d|… kre|}|d7 }q8)a  Determine if the suffix of one string is the prefix of another.

    Args:
      text1 First string.
      text2 Second string.

    Returns:
      The number of characters common to the end of the first
      string and the start of the second string.
    r   Nr   Tr   )r-   ry   r.   )
r   r    r!   rJ   rK   Útext_lengthÚbestÚlengthÚpatternÚfoundr   r   r   Údiff_commonOverlap  s.   

"øz#diff_match_patch.diff_commonOverlapc                    s  ˆ j dkrdS t|ƒt|ƒkr||}}n||}}t|ƒdk s*t|ƒd t|ƒk r,dS ‡ fdd„}|||t|ƒd d ƒ}|||t|ƒd d ƒ}|sP|sPdS |sU|}n|sZ|}nt|d ƒt|d ƒkri|}n|}t|ƒt|ƒkr{|\}	}
}}}n|\}}}	}
}|	|
|||fS )	aœ  Do the two texts share a substring which is at least half the length of
    the longer text?
    This speedup can produce non-minimal diffs.

    Args:
      text1: First string.
      text2: Second string.

    Returns:
      Five element Array, containing the prefix of text1, the suffix of text1,
      the prefix of text2, the suffix of text2 and the common middle.  Or None
      if there was no match.
    r   Nr   r)   c                    s  | ||t | ƒd  … }d}| |¡}|dkrwˆ  | |d… ||d… ¡}ˆ  | d|… |d|… ¡}t |ƒ|| k rk||| |… |||| …  }| d|| … }| || d… }	|d|| … }
||| d… }| ||d ¡}|dkst |ƒd t | ƒkrˆ||	|
||fS dS )a3  Does a substring of shorttext exist within longtext such that the
      substring is at least half the length of longtext?
      Closure, but does not reference any external variables.

      Args:
        longtext: Longer string.
        shorttext: Shorter string.
        i: Start index of quarter length substring within longtext.

      Returns:
        Five element Array, containing the prefix of longtext, the suffix of
        longtext, the prefix of shorttext, the suffix of shorttext and the
        common middle.  Or None if there was no match.
      r   r   r   Nr   r)   )r-   r.   r   r   )r2   r3   r4   ÚseedÚbest_commonÚjÚprefixLengthÚsuffixLengthÚbest_longtext_aÚbest_longtext_bÚbest_shorttext_aÚbest_shorttext_br   r   r   Údiff_halfMatchIG  s*   
ÿöÿz8diff_match_patch.diff_halfMatch.<locals>.diff_halfMatchIé   r   )r   r-   )r   r    r!   r2   r3   rŽ   Úhm1Úhm2r5   r6   r7   r8   r9   r:   r   r   r   r/   /  s.   

 %zdiff_match_patch.diff_halfMatchc                 C   s"  d}g }d}d}d\}}d\}}	|t |ƒk r²|| d | jkr5| |¡ |d}}|	d}}	|| d }ns|| d | jkrI|t || d ƒ7 }n
|	t || d ƒ7 }	|r¨t |ƒt||ƒkr¨t |ƒt||	ƒkr¨| |d | j|f¡ | j||d d  d f||d d < | ¡  t |ƒr‘| ¡  t |ƒrš|d }nd}d\}}d\}}	d}d}|d7 }|t |ƒk s|r¹|  |¡ |  	|¡ d}|t |ƒk �r�||d  d | jk�rƒ|| d | jk�rƒ||d  d }
|| d }|  
|
|¡}|  
||
¡}||k�r>|t |
ƒd k�s|t |ƒd k�r=| || j|d|… f¡ | j|
dt |
ƒ| … f||d < | j||d… f||d < |d7 }nA|t |
ƒd k�sP|t |ƒd k�r| || j|
d|… f¡ | j|dt |ƒ| … f||d < | j|
|d… f||d < |d7 }|d7 }|d7 }|t |ƒk sÇdS dS )	z�Reduce the number of edits by eliminating semantically trivial
    equalities.

    Args:
      diffs: Array of diff tuples.
    FNr   )r   r   r   r   Tg       @)r-   r   r   r+   ÚmaxÚinsertr,   r@   r   Údiff_cleanupSemanticLosslessr„   )r   r'   ÚchangesÚ
equalitiesÚlastEqualityrB   Úlength_insertions1Úlength_deletions1Úlength_insertions2Úlength_deletions2ÚdeletionÚ	insertionÚoverlap_length1Úoverlap_length2r   r   r   r?   ƒ  sŽ   



ÿÿ
Ý&




ÿÿ
ÿ€ÿãz%diff_match_patch.diff_cleanupSemanticc                    s  ‡ fdd„}d}|t |ƒd k �r
||d  d ˆ jkrü||d  d ˆ jkrü||d  d }|| d }||d  d }ˆ  ||¡}|r`|| d… }|d| … }||d| …  }|| }|}	|}
|}|||ƒ|||ƒ }|r´|r´|d |d kr´||d 7 }|dd… |d  }|dd… }|||ƒ|||ƒ }||kr¨|}|}	|}
|}|r´|r´|d |d ks|||d  d |	krü|	rÏ||d  d |	f||d < n	||d = |d8 }|| d |
f||< |ró||d  d |f||d < n	||d = |d8 }|d7 }|t |ƒd k sdS dS )zõLook for single edits surrounded on both sides by equalities
    which can be shifted sideways to align the edit to a word boundary.
    e.g: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.

    Args:
      diffs: Array of diff tuples.
    c                    sÔ   | r|sdS | d }|d }|  ¡  }|  ¡  }|o| ¡ }|o#| ¡ }|o-|dkp-|dk}|o7|dkp7|dk}	|o?ˆ j | ¡}
|	oGˆ j |¡}|
sL|rNdS |sR|	rTdS |r\|s\|r\dS |s`|rbd	S |sf|rhd
S dS )aK  Given two strings, compute a score representing whether the
      internal boundary falls on logical boundaries.
      Scores range from 6 (best) to 0 (worst).
      Closure, but does not reference any external variables.

      Args:
        one: First string.
        two: Second string.

      Returns:
        The score.
      é   r   r   úrg   é   r   r�   r)   r   )ÚisalnumÚisspaceÚBLANKLINEENDÚsearchÚBLANKLINESTARTÚmatch)ÚoneÚtwoÚchar1Úchar2ÚnonAlphaNumeric1ÚnonAlphaNumeric2Úwhitespace1Úwhitespace2Ú
lineBreak1Ú
lineBreak2Ú
blankLine1Ú
blankLine2r   r   r   Údiff_cleanupSemanticScoreë  s.   

zPdiff_match_patch.diff_cleanupSemanticLossless.<locals>.diff_cleanupSemanticScorer   r   N)r-   r   r   )r   r'   rµ   rB   Ú	equality1ÚeditÚ	equality2ÚcommonOffsetÚcommonStringÚbestEquality1ÚbestEditÚbestEquality2Ú	bestScoreÚscorer   r   r   r”   â  sZ   	2ÿÿõ

Ðz-diff_match_patch.diff_cleanupSemanticLosslessz\n\r?\n$z^\r?\n\r?\nc           
      C   sž  d}g }d}d}d}d}d}d}	|t |ƒk rÄ|| d | jkrGt || d ƒ| jk r>|s.|	r>| |¡ |}|	}|| d }ng }d}d }}	ns|| d | jkrSd}	nd}|rº|r_|r_|r_|	srt |ƒ| jd k rº|| | |	 dkrº| |d | j|f¡ | j||d d  d f||d d < | ¡  d}|r¡|r¡d }}	g }nt |ƒr©| ¡  t |ƒr²|d }nd}d }}	d}|d7 }|t |ƒk s|rÍ|  |¡ dS dS )	z‚Reduce the number of edits by eliminating operationally trivial
    equalities.

    Args:
      diffs: Array of diff tuples.
    FNr   r   Tr)   r�   r   )	r-   r   r   r   r,   r“   r+   r@   r   )
r   r'   r•   r–   r—   rB   Úpre_insÚpre_delÚpost_insÚpost_delr   r   r   Údiff_cleanupEfficiencyU  sb   ÿÿ

	ÿ
Í5ÿz'diff_match_patch.diff_cleanupEfficiencyc                 C   sÂ  |  | jdf¡ d}d}d}d}d}|t|ƒk �r_|| d | jkr4|d7 }||| d 7 }|d7 }�n%|| d | jkrO|d7 }||| d 7 }|d7 }�n
|| d | jk�rY|| dk�r|dkræ|dkræ|  ||¡}|dkr¹|| | d }|dkrœ|| d | jkrœ|| d || d |d|…  f||< n| d| j|d|… f¡ |d7 }||d… }||d… }|  ||¡}|dkræ|| d || d… || d  f||< |d| … }|d| … }g }	t|ƒdkrö|	  | j|f¡ t|ƒdk�r|	  | j|f¡ ||| 8 }|	|||| | …< |t|	ƒd 7 }n3|dk�rM||d  d | jk�rM||d  d ||d  d || d  f||d < ||= n|d7 }d}d}d}d}|t|ƒk s|d d dk�rl| ¡  d}
d}|t|ƒd k �rU||d  d | jk�rH||d  d | jk�rH|| d  	||d  d ¡�ró||d  d dk�rë|| d ||d  d || d dt||d  d ƒ …  f||< ||d  d ||d  d ||d  d  f||d < ||d = d}
nU|| d  
||d  d ¡�rH||d  d ||d  d ||d  d  f||d < || d || d t||d  d ƒd… ||d  d  f||< ||d = d}
|d7 }|t|ƒd k �sy|
�r_|  |¡ dS dS )z²Reorder and merge like edit sections.  Merge equalities.
    Any edit section can move as long as it doesn't cross an equality.

    Args:
      diffs: Array of diff tuples.
    r   r   r   Nr   FT)r   r   r-   r+   r,   r   r“   r   r@   ÚendswithÚ
startswithr   )r   r'   rB   rC   rD   rE   rF   r$   r`   Únew_opsr•   r   r   r   r   œ  s´   
ÿ

ÿ"ÿÌ6 
$ÿÿÿ
 ÿ
"ÿÿ
éÿz"diff_match_patch.diff_cleanupMergec           
      C   sœ   d}d}d}d}t t|ƒƒD ](}|| \}}	|| jkr!|t|	ƒ7 }|| jkr,|t|	ƒ7 }||kr2 n|}|}qt|ƒ|krH|| d | jkrH|S |||  S )a  loc is a location in text1, compute and return the equivalent location
    in text2.  e.g. "The cat" vs "The big cat", 1->1, 5->8

    Args:
      diffs: Array of diff tuples.
      loc: Location within text1.

    Returns:
      Location within text2.
    r   )rH   r-   r+   r,   )
r   r'   Úlocrt   ru   Úlast_chars1Úlast_chars2r`   Úoprj   r   r   r   Údiff_xIndex  s"   

zdiff_match_patch.diff_xIndexc                 C   sŒ   g }|D ]<\}}|  dd¡  dd¡  dd¡  dd¡}|| jkr'| d	| ¡ q|| jkr4| d
| ¡ q|| jkr@| d| ¡ qd |¡S )z‹Convert a diff array into a pretty HTML report.

    Args:
      diffs: Array of diff tuples.

    Returns:
      HTML representation.
    ú&z&amp;ú<z&lt;ú>z&gt;rg   z
&para;<br>z)<ins style="background:#e6ffe6;">%s</ins>z)<del style="background:#ffe6e6;">%s</del>z<span>%s</span>r   )Úreplacer+   r   r,   r   ri   )r   r'   ÚhtmlrË   Údatarj   r   r   r   Údiff_prettyHtml%  s   	ÿ


€
z diff_match_patch.diff_prettyHtmlc                 C   ó0   g }|D ]\}}|| j kr| |¡ qd |¡S )z–Compute and return the source text (all equalities and deletions).

    Args:
      diffs: Array of diff tuples.

    Returns:
      Source text.
    r   )r+   r   ri   ©r   r'   rj   rË   rÒ   r   r   r   Ú
diff_text1:  ó   	

€
zdiff_match_patch.diff_text1c                 C   rÔ   )z¡Compute and return the destination text (all equalities and insertions).

    Args:
      diffs: Array of diff tuples.

    Returns:
      Destination text.
    r   )r,   r   ri   rÕ   r   r   r   Ú
diff_text2I  r×   zdiff_match_patch.diff_text2c                 C   s|   d}d}d}|D ],\}}|| j kr|t|ƒ7 }q|| jkr$|t|ƒ7 }q|| jkr4|t||ƒ7 }d}d}q|t||ƒ7 }|S )zºCompute the Levenshtein distance; the number of inserted, deleted or
    substituted characters.

    Args:
      diffs: Array of diff tuples.

    Returns:
      Number of changes.
    r   )r+   r-   r,   r   r’   )r   r'   ÚlevenshteinÚ
insertionsÚ	deletionsrË   rÒ   r   r   r   Údiff_levenshteinX  s   



€z!diff_match_patch.diff_levenshteinc                 C   s‚   g }|D ]7\}}|| j kr| d¡}| dt |d¡ ¡ q|| jkr-| dt|ƒ ¡ q|| jkr;| dt|ƒ ¡ qd |¡S )aW  Crush the diff into an encoded string which describes the operations
    required to transform text1 into text2.
    E.g. =3	-2	+ing  -> Keep 3 chars, delete 2 chars, insert 'ing'.
    Operations are tab-separated.  Inserted text is escaped using %xx notation.

    Args:
      diffs: Array of diff tuples.

    Returns:
      Delta text.
    úutf-8ú+ú!~*'();/?:@&=+$,# z-%dz=%dú	)	r+   Úencoder   ÚurllibÚquoter,   r-   r   ri   rÕ   r   r   r   Údiff_toDeltar  s   



€
zdiff_match_patch.diff_toDeltac           
   	   C   sD  t |ƒtkr| d¡}g }d}| d¡}|D ]y}|dkrq|dd… }|d dkr:t |¡ d¡}| | j|f¡ q|d d	ksF|d d
krˆzt	|ƒ}W n t
yY   t
d| ƒ‚w |dk rdt
d| ƒ‚|||| … }	||7 }|d d
kr| | j|	f¡ q| | j|	f¡ qt
d|d  ƒ‚|t|ƒkr t
d|t|ƒf ƒ‚|S )a>  Given the original text1, and an encoded string which describes the
    operations required to transform text1 into text2, compute the full diff.

    Args:
      text1: Source string for the diff.
      delta: Delta text.

    Returns:
      Array of diff tuples.

    Raises:
      ValueError: If invalid input.
    Úasciir   rà   r   r   NrÞ   rÝ   ú-ú=z"Invalid number in diff_fromDelta: z#Negative number in diff_fromDelta: z*Invalid diff operation in diff_fromDelta: z9Delta length (%d) does not equal source text length (%d).)ÚtypeÚunicoderá   Úsplitrâ   ÚunquoteÚdecoder   r+   Úintr   r   r,   r-   )
r   r    rQ   r'   rB   ÚtokensÚtokenÚparamÚnrj   r   r   r   Údiff_fromDeltaŠ  sF   

ÿÿ
ÿÿzdiff_match_patch.diff_fromDeltac                 C   sn   |dks|dkrt dƒ‚tdt|t|ƒƒƒ}||krdS |s dS |||t|ƒ … |kr.|S |  |||¡}|S )zëLocate the best instance of 'pattern' in 'text' near 'loc'.

    Args:
      text: The text to search.
      pattern: The pattern to search for.
      loc: The location to search around.

    Returns:
      Best match index or -1.
    NzNull inputs. (match_main)r   r   )r   r’   ry   r-   Úmatch_bitap)r   rj   r‚   rÈ   r¨   r   r   r   Ú
match_mainÂ  s   zdiff_match_patch.match_mainc                    s`  ˆ  ˆ¡}‡ ‡‡fdd„}ˆj}| ˆˆ ¡}|dkr8t|d|ƒ|ƒ}| ˆˆ tˆƒ ¡}|dkr8t|d|ƒ|ƒ}dtˆƒd > }d}tˆƒt|ƒ }	d}
ttˆƒƒD ]Û}d}|	}||k rv||ˆ | ƒ|krh|}n|}	|	| d | }||k s\|}	tdˆ | d ƒ}tˆ | t|ƒƒtˆƒ }dg|d  }d|> d ||d < t||d dƒD ]v}t|ƒ|d kr´d}n
| ||d  d¡}|dkrÑ||d  d> dB |@ ||< n"||d  d> dB |@ |
|d  |
| B d> dB B |
|d  B ||< || |@ �r|||d ƒ}||k�r|}|d }|ˆ k�rtddˆ  | ƒ}q§ nq§||d ˆ ƒ|k�r+ |S |}
qR|S )a	  Locate the best instance of 'pattern' in 'text' near 'loc' using the
    Bitap algorithm.

    Args:
      text: The text to search.
      pattern: The pattern to search for.
      loc: The location to search around.

    Returns:
      Best match index or -1.
    c                    s@   t | ƒtˆƒ }tˆ | ƒ}ˆjs|rdp|S ||t ˆjƒ  S )a  Compute and return the score for a match with e errors and x location.
      Accesses loc and pattern through being a closure.

      Args:
        e: Number of errors in match.
        x: Location of match.

      Returns:
        Overall score for match (0.0 = good, 1.0 = bad).
      r   )Úfloatr-   Úabsr
   )Úer`   ÚaccuracyÚ	proximity©rÈ   r‚   r   r   r   Úmatch_bitapScoreó  s
   z6diff_match_patch.match_bitap.<locals>.match_bitapScorer   r   r   Nr)   )	Úmatch_alphabetr	   r.   ry   Úrfindr-   rH   r’   Úget)r   rj   r‚   rÈ   Úsrû   Úscore_thresholdÚbest_locÚ	matchmaskÚbin_maxÚlast_rdrW   Úbin_minÚbin_midÚstartÚfinishÚrdr‡   Ú	charMatchr¿   r   rú   r   ró   à  sh   
ûÿ
ÿ

€ÿzdiff_match_patch.match_bitapc                 C   sP   i }|D ]}d||< qt t|ƒƒD ]}|||   dt|ƒ| d > O  < q|S )z”Initialise the alphabet for the Bitap algorithm.

    Args:
      pattern: The text to encode.

    Returns:
      Hash of character locations.
    r   r   )rH   r-   )r   r‚   rÿ   rw   r4   r   r   r   rü   J  s   	
&zdiff_match_patch.match_alphabetc                 C   s˜  t |ƒdkrdS ||j|j|j … }d}| |¡| |¡krd| jdks1t |ƒ| j| j | j k rd|| j7 }|td|j| ƒ|j|j | … }| |¡| |¡krd| jdks1t |ƒ| j| j | j k s1|| j7 }|td|j| ƒ|j… }|rƒ| j|fg|j	dd…< ||j|j |j|j | … }|rž|j	 
| j|f¡ | jt |ƒ8  _| jt |ƒ8  _| jt |ƒt |ƒ 7  _| jt |ƒt |ƒ 7  _dS )z¬Increase the context until it is unique,
    but don't let the pattern expand beyond Match_MaxBits.

    Args:
      patch: The patch to grow.
      text: Source text.
    r   N)r-   Ústart2Úlength1r.   rý   r   r   r’   r   r'   r   Ústart1Úlength2)r   Úpatchrj   r‚   ÚpaddingÚprefixÚsuffixr   r   r   Úpatch_addContext\  sB   ÿÿ
ÿüÿÿ
ÿz!diff_match_patch.patch_addContextc                 C   sø  d}d}t |tƒr,t |tƒr,|du r,|}|  ||d¡}t|ƒdkr+|  |¡ |  |¡ n@t |tƒrA|du rA|du rA|}|  |¡}n+t |tƒrTt |tƒrT|du rT|}|}nt |tƒrht |tƒrht |tƒrh|}|}ntdƒ‚|spg S g }t	ƒ }d}d}	|}
|}t
t|ƒƒD ]ã}|| \}}t|jƒdkr�|| jkr�||_|	|_|| jkrÂ|j || ¡ | jt|ƒ7  _|d|	… | ||	d…  }nb|| jkré| jt|ƒ7  _|j || ¡ |d|	… ||	t|ƒ d…  }n;|| jk�r$t|ƒd| j k�r$t|jƒdk�r$t|ƒ|d k�r$|j || ¡ | jt|ƒ7  _| jt|ƒ7  _|| jk�rNt|ƒd| j k�rNt|jƒdk�rN|  ||
¡ | |¡ t	ƒ }|}
|	}|| jk�rZ|t|ƒ7 }|| jk�rf|	t|ƒ7 }	qƒt|jƒdk�rz|  ||
¡ | |¡ |S )a  Compute a list of patches to turn text1 into text2.
    Use diffs if provided, otherwise compute it ourselves.
    There are four ways to call this function, depending on what data is
    available to the caller:
    Method 1:
    a = text1, b = text2
    Method 2:
    a = diffs
    Method 3 (optimal):
    a = text1, b = diffs
    Method 4 (deprecated, use method 3):
    a = text1, b = text2, c = diffs

    Args:
      a: text1 (methods 1,3,4) or Array of diff tuples for text1 to
          text2 (method 2).
      b: text2 (methods 1,4) or Array of diff tuples for text1 to
          text2 (method 3) or undefined (method 2).
      c: Array of diff tuples for text1 to text2 (method 4) or
          undefined (methods 1,2,3).

    Returns:
      Array of Patch objects.
    NTr)   z"Unknown call format to patch_make.r   r   )Ú
isinstanceÚ
basestringr(   r-   r?   rÄ   ÚlistrÖ   r   Ú	patch_objrH   r'   r   r  r  r+   r   r  r,   r  r   r  )r   ÚaÚbÚcr    r'   Úpatchesr  Úchar_count1Úchar_count2Úprepatch_textÚpostpatch_textr`   Ú	diff_typeÚ	diff_textr   r   r   Ú
patch_make…  sŠ   

€ÿ

ÿ

ÿ"
€
zdiff_match_patch.patch_makec                 C   sR   g }|D ]"}t ƒ }|jdd… |_|j|_|j|_|j|_|j|_| |¡ q|S )z¥Given an array of patches, return another array that is identical.

    Args:
      patches: Array of Patch objects.

    Returns:
      Array of Patch objects.
    N)r  r'   r  r  r  r  r   )r   r  ÚpatchesCopyr  Ú	patchCopyr   r   r   Úpatch_deepCopy÷  s   	zdiff_match_patch.patch_deepCopyc                 C   s”  |s|g fS |   |¡}|  |¡}|| | }|  |¡ d}g }|D �]}|j| }|  |j¡}d}	t|ƒ| jkrg|  ||d| j… |¡}
|
dkrf|  ||| j d… |t|ƒ | j ¡}	|	dksd|
|	krfd}
n|  |||¡}
|
dkr€| 	d¡ ||j
|j 8 }q!| 	d¡ |
| }|	dkr˜||
|
t|ƒ … }n	||
|	| j … }||kr¼|d|
… |  |j¡ ||
t|ƒ d…  }q!|  ||d¡}t|ƒ| jkrÝ|  |¡tt|ƒƒ | jkrÝd|d< q!|  |¡ d}|jD ]R\}}|| jkrö|  ||¡}|| jk�r|d|
| … | ||
| d…  }n|| jk�r-|d|
| … ||
|  ||t|ƒ ¡ d…  }|| jk�r9|t|ƒ7 }qçq!|t|ƒt|ƒ … }||fS )a;  Merge a set of patches onto the text.  Return a patched text, as well
    as a list of true/false values indicating which patches were applied.

    Args:
      patches: Array of Patch objects.
      text: Old text.

    Returns:
      Two element Array, containing the new text and an array of boolean values.
    r   r   NFT)r%  Úpatch_addPaddingÚpatch_splitMaxr  rÖ   r'   r-   r   rô   r   r  r  rØ   r(   rÜ   rõ   r   r”   r   rÌ   r+   r,   )r   r  rj   ÚnullPaddingrQ   Úresultsr  Úexpected_locr    Úend_locÚ	start_locr!   r'   Úindex1rË   rÒ   Úindex2r   r   r   Úpatch_apply  sz   




ÿÿ€

ÿÿ


ÿÿ€özdiff_match_patch.patch_applyc           	      C   s"  | j }d}td|d ƒD ]}|t|ƒ7 }q|D ]}| j|7  _| j|7  _q|d }|j}|r:|d d | jkr`| d| j|f¡ | j|8  _| j|8  _| j|7  _| j	|7  _	nL|t
|d d ƒkr¬|t
|d d ƒ }|t
|d d ƒd… |d d  }|d d |f|d< | j|8  _| j|8  _| j|7  _| j	|7  _	|d }|j}|r¾|d d | jkrÖ| | j|f¡ | j|7  _| j	|7  _	|S |t
|d d ƒk�r|t
|d d ƒ }|d d |d|…  }|d d |f|d< | j|7  _| j	|7  _	|S )zôAdd some padding on text start and end so that edges can match
    something.  Intended to be called only from within patch_apply.

    Args:
      patches: Array of Patch objects.

    Returns:
      The padding string added to each side.
    r   r   r   Nr   )r   rH   Úchrr  r  r'   r   r“   r  r  r-   r   )	r   r  ÚpaddingLengthr(  r`   r  r'   ÚextraLengthÚnewTextr   r   r   r&  d  sJ   
$	øz!diff_match_patch.patch_addPaddingc                 C   sd  | j }|dkr	dS tt|ƒƒD �]Ÿ}|| j|krq|| }||= |d8 }|j}|j}d}t|jƒdk�r¯tƒ }d}	|t|ƒ |_|t|ƒ |_|r[t|ƒ |_|_|j 	| j
|f¡ t|jƒdk�rA|j|| j k �rA|jd \}
}|
| jkr”| jt|ƒ7  _|t|ƒ7 }|j 	|j d¡¡ d}	n�|
| jkrÐt|jƒdkrÐ|jd d | j
krÐt|ƒd| krÐ| jt|ƒ7  _|t|ƒ7 }d}	|j 	|
|f¡ |jd= na|d||j | j … }| jt|ƒ7  _|t|ƒ7 }|
| j
k�r| jt|ƒ7  _|t|ƒ7 }nd}	|j 	|
|f¡ ||jd d k�r|jd= n|jd d |jd d t|ƒd… f|jd< t|jƒdk�rA|j|| j k sl|  |j¡}|| j d… }|  |j¡d| j… }|�r›| jt|ƒ7  _| jt|ƒ7  _t|jƒdk�r’|jd d | j
k�r’| j
|jd d | f|jd< n	|j 	| j
|f¡ |	�s¨|d7 }| ||¡ t|jƒdks5qdS )	zÜLook through the patches and break up any which are longer than the
    maximum limit of the match algorithm.
    Intended to be called only from within patch_apply.

    Args:
      patches: Array of Patch objects.
    r   Nr   r   TFr)   r   )r   rH   r-   r  r  r  r'   r  r  r   r   r   r+   r@   r,   rØ   rÖ   r“   )r   r  Ú
patch_sizer`   Úbigpatchr  r  Ú
precontextr  Úemptyr   r!  Úpostcontextr   r   r   r'  ž  sŒ   

ÿ

ÿÞ%&ÿÁ€özdiff_match_patch.patch_splitMaxc                 C   s&   g }|D ]	}|  t|ƒ¡ qd |¡S )z¦Take a list of patches and return a textual representation.

    Args:
      patches: Array of Patch objects.

    Returns:
      Text representation of patches.
    r   )r   Ústrri   )r   r  rj   r  r   r   r   Úpatch_toTextö  s   	
zdiff_match_patch.patch_toTextc                 C   s&  t |ƒtkr| d¡}g }|s|S | d¡}t|ƒdk�rt d|d ¡}|s/td|d  ƒ‚tƒ }| 	|¡ t
| d¡ƒ|_| d¡dkrQ| jd8  _d|_n| d¡d	kr\d|_n| jd8  _t
| d¡ƒ|_t
| d
¡ƒ|_| d¡dkr…| jd8  _d|_n| d¡d	kr�d|_n| jd8  _t
| d¡ƒ|_|d= t|ƒdk�r|d r´|d d }nd}t |d dd… ¡}| d¡}|dkrÔ|j 	| j|f¡ n.|dkrâ|j 	| j|f¡ n |dkrð|j 	| j|f¡ n|dkrõn|dkrúntd||f ƒ‚|d= t|ƒdks©t|ƒdks|S )zîParse a textual representation of patches and return a list of patch
    objects.

    Args:
      textline: Text representation of patches.

    Returns:
      Array of Patch objects.

    Raises:
      ValueError: If invalid input.
    rå   rg   r   z$^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@$zInvalid patch string: r   r)   r   Ú0r�   r   NrÝ   rÞ   ræ   ú ú@zInvalid patch mode: '%s'
%s)rè   ré   rá   rê   r-   Úrer¨   r   r  r   rí   Úgroupr  r  r  r  râ   rë   rì   r'   r+   r,   r   )r   Útextliner  rj   Úmr  Úsignrn   r   r   r   Úpatch_fromText  sd   



çä6zdiff_match_patch.patch_fromText)TN)NN)-Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r,   r+   r   r(   r   r0   r1   rI   r=   r>   r   r   r„   r/   r?   r”   r>  Úcompiler¥   r§   rÄ   r   rÌ   rÓ   rÖ   rØ   rÜ   rä   rò   rô   ró   rü   r  r"  r%  r/  r&  r'  r:  rC  r   r   r   r   r   $   sR    "
<;9f?*T_
p
Gi 8j
)rX:Xr   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )r  z*Class representing one patch operation.
  c                 C   s"   g | _ d| _d| _d| _d| _dS )z-Initializes with an empty list of diffs.
    Nr   )r'   r  r  r  r  r   r   r   r   r   V  s
   
zpatch_obj.__init__c                 C   s(  | j dkrt| jƒd }n| j dkrt| jd ƒ}nt| jd ƒd t| j ƒ }| jdkr5t| jƒd }n| jdkrBt| jd ƒ}nt| jd ƒd t| jƒ }d|d|dg}| jD ]4\}}|tjkri| d¡ n|tj	krt| d	¡ n
|tj
kr~| d
¡ | d¡}| t |d¡d ¡ qZd |¡S )z™Emulate GNU diff's format.
    Header: @@ -382,8 +481,9 @@
    Indices are printed as 1-based, not 0-based.

    Returns:
      The GNU diff string.
    r   z,0r   ú,z@@ -z +z @@
rÞ   ræ   r<  rÝ   rß   rg   r   )r  r9  r  r  r  r'   r   r+   r   r,   r   rá   râ   rã   ri   )r   Úcoords1Úcoords2rj   rË   rÒ   r   r   r   Ú__str___  s*   









zpatch_obj.__str__N)rD  rE  rF  rG  r   rL  r   r   r   r   r  R  s    	r  )	Ú
__future__r   Ú
__author__r>  r   r   râ   r   r  r   r   r   r   Ú<module>   s0                 <