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

Method GetGroupedOpCodes

difflib/difflib.go:413–452  ·  view source on GitHub ↗

Isolate change clusters by eliminating ranges with no changes. Return a generator of groups with up to n lines of context. Each group is in the same format as returned by GetOpCodes().

(n int)

Source from the content-addressed store, hash-verified

411// Return a generator of groups with up to n lines of context.
412// Each group is in the same format as returned by GetOpCodes().
413func (m *SequenceMatcher) GetGroupedOpCodes(n int) [][]OpCode {
414 if n < 0 {
415 n = 3
416 }
417 codes := m.GetOpCodes()
418 if len(codes) == 0 {
419 codes = []OpCode{OpCode{'e', 0, 1, 0, 1}}
420 }
421 // Fixup leading and trailing groups if they show no changes.
422 if codes[0].Tag == 'e' {
423 c := codes[0]
424 i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2
425 codes[0] = OpCode{c.Tag, max(i1, i2-n), i2, max(j1, j2-n), j2}
426 }
427 if codes[len(codes)-1].Tag == 'e' {
428 c := codes[len(codes)-1]
429 i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2
430 codes[len(codes)-1] = OpCode{c.Tag, i1, min(i2, i1+n), j1, min(j2, j1+n)}
431 }
432 nn := n + n
433 groups := [][]OpCode{}
434 group := []OpCode{}
435 for _, c := range codes {
436 i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2
437 // End the current group and start a new one whenever
438 // there is a large range with no changes.
439 if c.Tag == 'e' && i2-i1 > nn {
440 group = append(group, OpCode{c.Tag, i1, min(i2, i1+n),
441 j1, min(j2, j1+n)})
442 groups = append(groups, group)
443 group = []OpCode{}
444 i1, j1 = max(i1, i2-n), max(j1, j2-n)
445 }
446 group = append(group, OpCode{c.Tag, i1, i2, j1, j2})
447 }
448 if len(group) > 0 && !(len(group) == 1 && group[0].Tag == 'e') {
449 groups = append(groups, group)
450 }
451 return groups
452}
453
454// Return a measure of the sequences' similarity (float in [0,1]).
455//

Callers 4

TestGroupedOpCodesFunction · 0.80
WriteUnifiedDiffFunction · 0.80
WriteContextDiffFunction · 0.80

Calls 3

GetOpCodesMethod · 0.95
maxFunction · 0.85
minFunction · 0.85

Tested by 2

TestGroupedOpCodesFunction · 0.64