Find longest matching block in a[alo:ahi] and b[blo:bhi]. By default it will find the longest match in the entirety of a and b. If isjunk is not defined: Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where alo <= i <= i+k <= ahi blo <= j <
(self, alo=0, ahi=None, blo=0, bhi=None)
| 304 | del b2j[elt] |
| 305 | |
| 306 | def find_longest_match(self, alo=0, ahi=None, blo=0, bhi=None): |
| 307 | """Find longest matching block in a[alo:ahi] and b[blo:bhi]. |
| 308 | |
| 309 | By default it will find the longest match in the entirety of a and b. |
| 310 | |
| 311 | If isjunk is not defined: |
| 312 | |
| 313 | Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where |
| 314 | alo <= i <= i+k <= ahi |
| 315 | blo <= j <= j+k <= bhi |
| 316 | and for all (i',j',k') meeting those conditions, |
| 317 | k >= k' |
| 318 | i <= i' |
| 319 | and if i == i', j <= j' |
| 320 | |
| 321 | In other words, of all maximal matching blocks, return one that |
| 322 | starts earliest in a, and of all those maximal matching blocks that |
| 323 | start earliest in a, return the one that starts earliest in b. |
| 324 | |
| 325 | >>> s = SequenceMatcher(None, " abcd", "abcd abcd") |
| 326 | >>> s.find_longest_match(0, 5, 0, 9) |
| 327 | Match(a=0, b=4, size=5) |
| 328 | |
| 329 | If isjunk is defined, first the longest matching block is |
| 330 | determined as above, but with the additional restriction that no |
| 331 | junk element appears in the block. Then that block is extended as |
| 332 | far as possible by matching (only) junk elements on both sides. So |
| 333 | the resulting block never matches on junk except as identical junk |
| 334 | happens to be adjacent to an "interesting" match. |
| 335 | |
| 336 | Here's the same example as before, but considering blanks to be |
| 337 | junk. That prevents " abcd" from matching the " abcd" at the tail |
| 338 | end of the second sequence directly. Instead only the "abcd" can |
| 339 | match, and matches the leftmost "abcd" in the second sequence: |
| 340 | |
| 341 | >>> s = SequenceMatcher(lambda x: x==" ", " abcd", "abcd abcd") |
| 342 | >>> s.find_longest_match(0, 5, 0, 9) |
| 343 | Match(a=1, b=0, size=4) |
| 344 | |
| 345 | If no blocks match, return (alo, blo, 0). |
| 346 | |
| 347 | >>> s = SequenceMatcher(None, "ab", "c") |
| 348 | >>> s.find_longest_match(0, 2, 0, 1) |
| 349 | Match(a=0, b=0, size=0) |
| 350 | """ |
| 351 | |
| 352 | # CAUTION: stripping common prefix or suffix would be incorrect. |
| 353 | # E.g., |
| 354 | # ab |
| 355 | # acab |
| 356 | # Longest matching block is "ab", but if common prefix is |
| 357 | # stripped, it's "a" (tied with "b"). UNIX(tm) diff does so |
| 358 | # strip, so ends up claiming that ab is changed to acab by |
| 359 | # inserting "ca" in the middle. That's minimal but unintuitive: |
| 360 | # "it's obvious" that someone inserted "ac" at the front. |
| 361 | # Windiff ends up at the same place as diff, but by pairing up |
| 362 | # the unique 'b's and then matching the first two 'a's. |
| 363 |