bpo-31324: Optimize support._match_test() (#4523) (#4524)

* bpo-31324: Optimize support._match_test() (#4421)

* Rename support._match_test() to support.match_test(): make it
  public
* Remove support.match_tests global variable. It is replaced with a
  new support.set_match_tests() function, so match_test() doesn't
  have to check each time if patterns were modified.
* Rewrite match_test(): use different code paths depending on the
  kind of patterns for best performances.

Co-Authored-By: Serhiy Storchaka <storchaka@gmail.com>
(cherry picked from commit 803ddd8ce22f0de3ab42fb98a225a704c000ef06)

* bpo-31324: Fix test.support.set_match_tests(None) (#4505)

(cherry picked from commit bb11c3c967afaf263e00844d4ab461b7fafd6d36)
(cherry picked from commit 70b2f8797146a56a6880743424f0bedf4fc30c62)
diff --git a/Lib/test/regrtest.py b/Lib/test/regrtest.py
index d836468..6df8f86 100755
--- a/Lib/test/regrtest.py
+++ b/Lib/test/regrtest.py
@@ -1037,7 +1037,7 @@
     if use_resources is not None:
         test_support.use_resources = use_resources
     try:
-        test_support.match_tests = match_tests
+        test_support.set_match_tests(match_tests)
         if failfast:
             test_support.failfast = True
         return runtest_inner(test, verbose, quiet, huntrleaks, pgo, testdir)
@@ -1580,12 +1580,12 @@
         if isinstance(test, unittest.TestSuite):
             _list_cases(test)
         elif isinstance(test, unittest.TestCase):
-            if test_support._match_test(test):
+            if test_support.match_test(test):
                 print(test.id())
 
 def list_cases(testdir, selected, match_tests):
     test_support.verbose = False
-    test_support.match_tests = match_tests
+    test_support.set_match_tests(match_tests)
 
     save_modules = set(sys.modules)
     skipped = []