public String diff_toDelta ( LinkedList < Diff > diffs ) { StringBuilder text = new StringBuilder ( ) ; for ( Diff aDiff : diffs ) { switch ( aDiff . operation ) { case INSERT : try { text . append ( "+" ) . append ( URLEncoder . encode ( aDiff . text , "UTF-8" ) . replace ( ' ' , ' ' ) ) . append ( "\t" ) ; } catch ( UnsupportedEncodingException e ) { // Not likely on modern system. throw new Error ( "This system does not support UTF-8." , e ) ; } break ; case DELETE : text . append ( "-" ) . append ( aDiff . text . length ( ) ) . append ( "\t" ) ; break ; case EQUAL : text . append ( "=" ) . append ( aDiff . text . length ( ) ) . append ( "\t" ) ; break ; } } String delta = text . toString ( ) ; if ( delta . length ( ) != 0 ) { // Strip off trailing tab character. delta = delta . substring ( 0 , delta . length ( ) - 1 ) ; delta = unescapeForEncodeUriCompatability ( delta ) ; } return delta ; }
Crush the diff into an encoded string which describes the operations required to transform text1 into text2 . E . g . = 3 \ t - 2 \ t + ing - > Keep 3 chars delete 2 chars insert ing . Operations are tab - separated . Inserted text is escaped using %xx notation .