Bug 1060419 - make AppendPrintf and nsPrintfCString use Printf.h, r=froydnj

MozReview-Commit-ID: 2E8FoiNxU8L
This commit is contained in:
Tom Tromey
2016-12-14 09:32:21 -07:00
parent e014854967
commit e90d95a3f9
55 changed files with 189 additions and 151 deletions

View File

@@ -7,6 +7,7 @@
#include "mozilla/CheckedInt.h"
#include "mozilla/double-conversion.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Printf.h"
using double_conversion::DoubleToStringConverter;
@@ -904,29 +905,41 @@ nsTSubstring_CharT::StripChars(const char_type* aChars, uint32_t aOffset)
mLength = to - mData;
}
int
nsTSubstring_CharT::AppendFunc(void* aArg, const char* aStr, uint32_t aLen)
struct MOZ_STACK_CLASS PrintfAppend_CharT : public mozilla::PrintfTarget
{
self_type* self = static_cast<self_type*>(aArg);
// NSPR sends us the final null terminator even though we don't want it
if (aLen && aStr[aLen - 1] == '\0') {
--aLen;
explicit PrintfAppend_CharT(nsTSubstring_CharT* aString)
: mString(aString)
{
}
self->AppendASCII(aStr, aLen);
bool append(const char* aStr, size_t aLen) override {
if (aLen == 0) {
return true;
}
return aLen;
}
// Printf sends us the final null terminator even though we don't want it
if (aStr[aLen - 1] == '\0') {
--aLen;
}
mString->AppendASCII(aStr, aLen);
return true;
}
private:
nsTSubstring_CharT* mString;
};
void
nsTSubstring_CharT::AppendPrintf(const char* aFormat, ...)
{
PrintfAppend_CharT appender(this);
va_list ap;
va_start(ap, aFormat);
uint32_t r = PR_vsxprintf(AppendFunc, this, aFormat, ap);
if (r == (uint32_t)-1) {
MOZ_CRASH("Allocation or other failure in PR_vsxprintf");
bool r = appender.vprint(aFormat, ap);
if (!r) {
MOZ_CRASH("Allocation or other failure in PrintfTarget::print");
}
va_end(ap);
}
@@ -934,9 +947,10 @@ nsTSubstring_CharT::AppendPrintf(const char* aFormat, ...)
void
nsTSubstring_CharT::AppendPrintf(const char* aFormat, va_list aAp)
{
uint32_t r = PR_vsxprintf(AppendFunc, this, aFormat, aAp);
if (r == (uint32_t)-1) {
MOZ_CRASH("Allocation or other failure in PR_vsxprintf");
PrintfAppend_CharT appender(this);
bool r = appender.vprint(aFormat, aAp);
if (!r) {
MOZ_CRASH("Allocation or other failure in PrintfTarget::print");
}
}