difflib.Patch.applyTo()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(1.9k)|赞(0)|评价(0)|浏览(172)

本文整理了Java中difflib.Patch.applyTo()方法的一些代码示例,展示了Patch.applyTo()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Patch.applyTo()方法的具体详情如下:
包路径:difflib.Patch
类名称:Patch
方法名:applyTo

Patch.applyTo介绍

[英]Apply this patch to the given target
[中]将此修补程序应用于给定目标

代码示例

代码示例来源:origin: com.custardsource.dybdob/java-diff-utils-copy

/**
 * Patch the original text with given patch
 * 
 * @param original the original text
 * @param patch the given patch
 * @return the revised text
 * @throws PatchFailedException if can't apply patch
 */
public static List<?> patch(List<?> original, Patch patch) throws PatchFailedException {
  return patch.applyTo(original);
}

代码示例来源:origin: com.googlecode.java-diff-utils/diffutils

/**
 * Patch the original text with given patch
 * 
 * @param original
 *            the original text
 * @param patch
 *            the given patch
 * @return the revised text
 * @throws PatchFailedException
 *             if can't apply patch
 */
public static <T> List<T> patch(List<T> original, Patch<T> patch)
    throws PatchFailedException {
  return patch.applyTo(original);
}

代码示例来源:origin: line/centraldogma

@Test
public void testTextPatches() throws PatchFailedException {
  final String oriStr = "1\n2\n3\n4\n5\n6\n7\n8\n9";
  final String newStr = "1a\n2\n3\n4\n5\n6\n7\n8\n9a";
  final String expectedUnifiedDiff = "--- /text_file.txt\n" +
                    "+++ /text_file.txt\n" +
                    "@@ -1,4 +1,4 @@\n" +
                    "-1\n" +
                    "+1a\n" +
                    " 2\n" +
                    " 3\n" +
                    " 4\n" +
                    "@@ -6,4 +6,4 @@\n" +
                    " 6\n" +
                    " 7\n" +
                    " 8\n" +
                    "-9\n" +
                    "+9a";
  final Change<String> change = Change.ofTextPatch("/text_file.txt", oriStr, newStr);
  assertEquals(expectedUnifiedDiff, change.content());
  final Patch<String> patch = DiffUtils.parseUnifiedDiff(Util.stringToLines(change.content()));
  final String patchedStr = String.join("\n", patch.applyTo(Util.stringToLines(oriStr)));
  assertEquals(newStr, patchedStr);
}

相关文章