È una funzionalità o un bug di clang c++11 std::regex_match?

È una funzionalità o un bug di clang c++11 std::regex_match?


Ho notato che un'espressione regolare, contenente due pattern con condizione OR, non corrisponde a una stringa di esempio se il primo pattern è una parte iniziale del secondo pattern (testato su clang 3.5 e clang 3.8):


std::regex_match("ab", std::regex("(ab|a)")) == true

ma


std::regex_match("ab", std::regex("(a|ab)")) == false

Penso true è logicamente corretto in entrambi i casi.


Clang e OSX:


$ cat > test.cpp
#include <string>
#include <regex>
#include <iostream>
int main() {
std::cout << std::regex_match("ab", std::regex("(a|ab)")) << std::endl;
std::cout << std::regex_match("ab", std::regex("(ab|a)")) << std::endl;
return 0;
}
^C
$ clang++ -v
Apple LLVM version 8.1.0 (clang-802.0.41)
Target: x86_64-apple-darwin16.5.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$ clang++ ./test.cpp -o test
$ ./test
0
1

Clang e FreeBSD:


$ cat > test.cpp
#include <string>
#include <regex>
#include <iostream>
int main() {
std::cout << std::regex_match("ab", std::regex("(a|ab)")) << std::endl;
std::cout << std::regex_match("ab", std::regex("(ab|a)")) << std::endl;
return 0;
}
^C
$ clang++ -v
FreeBSD clang version 3.8.0 (tags/RELEASE_380/final 262564) (based on LLVM 3.8.0)
Target: x86_64-unknown-freebsd11.0
Thread model: posix
InstalledDir: /usr/bin
$ clang++ ./test.cpp -o test
$ ./test
0
1

Linux e GCC:


$ cat > test.cpp
#include <string>
#include <regex>
#include <iostream>
int main() {
std::cout << std::regex_match("ab", std::regex("(a|ab)")) << std::endl;
std::cout << std::regex_match("ab", std::regex("(ab|a)")) << std::endl;
return 0;
}
^C
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.1-2ubuntu1~16.04' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.4.1 20160904 (Ubuntu 5.4.1-2ubuntu1~16.04)
$ g++ -std=gnu++11 ./test.cpp -o test
$ ./test
1
1

Chiesto da Max Fomichev

Risposte:


ECMAScript (la sintassi regex predefinita) tenta di abbinare le alternative in ordine, fermandosi al primo successo, il che significa che nella ricerca ordinaria (a la regex_search ) la regex a|ab non corrisponde mai all'intero ab; corrisponde sempre solo al a parte.


Lo standard era ambiguo su cosa regex_match avrebbe dovuto fare in questo caso, portando a una divergenza di attuazione. Vedi LWG numero 2273 per le interpretazioni concorrenti. Alla fine lo standard è stato modificato (vedi la risoluzione di quel problema) per chiarire che regex_match considera solo potenziali corrispondenze che corrispondono all'intera sequenza di input, come chiarisce l'esempio aggiunto allo standard:


std::regex re("Get|GetValue");
std::cmatch m;
regex_search("GetValue", m, re); // returns true, and m[0] contains "Get"
regex_match ("GetValue", m, re); // returns true, and m[0] contains "GetValue"

Il <regex> originale l'implementazione in libc++ utilizzava l'altra interpretazione, tuttavia, e semplicemente non veniva aggiornata per corrispondere alla risoluzione fino a poco tempo. Clang 4.0 ora stampa 1 1 .


Alcune risposte al codice


std::regex_match("ab", std::regex("(ab|a)")) == true 
std::regex_match("ab", std::regex("(a|ab)")) == false 
$ cat >
test.cpp #include <string>
#include <regex>
#include <iostream>
int main() {
std::cout <<
std::regex_match("ab", std::regex("(a|ab)")) <<
std::endl;
std::cout <<
std::regex_match("ab", std::regex("(ab|a)")) <<
std::endl;
return 0;
} ^C $ clang++ -v Apple LLVM version 8.1.0 (clang-802.0.41) Target: x86_64-apple-darwin16.5.0 Thread model: posix InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin $ clang++ ./test.cpp -o test $ ./test 0 1
$ cat >
test.cpp #include <string>
#include <regex>
#include <iostream>
int main() {
std::cout <<
std::regex_match("ab", std::regex("(a|ab)")) <<
std::endl;
std::cout <<
std::regex_match("ab", std::regex("(ab|a)")) <<
std::endl;
return 0;
} ^C $ clang++ -v FreeBSD clang version 3.8.0 (tags/RELEASE_380/final 262564) (based on LLVM 3.8.0) Target: x86_64-unknown-freebsd11.0 Thread model: posix InstalledDir: /usr/bin $ clang++ ./test.cpp -o test $ ./test 0 1
$ cat >
test.cpp #include <string>
#include <regex>
#include <iostream>
int main() {
std::cout <<
std::regex_match("ab", std::regex("(a|ab)")) <<
std::endl;
std::cout <<
std::regex_match("ab", std::regex("(ab|a)")) <<
std::endl;
return 0;
} ^C $ g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.1-2ubuntu1~16.04' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 5.4.1 20160904 (Ubuntu 5.4.1-2ubuntu1~16.04) $ g++ -std=gnu++11 ./test.cpp -o test $ ./test 1 1
std::regex re("Get|GetValue");
std::cmatch m;
regex_search("GetValue", m, re);
// returns true, and m[0] contains "Get" regex_match ("GetValue", m, re);
// returns true, and m[0] contains "GetValue"