[PATCH] D50534: [libc++] Fix handling of negated character classes in regex

Louis Dionne via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Aug 10 12:26:40 PDT 2018


ldionne updated this revision to Diff 160169.
ldionne added a comment.

Rewrite the patch in light of timshen's comment, and add tests.


Repository:
  rCXX libc++

https://reviews.llvm.org/D50534

Files:
  libcxx/include/regex
  libcxx/test/std/re/re.alg/re.alg.match/inverted_character_classes.pass.cpp
  libcxx/test/std/re/re.alg/re.alg.search/invert_neg_word_search.pass.cpp


Index: libcxx/test/std/re/re.alg/re.alg.search/invert_neg_word_search.pass.cpp
===================================================================
--- libcxx/test/std/re/re.alg/re.alg.search/invert_neg_word_search.pass.cpp
+++ libcxx/test/std/re/re.alg/re.alg.search/invert_neg_word_search.pass.cpp
@@ -18,7 +18,7 @@
 
 #include <regex>
 #include <cassert>
-#include "test_macros.h"
+
 
 // PR34310
 int main()
Index: libcxx/test/std/re/re.alg/re.alg.match/inverted_character_classes.pass.cpp
===================================================================
--- /dev/null
+++ libcxx/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.
+//
+//===----------------------------------------------------------------------===//
+
+// <regex>
+// UNSUPPORTED: c++98, c++03
+
+// Make sure that we correctly match inverted character classes.
+
+#include <cassert>
+#include <regex>
+
+
+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]")));
+}
Index: libcxx/include/regex
===================================================================
--- libcxx/include/regex
+++ libcxx/include/regex
@@ -2414,20 +2414,17 @@
                 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))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D50534.160169.patch
Type: text/x-patch
Size: 3683 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180810/4e1b3c93/attachment.bin>


More information about the cfe-commits mailing list