Merge pull request #178 from super1207/patch-2

fix cq code parse
This commit is contained in:
白池 2023-12-27 20:00:49 +09:00 committed by GitHub
commit 71dd9469ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,52 +9,61 @@ inline void replace_string(std::string& str, const std::string& from, const std:
} }
} }
inline int utf8_next_len(const std::string& str, size_t offset)
{
uint8_t c = (uint8_t)str[offset];
if (c >= 0xFC)
return 6;
else if (c >= 0xF8)
return 5;
else if (c >= 0xF0)
return 4;
else if (c >= 0xE0)
return 3;
else if (c >= 0xC0)
return 2;
else if (c > 0x00)
return 1;
else
return 0;
}
void decode_cqcode(const std::string& code, std::vector<std::unordered_map<std::string, std::string>>& dest) { void decode_cqcode(const std::string& code, std::vector<std::unordered_map<std::string, std::string>>& dest) {
std::string cache; std::string cache;
bool is_start = false; bool is_start = false;
std::string key_tmp; std::string key_tmp;
std::unordered_map<std::string, std::string> kv; std::unordered_map<std::string, std::string> kv;
for(int i = 0; i < code.size(); i++) { for(size_t i = 0; i < code.size(); i++) {
auto c = code[i]; int utf8_char_len = utf8_next_len(code, i);
if (c == '[') { if(utf8_char_len == 0) {
continue;
}
std::string_view c(&code[i],utf8_char_len);
if (c == "[") {
if (is_start) { if (is_start) {
throw illegal_code(); throw illegal_code();
} else { } else {
if (!cache.empty()) { if (!cache.empty()) {
std::unordered_map<std::string, std::string> kv; std::unordered_map<std::string, std::string> kv;
replace_string(cache, "&#91;", "[");
replace_string(cache, "&#93;", "]");
replace_string(cache, "&amp;", "&");
kv.emplace("_type", "text"); kv.emplace("_type", "text");
kv.emplace("text", cache); kv.emplace("text", cache);
dest.push_back(kv); dest.push_back(kv);
cache.clear(); cache.clear();
} }
auto c1 = code[i + 1]; std::string_view cq_flag(&code[i],4);
if (c1 == 'C') { if(cq_flag == "[CQ:"){
i++;
auto c2 = code[i + 1];
if(c2 == 'Q') {
i++;
auto c3 = code[i + 1];
if (c3 == ':') {
i++;
is_start = true; is_start = true;
} else { i += 3;
}else{
cache += c; cache += c;
cache += c1;
cache += c2;
continue;
}
} else {
cache += c;
cache += c1;
continue;
}
} else {
cache += c;
continue;
} }
} }
} }
else if (c == '=') { else if (c == "=") {
if (is_start) { if (is_start) {
if (cache.empty()) { if (cache.empty()) {
throw illegal_code(); throw illegal_code();
@ -70,17 +79,17 @@ void decode_cqcode(const std::string& code, std::vector<std::unordered_map<std::
cache += c; cache += c;
} }
} }
else if (c == ',') { else if (c == ",") {
if (is_start) { if (is_start) {
if (kv.count("_type") == 0 && !cache.empty()) { if (kv.count("_type") == 0 && !cache.empty()) {
kv.emplace("_type", cache); kv.emplace("_type", cache);
cache.clear(); cache.clear();
} else { } else {
if (!key_tmp.empty()) { if (!key_tmp.empty()) {
replace_string(cache, "&amp;", "&");
replace_string(cache, "&#91;", "["); replace_string(cache, "&#91;", "[");
replace_string(cache, "&#93;", "]"); replace_string(cache, "&#93;", "]");
replace_string(cache, "&#44;", ","); replace_string(cache, "&#44;", ",");
replace_string(cache, "&amp;", "&");
kv.emplace(key_tmp, cache); kv.emplace(key_tmp, cache);
cache.clear(); cache.clear();
key_tmp.clear(); key_tmp.clear();
@ -90,14 +99,14 @@ void decode_cqcode(const std::string& code, std::vector<std::unordered_map<std::
cache += c; cache += c;
} }
} }
else if (c == ']') { else if (c == "]") {
if (is_start) { if (is_start) {
if (!cache.empty()) { if (!cache.empty()) {
if (!key_tmp.empty()) { if (!key_tmp.empty()) {
replace_string(cache, "&amp;", "&");
replace_string(cache, "&#91;", "["); replace_string(cache, "&#91;", "[");
replace_string(cache, "&#93;", "]"); replace_string(cache, "&#93;", "]");
replace_string(cache, "&#44;", ","); replace_string(cache, "&#44;", ",");
replace_string(cache, "&amp;", "&");
kv.emplace(key_tmp, cache); kv.emplace(key_tmp, cache);
} else { } else {
kv.emplace("_type", cache); kv.emplace("_type", cache);
@ -114,10 +123,14 @@ void decode_cqcode(const std::string& code, std::vector<std::unordered_map<std::
} }
else { else {
cache += c; cache += c;
i += (utf8_char_len - 1);
} }
} }
if (!cache.empty()) { if (!cache.empty()) {
std::unordered_map<std::string, std::string> kv; std::unordered_map<std::string, std::string> kv;
replace_string(cache, "&#91;", "[");
replace_string(cache, "&#93;", "]");
replace_string(cache, "&amp;", "&");
kv.emplace("_type", "text"); kv.emplace("_type", "text");
kv.emplace("text", cache); kv.emplace("text", cache);
dest.push_back(kv); dest.push_back(kv);