public LinkedList<Patch> patch_make(String text1, LinkedList<Diff> diffs) {
    if (text1 == null || diffs == null) {
      throw new IllegalArgumentException("Null inputs. (patch_make)");
    }

    LinkedList<Patch> patches = new LinkedList<Patch>();
    if (diffs.isEmpty()) {
      return patches;  // Get rid of the null case.
    }
    Patch patch = new Patch();
    int char_count1 = 0;  // Number of characters into the text1 string.
    int char_count2 = 0;  // Number of characters into the text2 string.
    // Start with text1 (prepatch_text) and apply the diffs until we arrive at
    // text2 (postpatch_text). We recreate the patches one by one to determine
    // context info.
    String prepatch_text = text1;
    String postpatch_text = text1;
    for (Diff aDiff : diffs) {
      if (patch.diffs.isEmpty() && aDiff.operation != Operation.EQUAL) {
        // A new patch starts here.
        patch.start1 = char_count1;
        patch.start2 = char_count2;
      }

      switch (aDiff.operation) {
      case INSERT:
        patch.diffs.add(aDiff);
        patch.length2 += aDiff.text.length();
        postpatch_text = postpatch_text.substring(0, char_count2)
            + aDiff.text + postpatch_text.substring(char_count2);
        break;
      case DELETE:
        patch.length1 += aDiff.text.length();
        patch.diffs.add(aDiff);
        postpatch_text = postpatch_text.substring(0, char_count2)
            + postpatch_text.substring(char_count2 + aDiff.text.length());
        break;
      case EQUAL:
        if (aDiff.text.length() <= 2 * Patch_Margin
            && !patch.diffs.isEmpty() && aDiff != diffs.getLast()) {
          // Small equality inside a patch.
          patch.diffs.add(aDiff);
          patch.length1 += aDiff.text.length();
          patch.length2 += aDiff.text.length();
        }

        if (aDiff.text.length() >= 2 * Patch_Margin) {
          // Time for a new patch.
          if (!patch.diffs.isEmpty()) {
            patch_addContext(patch, prepatch_text);
            patches.add(patch);
            patch = new Patch();
            // Unlike Unidiff, our patch lists have a rolling context.
            // http://code.google.com/p/google-diff-match-patch/wiki/Unidiff
            // Update prepatch text & pos to reflect the application of the
            // just completed patch.
            prepatch_text = postpatch_text;
            char_count1 = char_count2;
          }
        }
        break;
      }

      // Update the current character count.
      if (aDiff.operation != Operation.INSERT) {
        char_count1 += aDiff.text.length();
      }
      if (aDiff.operation != Operation.DELETE) {
        char_count2 += aDiff.text.length();
      }
    }
    // Pick up the leftover patch if not empty.
    if (!patch.diffs.isEmpty()) {
      patch_addContext(patch, prepatch_text);
      patches.add(patch);
    }

    return patches;
  }
public LinkedList<Patch> patch_make(String text1, LinkedList<Diff> diffs) {
    if (text1 == null || diffs == null) {
      throw new IllegalArgumentException("Null inputs. (patch_make)");
    }

    LinkedList<Patch> patches = new LinkedList<Patch>();
    if (diffs.isEmpty()) {
      return patches;  
    }
    Patch patch = new Patch();
    int char_count1 = 0;  
    int char_count2 = 0;  
    
    
    
    String prepatch_text = text1;
    String postpatch_text = text1;
    for (Diff aDiff : diffs) {
      if (patch.diffs.isEmpty() && aDiff.operation != Operation.EQUAL) {
        
        patch.start1 = char_count1;
        patch.start2 = char_count2;
      }

      switch (aDiff.operation) {
      case INSERT:
        patch.diffs.add(aDiff);
        patch.length2 += aDiff.text.length();
        postpatch_text = postpatch_text.substring(0, char_count2)
            + aDiff.text + postpatch_text.substring(char_count2);
        break;
      case DELETE:
        patch.length1 += aDiff.text.length();
        patch.diffs.add(aDiff);
        postpatch_text = postpatch_text.substring(0, char_count2)
            + postpatch_text.substring(char_count2 + aDiff.text.length());
        break;
      case EQUAL:
        if (aDiff.text.length() <= 2 * Patch_Margin
            && !patch.diffs.isEmpty() && aDiff != diffs.getLast()) {
          
          patch.diffs.add(aDiff);
          patch.length1 += aDiff.text.length();
          patch.length2 += aDiff.text.length();
        }

        if (aDiff.text.length() >= 2 * Patch_Margin) {
          
          if (!patch.diffs.isEmpty()) {
            patch_addContext(patch, prepatch_text);
            patches.add(patch);
            patch = new Patch();
            
            
            
            
            prepatch_text = postpatch_text;
            char_count1 = char_count2;
          }
        }
        break;
      }

      
      if (aDiff.operation != Operation.INSERT) {
        char_count1 += aDiff.text.length();
      }
      if (aDiff.operation != Operation.DELETE) {
        char_count2 += aDiff.text.length();
      }
    }
    
    if (!patch.diffs.isEmpty()) {
      patch_addContext(patch, prepatch_text);
      patches.add(patch);
    }

    return patches;
  }
