Regex error using MSVC 2013

懵懂的女人 提交于 2019-12-06 20:03:26

You get a Stack overflow (parameters: 0x00000001, 0x00312FFC) exception as the pattern is not efficient. I think it is related to how std::regex processes repeated groups (you have define one with a +-quantifier group (.|\n)+). This pattern matches each and every character that is not a newline (.), or a newline (\n), and then stores the match inside a buffer. Then, an issue with the iterator debugging happens only in Debug mode. The std::_Orphan_Me is the place where the break occurs, and it is considered the most "expensive" method when matching strings. See performance killer -Debug Iterator Support in Visual studio

You should either switch to the Release mode, or test with a regex that does not require the use of the repeated groups, like any non-null character [^\x00] with lazy quantifier *?:

std::string str1 = "<GLVertex>\n#version 450 core\nlayout(location = 0) in vec3 pos;\nin VertexInfo{\n    vec2 uv;\n}vertexInfo;\nvoid main(){\n    gl_Position = vec4(pos, 1.0);\n    vertexInfo.uv = pos.xy;\n}\n<GLVertex/>\n<GLFragment>\n#version 450 core\nlayout(location = 0) uniform sampler2D g_map;\nuniform Color {\n    vec4 color;\n};\nlayout(location = 0) out vec4 fragColor;\nvoid main(){\n    fragColor = texture(g_map, vertexInfo.uv);\n}\n<GLFragment/>"; 
std::regex reg1("<GLVertex>([^\\x00]*?)<GLVertex/>");
std::smatch find1;
if (std::regex_search(str1, find1, reg1)){
    std::cout << find1[1].str();
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!