예제 #1
0
        private Change ProcessCommand(string command)
        {
            Change change = new Change();

            int commandIndex = -1;
            for (int i = 0; i < command.Length; i++) {
                if (char.IsLetter(command[i])) {
                    commandIndex = i;
                }
            }
            if (commandIndex == -1 || command.Length <= commandIndex) throw new Exception(invalidOutputMessage);

            switch (command[commandIndex]) {
                case 'd': change.Type = ChangeType.Remove; break;
                case 'c': change.Type = ChangeType.Change; break;
                case 'a': change.Type = ChangeType.Add; break;
                default: throw new Exception(invalidOutputMessage);
            }

            string left = command.Substring(0, commandIndex);
            string right = command.Substring(commandIndex + 1);

            ProcessRange(left, out change.StartPosition1, out change.EndPosition1);
            ProcessRange(right, out change.StartPosition2, out change.EndPosition2);

            return change;
        }
예제 #2
0
        private void Diff(DiffDocument r, DiffDocument c, int i, int j)
        {
            if (i > 0 && j > 0 && r[i] == c[j]) {
                Diff(r, c, i - 1, j - 1);
            } else {
                if (j > 0 && (i == 0 || table[i][j - 1] >= table[i - 1][j])) {
                    Diff(r, c, i, j - 1);

                    if (lastChange == null || lastChange.Type != ChangeType.Add) {
                        if (lastChange != null) {
                            changeList.Add(lastChange);
                        }

                        lastChange = new Change() { Type = ChangeType.Add, StartPosition1 = i, StartPosition2 = j };
                    }
                    lastChange.TextAdded += c[j] + Environment.NewLine;
                } else if (i > 0 && (j == 0 || table[i][j - 1] < table[i - 1][j])) {
                    Diff(r, c, i - 1, j);

                    if (lastChange == null || lastChange.Type != ChangeType.Remove) {
                        if (lastChange != null) {
                            changeList.Add(lastChange);
                        }
                        lastChange = new Change() { Type = ChangeType.Remove, StartPosition1 = i, StartPosition2 = j };
                    }
                    lastChange.TextDeleted += r[i] + Environment.NewLine;
                }

                if (lastChange != null) {
                    lastChange.EndPosition1 = i;
                    lastChange.EndPosition2 = j;
                }
            }
        }