MCPcopy Create free account
hub / github.com/pmezard/go-difflib / GetOpCodes

Method GetOpCodes

difflib/difflib.go:373–407  ·  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 characters, with these meanings: '

()

Source from the content-addressed store, hash-verified

371//
372// 'e' (equal): a[i1:i2] == b[j1:j2]
373func (m *SequenceMatcher) GetOpCodes() []OpCode {
374 if m.opCodes != nil {
375 return m.opCodes
376 }
377 i, j := 0, 0
378 matching := m.GetMatchingBlocks()
379 opCodes := make([]OpCode, 0, len(matching))
380 for _, m := range matching {
381 // invariant: we've pumped out correct diffs to change
382 // a[:i] into b[:j], and the next matching block is
383 // a[ai:ai+size] == b[bj:bj+size]. So we need to pump
384 // out a diff to change a[i:ai] into b[j:bj], pump out
385 // the matching block, and move (i,j) beyond the match
386 ai, bj, size := m.A, m.B, m.Size
387 tag := byte(0)
388 if i < ai && j < bj {
389 tag = 'r'
390 } else if i < ai {
391 tag = 'd'
392 } else if j < bj {
393 tag = 'i'
394 }
395 if tag > 0 {
396 opCodes = append(opCodes, OpCode{tag, i, ai, j, bj})
397 }
398 i, j = ai+size, bj+size
399 // the list of matching blocks is terminated by a
400 // sentinel with size 0
401 if size > 0 {
402 opCodes = append(opCodes, OpCode{'e', ai, i, bj, j})
403 }
404 }
405 m.opCodes = opCodes
406 return m.opCodes
407}
408
409// Isolate change clusters by eliminating ranges with no changes.
410//

Callers 4

GetGroupedOpCodesMethod · 0.95
TestGetOptCodesFunction · 0.80
TestWithAsciiOneInsertFunction · 0.80
TestWithAsciiOnDeleteFunction · 0.80

Calls 1

GetMatchingBlocksMethod · 0.95

Tested by 3

TestGetOptCodesFunction · 0.64
TestWithAsciiOneInsertFunction · 0.64
TestWithAsciiOnDeleteFunction · 0.64