diff options
author | mathieui <mathieui@mathieui.net> | 2020-05-12 22:32:50 +0200 |
---|---|---|
committer | mathieui <mathieui@mathieui.net> | 2020-05-12 22:32:50 +0200 |
commit | ef38ff8f6a7ef813a6b4ab9edad458b4b7e9941f (patch) | |
tree | 70763f4128c9250b4b7ead7e5a8dcc989ae7c8fb /poezio/common.py | |
parent | 1b974d2d9aec5c20d3111d208a24d7d67ab6b7de (diff) | |
parent | 1e7ce43789aae67fcfbb4b6fbe49a299fda1cfa1 (diff) | |
download | poezio-ef38ff8f6a7ef813a6b4ab9edad458b4b7e9941f.tar.gz poezio-ef38ff8f6a7ef813a6b4ab9edad458b4b7e9941f.tar.bz2 poezio-ef38ff8f6a7ef813a6b4ab9edad458b4b7e9941f.tar.xz poezio-ef38ff8f6a7ef813a6b4ab9edad458b4b7e9941f.zip |
Merge branch 'feature/unique-prefix-tab-names' into 'master'
Unique prefix tab names
Closes #3525
See merge request poezio/poezio!94
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 |