MCPcopy Create free account
hub / github.com/cogentcore/core / GetMatchingBlocks

Method GetMatchingBlocks

texteditor/difflib/difflib.go:310–361  ·  view source on GitHub ↗

GetMatchingBlocks returns a list of triples describing matching subsequences. Each triple is of the form (i, j, n), and means that a[i:i+n] == b[j:j+n]. The triples are monotonically increasing in i and in j. It's also guaranteed that if (i, j, n) and (i', j', n') are adjacent triples in the list,

()

Source from the content-addressed store, hash-verified

308// The last triple is a dummy, (len(a), len(b), 0), and is the only
309// triple with n==0.
310func (m *SequenceMatcher) GetMatchingBlocks() []Match {
311 if m.matchingBlocks != nil {
312 return m.matchingBlocks
313 }
314
315 var matchBlocks func(alo, ahi, blo, bhi int, matched []Match) []Match
316 matchBlocks = func(alo, ahi, blo, bhi int, matched []Match) []Match {
317 match := m.findLongestMatch(alo, ahi, blo, bhi)
318 i, j, k := match.A, match.B, match.Size
319 if match.Size > 0 {
320 if alo < i && blo < j {
321 matched = matchBlocks(alo, i, blo, j, matched)
322 }
323 matched = append(matched, match)
324 if i+k < ahi && j+k < bhi {
325 matched = matchBlocks(i+k, ahi, j+k, bhi, matched)
326 }
327 }
328 return matched
329 }
330 matched := matchBlocks(0, len(m.a), 0, len(m.b), nil)
331
332 // It's possible that we have adjacent equal blocks in the
333 // matching_blocks list now.
334 var nonAdjacent []Match
335 i1, j1, k1 := 0, 0, 0
336 for _, b := range matched {
337 // Is this block adjacent to i1, j1, k1?
338 i2, j2, k2 := b.A, b.B, b.Size
339 if i1+k1 == i2 && j1+k1 == j2 {
340 // Yes, so collapse them -- this just increases the length of
341 // the first block by the length of the second, and the first
342 // block so lengthened remains the block to compare against.
343 k1 += k2
344 } else {
345 // Not adjacent. Remember the first block (k1==0 means it's
346 // the dummy we started with), and make the second block the
347 // new block to compare against.
348 if k1 > 0 {
349 nonAdjacent = append(nonAdjacent, Match{i1, j1, k1})
350 }
351 i1, j1, k1 = i2, j2, k2
352 }
353 }
354 if k1 > 0 {
355 nonAdjacent = append(nonAdjacent, Match{i1, j1, k1})
356 }
357
358 nonAdjacent = append(nonAdjacent, Match{len(m.a), len(m.b), 0})
359 m.matchingBlocks = nonAdjacent
360 return m.matchingBlocks
361}
362
363// GetOpCodes returns a list of 5-tuples describing how to turn a into b.
364//

Callers 2

GetOpCodesMethod · 0.95
RatioMethod · 0.95

Calls 1

findLongestMatchMethod · 0.95

Tested by

no test coverage detected