MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / common_suffix

Function common_suffix

src/licensedcode/dmp.py:611–644  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

609
610
611def 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
647def merge(diffs):

Callers 3

differenceMethod · 0.70
half_match_iFunction · 0.70
mergeFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected