Author: marshall Date: Wed Oct 18 09:49:22 2017 New Revision: 316095 URL: http://llvm.org/viewvc/llvm-project?rev=316095&view=rev Log: Fix regex bug with ^\W. Thanks to Tim Shen for the patch. Reviewed as https://reviews.llvm.org/D37955
Added: libcxx/trunk/test/std/re/re.alg/re.alg.search/invert_neg_word_search.pass.cpp Modified: libcxx/trunk/include/regex Modified: libcxx/trunk/include/regex URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/regex?rev=316095&r1=316094&r2=316095&view=diff ============================================================================== --- libcxx/trunk/include/regex (original) +++ libcxx/trunk/include/regex Wed Oct 18 09:49:22 2017 @@ -2409,17 +2409,28 @@ __bracket_expression<_CharT, _Traits>::_ goto __exit; } } - if (!__neg_chars_.empty()) + // set of "__found" chars = + // 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. { - for (size_t __i = 0; __i < __neg_chars_.size(); ++__i) - { - if (__ch == __neg_chars_[__i]) - goto __is_neg_char; - } + const bool __in_neg_mask = (__neg_mask_ == 0) || + __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)) + { __found = true; goto __exit; + } } -__is_neg_char: if (!__ranges_.empty()) { string_type __s2 = __collate_ ? @@ -2450,11 +2461,6 @@ __is_neg_char: { __found = true; goto __exit; - } - if (__neg_mask_ && !__traits_.isctype(__ch, __neg_mask_)) - { - __found = true; - goto __exit; } } else Added: libcxx/trunk/test/std/re/re.alg/re.alg.search/invert_neg_word_search.pass.cpp URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/re/re.alg/re.alg.search/invert_neg_word_search.pass.cpp?rev=316095&view=auto ============================================================================== --- libcxx/trunk/test/std/re/re.alg/re.alg.search/invert_neg_word_search.pass.cpp (added) +++ libcxx/trunk/test/std/re/re.alg/re.alg.search/invert_neg_word_search.pass.cpp Wed Oct 18 09:49:22 2017 @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// + +// <regex> + +// template <class BidirectionalIterator, class Allocator, class charT, class traits> +// bool +// regex_search(BidirectionalIterator first, BidirectionalIterator last, +// match_results<BidirectionalIterator, Allocator>& m, +// const basic_regex<charT, traits>& e, +// regex_constants::match_flag_type flags = regex_constants::match_default); + +#include <regex> +#include <cassert> +#include "test_macros.h" + +// PR34310 +int main() +{ + assert(std::regex_search("HelloWorld", std::regex("[^\\W]"))); + assert(std::regex_search("_", std::regex("[^\\W]"))); + return 0; +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits