📄 string.cpp
字号:
//
// 格式化表示字节数的数字,如 2048 被格式化为“2.00 k”
//
CString FormatBytes ( double fFileSize )
{
CString csRet;
if ( fFileSize < 0.0 )
{
ASSERT ( FALSE );
return csRet;
}
if ( fFileSize < 1024.0 )
{
csRet.Format ( _T("%.2f B"), fFileSize );
}
else if ( fFileSize >= 1024.0 && fFileSize < 1024.0*1024.0 )
{
csRet.Format ( _T("%.2f K"), fFileSize/1024.0 );
}
else if ( fFileSize >= 1024.0*1024.0 && fFileSize < 1024.0*1024.0*1024.0 )
{
csRet.Format ( _T("%.2f M"), fFileSize/(1024.0*1024.0) );
}
else
{
csRet.Format ( _T("%.2f G"), fFileSize/(1024.0*1024.0*1024.0) );
}
return csRet;
}
//
// 为了方便 strchr() 或 strrchr() 函数正确查找字符,将字符串中的中文字符串用指定的字符替换掉
//
void ReplaceChineseStrToEnglish ( TCHAR *szBuf, TCHAR cReplace )
{
if ( !szBuf ) return;
for ( int i=0; szBuf[i] != _T('\0'); i++ )
{
if ( szBuf[i] < 0 && szBuf[i+1] != _T('\0') )
{
szBuf[i] = cReplace;
szBuf[i+1] = cReplace;
i ++;
}
}
}
TCHAR *hwStrChr ( LPCTSTR string, int c )
{
if ( !string ) return NULL;
CString csString = string;
ReplaceChineseStrToEnglish ( csString.GetBuffer(0), (c>=127) ? (c-1) : (c+1) );
csString.ReleaseBuffer ();
int nPos = csString.Find ( c );
if ( nPos < 0 ) return NULL;
return ( (TCHAR*)string + nPos );
}
//
// 净化字符串
//
void CleanseString(CString &cs, LPCTSTR lpszApp/*=NULL*/)
{
if ( cs.IsEmpty() ) return;
cs.TrimLeft ();
cs.TrimRight ();
if ( lpszApp && lstrlen(lpszApp) > 0 )
{
cs.TrimLeft ( lpszApp );
cs.TrimRight ( lpszApp );
}
}
//
// 将字符串分开并添加到字符串数组中,如将字符串“aaa\nbbb\nccc”分开为:
// “aaa”、“bbb”、“ccc”添加到 StrAry 中
// 注意:这个函数里使用了 strtok() C库函数,strtok() 函数是不能嵌套使用的
// 所以,如果在调用 PartStringAndAddToStrAry() 的上层调用中已经使用了 strtok()
// 那么将不能调用本函数 PartStringAndAddToStrAry(),可以调用 PartStringAndAddToStrAry()
// 的另一版本, 它是简单地通过 strchr() 查找完成的.
//
int PartStringAndAddToStrAry ( LPCTSTR pStr, CStringArray &StrAry, LPCTSTR seps/*="\t\r\n"*/ )
{
StrAry.RemoveAll();
TCHAR *token = strtok_hw( (TCHAR *)pStr, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
StrAry.Add ( token );
/* Get next token: */
token = strtok_hw( NULL, seps );
}
return (int)StrAry.GetSize();
}
//
// 将字符串分开并添加到字符串数组中,如将字符串“aaa\nbbb\nccc”分开为:
// “aaa”、“bbb”、“ccc”添加到 StrAry 中,分离标志为 nPartFlag
//
int PartStringAndAddToStrAry ( LPCTSTR pStr, CStringArray &StrAry, TCHAR nPartFlag )
{
TCHAR *pStart = (TCHAR *)pStr;
TCHAR *pFind = NULL;
TCHAR szTempBuf[10240] = {0};
while ( pFind = strchr_hw ( pStart, nPartFlag ) )
{
int nLenBytes = (int)((reinterpret_cast<char*>(pFind)) - (reinterpret_cast<char*>(pStart)));
if ( nLenBytes > 0 )
{
if ( nLenBytes > sizeof(szTempBuf)-2*sizeof(TCHAR) )
nLenBytes = sizeof(szTempBuf)-2*sizeof(TCHAR);
memcpy ( szTempBuf, pStart, nLenBytes );
szTempBuf [ nLenBytes/sizeof(TCHAR) ] = _T('\0');
CString csTempStr = szTempBuf;
csTempStr.Trim ();
StrAry.Add ( csTempStr );
}
else
{
StrAry.Add ( _T("") );
}
pStart = pFind + 1;
}
if ( (int)( pStart - pStr ) < (int)lstrlen ( pStr ) )
StrAry.Add ( pStart );
return (int)StrAry.GetSize();
}
//
// 将字符串分开并添加到字符串数组中,如将字符串“[aaa], [nbbb], [ccc]”分开为:
// “aaa”、“bbb”、“ccc”添加到 StrAry 中,分离标志为 nFirstFlag='[', nSecondFlag=']'
//
int PartStringAndAddToStrAry ( LPCTSTR lpszStr, CStringArray &StrAry, TCHAR nFirstFlag, TCHAR nSecondFlag )
{
StrAry.RemoveAll();
int nStartPos = 0, nEndPos = 0;
CString csBigString = GET_SAFE_STRING ( lpszStr );
BOOL bSentence = FALSE;
for ( ;; )
{
TCHAR cFindChar = ( bSentence ? nSecondFlag : nFirstFlag );
int nFindPos = StrFind ( csBigString, nEndPos, cFindChar, _T('\0') );
if ( nFindPos < 0 )
{
if ( bSentence )
{
CString csTemp = csBigString.Mid ( nStartPos+1 );
if ( csTemp.GetLength() > 0 )
StrAry.Add ( csTemp );
}
break;
}
if ( !bSentence )
{
nStartPos = nFindPos;
bSentence = TRUE;
}
else
{
nEndPos = nFindPos;
CString csTemp = csBigString.Mid ( nStartPos+1, nEndPos-nStartPos-1 );
StrAry.Add ( csTemp );
bSentence = FALSE;
}
nEndPos = nFindPos + 1;
}
return (int)StrAry.GetSize();
}
//
// 将字符串数字(或指定字符)分开来并添加到数组中,如将字符串“123 456;789”分开为:
// “123”、“456”、“789”添加到 StrAry 中
// 又如:“123 *34#,456*;987;”,seps="*#",那么分开后为:“123”、“*34#”、“456*”、
// “987”,如果不指定seps,那结果就是“123”、“34”、“456”、“987”。
// 返回分离后的数字字符串个数, 如果失败返回 -1
//
int PartDigitOrSpecifyCharAndAddToStrAry ( LPCTSTR pStr, CStringArray &StrAry, TCHAR *seps/*=NULL*/ )
{
if ( !pStr ) return -1;
int nStrLen = lstrlen ( pStr );
CString csDigitStr;
for ( int i=0; i<nStrLen; i++ )
{
if ( isdigit ( pStr[i] ) || ( seps && strchr_hw ( seps, pStr[i] ) ) )
csDigitStr += pStr[i];
else if ( csDigitStr.GetLength() > 0 )
{
StrAry.Add ( csDigitStr );
csDigitStr = _T("");
}
}
if ( csDigitStr.GetLength() > 0 )
StrAry.Add ( csDigitStr );
return (int)StrAry.GetSize();
}
//
// 从字符串 lpszOrg 中找 cFind 系列字符任何一个字符出现的位置,能防止中文后半部分被误找的情况发生
// 注意:cFind 输入应该以'\0'作为结束标志
// ret : ------------------------------------------------------------------
// >= nStart - 找到的位置
// -1 - 没找
//
int StrFind ( LPCTSTR lpszOrg, int nStart, TCHAR cFind, ... )
{
if ( !lpszOrg || nStart < 0 ) return -1;
CUIntArray UIntAryFind;
// 取得所有的 cFind 输入添加到数组中
va_list list;
va_start( list, cFind );
while ( cFind != _T('\0') )
{
UIntAryFind.Add ( cFind );
cFind = va_arg( list, TCHAR );
}
va_end( list );
int nChineseCharCount = 0;
BOOL bChineseStart = FALSE;
int nFoundPos = -1;
for ( int i=nStart; lpszOrg[i] != _T('\0'); i++ )
{
TCHAR chCur = lpszOrg[i];
if ( chCur < 0 )
{
nChineseCharCount ++;
bChineseStart = TRUE;
}
else
{
if ( (nChineseCharCount % 2) == 1 )
nChineseCharCount ++;
else
bChineseStart = FALSE;
}
if (
!bChineseStart
&&
FindFromArray ( UIntAryFind, chCur ) >= 0
)
{
nFoundPos = i;
break;
}
}
return nFoundPos;
}
void UIntAry2StrAry ( IN CUIntArray &UIntAry, OUT CStringArray &StrAry )
{
StrAry.RemoveAll();
for ( int i=0; i<UIntAry.GetSize(); i++ )
{
CString csText;
csText.Format ( _T("%u"), UIntAry.GetAt(i) );
StrAry.Add ( csText );
}
}
// 能被切分的字符 ( 不可显示字符 或者 汉字英文的接口处 或者 非数字亦非字母的字符 )
#define CAN_SYNCOPATE_CHAR(c) ( !isgraph(c) || (c) < 0 || (!isalnum(c) && !isalpha(c)) )
//
// 从一个句子中找到可以切分的地方,比如,输入:“how are you”,预切分点 nPreparePos 为 5,那么本函数
// 为了保证单词不被分开,而自动找到切分点位置为 3
// return : 可以切分的字符串长度
// 注意:如果是UNICODE编码的字符串,这里返回的只是多字节的字符串的位置,需要取得子字符串后再求长度
//
int FindSentenceSyncopatePostion ( char *szData, int nPreparePos )
{
if ( !szData || nPreparePos < 0 ) return 0;
int nStrLen = (int)strlen(szData);
if ( nPreparePos >= nStrLen-1 )
nPreparePos = nStrLen-1;
// 先做第一次扫描看有没有汉字,如果有,那最靠近 nPreparePos 位置汉字
// 的第一个字节和最后一个字节的位置
int nChineseStartPos = -1, nChineseEndPos = -1;
BOOL bChineseStart = FALSE;
int nStep = 1;
for ( int i=0; i<=nPreparePos; i+=nStep )
{
if ( szData[i] < 0 )
{
if ( !bChineseStart )
{
bChineseStart = TRUE;
nChineseStartPos = i;
nStep = 2;
}
}
else
{
if ( bChineseStart )
{
bChineseStart = FALSE;
nChineseEndPos = i-1;
nStep = 1;
}
}
}
if ( nChineseStartPos >= 0 && nChineseEndPos <= nChineseStartPos )
{
nChineseEndPos = nPreparePos;
}
// 是汉字
if ( nChineseEndPos == nPreparePos )
{
int nChineseLen = nChineseEndPos-nChineseStartPos+1;
if ( nChineseLen < 2 ) return nPreparePos;
if ( (nChineseLen % 2) == 0 ) // 偶数个
return nPreparePos+1;
else
return nPreparePos;
}
// 是字母或符号
else
{
if ( nPreparePos >= nStrLen-1 )
return nStrLen;
// 往回找一个可被切分的字符
for ( int i=nPreparePos; i>nPreparePos/2; i-- )
{
if ( CAN_SYNCOPATE_CHAR ( szData[i] ) || nChineseEndPos == i )
return i+1;
}
}
return nPreparePos+1;
}
//
// 在保证单词或汉字不被分断的前提下切分字符串
// return : ----------------------------------------------------------------------
// 切分后的字符串保存在返回的 CStringArray 数组中,用完后一定要删除这个指针
//
CStringArray *SyncopateString ( char *szText, int SyncopateLength )
{
if ( !szText || SyncopateLength < 1 )
return NULL;
int nTextTotalLen = (int)strlen (szText);
if ( nTextTotalLen < 1 ) return NULL;
int nSentLenCount = 0; // 已经发送的长度计数
int nRemainLenCount = nTextTotalLen; // 剩余还需要发送的长度
CStringArray *pStrAry = NULL;
char *szOneText = NULL;
szOneText = new char[SyncopateLength+1];
if ( !szOneText ) goto finished;
pStrAry = new CStringArray;
if ( !pStrAry ) goto finished;
while ( nRemainLenCount > 0 )
{
char* pStart = szText + nSentLenCount;
if ( *pStart == '\0' ) break;
int nSendLenThisTime = FindSentenceSyncopatePostion ( pStart, SyncopateLength-1 );
ASSERT ( nSendLenThisTime <= SyncopateLength );
if ( nSendLenThisTime < 1 ) break;
memset ( szOneText, 0, SyncopateLength+1 );
strncpy ( szOneText, pStart, nSendLenThisTime );
pStrAry->Add ( GetCompatibleString(szOneText,FALSE) );
nSentLenCount += nSendLenThisTime;
nRemainLenCount -= nSendLenThisTime;
}
finished:
if ( szOneText ) delete[] szOneText;
return pStrAry;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -