From 1c4735aed9442a8e442214a23a3df94bd8fc99b5 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Thu, 16 Nov 2017 22:08:07 +0000 Subject: Convert ByteString::{Format|FormatV} to static methods This CL moves the Format and FormatV methods of ByteString to be static. Bug: pdfium:934 Change-Id: I9c30455a789aff9f619b9d5bf89c0712644f2d9a Reviewed-on: https://pdfium-review.googlesource.com/18650 Commit-Queue: dsinclair Reviewed-by: Tom Sepez --- core/fpdfapi/edit/cpdf_creator.cpp | 12 ++-- core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp | 6 +- core/fpdfdoc/cpdf_formcontrol.cpp | 6 +- core/fpdfdoc/cpdf_formfield.cpp | 4 +- core/fpdfdoc/cpdf_interform.cpp | 2 +- core/fpdfdoc/cpvt_generateap.cpp | 30 ++++---- core/fxcrt/bytestring.cpp | 92 ++++++++++++++----------- core/fxcrt/bytestring.h | 11 ++- core/fxcrt/bytestring_unittest.cpp | 68 +++--------------- core/fxge/cfx_fontmgr.cpp | 4 +- 10 files changed, 95 insertions(+), 140 deletions(-) (limited to 'core') diff --git a/core/fpdfapi/edit/cpdf_creator.cpp b/core/fpdfapi/edit/cpdf_creator.cpp index 590229bf4f..db5bd681d4 100644 --- a/core/fpdfapi/edit/cpdf_creator.cpp +++ b/core/fpdfapi/edit/cpdf_creator.cpp @@ -556,15 +556,15 @@ int32_t CPDF_Creator::WriteDoc_Stage3() { j++; if (i == 1) - str.Format("0 %d\r\n0000000000 65535 f\r\n", j); + str = ByteString::Format("0 %d\r\n0000000000 65535 f\r\n", j); else - str.Format("%d %d\r\n", i, j - i); + str = ByteString::Format("%d %d\r\n", i, j - i); if (!m_Archive->WriteBlock(str.c_str(), str.GetLength())) return -1; while (i < j) { - str.Format("%010d 00000 n\r\n", m_ObjectOffsets[i++]); + str = ByteString::Format("%010d 00000 n\r\n", m_ObjectOffsets[i++]); if (!m_Archive->WriteBlock(str.c_str(), str.GetLength())) return -1; } @@ -590,16 +590,16 @@ int32_t CPDF_Creator::WriteDoc_Stage3() { } objnum = m_NewObjNumArray[i]; if (objnum == 1) - str.Format("0 %d\r\n0000000000 65535 f\r\n", j - i + 1); + str = ByteString::Format("0 %d\r\n0000000000 65535 f\r\n", j - i + 1); else - str.Format("%d %d\r\n", objnum, j - i); + str = ByteString::Format("%d %d\r\n", objnum, j - i); if (!m_Archive->WriteBlock(str.c_str(), str.GetLength())) return -1; while (i < j) { objnum = m_NewObjNumArray[i++]; - str.Format("%010d 00000 n\r\n", m_ObjectOffsets[objnum]); + str = ByteString::Format("%010d 00000 n\r\n", m_ObjectOffsets[objnum]); if (!m_Archive->WriteBlock(str.c_str(), str.GetLength())) return -1; } diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp index 593680e7ff..8a08a849cc 100644 --- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp +++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp @@ -138,10 +138,10 @@ ByteString CPDF_PageContentGenerator::RealizeResource( ByteString name; int idnum = 1; while (1) { - name.Format("FX%c%d", bsType[0], idnum); - if (!pResList->KeyExist(name)) { + name = ByteString::Format("FX%c%d", bsType[0], idnum); + if (!pResList->KeyExist(name)) break; - } + idnum++; } pResList->SetNewFor(name, m_pDocument.Get(), diff --git a/core/fpdfdoc/cpdf_formcontrol.cpp b/core/fpdfdoc/cpdf_formcontrol.cpp index 7d4067b52f..5e24c5fb9e 100644 --- a/core/fpdfdoc/cpdf_formcontrol.cpp +++ b/core/fpdfdoc/cpdf_formcontrol.cpp @@ -101,10 +101,8 @@ ByteString CPDF_FormControl::GetCheckedAPState() { ByteString csOn = GetOnStateName(); if (GetType() == CPDF_FormField::RadioButton || GetType() == CPDF_FormField::CheckBox) { - if (ToArray(FPDF_GetFieldAttr(m_pField->GetDict(), "Opt"))) { - int iIndex = m_pField->GetControlIndex(this); - csOn.Format("%d", iIndex); - } + if (ToArray(FPDF_GetFieldAttr(m_pField->GetDict(), "Opt"))) + csOn = ByteString::Format("%d", m_pField->GetControlIndex(this)); } if (csOn.IsEmpty()) csOn = "Yes"; diff --git a/core/fpdfdoc/cpdf_formfield.cpp b/core/fpdfdoc/cpdf_formfield.cpp index 637cb9eece..395b5c5713 100644 --- a/core/fpdfdoc/cpdf_formfield.cpp +++ b/core/fpdfdoc/cpdf_formfield.cpp @@ -749,9 +749,7 @@ bool CPDF_FormField::CheckControl(int iControlIndex, m_pDict->SetNewFor("V", "Off"); } } else if (bChecked) { - ByteString csIndex; - csIndex.Format("%d", iControlIndex); - m_pDict->SetNewFor("V", csIndex); + m_pDict->SetNewFor("V", ByteString::Format("%d", iControlIndex)); } if (bNotify && m_pForm->GetFormNotify()) m_pForm->GetFormNotify()->AfterCheckedStatusChange(this); diff --git a/core/fpdfdoc/cpdf_interform.cpp b/core/fpdfdoc/cpdf_interform.cpp index 50db639a4f..e42095c048 100644 --- a/core/fpdfdoc/cpdf_interform.cpp +++ b/core/fpdfdoc/cpdf_interform.cpp @@ -717,7 +717,7 @@ ByteString CPDF_InterForm::GenerateNewResourceName( if (m < iCount) csTmp += csStr[m++]; else - bsNum.Format("%d", num++); + bsNum = ByteString::Format("%d", num++); m++; } diff --git a/core/fpdfdoc/cpvt_generateap.cpp b/core/fpdfdoc/cpvt_generateap.cpp index c7b551f953..c0f9927229 100644 --- a/core/fpdfdoc/cpvt_generateap.cpp +++ b/core/fpdfdoc/cpvt_generateap.cpp @@ -44,25 +44,25 @@ ByteString GetPDFWordString(IPVT_FontMap* pFontMap, int32_t nFontIndex, uint16_t Word, uint16_t SubWord) { - ByteString sWord; - if (SubWord > 0) { - sWord.Format("%c", SubWord); - return sWord; - } + if (SubWord > 0) + return ByteString::Format("%c", SubWord); if (!pFontMap) - return sWord; + return ""; - if (CPDF_Font* pPDFFont = pFontMap->GetPDFFont(nFontIndex)) { - if (pPDFFont->GetBaseFont().Compare("Symbol") == 0 || - pPDFFont->GetBaseFont().Compare("ZapfDingbats") == 0) { - sWord.Format("%c", Word); - } else { - uint32_t dwCharCode = pPDFFont->CharCodeFromUnicode(Word); - if (dwCharCode != CPDF_Font::kInvalidCharCode) - pPDFFont->AppendChar(&sWord, dwCharCode); - } + CPDF_Font* pPDFFont = pFontMap->GetPDFFont(nFontIndex); + if (!pPDFFont) + return ""; + + if (pPDFFont->GetBaseFont().Compare("Symbol") == 0 || + pPDFFont->GetBaseFont().Compare("ZapfDingbats") == 0) { + return ByteString::Format("%c", Word); } + + ByteString sWord; + uint32_t dwCharCode = pPDFFont->CharCodeFromUnicode(Word); + if (dwCharCode != CPDF_Font::kInvalidCharCode) + pPDFFont->AppendChar(&sWord, dwCharCode); return sWord; } diff --git a/core/fxcrt/bytestring.cpp b/core/fxcrt/bytestring.cpp index ad3814b57e..e868678c9f 100644 --- a/core/fxcrt/bytestring.cpp +++ b/core/fxcrt/bytestring.cpp @@ -91,6 +91,57 @@ namespace fxcrt { static_assert(sizeof(ByteString) <= sizeof(char*), "Strings must not require more space than pointers"); +#define FORCE_ANSI 0x10000 +#define FORCE_UNICODE 0x20000 +#define FORCE_INT64 0x40000 + +// static +ByteString ByteString::FormatInteger(int i) { + char buf[32]; + FXSYS_snprintf(buf, 32, "%d", i); + return ByteString(buf); +} + +// static +ByteString ByteString::FormatFloat(float d) { + char buf[32]; + return ByteString(buf, FX_ftoa(d, buf)); +} + +// static +ByteString ByteString::FormatV(const char* pFormat, va_list argList) { + va_list argListCopy; + va_copy(argListCopy, argList); + int nMaxLen = vsnprintf(nullptr, 0, pFormat, argListCopy); + va_end(argListCopy); + + if (nMaxLen <= 0) + return ""; + + ByteString ret; + char* buf = ret.GetBuffer(nMaxLen); + if (buf) { + // In the following two calls, there's always space in the buffer for + // a terminating NUL that's not included in nMaxLen. + memset(buf, 0, nMaxLen + 1); + va_copy(argListCopy, argList); + vsnprintf(buf, nMaxLen + 1, pFormat, argListCopy); + va_end(argListCopy); + ret.ReleaseBuffer(ret.GetStringLength()); + } + return ret; +} + +// static +ByteString ByteString::Format(const char* pFormat, ...) { + va_list argList; + va_start(argList, pFormat); + ByteString ret = FormatV(pFormat, argList); + va_end(argList); + + return ret; +} + ByteString::ByteString(const char* pStr, size_t nLen) { if (nLen) m_pData.Reset(StringData::Create(pStr, nLen)); @@ -485,42 +536,6 @@ void ByteString::AllocCopy(ByteString& dest, dest.m_pData.Swap(pNewData); } -#define FORCE_ANSI 0x10000 -#define FORCE_UNICODE 0x20000 -#define FORCE_INT64 0x40000 - -ByteString ByteString::FormatInteger(int i) { - char buf[32]; - FXSYS_snprintf(buf, 32, "%d", i); - return ByteString(buf); -} - -void ByteString::FormatV(const char* pFormat, va_list argList) { - va_list argListCopy; - va_copy(argListCopy, argList); - int nMaxLen = vsnprintf(nullptr, 0, pFormat, argListCopy); - va_end(argListCopy); - if (nMaxLen > 0) { - GetBuffer(nMaxLen); - if (m_pData) { - // In the following two calls, there's always space in the buffer for - // a terminating NUL that's not included in nMaxLen. - memset(m_pData->m_String, 0, nMaxLen + 1); - va_copy(argListCopy, argList); - vsnprintf(m_pData->m_String, nMaxLen + 1, pFormat, argListCopy); - va_end(argListCopy); - ReleaseBuffer(GetStringLength()); - } - } -} - -void ByteString::Format(const char* pFormat, ...) { - va_list argList; - va_start(argList, pFormat); - FormatV(pFormat, argList); - va_end(argList); -} - void ByteString::SetAt(size_t index, char c) { ASSERT(IsValidIndex(index)); ReallocBeforeWrite(m_pData->m_nDataLength); @@ -783,11 +798,6 @@ void ByteString::TrimLeft() { TrimLeft("\x09\x0a\x0b\x0c\x0d\x20"); } -ByteString ByteString::FormatFloat(float d) { - char buf[32]; - return ByteString(buf, FX_ftoa(d, buf)); -} - std::ostream& operator<<(std::ostream& os, const ByteString& str) { return os.write(str.c_str(), str.GetLength()); } diff --git a/core/fxcrt/bytestring.h b/core/fxcrt/bytestring.h index f534b08b8a..1acece3aee 100644 --- a/core/fxcrt/bytestring.h +++ b/core/fxcrt/bytestring.h @@ -32,6 +32,11 @@ class ByteString { using const_iterator = const CharType*; using const_reverse_iterator = std::reverse_iterator; + static ByteString FormatInteger(int i); + static ByteString FormatFloat(float f); + static ByteString Format(const char* lpszFormat, ...); + static ByteString FormatV(const char* lpszFormat, va_list argList); + ByteString(); ByteString(const ByteString& other); ByteString(ByteString&& other) noexcept; @@ -138,9 +143,6 @@ class ByteString { size_t InsertAtBack(char ch) { return Insert(GetLength(), ch); } size_t Delete(size_t index, size_t count = 1); - void Format(const char* lpszFormat, ...); - void FormatV(const char* lpszFormat, va_list argList); - void Reserve(size_t len); char* GetBuffer(size_t len); void ReleaseBuffer(size_t len); @@ -181,9 +183,6 @@ class ByteString { uint32_t GetID() const { return AsStringView().GetID(); } - static ByteString FormatInteger(int i); - static ByteString FormatFloat(float f); - protected: using StringData = StringDataTemplate; diff --git a/core/fxcrt/bytestring_unittest.cpp b/core/fxcrt/bytestring_unittest.cpp index 4668fe1cc8..08c0e9211c 100644 --- a/core/fxcrt/bytestring_unittest.cpp +++ b/core/fxcrt/bytestring_unittest.cpp @@ -1448,67 +1448,19 @@ TEST(ByteStringView, AnyAllNoneOf) { } TEST(ByteString, FormatWidth) { - { - ByteString str; - str.Format("%5d", 1); - EXPECT_EQ(" 1", str); - } - - { - ByteString str; - str.Format("%d", 1); - EXPECT_EQ("1", str); - } - - { - ByteString str; - str.Format("%*d", 5, 1); - EXPECT_EQ(" 1", str); - } - - { - ByteString str; - str.Format("%-1d", 1); - EXPECT_EQ("1", str); - } - - { - ByteString str; - str.Format("%0d", 1); - EXPECT_EQ("1", str); - } + EXPECT_EQ(" 1", ByteString::Format("%5d", 1)); + EXPECT_EQ("1", ByteString::Format("%d", 1)); + EXPECT_EQ(" 1", ByteString::Format("%*d", 5, 1)); + EXPECT_EQ("1", ByteString::Format("%-1d", 1)); + EXPECT_EQ("1", ByteString::Format("%0d", 1)); } TEST(ByteString, FormatPrecision) { - { - ByteString str; - str.Format("%.2f", 1.12345); - EXPECT_EQ("1.12", str); - } - - { - ByteString str; - str.Format("%.*f", 3, 1.12345); - EXPECT_EQ("1.123", str); - } - - { - ByteString str; - str.Format("%f", 1.12345); - EXPECT_EQ("1.123450", str); - } - - { - ByteString str; - str.Format("%-1f", 1.12345); - EXPECT_EQ("1.123450", str); - } - - { - ByteString str; - str.Format("%0f", 1.12345); - EXPECT_EQ("1.123450", str); - } + EXPECT_EQ("1.12", ByteString::Format("%.2f", 1.12345)); + EXPECT_EQ("1.123", ByteString::Format("%.*f", 3, 1.12345)); + EXPECT_EQ("1.123450", ByteString::Format("%f", 1.12345)); + EXPECT_EQ("1.123450", ByteString::Format("%-1f", 1.12345)); + EXPECT_EQ("1.123450", ByteString::Format("%0f", 1.12345)); } TEST(ByteString, Empty) { diff --git a/core/fxge/cfx_fontmgr.cpp b/core/fxge/cfx_fontmgr.cpp index f68598e4b6..e12f341b3e 100644 --- a/core/fxge/cfx_fontmgr.cpp +++ b/core/fxge/cfx_fontmgr.cpp @@ -57,9 +57,7 @@ ByteString KeyNameFromFace(const ByteString& face_name, } ByteString KeyNameFromSize(int ttc_size, uint32_t checksum) { - ByteString key; - key.Format("%d:%d", ttc_size, checksum); - return key; + return ByteString::Format("%d:%d", ttc_size, checksum); } int GetTTCIndex(const uint8_t* pFontData, -- cgit v1.2.3