utf8编码的数据可直接使用下面的代码
最关键的步骤就是把字符串拆成单个字,UTF-8编码的字,如果只有一个字节则其最高二进制位为0;如果是多字节,其第一个字节从最高位开始,连续的二进制位值为1的个数决定了其编码的位数,其余各字节均以10开头。
UTF-8最多可用到6个字节。
1字节 0xxxxxxx
2字节 110xxxxx 10xxxxxx
3字节 1110xxxx 10xxxxxx 10xxxxxx
4字节 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
5字节 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
6字节 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
其它就很简单了
1、禁用字处理 禁用字拆分后以第一个为key保存
2、待测试字符串
a)、拆分成单字
b)、大写转小写,字母和空格全角转半角,去掉多余空格(英文字母后最多只会有一个空格,中文后不应该有空格)
c)、遍历字符串的所有字 检测每个字对应的禁用字组是否在待测字符串中
#include <string> #include <vector> #include <map> #include <set> #include <iostream> #include <sstream> #include <string.h> #include <stdio.h> class CDisableWord { struct SDisableWord { std::string str; }; typedef std::vector<SDisableWord> VDW; private: std::map<std::string, VDW> m_mapDisableWord; std::set<std::string> m_setAllDisableWord; // 特殊转换 std::map<std::string, std::string> m_mapSpecialWord; private: // 把字符串拆分为单个字 size_t SplitWord(const char* pSrc, unsigned int len, std::vector<std::string>& output); // 获取特殊字对应的转换字 const std::string* GetSpecialWord(const std::string& str); public: CDisableWord(); // 设置禁用字 void AddOneDisableWord(const std::string& str); // 检测 bool CheckStr(const char* pSrc, unsigned int len); bool CheckStr(const std::string& str); }; CDisableWord::CDisableWord() { std::string qjdx[26] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}; std::string qjxx[26] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"}; std::string dx = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; std::string rst = "abcdefghijklmnopqrstuvwxyz"; std::string str1 = "a"; std::string str2 = "a"; for(int i = 0; i < 26; i++) { str1[0] = rst[i]; str2[0] = dx[i]; m_mapSpecialWord[qjdx[i]] = str1; m_mapSpecialWord[qjxx[i]] = str1; m_mapSpecialWord[str2] = str1; } m_mapSpecialWord[" "] = std::string(" "); } // 把字符串拆分为单个字 size_t CDisableWord::SplitWord(const char* pSrc, unsigned int len, std::vector<std::string>& output) { std::string ch; unsigned char byte; for(unsigned int i = 0, wlen = 0; i < len; i += wlen) { byte = (unsigned char)pSrc[i]; if (byte >= 0xFC) wlen = 6; else if (byte >= 0xF8) wlen = 5; else if (byte >= 0xF0) wlen = 4; else if (byte >= 0xE0) wlen = 3; else if (byte >= 0xC0) wlen = 2; else wlen = 1; if(i + wlen > len) break; ch.clear(); for(unsigned int j = 0; j < wlen; j++) ch += pSrc[i+j]; output.push_back(ch); } return output.size(); } // 获取特殊字对应的转换字 const std::string* CDisableWord::GetSpecialWord(const std::string& str) { std::map<std::string, std::string>::iterator miter = m_mapSpecialWord.find(str); if(miter == m_mapSpecialWord.end()) return NULL; return &(miter->second); } void CDisableWord::AddOneDisableWord(const std::string& str) { if(m_setAllDisableWord.find(str) != m_setAllDisableWord.end()) return; std::vector<std::string> output; if(SplitWord(str.c_str(), str.size(), output) == 0 || output[0].size() == 0) return; std::map<std::string, VDW>::iterator miter = m_mapDisableWord.find(output[0]); if(miter == m_mapDisableWord.end()) { m_mapDisableWord[output[0]] = VDW(); miter = m_mapDisableWord.find(output[0]); } if(miter == m_mapDisableWord.end()) return; SDisableWord sdw; sdw.str = str; miter->second.push_back(sdw); } bool CDisableWord::CheckStr(const char* pSrc, unsigned int len) { if(len == 0) return true; std::string str(pSrc, len); return CheckStr(str); } bool CDisableWord::CheckStr(const std::string& str) { if(str.size() == 0) return true; std::vector<std::string> output; if(SplitWord(str.c_str(), str.size(), output) == 0 || output[0].size() == 0) return false; // 大写转小写 全角转半角 for(size_t i = 0; i < output.size(); ++i) { const std::string* pStr = GetSpecialWord(output[i]); if(pStr) output[i] = *pStr; } std::string StrSrc = ""; //转换之后的字符串 std::string StrDelSpace = ""; //删除非英文之后的所有空格 所有大写转成小写 std::set<std::string> sonly; for(size_t i = 0; i < output.size(); ++i) { sonly.insert(output[i]); StrSrc += output[i]; bool bnoadd = false; if(i > 0 && output[i] == " ") { bnoadd = true; for(int j = int(i - 1); j >= 0; --j) { if(output[j] == " ") continue; if(output[j].size() > 1) bnoadd = false; else if(j + 1 == int(i)) // 英文字符留一个空格 bnoadd = false; break; } } if(!bnoadd) StrDelSpace += output[i]; } bool bSame = (StrDelSpace == StrSrc); std::set<std::string>::iterator siter = sonly.begin(); for(; siter != sonly.end(); ++siter) { std::map<std::string, VDW>::iterator miter = m_mapDisableWord.find(*siter); if(miter == m_mapDisableWord.end()) continue; for(size_t j = 0; j < miter->second.size(); ++j) { SDisableWord& sdw = miter->second[j]; if(StrSrc.find(sdw.str) != std::string::npos) return false; else if(!bSame && StrDelSpace.find(sdw.str) != std::string::npos) return false; } } return true; } int main() { CDisableWord cdw; // 设置禁用字 std::string strdw[] = {"中文", "英文", "测试", "aabb", "测 试", "cc dd"}; for(int i = 0; i < 6; i++) cdw.AddOneDisableWord(strdw[i]); while(1) { char s[51]; std::cin.getline(s,50); if(cdw.CheckStr(s, strlen(s))) printf("收到:%s 没有敏感字\n", s); else printf("收到:%s 敏感字 敏感字 敏感字\n", s); } return 0; } // g++ -g -o DisableWord DisableWord.cpp
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。