📄 stringopt.cpp
字号:
// CharCode.cpp: implementation of the CCharCode class.
//
//////////////////////////////////////////////////////////////////////
/*
* Generated by MyEclipse Struts
*
* Written by Yang Huaisheng
* Homepage: http://codefan.spaces.live.com
* version 0.01
* create at 2006-04-30
*
* Distribute freely, except: don't remove my name from the source or
* documentation (don't take credit for my work), mark your changes (don't
* get me blamed for your possible bugs), don't alter or remove this
* notice.
* No warrantee of any kind, express or implied, is included with this
* software; use at your own risk, responsibility for damages (if any) to
* anyone resulting from the use of this software rests entirely with the
* user.
*
* Send bug reports, bug fixes, enhancements, requests, flames, etc. to
* codefan@hotmial.com
*
*/
#include "stdafx.h"
#include "StringOpt.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
char CCharCode::GetPYIndex(LPCTSTR szHanzi)
{
ASSERT(szHanzi);
UINT nCode = (BYTE(szHanzi[0]) << 8) + BYTE(szHanzi[1]);
if( nCode < 0xB0A1) return szHanzi[0];
if( nCode < 0xB0C5) return 'A';
if( nCode < 0xB2C1) return 'B';
if( nCode < 0xB4EE) return 'C';
if( nCode < 0xB6EA) return 'D';
if( nCode < 0xB7A2) return 'E';
if( nCode < 0xB8C1) return 'F';
if( nCode < 0xB9FE) return 'G';
if( nCode < 0xBBF7) return 'H';
if( nCode < 0xBFA6) return 'J';
if( nCode < 0xC0AC) return 'K';
if( nCode < 0xC2E8) return 'L';
if( nCode < 0xC4C3) return 'M';
if( nCode < 0xC5B6) return 'N';
if( nCode < 0xC5BE) return 'O';
if( nCode < 0xC6DA) return 'P';
if( nCode < 0xC8BB) return 'Q';
if( nCode < 0xC8F6) return 'R';
if( nCode < 0xCBFA) return 'S';
if( nCode < 0xCDDA) return 'T';
if( nCode < 0xCEF4) return 'W';
if( nCode < 0xD1B9) return 'X';
if( nCode < 0xD4D1) return 'Y';
if( nCode < 0xD7FA) return 'Z';
return '-';
}
CString CCharCode::GetPYABIndex(LPCTSTR szHanzi)
{
ASSERT(szHanzi);
int sl = strlen(szHanzi);
CString strRes("");
for(int i=0; i<sl;){
BYTE bCode = BYTE(szHanzi[i]);
if( bCode < 128){
strRes += szHanzi[i];
i++;
}else{
strRes += GetPYIndex(szHanzi+i);
i+=2;
}
}
return strRes;
}
CString CCharCode::DBCToSBC(LPCTSTR szHanzi)
{
ASSERT(szHanzi);
CString strRes("");
int sl = strlen(szHanzi);
for(int i=0; i<sl;){
if(BYTE(szHanzi[i]) > 128){
strRes += szHanzi[i];
strRes += szHanzi[i+1];
i+=2;
}else{
strRes += char(163);
strRes += char(128+BYTE(szHanzi[i]));
i++;
}
}
return strRes;
}
CString CCharCode::SBCToDBC(LPCTSTR szHanzi)
{
ASSERT(szHanzi);
CString strRes("");
int sl = strlen(szHanzi);
for(int i=0; i<sl;){
if(BYTE(szHanzi[i]) == 163){
strRes += char(BYTE(szHanzi[i+1])-128);
i+=2;
}else if (BYTE(szHanzi[i]) >= 128){
strRes += szHanzi[i];
i++;
strRes += szHanzi[i];
i++;
}else{
strRes += szHanzi[i];
i++;
}
}
return strRes;
}
void CStringOpt::GetStrNext(const char * szStr,const long sL,long * pNext)
{
long i,j;
i = 0; j = -1; pNext[0] = -1;
while(i<sL-1){
if(j == -1 || szStr[i] == szStr[j]){
i++; j++;
if( szStr[i] != szStr[j]) pNext[i] = j;
else pNext[i] = pNext[j];
}
else j = pNext[j];
}
}
long CStringOpt::StrFind_KMP(const char * szStr,const long nSL,const char * szSubStr,const long nSSL,const long * pNext, const long nStart)
{
long i,j;
i = nStart; j = -1;
while( i<nSL && j<nSSL){
if(j == -1 || szStr[i] == szSubStr[j]){
i++; j++;
} else
j = pNext[j];
}
if(j==nSSL) return i-nSSL;
return 0;
}
long CStringOpt::StrFind(const char * szStr, const long nSL,const char * szSubStr,const long nSSL, const long nStart)
{
long * pNext = new long[nSSL+1];
GetStrNext(szSubStr,nSSL,pNext);
long nInd = StrFind_KMP(szStr,nSL,szSubStr,nSSL,pNext,nStart);
delete [] pNext;
return nInd;
}
long CStringOpt::StrFind(const char * szStr,const char * szSubStr, const long nStart)
{
return StrFind(szStr,strlen(szStr),szSubStr,strlen(szSubStr),nStart);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -