U
    dn5                    @   sH   d 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 )ax  Diff Match and Patch
Copyright 2018 The diff-match-patch Authors.
https://github.com/google/diff-match-patch

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
z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   E/tmp/pip-unpacked-wheel-y8mi38gx/diff_match_patch/diff_match_patch.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s8|dkr@td||kr\|rX| j|fgS g S | ||}|d| }||d }||d }| ||}|dkrd}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maxsizetime
ValueError
DIFF_EQUALdiff_commonPrefixdiff_commonSuffixdiff_computeappenddiff_cleanupMerge)	r   text1text2
checklinesdeadlinecommonlengthcommonprefixZcommonsuffixdiffsr   r   r   	diff_mainN   s6    

zdiff_match_patch.diff_mainc                 C   s~  |s| j |fgS |s | j|fgS t|t|kr<|| }}n
|| }}||}|dkr| j |d| f| j|f| j ||t| d fg}t|t|kr| j|d d f|d< | j|d d f|d< |S t|dkr| j|f| j |fgS | ||}	|	r@|	\}
}}}}| |
|||}| ||||}|| j|fg | S |rpt|dkrpt|dkrp| |||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_commonZdiffs_aZdiffs_br   r   r   r      s8    

"zdiff_match_patch.diff_computec                 C   sH  |  ||\}}}| ||d|}| || | | || jdf d}d}d}d}	d}
|t|k r<|| d | jkr|d7 }|
|| d 7 }
n|| d | jkr|d7 }|	|| d 7 }	np|| d | jkr2|dkr"|dkr"| |	|
d|}|||| | |< || | t| }d}d}d}	d}
|d7 }q\|	  |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"   Z	linearrayr%   pointercount_deletecount_inserttext_deletetext_insertZsubDiffr   r   r   r.      s:    

zdiff_match_patch.diff_lineModec              	   C   sR  t |}t |}|| d d }|}d| }dg| }	d|	|d < |	dd }
|| }|d dk}d}d}d}d}t|D ]}t |kr q>t| | |d | dD ] }|| }|| ks||kr|	|d  |	|d  k r|	|d  }n|	|d  d }|| }||k rD||k rD|| || krD|d7 }|d7 }q
||	|< ||kr`|d7 }q||krt|d7 }q|r|| | }|dkr||k r|
| dkr||
|  }||kr| |||||    S qt| | |d | dD ]N}|| }|| ks(||kr6|
|d  |
|d  k r6|
|d  }n|
|d  d }|| }||k r||k r|| d  || d  kr|d7 }|d7 }qN||
|< ||kr|d7 }n||kr|d7 }nt|s|| | }|dkr||k r|	| dkr|	| }|| | }|| }||kr| |||||    S qqz| 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+   ranger   diff_bisectSplitr*   r)   )r   r   r    r"   text1_lengthtext2_lengthZmax_dZv_offsetZv_lengthZv1Zv2deltaZfrontZk1startZk1endZk2startZk2enddZk1Z	k1_offsetx1y1Z	k2_offsetZx2Zk2y2r   r   r   r/     s    
 *



 0



"
 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"   Ztext1aZtext2aZtext1bZtext2br%   Zdiffsbr   r   r   rC   i  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 r| d|}|dkr<t | d }| ||d  }|krh|t|  nPt  kr| |d }t | } | t  d |< |tt  d  |d }q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   chrjoin)textchars	lineStartlineEndline	lineArrayZlineHashZmaxLinesr   r   diff_linesToCharsMunge  s$    

zBdiff_match_patch.diff_linesToChars.<locals>.diff_linesToCharsMungei*,
 i )r   )r   r   r    rW   chars1chars2r   rU   r   r9     s    
%z"diff_match_patch.diff_linesToCharsc                 C   sX   t t|D ]F}g }|| d D ]}||t|  q || d d|f||< qdS )zRehydrate 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)rB   r+   r   ordrO   )r   r%   rV   r2   rP   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 rz||| ||| krd|}|}n|}|| d | }q:|S )zDetermine 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
pointermidZ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 r|| t||  || t||  krx|}|}n|}|| d | }q:|S )zDetermine 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'   r\   )r   r   r    r^   r_   r`   Z
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 rP|d| }t||}||krf|S d}d}|| d }||}	|	dkr|S ||	7 }|	dks|| d |d| krn|}|d7 }qndS )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   r   )r+   r]   r,   )
r   r   r    rD   rE   Z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 sTt|d t|k rXdS  fdd}|||t|d d }|||t|d d }|s|sdS |s|}n,|s|}n"t|d t|d kr|}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r | |d ||d } | d| |d| }t ||| k r||| | ||||   }| d||  }| || d }	|d||  }
||| d }|||d }q&t |d t | kr||	|
||fS d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   )r0   r1   r2   seedZbest_commonjZprefixLengthZsuffixLengthZbest_longtext_aZbest_longtext_bZbest_shorttext_aZbest_shorttext_br   r   r   diff_halfMatchIL  s0    
z8diff_match_patch.diff_halfMatch.<locals>.diff_halfMatchI   r   )r   r+   )r   r   r    r0   r1   rh   Zhm1Zhm2r3   r4   r5   r6   r7   r8   r   r   r   r-   4  s.    

 ,zdiff_match_patch.diff_halfMatchc                 C   s  d}g }d}d}d\}}d\}}	|t |k rf|| d | jkrl|| |d }}|	d }}	|| d }n|| d | jkr|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 }q |rv| | | 	| 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 }n|t |
