diff --git a/zhenxun/services/db_context.py b/zhenxun/services/db_context.py index 605db41b..dd9cd485 100644 --- a/zhenxun/services/db_context.py +++ b/zhenxun/services/db_context.py @@ -11,6 +11,7 @@ from tortoise.models import Model as Model_ from zhenxun.configs.config import BotConfig from zhenxun.utils.exception import HookPriorityException from zhenxun.utils.manager.priority_manager import PriorityLifecycle +from zhenxun.utils.utils import unicode_escape, unicode_unescape from .log import logger @@ -21,41 +22,6 @@ MODELS: list[str] = [] driver = nonebot.get_driver() -def unicode_escape(value: str) -> str: - """ - 将字符串转换为Unicode转义形式(仅处理未转义的特殊字符) - 已经转义过的字符串保持不变 - """ - if not value: - return value - - if re.search(r"\\u[0-9a-fA-F]{4}", value): - return value - - return "".join( - char - if 0x20 <= ord(char) <= 0x7E or char in ("\n", "\r", "\t") - else f"\\u{ord(char):04x}" - for char in value - ) - - -def unicode_unescape(value: str) -> str: - """ - 安全还原字符串中的Unicode转义序列 - 如果不是有效转义序列,保留原样 - """ - if not value: - return value - - # 仅处理有效的 \uXXXX 格式 - return re.sub( - r"(? str: + """ + 将字符串转换为Unicode转义形式(仅处理未转义的特殊字符) + 已经转义过的字符串保持不变 + """ + if not value: + return value + + if re.search(r"\\u[0-9a-fA-F]{4}", value): + return value + + return "".join( + char + if 0x20 <= ord(char) <= 0x7E or char in ("\n", "\r", "\t") + else f"\\u{ord(char):04x}" + for char in value + ) + + +def unicode_unescape(value: str) -> str: + """ + 安全还原字符串中的Unicode转义序列 + 如果不是有效转义序列,保留原样 + """ + if not value: + return value + + # 仅处理有效的 \uXXXX 格式 + return re.sub( + r"(?