MCPcopy Create free account
hub / github.com/RT-Thread/env-windows / unified_diff

Function unified_diff

tools/python-3.11.9-amd64/Lib/difflib.py:1095–1161  ·  view source on GitHub ↗

r""" Compare two sequences of lines; generate the delta as a unified diff. Unified diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by 'n' which defaults to three. By default, the diff control lines (those wi

(a, b, fromfile='', tofile='', fromfiledate='',
                 tofiledate='', n=3, lineterm='\n')

Source from the content-addressed store, hash-verified

1093 return '{},{}'.format(beginning, length)
1094
1095def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
1096 tofiledate='', n=3, lineterm='\n'):
1097 r"""
1098 Compare two sequences of lines; generate the delta as a unified diff.
1099
1100 Unified diffs are a compact way of showing line changes and a few
1101 lines of context. The number of context lines is set by 'n' which
1102 defaults to three.
1103
1104 By default, the diff control lines (those with ---, +++, or @@) are
1105 created with a trailing newline. This is helpful so that inputs
1106 created from file.readlines() result in diffs that are suitable for
1107 file.writelines() since both the inputs and outputs have trailing
1108 newlines.
1109
1110 For inputs that do not have trailing newlines, set the lineterm
1111 argument to "" so that the output will be uniformly newline free.
1112
1113 The unidiff format normally has a header for filenames and modification
1114 times. Any or all of these may be specified using strings for
1115 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
1116 The modification times are normally expressed in the ISO 8601 format.
1117
1118 Example:
1119
1120 >>> for line in unified_diff('one two three four'.split(),
1121 ... 'zero one tree four'.split(), 'Original', 'Current',
1122 ... '2005-01-26 23:30:50', '2010-04-02 10:20:52',
1123 ... lineterm=''):
1124 ... print(line) # doctest: +NORMALIZE_WHITESPACE
1125 --- Original 2005-01-26 23:30:50
1126 +++ Current 2010-04-02 10:20:52
1127 @@ -1,4 +1,4 @@
1128 +zero
1129 one
1130 -two
1131 -three
1132 +tree
1133 four
1134 """
1135
1136 _check_types(a, b, fromfile, tofile, fromfiledate, tofiledate, lineterm)
1137 started = False
1138 for group in SequenceMatcher(None,a,b).get_grouped_opcodes(n):
1139 if not started:
1140 started = True
1141 fromdate = '\t{}'.format(fromfiledate) if fromfiledate else ''
1142 todate = '\t{}'.format(tofiledate) if tofiledate else ''
1143 yield '--- {}{}{}'.format(fromfile, fromdate, lineterm)
1144 yield '+++ {}{}{}'.format(tofile, todate, lineterm)
1145
1146 first, last = group[0], group[-1]
1147 file1_range = _format_range_unified(first[1], last[2])
1148 file2_range = _format_range_unified(first[3], last[4])
1149 yield '@@ -{} +{} @@{}'.format(file1_range, file2_range, lineterm)
1150
1151 for tag, i1, i2, j1, j2 in group:
1152 if tag == 'equal':

Callers

nothing calls this directly

Calls 5

_check_typesFunction · 0.85
SequenceMatcherClass · 0.85
_format_range_unifiedFunction · 0.85
get_grouped_opcodesMethod · 0.80
formatMethod · 0.45

Tested by

no test coverage detected