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

Class Differ

src/licensedcode/dmp.py:123–384  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

121
122
123class Differ(object):
124
125 def __init__(self, timeout=0.1):
126 # Number of seconds to compute a diff before giving up (0 for infinity).
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)]

Callers 1

match_blocksFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected