Fix the bug in Regexp::ToString() that emitted [^].

Change-Id: Ida8aaacbf42e4763737078b601bbc91e6e9fa2ad
Reviewed-on: https://code-review.googlesource.com/c/re2/+/40910
Reviewed-by: Paul Wankadia <junyer@google.com>
diff --git a/re2/testing/parse_test.cc b/re2/testing/parse_test.cc
index 5cb3952..a3289b9 100644
--- a/re2/testing/parse_test.cc
+++ b/re2/testing/parse_test.cc
@@ -217,6 +217,10 @@
     Regexp::PerlClasses | Regexp::NeverNL },
   { "\\S", "cc{0-0x8 0xb 0xe-0x1f 0x21-0x10ffff}",
     Regexp::PerlClasses | Regexp::NeverNL | Regexp::FoldCase },
+
+  // Bug in Regexp::ToString() that emitted [^], which
+  // would (obviously) fail to parse when fed back in.
+  { "[\\s\\S]", "cc{0-0x10ffff}" },
 };
 
 bool RegexpEqualTestingOnly(Regexp* a, Regexp* b) {
diff --git a/re2/tostring.cc b/re2/tostring.cc
index 2d06551..a608c87 100644
--- a/re2/tostring.cc
+++ b/re2/tostring.cc
@@ -269,9 +269,9 @@
       }
       t_->append("[");
       // Heuristic: show class as negated if it contains the
-      // non-character 0xFFFE.
+      // non-character 0xFFFE and yet somehow isn't full.
       CharClass* cc = re->cc();
-      if (cc->Contains(0xFFFE)) {
+      if (cc->Contains(0xFFFE) && !cc->full()) {
         cc = cc->Negate();
         t_->append("^");
       }