關(guān)于c++11與c風(fēng)格路徑拼接的速度對(duì)比_第1頁(yè)
關(guān)于c++11與c風(fēng)格路徑拼接的速度對(duì)比_第2頁(yè)
關(guān)于c++11與c風(fēng)格路徑拼接的速度對(duì)比_第3頁(yè)
關(guān)于c++11與c風(fēng)格路徑拼接的速度對(duì)比_第4頁(yè)
關(guān)于c++11與c風(fēng)格路徑拼接的速度對(duì)比_第5頁(yè)
已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第關(guān)于c++11與c風(fēng)格路徑拼接的速度對(duì)比目錄這里用c++11的stingstream實(shí)現(xiàn)一個(gè)用c重新實(shí)現(xiàn)一遍c++11的std庫(kù)中沒(méi)有提供路徑拼接的功能

比如我需要將d:\\temp\\robin和..\\config.ini路徑拼接,

這里用c++11的stingstream實(shí)現(xiàn)一個(gè)

stringreplace_str(stringstr,conststringto_replaced,conststringnewchars)

for(string::size_typepos(0);pos!=string::npos;pos+=newchars.length())

{

pos=str.find(to_replaced,pos);

if(pos!=string::npos)

str.replace(pos,to_replaced.length(),newchars);

else

break;

}

return

str;

//windows

std::stringcombinePath(constchar*dir,constchar*name)

{

//printf("%s

+

%s\n",dir,name);

if(dir==nullptr||name==nullptr)

return"";

stringtemp=dir;

replace_str(temp,"/","\\");

std::stringstreamoss(temp);

std::dequestd::stringvecDir;

std::dequestd::stringvecName;

stringpart;

while(std::getline(oss,part,'\\'))

{

if(part.length()==0)

continue;

vecDir.emplace_back(part);

}

temp=name;

replace_str(temp,"/","\\");

oss.clear();

oss.str(temp);

while(std::getline(oss,part,'\\'))

{

if(part.length()==0)

continue;

vecName.emplace_back(part);

}

intindex=0;

while(indexvecName.size())

{

if(vecName[0]==".")

{

vecName.pop_front();

}

//elseif(vecName[0].find("..")!=vecName[0].npos)

elseif(vecName[0]=="..")

{

vecName.pop_front();

if(vecDir.size()1)

vecDir.pop_back();

}

else

{

vecDir.emplace_back(vecName[0]);

vecName.pop_front();

}

}

oss.clear();

oss.str("");

for(inti=0;ivecDir.size();i++)

{

ossvecDir[i];

if(vecDir.size()==1||i(vecDir.size()-1))

{

oss"\\";

}

}

returnoss.str();

}

測(cè)試方法:

voidtest1()

coutcombinePath("d:\\temp\\robin\\","..\\demo\\config.ini")endl;

coutendl;

coutcombinePath("d:","..\\demo\\config.ini")endl;

coutendl;

coutcombinePath("d:\\temp\\robin","../demo/config.ini")endl;

coutendl;

coutcombinePath("d:\\temp\\robin","./config.ini")endl;

coutendl;

coutcombinePath("d:\\temp\\robin","config.ini")endl;

coutendl;

coutcombinePath("d:\\temp\\robin\\","\\config.ini")endl;

coutendl;

}

測(cè)試發(fā)現(xiàn),release使用O2優(yōu)化,能平均在2~4微秒左右,還是不夠快啊,

用c重新實(shí)現(xiàn)一遍

//字符串范圍內(nèi),逆向查找

constchar*

strrfind(constchar*begin,constchar*end,constcharc)

constchar*buf=end;

while(buf=begin)

{

if(*buf==c)

returnbuf;

buf--;

}

returnnullptr;

size_t

combinePathC(char**buffer,constchar*dir,constchar*name)

intn1=strlen(dir);

intn2=strlen(name);

size_tlen=

n1+n2+2;

char*buf=newchar[len];

*buffer=buf;

memcpy(buf,dir,n1);

size_tlen1=n1;

//末尾保證有一個(gè)"\"

if(buf[n1-1]=='\\')

{

len1=n1;

}

else

{

buf[n1]='\\';

len1=n1+1;

}

buf[len1]='\0';

//len1++示當(dāng)前拼接的長(zhǎng)度

size_tindex=0;

char*rPart=(char*)name;

size_tlen2=n2;

while(indexn2)

//使用index向后滑動(dòng),直到末尾

{

//滑動(dòng)后當(dāng)前位置

rPart=(char*)name+index;

char*tmp=(char*)strchr(rPart,'\\');

if(tmp==nullptr)

//endhere

{

//拼接剩下的

len2=n2-index;

memcpy(buf+len1,rPart,len2);

len1+=len2;

buf[len1]='\0';

len1++;

break;

}

//當(dāng)前找到的長(zhǎng)度

len2=tmp-rPart;

if(len2==0)

//遇到"\config.ini",去掉1個(gè)字符

{

index+=1;

}

elseif(len2==1rPart[0]=='.')

//遇到".\config.ini",去掉2個(gè)字符

{

index+=2;

}

elseif(len2=2rPart[0]=='.')

//遇到"..\config.ini","..x\config.ini"去掉3個(gè)字符,末尾去掉一個(gè)目錄

{

index+=len2+1;

constchar*back=strrfind(buf,buf+len1-2,'\\');

//從末尾的"\"前一個(gè)字符開(kāi)始找

if(back==nullptr)

{

//dir當(dāng)前是這樣的情況,"d:\”

}

elseif((back-dir)==2)

//dir當(dāng)前是這樣的情況,"d:\temp\”

{

len1=3;

buf[3]='\0';

}

else

//dir當(dāng)前是這樣的情況,"d:\temp\test1\”

{

len1=back-buf+1;

buf[len1]='\0';

}

}

else

//

遇到首字符不為點(diǎn)"x\config.ini",

{

index+=len2+1;

memcpy(buf+len1,name+index,len2+1);

len1+=len2+1;

}

}

returnlen1;

}

測(cè)試一下:

char*buf=nullptr;

size_tlen;

len=combinePathC(buf,"d:\\temp\\robin\\","config.ini");

coutbufendl;

len=combinePathC(buf,"d:\\temp\\robin","config.ini");

coutbufendl;*/

len=combinePathC(buf,"d:\\temp\\robin",

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論