|
| 1 | +/* |
| 2 | + * Copyright (c) 2020. ForteScarlet |
| 3 | + * |
| 4 | + * catCode库相关代码使用 MIT License 开源,请遵守协议相关条款。 |
| 5 | + * |
| 6 | + * about MIT: https://opensource.org/licenses/MIT |
| 7 | + * |
| 8 | + * |
| 9 | + * |
| 10 | + * |
| 11 | + */ |
| 12 | + |
| 13 | +@file:Suppress("unused") |
| 14 | +@file:JvmName("Cats") |
| 15 | +package catcode |
| 16 | + |
| 17 | +/* |
| 18 | +& -> & |
| 19 | +[ -> [ |
| 20 | +] -> ] |
| 21 | + */ |
| 22 | +/* |
| 23 | +& -> & |
| 24 | +[ -> [ |
| 25 | +] -> ] |
| 26 | +, -> , |
| 27 | + */ |
| 28 | + |
| 29 | +internal val CAT_KV_SPLIT_ARRAY: Array<String> = arrayOf(CAT_KV) |
| 30 | +internal val CAT_PS_SPLIT_ARRAY: Array<String> = arrayOf(CAT_PS) |
| 31 | + |
| 32 | +/** Cat Decoder */ |
| 33 | +@Suppress("MemberVisibilityCanBePrivate") |
| 34 | +object CatDecoder { |
| 35 | + |
| 36 | + @JvmStatic |
| 37 | + val instance |
| 38 | + get() = this |
| 39 | + |
| 40 | + /** 非猫猫码文本消息解义 */ |
| 41 | + fun decodeText(str: String): String = |
| 42 | + str .replace("[", "[") |
| 43 | + .replace("]", "]") |
| 44 | + .replace("	", "\t") |
| 45 | + .replace(" ", "\r") |
| 46 | + .replace(" ", "\n") |
| 47 | + .replace("&", "&") |
| 48 | + |
| 49 | + /** 非猫猫码文本消息解义,如果[str]为null则返回null */ |
| 50 | + fun decodeTextOrNull(str: String?) : String? = str?.let { decodeText(it) } |
| 51 | + |
| 52 | + |
| 53 | + /** 猫猫码参数值消息解义 */ |
| 54 | + fun decodeParams(str: String): String = |
| 55 | + str .replace("[", "[") |
| 56 | + .replace("]", "]") |
| 57 | + .replace(",", ",") |
| 58 | + .replace("	", "\t") |
| 59 | + .replace(" ", "\r") |
| 60 | + .replace(" ", "\n") |
| 61 | + .replace("&", "&") |
| 62 | + |
| 63 | + /** 猫猫码参数值消息解义,如果[str]为null则返回null */ |
| 64 | + fun decodeParamsOrNull(str: String?): String? = str?.let { decodeParams(it) } |
| 65 | + |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +public fun String.deCatParam(): String = CatDecoder.decodeParams(this) |
| 70 | +public fun String.deCatText(): String = CatDecoder.decodeText(this) |
| 71 | + |
| 72 | + |
| 73 | +/** Cat Encoder */ |
| 74 | +@Suppress("MemberVisibilityCanBePrivate") |
| 75 | +object CatEncoder { |
| 76 | + |
| 77 | + @JvmStatic |
| 78 | + val instance |
| 79 | + get() = this |
| 80 | + |
| 81 | + /** 非猫猫码文本消息转义 */ |
| 82 | + fun encodeText(str: String): String = |
| 83 | + str.replace("&", "&") |
| 84 | + .replace("[", "[") |
| 85 | + .replace("]", "]") |
| 86 | + .replace("\t", "	") |
| 87 | + .replace("\r", " ") |
| 88 | + .replace("\n", " ") |
| 89 | + |
| 90 | + /** 非猫猫码文本消息转义。如果[str]为null则返回null */ |
| 91 | + fun encodeTextOrNull(str: String?): String? = str?.let { encodeText(it) } |
| 92 | + |
| 93 | + /** 猫猫码参数值消息转义 */ |
| 94 | + fun encodeParams(str: String): String = |
| 95 | + str.replace("&", "&") |
| 96 | + .replace("[", "[") |
| 97 | + .replace("]", "]") |
| 98 | + .replace(",", ",") |
| 99 | + .replace("\t", "	") |
| 100 | + .replace("\r", " ") |
| 101 | + .replace("\n", " ") |
| 102 | + |
| 103 | + /** 猫猫码参数值消息转义。如果[str]为null则返回null */ |
| 104 | + fun encodeParamsOrNull(str: String?): String? = str?.let { encodeParams(it) } |
| 105 | + |
| 106 | +} |
| 107 | + |
| 108 | +public fun String.enCatParam(): String = CatEncoder.encodeParams(this) |
| 109 | +public fun String.enCatText(): String = CatEncoder.encodeText(this) |
| 110 | + |
| 111 | + |
| 112 | +/** |
| 113 | + * 猫猫码的操作工具类 |
| 114 | + */ |
| 115 | +public object CatCodeUtil : NekoAibo("CAT") { |
| 116 | + @JvmStatic |
| 117 | + val instance |
| 118 | + get() = this |
| 119 | + |
| 120 | + override val catCodeHead: String = CAT_HEAD |
| 121 | + /** |
| 122 | + * 获取一个String为载体的[模板][CodeTemplate] |
| 123 | + * @see StringTemplate |
| 124 | + */ |
| 125 | + override val stringTemplate: CodeTemplate<String> get() = StringTemplate |
| 126 | + |
| 127 | + /** |
| 128 | + * 获取[Neko]为载体的[模板][CodeTemplate] |
| 129 | + * @see NekoTemplate |
| 130 | + */ |
| 131 | + override val nekoTemplate: CodeTemplate<Neko> get() = NekoTemplate |
| 132 | + |
| 133 | + /** |
| 134 | + * 构建一个String为载体类型的[构建器][CodeBuilder] |
| 135 | + */ |
| 136 | + override fun getStringCodeBuilder(type: String, encode: Boolean): CodeBuilder<String> = StringCodeBuilder(type, encode) |
| 137 | + |
| 138 | + /** |
| 139 | + * 构建一个[Neko]为载体类型的[构建器][CodeBuilder] |
| 140 | + */ |
| 141 | + override fun getNekoBuilder(type: String, encode: Boolean): CodeBuilder<Neko> = NekoBuilder(type) |
| 142 | + |
| 143 | + /** |
| 144 | + * 构建一个[Neko]为载体类型的[懒加载构建器][LazyCodeBuilder] |
| 145 | + */ |
| 146 | + override fun getLazyNekoBuilder(type: String, encode: Boolean): LazyCodeBuilder<Neko> = LazyNekoBuilder(type) |
| 147 | + |
| 148 | +} |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
0 commit comments