2017-07-24 95 views
-1

下面,我产生了破碎的代码和相同的固定版本。问题是我不能完全向自己解释为什么前者不起作用,但后者却起作用。我显然需要回顾一下C++语言的一些非常基本的概念:能否提供我应该查看的内容的指针,并且可能还会解释为什么我会用破碎的代码得到结果。基本概念与std ::字符串引用,std :: regex和boost :: filesystem

在代码中引用的'../docs/'目录中,我只是在linux上使用'touch'命令来创建大量不同长度的doc ...... html文件。

#include <iostream> 
#include <regex> 
#include <boost/filesystem.hpp> 

namespace fs = boost::filesystem; 

int main() { 
     fs::path p("../docs/"); 

     for (auto& dir_it : fs::directory_iterator(p)) { 
       std::regex re = std::regex("^(doc[a-z]+)\\.html$"); 
       std::smatch matches; 
       // BROKEN HERE: 
       if (std::regex_match(dir_it.path().filename().string(), matches, re)) { 
         std::cout << "\t\t" <<dir_it.path().filename().string(); 
         std::cout << "\t\t\t" << matches[1] << std::endl; 
       } 
     } 

     return 0; 
} 

产地:

 documentati.html      ati 
     documentationt.html      �}:ationt 
     document.html     document 
     documenta.html     documenta 
     docume.html      docume 
     documentat.html     documentat 
     docum.html      docum 
     documentatio.html      ��:atio 
     documen.html     documen 
     docu.html      docu 
     documentation.html      ��:ation 
     documaeuaoeu.html      ��:aoeu 

注1:错误上述被触发以文件名,它们是一定长度以上。我只理解这是因为std :: string对象正在调整其自身。

注2:上面的代码比在以下问题中使用的代码非常相似,但的boost :: regex_match来代替std :: regex_match: Can I use a mask to iterate files in a directory with Boost?
它使用之前,以及对我的工作,但现在我使用GCC 5.4而不是GCC 4.6,std :: regex代替boost :: regex,C++ 11,以及更新版本的boost :: filesystem。哪些变化是相关的,导致工作代码被破坏?

修正:

#include <iostream> 
#include <regex> 
#include <boost/filesystem.hpp> 

namespace fs = boost::filesystem; 

int main() { 
     fs::path p("../docs/"); 

     for (auto& dir_it : fs::directory_iterator(p)) { 
       std::regex re = std::regex("^(doc[a-z]+)\\.html$"); 
       std::smatch matches; 
       std::string p = dir_it.path().filename().string(); 
       if (std::regex_match(p, matches, re)) { 
         std::cout << "\t\t" <<dir_it.path().filename().string(); 
         std::cout << "\t\t\t" << matches[1] << std::endl; 
       } 
     } 

     return 0; 
} 

生产:

 documentati.html      documentati 
     documentationt.html      documentationt 
     document.html     document 
     documenta.html     documenta 
     docume.html      docume 
     documentat.html     documentat 
     docum.html      docum 
     documentatio.html      documentatio 
     documen.html     documen 
     docu.html      docu 
     documentation.html      documentation 
     documaeuaoeu.html      documaeuaoeu 

使用升压1.62.0-R1和gcc(Gentoo的5.4.0-R3),升压::文件系统文件不会出现()。filename()。string()返回: http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/reference.html#path-filename
看起来它取决于:
Why does boost::filesystem::path::string() return by value on Windows and by reference on POSIX?

回答

1

std::smatch不存储构成匹配的字符的副本,但是只有成对的迭代器存储到被搜索的原始字符串中。

第一个片段将一个临时字符串传递给std::regex_match。当你开始检查matches时,该字符串消失了,matches所持有的迭代器全部悬空。该程序然后通过尝试使用它们展示未定义的行为。

在第二个片段中,传递到std::regex_match的字符串在使用matches时仍然存在,所以一切正常。


注1:那问题表现不同,这取决于文件名长度可能是由于小串的优化。 std::string实现通常会在类实例中保留一个小缓冲区,并在那里存储短字符串;而更长的字符串被分配在堆上。可能由temp字符串先前占用的堆栈空间仍然完好无损,而堆空间已被重新用于其他分配。该方案展示未定义的行为 - “似乎有效”是UB的一种可能的表现形式。


注2:它并没有多大关系是否path::string()收益率的值或引用。 path::filename()按价值返回,因此dir_it.path().filename().string()将被破坏得太早,无论它是临时对象dir_it.path().filename()的成员,还是由string()实时制造。

+0

哦!现在我明白了,看起来很明显。 +1。谢谢你的时间。 – augustin