d ks|t |d kr||| j|
d| f | j|dt ||  f||d < | j|
|d f||d < |d7 }|d7 }|d7 }qdS )	zReduce 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_cleanupSemanticLosslessre   )r   r%   changes
equalitieslastEqualityr=   Zlength_insertions1Zlength_deletions1Zlength_insertions2Zlength_deletions2ZdeletionZ	insertionZoverlap_length1Z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|}|}	|}
|}q||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 }qdS )zLook 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:| }|oF| }|oZ|dkpZ|dk}|on|dkpn|dk}	|o~ j| }
|	o j|}|
s|rdS |s|	rdS |r|s|rdS |s|rd	S |s|rd
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   rM      r   ri   r'   r   )isalnumisspaceBLANKLINEENDsearchBLANKLINESTARTmatch)ZoneZtwoZchar1Zchar2ZnonAlphaNumeric1ZnonAlphaNumeric2Zwhitespace1Zwhitespace2Z
lineBreak1Z
lineBreak2Z
blankLine1Z
blankLine2r   r   r   diff_cleanupSemanticScore  s.    

zPdiff_match_patch.diff_cleanupSemanticLossless.<locals>.diff_cleanupSemanticScorer   r   N)r+   r   r   )r   r%   ry   r=   Z	equality1ZeditZ	equality2ZcommonOffsetZcommonStringZbestEquality1ZbestEditZbestEquality2Z	bestScorescorer   r   r   rl     sf    	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rt || d | jk r~|s^|	r~|| |}|	}|| d }ng }d}d }}	n|| d | jkrd}	nd}|r|r|r|r|	st || jd k r|| | |	 dkr||d | j|f | j||d d  d f||d d < |  d}|rN|rNd }}	g }n2t |r`|  t |rt|d }nd}d }}	d}|d7 }q |r| | dS )	zReduce the number of edits by eliminating operationally trivial
    equalities.

    Args:
      diffs: Array of diff tuples.
    FNr   r   Tr'   ri   r   )	r+   r   r   r   r*   rk   r)   r<   r   )
r   r%   rm   rn   ro   r=   Zpre_insZpre_delZpost_insZpost_delr   r   r   diff_cleanupEfficiency  sr    

	



z'diff_match_patch.diff_cleanupEfficiencyc                 C   s  | | jdf d}d}d}d}d}|t|k r|| d | jkrf|d7 }||| d 7 }|d7 }q$|| d | jkr|d7 }||| d 7 }|d7 }q$|| d | jkr$|| dkrD|dkr|dkr| ||}|dkrv|| | 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 }nf|dkr||d  d | jkr||d  d ||d  d || d  f||d < ||= n|d7 }d}d}d}d}q$|d d dkr|  d}
d}|t|d k r||d  d | jkr||d  d | jkr|| 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}
n|| d 
||d  d r||d  d ||d  d ||d  d  f||d < || d || d t||d  d d ||d  d  f||< ||d = d}
|d7 }q|
r| | dS )zReorder 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   rk   r   r<   endswith
startswithr   )r   r%   r=   r>   r?   r@   rA   r#   rK   Znew_opsrm   r   r   r   r     s    




 

" 
$
 
"
z"diff_match_patch.diff_cleanupMergec           
      C   s   d}d}d}d}t t|D ]P}|| \}}	|| jkrB|t|	7 }|| jkrX|t|	7 }||krd qn|}|}qt||kr|| d | jkr|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   )rB   r+   r)   r*   )
r   r%   locrX   rY   Zlast_chars1Zlast_chars2rK   oprP   r   r   r   diff_xIndexH  s"    

zdiff_match_patch.diff_xIndexc                 C   s   g }|D ]x\}}| dd dd dd dd}|| jkrN|d	|  q|| jkrh|d
|  q|| jkr|d|  qd|S )zConvert a diff array into a pretty HTML report.

    Args:
      diffs: Array of diff tuples.

    Returns:
      HTML representation.
    &z&amp;<z&lt;>z&gt;rM   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   rO   )r   r%   htmlr   datarP   r   r   r   diff_prettyHtmlh  s(    	   


z diff_match_patch.diff_prettyHtmlc                 C   s0   g }|D ]\}}|| j kr|| qd|S )zCompute and return the source text (all equalities and deletions).

    Args:
      diffs: Array of diff tuples.

    Returns:
      Source text.
    r   )r)   r   rO   r   r%   rP   r   r   r   r   r   
diff_text1  s
    	
zdiff_match_patch.diff_text1c                 C   s0   g }|D ]\}}|| j kr|| qd|S )zCompute and return the destination text (all equalities and insertions).

    Args:
      diffs: Array of diff tuples.

    Returns:
      Destination text.
    r   )r*   r   rO   r   r   r   r   
diff_text2  s
    	
zdiff_match_patch.diff_text2c                 C   s|   d}d}d}|D ]X\}}|| j kr0|t|7 }q|| jkrH|t|7 }q|| jkr|t||7 }d}d}q|t||7 }|S )zCompute 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   rj   )r   r%   ZlevenshteinZ
insertionsZ	deletionsr   r   r   r   r   diff_levenshtein  s    



z!diff_match_patch.diff_levenshteinc                 C   s   g }|D ]p\}}|| j kr>|d}|dtj|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parsequoter*   r+   r   rO   r   r   r   r   diff_toDelta  s    



zdiff_match_patch.diff_toDeltac           
   	   C   s4  g }d}| d}|D ]}|dkr$q|dd }|d dkrZtj|}|| j|f q|d dksr|d dkrzt|}W n  tk
r   td	| Y nX |dk rtd
| ||||  }	||7 }|d dkr|| j|	f n|| j	|	f qtd|d  q|t
|kr0t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.
    r   r   r   r   Nr   -=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).)splitr   r   unquoter   r)   intr   r   r*   r+   )
r   r   rF   r%   r=   tokenstokenparamnrP   r   r   r   diff_fromDelta  s@    


zdiff_match_patch.diff_fromDeltac                 C   sr   |dks|dkrt dtdt|t|}||kr8dS |s@dS |||t|  |kr\|S | |||}|S dS )zLocate 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   rj   r]   r+   match_bitap)r   rP   rc   r~   rx   r   r   r   
match_main  s    zdiff_match_patch.match_mainc                    sd   } fdd}j}| }|dkrpt|d||}| t }|dkrpt|d||}dtd > }d}tt| }	d}
ttD ]}d}|	}||k r|| | |kr|}n|}	|	| d | }q|}	td | d }t | t|t }dg|d  }d|> d ||d < t||d dD ]}t||d krfd}n|||d  d}|dkr||d  d> dB |@ ||< nD||d  d> dB |@ |
|d  |
| B d> dB B |
|d  B ||< || |@ rJ|||d }||krJ|}|d }| kr6tdd  | }n q@qJ||d  |krZ q`|}
q|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	   )erK   ZaccuracyZ	proximityr~   rc   r   r   r   match_bitapScore8  s
    z6diff_match_patch.match_bitap.<locals>.match_bitapScorer   r   r   Nr'   )	match_alphabetr   r,   r]   rfindr+   rB   rj   get)r   rP   rc   r~   sr   Zscore_thresholdZbest_locZ	matchmaskZbin_maxZlast_rdrG   Zbin_minZbin_midstartfinishrdrg   Z	charMatchrz   r   r   r   r   %  sd    