public LinkedList < Patch > patch_make ( String text1 , LinkedList < Diff > diffs ) { if ( text1 == null || diffs == null ) { throw new IllegalArgumentException ( "Null inputs. (patch_make)" ) ; } LinkedList < Patch > patches = new LinkedList < Patch > ( ) ; if ( diffs . isEmpty ( ) ) { return patches ; // Get rid of the null case. } Patch patch = new Patch ( ) ; int char_count1 = 0 ; // Number of characters into the text1 string. int char_count2 = 0 ; // Number of characters into the text2 string. // Start with text1 (prepatch_text) and apply the diffs until we arrive at // text2 (postpatch_text). We recreate the patches one by one to determine // context info. String prepatch_text = text1 ; String postpatch_text = text1 ; for ( Diff aDiff : diffs ) { if ( patch . diffs . isEmpty ( ) && aDiff . operation != Operation . EQUAL ) { // A new patch starts here. patch . start1 = char_count1 ; patch . start2 = char_count2 ; } switch ( aDiff . operation ) { case INSERT : patch . diffs . add ( aDiff ) ; patch . length2 += aDiff . text . length ( ) ; postpatch_text = postpatch_text . substring ( 0 , char_count2 ) + aDiff . text + postpatch_text . substring ( char_count2 ) ; break ; case DELETE : patch . length1 += aDiff . text . length ( ) ; patch . diffs . add ( aDiff ) ; postpatch_text = postpatch_text . substring ( 0 , char_count2 ) + postpatch_text . substring ( char_count2 + aDiff . text . length ( ) ) ; break ; case EQUAL : if ( aDiff . text . length ( ) <= 2 * Patch_Margin && ! patch . diffs . isEmpty ( ) && aDiff != diffs . getLast ( ) ) { // Small equality inside a patch. patch . diffs . add ( aDiff ) ; patch . length1 += aDiff . text . length ( ) ; patch . length2 += aDiff . text . length ( ) ; } if ( aDiff . text . length ( ) >= 2 * Patch_Margin ) { // Time for a new patch. if ( ! patch . diffs . isEmpty ( ) ) { patch_addContext ( patch , prepatch_text ) ; patches . add ( patch ) ; patch = new Patch ( ) ; // Unlike Unidiff, our patch lists have a rolling context. // http://code.google.com/p/google-diff-match-patch/wiki/Unidiff // Update prepatch text & pos to reflect the application of the // just completed patch. prepatch_text = postpatch_text ; char_count1 = char_count2 ; } } break ; } // Update the current character count. if ( aDiff . operation != Operation . INSERT ) { char_count1 += aDiff . text . length ( ) ; } if ( aDiff . operation != Operation . DELETE ) { char_count2 += aDiff . text . length ( ) ; } } // Pick up the leftover patch if not empty. if ( ! patch . diffs . isEmpty ( ) ) { patch_addContext ( patch , prepatch_text ) ; patches . add ( patch ) ; } return patches ; }
Compute a list of patches to turn text1 into text2.
text2 is not provided, diffs are the delta between text1 and text2.
@param text1 Old text.
@param diffs Array of Diff objects for text1 to text2.
@return LinkedList of Patch objects.
Compute a list of patches to turn text1 into text2 . text2 is not provided diffs are the delta between text1 and text2 .