From 258d154f064a8beccbec590ef94a125474e0ea80 Mon Sep 17 00:00:00 2001 From: molanp <104612722+molanp@users.noreply.github.com> Date: Mon, 14 Jul 2025 22:55:43 +0800 Subject: [PATCH] =?UTF-8?q?refactor(zhenxun):=20=E8=B0=83=E6=95=B4=20unico?= =?UTF-8?q?de=5Fescape=20=E5=92=8C=20unicode=5Funescape=20=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 从 db_context.py 中移除这两个函数 - 将它们添加到 utils.py 中,以更好地组织代码 --- zhenxun/services/db_context.py | 36 +--------------------------------- zhenxun/utils/utils.py | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 35 deletions(-) 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"(?