public static Patch<String> parseUnifiedDiff(List<String> diff){
  boolean inPrelude=true;
  List<String[]> rawChunk=new ArrayList<String[]>();
  Patch<String> patch=new Patch<String>();
  int old_ln=0, new_ln=0;
  String tag;
  String rest;
  for (  String line : diff) {
    if (inPrelude) {
      if (line.startsWith("+++")) {
        inPrelude=false;
      }
      continue;
    }
    Matcher m=unifiedDiffChunkRe.matcher(line);
    if (m.find()) {
      if (rawChunk.size() != 0) {
        List<String> oldChunkLines=new ArrayList<String>();
        List<String> newChunkLines=new ArrayList<String>();
        for (        String[] raw_line : rawChunk) {
          tag=raw_line[0];
          rest=raw_line[1];
          if (tag.equals(" ") || tag.equals("-")) {
            oldChunkLines.add(rest);
          }
          if (tag.equals(" ") || tag.equals("+")) {
            newChunkLines.add(rest);
          }
        }
        patch.addDelta(new ChangeDelta<String>(new Chunk<String>(old_ln - 1,oldChunkLines),new Chunk<String>(new_ln - 1,newChunkLines)));
        rawChunk.clear();
      }
      old_ln=m.group(1) == null ? 1 : Integer.parseInt(m.group(1));
      new_ln=m.group(3) == null ? 1 : Integer.parseInt(m.group(3));
      if (old_ln == 0) {
        old_ln+=1;
      }
      if (new_ln == 0) {
        new_ln+=1;
      }
    }
 else {
      if (line.length() > 0) {
        tag=line.substring(0,1);
        rest=line.substring(1);
        if (tag.equals(" ") || tag.equals("+") || tag.equals("-")) {
          rawChunk.add(new String[]{tag,rest});
        }
      }
 else {
        rawChunk.add(new String[]{" ",""});
      }
    }
  }
  if (rawChunk.size() != 0) {
    List<String> oldChunkLines=new ArrayList<String>();
    List<String> newChunkLines=new ArrayList<String>();
    for (    String[] raw_line : rawChunk) {
      tag=raw_line[0];
      rest=raw_line[1];
      if (tag.equals(" ") || tag.equals("-")) {
        oldChunkLines.add(rest);
      }
      if (tag.equals(" ") || tag.equals("+")) {
        newChunkLines.add(rest);
      }
    }
    patch.addDelta(new ChangeDelta<String>(new Chunk<String>(old_ln - 1,oldChunkLines),new Chunk<String>(new_ln - 1,newChunkLines)));
    rawChunk.clear();
  }
  return patch;
}

public static Patch < String > parseUnifiedDiff ( List < String > diff ) { boolean inPrelude = true ; List < String [ ] > rawChunk = new ArrayList < String [ ] > ( ) ; Patch < String > patch = new Patch < String > ( ) ; int old_ln = 0 , new_ln = 0 ; String tag ; String rest ; for ( String line : diff ) { if ( inPrelude ) { if ( line . startsWith ( "+++" ) ) { inPrelude = false ; } continue ; } Matcher m = unifiedDiffChunkRe . matcher ( line ) ; if ( m . find ( ) ) { if ( rawChunk . size ( ) != 0 ) { List < String > oldChunkLines = new ArrayList < String > ( ) ; List < String > newChunkLines = new ArrayList < String > ( ) ; for ( String [ ] raw_line : rawChunk ) { tag = raw_line [ 0 ] ; rest = raw_line [ 1 ] ; if ( tag . equals ( " " ) || tag . equals ( "-" ) ) { oldChunkLines . add ( rest ) ; } if ( tag . equals ( " " ) || tag . equals ( "+" ) ) { newChunkLines . add ( rest ) ; } } patch . addDelta ( new ChangeDelta < String > ( new Chunk < String > ( old_ln - 1 , oldChunkLines ) , new Chunk < String > ( new_ln - 1 , newChunkLines ) ) ) ; rawChunk . clear ( ) ; } old_ln = m . group ( 1 ) == null ? 1 : Integer . parseInt ( m . group ( 1 ) ) ; new_ln = m . group ( 3 ) == null ? 1 : Integer . parseInt ( m . group ( 3 ) ) ; if ( old_ln == 0 ) { old_ln += 1 ; } if ( new_ln == 0 ) { new_ln += 1 ; } } else { if ( line . length ( ) > 0 ) { tag = line . substring ( 0 , 1 ) ; rest = line . substring ( 1 ) ; if ( tag . equals ( " " ) || tag . equals ( "+" ) || tag . equals ( "-" ) ) { rawChunk . add ( new String [ ] { tag , rest } ) ; } } else { rawChunk . add ( new String [ ] { " " , "" } ) ; } } } if ( rawChunk . size ( ) != 0 ) { List < String > oldChunkLines = new ArrayList < String > ( ) ; List < String > newChunkLines = new ArrayList < String > ( ) ; for ( String [ ] raw_line : rawChunk ) { tag = raw_line [ 0 ] ; rest = raw_line [ 1 ] ; if ( tag . equals ( " " ) || tag . equals ( "-" ) ) { oldChunkLines . add ( rest ) ; } if ( tag . equals ( " " ) || tag . equals ( "+" ) ) { newChunkLines . add ( rest ) ; } } patch . addDelta ( new ChangeDelta < String > ( new Chunk < String > ( old_ln - 1 , oldChunkLines ) , new Chunk < String > ( new_ln - 1 , newChunkLines ) ) ) ; rawChunk . clear ( ) ; } return patch ; }
parse the given text in unified format and creates the list of deltas for it .
