From dffe9e0f1dde084f2aab8010345aeb1b7c8f7d4c Mon Sep 17 00:00:00 2001 From: Hans Wennborg Date: Thu, 6 Sep 2018 08:54:44 +0000 Subject: Merging r340609: ------------------------------------------------------------------------ r340609 | ldionne | 2018-08-24 16:10:28 +0200 (Fri, 24 Aug 2018) | 13 lines [libc++] Fix handling of negated character classes in regex Summary: This commit fixes a regression introduced in r316095, where we don't match inverted character classes when there's no negated characrers in the []'s. rdar://problem/43060054 Reviewers: mclow.lists, timshen, EricWF Subscribers: christof, dexonsmith, cfe-commits Differential Revision: https://reviews.llvm.org/D50534 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/libcxx/branches/release_70@341529 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/regex | 15 +++----- .../inverted_character_classes.pass.cpp | 44 ++++++++++++++++++++++ .../re.alg.search/invert_neg_word_search.pass.cpp | 2 +- 3 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 test/std/re/re.alg/re.alg.match/inverted_character_classes.pass.cpp diff --git a/include/regex b/include/regex index 84aacc029..dcdb14af9 100644 --- a/include/regex +++ b/include/regex @@ -2414,20 +2414,17 @@ __bracket_expression<_CharT, _Traits>::__exec(__state& __s) const goto __exit; } } - // set of "__found" chars = + // When there's at least one of __neg_chars_ and __neg_mask_, the set + // of "__found" chars is // union(complement(union(__neg_chars_, __neg_mask_)), // other cases...) // - // __neg_chars_ and __neg_mask_'d better be handled together, as there - // are no short circuit opportunities. - // - // In addition, when __neg_mask_/__neg_chars_ is empty, they should be - // treated as all ones/all chars. + // It doesn't make sense to check this when there are no __neg_chars_ + // and no __neg_mask_. + if (!(__neg_mask_ == 0 && __neg_chars_.empty())) { - const bool __in_neg_mask = (__neg_mask_ == 0) || - __traits_.isctype(__ch, __neg_mask_); + const bool __in_neg_mask = __traits_.isctype(__ch, __neg_mask_); const bool __in_neg_chars = - __neg_chars_.empty() || std::find(__neg_chars_.begin(), __neg_chars_.end(), __ch) != __neg_chars_.end(); if (!(__in_neg_mask || __in_neg_chars)) diff --git a/test/std/re/re.alg/re.alg.match/inverted_character_classes.pass.cpp b/test/std/re/re.alg/re.alg.match/inverted_character_classes.pass.cpp new file mode 100644 index 000000000..5a19edc1a --- /dev/null +++ b/test/std/re/re.alg/re.alg.match/inverted_character_classes.pass.cpp @@ -0,0 +1,44 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// +// UNSUPPORTED: c++98, c++03 + +// Make sure that we correctly match inverted character classes. + +#include +#include + + +int main() { + assert(std::regex_match("X", std::regex("[X]"))); + assert(std::regex_match("X", std::regex("[XY]"))); + assert(!std::regex_match("X", std::regex("[^X]"))); + assert(!std::regex_match("X", std::regex("[^XY]"))); + + assert(std::regex_match("X", std::regex("[\\S]"))); + assert(!std::regex_match("X", std::regex("[^\\S]"))); + + assert(!std::regex_match("X", std::regex("[\\s]"))); + assert(std::regex_match("X", std::regex("[^\\s]"))); + + assert(std::regex_match("X", std::regex("[\\s\\S]"))); + assert(std::regex_match("X", std::regex("[^Y\\s]"))); + assert(!std::regex_match("X", std::regex("[^X\\s]"))); + + assert(std::regex_match("X", std::regex("[\\w]"))); + assert(std::regex_match("_", std::regex("[\\w]"))); + assert(!std::regex_match("X", std::regex("[^\\w]"))); + assert(!std::regex_match("_", std::regex("[^\\w]"))); + + assert(!std::regex_match("X", std::regex("[\\W]"))); + assert(!std::regex_match("_", std::regex("[\\W]"))); + assert(std::regex_match("X", std::regex("[^\\W]"))); + assert(std::regex_match("_", std::regex("[^\\W]"))); +} diff --git a/test/std/re/re.alg/re.alg.search/invert_neg_word_search.pass.cpp b/test/std/re/re.alg/re.alg.search/invert_neg_word_search.pass.cpp index dd17d3519..dc0b98558 100644 --- a/test/std/re/re.alg/re.alg.search/invert_neg_word_search.pass.cpp +++ b/test/std/re/re.alg/re.alg.search/invert_neg_word_search.pass.cpp @@ -18,7 +18,7 @@ #include #include -#include "test_macros.h" + // PR34310 int main() -- cgit v1.2.3