private void timer1_Tick(object sender, EventArgs e) { lock (timer1) { if (resultQueue != null) { foreach (ResultItem item in resultQueue) { ListViewItem listItem = new ListViewItem(); listItem.Text = item.filename; DeltaInfo deltaInfo = DeltaInfo.Construct(item.before, item.after); if (deltaInfo != null) { if (deltaInfo.EndPosBefore - deltaInfo.StartPos > 0 && deltaInfo.EndPosAfter - deltaInfo.StartPos > 0) { listItem.SubItems.Add(item.before.Substring(deltaInfo.StartPos, deltaInfo.EndPosBefore - deltaInfo.StartPos).Trim()); listItem.SubItems.Add(item.after.Substring(deltaInfo.StartPos, deltaInfo.EndPosAfter - deltaInfo.StartPos).Trim()); occurrences += this.countDeltas(item.before, item.after); } else { listItem.Text = "ERROR" + listItem.Text; } Result.Items.Add(listItem); } } } resultQueue = null; } if (endSignalReceived) { StatusText.Text = "Done"; Go.Text = "Go"; timer1.Stop(); } else { if (currentlyScanning != "") { StatusText.Text = currentlyScanning.Substring(Path.Text.Length); } else { StatusText.Text = "Preparing..."; } } ProgressText.Text = String.Format("{0} scanned; {1} found in {2} files. ", scanned, occurrences, Result.Items.Count); }
private int countDeltas(string before, string after) { DeltaInfo deltaInfo = DeltaInfo.Construct(before, after); int deltas = 0; while (deltaInfo != null && deltaInfo.StartPos != deltaInfo.EndPosAfter && deltaInfo.StartPos != deltaInfo.EndPosBefore) { deltas++; before = before.Remove(deltaInfo.StartPos, deltaInfo.EndPosBefore - deltaInfo.StartPos); after = after.Remove(deltaInfo.StartPos, deltaInfo.EndPosAfter - deltaInfo.StartPos); deltaInfo = DeltaInfo.Construct(before, after); } return(deltas); }
public static DeltaInfo Construct(string before, string after) { if (before == after) { return(null); } char[] beforeArray = before.ToCharArray(); char[] afterArray = after.ToCharArray(); bool deltaFound = false; int startPos = 0; int endPosBefore = 0; int endPosAfter = 0; int minLength = Math.Min(beforeArray.Length, afterArray.Length); for (int pos = 0; pos < minLength; pos++) { if (!deltaFound && beforeArray[pos] != afterArray[pos]) { deltaFound = true; startPos = pos; } if (endPosBefore == 0 && deltaFound && (beforeArray[pos] == ';' || beforeArray[pos] == '\n')) { endPosBefore = pos; } if (endPosBefore == 0 && deltaFound && (beforeArray[pos] == '#')) { endPosBefore = pos - 11; } if (endPosAfter == 0 && deltaFound && (afterArray[pos] == ';' || afterArray[pos] == '\n')) { endPosAfter = pos; } if (endPosAfter == 0 && deltaFound && (afterArray[pos] == '#')) { endPosAfter = pos - 11; } if (endPosBefore != 0 && endPosAfter != 0) { break; } } for (; startPos > 0; startPos--) { if (afterArray[startPos] == '#') { break; } } startPos++; endPosBefore++; endPosAfter++; DeltaInfo deltaInfo = new DeltaInfo(); deltaInfo.StartPos = startPos; deltaInfo.EndPosAfter = endPosAfter; deltaInfo.EndPosBefore = endPosBefore; return(deltaInfo); }