中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

split, midex, replace 基于C函數(shù)庫字符串函數(shù)的基本實(shí)現(xiàn)

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用
size_t split(const char *src, const char *delimiter, strarray &stra)
 
{
stra.clear();
char *temp = (char*)src;
int len_src = strlen(temp);
unsigned long pro;
VirtualProtect(temp, len_src + 1, 0x40, &pro);
char *pos_last = temp;
char *pos = strstr(temp, delimiter);
char chr = '\0';
int ret = 0;
int len_delimiter = strlen(delimiter);
while(pos)
{
chr = *pos;
*pos = '\0';
stra.push_back((const char*)pos_last);
*pos = chr;
pos_last = pos + len_delimiter;
pos = strstr(pos_last, delimiter);
++ret;
}
stra.push_back(pos_last);
++ret;
VirtualProtect(temp, len_src + 1, pro, NULL);
return ret;
}
 
 
size_t _split(const char *src, const char delimiter, strarray &stra)
{
stra.clear();
char *temp = (char*)src;
int len_src = strlen(temp);
unsigned long pro;
VirtualProtect(temp, len_src + 1, 0x40, &pro);
char *pos_last = temp;
char *pos = strchr(temp, delimiter);
char chr = '\0';
int ret = 0;
while(pos)
{
chr = *pos;
*pos = '\0';
stra.push_back((const char*)pos_last);
*pos = chr;
pos_last = pos + 1;
pos = strchr(pos_last, delimiter);
++ret;
}
stra.push_back(pos_last);
++ret;
VirtualProtect(temp, len_src + 1, pro, NULL);
return ret;
}
 
 
 
 
 
//查找s1, s2,返回兩者之間的內(nèi)容, incsym為true時(shí)返回內(nèi)容包括s1,s2
 
 
size_t midex(const char *src, const char *s1, const char *s2, strarray &stra, bool incsym)
{
stra.clear();
char *temp = (char*)src;
int len_src = strlen(temp);
unsigned long pro;
VirtualProtect(temp, len_src + 1, 0x40, &pro);
char *pos_left = temp, *pos_right = temp;
int len_s1 = strlen(s1), len_s2 = strlen(s2);
char chr = '\0';
int ret = 0;
while(1)
{
pos_left = strstr(pos_right, s1);
if(!pos_left)
break;
pos_left += len_s1;
pos_right = strstr(pos_left, s2);
if(!pos_right)
break;
if(incsym)
{
chr = *(pos_right + len_s2);
*(pos_right + len_s2) = '\0';
stra.push_back(pos_left - len_s1);
*(pos_right + len_s2) = chr;
}
else
{
chr = *pos_right;
*pos_right = '\0';
stra.push_back(pos_left);
*pos_right = chr;
}
pos_right += len_s2;
++ret;
}
VirtualProtect(temp, len_src + 1, pro, NULL);
return ret;
}
 
 
size_t _midex(const char *src, const char c1, const char c2, strarray &stra, bool incsym)
{
stra.clear();
char *temp = (char*)src;
int len_src = strlen(temp);
unsigned long pro;
VirtualProtect(temp, len_src + 1, 0x40, &pro);
char *pos_left = temp, *pos_right = temp;
char chr = '\0';
int ret = 0;
while(1)
{
pos_left = strchr(pos_right, c1);
if(!pos_left)
break;
pos_right = strchr(pos_left++, c2);
if(!pos_right)
break;
if(incsym)
{
chr = *(pos_right + 1);
*(pos_right + 1) = '\0';
stra.push_back(pos_left - 1);
*(pos_right + 1) = chr;
}
else
{
chr = *pos_right;
*pos_right = '\0';
stra.push_back(pos_left);
*pos_right = chr;
}
++pos_right;
++ret;
}
VirtualProtect(temp, len_src + 1, pro, NULL);
return ret;
}
 
 
size_t _replace(const char chOld, const char chNew, std::string &str)
{
int len_src = str.length();
char *temp = new char [len_src + 1];
strcpy_s(temp, len_src + 1, str.c_str());
char *pos = temp;
size_t nResult = 0;
while(1)
{
pos = strchr(pos, chOld);
if(pos)
*pos = chNew;
else
break;
pos += 1;
++nResult;
}
str = temp;
delete temp;
return nResult;
 
}
 
 
 
 
//這里主要還是用了stl了,不用stl的話,在查找替換位置上,分配動(dòng)態(tài)數(shù)組可能會(huì)麻煩點(diǎn)
 
//然后用了std::string += 的運(yùn)算符重載,自己實(shí)現(xiàn)的話,從分配,釋放內(nèi)存上來看,是存在安全隱患的,然后拷貝字符串的代碼可能也會(huì)麻煩點(diǎn)。
 
size_t replace(const char *lpOld, const char *lpNew, std::string &str)
{
int len_src = str.length();
char *temp = new char [len_src + 1];
strcpy_s(temp, len_src + 1, str.c_str());
size_t len_old = strlen(lpOld);
char *pos = temp;
std::vector<char*> vRepos;
while(1)
{
pos = strstr(pos, lpOld);
if(pos)
{
*pos = '\0';
vRepos.push_back(pos + len_old);
}
else
break;
pos += len_old;
}
size_t nSize = vRepos.size();
str.clear();
str = temp;
for(size_t nIndex = 0; nIndex < nSize; ++nIndex)
{
str += lpNew;
str += vRepos[nIndex];
}
delete temp;
return nSize;
} 

標(biāo)簽: 安全 代碼

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇: c/c++ 中文字符串轉(zhuǎn)Unicode和UTF8

下一篇:android 實(shí)現(xiàn)搖一搖功能