Skip to content

Commit

Permalink
Bug 1673917 - Rename vprintf style nsTSubstring::AppendPrintf -> Appe…
Browse files Browse the repository at this point in the history
…ndVprintf. r=sg

This patch also expands tests to check that the results of creating string via
both methods are consistent.

Differential Revision: https://phabricator.services.mozilla.com/D95053
  • Loading branch information
Bryce Seager van Dyk committed Oct 29, 2020
1 parent 8263739 commit 2f82080
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 37 deletions.
2 changes: 1 addition & 1 deletion dom/indexedDB/ProfilerHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void LoggingHelper(bool aUseProfiler, const char* aFmt, va_list args) {
) {
nsAutoCString message;

message.AppendPrintf(aFmt, args);
message.AppendVprintf(aFmt, args);

MOZ_LOG(logModule, logLevel, ("%s", message.get()));

Expand Down
2 changes: 1 addition & 1 deletion gfx/thebes/gfxUserFontSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class MOZ_STACK_CLASS gfxOTSMessageContext : public gfxOTSContext {
va_start(va, format);

nsCString msg;
msg.AppendPrintf(format, va);
msg.AppendVprintf(format, va);

va_end(va);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static MOZ_FORMAT_PRINTF(1, 2) void TrimAndLog(const char* aFmt, ...) {

va_list ap;
va_start(ap, aFmt);
raw.AppendPrintf(aFmt, ap);
raw.AppendVprintf(aFmt, ap);
va_end(ap);

nsCOMPtr<nsIURLFormatter> urlFormatter =
Expand Down
2 changes: 1 addition & 1 deletion xpcom/build/LateWriteChecks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class SHA1Stream {
va_list list;
va_start(list, aFormat);
nsAutoCString str;
str.AppendPrintf(aFormat, list);
str.AppendVprintf(aFormat, list);
va_end(list);
mSHA1.update(str.get(), str.Length());
mozilla::Unused << fwrite(str.get(), 1, str.Length(), mFile);
Expand Down
2 changes: 1 addition & 1 deletion xpcom/string/nsPrintfCString.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class nsPrintfCString : public nsAutoCStringN<16> {
MOZ_FORMAT_PRINTF(2, 3) {
va_list ap;
va_start(ap, aFormat);
AppendPrintf(aFormat, ap);
AppendVprintf(aFormat, ap);
va_end(ap);
}
};
Expand Down
2 changes: 1 addition & 1 deletion xpcom/string/nsTSubstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ void nsTSubstring<T>::AppendPrintf(const char* aFormat, ...) {
}

template <typename T>
void nsTSubstring<T>::AppendPrintf(const char* aFormat, va_list aAp) {
void nsTSubstring<T>::AppendVprintf(const char* aFormat, va_list aAp) {
PrintfAppend<T> appender(this);
bool r = appender.vprint(aFormat, aAp);
if (!r) {
Expand Down
2 changes: 1 addition & 1 deletion xpcom/string/nsTSubstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ class nsTSubstring : public mozilla::detail::nsTStringRepr<T> {
* this with floating-point values as a result.
*/
void AppendPrintf(const char* aFormat, ...) MOZ_FORMAT_PRINTF(2, 3);
void AppendPrintf(const char* aFormat, va_list aAp) MOZ_FORMAT_PRINTF(2, 0);
void AppendVprintf(const char* aFormat, va_list aAp) MOZ_FORMAT_PRINTF(2, 0);
void AppendInt(int32_t aInteger) { AppendIntDec(aInteger); }
void AppendInt(int32_t aInteger, int aRadix) {
if (aRadix == 10) {
Expand Down
75 changes: 45 additions & 30 deletions xpcom/tests/gtest/TestStrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1968,58 +1968,71 @@ TEST_F(Strings, ConvertToSpan) {
}
}

// Macros for reducing verbosity of printf tests.
#define create_printf_strings(format, ...) \
nsCString appendPrintfString; \
appendPrintfString.AppendPrintf(format, __VA_ARGS__); \
const nsCString appendVprintfString( \
getAppendVprintfString(format, __VA_ARGS__));

#define verify_printf_strings(expected) \
EXPECT_TRUE(appendPrintfString.EqualsASCII(expected)) \
<< "appendPrintfString != expected:" << appendPrintfString.get() \
<< " != " << (expected); \
EXPECT_TRUE(appendPrintfString.Equals(appendVprintfString)) \
<< "appendPrintfString != appendVprintfString:" \
<< appendPrintfString.get() << " != " << appendVprintfString;

TEST_F(Strings, printf) {
auto getAppendVprintfString = [](const char* aFormat, ...) {
// Helper to get a string with contents set via AppendVprint.
nsCString cString;
va_list ap;
va_start(ap, aFormat);
cString.AppendVprintf(aFormat, ap);
va_end(ap);
return cString;
};

{
const char* format = "Characters %c %%";
const char* expectedOutput = "Characters B %";
nsCString cString;
cString.AppendPrintf(format, 'B');
EXPECT_TRUE(cString.EqualsASCII(expectedOutput))
<< cString.get() << " != " << expectedOutput;
create_printf_strings(format, 'B');
verify_printf_strings(expectedOutput);
}
{
const char* format = "Strings %s %s";
const char* expectedOutput = "Strings foo bar";
nsCString cString;
cString.AppendPrintf(format, "foo", "bar");
EXPECT_TRUE(cString.EqualsASCII(expectedOutput))
<< cString.get() << " != " << expectedOutput;
create_printf_strings(format, "foo", "bar");
verify_printf_strings(expectedOutput);
}
{
const int signedThree = 3;
const unsigned int unsignedTen = 10;
const char* format = "Integers %i %.3d %.2u %o %x %X";
const char* expectedOutput = "Integers 3 003 10 12 a A";
nsCString cString;
cString.AppendPrintf(format, signedThree, signedThree, unsignedTen,
unsignedTen, unsignedTen, unsignedTen);
EXPECT_TRUE(cString.EqualsASCII(expectedOutput))
<< cString.get() << " != " << expectedOutput;
create_printf_strings(format, signedThree, signedThree, unsignedTen,
unsignedTen, unsignedTen, unsignedTen);
verify_printf_strings(expectedOutput);
}
{
const char* format = "Floats %f %.0f %e %.2E";
const char* expectedOutput = "Floats 1.500000 2 1.500000e+00 1.50E+00";
nsCString cString;
cString.AppendPrintf(format, 1.5, 1.5, 1.5, 1.5);
EXPECT_TRUE(cString.EqualsASCII(expectedOutput))
<< cString.get() << " != " << expectedOutput;
create_printf_strings(format, 1.5, 1.5, 1.5, 1.5);
verify_printf_strings(expectedOutput);
}
{
const char* expectedOutput = "Just a string";
const char* format = "%s";
nsCString cString;
cString.AppendPrintf(format, "Just a string");
EXPECT_TRUE(cString.EqualsASCII(expectedOutput))
<< cString.get() << " != " << expectedOutput;
create_printf_strings(format, "Just a string");
verify_printf_strings(expectedOutput);
}
{
const char* anotherString = "another string";
const char* format = "Just a string and %s";
const char* expectedOutput = "Just a string and another string";
nsCString cString;
cString.AppendPrintf(format, anotherString);
EXPECT_TRUE(cString.EqualsASCII(expectedOutput))
<< cString.get() << " != " << expectedOutput;
create_printf_strings(format, anotherString);
verify_printf_strings(expectedOutput);
}
{
// This case tickles an unexpected overload resolution in MSVC where a
Expand All @@ -2028,15 +2041,17 @@ TEST_F(Strings, printf) {
char anotherString[] = "another string";
const char* format = "Just a string and %s";
const char* expectedOutput = "Just a string and another string";
nsCString cString;
// Calling with a non-const pointer triggers selection of va_list overload
// in MSVC at time of writing.
cString.AppendPrintf(format, (char*)anotherString);
EXPECT_TRUE(cString.EqualsASCII(expectedOutput))
<< cString.get() << " != " << expectedOutput;
// in MSVC at time of writing
create_printf_strings(format, (char*)anotherString);
verify_printf_strings(expectedOutput);
}
}

// We don't need these macros following the printf test.
#undef verify_printf_strings
#undef create_printf_strings

// Note the five calls in the loop, so divide by 100k
MOZ_GTEST_BENCH_F(Strings, PerfStripWhitespace, [this] {
nsCString test1(mExample1Utf8);
Expand Down

0 comments on commit 2f82080

Please sign in to comment.