src/main/java/com/sksamuel/diffpatch/DiffMatchPatch.java
public List<Patch> patch_fromText(String textline)
      throws IllegalArgumentException {
    List<Patch> patches = new LinkedList<Patch>();
    if (textline.length() == 0) {
      return patches;
    }
    List<String> textList = Arrays.asList(textline.split("\n"));
    LinkedList<String> text = new LinkedList<String>(textList);
    Patch patch;
    Pattern patchHeader
        = Pattern.compile("^@@ -(\\d+),?(\\d*) \\+(\\d+),?(\\d*) @@$");
    Matcher m;
    char sign;
    String line;
    while (!text.isEmpty()) {
      m = patchHeader.matcher(text.getFirst());
      if (!m.matches()) {
        throw new IllegalArgumentException(
            "Invalid patch string: " + text.getFirst());
      }
      patch = new Patch();
      patches.add(patch);
      patch.start1 = Integer.parseInt(m.group(1));
      if (m.group(2).length() == 0) {
        patch.start1--;
        patch.length1 = 1;
      } else if (m.group(2).equals("0")) {
        patch.length1 = 0;
      } else {
        patch.start1--;
        patch.length1 = Integer.parseInt(m.group(2));
      }

      patch.start2 = Integer.parseInt(m.group(3));
      if (m.group(4).length() == 0) {
        patch.start2--;
        patch.length2 = 1;
      } else if (m.group(4).equals("0")) {
        patch.length2 = 0;
      } else {
        patch.start2--;
        patch.length2 = Integer.parseInt(m.group(4));
      }
      text.removeFirst();

      while (!text.isEmpty()) {
        try {
          sign = text.getFirst().charAt(0);
        } catch (IndexOutOfBoundsException e) {
          // Blank line?  Whatever.
          text.removeFirst();
          continue;
        }
        line = text.getFirst().substring(1);
        line = line.replace("+", "%2B");  // decode would change all "+" to " "
        try {
          line = URLDecoder.decode(line, "UTF-8");
        } catch (UnsupportedEncodingException e) {
          // Not likely on modern system.
          throw new Error("This system does not support UTF-8.", e);
        } catch (IllegalArgumentException e) {
          // Malformed URI sequence.
          throw new IllegalArgumentException(
              "Illegal escape in patch_fromText: " + line, e);
        }
        if (sign == '-') {
          // Deletion.
          patch.diffs.add(new Diff(Operation.DELETE, line));
        } else if (sign == '+') {
          // Insertion.
          patch.diffs.add(new Diff(Operation.INSERT, line));
        } else if (sign == ' ') {
          // Minor equality.
          patch.diffs.add(new Diff(Operation.EQUAL, line));
        } else if (sign == '@') {
          // Start of next patch.
          break;
        } else {
          // WTF?
          throw new IllegalArgumentException(
              "Invalid patch mode '" + sign + "' in: " + line);
        }
        text.removeFirst();
      }
    }
    return patches;
  }
public List<Patch> patch_fromText(String textline)
      throws IllegalArgumentException {
    List<Patch> patches = new LinkedList<Patch>();
    if (textline.length() == 0) {
      return patches;
    }
    List<String> textList = Arrays.asList(textline.split("\n"));
    LinkedList<String> text = new LinkedList<String>(textList);
    Patch patch;
    Pattern patchHeader
        = Pattern.compile("^@@ -(\\d+),?(\\d*) \\+(\\d+),?(\\d*) @@$");
    Matcher m;
    char sign;
    String line;
    while (!text.isEmpty()) {
      m = patchHeader.matcher(text.getFirst());
      if (!m.matches()) {
        throw new IllegalArgumentException(
            "Invalid patch string: " + text.getFirst());
      }
      patch = new Patch();
      patches.add(patch);
      patch.start1 = Integer.parseInt(m.group(1));
      if (m.group(2).length() == 0) {
        patch.start1--;
        patch.length1 = 1;
      } else if (m.group(2).equals("0")) {
        patch.length1 = 0;
      } else {
        patch.start1--;
        patch.length1 = Integer.parseInt(m.group(2));
      }

      patch.start2 = Integer.parseInt(m.group(3));
      if (m.group(4).length() == 0) {
        patch.start2--;
        patch.length2 = 1;
      } else if (m.group(4).equals("0")) {
        patch.length2 = 0;
      } else {
        patch.start2--;
        patch.length2 = Integer.parseInt(m.group(4));
      }
      text.removeFirst();

      while (!text.isEmpty()) {
        try {
          sign = text.getFirst().charAt(0);
        } catch (IndexOutOfBoundsException e) {
          // Blank line?  Whatever.
          text.removeFirst();
          continue;
        }
        line = text.getFirst().substring(1);
        line = line.replace("+", "%2B");  // decode would change all "+" to " "
        try {
          line = URLDecoder.decode(line, "UTF-8");
        } catch (UnsupportedEncodingException e) {
          // Not likely on modern system.
          throw new Error("This system does not support UTF-8.", e);
        } catch (IllegalArgumentException e) {
          // Malformed URI sequence.
          throw new IllegalArgumentException(
              "Illegal escape in patch_fromText: " + line, e);
        }
        if (sign == '-') {
          // Deletion.
          patch.diffs.add(new Diff(Operation.DELETE, line));
        } else if (sign == '+') {
          // Insertion.
          patch.diffs.add(new Diff(Operation.INSERT, line));
        } else if (sign == ' ') {
          // Minor equality.
          patch.diffs.add(new Diff(Operation.EQUAL, line));
        } else if (sign == '@') {
          // Start of next patch.
          break;
        } else {
          // WTF?
          throw new IllegalArgumentException(
              "Invalid patch mode '" + sign + "' in: " + line);
        }
        text.removeFirst();
      }
    }
    return patches;
  }
