From 6e43d4092f4ec8827e68f016c9a8ed1976e26aaf Mon Sep 17 00:00:00 2001 From: crazywhalecc Date: Sat, 24 Dec 2022 01:38:15 +0800 Subject: [PATCH] add CatCode text encode options --- src/ZM/Utils/CatCode.php | 43 ++++++++++++++++++++---------------- src/ZM/Utils/MessageUtil.php | 5 +++-- 2 files changed, 27 insertions(+), 21 deletions(-) diff --git a/src/ZM/Utils/CatCode.php b/src/ZM/Utils/CatCode.php index 8bb84227..949e0415 100644 --- a/src/ZM/Utils/CatCode.php +++ b/src/ZM/Utils/CatCode.php @@ -10,8 +10,11 @@ class CatCode { /** * 从 MessageSegment 转换为 CatCode 字符串 + * + * @param mixed $message_segment MessageSegment 对象或数组 + * @param bool $encode_text 是否对文本进行 CatCode 编码(默认为否) */ - public static function fromSegment(array|MessageSegment|string $message_segment): string + public static function fromSegment(mixed $message_segment, bool $encode_text = false): string { // 传入的必须是段数组或段对象 if (is_array($message_segment)) { @@ -20,30 +23,31 @@ class CatCode if (!$v instanceof MessageSegment) { return ''; } - $str .= self::segment2CatCode($v); + $str .= self::segment2CatCode($v, $encode_text); } return $str; } - if ($message_segment instanceof MessageSegment) { - return self::segment2CatCode($message_segment); + return self::segment2CatCode($message_segment, $encode_text); } - - return $message_segment; + if (is_string($message_segment)) { + return $message_segment; + } + return ''; } /** * 转义CatCode的特殊字符 * - * @param int|string|\Stringable $msg 字符串 - * @param bool $is_content 如果是转义CatCode本体内容,则为false(默认),如果是参数内的字符串,则为true + * @param int|string|\Stringable $msg 字符串 + * @param bool $is_param 如果是转义CatCode本体内容,则为false(默认),如果是参数内的字符串,则为true * @return string 转义后的CatCode */ - public static function encode(\Stringable|int|string $msg, bool $is_content = false): string + public static function encode(\Stringable|int|string $msg, bool $is_param = false): string { $msg = str_replace(['&', '[', ']'], ['&', '[', ']'], (string) $msg); - if ($is_content) { - $msg = str_replace(',', ',', $msg); + if ($is_param) { + $msg = str_replace([',', '=', "\r", "\n", "\t"], [',', '=', ' ', ' ', ' '], $msg); } return $msg; } @@ -51,15 +55,15 @@ class CatCode /** * 反转义字符串中的CatCode敏感符号 * - * @param int|string|\Stringable $msg 字符串 - * @param bool $is_content 如果是解码CatCode本体内容,则为false(默认),如果是参数内的字符串,则为true + * @param int|string|\Stringable $msg 字符串 + * @param bool $is_param 如果是解码CatCode本体内容,则为false(默认),如果是参数内的字符串,则为true * @return string 转义后的CatCode */ - public static function decode(\Stringable|int|string $msg, bool $is_content = false): string + public static function decode(\Stringable|int|string $msg, bool $is_param = false): string { $msg = str_replace(['&', '[', ']'], ['&', '[', ']'], (string) $msg); - if ($is_content) { - $msg = str_replace(',', ',', $msg); + if ($is_param) { + $msg = str_replace([',', '=', ' ', ' ', ' '], [',', '=', "\r", "\n", "\t"], $msg); } return $msg; } @@ -67,11 +71,12 @@ class CatCode /** * 转换一个 Segment 为 CatCode * - * @param MessageSegment $segment 段对象 + * @param MessageSegment $segment 段对象 + * @param bool $encode_text 是否对文本进行CatCode转义 */ - private static function segment2CatCode(MessageSegment $segment): string + private static function segment2CatCode(MessageSegment $segment, bool $encode_text = false): string { - if ($segment->type === 'text') { + if (!$encode_text && $segment->type === 'text') { return $segment->data['text']; } $str = '[CatCode:' . $segment->type; diff --git a/src/ZM/Utils/MessageUtil.php b/src/ZM/Utils/MessageUtil.php index ac9ecf03..72aab1f1 100644 --- a/src/ZM/Utils/MessageUtil.php +++ b/src/ZM/Utils/MessageUtil.php @@ -15,10 +15,11 @@ class MessageUtil * 将消息段无损转换为 CatCode 字符串 * * @param array $message_segment 消息段 + * @param bool $encode_text 是否对文本进行 CatCode 转义 */ - public static function arrayToStr(array $message_segment): string + public static function arrayToStr(array $message_segment, bool $encode_text = false): string { - return CatCode::fromSegment($message_segment); + return CatCode::fromSegment($message_segment, $encode_text); } /**