MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / difference

Method difference

src/licensedcode/dmp.py:129–184  ·  view source on GitHub ↗

Find the differences between two texts. Simplifies the problem by stripping any common prefix or suffix off the texts before diffing. Args: text1: Old string to be diffed. text2: New string to be diffed. deadline: Optional time when the diff s

(self, text1, text2, deadline=None)

Source from the content-addressed store, hash-verified

127 self.timeout = timeout
128
129 def difference(self, text1, text2, deadline=None):
130 """
131 Find the differences between two texts. Simplifies the problem by
132 stripping any common prefix or suffix off the texts before diffing.
133
134 Args:
135 text1: Old string to be diffed.
136 text2: New string to be diffed.
137 deadline: Optional time when the diff should be complete by. Used
138 internally for recursive calls. Users should set timeout instead.
139
140 Returns:
141 Array of changes.
142 """
143 if text1 == None or text2 == None:
144 raise ValueError('Illegal empty inputs')
145
146 # Check for equality (speedup).
147 if text1 == text2:
148 if text1:
149 return [(DIFF_EQUAL, text1)]
150 return []
151
152 # Set a deadline by which time the diff must be complete.
153 if deadline == None:
154 # Unlike in most languages, Python counts time in seconds.
155 if not self.timeout:
156 deadline = sys.maxsize
157 else:
158 deadline = time.time() + self.timeout
159
160 # Trim off common prefix (speedup).
161 commonlength = common_prefix(text1, text2)
162 commonprefix = text1[:commonlength]
163 text1 = text1[commonlength:]
164 text2 = text2[commonlength:]
165
166 # Trim off common suffix (speedup).
167 commonlength = common_suffix(text1, text2)
168 if commonlength == 0:
169 commonsuffix = ''
170 else:
171 commonsuffix = text1[-commonlength:]
172 text1 = text1[:-commonlength]
173 text2 = text2[:-commonlength]
174
175 # Compute the diff on the middle block.
176 diffs = self.compute(text1, text2, deadline)
177
178 # Restore the prefix and suffix.
179 if commonprefix:
180 diffs[:0] = [(DIFF_EQUAL, commonprefix)]
181 if commonsuffix:
182 diffs.append((DIFF_EQUAL, commonsuffix))
183 diffs = merge(diffs)
184 return diffs
185
186 def compute(self, text1, text2, deadline):

Callers 7

match_blocksFunction · 0.95
computeMethod · 0.95
bisect_splitMethod · 0.95
merge_licensesFunction · 0.45
loadMethod · 0.45
packageMethod · 0.45
package_infoMethod · 0.45

Calls 5

computeMethod · 0.95
mergeFunction · 0.85
common_prefixFunction · 0.70
common_suffixFunction · 0.70
appendMethod · 0.45

Tested by

no test coverage detected