Determine the common suffix of two strings. Args: text1: First string. text2: Second string. Returns: The number of characters common to the end of each string.
(text1, text2)
| 609 | |
| 610 | |
| 611 | def common_suffix(text1, text2): |
| 612 | """ |
| 613 | Determine the common suffix of two strings. |
| 614 | |
| 615 | Args: |
| 616 | text1: First string. |
| 617 | text2: Second string. |
| 618 | |
| 619 | Returns: |
| 620 | The number of characters common to the end of each string. |
| 621 | """ |
| 622 | # Quick check for common null cases. |
| 623 | if not text1 or not text2 or text1[-1] != text2[-1]: |
| 624 | return 0 |
| 625 | # Binary search. |
| 626 | # Performance analysis: https://neil.fraser.name/news/2007/10/09/ |
| 627 | pointermin = 0 |
| 628 | |
| 629 | # TODO: move as args |
| 630 | len_text1 = len(text1) |
| 631 | len_text2 = len(text2) |
| 632 | |
| 633 | pointermax = min(len_text1, len_text2) |
| 634 | pointermid = pointermax |
| 635 | pointerend = 0 |
| 636 | |
| 637 | while pointermin < pointermid: |
| 638 | if (text1[-pointermid:len_text1 - pointerend] == text2[-pointermid:len(text2) - pointerend]): |
| 639 | pointermin = pointermid |
| 640 | pointerend = pointermin |
| 641 | else: |
| 642 | pointermax = pointermid |
| 643 | pointermid = (pointermax - pointermin) // 2 + pointermin |
| 644 | return pointermid |
| 645 | |
| 646 | |
| 647 | def merge(diffs): |
no outgoing calls
no test coverage detected