internal static void WriteUnified(StreamWriter stream, string[] a, string[] b, DiffItem[] items) { List <Hunk> hunkSet = new List <Hunk>(); for (int x = 0; x < items.Length; x++) { DiffItem item = items[x]; int prevDist = CONTEXT; if (x > 0) { prevDist = Math.Min(CONTEXT, item.StartA - (items[x - 1].StartA + items[x - 1].deletedA)); } int nextDist = CONTEXT; if (x < items.Length - 1) { nextDist = Math.Min(CONTEXT, items[x + 1].StartA - item.StartA - 1); } Hunk hunk = new Hunk(item, prevDist, nextDist, a.Length, b.Length); hunkSet.Add(hunk); if (nextDist == CONTEXT) { WriteHunkSet(stream, a, b, hunkSet); hunkSet.Clear(); } } WriteHunkSet(stream, a, b, hunkSet); }
internal static void WriteUnified(StreamWriter stream, string[] a, string[] b, DiffItem[] items) { List<Hunk> hunkSet = new List<Hunk>(); for (int x = 0; x < items.Length; x++) { DiffItem item = items[x]; int prevDist = CONTEXT; if (x > 0) prevDist = Math.Min(CONTEXT, item.StartA - (items[x - 1].StartA + items[x - 1].deletedA)); int nextDist = CONTEXT; if (x < items.Length - 1) nextDist = Math.Min(CONTEXT, items[x + 1].StartA - item.StartA - 1); Hunk hunk = new Hunk(item, prevDist, nextDist, a.Length, b.Length); hunkSet.Add(hunk); if (nextDist == CONTEXT) { WriteHunkSet(stream, a, b, hunkSet); hunkSet.Clear(); } } WriteHunkSet(stream, a, b, hunkSet); }
internal static void WriteNewFile(StreamWriter stream, string[] b) { DiffItem item = new DiffItem(); item.StartA = 0; item.StartB = 0; item.deletedA = 0; item.insertedB = b.Length; stream.WriteLine(String.Format("@@ -0,0 +1,{0} @@", b.Length)); Hunk hunk = new Hunk(item, 0, 0, 0, b.Length); stream.Write(hunk.ToString(new string[0], b)); }
} // LCS() /// <summary>Scan the tables of which lines are inserted and deleted, /// producing an edit script in forward order. /// </summary> /// dynamic array private static DiffItem[] CreateDiffs(DiffData DataA, DiffData DataB) { ArrayList a = new ArrayList(); DiffItem aItem; DiffItem []result; int StartA, StartB; int LineA, LineB; LineA = 0; LineB = 0; while (LineA < DataA.Length || LineB < DataB.Length) { if ((LineA < DataA.Length) && (! DataA.modified[LineA]) && (LineB < DataB.Length) && (! DataB.modified[LineB])) { // equal lines LineA++; LineB++; } else { // maybe deleted and/or inserted lines StartA = LineA; StartB = LineB; while (LineA < DataA.Length && (LineB >= DataB.Length || DataA.modified[LineA])) // while (LineA < DataA.Length && DataA.modified[LineA]) LineA++; while (LineB < DataB.Length && (LineA >= DataA.Length || DataB.modified[LineB])) // while (LineB < DataB.Length && DataB.modified[LineB]) LineB++; if ((StartA < LineA) || (StartB < LineB)) { // store a new difference-item aItem = new DiffItem(); aItem.StartA = StartA; aItem.StartB = StartB; aItem.deletedA = LineA - StartA; aItem.insertedB = LineB - StartB; a.Add(aItem); } // if } // if } // while result = new DiffItem[a.Count]; a.CopyTo(result); return (result); }
internal static void WriteHunkSet(StreamWriter stream, string[] a, string[] b, List <Hunk> hunkSet) { if (hunkSet.Count == 0) { return; } Hunk hunk1 = hunkSet[0]; DiffItem item1 = hunk1.Item; int ctxStartA = Math.Max(item1.StartA - CONTEXT, -1); int ctxStartB = Math.Max(item1.StartB - CONTEXT, -1); int linesA = 0; int linesB = 0; foreach (Hunk hunk in hunkSet) { linesA += hunk.LinesA; linesB += hunk.LinesB; } string header = String.Format("@@ -{0},{1} +", ctxStartA + 1, linesA); header += String.Format("{0},", ctxStartB + 1); header += String.Format("{0} @@", linesB); // header += String.Format("{0},{1},{2},{3}", // item1.StartA, item1.deletedA, item1.StartB, item1.insertedB); stream.WriteLine(header); foreach (Hunk hunk in hunkSet) { stream.Write(hunk.ToString(a, b)); } }
public Hunk(DiffItem item, int prevDist, int nextDist, int maxA, int maxB) { this.item = item; ctx1Start = Math.Max(item.StartA - prevDist, 0); int proposedEnd = Math.Min(item.StartA, ctx1Start + prevDist); ctx1End = Math.Min(proposedEnd, maxA); if (nextDist >= CONTEXT) { ctx2End = Math.Min(item.StartB + item.insertedB + nextDist, maxB); int proposedStart = Math.Max(item.StartB + item.insertedB, ctx2End - nextDist); //Console.WriteLine("proposedStart {0}, nextDist {1}", proposedStart, nextDist); ctx2Start = Math.Min(proposedStart, ctx2End); } ctxLineCnt = (ctx1End - ctx1Start) + (ctx2End - ctx2Start); //Console.WriteLine(String.Format("ctx1Start={0} ctx1End={1} ctx2Start={2} ctx2End={3} ctxLineCnt={4}\nmaxB={5} prevDist={6} nextDist={7}", // ctx1Start, ctx1End, ctx2Start, ctx2End, ctxLineCnt, maxB, prevDist, nextDist)); }