0

我需要找到两个文件之间的逐行差异,并在两个文件的并排视图中显示输出,就像 GitHub 的文件差异部分中显示的那样。我为此目的使用以下库:

        <dependency>
            <groupId>org.bitbucket.cowwoc</groupId>
            <artifactId>diff-match-patch</artifactId>
            <version>1.1</version>
        </dependency>

我正在使用以下逻辑逐行计算差异并将它们放入数组中,以便可以使用 javascript 在 HTML 中显示。我在这里所做的是,如果在两行之间发现差异,则搜索匹配行直到一个文件末尾的算法。如果有这样的情况,将在另一个文件之间插入一个空行,就像 GitHub 的文件差异中所做的那样,在两个文件之间添加或删除行。下面提到的算法一直工作到一个点,当行数增加时,它的行为很奇怪,循环运行了无数次。

         try {
            int flag = 0;
            int a=0;
            while (true) {
                if (textOneLength > textTwoLength) {
                    max = textOneLength;
                } else {
                    max = textTwoLength;
                }
                if (a >= textOneLength) {
                    diffs = diffMatchPatch.diffMain(Constants.EMPTY_STRING, textOneLines[flag]);
                } else if (a >= textTwoLength) {
                    diffs = diffMatchPatch.diffMain(textOneLines[flag], Constants.EMPTY_STRING);
                } else {
                    diffs = diffMatchPatch.diffMain(textOneLines[flag], textTwoLines[flag]);
                }
                diffMatchPatch.diffCleanupSemantic(diffs);
                for (DiffMatchPatch.Diff aDiff : diffs) {
                    if (aDiff.operation.equals(DiffMatchPatch.Operation.INSERT) ||
                            aDiff.operation.equals(DiffMatchPatch.Operation.DELETE)) {
                        containsConflict = true;
                        for(int j=flag; j<textOneLength; j++) {
                            if(textTwoLines[flag].equals(textOneLines[j]) && !textOneLines[j].isEmpty()){
                                String tempArray[] = new String[textTwoLength+1];
                                for(int k=0; k<textTwoLength+1; k++) {
                                    if(k<a-1){
                                        tempArray[k] = textTwoLines[k];
                                    }
                                    else if(k==a) {
                                        tempArray[k] = Constants.EMPTY_STRING;
                                    } else {
                                        tempArray[k] = textTwoLines[k-1];
                                    }
                                }
                                textTwoLines = tempArray;
                                textTwoLength++;
                                break;
                            }
                        }
                        for(int j=flag; j<textTwoLength; j++) {
                            if(textOneLines[flag].equals(textTwoLines[j]) && !textTwoLines[j].isEmpty()){
                                String tempArray[] = new String[textOneLength+1];
                                for(int k=0; k<textOneLength+1; k++) {
                                    if(k<a-1){
                                        tempArray[k] = textOneLines[k];
                                    }
                                    else if(k==a) {
                                        tempArray[k] = Constants.EMPTY_STRING;
                                    } else {
                                        tempArray[k] = textOneLines[k-1];
                                    }
                                }
                                textOneLines = tempArray;
                                textOneLength++;
                                break;
                            }
                        }
                        break;
                    } else if (aDiff.operation.equals(DiffMatchPatch.Operation.EQUAL)) {
                        containsConflict = false;
                    }
                }
                if (a >= textOneLength) {
                    diffOutputs.add(new DiffOutput(Constants.EMPTY_STRING, textTwoLines[a], containsConflict));
                } else if (a >= textTwoLength) {
                    diffOutputs.add(new DiffOutput(textOneLines[a], Constants.EMPTY_STRING, containsConflict));
                } else {
                    diffOutputs.add(new DiffOutput(textOneLines[a], textTwoLines[a], containsConflict));
                }
                    flag++;
                a++;
                if(flag >= textOneLines.length || flag >= textTwoLines.length) {
                    break;
                }
            }
            return diffOutputs;
        } catch (Exception e) {
            logger.info("Exception occurred while performing diff analysis: %s", e);
        }

是否有任何其他算法可满足我的要求,或者有什么方法可以解决我的问题?

4

0 回答 0