Returns the length of the overlap between two strings. Example: "foo baro" and "baro zoo" -> 4
( left_string, right_string )
| 130 | |
| 131 | |
| 132 | def OverlapLength( left_string, right_string ): |
| 133 | """Returns the length of the overlap between two strings. |
| 134 | Example: "foo baro" and "baro zoo" -> 4 |
| 135 | """ |
| 136 | left_string_length = len( left_string ) |
| 137 | right_string_length = len( right_string ) |
| 138 | |
| 139 | if not left_string_length or not right_string_length: |
| 140 | return 0 |
| 141 | |
| 142 | # Truncate the longer string. |
| 143 | if left_string_length > right_string_length: |
| 144 | left_string = left_string[ -right_string_length: ] |
| 145 | elif left_string_length < right_string_length: |
| 146 | right_string = right_string[ :left_string_length ] |
| 147 | |
| 148 | if left_string == right_string: |
| 149 | return min( left_string_length, right_string_length ) |
| 150 | |
| 151 | # Start by looking for a single character match |
| 152 | # and increase length until no match is found. |
| 153 | best = 0 |
| 154 | length = 1 |
| 155 | while True: |
| 156 | pattern = left_string[ -length: ] |
| 157 | found = right_string.find( pattern ) |
| 158 | if found < 0: |
| 159 | return best |
| 160 | length += found |
| 161 | if left_string[ -length: ] == right_string[ :length ]: |
| 162 | best = length |
| 163 | length += 1 |
no outgoing calls
no test coverage detected