22.c++-替换字符串中指定的字符(随手源码)

void WReplace(wchar_t* pDstOut, wchar_t* pSrcIn,  wchar_t* pSrcRpl,  wchar_t* pDstRpl)
{
	wchar_t* pi = pSrcIn;
	wchar_t* po = pDstOut;
	
	int nSrcRplLen = wcslen( pSrcRpl );
	int nDstRplLen = wcslen( pDstRpl );
	
	wchar_t *p = NULL;
	int nLen = 0;
	
	do
	{
		// 找到下一个替换点
		p = wcsstr(pi, pSrcRpl);
		
		if (p != NULL) 
		{
			// 拷贝上一个替换点和下一个替换点中间的字符串
			nLen = p - pi;
			
			memcpy(po, pi, nLen*sizeof(wchar_t));
			
			// 拷贝需要替换的字符串
			memcpy( po + nLen, pDstRpl, nDstRplLen*sizeof(wchar_t));
		}
		else
		{
			wcscpy(po, pi);
			
			// 如果没有需要拷贝的字符串,说明循环应该结束
			break;
		}
		
		pi = p + nSrcRplLen;
		po = po + nLen + nDstRplLen;
		
	} while (p != NULL);
}

你可能感兴趣的:(22.c++-替换字符串中指定的字符(随手源码))