Bug 1388789 - handle unrecognized escapes in nsTextFormatter; r=froydnj
authorTom Tromey <tom@tromey.com>
Fri, 01 Sep 2017 08:31:49 -0600
changeset 381906 2aa6bbd7fca8687c08e107863baef93918f91788
parent 381905 ed7a9616664877b2a543deb84090201a0543b2d5
child 381907 2c6474a8bc311bd18f71d9a19d568a3a69210bd3
push id32542
push userkwierso@gmail.com
push dateWed, 20 Sep 2017 21:07:55 +0000
treeherdermozilla-central@319a34bea9e4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1388789
milestone57.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1388789 - handle unrecognized escapes in nsTextFormatter; r=froydnj nsTextFormatter tried to pass unrecognized escapes in the format string through to the output. However, if the format held a width or precision, that text was not output. It seems better to me to try to preserve the format text as-is. MozReview-Commit-ID: HoBykpfzK7C
xpcom/string/nsTextFormatter.cpp
xpcom/tests/gtest/TestTextFormatter.cpp
--- a/xpcom/string/nsTextFormatter.cpp
+++ b/xpcom/string/nsTextFormatter.cpp
@@ -859,16 +859,20 @@ dosprintf(SprintfStateStr* aState, const
       if (rv < 0) {
         va_end(aAp);
         FREE_IF_NECESSARY(nas);
         return rv;
       }
       continue;
     }
 
+    // Save the location of the "%" in case we decide it isn't a
+    // format and want to just emit the text from the format string.
+    const char16_t* percentPointer = aFmt - 1;
+
     /*
     ** Gobble up the % format string. Hopefully we have handled all
     ** of the strange cases!
     */
     flags = 0;
     c = *aFmt++;
     if (c == '%') {
       /* quoting a % with %% */
@@ -1154,27 +1158,17 @@ dosprintf(SprintfStateStr* aState, const
         u.ip = va_arg(aAp, int*);
         if (u.ip) {
           *u.ip = aState->cur - aState->base;
         }
         break;
 
       default:
         /* Not a % token after all... skip it */
-#if 0
-        MOZ_ASSERT(0);
-#endif
-        char16_t perct = '%';
-        rv = (*aState->stuff)(aState, &perct, 1);
-        if (rv < 0) {
-          va_end(aAp);
-          FREE_IF_NECESSARY(nas);
-          return rv;
-        }
-        rv = (*aState->stuff)(aState, aFmt - 1, 1);
+        rv = (*aState->stuff)(aState, percentPointer, aFmt - percentPointer);
         if (rv < 0) {
           va_end(aAp);
           FREE_IF_NECESSARY(nas);
           return rv;
         }
     }
   }
 
--- a/xpcom/tests/gtest/TestTextFormatter.cpp
+++ b/xpcom/tests/gtest/TestTextFormatter.cpp
@@ -25,10 +25,15 @@ TEST(TextFormatter, Tests)
                                 0xAC00, 0xFF45, 0x0103, 0x20, 0x33,
                                 0x20, 0x33, 0x33, 0x33, 0x20, 0x33,
                                 0x33, 0x33, 0x20, 0x48, 0x65, 0x6C,
                                 0x6C, 0x6F};
 
   for (uint32_t i=0; i<out.Length(); i++) {
     ASSERT_EQ(uout[i], expected[i]);
   }
+
+  // Test that an unrecognized escape is passed through.
+  nsString out2;
+  nsTextFormatter::ssprintf(out2, u"%1m!", 23);
+  EXPECT_STREQ("%1m!", NS_ConvertUTF16toUTF8(out2).get());
 }