过滤非法字符 三

来源:互联网 发布:社区红色网络家园 编辑:程序博客网 时间:2024/05/01 07:13

实现文件:

#include "stdafx.h"

#include <string>
#include <vector>
#include <algorithm>
#include <functional>

#include "WordLib.h"
#include "_string.h"

namespace WordLib{

typedef std::basic_string<char, ChatTraintsEx> __String;
typedef std::vector<__String> __WordList;

static const char* __strWordLib[] = {
"***",
"***",
"***",
"**** ",
"**** you",
"script"
};

static __WordList __wordList;

static __String __nullStr = " ";

void __InitWordList()
{
if(__wordList.size() != 0)
return ;

for(size_t i = 0; i < sizeof(__strWordLib) / sizeof(const char*); ++i)
__wordList.push_back(__strWordLib[i]);

std::sort(__wordList.begin(), __wordList.end());
}

bool _FLYINGMAGIC_2_CUT_WORD IsValid(const char* str)
{
__InitWordList();

if(str != NULL)
{
__String strSrc(str);

__WordList::iterator itEnd = __wordList.end();

for(__WordList::iterator it = __wordList.begin(); it != itEnd; ++it)
{
if(strSrc.find(*it) != -1)
return false;
}
}

return true;
}

bool _FLYINGMAGIC_2_CUT_WORD Filtrate(char* dst, size_t len, const char* src)
{
__InitWordList();

bool bRet = false;

if(src != NULL && dst != NULL && strlen(src) <= len)
{
__String strSrc(src);

__WordList::iterator itEnd = __wordList.end();

for(__WordList::iterator it = __wordList.begin(); it != itEnd; ++it)
{
__String::size_type begin = strSrc.find(*it);
if(begin != -1)
{
for(__String::size_type t = 0; t < it->size(); ++t)
strSrc[begin + t] = ' ';
// strSrc.assign(__nullStr, begin, it->size());

bRet = true;
}
}

strcpy(dst, strSrc.c_str());
}

return bRet;
}
}

原创粉丝点击