Compute a list of patches to turn text1 into text2. text1 will be derived
from the provided diffs.

@param diffs
Array of Diff objects for text1 to text2.
@return LinkedList of Patch objects.
766219
public LinkedList<Patch> patch_make(LinkedList<Diff> diffs) {
        if (diffs == null) {
            throw new IllegalArgumentException("Null inputs. (patch_make)");
        }
        // No origin string provided, compute our own.
        String text1 = diff_text1(diffs);
        return patch_make(text1, diffs);
    }
213719
def diff_commonSuffix(self, text1, text2):
    
    # Quick check for common null cases.
    if not text1 or not text2 or text1[-1] != text2[-1]:
      return 0
    # Binary search.
    # Performance analysis: https://neil.fraser.name/news/2007/10/09/
    pointermin = 0
    pointermax = min(len(text1), len(text2))
    pointermid = pointermax
    pointerend = 0
    while pointermin < pointermid:
      if (text1[-pointermid:len(text1) - pointerend] ==
          text2[-pointermid:len(text2) - pointerend]):
        pointermin = pointermid
        pointerend = pointermin
      else:
        pointermax = pointermid
      pointermid = (pointermax - pointermin) // 2 + pointermin
    return pointermid
1149869
protected void patch_addContext(Patch patch, String text) {
        if (text.length() == 0) {
            return;
        }
        String pattern = text.substring(patch.start2, patch.start2
                + patch.length1);
        int padding = 0;

        // Look for the first and last matches of pattern in text. If two
        // different
        // matches are found, increase the pattern length.
        while (text.indexOf(pattern) != text.lastIndexOf(pattern)
                && pattern.length() < Match_MaxBits - Patch_Margin
                        - Patch_Margin) {
            padding += Patch_Margin;
            pattern = text.substring(
                    Math.max(0, patch.start2 - padding),
                    Math.min(text.length(), patch.start2 + patch.length1
                            + padding));
        }
        // Add one chunk for good luck.
        padding += Patch_Margin;

        // Add the prefix.
        String prefix = text.substring(Math.max(0, patch.start2 - padding),
                patch.start2);
        if (prefix.length() != 0) {
            patch.diffs.addFirst(new Diff(Operation.EQUAL, prefix));
        }
        // Add the suffix.
        String suffix = text
                .substring(
                        patch.start2 + patch.length1,
                        Math.min(text.length(), patch.start2 + patch.length1
                                + padding));
        if (suffix.length() != 0) {
            patch.diffs.addLast(new Diff(Operation.EQUAL, suffix));
        }

        // Roll back the start points.
        patch.start1 -= prefix.length();
        patch.start2 -= prefix.length();
        // Extend the lengths.
        patch.length1 += prefix.length() + suffix.length();
        patch.length2 += prefix.length() + suffix.length();
    }
822365
void charsToLines(LinkedList<Change> diffs,
                                     List<String> lineArray) {
        StringBuilder text;
        for (Change diff : diffs) {
            text = new StringBuilder();
            for (int y = 0; y < diff.text.length(); y++) {
                text.append(lineArray.get(diff.text.charAt(y)));
            }
            diff.text = text.toString();
        }
    }
575464
def diff_text1(self, diffs):
    
    text = []
    for (op, data) in diffs:
      if op != self.DIFF_INSERT:
        text.append(data)
    return "".join(text)
182173
private function patchMapOrList( array $base, Diff $diff ): array {
		if ( $diff->looksAssociative() ) {
			return $this->patch( $base, $diff );
		}

		return $this->listPatcher->patch( $base, $diff );
	}
439688
public String diff_text2(LinkedList<Diff> diffs) {
        StringBuilder text = new StringBuilder();
        for (Diff aDiff : diffs) {
            if (aDiff.operation != Operation.DELETE) {
                text.append(aDiff.text);
            }
        }
        return text.toString();
    }
1342251
private LinkedList<Change> compute(String text1, String text2, boolean checkLines) {
        LinkedList<Change> diffs = new LinkedList<>();

        if (text1.length() == 0) {
            // Just add some text (speedup).
            diffs.add(new Change(Operation.INSERT, text2));
            return diffs;
        }

        if (text2.length() == 0) {
            // Just delete some text (speedup).
            diffs.add(new Change(Operation.DELETE, text1));
            return diffs;
        }

        String longtext = text1.length() > text2.length() ? text1 : text2;
        String shorttext = text1.length() > text2.length() ? text2 : text1;
        int i = longtext.indexOf(shorttext);
        if (i != -1) {
            // Shorter text is inside the longer text (speedup).
            Operation op = (text1.length() > text2.length()) ?
                           Operation.DELETE : Operation.INSERT;
            diffs.add(new Change(op, longtext.substring(0, i)));
            diffs.add(new Change(Operation.EQUAL, shorttext));
            diffs.add(new Change(op, longtext.substring(i + shorttext.length())));
            return diffs;
        }

        if (shorttext.length() == 1) {
            // Single character string.
            // After the previous speedup, the character can't be an equality.
            diffs.add(new Change(Operation.DELETE, text1));
            diffs.add(new Change(Operation.INSERT, text2));
            return diffs;
        }

        // Check to see if the problem can be split in two.
        String[] hm = halfMatch(text1, text2);
        if (hm != null) {
            // A half-match was found, sort out the return data.
            String text1_a = hm[0];
            String text1_b = hm[1];
            String text2_a = hm[2];
            String text2_b = hm[3];
            String mid_common = hm[4];
            // Send both pairs off for separate processing.
            LinkedList<Change> diffs_a = main(text1_a, text2_a, checkLines);
            LinkedList<Change> diffs_b = main(text1_b, text2_b, checkLines);
            // Merge the results.
            diffs = diffs_a;
            diffs.add(new Change(Operation.EQUAL, mid_common));
            diffs.addAll(diffs_b);
            return diffs;
        }

        if (checkLines && text1.length() > 100 && text2.length() > 100) {
            return lineMode(text1, text2);
        }

        return bisect(text1, text2);
    }
