diff options
author | Dan Sinclair <dsinclair@chromium.org> | 2017-11-16 22:08:07 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2017-11-16 22:08:07 +0000 |
commit | 1c4735aed9442a8e442214a23a3df94bd8fc99b5 (patch) | |
tree | 37264efcc3a1d90e3a9f05384d283923c828242f /core/fxcrt/bytestring.cpp | |
parent | 3f1c832dda209cf6682bb75316c07d71332fe6c3 (diff) | |
download | pdfium-1c4735aed9442a8e442214a23a3df94bd8fc99b5.tar.xz |
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 <dsinclair@chromium.org>
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'core/fxcrt/bytestring.cpp')
-rw-r--r-- | core/fxcrt/bytestring.cpp | 92 |
1 files changed, 51 insertions, 41 deletions
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()); } |