|
21 | 21 | ClassVar,
|
22 | 22 | Dict,
|
23 | 23 | Mapping,
|
| 24 | + Match, |
24 | 25 | MutableMapping,
|
25 | 26 | Optional,
|
26 | 27 | Tuple,
|
@@ -380,37 +381,31 @@ def map_username_to_mxid_localpart(
|
380 | 381 | onto different mxids
|
381 | 382 |
|
382 | 383 | Returns:
|
383 |
| - unicode: string suitable for a mxid localpart |
| 384 | + string suitable for a mxid localpart |
384 | 385 | """
|
385 | 386 | if not isinstance(username, bytes):
|
386 | 387 | username = username.encode("utf-8")
|
387 | 388 |
|
388 | 389 | # first we sort out upper-case characters
|
389 | 390 | if case_sensitive:
|
390 | 391 |
|
391 |
| - def f1(m): |
| 392 | + def f1(m: Match[bytes]) -> bytes: |
392 | 393 | return b"_" + m.group().lower()
|
393 | 394 |
|
394 | 395 | username = UPPER_CASE_PATTERN.sub(f1, username)
|
395 | 396 | else:
|
396 | 397 | username = username.lower()
|
397 | 398 |
|
398 |
| - # then we sort out non-ascii characters |
399 |
| - def f2(m): |
400 |
| - g = m.group()[0] |
401 |
| - if isinstance(g, str): |
402 |
| - # on python 2, we need to do a ord(). On python 3, the |
403 |
| - # byte itself will do. |
404 |
| - g = ord(g) |
405 |
| - return b"=%02x" % (g,) |
| 399 | + # then we sort out non-ascii characters by converting to the hex equivalent. |
| 400 | + def f2(m: Match[bytes]) -> bytes: |
| 401 | + return b"=%02x" % (m.group()[0],) |
406 | 402 |
|
407 | 403 | username = NON_MXID_CHARACTER_PATTERN.sub(f2, username)
|
408 | 404 |
|
409 | 405 | # we also do the =-escaping to mxids starting with an underscore.
|
410 | 406 | username = re.sub(b"^_", b"=5f", username)
|
411 | 407 |
|
412 |
| - # we should now only have ascii bytes left, so can decode back to a |
413 |
| - # unicode. |
| 408 | + # we should now only have ascii bytes left, so can decode back to a string. |
414 | 409 | return username.decode("ascii")
|
415 | 410 |
|
416 | 411 |
|
|
0 commit comments