890617
public FulltextMatch setPatterns(
                    final Collection<String> patterns
                    )
    {
        if (patterns == null  ||  patterns.size() == 0) {
            clearPatterns();
            return this;
        }

        synchronized (patterns) {
            for (String  pattern : patterns) {
                if (pattern == null  ||  pattern.length() == 0) {
                    throw new IllegalArgumentException( "null element" );
                }
            }

            clearPatterns();
            for (String  pattern : patterns) {
                addPattern( pattern );
            }
        }

        return this;
    }
1263591
public static String[] difference(final String[] list1, final String[] list2) {
        HashSet<String> set = new HashSet<String>();
        HashSet<String> set1 = new HashSet<String>(Arrays.asList(list1));
        HashSet<String> set2 = new HashSet<String>(Arrays.asList(list2));
        for (final String s : list1) {
            if (!set2.contains(s)) {
                set.add(s);
            }
        }
        for (final String s : list2) {
            if (!set1.contains(s)) {
                set.add(s);
            }
        }
        return set.toArray(new String[set.size()]);
    }
999085
public int diff_levenshtein(LinkedList<Diff> diffs) {
        int levenshtein = 0;
        int insertions = 0;
        int deletions = 0;
        for (Diff aDiff : diffs) {
            switch (aDiff.operation) {
            case INSERT:
                insertions += aDiff.text.length();
                break;
            case DELETE:
                deletions += aDiff.text.length();
                break;
            case EQUAL:
                // A deletion and an insertion is one substitution.
                levenshtein += Math.max(insertions, deletions);
                insertions = 0;
                deletions = 0;
                break;
            }
        }
        levenshtein += Math.max(insertions, deletions);
        return levenshtein;
    }
994296
public ReplaceInList repairCommand(final ReplaceInList toRepair, final AddToList repairAgainst) {
        if (repairAgainst.getPosition() > toRepair.getPosition()) {
            return toRepair;
        }
        return new ReplaceInList(toRepair.getListId(), toRepair.getListVersionChange(), toRepair.getValue(),
                toRepair.getPosition() + 1);
    }
1150841
public static Optional<Integer> findLineInDiff(
      final String patchString, final Integer lineToComment) {
    if (patchString == null) {
      return Optional.empty();
    }
    int currentLine = -1;
    int patchLocation = 0;
    for (final String line : patchString.split("\n")) {
      if (line.startsWith("@")) {
        final Matcher matcher =
            Pattern.compile(
                    "@@\\p{IsWhite_Space}-[0-9]+(?:,[0-9]+)?\\p{IsWhite_Space}\\+([0-9]+)(?:,[0-9]+)?\\p{IsWhite_Space}@@.*")
                .matcher(line);
        if (!matcher.matches()) {
          throw new IllegalStateException(
              "Unable to parse patch line " + line + "\nFull patch: \n" + patchString);
        }
        currentLine = Integer.parseInt(matcher.group(1));
      } else if (line.startsWith("+") || line.startsWith(" ")) {
        // Added or unmodified
        if (currentLine == lineToComment) {
          return Optional.ofNullable(patchLocation);
        }
        currentLine++;
      }
      patchLocation++;
    }
    return Optional.empty();
  }
1279383
public List<String> getTextValuesForPatternName(final String name) {
        List<String> result = new ArrayList<String>();
        List<Node> jars = this.get(name);
        for (Node node : jars) {
            String text = node.getText();
            if (text != null) {
                result.add(text);
            }
        }
        return Collections.unmodifiableList(result);
    }
89575
@MapMethod
    public List<Map<String,Object>> diff(Map<String,Object> old, Map<String,Object> nw){
        List<ObjectDiff.DiffBean> l = Objects.diff(old,nw);
        List<Map<String,Object>> list = Objects.newArrayList();
        for(ObjectDiff.DiffBean b:l){
            list.add(Objects.newHashMap(String.class,Object.class,
                    "path",b.getPath(),"old",b.getOldValue(),"new",b.getNewValue()
            ));
        }
        return list;
    }
282882
public final void replace(final List<Resource> r1, final List<Resource> r2) {
    this.resources.removeAll(r1);
    this.resources.addAll(r2);
  }
1101284
public static DiffSet diffSet(Map<String, Object> introspectionOld, Map<String, Object> introspectionNew) {
        return new DiffSet(introspectionOld, introspectionNew);
    }