Bug 1388789 - handle unrecognized escapes in nsTextFormatter; r?froydnj draft
authorTom Tromey <tom@tromey.com>
Fri, 01 Sep 2017 08:31:49 -0600
changeset 667175 bd688ff364af64670ca480dc8a57cc54f89925d7
parent 667174 5e8b4a10807102a20f6ad3f3469fb33bdb94234d
child 667176 f5ab68c2eb4a3cf18caacac6eb5f54dd80a833c9
push id80636
push userbmo:ttromey@mozilla.com
push dateTue, 19 Sep 2017 20:02:29 +0000
reviewersfroydnj
bugs1388789
milestone57.0a1
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());
 }