Merging r259682:
------------------------------------------------------------------------
r259682 | dexonsmith | 2016-02-03 11:30:20 -0800 (Wed, 03 Feb 2016) | 7 lines

re.results.form: Format out-of-range subexpression references as null

Rather than crashing in match_results::format() when a reference to a
marked subexpression is out of range, format the subexpression as empty
(i.e., replace it with an empty string).  Note that
match_results::operator[]() has a range-check and returns a null match
in this case, so this just re-uses that logic.
------------------------------------------------------------------------


git-svn-id: https://llvm.org/svn/llvm-project/libcxx/branches/release_38@260983 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/regex b/include/regex
index ca455f0..f1f3264 100644
--- a/include/regex
+++ b/include/regex
@@ -5387,8 +5387,8 @@
                 if ('0' <= *__fmt_first && *__fmt_first <= '9')
                 {
                     size_t __i = *__fmt_first - '0';
-                    __out = _VSTD::copy(__matches_[__i].first,
-                                       __matches_[__i].second, __out);
+                    __out = _VSTD::copy((*this)[__i].first,
+                                        (*this)[__i].second, __out);
                 }
                 else
                 {
@@ -5439,8 +5439,8 @@
                             ++__fmt_first;
                             __i = 10 * __i + *__fmt_first - '0';
                         }
-                        __out = _VSTD::copy(__matches_[__i].first,
-                                           __matches_[__i].second, __out);
+                        __out = _VSTD::copy((*this)[__i].first,
+                                            (*this)[__i].second, __out);
                     }
                     else
                     {
diff --git a/test/std/re/re.results/re.results.form/form1.pass.cpp b/test/std/re/re.results/re.results.form/form1.pass.cpp
index 9701c60..5b04c68 100644
--- a/test/std/re/re.results/re.results.form/form1.pass.cpp
+++ b/test/std/re/re.results/re.results.form/form1.pass.cpp
@@ -38,6 +38,31 @@
     {
         std::match_results<const char*> m;
         const char s[] = "abcdefghijk";
+        assert(std::regex_search(s, m, std::regex("cd((e)fg)hi",
+                                                  std::regex_constants::nosubs)));
+
+        char out[100] = {0};
+        const char fmt[] = "prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
+        char* r = m.format(output_iterator<char*>(out),
+                    fmt, fmt + std::char_traits<char>::length(fmt)).base();
+        assert(r == out + 54);
+        assert(std::string(out) == "prefix: ab, match: cdefghi, suffix: jk, m[1]: , m[2]: ");
+    }
+    {
+        std::match_results<const char*> m;
+        const char s[] = "abcdefghijk";
+        assert(std::regex_search(s, m, std::regex("cdefghi")));
+
+        char out[100] = {0};
+        const char fmt[] = "prefix: $`, match: $&, suffix: $', m[1]: $1, m[2]: $2";
+        char* r = m.format(output_iterator<char*>(out),
+                    fmt, fmt + std::char_traits<char>::length(fmt)).base();
+        assert(r == out + 54);
+        assert(std::string(out) == "prefix: ab, match: cdefghi, suffix: jk, m[1]: , m[2]: ");
+    }
+    {
+        std::match_results<const char*> m;
+        const char s[] = "abcdefghijk";
         assert(std::regex_search(s, m, std::regex("cd((e)fg)hi")));
 
         char out[100] = {0};
@@ -61,6 +86,33 @@
         assert(r == out + 34);
         assert(std::string(out) == "match: cdefghi, m[1]: efg, m[2]: e");
     }
+    {
+        std::match_results<const char*> m;
+        const char s[] = "abcdefghijk";
+        assert(std::regex_search(s, m, std::regex("cd((e)fg)hi",
+                                                  std::regex_constants::nosubs)));
+
+        char out[100] = {0};
+        const char fmt[] = "match: &, m[1]: \\1, m[2]: \\2";
+        char* r = m.format(output_iterator<char*>(out),
+                    fmt, fmt + std::char_traits<char>::length(fmt),
+                    std::regex_constants::format_sed).base();
+        assert(r == out + 30);
+        assert(std::string(out) == "match: cdefghi, m[1]: , m[2]: ");
+    }
+    {
+        std::match_results<const char*> m;
+        const char s[] = "abcdefghijk";
+        assert(std::regex_search(s, m, std::regex("cdefghi")));
+
+        char out[100] = {0};
+        const char fmt[] = "match: &, m[1]: \\1, m[2]: \\2";
+        char* r = m.format(output_iterator<char*>(out),
+                    fmt, fmt + std::char_traits<char>::length(fmt),
+                    std::regex_constants::format_sed).base();
+        assert(r == out + 30);
+        assert(std::string(out) == "match: cdefghi, m[1]: , m[2]: ");
+    }
 
     {
         std::match_results<const wchar_t*> m;