MCPcopy Index your code
hub / github.com/RustPython/RustPython / get_opcodes

Method get_opcodes

Lib/difflib.py:492–545  ·  view source on GitHub ↗

Return list of 5-tuples describing how to turn a into b. Each tuple is of the form (tag, i1, i2, j1, j2). The first tuple has i1 == j1 == 0, and remaining tuples have i1 == the i2 from the tuple preceding it, and likewise for j1 == the previous j2. The tags are str

(self)

Source from the content-addressed store, hash-verified

490 return self.matching_blocks
491
492 def get_opcodes(self):
493 """Return list of 5-tuples describing how to turn a into b.
494
495 Each tuple is of the form (tag, i1, i2, j1, j2). The first tuple
496 has i1 == j1 == 0, and remaining tuples have i1 == the i2 from the
497 tuple preceding it, and likewise for j1 == the previous j2.
498
499 The tags are strings, with these meanings:
500
501 'replace': a[i1:i2] should be replaced by b[j1:j2]
502 'delete': a[i1:i2] should be deleted.
503 Note that j1==j2 in this case.
504 'insert': b[j1:j2] should be inserted at a[i1:i1].
505 Note that i1==i2 in this case.
506 'equal': a[i1:i2] == b[j1:j2]
507
508 >>> a = "qabxcd"
509 >>> b = "abycdf"
510 >>> s = SequenceMatcher(None, a, b)
511 >>> for tag, i1, i2, j1, j2 in s.get_opcodes():
512 ... print(("%7s a[%d:%d] (%s) b[%d:%d] (%s)" %
513 ... (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2])))
514 delete a[0:1] (q) b[0:0] ()
515 equal a[1:3] (ab) b[0:2] (ab)
516 replace a[3:4] (x) b[2:3] (y)
517 equal a[4:6] (cd) b[3:5] (cd)
518 insert a[6:6] () b[5:6] (f)
519 """
520
521 if self.opcodes is not None:
522 return self.opcodes
523 i = j = 0
524 self.opcodes = answer = []
525 for ai, bj, size in self.get_matching_blocks():
526 # invariant: we've pumped out correct diffs to change
527 # a[:i] into b[:j], and the next matching block is
528 # a[ai:ai+size] == b[bj:bj+size]. So we need to pump
529 # out a diff to change a[i:ai] into b[j:bj], pump out
530 # the matching block, and move (i,j) beyond the match
531 tag = ''
532 if i < ai and j < bj:
533 tag = 'replace'
534 elif i < ai:
535 tag = 'delete'
536 elif j < bj:
537 tag = 'insert'
538 if tag:
539 answer.append( (tag, i, ai, j, bj) )
540 i, j = ai+size, bj+size
541 # the list of matching blocks is terminated by a
542 # sentinel with size 0
543 if size:
544 answer.append( ('equal', ai, i, bj, j) )
545 return answer
546
547 def get_grouped_opcodes(self, n=3):
548 """ Isolate change clusters by eliminating ranges with no changes.

Callers 7

get_grouped_opcodesMethod · 0.95
compareMethod · 0.95
_fancy_replaceMethod · 0.95
test_one_insertMethod · 0.95
test_one_deleteMethod · 0.95
test_opcode_cachingMethod · 0.95
test_recursion_limitMethod · 0.80

Calls 2

get_matching_blocksMethod · 0.95
appendMethod · 0.45

Tested by 4

test_one_insertMethod · 0.76
test_one_deleteMethod · 0.76
test_opcode_cachingMethod · 0.76
test_recursion_limitMethod · 0.64