add CatCode text encode options

This commit is contained in:
crazywhalecc
2022-12-24 01:38:15 +08:00
parent 0ac2985fc1
commit 6e43d4092f
2 changed files with 27 additions and 21 deletions

View File

@@ -10,8 +10,11 @@ class CatCode
{ {
/** /**
* 从 MessageSegment 转换为 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)) { if (is_array($message_segment)) {
@@ -20,30 +23,31 @@ class CatCode
if (!$v instanceof MessageSegment) { if (!$v instanceof MessageSegment) {
return ''; return '';
} }
$str .= self::segment2CatCode($v); $str .= self::segment2CatCode($v, $encode_text);
} }
return $str; return $str;
} }
if ($message_segment instanceof MessageSegment) { if ($message_segment instanceof MessageSegment) {
return self::segment2CatCode($message_segment); return self::segment2CatCode($message_segment, $encode_text);
} }
if (is_string($message_segment)) {
return $message_segment; return $message_segment;
}
return '';
} }
/** /**
* 转义CatCode的特殊字符 * 转义CatCode的特殊字符
* *
* @param int|string|\Stringable $msg 字符串 * @param int|string|\Stringable $msg 字符串
* @param bool $is_content 如果是转义CatCode本体内容则为false默认如果是参数内的字符串则为true * @param bool $is_param 如果是转义CatCode本体内容则为false默认如果是参数内的字符串则为true
* @return string 转义后的CatCode * @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); $msg = str_replace(['&', '[', ']'], ['&', '[', ']'], (string) $msg);
if ($is_content) { if ($is_param) {
$msg = str_replace(',', ',', $msg); $msg = str_replace([',', '=', "\r", "\n", "\t"], [',', '=', '
', '
', '	'], $msg);
} }
return $msg; return $msg;
} }
@@ -51,15 +55,15 @@ class CatCode
/** /**
* 反转义字符串中的CatCode敏感符号 * 反转义字符串中的CatCode敏感符号
* *
* @param int|string|\Stringable $msg 字符串 * @param int|string|\Stringable $msg 字符串
* @param bool $is_content 如果是解码CatCode本体内容则为false默认如果是参数内的字符串则为true * @param bool $is_param 如果是解码CatCode本体内容则为false默认如果是参数内的字符串则为true
* @return string 转义后的CatCode * @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); $msg = str_replace(['&', '[', ']'], ['&', '[', ']'], (string) $msg);
if ($is_content) { if ($is_param) {
$msg = str_replace(',', ',', $msg); $msg = str_replace([',', '=', '
', '
', '	'], [',', '=', "\r", "\n", "\t"], $msg);
} }
return $msg; return $msg;
} }
@@ -67,11 +71,12 @@ class CatCode
/** /**
* 转换一个 Segment 为 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']; return $segment->data['text'];
} }
$str = '[CatCode:' . $segment->type; $str = '[CatCode:' . $segment->type;

View File

@@ -15,10 +15,11 @@ class MessageUtil
* 将消息段无损转换为 CatCode 字符串 * 将消息段无损转换为 CatCode 字符串
* *
* @param array $message_segment 消息段 * @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);
} }
/** /**