MCPcopy Index your code
hub / github.com/expr-lang/expr / GetMatchingBlocks

Method GetMatchingBlocks

internal/difflib/difflib.go:308–359  ·  view source on GitHub ↗

Return 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, and the second is no

()

Source from the content-addressed store, hash-verified

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

Callers 2

GetOpCodesMethod · 0.95
RatioMethod · 0.95

Calls 1

findLongestMatchMethod · 0.95

Tested by

no test coverage detected