<s> """<STR_LIT>""" <EOL> """<STR_LIT>""" <EOL> __author__ = '<STR_LIT>' <EOL> import math <EOL> import re <EOL> import sys <EOL> import time <EOL> import urllib . parse <EOL> class diff_match_patch : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . Diff_Timeout = <NUM_LIT:1.0> <EOL> self . Diff_EditCost = <NUM_LIT:4> <EOL> self . Match_Threshold = <NUM_LIT:0.5> <EOL> self . Match_Distance = <NUM_LIT:1000> <EOL> self . Patch_DeleteThreshold = <NUM_LIT:0.5> <EOL> self . Patch_Margin = <NUM_LIT:4> <EOL> self . Match_MaxBits = <NUM_LIT:32> <EOL> DIFF_DELETE = - <NUM_LIT:1> <EOL> DIFF_INSERT = <NUM_LIT:1> <EOL> DIFF_EQUAL = <NUM_LIT:0> <EOL> def diff_main ( self , text1 , text2 , checklines = True , deadline = None ) : <EOL> """<STR_LIT>""" <EOL> if deadline == None : <EOL> if self . Diff_Timeout <= <NUM_LIT:0> : <EOL> deadline = sys . maxsize <EOL> else : <EOL> deadline = time . time ( ) + self . Diff_Timeout <EOL> if text1 == None or text2 == None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if text1 == text2 : <EOL> if text1 : <EOL> return [ ( self . DIFF_EQUAL , text1 ) ] <EOL> return [ ] <EOL> commonlength = self . diff_commonPrefix ( text1 , text2 ) <EOL> commonprefix = text1 [ : commonlength ] <EOL> text1 = text1 [ commonlength : ] <EOL> text2 = text2 [ commonlength : ] <EOL> commonlength = self . diff_commonSuffix ( text1 , text2 ) <EOL> if commonlength == <NUM_LIT:0> : <EOL> commonsuffix = '<STR_LIT>' <EOL> else : <EOL> commonsuffix = text1 [ - commonlength : ] <EOL> text1 = text1 [ : - commonlength ] <EOL> text2 = text2 [ : - commonlength ] <EOL> diffs = self . diff_compute ( text1 , text2 , checklines , deadline ) <EOL> if commonprefix : <EOL> diffs [ : <NUM_LIT:0> ] = [ ( self . DIFF_EQUAL , commonprefix ) ] <EOL> if commonsuffix : <EOL> diffs . append ( ( self . DIFF_EQUAL , commonsuffix ) ) <EOL> self . diff_cleanupMerge ( diffs ) <EOL> return diffs <EOL> def diff_compute ( self , text1 , text2 , checklines , deadline ) : <EOL> """<STR_LIT>""" <EOL> if not text1 : <EOL> return [ ( self . DIFF_INSERT , text2 ) ] <EOL> if not text2 : <EOL> return [ ( self . DIFF_DELETE , text1 ) ] <EOL> if len ( text1 ) > len ( text2 ) : <EOL> ( longtext , shorttext ) = ( text1 , text2 ) <EOL> else : <EOL> ( shorttext , longtext ) = ( text1 , text2 ) <EOL> i = longtext . find ( shorttext ) <EOL> if i != - <NUM_LIT:1> : <EOL> diffs = [ ( self . DIFF_INSERT , longtext [ : i ] ) , ( self . DIFF_EQUAL , shorttext ) , <EOL> ( self . DIFF_INSERT , longtext [ i + len ( shorttext ) : ] ) ] <EOL> if len ( text1 ) > len ( text2 ) : <EOL> diffs [ <NUM_LIT:0> ] = ( self . DIFF_DELETE , diffs [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) <EOL> diffs [ <NUM_LIT:2> ] = ( self . DIFF_DELETE , diffs [ <NUM_LIT:2> ] [ <NUM_LIT:1> ] ) <EOL> return diffs <EOL> if len ( shorttext ) == <NUM_LIT:1> : <EOL> return [ ( self . DIFF_DELETE , text1 ) , ( self . DIFF_INSERT , text2 ) ] <EOL> hm = self . diff_halfMatch ( text1 , text2 ) <EOL> if hm : <EOL> ( text1_a , text1_b , text2_a , text2_b , mid_common ) = hm <EOL> diffs_a = self . diff_main ( text1_a , text2_a , checklines , deadline ) <EOL> diffs_b = self . diff_main ( text1_b , text2_b , checklines , deadline ) <EOL> return diffs_a + [ ( self . DIFF_EQUAL , mid_common ) ] + diffs_b <EOL> if checklines and len ( text1 ) > <NUM_LIT:100> and len ( text2 ) > <NUM_LIT:100> : <EOL> return self . diff_lineMode ( text1 , text2 , deadline ) <EOL> return self . diff_bisect ( text1 , text2 , deadline ) <EOL> def diff_lineMode ( self , text1 , text2 , deadline ) : <EOL> """<STR_LIT>""" <EOL> ( text1 , text2 , linearray ) = self . diff_linesToChars ( text1 , text2 ) <EOL> diffs = self . diff_main ( text1 , text2 , False , deadline ) <EOL> self . diff_charsToLines ( diffs , linearray ) <EOL> self . diff_cleanupSemantic ( diffs ) <EOL> diffs . append ( ( self . DIFF_EQUAL , '<STR_LIT>' ) ) <EOL> pointer = <NUM_LIT:0> <EOL> count_delete = <NUM_LIT:0> <EOL> count_insert = <NUM_LIT:0> <EOL> text_delete = '<STR_LIT>' <EOL> text_insert = '<STR_LIT>' <EOL> while pointer < len ( diffs ) : <EOL> if diffs [ pointer ] [ <NUM_LIT:0> ] == self . DIFF_INSERT : <EOL> count_insert += <NUM_LIT:1> <EOL> text_insert += diffs [ pointer ] [ <NUM_LIT:1> ] <EOL> elif diffs [ pointer ] [ <NUM_LIT:0> ] == self . DIFF_DELETE : <EOL> count_delete += <NUM_LIT:1> <EOL> text_delete += diffs [ pointer ] [ <NUM_LIT:1> ] <EOL> elif diffs [ pointer ] [ <NUM_LIT:0> ] == self . DIFF_EQUAL : <EOL> if count_delete >= <NUM_LIT:1> and count_insert >= <NUM_LIT:1> : <EOL> a = self . diff_main ( text_delete , text_insert , False , deadline ) <EOL> diffs [ pointer - count_delete - count_insert : pointer ] = a <EOL> pointer = pointer - count_delete - count_insert + len ( a ) <EOL> count_insert = <NUM_LIT:0> <EOL> count_delete = <NUM_LIT:0> <EOL> text_delete = '<STR_LIT>' <EOL> text_insert = '<STR_LIT>' <EOL> pointer += <NUM_LIT:1> <EOL> diffs . pop ( ) <EOL> return diffs <EOL> def diff_bisect ( self , text1 , text2 , deadline ) : <EOL> """<STR_LIT>""" <EOL> text1_length = len ( text1 ) <EOL> text2_length = len ( text2 ) <EOL> max_d = ( text1_length + text2_length + <NUM_LIT:1> ) // <NUM_LIT:2> <EOL> v_offset = max_d <EOL> v_length = <NUM_LIT:2> * max_d <EOL> v1 = [ - <NUM_LIT:1> ] * v_length <EOL> v1 [ v_offset + <NUM_LIT:1> ] = <NUM_LIT:0> <EOL> v2 = v1 [ : ] <EOL> delta = text1_length - text2_length <EOL> front = ( delta % <NUM_LIT:2> != <NUM_LIT:0> ) <EOL> k1start = <NUM_LIT:0> <EOL> k1end = <NUM_LIT:0> <EOL> k2start = <NUM_LIT:0> <EOL> k2end = <NUM_LIT:0> <EOL> for d in range ( max_d ) : <EOL> if time . time ( ) > deadline : <EOL> break <EOL> for k1 in range ( - d + k1start , d + <NUM_LIT:1> - k1end , <NUM_LIT:2> ) : <EOL> k1_offset = v_offset + k1 <EOL> if k1 == - d or ( k1 != d and <EOL> v1 [ k1_offset - <NUM_LIT:1> ] < v1 [ k1_offset + <NUM_LIT:1> ] ) : <EOL> x1 = v1 [ k1_offset + <NUM_LIT:1> ] <EOL> else : <EOL> x1 = v1 [ k1_offset - <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> y1 = x1 - k1 <EOL> while ( x1 < text1_length and y1 < text2_length and <EOL> text1 [ x1 ] == text2 [ y1 ] ) : <EOL> x1 += <NUM_LIT:1> <EOL> y1 += <NUM_LIT:1> <EOL> v1 [ k1_offset ] = x1 <EOL> if x1 > text1_length : <EOL> k1end += <NUM_LIT:2> <EOL> elif y1 > text2_length : <EOL> k1start += <NUM_LIT:2> <EOL> elif front : <EOL> k2_offset = v_offset + delta - k1 <EOL> if k2_offset >= <NUM_LIT:0> and k2_offset < v_length and v2 [ k2_offset ] != - <NUM_LIT:1> : <EOL> x2 = text1_length - v2 [ k2_offset ] <EOL> if x1 >= x2 : <EOL> return self . diff_bisectSplit ( text1 , text2 , x1 , y1 , deadline ) <EOL> for k2 in range ( - d + k2start , d + <NUM_LIT:1> - k2end , <NUM_LIT:2> ) : <EOL> k2_offset = v_offset + k2 <EOL> if k2 == - d or ( k2 != d and <EOL> v2 [ k2_offset - <NUM_LIT:1> ] < v2 [ k2_offset + <NUM_LIT:1> ] ) : <EOL> x2 = v2 [ k2_offset + <NUM_LIT:1> ] <EOL> else : <EOL> x2 = v2 [ k2_offset - <NUM_LIT:1> ] + <NUM_LIT:1> <EOL> y2 = x2 - k2 <EOL> while ( x2 < text1_length and y2 < text2_length and <EOL> text1 [ - x2 - <NUM_LIT:1> ] == text2 [ - y2 - <NUM_LIT:1> ] ) : <EOL> x2 += <NUM_LIT:1> <EOL> y2 += <NUM_LIT:1> <EOL> v2 [ k2_offset ] = x2 <EOL> if x2 > text1_length : <EOL> k2end += <NUM_LIT:2> <EOL> elif y2 > text2_length : <EOL> k2start += <NUM_LIT:2> <EOL> elif not front : <EOL> k1_offset = v_offset + delta - k2 <EOL> if k1_offset >= <NUM_LIT:0> and k1_offset < v_length and v1 [ k1_offset ] != - <NUM_LIT:1> : <EOL> x1 = v1 [ k1_offset ] <EOL> y1 = v_offset + x1 - k1_offset <EOL> x2 = text1_length - x2 <EOL> if x1 >= x2 : <EOL> return self . diff_bisectSplit ( text1 , text2 , x1 , y1 , deadline ) <EOL> return [ ( self . DIFF_DELETE , text1 ) , ( self . DIFF_INSERT , text2 ) ] <EOL> def diff_bisectSplit ( self , text1 , text2 , x , y , deadline ) : <EOL> """<STR_LIT>""" <EOL> text1a = text1 [ : x ] <EOL> text2a = text2 [ : y ] <EOL> text1b = text1 [ x : ] <EOL> text2b = text2 [ y : ] <EOL> diffs = self . diff_main ( text1a , text2a , False , deadline ) <EOL> diffsb = self . diff_main ( text1b , text2b , False , deadline ) <EOL> return diffs + diffsb <EOL> def diff_linesToChars ( self , text1 , text2 ) : <EOL> """<STR_LIT>""" <EOL> lineArray = [ ] <EOL> lineHash = { } <EOL> lineArray . append ( '<STR_LIT>' ) <EOL> def diff_linesToCharsMunge ( text ) : <EOL> """<STR_LIT>""" <EOL> chars = [ ] <EOL> lineStart = <NUM_LIT:0> <EOL> lineEnd = - <NUM_LIT:1> <EOL> while lineEnd < len ( text ) - <NUM_LIT:1> : <EOL> lineEnd = text . find ( '<STR_LIT:\n>' , lineStart ) <EOL> if lineEnd == - <NUM_LIT:1> : <EOL> lineEnd = len ( text ) - <NUM_LIT:1> <EOL> line = text [ lineStart : lineEnd + <NUM_LIT:1> ] <EOL> lineStart = lineEnd + <NUM_LIT:1> <EOL> if line in lineHash : <EOL> chars . append ( chr ( lineHash [ line ] ) ) <EOL> else : <EOL> lineArray . append ( line ) <EOL> lineHash [ line ] = len ( lineArray ) - <NUM_LIT:1> <EOL> chars . append ( chr ( len ( lineArray ) - <NUM_LIT:1> ) ) <EOL> return "<STR_LIT>" . join ( chars ) <EOL> chars1 = diff_linesToCharsMunge ( text1 ) <EOL> chars2 = diff_linesToCharsMunge ( text2 ) <EOL> return ( chars1 , chars2 , lineArray ) <EOL> def diff_charsToLines ( self , diffs , lineArray ) : <EOL> """<STR_LIT>""" <EOL> for x in range ( len ( diffs ) ) : <EOL> text = [ ] <EOL> for char in diffs [ x ] [ <NUM_LIT:1> ] : <EOL> text . append ( lineArray [ ord ( char ) ] ) <EOL> diffs [ x ] = ( diffs [ x ] [ <NUM_LIT:0> ] , "<STR_LIT>" . join ( text ) ) <EOL> def diff_commonPrefix ( self , text1 , text2 ) : <EOL> """<STR_LIT>""" <EOL> if not text1 or not text2 or text1 [ <NUM_LIT:0> ] != text2 [ <NUM_LIT:0> ] : <EOL> return <NUM_LIT:0> <EOL> pointermin = <NUM_LIT:0> <EOL> pointermax = min ( len ( text1 ) , len ( text2 ) ) <EOL> pointermid = pointermax <EOL> pointerstart = <NUM_LIT:0> <EOL> while pointermin < pointermid : <EOL> if text1 [ pointerstart : pointermid ] == text2 [ pointerstart : pointermid ] : <EOL> pointermin = pointermid <EOL> pointerstart = pointermin <EOL> else : <EOL> pointermax = pointermid <EOL> pointermid = ( pointermax - pointermin ) // <NUM_LIT:2> + pointermin <EOL> return pointermid <EOL> def diff_commonSuffix ( self , text1 , text2 ) : <EOL> """<STR_LIT>""" <EOL> if not text1 or not text2 or text1 [ - <NUM_LIT:1> ] != text2 [ - <NUM_LIT:1> ] : <EOL> return <NUM_LIT:0> <EOL> pointermin = <NUM_LIT:0> <EOL> pointermax = min ( len ( text1 ) , len ( text2 ) ) <EOL> pointermid = pointermax <EOL> pointerend = <NUM_LIT:0> <EOL> while pointermin < pointermid : <EOL> if ( text1 [ - pointermid : len ( text1 ) - pointerend ] == <EOL> text2 [ - pointermid : len ( text2 ) - pointerend ] ) : <EOL> pointermin = pointermid <EOL> pointerend = pointermin <EOL> else : <EOL> pointermax = pointermid <EOL> pointermid = ( pointermax - pointermin ) // <NUM_LIT:2> + pointermin <EOL> return pointermid <EOL> def diff_commonOverlap ( self , text1 , text2 ) : <EOL> """<STR_LIT>""" <EOL> text1_length = len ( text1 ) <EOL> text2_length = len ( text2 ) <EOL> if text1_length == <NUM_LIT:0> or text2_length == <NUM_LIT:0> : <EOL> return <NUM_LIT:0> <EOL> if text1_length > text2_length : <EOL> text1 = text1 [ - text2_length : ] <EOL> elif text1_length < text2_length : <EOL> text2 = text2 [ : text1_length ] <EOL> text_length = min ( text1_length , text2_length ) <EOL> if text1 == text2 : <EOL> return text_length <EOL> best = <NUM_LIT:0> <EOL> length = <NUM_LIT:1> <EOL> while True : <EOL> pattern = text1 [ - length : ] <EOL> found = text2 . find ( pattern ) <EOL> if found == - <NUM_LIT:1> : <EOL> return best <EOL> length += found <EOL> if found == <NUM_LIT:0> or text1 [ - length : ] == text2 [ : length ] : <EOL> best = length <EOL> length += <NUM_LIT:1> <EOL> def diff_halfMatch ( self , text1 , text2 ) : <EOL> """<STR_LIT>""" <EOL> if self . Diff_Timeout <= <NUM_LIT:0> : <EOL> return None <EOL> if len ( text1 ) > len ( text2 ) : <EOL> ( longtext , shorttext ) = ( text1 , text2 ) <EOL> else : <EOL> ( shorttext , longtext ) = ( text1 , text2 ) <EOL> if len ( longtext ) < <NUM_LIT:4> or len ( shorttext ) * <NUM_LIT:2> < len ( longtext ) : <EOL> return None <EOL> def diff_halfMatchI ( longtext , shorttext , i ) : <EOL> """<STR_LIT>""" <EOL> seed = longtext [ i : i + len ( longtext ) // <NUM_LIT:4> ] <EOL> best_common = '<STR_LIT>' <EOL> j = shorttext . find ( seed ) <EOL> while j != - <NUM_LIT:1> : <EOL> prefixLength = self . diff_commonPrefix ( longtext [ i : ] , shorttext [ j : ] ) <EOL> suffixLength = self . diff_commonSuffix ( longtext [ : i ] , shorttext [ : j ] ) <EOL> if len ( best_common ) < suffixLength + prefixLength : <EOL> best_common = ( shorttext [ j - suffixLength : j ] + <EOL> shorttext [ j : j + prefixLength ] ) <EOL> best_longtext_a = longtext [ : i - suffixLength ] <EOL> best_longtext_b = longtext [ i + prefixLength : ] <EOL> best_shorttext_a = shorttext [ : j - suffixLength ] <EOL> best_shorttext_b = shorttext [ j + prefixLength : ] <EOL> j = shorttext . find ( seed , j + <NUM_LIT:1> ) <EOL> if len ( best_common ) * <NUM_LIT:2> >= len ( longtext ) : <EOL> return ( best_longtext_a , best_longtext_b , <EOL> best_shorttext_a , best_shorttext_b , best_common ) <EOL> else : <EOL> return None <EOL> hm1 = diff_halfMatchI ( longtext , shorttext , ( len ( longtext ) + <NUM_LIT:3> ) // <NUM_LIT:4> ) <EOL> hm2 = diff_halfMatchI ( longtext , shorttext , ( len ( longtext ) + <NUM_LIT:1> ) // <NUM_LIT:2> ) <EOL> if not hm1 and not hm2 : <EOL> return None <EOL> elif not hm2 : <EOL> hm = hm1 <EOL> elif not hm1 : <EOL> hm = hm2 <EOL> else : <EOL> if len ( hm1 [ <NUM_LIT:4> ] ) > len ( hm2 [ <NUM_LIT:4> ] ) : <EOL> hm = hm1 <EOL> else : <EOL> hm = hm2 <EOL> if len ( text1 ) > len ( text2 ) : <EOL> ( text1_a , text1_b , text2_a , text2_b , mid_common ) = hm <EOL> else : <EOL> ( text2_a , text2_b , text1_a , text1_b , mid_common ) = hm <EOL> return ( text1_a , text1_b , text2_a , text2_b , mid_common ) <EOL> def diff_cleanupSemantic ( self , diffs ) : <EOL> """<STR_LIT>""" <EOL> changes = False <EOL> equalities = [ ] <EOL> lastequality = None <EOL> pointer = <NUM_LIT:0> <EOL> length_insertions1 , length_deletions1 = <NUM_LIT:0> , <NUM_LIT:0> <EOL> length_insertions2 , length_deletions2 = <NUM_LIT:0> , <NUM_LIT:0> <EOL> while pointer < len ( diffs ) : <EOL> if diffs [ pointer ] [ <NUM_LIT:0> ] == self . DIFF_EQUAL : <EOL> equalities . append ( pointer ) <EOL> length_insertions1 , length_insertions2 = length_insertions2 , <NUM_LIT:0> <EOL> length_deletions1 , length_deletions2 = length_deletions2 , <NUM_LIT:0> <EOL> lastequality = diffs [ pointer ] [ <NUM_LIT:1> ] <EOL> else : <EOL> if diffs [ pointer ] [ <NUM_LIT:0> ] == self . DIFF_INSERT : <EOL> length_insertions2 += len ( diffs [ pointer ] [ <NUM_LIT:1> ] ) <EOL> else : <EOL> length_deletions2 += len ( diffs [ pointer ] [ <NUM_LIT:1> ] ) <EOL> if ( lastequality and ( len ( lastequality ) <= <EOL> max ( length_insertions1 , length_deletions1 ) ) and <EOL> ( len ( lastequality ) <= max ( length_insertions2 , length_deletions2 ) ) ) : <EOL> diffs . insert ( equalities [ - <NUM_LIT:1> ] , ( self . DIFF_DELETE , lastequality ) ) <EOL> diffs [ equalities [ - <NUM_LIT:1> ] + <NUM_LIT:1> ] = ( self . DIFF_INSERT , <EOL> diffs [ equalities [ - <NUM_LIT:1> ] + <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> equalities . pop ( ) <EOL> if len ( equalities ) : <EOL> equalities . pop ( ) <EOL> if len ( equalities ) : <EOL> pointer = equalities [ - <NUM_LIT:1> ] <EOL> else : <EOL> pointer = - <NUM_LIT:1> <EOL> length_insertions1 , length_deletions1 = <NUM_LIT:0> , <NUM_LIT:0> <EOL> length_insertions2 , length_deletions2 = <NUM_LIT:0> , <NUM_LIT:0> <EOL> lastequality = None <EOL> changes = True <EOL> pointer += <NUM_LIT:1> <EOL> if changes : <EOL> self . diff_cleanupMerge ( diffs ) <EOL> self . diff_cleanupSemanticLossless ( diffs ) <EOL> pointer = <NUM_LIT:1> <EOL> while pointer < len ( diffs ) : <EOL> if ( diffs [ pointer - <NUM_LIT:1> ] [ <NUM_LIT:0> ] == self . DIFF_DELETE and <EOL> diffs [ pointer ] [ <NUM_LIT:0> ] == self . DIFF_INSERT ) : <EOL> deletion = diffs [ pointer - <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> insertion = diffs [ pointer ] [ <NUM_LIT:1> ] <EOL> overlap_length1 = self . diff_commonOverlap ( deletion , insertion ) <EOL> overlap_length2 = self . diff_commonOverlap ( insertion , deletion ) <EOL> if overlap_length1 >= overlap_length2 : <EOL> if ( overlap_length1 >= len ( deletion ) / <NUM_LIT> or <EOL> overlap_length1 >= len ( insertion ) / <NUM_LIT> ) : <EOL> diffs . insert ( pointer , ( self . DIFF_EQUAL , <EOL> insertion [ : overlap_length1 ] ) ) <EOL> diffs [ pointer - <NUM_LIT:1> ] = ( self . DIFF_DELETE , <EOL> deletion [ : len ( deletion ) - overlap_length1 ] ) <EOL> diffs [ pointer + <NUM_LIT:1> ] = ( self . DIFF_INSERT , <EOL> insertion [ overlap_length1 : ] ) <EOL> pointer += <NUM_LIT:1> <EOL> else : <EOL> if ( overlap_length2 >= len ( deletion ) / <NUM_LIT> or <EOL> overlap_length2 >= len ( insertion ) / <NUM_LIT> ) : <EOL> diffs . insert ( pointer , ( self . DIFF_EQUAL , deletion [ : overlap_length2 ] ) ) <EOL> diffs [ pointer - <NUM_LIT:1> ] = ( self . DIFF_INSERT , <EOL> insertion [ : len ( insertion ) - overlap_length2 ] ) <EOL> diffs [ pointer + <NUM_LIT:1> ] = ( self . DIFF_DELETE , deletion [ overlap_length2 : ] ) <EOL> pointer += <NUM_LIT:1> <EOL> pointer += <NUM_LIT:1> <EOL> pointer += <NUM_LIT:1> <EOL> def diff_cleanupSemanticLossless ( self , diffs ) : <EOL> """<STR_LIT>""" <EOL> def diff_cleanupSemanticScore ( one , two ) : <EOL> """<STR_LIT>""" <EOL> if not one or not two : <EOL> return <NUM_LIT:6> <EOL> char1 = one [ - <NUM_LIT:1> ] <EOL> char2 = two [ <NUM_LIT:0> ] <EOL> nonAlphaNumeric1 = not char1 . isalnum ( ) <EOL> nonAlphaNumeric2 = not char2 . isalnum ( ) <EOL> whitespace1 = nonAlphaNumeric1 and char1 . isspace ( ) <EOL> whitespace2 = nonAlphaNumeric2 and char2 . isspace ( ) <EOL> lineBreak1 = whitespace1 and ( char1 == "<STR_LIT:\r>" or char1 == "<STR_LIT:\n>" ) <EOL> lineBreak2 = whitespace2 and ( char2 == "<STR_LIT:\r>" or char2 == "<STR_LIT:\n>" ) <EOL> blankLine1 = lineBreak1 and self . BLANKLINEEND . search ( one ) <EOL> blankLine2 = lineBreak2 and self . BLANKLINESTART . match ( two ) <EOL> if blankLine1 or blankLine2 : <EOL> return <NUM_LIT:5> <EOL> elif lineBreak1 or lineBreak2 : <EOL> return <NUM_LIT:4> <EOL> elif nonAlphaNumeric1 and not whitespace1 and whitespace2 : <EOL> return <NUM_LIT:3> <EOL> elif whitespace1 or whitespace2 : <EOL> return <NUM_LIT:2> <EOL> elif nonAlphaNumeric1 or nonAlphaNumeric2 : <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> pointer = <NUM_LIT:1> <EOL> while pointer < len ( diffs ) - <NUM_LIT:1> : <EOL> if ( diffs [ pointer - <NUM_LIT:1> ] [ <NUM_LIT:0> ] == self . DIFF_EQUAL and <EOL> diffs [ pointer + <NUM_LIT:1> ] [ <NUM_LIT:0> ] == self . DIFF_EQUAL ) : <EOL> equality1 = diffs [ pointer - <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> edit = diffs [ pointer ] [ <NUM_LIT:1> ] <EOL> equality2 = diffs [ pointer + <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> commonOffset = self . diff_commonSuffix ( equality1 , edit ) <EOL> if commonOffset : <EOL> commonString = edit [ - commonOffset : ] <EOL> equality1 = equality1 [ : - commonOffset ] <EOL> edit = commonString + edit [ : - commonOffset ] <EOL> equality2 = commonString + equality2 <EOL> bestEquality1 = equality1 <EOL> bestEdit = edit <EOL> bestEquality2 = equality2 <EOL> bestScore = ( diff_cleanupSemanticScore ( equality1 , edit ) + <EOL> diff_cleanupSemanticScore ( edit , equality2 ) ) <EOL> while edit and equality2 and edit [ <NUM_LIT:0> ] == equality2 [ <NUM_LIT:0> ] : <EOL> equality1 += edit [ <NUM_LIT:0> ] <EOL> edit = edit [ <NUM_LIT:1> : ] + equality2 [ <NUM_LIT:0> ] <EOL> equality2 = equality2 [ <NUM_LIT:1> : ] <EOL> score = ( diff_cleanupSemanticScore ( equality1 , edit ) + <EOL> diff_cleanupSemanticScore ( edit , equality2 ) ) <EOL> if score >= bestScore : <EOL> bestScore = score <EOL> bestEquality1 = equality1 <EOL> bestEdit = edit <EOL> bestEquality2 = equality2 <EOL> if diffs [ pointer - <NUM_LIT:1> ] [ <NUM_LIT:1> ] != bestEquality1 : <EOL> if bestEquality1 : <EOL> diffs [ pointer - <NUM_LIT:1> ] = ( diffs [ pointer - <NUM_LIT:1> ] [ <NUM_LIT:0> ] , bestEquality1 ) <EOL> else : <EOL> del diffs [ pointer - <NUM_LIT:1> ] <EOL> pointer -= <NUM_LIT:1> <EOL> diffs [ pointer ] = ( diffs [ pointer ] [ <NUM_LIT:0> ] , bestEdit ) <EOL> if bestEquality2 : <EOL> diffs [ pointer + <NUM_LIT:1> ] = ( diffs [ pointer + <NUM_LIT:1> ] [ <NUM_LIT:0> ] , bestEquality2 ) <EOL> else : <EOL> del diffs [ pointer + <NUM_LIT:1> ] <EOL> pointer -= <NUM_LIT:1> <EOL> pointer += <NUM_LIT:1> <EOL> BLANKLINEEND = re . compile ( r"<STR_LIT>" ) ; <EOL> BLANKLINESTART = re . compile ( r"<STR_LIT>" ) ; <EOL> def diff_cleanupEfficiency ( self , diffs ) : <EOL> """<STR_LIT>""" <EOL> changes = False <EOL> equalities = [ ] <EOL> lastequality = None <EOL> pointer = <NUM_LIT:0> <EOL> pre_ins = False <EOL> pre_del = False <EOL> post_ins = False <EOL> post_del = False <EOL> while pointer < len ( diffs ) : <EOL> if diffs [ pointer ] [ <NUM_LIT:0> ] == self . DIFF_EQUAL : <EOL> if ( len ( diffs [ pointer ] [ <NUM_LIT:1> ] ) < self . Diff_EditCost and <EOL> ( post_ins or post_del ) ) : <EOL> equalities . append ( pointer ) <EOL> pre_ins = post_ins <EOL> pre_del = post_del <EOL> lastequality = diffs [ pointer ] [ <NUM_LIT:1> ] <EOL> else : <EOL> equalities = [ ] <EOL> lastequality = None <EOL> post_ins = post_del = False <EOL> else : <EOL> if diffs [ pointer ] [ <NUM_LIT:0> ] == self . DIFF_DELETE : <EOL> post_del = True <EOL> else : <EOL> post_ins = True <EOL> if lastequality and ( ( pre_ins and pre_del and post_ins and post_del ) or <EOL> ( ( len ( lastequality ) < self . Diff_EditCost / <NUM_LIT:2> ) and <EOL> ( pre_ins + pre_del + post_ins + post_del ) == <NUM_LIT:3> ) ) : <EOL> diffs . insert ( equalities [ - <NUM_LIT:1> ] , ( self . DIFF_DELETE , lastequality ) ) <EOL> diffs [ equalities [ - <NUM_LIT:1> ] + <NUM_LIT:1> ] = ( self . DIFF_INSERT , <EOL> diffs [ equalities [ - <NUM_LIT:1> ] + <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> equalities . pop ( ) <EOL> lastequality = None <EOL> if pre_ins and pre_del : <EOL> post_ins = post_del = True <EOL> equalities = [ ] <EOL> else : <EOL> if len ( equalities ) : <EOL> equalities . pop ( ) <EOL> if len ( equalities ) : <EOL> pointer = equalities [ - <NUM_LIT:1> ] <EOL> else : <EOL> pointer = - <NUM_LIT:1> <EOL> post_ins = post_del = False <EOL> changes = True <EOL> pointer += <NUM_LIT:1> <EOL> if changes : <EOL> self . diff_cleanupMerge ( diffs ) <EOL> def diff_cleanupMerge ( self , diffs ) : <EOL> """<STR_LIT>""" <EOL> diffs . append ( ( self . DIFF_EQUAL , '<STR_LIT>' ) ) <EOL> pointer = <NUM_LIT:0> <EOL> count_delete = <NUM_LIT:0> <EOL> count_insert = <NUM_LIT:0> <EOL> text_delete = '<STR_LIT>' <EOL> text_insert = '<STR_LIT>' <EOL> while pointer < len ( diffs ) : <EOL> if diffs [ pointer ] [ <NUM_LIT:0> ] == self . DIFF_INSERT : <EOL> count_insert += <NUM_LIT:1> <EOL> text_insert += diffs [ pointer ] [ <NUM_LIT:1> ] <EOL> pointer += <NUM_LIT:1> <EOL> elif diffs [ pointer ] [ <NUM_LIT:0> ] == self . DIFF_DELETE : <EOL> count_delete += <NUM_LIT:1> <EOL> text_delete += diffs [ pointer ] [ <NUM_LIT:1> ] <EOL> pointer += <NUM_LIT:1> <EOL> elif diffs [ pointer ] [ <NUM_LIT:0> ] == self . DIFF_EQUAL : <EOL> if count_delete + count_insert > <NUM_LIT:1> : <EOL> if count_delete != <NUM_LIT:0> and count_insert != <NUM_LIT:0> : <EOL> commonlength = self . diff_commonPrefix ( text_insert , text_delete ) <EOL> if commonlength != <NUM_LIT:0> : <EOL> x = pointer - count_delete - count_insert - <NUM_LIT:1> <EOL> if x >= <NUM_LIT:0> and diffs [ x ] [ <NUM_LIT:0> ] == self . DIFF_EQUAL : <EOL> diffs [ x ] = ( diffs [ x ] [ <NUM_LIT:0> ] , diffs [ x ] [ <NUM_LIT:1> ] + <EOL> text_insert [ : commonlength ] ) <EOL> else : <EOL> diffs . insert ( <NUM_LIT:0> , ( self . DIFF_EQUAL , text_insert [ : commonlength ] ) ) <EOL> pointer += <NUM_LIT:1> <EOL> text_insert = text_insert [ commonlength : ] <EOL> text_delete = text_delete [ commonlength : ] <EOL> commonlength = self . diff_commonSuffix ( text_insert , text_delete ) <EOL> if commonlength != <NUM_LIT:0> : <EOL> diffs [ pointer ] = ( diffs [ pointer ] [ <NUM_LIT:0> ] , text_insert [ - commonlength : ] + <EOL> diffs [ pointer ] [ <NUM_LIT:1> ] ) <EOL> text_insert = text_insert [ : - commonlength ] <EOL> text_delete = text_delete [ : - commonlength ] <EOL> if count_delete == <NUM_LIT:0> : <EOL> diffs [ pointer - count_insert : pointer ] = [ <EOL> ( self . DIFF_INSERT , text_insert ) ] <EOL> elif count_insert == <NUM_LIT:0> : <EOL> diffs [ pointer - count_delete : pointer ] = [ <EOL> ( self . DIFF_DELETE , text_delete ) ] <EOL> else : <EOL> diffs [ pointer - count_delete - count_insert : pointer ] = [ <EOL> ( self . DIFF_DELETE , text_delete ) , <EOL> ( self . DIFF_INSERT , text_insert ) ] <EOL> pointer = pointer - count_delete - count_insert + <NUM_LIT:1> <EOL> if count_delete != <NUM_LIT:0> : <EOL> pointer += <NUM_LIT:1> <EOL> if count_insert != <NUM_LIT:0> : <EOL> pointer += <NUM_LIT:1> <EOL> elif pointer != <NUM_LIT:0> and diffs [ pointer - <NUM_LIT:1> ] [ <NUM_LIT:0> ] == self . DIFF_EQUAL : <EOL> diffs [ pointer - <NUM_LIT:1> ] = ( diffs [ pointer - <NUM_LIT:1> ] [ <NUM_LIT:0> ] , <EOL> diffs [ pointer - <NUM_LIT:1> ] [ <NUM_LIT:1> ] + diffs [ pointer ] [ <NUM_LIT:1> ] ) <EOL> del diffs [ pointer ] <EOL> else : <EOL> pointer += <NUM_LIT:1> <EOL> count_insert = <NUM_LIT:0> <EOL> count_delete = <NUM_LIT:0> <EOL> text_delete = '<STR_LIT>' <EOL> text_insert = '<STR_LIT>' <EOL> if diffs [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] == '<STR_LIT>' : <EOL> diffs . pop ( ) <EOL> changes = False <EOL> pointer = <NUM_LIT:1> <EOL> while pointer < len ( diffs ) - <NUM_LIT:1> : <EOL> if ( diffs [ pointer - <NUM_LIT:1> ] [ <NUM_LIT:0> ] == self . DIFF_EQUAL and <EOL> diffs [ pointer + <NUM_LIT:1> ] [ <NUM_LIT:0> ] == self . DIFF_EQUAL ) : <EOL> if diffs [ pointer ] [ <NUM_LIT:1> ] . endswith ( diffs [ pointer - <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) : <EOL> diffs [ pointer ] = ( diffs [ pointer ] [ <NUM_LIT:0> ] , <EOL> diffs [ pointer - <NUM_LIT:1> ] [ <NUM_LIT:1> ] + <EOL> diffs [ pointer ] [ <NUM_LIT:1> ] [ : - len ( diffs [ pointer - <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) ] ) <EOL> diffs [ pointer + <NUM_LIT:1> ] = ( diffs [ pointer + <NUM_LIT:1> ] [ <NUM_LIT:0> ] , <EOL> diffs [ pointer - <NUM_LIT:1> ] [ <NUM_LIT:1> ] + diffs [ pointer + <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> del diffs [ pointer - <NUM_LIT:1> ] <EOL> changes = True <EOL> elif diffs [ pointer ] [ <NUM_LIT:1> ] . startswith ( diffs [ pointer + <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) : <EOL> diffs [ pointer - <NUM_LIT:1> ] = ( diffs [ pointer - <NUM_LIT:1> ] [ <NUM_LIT:0> ] , <EOL> diffs [ pointer - <NUM_LIT:1> ] [ <NUM_LIT:1> ] + diffs [ pointer + <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> diffs [ pointer ] = ( diffs [ pointer ] [ <NUM_LIT:0> ] , <EOL> diffs [ pointer ] [ <NUM_LIT:1> ] [ len ( diffs [ pointer + <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) : ] + <EOL> diffs [ pointer + <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> del diffs [ pointer + <NUM_LIT:1> ] <EOL> changes = True <EOL> pointer += <NUM_LIT:1> <EOL> if changes : <EOL> self . diff_cleanupMerge ( diffs ) <EOL> def diff_xIndex ( self , diffs , loc ) : <EOL> """<STR_LIT>""" <EOL> chars1 = <NUM_LIT:0> <EOL> chars2 = <NUM_LIT:0> <EOL> last_chars1 = <NUM_LIT:0> <EOL> last_chars2 = <NUM_LIT:0> <EOL> for x in range ( len ( diffs ) ) : <EOL> ( op , text ) = diffs [ x ] <EOL> if op != self . DIFF_INSERT : <EOL> chars1 += len ( text ) <EOL> if op != self . DIFF_DELETE : <EOL> chars2 += len ( text ) <EOL> if chars1 > loc : <EOL> break <EOL> last_chars1 = chars1 <EOL> last_chars2 = chars2 <EOL> if len ( diffs ) != x and diffs [ x ] [ <NUM_LIT:0> ] == self . DIFF_DELETE : <EOL> return last_chars2 <EOL> return last_chars2 + ( loc - last_chars1 ) <EOL> def diff_prettyHtml ( self , diffs ) : <EOL> """<STR_LIT>""" <EOL> html = [ ] <EOL> for ( op , data ) in diffs : <EOL> text = ( data . replace ( "<STR_LIT:&>" , "<STR_LIT>" ) . replace ( "<STR_LIT:<>" , "<STR_LIT>" ) <EOL> . replace ( "<STR_LIT:>>" , "<STR_LIT>" ) . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) ) <EOL> if op == self . DIFF_INSERT : <EOL> html . append ( "<STR_LIT>" % text ) <EOL> elif op == self . DIFF_DELETE : <EOL> html . append ( "<STR_LIT>" % text ) <EOL> elif op == self . DIFF_EQUAL : <EOL> html . append ( "<STR_LIT>" % text ) <EOL> return "<STR_LIT>" . join ( html ) <EOL> def diff_text1 ( self , diffs ) : <EOL> """<STR_LIT>""" <EOL> text = [ ] <EOL> for ( op , data ) in diffs : <EOL> if op != self . DIFF_INSERT : <EOL> text . append ( data ) <EOL> return "<STR_LIT>" . join ( text ) <EOL> def diff_text2 ( self , diffs ) : <EOL> """<STR_LIT>""" <EOL> text = [ ] <EOL> for ( op , data ) in diffs : <EOL> if op != self . DIFF_DELETE : <EOL> text . append ( data ) <EOL> return "<STR_LIT>" . join ( text ) <EOL> def diff_levenshtein ( self , diffs ) : <EOL> """<STR_LIT>""" <EOL> levenshtein = <NUM_LIT:0> <EOL> insertions = <NUM_LIT:0> <EOL> deletions = <NUM_LIT:0> <EOL> for ( op , data ) in diffs : <EOL> if op == self . DIFF_INSERT : <EOL> insertions += len ( data ) <EOL> elif op == self . DIFF_DELETE : <EOL> deletions += len ( data ) <EOL> elif op == self . DIFF_EQUAL : <EOL> levenshtein += max ( insertions , deletions ) <EOL> insertions = <NUM_LIT:0> <EOL> deletions = <NUM_LIT:0> <EOL> levenshtein += max ( insertions , deletions ) <EOL> return levenshtein <EOL> def diff_toDelta ( self , diffs ) : <EOL> """<STR_LIT>""" <EOL> text = [ ] <EOL> for ( op , data ) in diffs : <EOL> if op == self . DIFF_INSERT : <EOL> data = data . encode ( "<STR_LIT:utf-8>" ) <EOL> text . append ( "<STR_LIT:+>" + urllib . parse . quote ( data , "<STR_LIT>" ) ) <EOL> elif op == self . DIFF_DELETE : <EOL> text . append ( "<STR_LIT>" % len ( data ) ) <EOL> elif op == self . DIFF_EQUAL : <EOL> text . append ( "<STR_LIT>" % len ( data ) ) <EOL> return "<STR_LIT:\t>" . join ( text ) <EOL> def diff_fromDelta ( self , text1 , delta ) : <EOL> """<STR_LIT>""" <EOL> diffs = [ ] <EOL> pointer = <NUM_LIT:0> <EOL> tokens = delta . split ( "<STR_LIT:\t>" ) <EOL> for token in tokens : <EOL> if token == "<STR_LIT>" : <EOL> continue <EOL> param = token [ <NUM_LIT:1> : ] <EOL> if token [ <NUM_LIT:0> ] == "<STR_LIT:+>" : <EOL> param = urllib . parse . unquote ( param ) <EOL> diffs . append ( ( self . DIFF_INSERT , param ) ) <EOL> elif token [ <NUM_LIT:0> ] == "<STR_LIT:->" or token [ <NUM_LIT:0> ] == "<STR_LIT:=>" : <EOL> try : <EOL> n = int ( param ) <EOL> except ValueError : <EOL> raise ValueError ( "<STR_LIT>" + param ) <EOL> if n < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" + param ) <EOL> text = text1 [ pointer : pointer + n ] <EOL> pointer += n <EOL> if token [ <NUM_LIT:0> ] == "<STR_LIT:=>" : <EOL> diffs . append ( ( self . DIFF_EQUAL , text ) ) <EOL> else : <EOL> diffs . append ( ( self . DIFF_DELETE , text ) ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" + <EOL> token [ <NUM_LIT:0> ] ) <EOL> if pointer != len ( text1 ) : <EOL> raise ValueError ( <EOL> "<STR_LIT>" % <EOL> ( pointer , len ( text1 ) ) ) <EOL> return diffs <EOL> def match_main ( self , text , pattern , loc ) : <EOL> """<STR_LIT>""" <EOL> if text == None or pattern == None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> loc = max ( <NUM_LIT:0> , min ( loc , len ( text ) ) ) <EOL> if text == pattern : <EOL> return <NUM_LIT:0> <EOL> elif not text : <EOL> return - <NUM_LIT:1> <EOL> elif text [ loc : loc + len ( pattern ) ] == pattern : <EOL> return loc <EOL> else : <EOL> match = self . match_bitap ( text , pattern , loc ) <EOL> return match <EOL> def match_bitap ( self , text , pattern , loc ) : <EOL> """<STR_LIT>""" <EOL> s = self . match_alphabet ( pattern ) <EOL> def match_bitapScore ( e , x ) : <EOL> """<STR_LIT>""" <EOL> accuracy = float ( e ) / len ( pattern ) <EOL> proximity = abs ( loc - x ) <EOL> if not self . Match_Distance : <EOL> return proximity and <NUM_LIT:1.0> or accuracy <EOL> return accuracy + ( proximity / float ( self . Match_Distance ) ) <EOL> score_threshold = self . Match_Threshold <EOL> best_loc = text . find ( pattern , loc ) <EOL> if best_loc != - <NUM_LIT:1> : <EOL> score_threshold = min ( match_bitapScore ( <NUM_LIT:0> , best_loc ) , score_threshold ) <EOL> best_loc = text . rfind ( pattern , loc + len ( pattern ) ) <EOL> if best_loc != - <NUM_LIT:1> : <EOL> score_threshold = min ( match_bitapScore ( <NUM_LIT:0> , best_loc ) , score_threshold ) <EOL> matchmask = <NUM_LIT:1> << ( len ( pattern ) - <NUM_LIT:1> ) <EOL> best_loc = - <NUM_LIT:1> <EOL> bin_max = len ( pattern ) + len ( text ) <EOL> last_rd = None <EOL> for d in range ( len ( pattern ) ) : <EOL> bin_min = <NUM_LIT:0> <EOL> bin_mid = bin_max <EOL> while bin_min < bin_mid : <EOL> if match_bitapScore ( d , loc + bin_mid ) <= score_threshold : <EOL> bin_min = bin_mid <EOL> else : <EOL> bin_max = bin_mid <EOL> bin_mid = ( bin_max - bin_min ) // <NUM_LIT:2> + bin_min <EOL> bin_max = bin_mid <EOL> start = max ( <NUM_LIT:1> , loc - bin_mid + <NUM_LIT:1> ) <EOL> finish = min ( loc + bin_mid , len ( text ) ) + len ( pattern ) <EOL> rd = [ <NUM_LIT:0> ] * ( finish + <NUM_LIT:2> ) <EOL> rd [ finish + <NUM_LIT:1> ] = ( <NUM_LIT:1> << d ) - <NUM_LIT:1> <EOL> for j in range ( finish , start - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> if len ( text ) <= j - <NUM_LIT:1> : <EOL> charMatch = <NUM_LIT:0> <EOL> else : <EOL> charMatch = s . get ( text [ j - <NUM_LIT:1> ] , <NUM_LIT:0> ) <EOL> if d == <NUM_LIT:0> : <EOL> rd [ j ] = ( ( rd [ j + <NUM_LIT:1> ] << <NUM_LIT:1> ) | <NUM_LIT:1> ) & charMatch <EOL> else : <EOL> rd [ j ] = ( ( ( rd [ j + <NUM_LIT:1> ] << <NUM_LIT:1> ) | <NUM_LIT:1> ) & charMatch ) | ( <EOL> ( ( last_rd [ j + <NUM_LIT:1> ] | last_rd [ j ] ) << <NUM_LIT:1> ) | <NUM_LIT:1> ) | last_rd [ j + <NUM_LIT:1> ] <EOL> if rd [ j ] & matchmask : <EOL> score = match_bitapScore ( d , j - <NUM_LIT:1> ) <EOL> if score <= score_threshold : <EOL> score_threshold = score <EOL> best_loc = j - <NUM_LIT:1> <EOL> if best_loc > loc : <EOL> start = max ( <NUM_LIT:1> , <NUM_LIT:2> * loc - best_loc ) <EOL> else : <EOL> break <EOL> if match_bitapScore ( d + <NUM_LIT:1> , loc ) > score_threshold : <EOL> break <EOL> last_rd = rd <EOL> return best_loc <EOL> def match_alphabet ( self , pattern ) : <EOL> """<STR_LIT>""" <EOL> s = { } <EOL> for char in pattern : <EOL> s [ char ] = <NUM_LIT:0> <EOL> for i in range ( len ( pattern ) ) : <EOL> s [ pattern [ i ] ] |= <NUM_LIT:1> << ( len ( pattern ) - i - <NUM_LIT:1> ) <EOL> return s <EOL> def patch_addContext ( self , patch , text ) : <EOL> """<STR_LIT>""" <EOL> if len ( text ) == <NUM_LIT:0> : <EOL> return <EOL> pattern = text [ patch . start2 : patch . start2 + patch . length1 ] <EOL> padding = <NUM_LIT:0> <EOL> while ( text . find ( pattern ) != text . rfind ( pattern ) and ( self . Match_MaxBits == <EOL> <NUM_LIT:0> or len ( pattern ) < self . Match_MaxBits - self . Patch_Margin - <EOL> self . Patch_Margin ) ) : <EOL> padding += self . Patch_Margin <EOL> pattern = text [ max ( <NUM_LIT:0> , patch . start2 - padding ) : <EOL> patch . start2 + patch . length1 + padding ] <EOL> padding += self . Patch_Margin <EOL> prefix = text [ max ( <NUM_LIT:0> , patch . start2 - padding ) : patch . start2 ] <EOL> if prefix : <EOL> patch . diffs [ : <NUM_LIT:0> ] = [ ( self . DIFF_EQUAL , prefix ) ] <EOL> suffix = text [ patch . start2 + patch . length1 : <EOL> patch . start2 + patch . length1 + padding ] <EOL> if suffix : <EOL> patch . diffs . append ( ( self . DIFF_EQUAL , suffix ) ) <EOL> patch . start1 -= len ( prefix ) <EOL> patch . start2 -= len ( prefix ) <EOL> patch . length1 += len ( prefix ) + len ( suffix ) <EOL> patch . length2 += len ( prefix ) + len ( suffix ) <EOL> def patch_make ( self , a , b = None , c = None ) : <EOL> """<STR_LIT>""" <EOL> text1 = None <EOL> diffs = None <EOL> if isinstance ( a , str ) and isinstance ( b , str ) and c is None : <EOL> text1 = a <EOL> diffs = self . diff_main ( text1 , b , True ) <EOL> if len ( diffs ) > <NUM_LIT:2> : <EOL> self . diff_cleanupSemantic ( diffs ) <EOL> self . diff_cleanupEfficiency ( diffs ) <EOL> elif isinstance ( a , list ) and b is None and c is None : <EOL> diffs = a <EOL> text1 = self . diff_text1 ( diffs ) <EOL> elif isinstance ( a , str ) and isinstance ( b , list ) and c is None : <EOL> text1 = a <EOL> diffs = b <EOL> elif ( isinstance ( a , str ) and isinstance ( b , str ) and <EOL> isinstance ( c , list ) ) : <EOL> text1 = a <EOL> diffs = c <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if not diffs : <EOL> return [ ] <EOL> patches = [ ] <EOL> patch = patch_obj ( ) <EOL> char_count1 = <NUM_LIT:0> <EOL> char_count2 = <NUM_LIT:0> <EOL> prepatch_text = text1 <EOL> postpatch_text = text1 <EOL> for x in range ( len ( diffs ) ) : <EOL> ( diff_type , diff_text ) = diffs [ x ] <EOL> if len ( patch . diffs ) == <NUM_LIT:0> and diff_type != self . DIFF_EQUAL : <EOL> patch . start1 = char_count1 <EOL> patch . start2 = char_count2 <EOL> if diff_type == self . DIFF_INSERT : <EOL> patch . diffs . append ( diffs [ x ] ) <EOL> patch . length2 += len ( diff_text ) <EOL> postpatch_text = ( postpatch_text [ : char_count2 ] + diff_text + <EOL> postpatch_text [ char_count2 : ] ) <EOL> elif diff_type == self . DIFF_DELETE : <EOL> patch . length1 += len ( diff_text ) <EOL> patch . diffs . append ( diffs [ x ] ) <EOL> postpatch_text = ( postpatch_text [ : char_count2 ] + <EOL> postpatch_text [ char_count2 + len ( diff_text ) : ] ) <EOL> elif ( diff_type == self . DIFF_EQUAL and <EOL> len ( diff_text ) <= <NUM_LIT:2> * self . Patch_Margin and <EOL> len ( patch . diffs ) != <NUM_LIT:0> and len ( diffs ) != x + <NUM_LIT:1> ) : <EOL> patch . diffs . append ( diffs [ x ] ) <EOL> patch . length1 += len ( diff_text ) <EOL> patch . length2 += len ( diff_text ) <EOL> if ( diff_type == self . DIFF_EQUAL and <EOL> len ( diff_text ) >= <NUM_LIT:2> * self . Patch_Margin ) : <EOL> if len ( patch . diffs ) != <NUM_LIT:0> : <EOL> self . patch_addContext ( patch , prepatch_text ) <EOL> patches . append ( patch ) <EOL> patch = patch_obj ( ) <EOL> prepatch_text = postpatch_text <EOL> char_count1 = char_count2 <EOL> if diff_type != self . DIFF_INSERT : <EOL> char_count1 += len ( diff_text ) <EOL> if diff_type != self . DIFF_DELETE : <EOL> char_count2 += len ( diff_text ) <EOL> if len ( patch . diffs ) != <NUM_LIT:0> : <EOL> self . patch_addContext ( patch , prepatch_text ) <EOL> patches . append ( patch ) <EOL> return patches <EOL> def patch_deepCopy ( self , patches ) : <EOL> """<STR_LIT>""" <EOL> patchesCopy = [ ] <EOL> for patch in patches : <EOL> patchCopy = patch_obj ( ) <EOL> patchCopy . diffs = patch . diffs [ : ] <EOL> patchCopy . start1 = patch . start1 <EOL> patchCopy . start2 = patch . start2 <EOL> patchCopy . length1 = patch . length1 <EOL> patchCopy . length2 = patch . length2 <EOL> patchesCopy . append ( patchCopy ) <EOL> return patchesCopy <EOL> def patch_apply ( self , patches , text ) : <EOL> """<STR_LIT>""" <EOL> if not patches : <EOL> return ( text , [ ] ) <EOL> patches = self . patch_deepCopy ( patches ) <EOL> nullPadding = self . patch_addPadding ( patches ) <EOL> text = nullPadding + text + nullPadding <EOL> self . patch_splitMax ( patches ) <EOL> delta = <NUM_LIT:0> <EOL> results = [ ] <EOL> for patch in patches : <EOL> expected_loc = patch . start2 + delta <EOL> text1 = self . diff_text1 ( patch . diffs ) <EOL> end_loc = - <NUM_LIT:1> <EOL> if len ( text1 ) > self . Match_MaxBits : <EOL> start_loc = self . match_main ( text , text1 [ : self . Match_MaxBits ] , <EOL> expected_loc ) <EOL> if start_loc != - <NUM_LIT:1> : <EOL> end_loc = self . match_main ( text , text1 [ - self . Match_MaxBits : ] , <EOL> expected_loc + len ( text1 ) - self . Match_MaxBits ) <EOL> if end_loc == - <NUM_LIT:1> or start_loc >= end_loc : <EOL> start_loc = - <NUM_LIT:1> <EOL> else : <EOL> start_loc = self . match_main ( text , text1 , expected_loc ) <EOL> if start_loc == - <NUM_LIT:1> : <EOL> results . append ( False ) <EOL> delta -= patch . length2 - patch . length1 <EOL> else : <EOL> results . append ( True ) <EOL> delta = start_loc - expected_loc <EOL> if end_loc == - <NUM_LIT:1> : <EOL> text2 = text [ start_loc : start_loc + len ( text1 ) ] <EOL> else : <EOL> text2 = text [ start_loc : end_loc + self . Match_MaxBits ] <EOL> if text1 == text2 : <EOL> text = ( text [ : start_loc ] + self . diff_text2 ( patch . diffs ) + <EOL> text [ start_loc + len ( text1 ) : ] ) <EOL> else : <EOL> diffs = self . diff_main ( text1 , text2 , False ) <EOL> if ( len ( text1 ) > self . Match_MaxBits and <EOL> self . diff_levenshtein ( diffs ) / float ( len ( text1 ) ) > <EOL> self . Patch_DeleteThreshold ) : <EOL> results [ - <NUM_LIT:1> ] = False <EOL> else : <EOL> self . diff_cleanupSemanticLossless ( diffs ) <EOL> index1 = <NUM_LIT:0> <EOL> for ( op , data ) in patch . diffs : <EOL> if op != self . DIFF_EQUAL : <EOL> index2 = self . diff_xIndex ( diffs , index1 ) <EOL> if op == self . DIFF_INSERT : <EOL> text = text [ : start_loc + index2 ] + data + text [ start_loc + <EOL> index2 : ] <EOL> elif op == self . DIFF_DELETE : <EOL> text = text [ : start_loc + index2 ] + text [ start_loc + <EOL> self . diff_xIndex ( diffs , index1 + len ( data ) ) : ] <EOL> if op != self . DIFF_DELETE : <EOL> index1 += len ( data ) <EOL> text = text [ len ( nullPadding ) : - len ( nullPadding ) ] <EOL> return ( text , results ) <EOL> def patch_addPadding ( self , patches ) : <EOL> """<STR_LIT>""" <EOL> paddingLength = self . Patch_Margin <EOL> nullPadding = "<STR_LIT>" <EOL> for x in range ( <NUM_LIT:1> , paddingLength + <NUM_LIT:1> ) : <EOL> nullPadding += chr ( x ) <EOL> for patch in patches : <EOL> patch . start1 += paddingLength <EOL> patch . start2 += paddingLength <EOL> patch = patches [ <NUM_LIT:0> ] <EOL> diffs = patch . diffs <EOL> if not diffs or diffs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] != self . DIFF_EQUAL : <EOL> diffs . insert ( <NUM_LIT:0> , ( self . DIFF_EQUAL , nullPadding ) ) <EOL> patch . start1 -= paddingLength <EOL> patch . start2 -= paddingLength <EOL> patch . length1 += paddingLength <EOL> patch . length2 += paddingLength <EOL> elif paddingLength > len ( diffs [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) : <EOL> extraLength = paddingLength - len ( diffs [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) <EOL> newText = nullPadding [ len ( diffs [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) : ] + diffs [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> diffs [ <NUM_LIT:0> ] = ( diffs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , newText ) <EOL> patch . start1 -= extraLength <EOL> patch . start2 -= extraLength <EOL> patch . length1 += extraLength <EOL> patch . length2 += extraLength <EOL> patch = patches [ - <NUM_LIT:1> ] <EOL> diffs = patch . diffs <EOL> if not diffs or diffs [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] != self . DIFF_EQUAL : <EOL> diffs . append ( ( self . DIFF_EQUAL , nullPadding ) ) <EOL> patch . length1 += paddingLength <EOL> patch . length2 += paddingLength <EOL> elif paddingLength > len ( diffs [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) : <EOL> extraLength = paddingLength - len ( diffs [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] ) <EOL> newText = diffs [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] + nullPadding [ : extraLength ] <EOL> diffs [ - <NUM_LIT:1> ] = ( diffs [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] , newText ) <EOL> patch . length1 += extraLength <EOL> patch . length2 += extraLength <EOL> return nullPadding <EOL> def patch_splitMax ( self , patches ) : <EOL> """<STR_LIT>""" <EOL> patch_size = self . Match_MaxBits <EOL> if patch_size == <NUM_LIT:0> : <EOL> return <EOL> for x in range ( len ( patches ) ) : <EOL> if patches [ x ] . length1 <= patch_size : <EOL> continue <EOL> bigpatch = patches [ x ] <EOL> del patches [ x ] <EOL> x -= <NUM_LIT:1> <EOL> start1 = bigpatch . start1 <EOL> start2 = bigpatch . start2 <EOL> precontext = '<STR_LIT>' <EOL> while len ( bigpatch . diffs ) != <NUM_LIT:0> : <EOL> patch = patch_obj ( ) <EOL> empty = True <EOL> patch . start1 = start1 - len ( precontext ) <EOL> patch . start2 = start2 - len ( precontext ) <EOL> if precontext : <EOL> patch . length1 = patch . length2 = len ( precontext ) <EOL> patch . diffs . append ( ( self . DIFF_EQUAL , precontext ) ) <EOL> while ( len ( bigpatch . diffs ) != <NUM_LIT:0> and <EOL> patch . length1 < patch_size - self . Patch_Margin ) : <EOL> ( diff_type , diff_text ) = bigpatch . diffs [ <NUM_LIT:0> ] <EOL> if diff_type == self . DIFF_INSERT : <EOL> patch . length2 += len ( diff_text ) <EOL> start2 += len ( diff_text ) <EOL> patch . diffs . append ( bigpatch . diffs . pop ( <NUM_LIT:0> ) ) <EOL> empty = False <EOL> elif ( diff_type == self . DIFF_DELETE and len ( patch . diffs ) == <NUM_LIT:1> and <EOL> patch . diffs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == self . DIFF_EQUAL and <EOL> len ( diff_text ) > <NUM_LIT:2> * patch_size ) : <EOL> patch . length1 += len ( diff_text ) <EOL> start1 += len ( diff_text ) <EOL> empty = False <EOL> patch . diffs . append ( ( diff_type , diff_text ) ) <EOL> del bigpatch . diffs [ <NUM_LIT:0> ] <EOL> else : <EOL> diff_text = diff_text [ : patch_size - patch . length1 - <EOL> self . Patch_Margin ] <EOL> patch . length1 += len ( diff_text ) <EOL> start1 += len ( diff_text ) <EOL> if diff_type == self . DIFF_EQUAL : <EOL> patch . length2 += len ( diff_text ) <EOL> start2 += len ( diff_text ) <EOL> else : <EOL> empty = False <EOL> patch . diffs . append ( ( diff_type , diff_text ) ) <EOL> if diff_text == bigpatch . diffs [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] : <EOL> del bigpatch . diffs [ <NUM_LIT:0> ] <EOL> else : <EOL> bigpatch . diffs [ <NUM_LIT:0> ] = ( bigpatch . diffs [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , <EOL> bigpatch . diffs [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] [ len ( diff_text ) : ] ) <EOL> precontext = self . diff_text2 ( patch . diffs ) <EOL> precontext = precontext [ - self . Patch_Margin : ] <EOL> postcontext = self . diff_text1 ( bigpatch . diffs ) [ : self . Patch_Margin ] <EOL> if postcontext : <EOL> patch . length1 += len ( postcontext ) <EOL> patch . length2 += len ( postcontext ) <EOL> if len ( patch . diffs ) != <NUM_LIT:0> and patch . diffs [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] == self . DIFF_EQUAL : <EOL> patch . diffs [ - <NUM_LIT:1> ] = ( self . DIFF_EQUAL , patch . diffs [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] + <EOL> postcontext ) <EOL> else : <EOL> patch . diffs . append ( ( self . DIFF_EQUAL , postcontext ) ) <EOL> if not empty : <EOL> x += <NUM_LIT:1> <EOL> patches . insert ( x , patch ) <EOL> def patch_toText ( self , patches ) : <EOL> """<STR_LIT>""" <EOL> text = [ ] <EOL> for patch in patches : <EOL> text . append ( str ( patch ) ) <EOL> return "<STR_LIT>" . join ( text ) <EOL> def patch_fromText ( self , textline ) : <EOL> """<STR_LIT>""" <EOL> patches = [ ] <EOL> if not textline : <EOL> return patches <EOL> text = textline . split ( '<STR_LIT:\n>' ) <EOL> while len ( text ) != <NUM_LIT:0> : <EOL> m = re . match ( "<STR_LIT>" , text [ <NUM_LIT:0> ] ) <EOL> if not m : <EOL> raise ValueError ( "<STR_LIT>" + text [ <NUM_LIT:0> ] ) <EOL> patch = patch_obj ( ) <EOL> patches . append ( patch ) <EOL> patch . start1 = int ( m . group ( <NUM_LIT:1> ) ) <EOL> if m . group ( <NUM_LIT:2> ) == '<STR_LIT>' : <EOL> patch . start1 -= <NUM_LIT:1> <EOL> patch . length1 = <NUM_LIT:1> <EOL> elif m . group ( <NUM_LIT:2> ) == '<STR_LIT:0>' : <EOL> patch . length1 = <NUM_LIT:0> <EOL> else : <EOL> patch . start1 -= <NUM_LIT:1> <EOL> patch . length1 = int ( m . group ( <NUM_LIT:2> ) ) <EOL> patch . start2 = int ( m . group ( <NUM_LIT:3> ) ) <EOL> if m . group ( <NUM_LIT:4> ) == '<STR_LIT>' : <EOL> patch . start2 -= <NUM_LIT:1> <EOL> patch . length2 = <NUM_LIT:1> <EOL> elif m . group ( <NUM_LIT:4> ) == '<STR_LIT:0>' : <EOL> patch . length2 = <NUM_LIT:0> <EOL> else : <EOL> patch . start2 -= <NUM_LIT:1> <EOL> patch . length2 = int ( m . group ( <NUM_LIT:4> ) ) <EOL> del text [ <NUM_LIT:0> ] <EOL> while len ( text ) != <NUM_LIT:0> : <EOL> if text [ <NUM_LIT:0> ] : <EOL> sign = text [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> else : <EOL> sign = '<STR_LIT>' <EOL> line = urllib . parse . unquote ( text [ <NUM_LIT:0> ] [ <NUM_LIT:1> : ] ) <EOL> if sign == '<STR_LIT:+>' : <EOL> patch . diffs . append ( ( self . DIFF_INSERT , line ) ) <EOL> elif sign == '<STR_LIT:->' : <EOL> patch . diffs . append ( ( self . DIFF_DELETE , line ) ) <EOL> elif sign == '<STR_LIT:U+0020>' : <EOL> patch . diffs . append ( ( self . DIFF_EQUAL , line ) ) <EOL> elif sign == '<STR_LIT:@>' : <EOL> break <EOL> elif sign == '<STR_LIT>' : <EOL> pass <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % ( sign , line ) ) <EOL> del text [ <NUM_LIT:0> ] <EOL> return patches <EOL> class patch_obj : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> self . diffs = [ ] <EOL> self . start1 = None <EOL> self . start2 = None <EOL> self . length1 = <NUM_LIT:0> <EOL> self . length2 = <NUM_LIT:0> <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . length1 == <NUM_LIT:0> : <EOL> coords1 = str ( self . start1 ) + "<STR_LIT>" <EOL> elif self . length1 == <NUM_LIT:1> : <EOL> coords1 = str ( self . start1 + <NUM_LIT:1> ) <EOL> else : <EOL> coords1 = str ( self . start1 + <NUM_LIT:1> ) + "<STR_LIT:U+002C>" + str ( self . length1 ) <EOL> if self . length2 == <NUM_LIT:0> : <EOL> coords2 = str ( self . start2 ) + "<STR_LIT>" <EOL> elif self . length2 == <NUM_LIT:1> : <EOL> coords2 = str ( self . start2 + <NUM_LIT:1> ) <EOL> else : <EOL> coords2 = str ( self . start2 + <NUM_LIT:1> ) + "<STR_LIT:U+002C>" + str ( self . length2 ) <EOL> text = [ "<STR_LIT>" , coords1 , "<STR_LIT>" , coords2 , "<STR_LIT>" ] <EOL> for ( op , data ) in self . diffs : <EOL> if op == diff_match_patch . DIFF_INSERT : <EOL> text . append ( "<STR_LIT:+>" ) <EOL> elif op == diff_match_patch . DIFF_DELETE : <EOL> text . append ( "<STR_LIT:->" ) <EOL> elif op == diff_match_patch . DIFF_EQUAL : <EOL> text . append ( "<STR_LIT:U+0020>" ) <EOL> data = data . encode ( "<STR_LIT:utf-8>" ) <EOL> text . append ( urllib . parse . quote ( data , "<STR_LIT>" ) + "<STR_LIT:\n>" ) <EOL> return "<STR_LIT>" . join ( text ) </s>
