MMString 的源码如下,使用MMString ,可以轻松的实现手机号段查询,短信业务中的业务代码模糊匹配。
由于MMString 是string的子类,使用起来和string完全一样,非常方便。同时通过模板参数来控制匹配过程是是否对大小写敏感。
// //
// mmstring.h - 最长匹配string //
// //
// Larrin Workshop //
// //
// email: larrin2002@msn.com //
// //
// HomePage : http://www.larrin.net //
// //
#ifndef LARRINWORKSHOP_MAX_LENGTH_MATCH_STRING__H
#define LARRINWORKSHOP_MAX_LENGTH_MATCH_STRING__H
#include <string>
using std::string ;
#ifndef WIN32
#define strnicmp strnicasecmp
#endif
template <bool ignorecase=false>
class MMString : public string
{
bool m_isKey ;
string m_nextlongger_str;
string m_shortest_str ;
bool m_ignorecase;
public:
MMString() : string() , m_isKey(true) , m_ignorecase(ignorecase)
{
}
MMString(char * str) : string(str) , m_isKey(true) , m_ignorecase(ignorecase)
{
}
MMString(bool key,char * str) : string(str) , m_isKey(key) , m_ignorecase(ignorecase)
{
}
MMString(char * str, int len):string(str,len) , m_isKey(true) , m_ignorecase(ignorecase)
{
}
MMString(size_type Size, value_type Character):string(Size,Character) , m_isKey(true) , m_ignorecase(ignorecase)
{
}
MMString(const MMString &dstr)
{
this->m_isKey = dstr.m_isKey ;
this->m_nextlongger_str = dstr.m_nextlongger_str;
this->m_shortest_str = dstr.m_shortest_str;
this->m_ignorecase = dstr.m_ignorecase;
assign(dstr.c_str());
}
virtual ~MMString(){}
MMString operator=(const MMString &val)
{
this->m_isKey = val.m_isKey ;
this->m_nextlongger_str = val.m_nextlongger_str;
this->m_shortest_str = val.m_shortest_str;
this->m_ignorecase = val.m_ignorecase;
assign(val.c_str());
return *this ;
}
bool isKey()
{
return this->m_isKey ;
}
bool operator < (const MMString &s) const
{
int len1 = this->length();
int len2 = s.length();
int ret;
if(m_ignorecase)
ret = strnicmp(this->c_str(),s.c_str(),len1 < len2 ? len1 : len2);
else
ret = strncmp(this->c_str(),s.c_str(),len1 < len2 ? len1 : len2);
if(this->m_isKey && s.m_isKey)
{
if(len1 > len2)
{
if(ret == 0)
{
if(m_shortest_str.empty() || m_shortest_str.length() > len2)
{
string * str = const_cast<string *>(&(m_shortest_str));
*str = s.c_str();
}
if(s.m_nextlongger_str.length() < len1)
{
string * str = const_cast<string *>(&(s.m_nextlongger_str));
*str = c_str();
}
}
return ret <= 0 ;
}
else
{
if(ret == 0)
{
if(m_nextlongger_str.length() < len2)
{
string * str = const_cast<string *>(&(this->m_nextlongger_str));
*str = s.c_str();
}
if(s.m_shortest_str.empty() || s.m_shortest_str.length() > len1)
{
string * str = const_cast<string *>(&(s.m_shortest_str));
*str = c_str();
}
}
return ret < 0 ;
}
}
else
{
if(this->m_isKey)
{
if(len1 > len2)
return ret <= 0 ;
else
{
if(ret == 0)
return false ;
else
{
if(ret > 0)
{
int len3 = this->m_shortest_str.length();
if(len3 > 0)
{
if(!this->m_ignorecase)
{
if(strncmp(this->c_str(),s.c_str(),len3) == 0)
return true ;
}
else
{
if(strnicmp(this->c_str(),s.c_str(),len3) == 0)
return true ;
}
}
}
return ret < 0 ;
}
}
}
else
{
if(len1 < len2)
return ret < 0 ;
else
{
if(ret == 0)
{
if(s.m_nextlongger_str.length() > 0)
{
int len3 = s.m_nextlongger_str.length() ;
if(len3 > len1)
return false ;
if(!this->m_ignorecase)
{
if(strncmp(s.m_nextlongger_str.c_str(),this->c_str(),len3) == 0)
return true ;
}
else
{
if(strnicmp(s.m_nextlongger_str.c_str(),this->c_str(),len3) == 0)
return true ;
}
return false ;
}
return false ;
}
else
return ret < 0 ;
}
}
}
}
};
typedef MMString<false> mmstring ;
typedef MMString<true> immstring;
#endif //LARRINWORKSHOP_MAX_LENGTH_MATCH_STRING__H
下面是测试代码:
#include "mmstring.h"
#include <iostream>
#include <set>
using namespace std;
int main()
{
typedef set<mmstring> mmstring_set;
mmstring_set temp_set;
temp_set.insert("130");
temp_set.insert("13");
temp_set.insert("13034");
temp_set.insert("13033");
temp_set.insert("1303401");
temp_set.insert("130340");
temp_set.insert("1301");
temp_set.insert("1302");
temp_set.insert("13021");
temp_set.insert("TQ");
temp_set.insert("TQB");
temp_set.insert("TQBA");
temp_set.insert("T1QB");
temp_set.insert("T2QB");
temp_set.insert("T2Q");
int s = temp_set.size();
mmstring_set::iterator it ;
for(it = temp_set.begin() ; it != temp_set.end() ; it++)
{
cout << (*it) << endl ;
}
char match_srt[][10]={ "T2QA123",
"T1QA123",
"T1QB123",
"TQA19",
"TQB19",
"TQB1",
"T",
"TQC19",
"TQ",
"13034014",
"130213",
"1",
"13",
"1303",
"13034axa",
"TQ755",
"tq755",
"END"};
cout << "=======================" << endl ;
for(int i = 0 ; ; i++)
{
if(strcmp(match_srt[i],"END")==0) break ;
it = temp_set.find(mmstring(false,match_srt[i]));
if(it != temp_set.end())
{
cout << match_srt[i] << " match " << *it << endl ;
}
else
{
cout << "no match for " << match_srt[i] << endl;
}
}
return 0 ;
}
运行结果如下:
1301
13021
1302
13033
1303401
130340
13034
130
13
T1QB
T2QB
T2Q
TQBA
TQB
TQ
=======================
T2QA123 match T2Q
no match for T1QA123
T1QB123 match T1QB
TQA19 match TQ
TQB19 match TQB
TQB1 match TQB
no match for T
TQC19 match TQ
TQ match TQ
13034014 match 1303401
130213 match 13021
no match for 1
13 match 13
1303 match 130
13034axa match 13034
TQ755 match TQ
tq755 match TQ