zdiff_match_patch.match_bitapc                 C   sP   i }|D ]}d||< qt t|D ](}|||   dt|| d > O  < q"|S )zInitialise the alphabet for the Bitap algorithm.

    Args:
      pattern: The text to encode.

    Returns:
      Hash of character locations.
    r   r   )rB   r+   )r   rc   r   r[   r2   r   r   r   r     s    	
&zdiff_match_patch.match_alphabetc                 C   sd  t |dkrdS ||j|j|j  }d}||||kr| jdksbt || j| j | j k r|| j7 }|td|j| |j|j |  }q*|| 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 )zIncrease 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   rj   r   r%   r   start1length2)r   patchrP   rc   paddingprefixsuffixr   r   r   patch_addContext  s8    
 

 z!diff_match_patch.patch_addContextc                 C   s  d}d}t |trXt |trX|dkrX|}| ||d}t|dkr| | | | nt |tr|dkr|dkr|}| |}nVt |trt |tr|dkr|}|}n0t |trt |trt |tr|}|}ntd|sg 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  }n|| jkr| jt|7  _|j||  |d|	 ||	t| d  }nv|| jkrRt|d| j krRt|jdkrRt||d krR|j||  | jt|7  _| jt|7  _|| jkrt|d| j krt|jdkr| ||
 || t	 }|}
