diff options
author | Jonas Schäfer <j.wielicki@sotecware.net> | 2020-05-10 11:08:05 +0200 |
---|---|---|
committer | Jonas Schäfer <j.wielicki@sotecware.net> | 2020-05-12 22:26:51 +0200 |
commit | d4d0c1a19f5fc5df0f082df1fb7323141175a310 (patch) | |
tree | 86dee7fc58cc441a14b661fa5e003788a5eac18d /poezio/common.py | |
parent | 1b974d2d9aec5c20d3111d208a24d7d67ab6b7de (diff) | |
download | poezio-d4d0c1a19f5fc5df0f082df1fb7323141175a310.tar.gz poezio-d4d0c1a19f5fc5df0f082df1fb7323141175a310.tar.bz2 poezio-d4d0c1a19f5fc5df0f082df1fb7323141175a310.tar.xz poezio-d4d0c1a19f5fc5df0f082df1fb7323141175a310.zip |
Add function to calculate unique prefix of two strings
Diffstat (limited to 'poezio/common.py')
-rw-r--r-- | poezio/common.py | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/poezio/common.py b/poezio/common.py index ba179310..7cddc306 100644 --- a/poezio/common.py +++ b/poezio/common.py @@ -17,6 +17,7 @@ import subprocess import time import string import logging +import itertools from slixmpp import JID, InvalidJID, Message from poezio.poezio_shlex import shlex @@ -468,3 +469,22 @@ def safeJID(*args, **kwargs) -> JID: exc_info=True, ) return JID('') + + +def unique_prefix_of(a: str, b: str) -> str: + """ + Return the unique prefix of `a` with `b`. + + Corner cases: + + * If `a` and `b` share no prefix, the first letter of `a` is returned. + * If `a` and `b` are equal, `a` is returned. + * If `a` is a prefix of `b`, `a` is returned. + * If `b` is a prefix of `a`, `b` plus the first letter of `a` after the + common prefix is returned. + """ + for i, (ca, cb) in enumerate(itertools.zip_longest(a, b)): + if ca != cb: + return a[:i+1] + # both are equal, return a + return a |