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

Function match_blocks

src/licensedcode/dmp.py:66–99  ·  view source on GitHub ↗

Return a list of matching block Match triples describing matching subsequences of `a` in `b` starting from the `a_start` position in `a` up to the `a_end` position in `a`.

(a, b, a_start, a_end, *args, **kwargs)

Source from the content-addressed store, hash-verified

64
65
66def match_blocks(a, b, a_start, a_end, *args, **kwargs):
67 """
68 Return a list of matching block Match triples describing matching
69 subsequences of `a` in `b` starting from the `a_start` position in `a` up to
70 the `a_end` position in `a`.
71 """
72 if TRACE:
73 logger_debug('a_start', a_start, 'a_end', a_end)
74 # convert sequences to strings
75 text1 = int2unicode(a[a_start:a_end])
76 text2 = int2unicode(b)
77
78 df = Differ(timeout=0.01)
79 diffs = df.difference(text1, text2)
80 diffs = trim(diffs)
81
82 apos = a_start
83 bpos = 0
84 matches = []
85 for op, matched_text in diffs:
86 size = len(matched_text)
87 if not size:
88 continue
89 if op == DIFF_EQUAL:
90 matches.append(Match(apos, bpos, size))
91 apos += size
92 bpos += size
93 elif op == DIFF_INSERT:
94 bpos += size
95
96 elif op == DIFF_DELETE:
97 apos += size
98
99 return matches
100
101
102def int2unicode(nums):

Callers

nothing calls this directly

Calls 7

differenceMethod · 0.95
int2unicodeFunction · 0.85
DifferClass · 0.85
trimFunction · 0.85
MatchFunction · 0.85
logger_debugFunction · 0.70
appendMethod · 0.45

Tested by

no test coverage detected