|	}|| jkr|t|7 }|| jkr|	t|7 }	qt|jdkr | ||
 || |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strr&   r+   r;   r{   listr   r   	patch_objrB   r%   r   r   r   r)   r   r   r*   r   r   r   )r   abcr   r%   patchesr   Zchar_count1Zchar_count2Zprepatch_textZpostpatch_textrK   	diff_type	diff_textr   r   r   
patch_make  s    



 

zdiff_match_patch.patch_makec                 C   sR   g }|D ]D}t  }|jdd |_|j|_|j|_|j|_|j|_|| q|S )zGiven 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   ZpatchesCopyr   Z	patchCopyr   r   r   patch_deepCopyG  s    	zdiff_match_patch.patch_deepCopyc                 C   s  |s|g fS |  |}| |}|| | }| | d}g }|D ]>}|j| }| |j}d}	t|| jkr| ||d| j |}
|
dkr| ||| j d |t| | j }	|	dks|
|	krd}
n| |||}
|
dkr|	d ||j
|j 8 }qB|	d |
| }|	dkr4||
|
t|  }n||
|	| j  }||kr~|d|
 | |j ||
t| d  }qB| ||d}t|| jkr| |tt| | jkrd|d< qB| | d}|jD ]\}}|| jkr| ||}|| jkr*|d|
|  | ||
| d  }n<|| jkrf|d|
|  ||
| ||t|  d  }|| jkr|t|7 }qqB|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
   rl   r   r   r)   r*   )r   r   rP   nullPaddingrF   resultsr   Zexpected_locr   Zend_locZ	start_locr    r%   Zindex1r   r   index2r   r   r   patch_apply\  s    




  








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}|rt|d d | jkr|d| j|f | j|8  _| j|8  _| j|7  _| j	|7  _	n|t
|d d krZ|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  _	nr|t
|d d kr"|t
|d d  }|d d |d|  }|d d |f|d< | j|7  _| j	|7  _	|S )zAdd 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   rB   rN   r   r   r%   r   rk   r   r   r+   r   )	r   r   ZpaddingLengthr   rK   r   r%   ZextraLengthZnewTextr   r   r   r     sH    
$z!diff_match_patch.patch_addPaddingc                 C   s@  | j }|dkrdS tt|D ]}|| j|kr4q|| }||= |d8 }|j}|j}d}t|jdkrt }d}	|t| |_|t| |_|rt| |_|_|j	| j
|f t|jdkrl|j|| j k rl|jd \}
}|
| jkr(| jt|7  _|t|7 }|j	|jd d}	q|
| jkrt|jdkr|jd d | j
krt|d| kr| jt|7  _|t|7 }d}	|j	|
|f |jd= q|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= q|jd d |jd d t|d f|jd< q| |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 |	sZ|d7 }||| qZqdS )	zLook 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   rB   r+   r   r   r   r%   r   r   r   r   r   r)   r<   r*   r   r   rk   )r   r   Z
patch_sizerK   Zbigpatchr   r   Z
precontextr   emptyr   r   Zpostcontextr   r   r   r     s    

