summaryrefslogtreecommitdiff
path: root/poezio/common.py
diff options
context:
space:
mode:
Diffstat (limited to 'poezio/common.py')
-rw-r--r--poezio/common.py20
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