public List < Patch > patch_fromText ( String textline ) throws IllegalArgumentException { List < Patch > patches = new LinkedList < Patch > ( ) ; if ( textline . length ( ) == 0 ) { return patches ; } List < String > textList = Arrays . asList ( textline . split ( "\n" ) ) ; LinkedList < String > text = new LinkedList < String > ( textList ) ; Patch patch ; Pattern patchHeader = Pattern . compile ( "^@@ -(\\d+),?(\\d*) \\+(\\d+),?(\\d*) @@$" ) ; Matcher m ; char sign ; String line ; while ( ! text . isEmpty ( ) ) { m = patchHeader . matcher ( text . getFirst ( ) ) ; if ( ! m . matches ( ) ) { throw new IllegalArgumentException ( "Invalid patch string: " + text . getFirst ( ) ) ; } patch = new Patch ( ) ; patches . add ( patch ) ; patch . start1 = Integer . parseInt ( m . group ( 1 ) ) ; if ( m . group ( 2 ) . length ( ) == 0 ) { patch . start1 -- ; patch . length1 = 1 ; } else if ( m . group ( 2 ) . equals ( "0" ) ) { patch . length1 = 0 ; } else { patch . start1 -- ; patch . length1 = Integer . parseInt ( m . group ( 2 ) ) ; } patch . start2 = Integer . parseInt ( m . group ( 3 ) ) ; if ( m . group ( 4 ) . length ( ) == 0 ) { patch . start2 -- ; patch . length2 = 1 ; } else if ( m . group ( 4 ) . equals ( "0" ) ) { patch . length2 = 0 ; } else { patch . start2 -- ; patch . length2 = Integer . parseInt ( m . group ( 4 ) ) ; } text . removeFirst ( ) ; while ( ! text . isEmpty ( ) ) { try { sign = text . getFirst ( ) . charAt ( 0 ) ; } catch ( IndexOutOfBoundsException e ) { // Blank line?  Whatever. text . removeFirst ( ) ; continue ; } line = text . getFirst ( ) . substring ( 1 ) ; line = line . replace ( "+" , "%2B" ) ; // decode would change all "+" to " " try { line = URLDecoder . decode ( line , "UTF-8" ) ; } catch ( UnsupportedEncodingException e ) { // Not likely on modern system. throw new Error ( "This system does not support UTF-8." , e ) ; } catch ( IllegalArgumentException e ) { // Malformed URI sequence. throw new IllegalArgumentException ( "Illegal escape in patch_fromText: " + line , e ) ; } if ( sign == ' ' ) { // Deletion. patch . diffs . add ( new Diff ( Operation . DELETE , line ) ) ; } else if ( sign == ' ' ) { // Insertion. patch . diffs . add ( new Diff ( Operation . INSERT , line ) ) ; } else if ( sign == ' ' ) { // Minor equality. patch . diffs . add ( new Diff ( Operation . EQUAL , line ) ) ; } else if ( sign == ' ' ) { // Start of next patch. break ; } else { // WTF? throw new IllegalArgumentException ( "Invalid patch mode '" + sign + "' in: " + line ) ; } text . removeFirst ( ) ; } } return patches ; }
Parse a textual representation of patches and return a List of Patch
objects.
@param textline Text representation of patches.
@return List of Patch objects.
@throws IllegalArgumentException If invalid input.
Parse a textual representation of patches and return a List of Patch objects .