&zdiff_match_patch.patch_splitMaxc                 C   s&   g }|D ]}| t| qd|S )zTake a list of patches and return a textual representation.

    Args:
      patches: Array of Patch objects.

    Returns:
      Text representation of patches.
    r   )r   r   rO   )r   r   rP   r   r   r   r   patch_toTexta  s    	zdiff_match_patch.patch_toTextc                 C   s  g }|s|S | d}t|dkr td|d }|sHtd|d  t }|| t|d|_	|ddkr| j	d8  _	d|_
n4|ddkrd|_
n| j	d8  _	t|d|_
t|d	|_|d
dkr| jd8  _d|_n6|d
dkrd|_n| jd8  _t|d
|_|d= t|dkr|d rT|d d }nd}tj|d dd }|dkr|j| j|f nf|dkr|j| j|f nH|dkr|j| j|f n*|dkrqn|dkrntd||f |d= q0q|S )zParse 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.
    rM   r   z$^@@ -(\d+),?(\d*) \+(\d+),?(\d*) @@$zInvalid patch string: r   r'   r   0ri   r   Nr   r    @zInvalid patch mode: '%s'
%s)r   r+   rerx   r   r   r   r   groupr   r   r   r   r   r   r   r%   r)   r*   r   )r   Ztextliner   rP   mr   signrT   r   r   r   patch_fromTexto  sZ    







zdiff_match_patch.patch_fromText)TN)NN)-__name__
__module____qualname____doc__r   r*   r)   r   r&   r   r.   r/   rC   r9   r:   r   r   re   r-   r;   rl   r   compileru   rw   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   "   sP   "
<>9h?*[yt

N{ 6m,
wi:b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     s
    zpatch_obj.__init__c                 C   s*  | j dkrt| jd }n6| j dkr4t| jd }nt| jd d t| j  }| jdkrjt| jd }n6| jdkrt| jd }nt| jd d t| j }d|d|dg}| jD ]j\}}|tjkr|d n*|tj	kr|d	 n|tj
kr|d
 |d}|tj|dd  qd|S )zEmulate 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   rM   r   )r   r   r   r   r   r%   r   r)   r   r*   r   r   r   r   r   rO   )r   Zcoords1Zcoords2rP   r   r   r   r   r   __str__  s*    








zpatch_obj.__str__N)r   r   r   r   r   r   r   r   r   r   r     s   	r   )	r   
__author__r   r   r   urllib.parser   r   r   r   r   r   r   <module>   s,                  %