mirror of
https://github.com/Paillat-dev/dismoji.git
synced 2026-01-02 00:56:19 +00:00
⚡ Refactor emojize and demojize functions to use shared replacement functions for improved performance (#18)
This commit is contained in:
@@ -30,6 +30,13 @@ EMOJI_PATTERN = re.compile(r":([a-zA-Z0-9_-]+):")
|
|||||||
EMOJI_CHARS_PATTERN = re.compile("|".join(map(re.escape, REVERSE_EMOJI_MAPPING.keys())))
|
EMOJI_CHARS_PATTERN = re.compile("|".join(map(re.escape, REVERSE_EMOJI_MAPPING.keys())))
|
||||||
|
|
||||||
|
|
||||||
|
def _replace(match: re.Match[str]) -> str:
|
||||||
|
emoji_name = match.group(1)
|
||||||
|
if emoji_name in EMOJI_MAPPING:
|
||||||
|
return EMOJI_MAPPING[emoji_name]
|
||||||
|
return match.group(0)
|
||||||
|
|
||||||
|
|
||||||
def emojize(s: str) -> str:
|
def emojize(s: str) -> str:
|
||||||
"""Convert a string with emoji names to a string with emoji characters.
|
"""Convert a string with emoji names to a string with emoji characters.
|
||||||
|
|
||||||
@@ -40,14 +47,12 @@ def emojize(s: str) -> str:
|
|||||||
str: The input string with emoji names replaced by emoji characters.
|
str: The input string with emoji names replaced by emoji characters.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
return EMOJI_PATTERN.sub(_replace, s)
|
||||||
|
|
||||||
def replace(match: re.Match[str]) -> str:
|
|
||||||
emoji_name = match.group(1)
|
|
||||||
if emoji_name in EMOJI_MAPPING:
|
|
||||||
return EMOJI_MAPPING[emoji_name]
|
|
||||||
return match.group(0)
|
|
||||||
|
|
||||||
return EMOJI_PATTERN.sub(replace, s)
|
def _reverse_replace(match: re.Match[str]) -> str:
|
||||||
|
emoji = match.group(0)
|
||||||
|
return f":{REVERSE_EMOJI_MAPPING[emoji]}:"
|
||||||
|
|
||||||
|
|
||||||
def demojize(s: str) -> str:
|
def demojize(s: str) -> str:
|
||||||
@@ -60,12 +65,7 @@ def demojize(s: str) -> str:
|
|||||||
str: The input string with emoji characters replaced by emoji names.
|
str: The input string with emoji characters replaced by emoji names.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
return EMOJI_CHARS_PATTERN.sub(_reverse_replace, s)
|
||||||
def replace(match: re.Match[str]) -> str:
|
|
||||||
emoji = match.group(0)
|
|
||||||
return f":{REVERSE_EMOJI_MAPPING[emoji]}:"
|
|
||||||
|
|
||||||
return EMOJI_CHARS_PATTERN.sub(replace, s)
|
|
||||||
|
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
|
|||||||
Reference in New Issue
Block a user