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
--- 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());
}