From fcdb2df009796a78369a7fe8bcaab76b27f5450b Mon Sep 17 00:00:00 2001 From: Tom Sepez Date: Fri, 31 Mar 2017 10:32:07 -0700 Subject: Re-arrange fxcrt string files to match naming. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The top-level fx_string.h is kept to include all strings. Put all fx_atof code in fx_basic_util.cpp rather than header. Change-Id: I61fe768f2e1ddf8438d27e410929f4cff918a9a3 Reviewed-on: https://pdfium-review.googlesource.com/3530 Reviewed-by: Nicolás Peña Commit-Queue: Tom Sepez --- BUILD.gn | 10 +- core/fxcrt/cfx_bytestring.cpp | 820 ++++++++++++++++++++++ core/fxcrt/cfx_bytestring.h | 234 +++++++ core/fxcrt/cfx_bytestring_unittest.cpp | 1091 ++++++++++++++++++++++++++++++ core/fxcrt/cfx_string_c_template.h | 3 + core/fxcrt/cfx_widestring.cpp | 1044 ++++++++++++++++++++++++++++ core/fxcrt/cfx_widestring.h | 231 +++++++ core/fxcrt/cfx_widestring_unittest.cpp | 942 ++++++++++++++++++++++++++ core/fxcrt/fx_basic_bstring.cpp | 818 ---------------------- core/fxcrt/fx_basic_bstring_unittest.cpp | 1090 ----------------------------- core/fxcrt/fx_basic_util.cpp | 4 + core/fxcrt/fx_basic_wstring.cpp | 1042 ---------------------------- core/fxcrt/fx_basic_wstring_unittest.cpp | 941 -------------------------- core/fxcrt/fx_string.h | 441 +----------- 14 files changed, 4378 insertions(+), 4333 deletions(-) create mode 100644 core/fxcrt/cfx_bytestring.cpp create mode 100644 core/fxcrt/cfx_bytestring.h create mode 100644 core/fxcrt/cfx_bytestring_unittest.cpp create mode 100644 core/fxcrt/cfx_widestring.cpp create mode 100644 core/fxcrt/cfx_widestring.h create mode 100644 core/fxcrt/cfx_widestring_unittest.cpp delete mode 100644 core/fxcrt/fx_basic_bstring.cpp delete mode 100644 core/fxcrt/fx_basic_bstring_unittest.cpp delete mode 100644 core/fxcrt/fx_basic_wstring.cpp delete mode 100644 core/fxcrt/fx_basic_wstring_unittest.cpp diff --git a/BUILD.gn b/BUILD.gn index f142d5fa7f..0bf5a01309 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -751,6 +751,8 @@ config("fxge_warnings") { static_library("fxcrt") { sources = [ + "core/fxcrt/cfx_bytestring.cpp", + "core/fxcrt/cfx_bytestring.h", "core/fxcrt/cfx_maybe_owned.h", "core/fxcrt/cfx_observable.h", "core/fxcrt/cfx_retain_ptr.h", @@ -759,16 +761,16 @@ static_library("fxcrt") { "core/fxcrt/cfx_string_data_template.h", "core/fxcrt/cfx_string_pool_template.h", "core/fxcrt/cfx_weak_ptr.h", + "core/fxcrt/cfx_widestring.cpp", + "core/fxcrt/cfx_widestring.h", "core/fxcrt/extension.h", "core/fxcrt/fx_basic.h", - "core/fxcrt/fx_basic_bstring.cpp", "core/fxcrt/fx_basic_buffer.cpp", "core/fxcrt/fx_basic_coords.cpp", "core/fxcrt/fx_basic_gcc.cpp", "core/fxcrt/fx_basic_memmgr.cpp", "core/fxcrt/fx_basic_utf.cpp", "core/fxcrt/fx_basic_util.cpp", - "core/fxcrt/fx_basic_wstring.cpp", "core/fxcrt/fx_bidi.cpp", "core/fxcrt/fx_bidi.h", "core/fxcrt/fx_coordinates.h", @@ -1826,17 +1828,17 @@ test("pdfium_unittests") { "core/fxcodec/codec/fx_codec_rle_unittest.cpp", "core/fxcodec/jbig2/JBig2_BitStream_unittest.cpp", "core/fxcodec/jbig2/JBig2_Image_unittest.cpp", + "core/fxcrt/cfx_bytestring_unittest.cpp", "core/fxcrt/cfx_maybe_owned_unittest.cpp", "core/fxcrt/cfx_observable_unittest.cpp", "core/fxcrt/cfx_retain_ptr_unittest.cpp", "core/fxcrt/cfx_shared_copy_on_write_unittest.cpp", "core/fxcrt/cfx_string_pool_template_unittest.cpp", "core/fxcrt/cfx_weak_ptr_unittest.cpp", - "core/fxcrt/fx_basic_bstring_unittest.cpp", + "core/fxcrt/cfx_widestring_unittest.cpp", "core/fxcrt/fx_basic_gcc_unittest.cpp", "core/fxcrt/fx_basic_memmgr_unittest.cpp", "core/fxcrt/fx_basic_util_unittest.cpp", - "core/fxcrt/fx_basic_wstring_unittest.cpp", "core/fxcrt/fx_bidi_unittest.cpp", "core/fxcrt/fx_extension_unittest.cpp", "core/fxcrt/fx_system_unittest.cpp", diff --git a/core/fxcrt/cfx_bytestring.cpp b/core/fxcrt/cfx_bytestring.cpp new file mode 100644 index 0000000000..2d3f0ab46a --- /dev/null +++ b/core/fxcrt/cfx_bytestring.cpp @@ -0,0 +1,820 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/fxcrt/cfx_bytestring.h" + +#include + +#include +#include + +#include "core/fxcrt/cfx_string_pool_template.h" +#include "core/fxcrt/fx_basic.h" +#include "third_party/base/numerics/safe_math.h" +#include "third_party/base/stl_util.h" + +template class CFX_StringDataTemplate; +template class CFX_StringCTemplate; +template class CFX_StringPoolTemplate; +template struct std::hash; + +namespace { + +int Buffer_itoa(char* buf, int i, uint32_t flags) { + if (i == 0) { + buf[0] = '0'; + return 1; + } + char buf1[32]; + int buf_pos = 31; + uint32_t u = i; + if ((flags & FXFORMAT_SIGNED) && i < 0) { + u = -i; + } + int base = 10; + const char* str = "0123456789abcdef"; + if (flags & FXFORMAT_HEX) { + base = 16; + if (flags & FXFORMAT_CAPITAL) { + str = "0123456789ABCDEF"; + } + } + while (u != 0) { + buf1[buf_pos--] = str[u % base]; + u = u / base; + } + if ((flags & FXFORMAT_SIGNED) && i < 0) { + buf1[buf_pos--] = '-'; + } + int len = 31 - buf_pos; + for (int ii = 0; ii < len; ii++) { + buf[ii] = buf1[ii + buf_pos + 1]; + } + return len; +} + +const char* FX_strstr(const char* haystack, + int haystack_len, + const char* needle, + int needle_len) { + if (needle_len > haystack_len || needle_len == 0) { + return nullptr; + } + const char* end_ptr = haystack + haystack_len - needle_len; + while (haystack <= end_ptr) { + int i = 0; + while (1) { + if (haystack[i] != needle[i]) { + break; + } + i++; + if (i == needle_len) { + return haystack; + } + } + haystack++; + } + return nullptr; +} + +} // namespace + +static_assert(sizeof(CFX_ByteString) <= sizeof(char*), + "Strings must not require more space than pointers"); + +CFX_ByteString::CFX_ByteString(const char* pStr, FX_STRSIZE nLen) { + if (nLen < 0) + nLen = pStr ? FXSYS_strlen(pStr) : 0; + + if (nLen) + m_pData.Reset(StringData::Create(pStr, nLen)); +} + +CFX_ByteString::CFX_ByteString(const uint8_t* pStr, FX_STRSIZE nLen) { + if (nLen > 0) { + m_pData.Reset( + StringData::Create(reinterpret_cast(pStr), nLen)); + } +} + +CFX_ByteString::CFX_ByteString() {} + +CFX_ByteString::CFX_ByteString(const CFX_ByteString& other) + : m_pData(other.m_pData) {} + +CFX_ByteString::CFX_ByteString(CFX_ByteString&& other) noexcept { + m_pData.Swap(other.m_pData); +} + +CFX_ByteString::CFX_ByteString(char ch) { + m_pData.Reset(StringData::Create(1)); + m_pData->m_String[0] = ch; +} + +CFX_ByteString::CFX_ByteString(const char* ptr) + : CFX_ByteString(ptr, ptr ? FXSYS_strlen(ptr) : 0) {} + +CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& stringSrc) { + if (!stringSrc.IsEmpty()) + m_pData.Reset(StringData::Create(stringSrc.c_str(), stringSrc.GetLength())); +} + +CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& str1, + const CFX_ByteStringC& str2) { + int nNewLen = str1.GetLength() + str2.GetLength(); + if (nNewLen == 0) + return; + + m_pData.Reset(StringData::Create(nNewLen)); + m_pData->CopyContents(str1.c_str(), str1.GetLength()); + m_pData->CopyContentsAt(str1.GetLength(), str2.c_str(), str2.GetLength()); +} + +CFX_ByteString::~CFX_ByteString() {} + +const CFX_ByteString& CFX_ByteString::operator=(const char* pStr) { + if (!pStr || !pStr[0]) + clear(); + else + AssignCopy(pStr, FXSYS_strlen(pStr)); + + return *this; +} + +const CFX_ByteString& CFX_ByteString::operator=( + const CFX_ByteStringC& stringSrc) { + if (stringSrc.IsEmpty()) + clear(); + else + AssignCopy(stringSrc.c_str(), stringSrc.GetLength()); + + return *this; +} + +const CFX_ByteString& CFX_ByteString::operator=( + const CFX_ByteString& stringSrc) { + if (m_pData != stringSrc.m_pData) + m_pData = stringSrc.m_pData; + + return *this; +} + +const CFX_ByteString& CFX_ByteString::operator+=(const char* pStr) { + if (pStr) + Concat(pStr, FXSYS_strlen(pStr)); + + return *this; +} + +const CFX_ByteString& CFX_ByteString::operator+=(char ch) { + Concat(&ch, 1); + return *this; +} + +const CFX_ByteString& CFX_ByteString::operator+=(const CFX_ByteString& str) { + if (str.m_pData) + Concat(str.m_pData->m_String, str.m_pData->m_nDataLength); + + return *this; +} + +const CFX_ByteString& CFX_ByteString::operator+=(const CFX_ByteStringC& str) { + if (!str.IsEmpty()) + Concat(str.c_str(), str.GetLength()); + + return *this; +} + +bool CFX_ByteString::operator==(const char* ptr) const { + if (!m_pData) + return !ptr || !ptr[0]; + + if (!ptr) + return m_pData->m_nDataLength == 0; + + return FXSYS_strlen(ptr) == m_pData->m_nDataLength && + FXSYS_memcmp(ptr, m_pData->m_String, m_pData->m_nDataLength) == 0; +} + +bool CFX_ByteString::operator==(const CFX_ByteStringC& str) const { + if (!m_pData) + return str.IsEmpty(); + + return m_pData->m_nDataLength == str.GetLength() && + FXSYS_memcmp(m_pData->m_String, str.c_str(), str.GetLength()) == 0; +} + +bool CFX_ByteString::operator==(const CFX_ByteString& other) const { + if (m_pData == other.m_pData) + return true; + + if (IsEmpty()) + return other.IsEmpty(); + + if (other.IsEmpty()) + return false; + + return other.m_pData->m_nDataLength == m_pData->m_nDataLength && + FXSYS_memcmp(other.m_pData->m_String, m_pData->m_String, + m_pData->m_nDataLength) == 0; +} + +bool CFX_ByteString::operator<(const CFX_ByteString& str) const { + if (m_pData == str.m_pData) + return false; + + int result = FXSYS_memcmp(c_str(), str.c_str(), + std::min(GetLength(), str.GetLength())); + return result < 0 || (result == 0 && GetLength() < str.GetLength()); +} + +bool CFX_ByteString::EqualNoCase(const CFX_ByteStringC& str) const { + if (!m_pData) + return str.IsEmpty(); + + FX_STRSIZE len = str.GetLength(); + if (m_pData->m_nDataLength != len) + return false; + + const uint8_t* pThis = (const uint8_t*)m_pData->m_String; + const uint8_t* pThat = str.raw_str(); + for (FX_STRSIZE i = 0; i < len; i++) { + if ((*pThis) != (*pThat)) { + uint8_t bThis = *pThis; + if (bThis >= 'A' && bThis <= 'Z') + bThis += 'a' - 'A'; + + uint8_t bThat = *pThat; + if (bThat >= 'A' && bThat <= 'Z') + bThat += 'a' - 'A'; + + if (bThis != bThat) + return false; + } + pThis++; + pThat++; + } + return true; +} + +void CFX_ByteString::AssignCopy(const char* pSrcData, FX_STRSIZE nSrcLen) { + AllocBeforeWrite(nSrcLen); + m_pData->CopyContents(pSrcData, nSrcLen); + m_pData->m_nDataLength = nSrcLen; +} + +void CFX_ByteString::ReallocBeforeWrite(FX_STRSIZE nNewLength) { + if (m_pData && m_pData->CanOperateInPlace(nNewLength)) + return; + + if (nNewLength <= 0) { + clear(); + return; + } + + CFX_RetainPtr pNewData(StringData::Create(nNewLength)); + if (m_pData) { + FX_STRSIZE nCopyLength = std::min(m_pData->m_nDataLength, nNewLength); + pNewData->CopyContents(m_pData->m_String, nCopyLength); + pNewData->m_nDataLength = nCopyLength; + } else { + pNewData->m_nDataLength = 0; + } + pNewData->m_String[pNewData->m_nDataLength] = 0; + m_pData.Swap(pNewData); +} + +void CFX_ByteString::AllocBeforeWrite(FX_STRSIZE nNewLength) { + if (m_pData && m_pData->CanOperateInPlace(nNewLength)) + return; + + if (nNewLength <= 0) { + clear(); + return; + } + + m_pData.Reset(StringData::Create(nNewLength)); +} + +void CFX_ByteString::ReleaseBuffer(FX_STRSIZE nNewLength) { + if (!m_pData) + return; + + if (nNewLength == -1) + nNewLength = FXSYS_strlen(m_pData->m_String); + + nNewLength = std::min(nNewLength, m_pData->m_nAllocLength); + if (nNewLength == 0) { + clear(); + return; + } + + ASSERT(m_pData->m_nRefs == 1); + m_pData->m_nDataLength = nNewLength; + m_pData->m_String[nNewLength] = 0; + if (m_pData->m_nAllocLength - nNewLength >= 32) { + // Over arbitrary threshold, so pay the price to relocate. Force copy to + // always occur by holding a second reference to the string. + CFX_ByteString preserve(*this); + ReallocBeforeWrite(nNewLength); + } +} + +void CFX_ByteString::Reserve(FX_STRSIZE len) { + GetBuffer(len); +} + +char* CFX_ByteString::GetBuffer(FX_STRSIZE nMinBufLength) { + if (!m_pData) { + if (nMinBufLength == 0) + return nullptr; + + m_pData.Reset(StringData::Create(nMinBufLength)); + m_pData->m_nDataLength = 0; + m_pData->m_String[0] = 0; + return m_pData->m_String; + } + + if (m_pData->CanOperateInPlace(nMinBufLength)) + return m_pData->m_String; + + nMinBufLength = std::max(nMinBufLength, m_pData->m_nDataLength); + if (nMinBufLength == 0) + return nullptr; + + CFX_RetainPtr pNewData(StringData::Create(nMinBufLength)); + pNewData->CopyContents(*m_pData); + pNewData->m_nDataLength = m_pData->m_nDataLength; + m_pData.Swap(pNewData); + return m_pData->m_String; +} + +FX_STRSIZE CFX_ByteString::Delete(FX_STRSIZE nIndex, FX_STRSIZE nCount) { + if (!m_pData) + return 0; + + if (nIndex < 0) + nIndex = 0; + + FX_STRSIZE nOldLength = m_pData->m_nDataLength; + if (nCount > 0 && nIndex < nOldLength) { + FX_STRSIZE mLength = nIndex + nCount; + if (mLength >= nOldLength) { + m_pData->m_nDataLength = nIndex; + return m_pData->m_nDataLength; + } + ReallocBeforeWrite(nOldLength); + int nCharsToCopy = nOldLength - mLength + 1; + FXSYS_memmove(m_pData->m_String + nIndex, m_pData->m_String + mLength, + nCharsToCopy); + m_pData->m_nDataLength = nOldLength - nCount; + } + return m_pData->m_nDataLength; +} + +void CFX_ByteString::Concat(const char* pSrcData, FX_STRSIZE nSrcLen) { + if (!pSrcData || nSrcLen <= 0) + return; + + if (!m_pData) { + m_pData.Reset(StringData::Create(pSrcData, nSrcLen)); + return; + } + + if (m_pData->CanOperateInPlace(m_pData->m_nDataLength + nSrcLen)) { + m_pData->CopyContentsAt(m_pData->m_nDataLength, pSrcData, nSrcLen); + m_pData->m_nDataLength += nSrcLen; + return; + } + + CFX_RetainPtr pNewData( + StringData::Create(m_pData->m_nDataLength + nSrcLen)); + pNewData->CopyContents(*m_pData); + pNewData->CopyContentsAt(m_pData->m_nDataLength, pSrcData, nSrcLen); + m_pData.Swap(pNewData); +} + +CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst) const { + if (!m_pData) + return CFX_ByteString(); + + return Mid(nFirst, m_pData->m_nDataLength - nFirst); +} + +CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const { + if (!m_pData) + return CFX_ByteString(); + + nFirst = pdfium::clamp(nFirst, 0, m_pData->m_nDataLength); + nCount = pdfium::clamp(nCount, 0, m_pData->m_nDataLength - nFirst); + if (nCount == 0) + return CFX_ByteString(); + + if (nFirst == 0 && nCount == m_pData->m_nDataLength) + return *this; + + CFX_ByteString dest; + AllocCopy(dest, nCount, nFirst); + return dest; +} + +void CFX_ByteString::AllocCopy(CFX_ByteString& dest, + FX_STRSIZE nCopyLen, + FX_STRSIZE nCopyIndex) const { + if (nCopyLen <= 0) + return; + + CFX_RetainPtr pNewData( + StringData::Create(m_pData->m_String + nCopyIndex, nCopyLen)); + dest.m_pData.Swap(pNewData); +} + +#define FORCE_ANSI 0x10000 +#define FORCE_UNICODE 0x20000 +#define FORCE_INT64 0x40000 + +CFX_ByteString CFX_ByteString::FormatInteger(int i, uint32_t flags) { + char buf[32]; + return CFX_ByteString(buf, Buffer_itoa(buf, i, flags)); +} + +void CFX_ByteString::FormatV(const char* pFormat, va_list argList) { + va_list argListSave; + FX_VA_COPY(argListSave, argList); + FX_STRSIZE nMaxLen = vsnprintf(nullptr, 0, pFormat, argList); + 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); + vsnprintf(m_pData->m_String, nMaxLen + 1, pFormat, argListSave); + ReleaseBuffer(); + } + } + va_end(argListSave); +} + +void CFX_ByteString::Format(const char* pFormat, ...) { + va_list argList; + va_start(argList, pFormat); + FormatV(pFormat, argList); + va_end(argList); +} + +FX_STRSIZE CFX_ByteString::Insert(FX_STRSIZE nIndex, char ch) { + FX_STRSIZE nNewLength = m_pData ? m_pData->m_nDataLength : 0; + nIndex = std::max(nIndex, 0); + nIndex = std::min(nIndex, nNewLength); + nNewLength++; + + ReallocBeforeWrite(nNewLength); + FXSYS_memmove(m_pData->m_String + nIndex + 1, m_pData->m_String + nIndex, + nNewLength - nIndex); + m_pData->m_String[nIndex] = ch; + m_pData->m_nDataLength = nNewLength; + return nNewLength; +} + +CFX_ByteString CFX_ByteString::Right(FX_STRSIZE nCount) const { + if (!m_pData) + return CFX_ByteString(); + + nCount = std::max(nCount, 0); + if (nCount >= m_pData->m_nDataLength) + return *this; + + CFX_ByteString dest; + AllocCopy(dest, nCount, m_pData->m_nDataLength - nCount); + return dest; +} + +CFX_ByteString CFX_ByteString::Left(FX_STRSIZE nCount) const { + if (!m_pData) + return CFX_ByteString(); + + nCount = std::max(nCount, 0); + if (nCount >= m_pData->m_nDataLength) + return *this; + + CFX_ByteString dest; + AllocCopy(dest, nCount, 0); + return dest; +} + +FX_STRSIZE CFX_ByteString::Find(char ch, FX_STRSIZE nStart) const { + if (!m_pData) + return -1; + + if (nStart < 0 || nStart >= m_pData->m_nDataLength) + return -1; + + const char* pStr = static_cast( + memchr(m_pData->m_String + nStart, ch, m_pData->m_nDataLength - nStart)); + return pStr ? pStr - m_pData->m_String : -1; +} + +FX_STRSIZE CFX_ByteString::ReverseFind(char ch) const { + if (!m_pData) + return -1; + + FX_STRSIZE nLength = m_pData->m_nDataLength; + while (nLength--) { + if (m_pData->m_String[nLength] == ch) + return nLength; + } + return -1; +} + +FX_STRSIZE CFX_ByteString::Find(const CFX_ByteStringC& pSub, + FX_STRSIZE nStart) const { + if (!m_pData) + return -1; + + FX_STRSIZE nLength = m_pData->m_nDataLength; + if (nStart > nLength) + return -1; + + const char* pStr = + FX_strstr(m_pData->m_String + nStart, m_pData->m_nDataLength - nStart, + pSub.c_str(), pSub.GetLength()); + return pStr ? (int)(pStr - m_pData->m_String) : -1; +} + +void CFX_ByteString::MakeLower() { + if (!m_pData) + return; + + ReallocBeforeWrite(m_pData->m_nDataLength); + FXSYS_strlwr(m_pData->m_String); +} + +void CFX_ByteString::MakeUpper() { + if (!m_pData) + return; + + ReallocBeforeWrite(m_pData->m_nDataLength); + FXSYS_strupr(m_pData->m_String); +} + +FX_STRSIZE CFX_ByteString::Remove(char chRemove) { + if (!m_pData || m_pData->m_nDataLength < 1) + return 0; + + char* pstrSource = m_pData->m_String; + char* pstrEnd = m_pData->m_String + m_pData->m_nDataLength; + while (pstrSource < pstrEnd) { + if (*pstrSource == chRemove) + break; + pstrSource++; + } + if (pstrSource == pstrEnd) + return 0; + + ptrdiff_t copied = pstrSource - m_pData->m_String; + ReallocBeforeWrite(m_pData->m_nDataLength); + pstrSource = m_pData->m_String + copied; + pstrEnd = m_pData->m_String + m_pData->m_nDataLength; + + char* pstrDest = pstrSource; + while (pstrSource < pstrEnd) { + if (*pstrSource != chRemove) { + *pstrDest = *pstrSource; + pstrDest++; + } + pstrSource++; + } + + *pstrDest = 0; + FX_STRSIZE nCount = (FX_STRSIZE)(pstrSource - pstrDest); + m_pData->m_nDataLength -= nCount; + return nCount; +} + +FX_STRSIZE CFX_ByteString::Replace(const CFX_ByteStringC& pOld, + const CFX_ByteStringC& pNew) { + if (!m_pData || pOld.IsEmpty()) + return 0; + + FX_STRSIZE nSourceLen = pOld.GetLength(); + FX_STRSIZE nReplacementLen = pNew.GetLength(); + FX_STRSIZE nCount = 0; + const char* pStart = m_pData->m_String; + char* pEnd = m_pData->m_String + m_pData->m_nDataLength; + while (1) { + const char* pTarget = FX_strstr(pStart, (FX_STRSIZE)(pEnd - pStart), + pOld.c_str(), nSourceLen); + if (!pTarget) + break; + + nCount++; + pStart = pTarget + nSourceLen; + } + if (nCount == 0) + return 0; + + FX_STRSIZE nNewLength = + m_pData->m_nDataLength + (nReplacementLen - nSourceLen) * nCount; + + if (nNewLength == 0) { + clear(); + return nCount; + } + + CFX_RetainPtr pNewData(StringData::Create(nNewLength)); + pStart = m_pData->m_String; + char* pDest = pNewData->m_String; + for (FX_STRSIZE i = 0; i < nCount; i++) { + const char* pTarget = FX_strstr(pStart, (FX_STRSIZE)(pEnd - pStart), + pOld.c_str(), nSourceLen); + FXSYS_memcpy(pDest, pStart, pTarget - pStart); + pDest += pTarget - pStart; + FXSYS_memcpy(pDest, pNew.c_str(), pNew.GetLength()); + pDest += pNew.GetLength(); + pStart = pTarget + nSourceLen; + } + FXSYS_memcpy(pDest, pStart, pEnd - pStart); + m_pData.Swap(pNewData); + return nCount; +} + +void CFX_ByteString::SetAt(FX_STRSIZE nIndex, char ch) { + if (!m_pData) { + return; + } + ASSERT(nIndex >= 0); + ASSERT(nIndex < m_pData->m_nDataLength); + ReallocBeforeWrite(m_pData->m_nDataLength); + m_pData->m_String[nIndex] = ch; +} + +CFX_WideString CFX_ByteString::UTF8Decode() const { + CFX_UTF8Decoder decoder; + for (FX_STRSIZE i = 0; i < GetLength(); i++) { + decoder.Input((uint8_t)m_pData->m_String[i]); + } + return CFX_WideString(decoder.GetResult()); +} + +// static +CFX_ByteString CFX_ByteString::FromUnicode(const wchar_t* str, FX_STRSIZE len) { + FX_STRSIZE str_len = len >= 0 ? len : FXSYS_wcslen(str); + return FromUnicode(CFX_WideString(str, str_len)); +} + +// static +CFX_ByteString CFX_ByteString::FromUnicode(const CFX_WideString& str) { + return CFX_CharMap::GetByteString(0, str.AsStringC()); +} + +int CFX_ByteString::Compare(const CFX_ByteStringC& str) const { + if (!m_pData) { + return str.IsEmpty() ? 0 : -1; + } + int this_len = m_pData->m_nDataLength; + int that_len = str.GetLength(); + int min_len = this_len < that_len ? this_len : that_len; + for (int i = 0; i < min_len; i++) { + if ((uint8_t)m_pData->m_String[i] < str.GetAt(i)) { + return -1; + } + if ((uint8_t)m_pData->m_String[i] > str.GetAt(i)) { + return 1; + } + } + if (this_len < that_len) { + return -1; + } + if (this_len > that_len) { + return 1; + } + return 0; +} + +void CFX_ByteString::TrimRight(const CFX_ByteStringC& pTargets) { + if (!m_pData || pTargets.IsEmpty()) { + return; + } + FX_STRSIZE pos = GetLength(); + if (pos < 1) { + return; + } + while (pos) { + FX_STRSIZE i = 0; + while (i < pTargets.GetLength() && + pTargets[i] != m_pData->m_String[pos - 1]) { + i++; + } + if (i == pTargets.GetLength()) { + break; + } + pos--; + } + if (pos < m_pData->m_nDataLength) { + ReallocBeforeWrite(m_pData->m_nDataLength); + m_pData->m_String[pos] = 0; + m_pData->m_nDataLength = pos; + } +} + +void CFX_ByteString::TrimRight(char chTarget) { + TrimRight(CFX_ByteStringC(chTarget)); +} + +void CFX_ByteString::TrimRight() { + TrimRight("\x09\x0a\x0b\x0c\x0d\x20"); +} + +void CFX_ByteString::TrimLeft(const CFX_ByteStringC& pTargets) { + if (!m_pData || pTargets.IsEmpty()) + return; + + FX_STRSIZE len = GetLength(); + if (len < 1) + return; + + FX_STRSIZE pos = 0; + while (pos < len) { + FX_STRSIZE i = 0; + while (i < pTargets.GetLength() && pTargets[i] != m_pData->m_String[pos]) { + i++; + } + if (i == pTargets.GetLength()) { + break; + } + pos++; + } + if (pos) { + ReallocBeforeWrite(len); + FX_STRSIZE nDataLength = len - pos; + FXSYS_memmove(m_pData->m_String, m_pData->m_String + pos, + (nDataLength + 1) * sizeof(char)); + m_pData->m_nDataLength = nDataLength; + } +} + +void CFX_ByteString::TrimLeft(char chTarget) { + TrimLeft(CFX_ByteStringC(chTarget)); +} + +void CFX_ByteString::TrimLeft() { + TrimLeft("\x09\x0a\x0b\x0c\x0d\x20"); +} + +uint32_t CFX_ByteString::GetID(FX_STRSIZE start_pos) const { + return AsStringC().GetID(start_pos); +} +FX_STRSIZE FX_ftoa(float d, char* buf) { + buf[0] = '0'; + buf[1] = '\0'; + if (d == 0.0f) { + return 1; + } + bool bNegative = false; + if (d < 0) { + bNegative = true; + d = -d; + } + int scale = 1; + int scaled = FXSYS_round(d); + while (scaled < 100000) { + if (scale == 1000000) { + break; + } + scale *= 10; + scaled = FXSYS_round(d * scale); + } + if (scaled == 0) { + return 1; + } + char buf2[32]; + int buf_size = 0; + if (bNegative) { + buf[buf_size++] = '-'; + } + int i = scaled / scale; + FXSYS_itoa(i, buf2, 10); + FX_STRSIZE len = FXSYS_strlen(buf2); + FXSYS_memcpy(buf + buf_size, buf2, len); + buf_size += len; + int fraction = scaled % scale; + if (fraction == 0) { + return buf_size; + } + buf[buf_size++] = '.'; + scale /= 10; + while (fraction) { + buf[buf_size++] = '0' + fraction / scale; + fraction %= scale; + scale /= 10; + } + return buf_size; +} +CFX_ByteString CFX_ByteString::FormatFloat(float d, int precision) { + char buf[32]; + FX_STRSIZE len = FX_ftoa(d, buf); + return CFX_ByteString(buf, len); +} diff --git a/core/fxcrt/cfx_bytestring.h b/core/fxcrt/cfx_bytestring.h new file mode 100644 index 0000000000..9596fc5eee --- /dev/null +++ b/core/fxcrt/cfx_bytestring.h @@ -0,0 +1,234 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FXCRT_CFX_BYTESTRING_H_ +#define CORE_FXCRT_CFX_BYTESTRING_H_ + +#include + +#include "core/fxcrt/cfx_retain_ptr.h" +#include "core/fxcrt/cfx_string_c_template.h" +#include "core/fxcrt/cfx_string_data_template.h" +#include "core/fxcrt/fx_memory.h" +#include "core/fxcrt/fx_system.h" + +class CFX_WideString; + +// A mutable string with shared buffers using copy-on-write semantics that +// avoids the cost of std::string's iterator stability guarantees. +class CFX_ByteString { + public: + using CharType = char; + + CFX_ByteString(); + CFX_ByteString(const CFX_ByteString& other); + CFX_ByteString(CFX_ByteString&& other) noexcept; + + // Deliberately implicit to avoid calling on every string literal. + // NOLINTNEXTLINE(runtime/explicit) + CFX_ByteString(char ch); + // NOLINTNEXTLINE(runtime/explicit) + CFX_ByteString(const char* ptr); + + CFX_ByteString(const char* ptr, FX_STRSIZE len); + CFX_ByteString(const uint8_t* ptr, FX_STRSIZE len); + + explicit CFX_ByteString(const CFX_ByteStringC& bstrc); + CFX_ByteString(const CFX_ByteStringC& bstrc1, const CFX_ByteStringC& bstrc2); + + ~CFX_ByteString(); + + void clear() { m_pData.Reset(); } + + static CFX_ByteString FromUnicode(const wchar_t* ptr, FX_STRSIZE len = -1); + static CFX_ByteString FromUnicode(const CFX_WideString& str); + + // Explicit conversion to C-style string. + // Note: Any subsequent modification of |this| will invalidate the result. + const char* c_str() const { return m_pData ? m_pData->m_String : ""; } + + // Explicit conversion to uint8_t*. + // Note: Any subsequent modification of |this| will invalidate the result. + const uint8_t* raw_str() const { + return m_pData ? reinterpret_cast(m_pData->m_String) + : nullptr; + } + + // Explicit conversion to CFX_ByteStringC. + // Note: Any subsequent modification of |this| will invalidate the result. + CFX_ByteStringC AsStringC() const { + return CFX_ByteStringC(raw_str(), GetLength()); + } + + FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } + bool IsEmpty() const { return !GetLength(); } + + int Compare(const CFX_ByteStringC& str) const; + bool EqualNoCase(const CFX_ByteStringC& str) const; + + bool operator==(const char* ptr) const; + bool operator==(const CFX_ByteStringC& str) const; + bool operator==(const CFX_ByteString& other) const; + + bool operator!=(const char* ptr) const { return !(*this == ptr); } + bool operator!=(const CFX_ByteStringC& str) const { return !(*this == str); } + bool operator!=(const CFX_ByteString& other) const { + return !(*this == other); + } + + bool operator<(const CFX_ByteString& str) const; + + const CFX_ByteString& operator=(const char* str); + const CFX_ByteString& operator=(const CFX_ByteStringC& bstrc); + const CFX_ByteString& operator=(const CFX_ByteString& stringSrc); + + const CFX_ByteString& operator+=(char ch); + const CFX_ByteString& operator+=(const char* str); + const CFX_ByteString& operator+=(const CFX_ByteString& str); + const CFX_ByteString& operator+=(const CFX_ByteStringC& bstrc); + + uint8_t GetAt(FX_STRSIZE nIndex) const { + return m_pData ? m_pData->m_String[nIndex] : 0; + } + + uint8_t operator[](FX_STRSIZE nIndex) const { + return m_pData ? m_pData->m_String[nIndex] : 0; + } + + void SetAt(FX_STRSIZE nIndex, char ch); + FX_STRSIZE Insert(FX_STRSIZE index, char ch); + FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); + + void Format(const char* lpszFormat, ...); + void FormatV(const char* lpszFormat, va_list argList); + + void Reserve(FX_STRSIZE len); + char* GetBuffer(FX_STRSIZE len); + void ReleaseBuffer(FX_STRSIZE len = -1); + + CFX_ByteString Mid(FX_STRSIZE first) const; + CFX_ByteString Mid(FX_STRSIZE first, FX_STRSIZE count) const; + CFX_ByteString Left(FX_STRSIZE count) const; + CFX_ByteString Right(FX_STRSIZE count) const; + + FX_STRSIZE Find(const CFX_ByteStringC& lpszSub, FX_STRSIZE start = 0) const; + FX_STRSIZE Find(char ch, FX_STRSIZE start = 0) const; + FX_STRSIZE ReverseFind(char ch) const; + + void MakeLower(); + void MakeUpper(); + + void TrimRight(); + void TrimRight(char chTarget); + void TrimRight(const CFX_ByteStringC& lpszTargets); + + void TrimLeft(); + void TrimLeft(char chTarget); + void TrimLeft(const CFX_ByteStringC& lpszTargets); + + FX_STRSIZE Replace(const CFX_ByteStringC& lpszOld, + const CFX_ByteStringC& lpszNew); + + FX_STRSIZE Remove(char ch); + + CFX_WideString UTF8Decode() const; + + uint32_t GetID(FX_STRSIZE start_pos = 0) const; + +#define FXFORMAT_SIGNED 1 +#define FXFORMAT_HEX 2 +#define FXFORMAT_CAPITAL 4 + + static CFX_ByteString FormatInteger(int i, uint32_t flags = 0); + static CFX_ByteString FormatFloat(float f, int precision = 0); + + protected: + using StringData = CFX_StringDataTemplate; + + void ReallocBeforeWrite(FX_STRSIZE nNewLen); + void AllocBeforeWrite(FX_STRSIZE nNewLen); + void AllocCopy(CFX_ByteString& dest, + FX_STRSIZE nCopyLen, + FX_STRSIZE nCopyIndex) const; + void AssignCopy(const char* pSrcData, FX_STRSIZE nSrcLen); + void Concat(const char* lpszSrcData, FX_STRSIZE nSrcLen); + + CFX_RetainPtr m_pData; + + friend class fxcrt_ByteStringConcat_Test; + friend class fxcrt_ByteStringPool_Test; +}; + +inline bool operator==(const char* lhs, const CFX_ByteString& rhs) { + return rhs == lhs; +} +inline bool operator==(const CFX_ByteStringC& lhs, const CFX_ByteString& rhs) { + return rhs == lhs; +} +inline bool operator!=(const char* lhs, const CFX_ByteString& rhs) { + return rhs != lhs; +} +inline bool operator!=(const CFX_ByteStringC& lhs, const CFX_ByteString& rhs) { + return rhs != lhs; +} + +inline CFX_ByteString operator+(const CFX_ByteStringC& str1, + const CFX_ByteStringC& str2) { + return CFX_ByteString(str1, str2); +} +inline CFX_ByteString operator+(const CFX_ByteStringC& str1, const char* str2) { + return CFX_ByteString(str1, str2); +} +inline CFX_ByteString operator+(const char* str1, const CFX_ByteStringC& str2) { + return CFX_ByteString(str1, str2); +} +inline CFX_ByteString operator+(const CFX_ByteStringC& str1, char ch) { + return CFX_ByteString(str1, CFX_ByteStringC(ch)); +} +inline CFX_ByteString operator+(char ch, const CFX_ByteStringC& str2) { + return CFX_ByteString(ch, str2); +} +inline CFX_ByteString operator+(const CFX_ByteString& str1, + const CFX_ByteString& str2) { + return CFX_ByteString(str1.AsStringC(), str2.AsStringC()); +} +inline CFX_ByteString operator+(const CFX_ByteString& str1, char ch) { + return CFX_ByteString(str1.AsStringC(), CFX_ByteStringC(ch)); +} +inline CFX_ByteString operator+(char ch, const CFX_ByteString& str2) { + return CFX_ByteString(ch, str2.AsStringC()); +} +inline CFX_ByteString operator+(const CFX_ByteString& str1, const char* str2) { + return CFX_ByteString(str1.AsStringC(), str2); +} +inline CFX_ByteString operator+(const char* str1, const CFX_ByteString& str2) { + return CFX_ByteString(str1, str2.AsStringC()); +} +inline CFX_ByteString operator+(const CFX_ByteString& str1, + const CFX_ByteStringC& str2) { + return CFX_ByteString(str1.AsStringC(), str2); +} +inline CFX_ByteString operator+(const CFX_ByteStringC& str1, + const CFX_ByteString& str2) { + return CFX_ByteString(str1, str2.AsStringC()); +} + +uint32_t FX_HashCode_GetA(const CFX_ByteStringC& str, bool bIgnoreCase); + +namespace std { + +template <> +struct hash { + std::size_t operator()(const CFX_ByteString& str) const { + return FX_HashCode_GetA(str.AsStringC(), false); + } +}; + +} // namespace std + +extern template struct std::hash; + +#endif // CORE_FXCRT_CFX_BYTESTRING_H_ diff --git a/core/fxcrt/cfx_bytestring_unittest.cpp b/core/fxcrt/cfx_bytestring_unittest.cpp new file mode 100644 index 0000000000..4f2691c404 --- /dev/null +++ b/core/fxcrt/cfx_bytestring_unittest.cpp @@ -0,0 +1,1091 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "core/fxcrt/cfx_bytestring.h" + +#include "testing/fx_string_testhelpers.h" +#include "testing/gtest/include/gtest/gtest.h" + +TEST(fxcrt, ByteStringOperatorSubscript) { + // CFX_ByteString includes the NUL terminator for non-empty strings. + CFX_ByteString abc("abc"); + EXPECT_EQ('a', abc[0]); + EXPECT_EQ('b', abc[1]); + EXPECT_EQ('c', abc[2]); + EXPECT_EQ(0, abc[3]); +} + +TEST(fxcrt, ByteStringOperatorLT) { + CFX_ByteString empty; + CFX_ByteString a("a"); + CFX_ByteString abc("abc"); + CFX_ByteString def("def"); + + EXPECT_FALSE(empty < empty); + EXPECT_FALSE(a < a); + EXPECT_FALSE(abc < abc); + EXPECT_FALSE(def < def); + + EXPECT_TRUE(empty < a); + EXPECT_FALSE(a < empty); + + EXPECT_TRUE(empty < abc); + EXPECT_FALSE(abc < empty); + + EXPECT_TRUE(empty < def); + EXPECT_FALSE(def < empty); + + EXPECT_TRUE(a < abc); + EXPECT_FALSE(abc < a); + + EXPECT_TRUE(a < def); + EXPECT_FALSE(def < a); + + EXPECT_TRUE(abc < def); + EXPECT_FALSE(def < abc); +} + +TEST(fxcrt, ByteStringOperatorEQ) { + CFX_ByteString null_string; + EXPECT_TRUE(null_string == null_string); + + CFX_ByteString empty_string(""); + EXPECT_TRUE(empty_string == empty_string); + EXPECT_TRUE(empty_string == null_string); + EXPECT_TRUE(null_string == empty_string); + + CFX_ByteString deleted_string("hello"); + deleted_string.Delete(0, 5); + EXPECT_TRUE(deleted_string == deleted_string); + EXPECT_TRUE(deleted_string == null_string); + EXPECT_TRUE(deleted_string == empty_string); + EXPECT_TRUE(null_string == deleted_string); + EXPECT_TRUE(empty_string == deleted_string); + + CFX_ByteString byte_string("hello"); + EXPECT_TRUE(byte_string == byte_string); + EXPECT_FALSE(byte_string == null_string); + EXPECT_FALSE(byte_string == empty_string); + EXPECT_FALSE(byte_string == deleted_string); + EXPECT_FALSE(null_string == byte_string); + EXPECT_FALSE(empty_string == byte_string); + EXPECT_FALSE(deleted_string == byte_string); + + CFX_ByteString byte_string_same1("hello"); + EXPECT_TRUE(byte_string == byte_string_same1); + EXPECT_TRUE(byte_string_same1 == byte_string); + + CFX_ByteString byte_string_same2(byte_string); + EXPECT_TRUE(byte_string == byte_string_same2); + EXPECT_TRUE(byte_string_same2 == byte_string); + + CFX_ByteString byte_string1("he"); + CFX_ByteString byte_string2("hellp"); + CFX_ByteString byte_string3("hellod"); + EXPECT_FALSE(byte_string == byte_string1); + EXPECT_FALSE(byte_string == byte_string2); + EXPECT_FALSE(byte_string == byte_string3); + EXPECT_FALSE(byte_string1 == byte_string); + EXPECT_FALSE(byte_string2 == byte_string); + EXPECT_FALSE(byte_string3 == byte_string); + + CFX_ByteStringC null_string_c; + CFX_ByteStringC empty_string_c(""); + EXPECT_TRUE(null_string == null_string_c); + EXPECT_TRUE(null_string == empty_string_c); + EXPECT_TRUE(empty_string == null_string_c); + EXPECT_TRUE(empty_string == empty_string_c); + EXPECT_TRUE(deleted_string == null_string_c); + EXPECT_TRUE(deleted_string == empty_string_c); + EXPECT_TRUE(null_string_c == null_string); + EXPECT_TRUE(empty_string_c == null_string); + EXPECT_TRUE(null_string_c == empty_string); + EXPECT_TRUE(empty_string_c == empty_string); + EXPECT_TRUE(null_string_c == deleted_string); + EXPECT_TRUE(empty_string_c == deleted_string); + + CFX_ByteStringC byte_string_c_same1("hello"); + EXPECT_TRUE(byte_string == byte_string_c_same1); + EXPECT_TRUE(byte_string_c_same1 == byte_string); + + CFX_ByteStringC byte_string_c1("he"); + CFX_ByteStringC byte_string_c2("hellp"); + CFX_ByteStringC byte_string_c3("hellod"); + EXPECT_FALSE(byte_string == byte_string_c1); + EXPECT_FALSE(byte_string == byte_string_c2); + EXPECT_FALSE(byte_string == byte_string_c3); + EXPECT_FALSE(byte_string_c1 == byte_string); + EXPECT_FALSE(byte_string_c2 == byte_string); + EXPECT_FALSE(byte_string_c3 == byte_string); + + const char* c_null_string = nullptr; + const char* c_empty_string = ""; + EXPECT_TRUE(null_string == c_null_string); + EXPECT_TRUE(null_string == c_empty_string); + EXPECT_TRUE(empty_string == c_null_string); + EXPECT_TRUE(empty_string == c_empty_string); + EXPECT_TRUE(deleted_string == c_null_string); + EXPECT_TRUE(deleted_string == c_empty_string); + EXPECT_TRUE(c_null_string == null_string); + EXPECT_TRUE(c_empty_string == null_string); + EXPECT_TRUE(c_null_string == empty_string); + EXPECT_TRUE(c_empty_string == empty_string); + EXPECT_TRUE(c_null_string == deleted_string); + EXPECT_TRUE(c_empty_string == deleted_string); + + const char* c_string_same1 = "hello"; + EXPECT_TRUE(byte_string == c_string_same1); + EXPECT_TRUE(c_string_same1 == byte_string); + + const char* c_string1 = "he"; + const char* c_string2 = "hellp"; + const char* c_string3 = "hellod"; + EXPECT_FALSE(byte_string == c_string1); + EXPECT_FALSE(byte_string == c_string2); + EXPECT_FALSE(byte_string == c_string3); + EXPECT_FALSE(c_string1 == byte_string); + EXPECT_FALSE(c_string2 == byte_string); + EXPECT_FALSE(c_string3 == byte_string); +} + +TEST(fxcrt, ByteStringOperatorNE) { + CFX_ByteString null_string; + EXPECT_FALSE(null_string != null_string); + + CFX_ByteString empty_string(""); + EXPECT_FALSE(empty_string != empty_string); + EXPECT_FALSE(empty_string != null_string); + EXPECT_FALSE(null_string != empty_string); + + CFX_ByteString deleted_string("hello"); + deleted_string.Delete(0, 5); + EXPECT_FALSE(deleted_string != deleted_string); + EXPECT_FALSE(deleted_string != null_string); + EXPECT_FALSE(deleted_string != empty_string); + EXPECT_FALSE(deleted_string != deleted_string); + EXPECT_FALSE(null_string != deleted_string); + EXPECT_FALSE(empty_string != deleted_string); + EXPECT_FALSE(deleted_string != deleted_string); + + CFX_ByteString byte_string("hello"); + EXPECT_FALSE(byte_string != byte_string); + EXPECT_TRUE(byte_string != null_string); + EXPECT_TRUE(byte_string != empty_string); + EXPECT_TRUE(byte_string != deleted_string); + EXPECT_TRUE(null_string != byte_string); + EXPECT_TRUE(empty_string != byte_string); + EXPECT_TRUE(deleted_string != byte_string); + + CFX_ByteString byte_string_same1("hello"); + EXPECT_FALSE(byte_string != byte_string_same1); + EXPECT_FALSE(byte_string_same1 != byte_string); + + CFX_ByteString byte_string_same2(byte_string); + EXPECT_FALSE(byte_string != byte_string_same2); + EXPECT_FALSE(byte_string_same2 != byte_string); + + CFX_ByteString byte_string1("he"); + CFX_ByteString byte_string2("hellp"); + CFX_ByteString byte_string3("hellod"); + EXPECT_TRUE(byte_string != byte_string1); + EXPECT_TRUE(byte_string != byte_string2); + EXPECT_TRUE(byte_string != byte_string3); + EXPECT_TRUE(byte_string1 != byte_string); + EXPECT_TRUE(byte_string2 != byte_string); + EXPECT_TRUE(byte_string3 != byte_string); + + CFX_ByteStringC null_string_c; + CFX_ByteStringC empty_string_c(""); + EXPECT_FALSE(null_string != null_string_c); + EXPECT_FALSE(null_string != empty_string_c); + EXPECT_FALSE(empty_string != null_string_c); + EXPECT_FALSE(empty_string != empty_string_c); + EXPECT_FALSE(null_string_c != null_string); + EXPECT_FALSE(empty_string_c != null_string); + EXPECT_FALSE(null_string_c != empty_string); + EXPECT_FALSE(empty_string_c != empty_string); + + CFX_ByteStringC byte_string_c_same1("hello"); + EXPECT_FALSE(byte_string != byte_string_c_same1); + EXPECT_FALSE(byte_string_c_same1 != byte_string); + + CFX_ByteStringC byte_string_c1("he"); + CFX_ByteStringC byte_string_c2("hellp"); + CFX_ByteStringC byte_string_c3("hellod"); + EXPECT_TRUE(byte_string != byte_string_c1); + EXPECT_TRUE(byte_string != byte_string_c2); + EXPECT_TRUE(byte_string != byte_string_c3); + EXPECT_TRUE(byte_string_c1 != byte_string); + EXPECT_TRUE(byte_string_c2 != byte_string); + EXPECT_TRUE(byte_string_c3 != byte_string); + + const char* c_null_string = nullptr; + const char* c_empty_string = ""; + EXPECT_FALSE(null_string != c_null_string); + EXPECT_FALSE(null_string != c_empty_string); + EXPECT_FALSE(empty_string != c_null_string); + EXPECT_FALSE(empty_string != c_empty_string); + EXPECT_FALSE(deleted_string != c_null_string); + EXPECT_FALSE(deleted_string != c_empty_string); + EXPECT_FALSE(c_null_string != null_string); + EXPECT_FALSE(c_empty_string != null_string); + EXPECT_FALSE(c_null_string != empty_string); + EXPECT_FALSE(c_empty_string != empty_string); + EXPECT_FALSE(c_null_string != deleted_string); + EXPECT_FALSE(c_empty_string != deleted_string); + + const char* c_string_same1 = "hello"; + EXPECT_FALSE(byte_string != c_string_same1); + EXPECT_FALSE(c_string_same1 != byte_string); + + const char* c_string1 = "he"; + const char* c_string2 = "hellp"; + const char* c_string3 = "hellod"; + EXPECT_TRUE(byte_string != c_string1); + EXPECT_TRUE(byte_string != c_string2); + EXPECT_TRUE(byte_string != c_string3); + EXPECT_TRUE(c_string1 != byte_string); + EXPECT_TRUE(c_string2 != byte_string); + EXPECT_TRUE(c_string3 != byte_string); +} + +TEST(fxcrt, ByteStringCNull) { + CFX_ByteStringC null_string; + EXPECT_FALSE(null_string.raw_str()); + EXPECT_EQ(null_string.GetLength(), 0); + EXPECT_TRUE(null_string.IsEmpty()); + + CFX_ByteStringC another_null_string; + EXPECT_EQ(null_string, another_null_string); + + CFX_ByteStringC copied_null_string(null_string); + EXPECT_FALSE(copied_null_string.raw_str()); + EXPECT_EQ(copied_null_string.GetLength(), 0); + EXPECT_TRUE(copied_null_string.IsEmpty()); + EXPECT_EQ(null_string, copied_null_string); + + CFX_ByteStringC empty_string(""); // Pointer to NUL, not NULL pointer. + EXPECT_TRUE(empty_string.raw_str()); + EXPECT_EQ(empty_string.GetLength(), 0); + EXPECT_TRUE(empty_string.IsEmpty()); + EXPECT_EQ(null_string, empty_string); + + CFX_ByteStringC assigned_null_string("initially not nullptr"); + assigned_null_string = null_string; + EXPECT_FALSE(assigned_null_string.raw_str()); + EXPECT_EQ(assigned_null_string.GetLength(), 0); + EXPECT_TRUE(assigned_null_string.IsEmpty()); + EXPECT_EQ(null_string, assigned_null_string); + + CFX_ByteStringC assigned_nullptr_string("initially not nullptr"); + assigned_nullptr_string = (const char*)nullptr; + EXPECT_FALSE(assigned_nullptr_string.raw_str()); + EXPECT_EQ(assigned_nullptr_string.GetLength(), 0); + EXPECT_TRUE(assigned_nullptr_string.IsEmpty()); + EXPECT_EQ(null_string, assigned_nullptr_string); + + CFX_ByteStringC non_null_string("a"); + EXPECT_NE(null_string, non_null_string); +} + +TEST(fxcrt, ByteStringConcat) { + CFX_ByteString fred; + fred.Concat("FRED", 4); + EXPECT_EQ("FRED", fred); + + fred.Concat("DY", 2); + EXPECT_EQ("FREDDY", fred); + + fred.Delete(3, 3); + EXPECT_EQ("FRE", fred); + + fred.Concat("D", 1); + EXPECT_EQ("FRED", fred); + + CFX_ByteString copy = fred; + fred.Concat("DY", 2); + EXPECT_EQ("FREDDY", fred); + EXPECT_EQ("FRED", copy); + + // Test invalid arguments. + copy = fred; + fred.Concat("freddy", -6); + CFX_ByteString not_aliased("xxxxxx"); + EXPECT_EQ("FREDDY", fred); + EXPECT_EQ("xxxxxx", not_aliased); +} + +TEST(fxcrt, ByteStringRemove) { + CFX_ByteString freed("FREED"); + freed.Remove('E'); + EXPECT_EQ("FRD", freed); + freed.Remove('F'); + EXPECT_EQ("RD", freed); + freed.Remove('D'); + EXPECT_EQ("R", freed); + freed.Remove('X'); + EXPECT_EQ("R", freed); + freed.Remove('R'); + EXPECT_EQ("", freed); + + CFX_ByteString empty; + empty.Remove('X'); + EXPECT_EQ("", empty); +} + +TEST(fxcrt, ByteStringRemoveCopies) { + CFX_ByteString freed("FREED"); + const char* old_buffer = freed.c_str(); + + // No change with single reference - no copy. + freed.Remove('Q'); + EXPECT_EQ("FREED", freed); + EXPECT_EQ(old_buffer, freed.c_str()); + + // Change with single reference - no copy. + freed.Remove('E'); + EXPECT_EQ("FRD", freed); + EXPECT_EQ(old_buffer, freed.c_str()); + + // No change with multiple references - no copy. + CFX_ByteString shared(freed); + freed.Remove('Q'); + EXPECT_EQ("FRD", freed); + EXPECT_EQ(old_buffer, freed.c_str()); + EXPECT_EQ(old_buffer, shared.c_str()); + + // Change with multiple references -- must copy. + freed.Remove('D'); + EXPECT_EQ("FR", freed); + EXPECT_NE(old_buffer, freed.c_str()); + EXPECT_EQ("FRD", shared); + EXPECT_EQ(old_buffer, shared.c_str()); +} + +TEST(fxcrt, ByteStringReplace) { + CFX_ByteString fred("FRED"); + fred.Replace("FR", "BL"); + EXPECT_EQ("BLED", fred); + fred.Replace("D", "DDY"); + EXPECT_EQ("BLEDDY", fred); + fred.Replace("LEDD", ""); + EXPECT_EQ("BY", fred); + fred.Replace("X", "CLAMS"); + EXPECT_EQ("BY", fred); + fred.Replace("BY", "HI"); + EXPECT_EQ("HI", fred); + fred.Replace("", "CLAMS"); + EXPECT_EQ("HI", fred); + fred.Replace("HI", ""); + EXPECT_EQ("", fred); +} + +TEST(fxcrt, ByteStringInsert) { + CFX_ByteString fred("FRED"); + fred.Insert(-1, 'X'); + EXPECT_EQ("XFRED", fred); + fred.Insert(0, 'S'); + EXPECT_EQ("SXFRED", fred); + fred.Insert(2, 'T'); + EXPECT_EQ("SXTFRED", fred); + fred.Insert(5, 'U'); + EXPECT_EQ("SXTFRUED", fred); + fred.Insert(8, 'V'); + EXPECT_EQ("SXTFRUEDV", fred); + fred.Insert(12, 'P'); + EXPECT_EQ("SXTFRUEDVP", fred); + { + CFX_ByteString empty; + empty.Insert(-1, 'X'); + EXPECT_EQ("X", empty); + } + { + CFX_ByteString empty; + empty.Insert(0, 'X'); + EXPECT_EQ("X", empty); + } + { + CFX_ByteString empty; + empty.Insert(5, 'X'); + EXPECT_EQ("X", empty); + } +} + +TEST(fxcrt, ByteStringDelete) { + CFX_ByteString fred("FRED"); + fred.Delete(0, 2); + EXPECT_EQ("ED", fred); + fred.Delete(1); + EXPECT_EQ("E", fred); + fred.Delete(-1); + EXPECT_EQ("", fred); + fred.Delete(1); + EXPECT_EQ("", fred); + + CFX_ByteString empty; + empty.Delete(0); + EXPECT_EQ("", empty); + empty.Delete(-1); + EXPECT_EQ("", empty); + empty.Delete(1); + EXPECT_EQ("", empty); +} + +TEST(fxcrt, ByteStringMid) { + CFX_ByteString fred("FRED"); + EXPECT_EQ("", fred.Mid(0, 0)); + EXPECT_EQ("", fred.Mid(3, 0)); + EXPECT_EQ("FRED", fred.Mid(0)); + EXPECT_EQ("RED", fred.Mid(1)); + EXPECT_EQ("ED", fred.Mid(2)); + EXPECT_EQ("D", fred.Mid(3)); + EXPECT_EQ("F", fred.Mid(0, 1)); + EXPECT_EQ("R", fred.Mid(1, 1)); + EXPECT_EQ("E", fred.Mid(2, 1)); + EXPECT_EQ("D", fred.Mid(3, 1)); + EXPECT_EQ("FR", fred.Mid(0, 2)); + EXPECT_EQ("FRED", fred.Mid(0, 4)); + EXPECT_EQ("FRED", fred.Mid(0, 10)); + + EXPECT_EQ("FR", fred.Mid(-1, 2)); + EXPECT_EQ("RED", fred.Mid(1, 4)); + EXPECT_EQ("", fred.Mid(4, 1)); + + CFX_ByteString empty; + EXPECT_EQ("", empty.Mid(0, 0)); + EXPECT_EQ("", empty.Mid(0)); + EXPECT_EQ("", empty.Mid(1)); + EXPECT_EQ("", empty.Mid(-1)); +} + +TEST(fxcrt, ByteStringLeft) { + CFX_ByteString fred("FRED"); + EXPECT_EQ("", fred.Left(0)); + EXPECT_EQ("F", fred.Left(1)); + EXPECT_EQ("FR", fred.Left(2)); + EXPECT_EQ("FRE", fred.Left(3)); + EXPECT_EQ("FRED", fred.Left(4)); + + EXPECT_EQ("FRED", fred.Left(5)); + EXPECT_EQ("", fred.Left(-1)); + + CFX_ByteString empty; + EXPECT_EQ("", empty.Left(0)); + EXPECT_EQ("", empty.Left(1)); + EXPECT_EQ("", empty.Left(-1)); +} + +TEST(fxcrt, ByteStringRight) { + CFX_ByteString fred("FRED"); + EXPECT_EQ("", fred.Right(0)); + EXPECT_EQ("D", fred.Right(1)); + EXPECT_EQ("ED", fred.Right(2)); + EXPECT_EQ("RED", fred.Right(3)); + EXPECT_EQ("FRED", fred.Right(4)); + + EXPECT_EQ("FRED", fred.Right(5)); + EXPECT_EQ("", fred.Right(-1)); + + CFX_ByteString empty; + EXPECT_EQ("", empty.Right(0)); + EXPECT_EQ("", empty.Right(1)); + EXPECT_EQ("", empty.Right(-1)); +} + +TEST(fxcrt, ByteStringUpperLower) { + CFX_ByteString fred("F-Re.42D"); + fred.MakeLower(); + EXPECT_EQ("f-re.42d", fred); + fred.MakeUpper(); + EXPECT_EQ("F-RE.42D", fred); + + CFX_ByteString empty; + empty.MakeLower(); + EXPECT_EQ("", empty); + empty.MakeUpper(); + EXPECT_EQ("", empty); +} + +TEST(fxcrt, ByteStringTrimRight) { + CFX_ByteString fred(" FRED "); + fred.TrimRight(); + EXPECT_EQ(" FRED", fred); + fred.TrimRight('E'); + EXPECT_EQ(" FRED", fred); + fred.TrimRight('D'); + EXPECT_EQ(" FRE", fred); + fred.TrimRight("ERP"); + EXPECT_EQ(" F", fred); + + CFX_ByteString blank(" "); + blank.TrimRight("ERP"); + EXPECT_EQ(" ", blank); + blank.TrimRight('E'); + EXPECT_EQ(" ", blank); + blank.TrimRight(); + EXPECT_EQ("", blank); + + CFX_ByteString empty; + empty.TrimRight("ERP"); + EXPECT_EQ("", empty); + empty.TrimRight('E'); + EXPECT_EQ("", empty); + empty.TrimRight(); + EXPECT_EQ("", empty); +} + +TEST(fxcrt, ByteStringTrimRightCopies) { + { + // With a single reference, no copy takes place. + CFX_ByteString fred(" FRED "); + const char* old_buffer = fred.c_str(); + fred.TrimRight(); + EXPECT_EQ(" FRED", fred); + EXPECT_EQ(old_buffer, fred.c_str()); + } + { + // With multiple references, we must copy. + CFX_ByteString fred(" FRED "); + CFX_ByteString other_fred = fred; + const char* old_buffer = fred.c_str(); + fred.TrimRight(); + EXPECT_EQ(" FRED", fred); + EXPECT_EQ(" FRED ", other_fred); + EXPECT_NE(old_buffer, fred.c_str()); + } + { + // With multiple references, but no modifications, no copy. + CFX_ByteString fred("FRED"); + CFX_ByteString other_fred = fred; + const char* old_buffer = fred.c_str(); + fred.TrimRight(); + EXPECT_EQ("FRED", fred); + EXPECT_EQ("FRED", other_fred); + EXPECT_EQ(old_buffer, fred.c_str()); + } +} + +TEST(fxcrt, ByteStringTrimLeft) { + CFX_ByteString fred(" FRED "); + fred.TrimLeft(); + EXPECT_EQ("FRED ", fred); + fred.TrimLeft('E'); + EXPECT_EQ("FRED ", fred); + fred.TrimLeft('F'); + EXPECT_EQ("RED ", fred); + fred.TrimLeft("ERP"); + EXPECT_EQ("D ", fred); + + CFX_ByteString blank(" "); + blank.TrimLeft("ERP"); + EXPECT_EQ(" ", blank); + blank.TrimLeft('E'); + EXPECT_EQ(" ", blank); + blank.TrimLeft(); + EXPECT_EQ("", blank); + + CFX_ByteString empty; + empty.TrimLeft("ERP"); + EXPECT_EQ("", empty); + empty.TrimLeft('E'); + EXPECT_EQ("", empty); + empty.TrimLeft(); + EXPECT_EQ("", empty); +} + +TEST(fxcrt, ByteStringTrimLeftCopies) { + { + // With a single reference, no copy takes place. + CFX_ByteString fred(" FRED "); + const char* old_buffer = fred.c_str(); + fred.TrimLeft(); + EXPECT_EQ("FRED ", fred); + EXPECT_EQ(old_buffer, fred.c_str()); + } + { + // With multiple references, we must copy. + CFX_ByteString fred(" FRED "); + CFX_ByteString other_fred = fred; + const char* old_buffer = fred.c_str(); + fred.TrimLeft(); + EXPECT_EQ("FRED ", fred); + EXPECT_EQ(" FRED ", other_fred); + EXPECT_NE(old_buffer, fred.c_str()); + } + { + // With multiple references, but no modifications, no copy. + CFX_ByteString fred("FRED"); + CFX_ByteString other_fred = fred; + const char* old_buffer = fred.c_str(); + fred.TrimLeft(); + EXPECT_EQ("FRED", fred); + EXPECT_EQ("FRED", other_fred); + EXPECT_EQ(old_buffer, fred.c_str()); + } +} + +TEST(fxcrt, ByteStringReserve) { + { + CFX_ByteString str; + str.Reserve(6); + const char* old_buffer = str.c_str(); + str += "ABCDEF"; + EXPECT_EQ(old_buffer, str.c_str()); + str += "Blah Blah Blah Blah Blah Blah"; + EXPECT_NE(old_buffer, str.c_str()); + } + { + CFX_ByteString str("A"); + str.Reserve(6); + const char* old_buffer = str.c_str(); + str += "BCDEF"; + EXPECT_EQ(old_buffer, str.c_str()); + str += "Blah Blah Blah Blah Blah Blah"; + EXPECT_NE(old_buffer, str.c_str()); + } +} + +TEST(fxcrt, ByteStringGetBuffer) { + { + CFX_ByteString str; + char* buffer = str.GetBuffer(12); + // NOLINTNEXTLINE(runtime/printf) + strcpy(buffer, "clams"); + str.ReleaseBuffer(); + EXPECT_EQ("clams", str); + } + { + CFX_ByteString str("cl"); + char* buffer = str.GetBuffer(12); + // NOLINTNEXTLINE(runtime/printf) + strcpy(buffer + 2, "ams"); + str.ReleaseBuffer(); + EXPECT_EQ("clams", str); + } +} + +TEST(fxcrt, ByteStringReleaseBuffer) { + { + CFX_ByteString str; + str.Reserve(12); + str += "clams"; + const char* old_buffer = str.c_str(); + str.ReleaseBuffer(4); + EXPECT_EQ(old_buffer, str.c_str()); + EXPECT_EQ("clam", str); + } + { + CFX_ByteString str("c"); + str.Reserve(12); + str += "lams"; + const char* old_buffer = str.c_str(); + str.ReleaseBuffer(4); + EXPECT_EQ(old_buffer, str.c_str()); + EXPECT_EQ("clam", str); + } + { + CFX_ByteString str; + str.Reserve(200); + str += "clams"; + const char* old_buffer = str.c_str(); + str.ReleaseBuffer(4); + EXPECT_NE(old_buffer, str.c_str()); + EXPECT_EQ("clam", str); + } + { + CFX_ByteString str("c"); + str.Reserve(200); + str += "lams"; + const char* old_buffer = str.c_str(); + str.ReleaseBuffer(4); + EXPECT_NE(old_buffer, str.c_str()); + EXPECT_EQ("clam", str); + } +} + +TEST(fxcrt, ByteStringCNotNull) { + CFX_ByteStringC string3("abc"); + CFX_ByteStringC string6("abcdef"); + CFX_ByteStringC alternate_string3("abcdef", 3); + CFX_ByteStringC embedded_nul_string7("abc\0def", 7); + CFX_ByteStringC illegal_string7("abcdef", 7); + + EXPECT_EQ(3, string3.GetLength()); + EXPECT_EQ(6, string6.GetLength()); + EXPECT_EQ(3, alternate_string3.GetLength()); + EXPECT_EQ(7, embedded_nul_string7.GetLength()); + EXPECT_EQ(7, illegal_string7.GetLength()); + + EXPECT_NE(string3, string6); + EXPECT_EQ(string3, alternate_string3); + EXPECT_NE(string3, embedded_nul_string7); + EXPECT_NE(string3, illegal_string7); + EXPECT_NE(string6, alternate_string3); + EXPECT_NE(string6, embedded_nul_string7); + EXPECT_NE(string6, illegal_string7); + EXPECT_NE(alternate_string3, embedded_nul_string7); + EXPECT_NE(alternate_string3, illegal_string7); + EXPECT_NE(embedded_nul_string7, illegal_string7); + + CFX_ByteStringC copied_string3(string3); + CFX_ByteStringC copied_alternate_string3(alternate_string3); + CFX_ByteStringC copied_embedded_nul_string7(embedded_nul_string7); + + EXPECT_EQ(string3, copied_string3); + EXPECT_EQ(alternate_string3, copied_alternate_string3); + EXPECT_EQ(embedded_nul_string7, copied_embedded_nul_string7); + + CFX_ByteStringC assigned_string3("intially something else"); + CFX_ByteStringC assigned_alternate_string3("initally something else"); + CFX_ByteStringC assigned_ptr_string3("initially something else"); + CFX_ByteStringC assigned_embedded_nul_string7("initially something else"); + + assigned_string3 = string3; + assigned_alternate_string3 = alternate_string3; + assigned_ptr_string3 = "abc"; + assigned_embedded_nul_string7 = embedded_nul_string7; + EXPECT_EQ(string3, assigned_string3); + EXPECT_EQ(alternate_string3, assigned_alternate_string3); + EXPECT_EQ(alternate_string3, assigned_ptr_string3); + EXPECT_EQ(embedded_nul_string7, assigned_embedded_nul_string7); +} + +TEST(fxcrt, ByteStringCFromChar) { + CFX_ByteStringC null_string; + CFX_ByteStringC lower_a_string("a"); + + // Must have lvalues that outlive the corresponding ByteStringC. + char nul = '\0'; + char lower_a = 'a'; + CFX_ByteStringC nul_string_from_char(nul); + CFX_ByteStringC lower_a_string_from_char(lower_a); + + // Pointer to nul, not nullptr ptr, hence length 1 ... + EXPECT_EQ(1, nul_string_from_char.GetLength()); + EXPECT_NE(null_string, nul_string_from_char); + + EXPECT_EQ(1, lower_a_string_from_char.GetLength()); + EXPECT_EQ(lower_a_string, lower_a_string_from_char); + EXPECT_NE(nul_string_from_char, lower_a_string_from_char); + + CFX_ByteStringC longer_string("ab"); + EXPECT_NE(longer_string, lower_a_string_from_char); +} + +TEST(fxcrt, ByteStringCGetID) { + CFX_ByteStringC null_string; + EXPECT_EQ(0u, null_string.GetID()); + EXPECT_EQ(0u, null_string.GetID(1)); + EXPECT_EQ(0u, null_string.GetID(-1)); + EXPECT_EQ(0u, null_string.GetID(-1000000)); + + CFX_ByteStringC empty_string(""); + EXPECT_EQ(0u, empty_string.GetID()); + EXPECT_EQ(0u, empty_string.GetID(1)); + EXPECT_EQ(0u, empty_string.GetID(-1)); + EXPECT_EQ(0u, empty_string.GetID(-1000000)); + + CFX_ByteStringC short_string("ab"); + EXPECT_EQ(FXBSTR_ID('a', 'b', 0, 0), short_string.GetID()); + EXPECT_EQ(FXBSTR_ID('b', 0, 0, 0), short_string.GetID(1)); + EXPECT_EQ(0u, short_string.GetID(2)); + EXPECT_EQ(0u, short_string.GetID(-1)); + EXPECT_EQ(0u, short_string.GetID(-1000000)); + + CFX_ByteStringC longer_string("abcdef"); + EXPECT_EQ(FXBSTR_ID('a', 'b', 'c', 'd'), longer_string.GetID()); + EXPECT_EQ(FXBSTR_ID('b', 'c', 'd', 'e'), longer_string.GetID(1)); + EXPECT_EQ(FXBSTR_ID('c', 'd', 'e', 'f'), longer_string.GetID(2)); + EXPECT_EQ(FXBSTR_ID('d', 'e', 'f', 0), longer_string.GetID(3)); + EXPECT_EQ(FXBSTR_ID('e', 'f', 0, 0), longer_string.GetID(4)); + EXPECT_EQ(FXBSTR_ID('f', 0, 0, 0), longer_string.GetID(5)); + EXPECT_EQ(0u, longer_string.GetID(6)); + EXPECT_EQ(0u, longer_string.GetID(-1)); + EXPECT_EQ(0u, longer_string.GetID(-1000000)); +} + +TEST(fxcrt, ByteStringCFind) { + CFX_ByteStringC null_string; + EXPECT_EQ(-1, null_string.Find('a')); + EXPECT_EQ(-1, null_string.Find(0)); + + CFX_ByteStringC empty_string(""); + EXPECT_EQ(-1, empty_string.Find('a')); + EXPECT_EQ(-1, empty_string.Find(0)); + + CFX_ByteStringC single_string("a"); + EXPECT_EQ(0, single_string.Find('a')); + EXPECT_EQ(-1, single_string.Find('b')); + EXPECT_EQ(-1, single_string.Find(0)); + + CFX_ByteStringC longer_string("abccc"); + EXPECT_EQ(0, longer_string.Find('a')); + EXPECT_EQ(2, longer_string.Find('c')); + EXPECT_EQ(-1, longer_string.Find(0)); + + CFX_ByteStringC hibyte_string( + "ab\x8c" + "def"); + EXPECT_EQ(2, hibyte_string.Find('\x8c')); +} + +TEST(fxcrt, ByteStringCMid) { + CFX_ByteStringC null_string; + EXPECT_EQ(null_string, null_string.Mid(0, 1)); + EXPECT_EQ(null_string, null_string.Mid(1, 1)); + + CFX_ByteStringC empty_string(""); + EXPECT_EQ(empty_string, empty_string.Mid(0, 1)); + EXPECT_EQ(empty_string, empty_string.Mid(1, 1)); + + CFX_ByteStringC single_character("a"); + EXPECT_EQ(empty_string, single_character.Mid(0, 0)); + EXPECT_EQ(single_character, single_character.Mid(0, 1)); + EXPECT_EQ(empty_string, single_character.Mid(1, 0)); + EXPECT_EQ(empty_string, single_character.Mid(1, 1)); + + CFX_ByteStringC longer_string("abcdef"); + EXPECT_EQ(longer_string, longer_string.Mid(0, 6)); + EXPECT_EQ(longer_string, longer_string.Mid(0, 187)); + EXPECT_EQ(longer_string, longer_string.Mid(-42, 6)); + EXPECT_EQ(longer_string, longer_string.Mid(-42, 187)); + + CFX_ByteStringC leading_substring("ab"); + EXPECT_EQ(leading_substring, longer_string.Mid(0, 2)); + EXPECT_EQ(leading_substring, longer_string.Mid(-1, 2)); + + CFX_ByteStringC middle_substring("bcde"); + EXPECT_EQ(middle_substring, longer_string.Mid(1, 4)); + + CFX_ByteStringC trailing_substring("ef"); + EXPECT_EQ(trailing_substring, longer_string.Mid(4, 2)); + EXPECT_EQ(trailing_substring, longer_string.Mid(4, 3)); +} + +TEST(fxcrt, ByteStringCGetAt) { + CFX_ByteString short_string("a"); + CFX_ByteString longer_string("abc"); + CFX_ByteString embedded_nul_string("ab\0c", 4); + + EXPECT_EQ('a', short_string.GetAt(0)); + EXPECT_EQ('c', longer_string.GetAt(2)); + EXPECT_EQ('b', embedded_nul_string.GetAt(1)); + EXPECT_EQ('\0', embedded_nul_string.GetAt(2)); + EXPECT_EQ('c', embedded_nul_string.GetAt(3)); +} + +TEST(fxcrt, ByteStringCOperatorSubscript) { + // CFX_ByteStringC includes the NUL terminator for non-empty strings. + CFX_ByteStringC abc("abc"); + EXPECT_EQ('a', abc[0]); + EXPECT_EQ('b', abc[1]); + EXPECT_EQ('c', abc[2]); + EXPECT_EQ(0, abc[3]); +} + +TEST(fxcrt, ByteStringCOperatorLT) { + CFX_ByteStringC empty; + CFX_ByteStringC a("a"); + CFX_ByteStringC abc("abc"); + CFX_ByteStringC def("def"); + + EXPECT_FALSE(empty < empty); + EXPECT_FALSE(a < a); + EXPECT_FALSE(abc < abc); + EXPECT_FALSE(def < def); + + EXPECT_TRUE(empty < a); + EXPECT_FALSE(a < empty); + + EXPECT_TRUE(empty < abc); + EXPECT_FALSE(abc < empty); + + EXPECT_TRUE(empty < def); + EXPECT_FALSE(def < empty); + + EXPECT_TRUE(a < abc); + EXPECT_FALSE(abc < a); + + EXPECT_TRUE(a < def); + EXPECT_FALSE(def < a); + + EXPECT_TRUE(abc < def); + EXPECT_FALSE(def < abc); +} + +TEST(fxcrt, ByteStringCOperatorEQ) { + CFX_ByteStringC byte_string_c("hello"); + EXPECT_TRUE(byte_string_c == byte_string_c); + + CFX_ByteStringC byte_string_c_same1("hello"); + EXPECT_TRUE(byte_string_c == byte_string_c_same1); + EXPECT_TRUE(byte_string_c_same1 == byte_string_c); + + CFX_ByteStringC byte_string_c_same2(byte_string_c); + EXPECT_TRUE(byte_string_c == byte_string_c_same2); + EXPECT_TRUE(byte_string_c_same2 == byte_string_c); + + CFX_ByteStringC byte_string_c1("he"); + CFX_ByteStringC byte_string_c2("hellp"); + CFX_ByteStringC byte_string_c3("hellod"); + EXPECT_FALSE(byte_string_c == byte_string_c1); + EXPECT_FALSE(byte_string_c == byte_string_c2); + EXPECT_FALSE(byte_string_c == byte_string_c3); + EXPECT_FALSE(byte_string_c1 == byte_string_c); + EXPECT_FALSE(byte_string_c2 == byte_string_c); + EXPECT_FALSE(byte_string_c3 == byte_string_c); + + CFX_ByteString byte_string_same1("hello"); + EXPECT_TRUE(byte_string_c == byte_string_same1); + EXPECT_TRUE(byte_string_same1 == byte_string_c); + + CFX_ByteString byte_string1("he"); + CFX_ByteString byte_string2("hellp"); + CFX_ByteString byte_string3("hellod"); + EXPECT_FALSE(byte_string_c == byte_string1); + EXPECT_FALSE(byte_string_c == byte_string2); + EXPECT_FALSE(byte_string_c == byte_string3); + EXPECT_FALSE(byte_string1 == byte_string_c); + EXPECT_FALSE(byte_string2 == byte_string_c); + EXPECT_FALSE(byte_string3 == byte_string_c); + + const char* c_string_same1 = "hello"; + EXPECT_TRUE(byte_string_c == c_string_same1); + EXPECT_TRUE(c_string_same1 == byte_string_c); + + const char* c_string1 = "he"; + const char* c_string2 = "hellp"; + const char* c_string3 = "hellod"; + EXPECT_FALSE(byte_string_c == c_string1); + EXPECT_FALSE(byte_string_c == c_string2); + EXPECT_FALSE(byte_string_c == c_string3); + + EXPECT_FALSE(c_string1 == byte_string_c); + EXPECT_FALSE(c_string2 == byte_string_c); + EXPECT_FALSE(c_string3 == byte_string_c); +} + +TEST(fxcrt, ByteStringCOperatorNE) { + CFX_ByteStringC byte_string_c("hello"); + EXPECT_FALSE(byte_string_c != byte_string_c); + + CFX_ByteStringC byte_string_c_same1("hello"); + EXPECT_FALSE(byte_string_c != byte_string_c_same1); + EXPECT_FALSE(byte_string_c_same1 != byte_string_c); + + CFX_ByteStringC byte_string_c_same2(byte_string_c); + EXPECT_FALSE(byte_string_c != byte_string_c_same2); + EXPECT_FALSE(byte_string_c_same2 != byte_string_c); + + CFX_ByteStringC byte_string_c1("he"); + CFX_ByteStringC byte_string_c2("hellp"); + CFX_ByteStringC byte_string_c3("hellod"); + EXPECT_TRUE(byte_string_c != byte_string_c1); + EXPECT_TRUE(byte_string_c != byte_string_c2); + EXPECT_TRUE(byte_string_c != byte_string_c3); + EXPECT_TRUE(byte_string_c1 != byte_string_c); + EXPECT_TRUE(byte_string_c2 != byte_string_c); + EXPECT_TRUE(byte_string_c3 != byte_string_c); + + CFX_ByteString byte_string_same1("hello"); + EXPECT_FALSE(byte_string_c != byte_string_same1); + EXPECT_FALSE(byte_string_same1 != byte_string_c); + + CFX_ByteString byte_string1("he"); + CFX_ByteString byte_string2("hellp"); + CFX_ByteString byte_string3("hellod"); + EXPECT_TRUE(byte_string_c != byte_string1); + EXPECT_TRUE(byte_string_c != byte_string2); + EXPECT_TRUE(byte_string_c != byte_string3); + EXPECT_TRUE(byte_string1 != byte_string_c); + EXPECT_TRUE(byte_string2 != byte_string_c); + EXPECT_TRUE(byte_string3 != byte_string_c); + + const char* c_string_same1 = "hello"; + EXPECT_FALSE(byte_string_c != c_string_same1); + EXPECT_FALSE(c_string_same1 != byte_string_c); + + const char* c_string1 = "he"; + const char* c_string2 = "hellp"; + const char* c_string3 = "hellod"; + EXPECT_TRUE(byte_string_c != c_string1); + EXPECT_TRUE(byte_string_c != c_string2); + EXPECT_TRUE(byte_string_c != c_string3); + + EXPECT_TRUE(c_string1 != byte_string_c); + EXPECT_TRUE(c_string2 != byte_string_c); + EXPECT_TRUE(c_string3 != byte_string_c); +} + +TEST(fxcrt, ByteStringFormatWidth) { + { + CFX_ByteString str; + str.Format("%5d", 1); + EXPECT_EQ(" 1", str); + } + + { + CFX_ByteString str; + str.Format("%d", 1); + EXPECT_EQ("1", str); + } + + { + CFX_ByteString str; + str.Format("%*d", 5, 1); + EXPECT_EQ(" 1", str); + } + + { + CFX_ByteString str; + str.Format("%-1d", 1); + EXPECT_EQ("1", str); + } + + { + CFX_ByteString str; + str.Format("%0d", 1); + EXPECT_EQ("1", str); + } +} + +TEST(fxcrt, ByteStringFormatPrecision) { + { + CFX_ByteString str; + str.Format("%.2f", 1.12345); + EXPECT_EQ("1.12", str); + } + + { + CFX_ByteString str; + str.Format("%.*f", 3, 1.12345); + EXPECT_EQ("1.123", str); + } + + { + CFX_ByteString str; + str.Format("%f", 1.12345); + EXPECT_EQ("1.123450", str); + } + + { + CFX_ByteString str; + str.Format("%-1f", 1.12345); + EXPECT_EQ("1.123450", str); + } + + { + CFX_ByteString str; + str.Format("%0f", 1.12345); + EXPECT_EQ("1.123450", str); + } +} + +TEST(fxcrt, EmptyByteString) { + CFX_ByteString empty_str; + EXPECT_TRUE(empty_str.IsEmpty()); + EXPECT_EQ(0, empty_str.GetLength()); + const char* cstr = empty_str.c_str(); + EXPECT_EQ(0, FXSYS_strlen(cstr)); +} diff --git a/core/fxcrt/cfx_string_c_template.h b/core/fxcrt/cfx_string_c_template.h index 3e3972ca92..b52817d790 100644 --- a/core/fxcrt/cfx_string_c_template.h +++ b/core/fxcrt/cfx_string_c_template.h @@ -167,4 +167,7 @@ inline bool operator!=(const T* lhs, const CFX_StringCTemplate& rhs) { extern template class CFX_StringCTemplate; extern template class CFX_StringCTemplate; +using CFX_ByteStringC = CFX_StringCTemplate; +using CFX_WideStringC = CFX_StringCTemplate; + #endif // CORE_FXCRT_CFX_STRING_C_TEMPLATE_H_ diff --git a/core/fxcrt/cfx_widestring.cpp b/core/fxcrt/cfx_widestring.cpp new file mode 100644 index 0000000000..49a62da9a7 --- /dev/null +++ b/core/fxcrt/cfx_widestring.cpp @@ -0,0 +1,1044 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/fxcrt/cfx_widestring.h" + +#include + +#include +#include + +#include "core/fxcrt/cfx_string_pool_template.h" +#include "core/fxcrt/fx_basic.h" +#include "core/fxcrt/fx_ext.h" +#include "third_party/base/numerics/safe_math.h" +#include "third_party/base/stl_util.h" + +template class CFX_StringDataTemplate; +template class CFX_StringCTemplate; +template class CFX_StringPoolTemplate; +template struct std::hash; + +#define FORCE_ANSI 0x10000 +#define FORCE_UNICODE 0x20000 +#define FORCE_INT64 0x40000 + +namespace { + +#ifndef NDEBUG +bool IsValidCodePage(uint16_t codepage) { + switch (codepage) { + case 0: + case 932: + case 936: + case 949: + case 950: + return true; + + default: + return false; + } +} +#endif + +const wchar_t* FX_wcsstr(const wchar_t* haystack, + int haystack_len, + const wchar_t* needle, + int needle_len) { + if (needle_len > haystack_len || needle_len == 0) { + return nullptr; + } + const wchar_t* end_ptr = haystack + haystack_len - needle_len; + while (haystack <= end_ptr) { + int i = 0; + while (1) { + if (haystack[i] != needle[i]) { + break; + } + i++; + if (i == needle_len) { + return haystack; + } + } + haystack++; + } + return nullptr; +} + +FX_STRSIZE GuessSizeForVSWPrintf(const wchar_t* pFormat, va_list argList) { + FX_STRSIZE nMaxLen = 0; + for (const wchar_t* pStr = pFormat; *pStr != 0; pStr++) { + if (*pStr != '%' || *(pStr = pStr + 1) == '%') { + ++nMaxLen; + continue; + } + int nItemLen = 0; + int nWidth = 0; + for (; *pStr != 0; pStr++) { + if (*pStr == '#') { + nMaxLen += 2; + } else if (*pStr == '*') { + nWidth = va_arg(argList, int); + } else if (*pStr != '-' && *pStr != '+' && *pStr != '0' && *pStr != ' ') { + break; + } + } + if (nWidth == 0) { + nWidth = FXSYS_wtoi(pStr); + while (std::iswdigit(*pStr)) + ++pStr; + } + if (nWidth < 0 || nWidth > 128 * 1024) + return -1; + int nPrecision = 0; + if (*pStr == '.') { + pStr++; + if (*pStr == '*') { + nPrecision = va_arg(argList, int); + pStr++; + } else { + nPrecision = FXSYS_wtoi(pStr); + while (std::iswdigit(*pStr)) + ++pStr; + } + } + if (nPrecision < 0 || nPrecision > 128 * 1024) + return -1; + int nModifier = 0; + if (*pStr == L'I' && *(pStr + 1) == L'6' && *(pStr + 2) == L'4') { + pStr += 3; + nModifier = FORCE_INT64; + } else { + switch (*pStr) { + case 'h': + nModifier = FORCE_ANSI; + pStr++; + break; + case 'l': + nModifier = FORCE_UNICODE; + pStr++; + break; + case 'F': + case 'N': + case 'L': + pStr++; + break; + } + } + switch (*pStr | nModifier) { + case 'c': + case 'C': + nItemLen = 2; + va_arg(argList, int); + break; + case 'c' | FORCE_ANSI: + case 'C' | FORCE_ANSI: + nItemLen = 2; + va_arg(argList, int); + break; + case 'c' | FORCE_UNICODE: + case 'C' | FORCE_UNICODE: + nItemLen = 2; + va_arg(argList, int); + break; + case 's': { + const wchar_t* pstrNextArg = va_arg(argList, const wchar_t*); + if (pstrNextArg) { + nItemLen = FXSYS_wcslen(pstrNextArg); + if (nItemLen < 1) { + nItemLen = 1; + } + } else { + nItemLen = 6; + } + } break; + case 'S': { + const char* pstrNextArg = va_arg(argList, const char*); + if (pstrNextArg) { + nItemLen = FXSYS_strlen(pstrNextArg); + if (nItemLen < 1) { + nItemLen = 1; + } + } else { + nItemLen = 6; + } + } break; + case 's' | FORCE_ANSI: + case 'S' | FORCE_ANSI: { + const char* pstrNextArg = va_arg(argList, const char*); + if (pstrNextArg) { + nItemLen = FXSYS_strlen(pstrNextArg); + if (nItemLen < 1) { + nItemLen = 1; + } + } else { + nItemLen = 6; + } + } break; + case 's' | FORCE_UNICODE: + case 'S' | FORCE_UNICODE: { + const wchar_t* pstrNextArg = va_arg(argList, wchar_t*); + if (pstrNextArg) { + nItemLen = FXSYS_wcslen(pstrNextArg); + if (nItemLen < 1) { + nItemLen = 1; + } + } else { + nItemLen = 6; + } + } break; + } + if (nItemLen != 0) { + if (nPrecision != 0 && nItemLen > nPrecision) { + nItemLen = nPrecision; + } + if (nItemLen < nWidth) { + nItemLen = nWidth; + } + } else { + switch (*pStr) { + case 'd': + case 'i': + case 'u': + case 'x': + case 'X': + case 'o': + if (nModifier & FORCE_INT64) { + va_arg(argList, int64_t); + } else { + va_arg(argList, int); + } + nItemLen = 32; + if (nItemLen < nWidth + nPrecision) { + nItemLen = nWidth + nPrecision; + } + break; + case 'a': + case 'A': + case 'e': + case 'E': + case 'g': + case 'G': + va_arg(argList, double); + nItemLen = 128; + if (nItemLen < nWidth + nPrecision) { + nItemLen = nWidth + nPrecision; + } + break; + case 'f': + if (nWidth + nPrecision > 100) { + nItemLen = nPrecision + nWidth + 128; + } else { + double f; + char pszTemp[256]; + f = va_arg(argList, double); + FXSYS_snprintf(pszTemp, sizeof(pszTemp), "%*.*f", nWidth, + nPrecision + 6, f); + nItemLen = FXSYS_strlen(pszTemp); + } + break; + case 'p': + va_arg(argList, void*); + nItemLen = 32; + if (nItemLen < nWidth + nPrecision) { + nItemLen = nWidth + nPrecision; + } + break; + case 'n': + va_arg(argList, int*); + break; + } + } + nMaxLen += nItemLen; + } + nMaxLen += 32; // Fudge factor. + return nMaxLen; +} + +} // namespace + +static_assert(sizeof(CFX_WideString) <= sizeof(wchar_t*), + "Strings must not require more space than pointers"); + +CFX_WideString::CFX_WideString() {} + +CFX_WideString::CFX_WideString(const CFX_WideString& other) + : m_pData(other.m_pData) {} + +CFX_WideString::CFX_WideString(CFX_WideString&& other) noexcept { + m_pData.Swap(other.m_pData); +} + +CFX_WideString::CFX_WideString(const wchar_t* pStr, FX_STRSIZE nLen) { + if (nLen < 0) + nLen = pStr ? FXSYS_wcslen(pStr) : 0; + + if (nLen) + m_pData.Reset(StringData::Create(pStr, nLen)); +} + +CFX_WideString::CFX_WideString(wchar_t ch) { + m_pData.Reset(StringData::Create(1)); + m_pData->m_String[0] = ch; +} + +CFX_WideString::CFX_WideString(const wchar_t* ptr) + : CFX_WideString(ptr, ptr ? FXSYS_wcslen(ptr) : 0) {} + +CFX_WideString::CFX_WideString(const CFX_WideStringC& stringSrc) { + if (!stringSrc.IsEmpty()) { + m_pData.Reset(StringData::Create(stringSrc.c_str(), stringSrc.GetLength())); + } +} + +CFX_WideString::CFX_WideString(const CFX_WideStringC& str1, + const CFX_WideStringC& str2) { + int nNewLen = str1.GetLength() + str2.GetLength(); + if (nNewLen == 0) + return; + + m_pData.Reset(StringData::Create(nNewLen)); + m_pData->CopyContents(str1.c_str(), str1.GetLength()); + m_pData->CopyContentsAt(str1.GetLength(), str2.c_str(), str2.GetLength()); +} + +CFX_WideString::~CFX_WideString() {} + +const CFX_WideString& CFX_WideString::operator=(const wchar_t* pStr) { + if (!pStr || !pStr[0]) + clear(); + else + AssignCopy(pStr, FXSYS_wcslen(pStr)); + + return *this; +} + +const CFX_WideString& CFX_WideString::operator=( + const CFX_WideStringC& stringSrc) { + if (stringSrc.IsEmpty()) + clear(); + else + AssignCopy(stringSrc.c_str(), stringSrc.GetLength()); + + return *this; +} + +const CFX_WideString& CFX_WideString::operator=( + const CFX_WideString& stringSrc) { + if (m_pData != stringSrc.m_pData) + m_pData = stringSrc.m_pData; + + return *this; +} + +const CFX_WideString& CFX_WideString::operator+=(const wchar_t* pStr) { + if (pStr) + Concat(pStr, FXSYS_wcslen(pStr)); + + return *this; +} + +const CFX_WideString& CFX_WideString::operator+=(wchar_t ch) { + Concat(&ch, 1); + return *this; +} + +const CFX_WideString& CFX_WideString::operator+=(const CFX_WideString& str) { + if (str.m_pData) + Concat(str.m_pData->m_String, str.m_pData->m_nDataLength); + + return *this; +} + +const CFX_WideString& CFX_WideString::operator+=(const CFX_WideStringC& str) { + if (!str.IsEmpty()) + Concat(str.c_str(), str.GetLength()); + + return *this; +} + +bool CFX_WideString::operator==(const wchar_t* ptr) const { + if (!m_pData) + return !ptr || !ptr[0]; + + if (!ptr) + return m_pData->m_nDataLength == 0; + + return wcslen(ptr) == static_cast(m_pData->m_nDataLength) && + wmemcmp(ptr, m_pData->m_String, m_pData->m_nDataLength) == 0; +} + +bool CFX_WideString::operator==(const CFX_WideStringC& str) const { + if (!m_pData) + return str.IsEmpty(); + + return m_pData->m_nDataLength == str.GetLength() && + wmemcmp(m_pData->m_String, str.c_str(), str.GetLength()) == 0; +} + +bool CFX_WideString::operator==(const CFX_WideString& other) const { + if (m_pData == other.m_pData) + return true; + + if (IsEmpty()) + return other.IsEmpty(); + + if (other.IsEmpty()) + return false; + + return other.m_pData->m_nDataLength == m_pData->m_nDataLength && + wmemcmp(other.m_pData->m_String, m_pData->m_String, + m_pData->m_nDataLength) == 0; +} + +bool CFX_WideString::operator<(const CFX_WideString& str) const { + if (m_pData == str.m_pData) + return false; + + int result = + wmemcmp(c_str(), str.c_str(), std::min(GetLength(), str.GetLength())); + return result < 0 || (result == 0 && GetLength() < str.GetLength()); +} + +void CFX_WideString::AssignCopy(const wchar_t* pSrcData, FX_STRSIZE nSrcLen) { + AllocBeforeWrite(nSrcLen); + m_pData->CopyContents(pSrcData, nSrcLen); + m_pData->m_nDataLength = nSrcLen; +} + +void CFX_WideString::ReallocBeforeWrite(FX_STRSIZE nNewLength) { + if (m_pData && m_pData->CanOperateInPlace(nNewLength)) + return; + + if (nNewLength <= 0) { + clear(); + return; + } + + CFX_RetainPtr pNewData(StringData::Create(nNewLength)); + if (m_pData) { + FX_STRSIZE nCopyLength = std::min(m_pData->m_nDataLength, nNewLength); + pNewData->CopyContents(m_pData->m_String, nCopyLength); + pNewData->m_nDataLength = nCopyLength; + } else { + pNewData->m_nDataLength = 0; + } + pNewData->m_String[pNewData->m_nDataLength] = 0; + m_pData.Swap(pNewData); +} + +void CFX_WideString::AllocBeforeWrite(FX_STRSIZE nNewLength) { + if (m_pData && m_pData->CanOperateInPlace(nNewLength)) + return; + + if (nNewLength <= 0) { + clear(); + return; + } + + m_pData.Reset(StringData::Create(nNewLength)); +} + +void CFX_WideString::ReleaseBuffer(FX_STRSIZE nNewLength) { + if (!m_pData) + return; + + if (nNewLength == -1) + nNewLength = FXSYS_wcslen(m_pData->m_String); + + nNewLength = std::min(nNewLength, m_pData->m_nAllocLength); + if (nNewLength == 0) { + clear(); + return; + } + + ASSERT(m_pData->m_nRefs == 1); + m_pData->m_nDataLength = nNewLength; + m_pData->m_String[nNewLength] = 0; + if (m_pData->m_nAllocLength - nNewLength >= 32) { + // Over arbitrary threshold, so pay the price to relocate. Force copy to + // always occur by holding a second reference to the string. + CFX_WideString preserve(*this); + ReallocBeforeWrite(nNewLength); + } +} + +void CFX_WideString::Reserve(FX_STRSIZE len) { + GetBuffer(len); +} + +wchar_t* CFX_WideString::GetBuffer(FX_STRSIZE nMinBufLength) { + if (!m_pData) { + if (nMinBufLength == 0) + return nullptr; + + m_pData.Reset(StringData::Create(nMinBufLength)); + m_pData->m_nDataLength = 0; + m_pData->m_String[0] = 0; + return m_pData->m_String; + } + + if (m_pData->CanOperateInPlace(nMinBufLength)) + return m_pData->m_String; + + nMinBufLength = std::max(nMinBufLength, m_pData->m_nDataLength); + if (nMinBufLength == 0) + return nullptr; + + CFX_RetainPtr pNewData(StringData::Create(nMinBufLength)); + pNewData->CopyContents(*m_pData); + pNewData->m_nDataLength = m_pData->m_nDataLength; + m_pData.Swap(pNewData); + return m_pData->m_String; +} + +FX_STRSIZE CFX_WideString::Delete(FX_STRSIZE nIndex, FX_STRSIZE nCount) { + if (!m_pData) + return 0; + + if (nIndex < 0) + nIndex = 0; + + FX_STRSIZE nOldLength = m_pData->m_nDataLength; + if (nCount > 0 && nIndex < nOldLength) { + FX_STRSIZE mLength = nIndex + nCount; + if (mLength >= nOldLength) { + m_pData->m_nDataLength = nIndex; + return m_pData->m_nDataLength; + } + ReallocBeforeWrite(nOldLength); + int nCharsToCopy = nOldLength - mLength + 1; + wmemmove(m_pData->m_String + nIndex, m_pData->m_String + mLength, + nCharsToCopy); + m_pData->m_nDataLength = nOldLength - nCount; + } + return m_pData->m_nDataLength; +} + +void CFX_WideString::Concat(const wchar_t* pSrcData, FX_STRSIZE nSrcLen) { + if (!pSrcData || nSrcLen <= 0) + return; + + if (!m_pData) { + m_pData.Reset(StringData::Create(pSrcData, nSrcLen)); + return; + } + + if (m_pData->CanOperateInPlace(m_pData->m_nDataLength + nSrcLen)) { + m_pData->CopyContentsAt(m_pData->m_nDataLength, pSrcData, nSrcLen); + m_pData->m_nDataLength += nSrcLen; + return; + } + + CFX_RetainPtr pNewData( + StringData::Create(m_pData->m_nDataLength + nSrcLen)); + pNewData->CopyContents(*m_pData); + pNewData->CopyContentsAt(m_pData->m_nDataLength, pSrcData, nSrcLen); + m_pData.Swap(pNewData); +} + +CFX_ByteString CFX_WideString::UTF8Encode() const { + return FX_UTF8Encode(AsStringC()); +} + +CFX_ByteString CFX_WideString::UTF16LE_Encode() const { + if (!m_pData) { + return CFX_ByteString("\0\0", 2); + } + int len = m_pData->m_nDataLength; + CFX_ByteString result; + char* buffer = result.GetBuffer(len * 2 + 2); + for (int i = 0; i < len; i++) { + buffer[i * 2] = m_pData->m_String[i] & 0xff; + buffer[i * 2 + 1] = m_pData->m_String[i] >> 8; + } + buffer[len * 2] = 0; + buffer[len * 2 + 1] = 0; + result.ReleaseBuffer(len * 2 + 2); + return result; +} + +CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst) const { + if (!m_pData) + return CFX_WideString(); + + return Mid(nFirst, m_pData->m_nDataLength - nFirst); +} + +CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const { + if (!m_pData) + return CFX_WideString(); + + nFirst = pdfium::clamp(nFirst, 0, m_pData->m_nDataLength); + nCount = pdfium::clamp(nCount, 0, m_pData->m_nDataLength - nFirst); + if (nCount == 0) + return CFX_WideString(); + + if (nFirst == 0 && nCount == m_pData->m_nDataLength) + return *this; + + CFX_WideString dest; + AllocCopy(dest, nCount, nFirst); + return dest; +} + +void CFX_WideString::AllocCopy(CFX_WideString& dest, + FX_STRSIZE nCopyLen, + FX_STRSIZE nCopyIndex) const { + if (nCopyLen <= 0) + return; + + CFX_RetainPtr pNewData( + StringData::Create(m_pData->m_String + nCopyIndex, nCopyLen)); + dest.m_pData.Swap(pNewData); +} + +bool CFX_WideString::TryVSWPrintf(FX_STRSIZE size, + const wchar_t* pFormat, + va_list argList) { + GetBuffer(size); + if (!m_pData) + return true; + + // In the following two calls, there's always space in the buffer for + // a terminating NUL that's not included in nMaxLen. + // For vswprintf(), MSAN won't untaint the buffer on a truncated write's + // -1 return code even though the buffer is written. Probably just as well + // not to trust the vendor's implementation to write anything anyways. + // See https://crbug.com/705912. + memset(m_pData->m_String, 0, (size + 1) * sizeof(wchar_t)); + int ret = vswprintf(m_pData->m_String, size + 1, pFormat, argList); + ReleaseBuffer(); + return ret >= 0 || m_pData->m_String[size - 1] == 0; +} + +void CFX_WideString::FormatV(const wchar_t* pFormat, va_list argList) { + va_list argListCopy; + FX_VA_COPY(argListCopy, argList); + FX_STRSIZE nMaxLen = vswprintf(nullptr, 0, pFormat, argListCopy); + va_end(argListCopy); + if (nMaxLen <= 0) { + nMaxLen = GuessSizeForVSWPrintf(pFormat, argListCopy); + if (nMaxLen <= 0) + return; + } + while (nMaxLen < 32 * 1024) { + FX_VA_COPY(argListCopy, argList); + bool bRetryPointless = TryVSWPrintf(nMaxLen, pFormat, argListCopy); + va_end(argListCopy); + if (bRetryPointless) + break; + nMaxLen *= 2; + } +} + +void CFX_WideString::Format(const wchar_t* pFormat, ...) { + va_list argList; + va_start(argList, pFormat); + FormatV(pFormat, argList); + va_end(argList); +} + +FX_STRSIZE CFX_WideString::Insert(FX_STRSIZE nIndex, wchar_t ch) { + FX_STRSIZE nNewLength = m_pData ? m_pData->m_nDataLength : 0; + nIndex = std::max(nIndex, 0); + nIndex = std::min(nIndex, nNewLength); + nNewLength++; + + ReallocBeforeWrite(nNewLength); + wmemmove(m_pData->m_String + nIndex + 1, m_pData->m_String + nIndex, + nNewLength - nIndex); + m_pData->m_String[nIndex] = ch; + m_pData->m_nDataLength = nNewLength; + return nNewLength; +} + +CFX_WideString CFX_WideString::Right(FX_STRSIZE nCount) const { + if (!m_pData) + return CFX_WideString(); + + nCount = std::max(nCount, 0); + if (nCount >= m_pData->m_nDataLength) + return *this; + + CFX_WideString dest; + AllocCopy(dest, nCount, m_pData->m_nDataLength - nCount); + return dest; +} + +CFX_WideString CFX_WideString::Left(FX_STRSIZE nCount) const { + if (!m_pData) + return CFX_WideString(); + + nCount = std::max(nCount, 0); + if (nCount >= m_pData->m_nDataLength) + return *this; + + CFX_WideString dest; + AllocCopy(dest, nCount, 0); + return dest; +} + +FX_STRSIZE CFX_WideString::Find(wchar_t ch, FX_STRSIZE nStart) const { + if (!m_pData) + return -1; + + if (nStart < 0 || nStart >= m_pData->m_nDataLength) + return -1; + + const wchar_t* pStr = + wmemchr(m_pData->m_String + nStart, ch, m_pData->m_nDataLength - nStart); + return pStr ? pStr - m_pData->m_String : -1; +} + +FX_STRSIZE CFX_WideString::Find(const CFX_WideStringC& pSub, + FX_STRSIZE nStart) const { + if (!m_pData) + return -1; + + FX_STRSIZE nLength = m_pData->m_nDataLength; + if (nStart > nLength) + return -1; + + const wchar_t* pStr = + FX_wcsstr(m_pData->m_String + nStart, m_pData->m_nDataLength - nStart, + pSub.c_str(), pSub.GetLength()); + return pStr ? (int)(pStr - m_pData->m_String) : -1; +} + +void CFX_WideString::MakeLower() { + if (!m_pData) + return; + + ReallocBeforeWrite(m_pData->m_nDataLength); + FXSYS_wcslwr(m_pData->m_String); +} + +void CFX_WideString::MakeUpper() { + if (!m_pData) + return; + + ReallocBeforeWrite(m_pData->m_nDataLength); + FXSYS_wcsupr(m_pData->m_String); +} + +FX_STRSIZE CFX_WideString::Remove(wchar_t chRemove) { + if (!m_pData || m_pData->m_nDataLength < 1) + return 0; + + wchar_t* pstrSource = m_pData->m_String; + wchar_t* pstrEnd = m_pData->m_String + m_pData->m_nDataLength; + while (pstrSource < pstrEnd) { + if (*pstrSource == chRemove) + break; + pstrSource++; + } + if (pstrSource == pstrEnd) + return 0; + + ptrdiff_t copied = pstrSource - m_pData->m_String; + ReallocBeforeWrite(m_pData->m_nDataLength); + pstrSource = m_pData->m_String + copied; + pstrEnd = m_pData->m_String + m_pData->m_nDataLength; + + wchar_t* pstrDest = pstrSource; + while (pstrSource < pstrEnd) { + if (*pstrSource != chRemove) { + *pstrDest = *pstrSource; + pstrDest++; + } + pstrSource++; + } + + *pstrDest = 0; + FX_STRSIZE nCount = (FX_STRSIZE)(pstrSource - pstrDest); + m_pData->m_nDataLength -= nCount; + return nCount; +} + +FX_STRSIZE CFX_WideString::Replace(const CFX_WideStringC& pOld, + const CFX_WideStringC& pNew) { + if (!m_pData || pOld.IsEmpty()) + return 0; + + FX_STRSIZE nSourceLen = pOld.GetLength(); + FX_STRSIZE nReplacementLen = pNew.GetLength(); + FX_STRSIZE nCount = 0; + const wchar_t* pStart = m_pData->m_String; + wchar_t* pEnd = m_pData->m_String + m_pData->m_nDataLength; + while (1) { + const wchar_t* pTarget = FX_wcsstr(pStart, (FX_STRSIZE)(pEnd - pStart), + pOld.c_str(), nSourceLen); + if (!pTarget) + break; + + nCount++; + pStart = pTarget + nSourceLen; + } + if (nCount == 0) + return 0; + + FX_STRSIZE nNewLength = + m_pData->m_nDataLength + (nReplacementLen - nSourceLen) * nCount; + + if (nNewLength == 0) { + clear(); + return nCount; + } + + CFX_RetainPtr pNewData(StringData::Create(nNewLength)); + pStart = m_pData->m_String; + wchar_t* pDest = pNewData->m_String; + for (FX_STRSIZE i = 0; i < nCount; i++) { + const wchar_t* pTarget = FX_wcsstr(pStart, (FX_STRSIZE)(pEnd - pStart), + pOld.c_str(), nSourceLen); + wmemcpy(pDest, pStart, pTarget - pStart); + pDest += pTarget - pStart; + wmemcpy(pDest, pNew.c_str(), pNew.GetLength()); + pDest += pNew.GetLength(); + pStart = pTarget + nSourceLen; + } + wmemcpy(pDest, pStart, pEnd - pStart); + m_pData.Swap(pNewData); + return nCount; +} + +void CFX_WideString::SetAt(FX_STRSIZE nIndex, wchar_t ch) { + if (!m_pData) { + return; + } + ASSERT(nIndex >= 0); + ASSERT(nIndex < m_pData->m_nDataLength); + ReallocBeforeWrite(m_pData->m_nDataLength); + m_pData->m_String[nIndex] = ch; +} + +// static +CFX_WideString CFX_WideString::FromLocal(const CFX_ByteStringC& str) { + return FromCodePage(str, 0); +} + +// static +CFX_WideString CFX_WideString::FromCodePage(const CFX_ByteStringC& str, + uint16_t codepage) { + return CFX_CharMap::GetWideString(codepage, str); +} + +// static +CFX_WideString CFX_WideString::FromUTF8(const CFX_ByteStringC& str) { + if (str.IsEmpty()) + return CFX_WideString(); + + CFX_UTF8Decoder decoder; + for (FX_STRSIZE i = 0; i < str.GetLength(); i++) + decoder.Input(str[i]); + + return CFX_WideString(decoder.GetResult()); +} + +// static +CFX_WideString CFX_WideString::FromUTF16LE(const unsigned short* wstr, + FX_STRSIZE wlen) { + if (!wstr || 0 == wlen) { + return CFX_WideString(); + } + + CFX_WideString result; + wchar_t* buf = result.GetBuffer(wlen); + for (int i = 0; i < wlen; i++) { + buf[i] = wstr[i]; + } + result.ReleaseBuffer(wlen); + return result; +} + +int CFX_WideString::Compare(const wchar_t* lpsz) const { + if (m_pData) + return FXSYS_wcscmp(m_pData->m_String, lpsz); + return (!lpsz || lpsz[0] == 0) ? 0 : -1; +} + +int CFX_WideString::Compare(const CFX_WideString& str) const { + if (!m_pData) { + if (!str.m_pData) { + return 0; + } + return -1; + } + if (!str.m_pData) { + return 1; + } + int this_len = m_pData->m_nDataLength; + int that_len = str.m_pData->m_nDataLength; + int min_len = this_len < that_len ? this_len : that_len; + for (int i = 0; i < min_len; i++) { + if (m_pData->m_String[i] < str.m_pData->m_String[i]) { + return -1; + } + if (m_pData->m_String[i] > str.m_pData->m_String[i]) { + return 1; + } + } + if (this_len < that_len) { + return -1; + } + if (this_len > that_len) { + return 1; + } + return 0; +} + +int CFX_WideString::CompareNoCase(const wchar_t* lpsz) const { + if (!m_pData) { + return (!lpsz || lpsz[0] == 0) ? 0 : -1; + } + return FXSYS_wcsicmp(m_pData->m_String, lpsz); +} + +FX_STRSIZE CFX_WideString::WStringLength(const unsigned short* str) { + FX_STRSIZE len = 0; + if (str) + while (str[len]) + len++; + return len; +} + +void CFX_WideString::TrimRight(const CFX_WideStringC& pTargets) { + if (IsEmpty() || pTargets.IsEmpty()) + return; + + FX_STRSIZE pos = GetLength(); + while (pos && pTargets.Find(m_pData->m_String[pos - 1]) != -1) + pos--; + + if (pos < m_pData->m_nDataLength) { + ReallocBeforeWrite(m_pData->m_nDataLength); + m_pData->m_String[pos] = 0; + m_pData->m_nDataLength = pos; + } +} + +void CFX_WideString::TrimRight(wchar_t chTarget) { + wchar_t str[2] = {chTarget, 0}; + TrimRight(str); +} + +void CFX_WideString::TrimRight() { + TrimRight(L"\x09\x0a\x0b\x0c\x0d\x20"); +} + +void CFX_WideString::TrimLeft(const CFX_WideStringC& pTargets) { + if (!m_pData || pTargets.IsEmpty()) + return; + + FX_STRSIZE len = GetLength(); + if (len < 1) + return; + + FX_STRSIZE pos = 0; + while (pos < len) { + FX_STRSIZE i = 0; + while (i < pTargets.GetLength() && + pTargets.CharAt(i) != m_pData->m_String[pos]) { + i++; + } + if (i == pTargets.GetLength()) { + break; + } + pos++; + } + if (pos) { + ReallocBeforeWrite(len); + FX_STRSIZE nDataLength = len - pos; + FXSYS_memmove(m_pData->m_String, m_pData->m_String + pos, + (nDataLength + 1) * sizeof(wchar_t)); + m_pData->m_nDataLength = nDataLength; + } +} + +void CFX_WideString::TrimLeft(wchar_t chTarget) { + wchar_t str[2] = {chTarget, 0}; + TrimLeft(str); +} + +void CFX_WideString::TrimLeft() { + TrimLeft(L"\x09\x0a\x0b\x0c\x0d\x20"); +} +float FX_wtof(const wchar_t* str, int len) { + if (len == 0) { + return 0.0; + } + int cc = 0; + bool bNegative = false; + if (str[0] == '+') { + cc++; + } else if (str[0] == '-') { + bNegative = true; + cc++; + } + int integer = 0; + while (cc < len) { + if (str[cc] == '.') { + break; + } + integer = integer * 10 + FXSYS_toDecimalDigit(str[cc]); + cc++; + } + float fraction = 0; + if (str[cc] == '.') { + cc++; + float scale = 0.1f; + while (cc < len) { + fraction += scale * FXSYS_toDecimalDigit(str[cc]); + scale *= 0.1f; + cc++; + } + } + fraction += (float)integer; + return bNegative ? -fraction : fraction; +} + +int CFX_WideString::GetInteger() const { + return m_pData ? FXSYS_wtoi(m_pData->m_String) : 0; +} + +float CFX_WideString::GetFloat() const { + return m_pData ? FX_wtof(m_pData->m_String, m_pData->m_nDataLength) : 0.0f; +} + +// static +CFX_ByteString CFX_CharMap::GetByteString(uint16_t codepage, + const CFX_WideStringC& wstr) { + ASSERT(IsValidCodePage(codepage)); + int src_len = wstr.GetLength(); + int dest_len = FXSYS_WideCharToMultiByte(codepage, 0, wstr.c_str(), src_len, + nullptr, 0, nullptr, nullptr); + CFX_ByteString bstr; + if (dest_len) { + char* dest_buf = bstr.GetBuffer(dest_len); + FXSYS_WideCharToMultiByte(codepage, 0, wstr.c_str(), src_len, dest_buf, + dest_len, nullptr, nullptr); + bstr.ReleaseBuffer(dest_len); + } + return bstr; +} + +// static +CFX_WideString CFX_CharMap::GetWideString(uint16_t codepage, + const CFX_ByteStringC& bstr) { + ASSERT(IsValidCodePage(codepage)); + int src_len = bstr.GetLength(); + int dest_len = + FXSYS_MultiByteToWideChar(codepage, 0, bstr.c_str(), src_len, nullptr, 0); + CFX_WideString wstr; + if (dest_len) { + wchar_t* dest_buf = wstr.GetBuffer(dest_len); + FXSYS_MultiByteToWideChar(codepage, 0, bstr.c_str(), src_len, dest_buf, + dest_len); + wstr.ReleaseBuffer(dest_len); + } + return wstr; +} diff --git a/core/fxcrt/cfx_widestring.h b/core/fxcrt/cfx_widestring.h new file mode 100644 index 0000000000..397a5dcee2 --- /dev/null +++ b/core/fxcrt/cfx_widestring.h @@ -0,0 +1,231 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FXCRT_CFX_WIDESTRING_H_ +#define CORE_FXCRT_CFX_WIDESTRING_H_ + +#include + +#include "core/fxcrt/cfx_retain_ptr.h" +#include "core/fxcrt/cfx_string_c_template.h" +#include "core/fxcrt/cfx_string_data_template.h" +#include "core/fxcrt/fx_memory.h" +#include "core/fxcrt/fx_system.h" + +class CFX_ByteString; + +// A mutable string with shared buffers using copy-on-write semantics that +// avoids the cost of std::string's iterator stability guarantees. +class CFX_WideString { + public: + using CharType = wchar_t; + + CFX_WideString(); + CFX_WideString(const CFX_WideString& other); + CFX_WideString(CFX_WideString&& other) noexcept; + + // Deliberately implicit to avoid calling on every string literal. + // NOLINTNEXTLINE(runtime/explicit) + CFX_WideString(wchar_t ch); + // NOLINTNEXTLINE(runtime/explicit) + CFX_WideString(const wchar_t* ptr); + + CFX_WideString(const wchar_t* ptr, FX_STRSIZE len); + + explicit CFX_WideString(const CFX_WideStringC& str); + CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2); + + ~CFX_WideString(); + + static CFX_WideString FromLocal(const CFX_ByteStringC& str); + static CFX_WideString FromCodePage(const CFX_ByteStringC& str, + uint16_t codepage); + + static CFX_WideString FromUTF8(const CFX_ByteStringC& str); + static CFX_WideString FromUTF16LE(const unsigned short* str, FX_STRSIZE len); + + static FX_STRSIZE WStringLength(const unsigned short* str); + + // Explicit conversion to C-style wide string. + // Note: Any subsequent modification of |this| will invalidate the result. + const wchar_t* c_str() const { return m_pData ? m_pData->m_String : L""; } + + // Explicit conversion to CFX_WideStringC. + // Note: Any subsequent modification of |this| will invalidate the result. + CFX_WideStringC AsStringC() const { + return CFX_WideStringC(c_str(), GetLength()); + } + + void clear() { m_pData.Reset(); } + + FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } + bool IsEmpty() const { return !GetLength(); } + + const CFX_WideString& operator=(const wchar_t* str); + const CFX_WideString& operator=(const CFX_WideString& stringSrc); + const CFX_WideString& operator=(const CFX_WideStringC& stringSrc); + + const CFX_WideString& operator+=(const wchar_t* str); + const CFX_WideString& operator+=(wchar_t ch); + const CFX_WideString& operator+=(const CFX_WideString& str); + const CFX_WideString& operator+=(const CFX_WideStringC& str); + + bool operator==(const wchar_t* ptr) const; + bool operator==(const CFX_WideStringC& str) const; + bool operator==(const CFX_WideString& other) const; + + bool operator!=(const wchar_t* ptr) const { return !(*this == ptr); } + bool operator!=(const CFX_WideStringC& str) const { return !(*this == str); } + bool operator!=(const CFX_WideString& other) const { + return !(*this == other); + } + + bool operator<(const CFX_WideString& str) const; + + wchar_t GetAt(FX_STRSIZE nIndex) const { + return m_pData ? m_pData->m_String[nIndex] : 0; + } + + wchar_t operator[](FX_STRSIZE nIndex) const { + return m_pData ? m_pData->m_String[nIndex] : 0; + } + + void SetAt(FX_STRSIZE nIndex, wchar_t ch); + + int Compare(const wchar_t* str) const; + int Compare(const CFX_WideString& str) const; + int CompareNoCase(const wchar_t* str) const; + + CFX_WideString Mid(FX_STRSIZE first) const; + CFX_WideString Mid(FX_STRSIZE first, FX_STRSIZE count) const; + CFX_WideString Left(FX_STRSIZE count) const; + CFX_WideString Right(FX_STRSIZE count) const; + + FX_STRSIZE Insert(FX_STRSIZE index, wchar_t ch); + FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); + + void Format(const wchar_t* lpszFormat, ...); + void FormatV(const wchar_t* lpszFormat, va_list argList); + + void MakeLower(); + void MakeUpper(); + + void TrimRight(); + void TrimRight(wchar_t chTarget); + void TrimRight(const CFX_WideStringC& pTargets); + + void TrimLeft(); + void TrimLeft(wchar_t chTarget); + void TrimLeft(const CFX_WideStringC& pTargets); + + void Reserve(FX_STRSIZE len); + wchar_t* GetBuffer(FX_STRSIZE len); + void ReleaseBuffer(FX_STRSIZE len = -1); + + int GetInteger() const; + float GetFloat() const; + + FX_STRSIZE Find(const CFX_WideStringC& pSub, FX_STRSIZE start = 0) const; + FX_STRSIZE Find(wchar_t ch, FX_STRSIZE start = 0) const; + FX_STRSIZE Replace(const CFX_WideStringC& pOld, const CFX_WideStringC& pNew); + FX_STRSIZE Remove(wchar_t ch); + + CFX_ByteString UTF8Encode() const; + CFX_ByteString UTF16LE_Encode() const; + + protected: + using StringData = CFX_StringDataTemplate; + + void ReallocBeforeWrite(FX_STRSIZE nLen); + void AllocBeforeWrite(FX_STRSIZE nLen); + void AllocCopy(CFX_WideString& dest, + FX_STRSIZE nCopyLen, + FX_STRSIZE nCopyIndex) const; + void AssignCopy(const wchar_t* pSrcData, FX_STRSIZE nSrcLen); + void Concat(const wchar_t* lpszSrcData, FX_STRSIZE nSrcLen); + + // Returns true unless we ran out of space. + bool TryVSWPrintf(FX_STRSIZE size, const wchar_t* format, va_list argList); + + CFX_RetainPtr m_pData; + + friend class fxcrt_WideStringConcatInPlace_Test; + friend class fxcrt_WideStringPool_Test; +}; + +inline CFX_WideString operator+(const CFX_WideStringC& str1, + const CFX_WideStringC& str2) { + return CFX_WideString(str1, str2); +} +inline CFX_WideString operator+(const CFX_WideStringC& str1, + const wchar_t* str2) { + return CFX_WideString(str1, str2); +} +inline CFX_WideString operator+(const wchar_t* str1, + const CFX_WideStringC& str2) { + return CFX_WideString(str1, str2); +} +inline CFX_WideString operator+(const CFX_WideStringC& str1, wchar_t ch) { + return CFX_WideString(str1, CFX_WideStringC(ch)); +} +inline CFX_WideString operator+(wchar_t ch, const CFX_WideStringC& str2) { + return CFX_WideString(ch, str2); +} +inline CFX_WideString operator+(const CFX_WideString& str1, + const CFX_WideString& str2) { + return CFX_WideString(str1.AsStringC(), str2.AsStringC()); +} +inline CFX_WideString operator+(const CFX_WideString& str1, wchar_t ch) { + return CFX_WideString(str1.AsStringC(), CFX_WideStringC(ch)); +} +inline CFX_WideString operator+(wchar_t ch, const CFX_WideString& str2) { + return CFX_WideString(ch, str2.AsStringC()); +} +inline CFX_WideString operator+(const CFX_WideString& str1, + const wchar_t* str2) { + return CFX_WideString(str1.AsStringC(), str2); +} +inline CFX_WideString operator+(const wchar_t* str1, + const CFX_WideString& str2) { + return CFX_WideString(str1, str2.AsStringC()); +} +inline CFX_WideString operator+(const CFX_WideString& str1, + const CFX_WideStringC& str2) { + return CFX_WideString(str1.AsStringC(), str2); +} +inline CFX_WideString operator+(const CFX_WideStringC& str1, + const CFX_WideString& str2) { + return CFX_WideString(str1, str2.AsStringC()); +} +inline bool operator==(const wchar_t* lhs, const CFX_WideString& rhs) { + return rhs == lhs; +} +inline bool operator==(const CFX_WideStringC& lhs, const CFX_WideString& rhs) { + return rhs == lhs; +} +inline bool operator!=(const wchar_t* lhs, const CFX_WideString& rhs) { + return rhs != lhs; +} +inline bool operator!=(const CFX_WideStringC& lhs, const CFX_WideString& rhs) { + return rhs != lhs; +} + +uint32_t FX_HashCode_GetW(const CFX_WideStringC& str, bool bIgnoreCase); + +namespace std { + +template <> +struct hash { + std::size_t operator()(const CFX_WideString& str) const { + return FX_HashCode_GetW(str.AsStringC(), false); + } +}; + +} // namespace std + +extern template struct std::hash; + +#endif // CORE_FXCRT_CFX_WIDESTRING_H_ diff --git a/core/fxcrt/cfx_widestring_unittest.cpp b/core/fxcrt/cfx_widestring_unittest.cpp new file mode 100644 index 0000000000..c1728af1b1 --- /dev/null +++ b/core/fxcrt/cfx_widestring_unittest.cpp @@ -0,0 +1,942 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "core/fxcrt/cfx_widestring.h" + +#include "testing/fx_string_testhelpers.h" +#include "testing/gtest/include/gtest/gtest.h" + +TEST(fxcrt, WideStringOperatorSubscript) { + // CFX_WideString includes the NUL terminator for non-empty strings. + CFX_WideString abc(L"abc"); + EXPECT_EQ(L'a', abc[0]); + EXPECT_EQ(L'b', abc[1]); + EXPECT_EQ(L'c', abc[2]); + EXPECT_EQ(L'\0', abc[3]); +} + +TEST(fxcrt, WideStringOperatorLT) { + CFX_WideString empty; + CFX_WideString a(L"a"); + CFX_WideString abc(L"\x0110qq"); // Comes before despite endianness. + CFX_WideString def(L"\x1001qq"); // Comes after despite endianness. + + EXPECT_FALSE(empty < empty); + EXPECT_FALSE(a < a); + EXPECT_FALSE(abc < abc); + EXPECT_FALSE(def < def); + + EXPECT_TRUE(empty < a); + EXPECT_FALSE(a < empty); + + EXPECT_TRUE(empty < abc); + EXPECT_FALSE(abc < empty); + + EXPECT_TRUE(empty < def); + EXPECT_FALSE(def < empty); + + EXPECT_TRUE(a < abc); + EXPECT_FALSE(abc < a); + + EXPECT_TRUE(a < def); + EXPECT_FALSE(def < a); + + EXPECT_TRUE(abc < def); + EXPECT_FALSE(def < abc); +} + +TEST(fxcrt, WideStringOperatorEQ) { + CFX_WideString null_string; + EXPECT_TRUE(null_string == null_string); + + CFX_WideString empty_string(L""); + EXPECT_TRUE(empty_string == empty_string); + EXPECT_TRUE(empty_string == null_string); + EXPECT_TRUE(null_string == empty_string); + + CFX_WideString deleted_string(L"hello"); + deleted_string.Delete(0, 5); + EXPECT_TRUE(deleted_string == deleted_string); + EXPECT_TRUE(deleted_string == null_string); + EXPECT_TRUE(deleted_string == empty_string); + EXPECT_TRUE(null_string == deleted_string); + EXPECT_TRUE(null_string == empty_string); + + CFX_WideString wide_string(L"hello"); + EXPECT_TRUE(wide_string == wide_string); + EXPECT_FALSE(wide_string == null_string); + EXPECT_FALSE(wide_string == empty_string); + EXPECT_FALSE(wide_string == deleted_string); + EXPECT_FALSE(null_string == wide_string); + EXPECT_FALSE(empty_string == wide_string); + EXPECT_FALSE(deleted_string == wide_string); + + CFX_WideString wide_string_same1(L"hello"); + EXPECT_TRUE(wide_string == wide_string_same1); + EXPECT_TRUE(wide_string_same1 == wide_string); + + CFX_WideString wide_string_same2(wide_string); + EXPECT_TRUE(wide_string == wide_string_same2); + EXPECT_TRUE(wide_string_same2 == wide_string); + + CFX_WideString wide_string1(L"he"); + CFX_WideString wide_string2(L"hellp"); + CFX_WideString wide_string3(L"hellod"); + EXPECT_FALSE(wide_string == wide_string1); + EXPECT_FALSE(wide_string == wide_string2); + EXPECT_FALSE(wide_string == wide_string3); + EXPECT_FALSE(wide_string1 == wide_string); + EXPECT_FALSE(wide_string2 == wide_string); + EXPECT_FALSE(wide_string3 == wide_string); + + CFX_WideStringC null_string_c; + CFX_WideStringC empty_string_c(L""); + EXPECT_TRUE(null_string == null_string_c); + EXPECT_TRUE(null_string == empty_string_c); + EXPECT_TRUE(empty_string == null_string_c); + EXPECT_TRUE(empty_string == empty_string_c); + EXPECT_TRUE(deleted_string == null_string_c); + EXPECT_TRUE(deleted_string == empty_string_c); + EXPECT_TRUE(null_string_c == null_string); + EXPECT_TRUE(empty_string_c == null_string); + EXPECT_TRUE(null_string_c == empty_string); + EXPECT_TRUE(empty_string_c == empty_string); + EXPECT_TRUE(null_string_c == deleted_string); + EXPECT_TRUE(empty_string_c == deleted_string); + + CFX_WideStringC wide_string_c_same1(L"hello"); + EXPECT_TRUE(wide_string == wide_string_c_same1); + EXPECT_TRUE(wide_string_c_same1 == wide_string); + + CFX_WideStringC wide_string_c1(L"he"); + CFX_WideStringC wide_string_c2(L"hellp"); + CFX_WideStringC wide_string_c3(L"hellod"); + EXPECT_FALSE(wide_string == wide_string_c1); + EXPECT_FALSE(wide_string == wide_string_c2); + EXPECT_FALSE(wide_string == wide_string_c3); + EXPECT_FALSE(wide_string_c1 == wide_string); + EXPECT_FALSE(wide_string_c2 == wide_string); + EXPECT_FALSE(wide_string_c3 == wide_string); + + const wchar_t* c_null_string = nullptr; + const wchar_t* c_empty_string = L""; + EXPECT_TRUE(null_string == c_null_string); + EXPECT_TRUE(null_string == c_empty_string); + EXPECT_TRUE(empty_string == c_null_string); + EXPECT_TRUE(empty_string == c_empty_string); + EXPECT_TRUE(deleted_string == c_null_string); + EXPECT_TRUE(deleted_string == c_empty_string); + EXPECT_TRUE(c_null_string == null_string); + EXPECT_TRUE(c_empty_string == null_string); + EXPECT_TRUE(c_null_string == empty_string); + EXPECT_TRUE(c_empty_string == empty_string); + EXPECT_TRUE(c_null_string == deleted_string); + EXPECT_TRUE(c_empty_string == deleted_string); + + const wchar_t* c_string_same1 = L"hello"; + EXPECT_TRUE(wide_string == c_string_same1); + EXPECT_TRUE(c_string_same1 == wide_string); + + const wchar_t* c_string1 = L"he"; + const wchar_t* c_string2 = L"hellp"; + const wchar_t* c_string3 = L"hellod"; + EXPECT_FALSE(wide_string == c_string1); + EXPECT_FALSE(wide_string == c_string2); + EXPECT_FALSE(wide_string == c_string3); + EXPECT_FALSE(c_string1 == wide_string); + EXPECT_FALSE(c_string2 == wide_string); + EXPECT_FALSE(c_string3 == wide_string); +} + +TEST(fxcrt, WideStringOperatorNE) { + CFX_WideString null_string; + EXPECT_FALSE(null_string != null_string); + + CFX_WideString empty_string(L""); + EXPECT_FALSE(empty_string != empty_string); + EXPECT_FALSE(empty_string != null_string); + EXPECT_FALSE(null_string != empty_string); + + CFX_WideString deleted_string(L"hello"); + deleted_string.Delete(0, 5); + EXPECT_FALSE(deleted_string != deleted_string); + EXPECT_FALSE(deleted_string != null_string); + EXPECT_FALSE(deleted_string != empty_string); + EXPECT_FALSE(null_string != deleted_string); + EXPECT_FALSE(null_string != empty_string); + + CFX_WideString wide_string(L"hello"); + EXPECT_FALSE(wide_string != wide_string); + EXPECT_TRUE(wide_string != null_string); + EXPECT_TRUE(wide_string != empty_string); + EXPECT_TRUE(wide_string != deleted_string); + EXPECT_TRUE(null_string != wide_string); + EXPECT_TRUE(empty_string != wide_string); + EXPECT_TRUE(deleted_string != wide_string); + + CFX_WideString wide_string_same1(L"hello"); + EXPECT_FALSE(wide_string != wide_string_same1); + EXPECT_FALSE(wide_string_same1 != wide_string); + + CFX_WideString wide_string_same2(wide_string); + EXPECT_FALSE(wide_string != wide_string_same2); + EXPECT_FALSE(wide_string_same2 != wide_string); + + CFX_WideString wide_string1(L"he"); + CFX_WideString wide_string2(L"hellp"); + CFX_WideString wide_string3(L"hellod"); + EXPECT_TRUE(wide_string != wide_string1); + EXPECT_TRUE(wide_string != wide_string2); + EXPECT_TRUE(wide_string != wide_string3); + EXPECT_TRUE(wide_string1 != wide_string); + EXPECT_TRUE(wide_string2 != wide_string); + EXPECT_TRUE(wide_string3 != wide_string); + + CFX_WideStringC null_string_c; + CFX_WideStringC empty_string_c(L""); + EXPECT_FALSE(null_string != null_string_c); + EXPECT_FALSE(null_string != empty_string_c); + EXPECT_FALSE(empty_string != null_string_c); + EXPECT_FALSE(empty_string != empty_string_c); + EXPECT_FALSE(deleted_string != null_string_c); + EXPECT_FALSE(deleted_string != empty_string_c); + EXPECT_FALSE(null_string_c != null_string); + EXPECT_FALSE(empty_string_c != null_string); + EXPECT_FALSE(null_string_c != empty_string); + EXPECT_FALSE(empty_string_c != empty_string); + + CFX_WideStringC wide_string_c_same1(L"hello"); + EXPECT_FALSE(wide_string != wide_string_c_same1); + EXPECT_FALSE(wide_string_c_same1 != wide_string); + + CFX_WideStringC wide_string_c1(L"he"); + CFX_WideStringC wide_string_c2(L"hellp"); + CFX_WideStringC wide_string_c3(L"hellod"); + EXPECT_TRUE(wide_string != wide_string_c1); + EXPECT_TRUE(wide_string != wide_string_c2); + EXPECT_TRUE(wide_string != wide_string_c3); + EXPECT_TRUE(wide_string_c1 != wide_string); + EXPECT_TRUE(wide_string_c2 != wide_string); + EXPECT_TRUE(wide_string_c3 != wide_string); + + const wchar_t* c_null_string = nullptr; + const wchar_t* c_empty_string = L""; + EXPECT_FALSE(null_string != c_null_string); + EXPECT_FALSE(null_string != c_empty_string); + EXPECT_FALSE(empty_string != c_null_string); + EXPECT_FALSE(empty_string != c_empty_string); + EXPECT_FALSE(deleted_string != c_null_string); + EXPECT_FALSE(deleted_string != c_empty_string); + EXPECT_FALSE(c_null_string != null_string); + EXPECT_FALSE(c_empty_string != null_string); + EXPECT_FALSE(c_null_string != empty_string); + EXPECT_FALSE(c_empty_string != empty_string); + EXPECT_FALSE(c_null_string != deleted_string); + EXPECT_FALSE(c_empty_string != deleted_string); + + const wchar_t* c_string_same1 = L"hello"; + EXPECT_FALSE(wide_string != c_string_same1); + EXPECT_FALSE(c_string_same1 != wide_string); + + const wchar_t* c_string1 = L"he"; + const wchar_t* c_string2 = L"hellp"; + const wchar_t* c_string3 = L"hellod"; + EXPECT_TRUE(wide_string != c_string1); + EXPECT_TRUE(wide_string != c_string2); + EXPECT_TRUE(wide_string != c_string3); + EXPECT_TRUE(c_string1 != wide_string); + EXPECT_TRUE(c_string2 != wide_string); + EXPECT_TRUE(c_string3 != wide_string); +} + +TEST(fxcrt, WideStringConcatInPlace) { + CFX_WideString fred; + fred.Concat(L"FRED", 4); + EXPECT_EQ(L"FRED", fred); + + fred.Concat(L"DY", 2); + EXPECT_EQ(L"FREDDY", fred); + + fred.Delete(3, 3); + EXPECT_EQ(L"FRE", fred); + + fred.Concat(L"D", 1); + EXPECT_EQ(L"FRED", fred); + + CFX_WideString copy = fred; + fred.Concat(L"DY", 2); + EXPECT_EQ(L"FREDDY", fred); + EXPECT_EQ(L"FRED", copy); + + // Test invalid arguments. + copy = fred; + fred.Concat(L"freddy", -6); + CFX_WideString not_aliased(L"xxxxxx"); + EXPECT_EQ(L"FREDDY", fred); + EXPECT_EQ(L"xxxxxx", not_aliased); +} + +TEST(fxcrt, WideStringRemove) { + CFX_WideString freed(L"FREED"); + freed.Remove(L'E'); + EXPECT_EQ(L"FRD", freed); + freed.Remove(L'F'); + EXPECT_EQ(L"RD", freed); + freed.Remove(L'D'); + EXPECT_EQ(L"R", freed); + freed.Remove(L'X'); + EXPECT_EQ(L"R", freed); + freed.Remove(L'R'); + EXPECT_EQ(L"", freed); + + CFX_WideString empty; + empty.Remove(L'X'); + EXPECT_EQ(L"", empty); +} + +TEST(fxcrt, WideStringRemoveCopies) { + CFX_WideString freed(L"FREED"); + const wchar_t* old_buffer = freed.c_str(); + + // No change with single reference - no copy. + freed.Remove(L'Q'); + EXPECT_EQ(L"FREED", freed); + EXPECT_EQ(old_buffer, freed.c_str()); + + // Change with single reference - no copy. + freed.Remove(L'E'); + EXPECT_EQ(L"FRD", freed); + EXPECT_EQ(old_buffer, freed.c_str()); + + // No change with multiple references - no copy. + CFX_WideString shared(freed); + freed.Remove(L'Q'); + EXPECT_EQ(L"FRD", freed); + EXPECT_EQ(old_buffer, freed.c_str()); + EXPECT_EQ(old_buffer, shared.c_str()); + + // Change with multiple references -- must copy. + freed.Remove(L'D'); + EXPECT_EQ(L"FR", freed); + EXPECT_NE(old_buffer, freed.c_str()); + EXPECT_EQ(L"FRD", shared); + EXPECT_EQ(old_buffer, shared.c_str()); +} + +TEST(fxcrt, WideStringReplace) { + CFX_WideString fred(L"FRED"); + fred.Replace(L"FR", L"BL"); + EXPECT_EQ(L"BLED", fred); + fred.Replace(L"D", L"DDY"); + EXPECT_EQ(L"BLEDDY", fred); + fred.Replace(L"LEDD", L""); + EXPECT_EQ(L"BY", fred); + fred.Replace(L"X", L"CLAMS"); + EXPECT_EQ(L"BY", fred); + fred.Replace(L"BY", L"HI"); + EXPECT_EQ(L"HI", fred); + fred.Replace(L"", L"CLAMS"); + EXPECT_EQ(L"HI", fred); + fred.Replace(L"HI", L""); + EXPECT_EQ(L"", fred); +} + +TEST(fxcrt, WideStringInsert) { + CFX_WideString fred(L"FRED"); + fred.Insert(-1, 'X'); + EXPECT_EQ(L"XFRED", fred); + + fred.Insert(0, 'S'); + EXPECT_EQ(L"SXFRED", fred); + + fred.Insert(2, 'T'); + EXPECT_EQ(L"SXTFRED", fred); + + fred.Insert(5, 'U'); + EXPECT_EQ(L"SXTFRUED", fred); + + fred.Insert(8, 'V'); + EXPECT_EQ(L"SXTFRUEDV", fred); + + fred.Insert(12, 'P'); + EXPECT_EQ(L"SXTFRUEDVP", fred); + + { + CFX_WideString empty; + empty.Insert(-1, 'X'); + EXPECT_EQ(L"X", empty); + } + { + CFX_WideString empty; + empty.Insert(0, 'X'); + EXPECT_EQ(L"X", empty); + } + { + CFX_WideString empty; + empty.Insert(5, 'X'); + EXPECT_EQ(L"X", empty); + } +} + +TEST(fxcrt, WideStringDelete) { + CFX_WideString fred(L"FRED"); + fred.Delete(0, 2); + EXPECT_EQ(L"ED", fred); + fred.Delete(1); + EXPECT_EQ(L"E", fred); + fred.Delete(-1); + EXPECT_EQ(L"", fred); + fred.Delete(1); + EXPECT_EQ(L"", fred); + + CFX_WideString empty; + empty.Delete(0); + EXPECT_EQ(L"", empty); + empty.Delete(-1); + EXPECT_EQ(L"", empty); + empty.Delete(1); + EXPECT_EQ(L"", empty); +} + +TEST(fxcrt, WideStringMid) { + CFX_WideString fred(L"FRED"); + EXPECT_EQ(L"", fred.Mid(0, 0)); + EXPECT_EQ(L"", fred.Mid(3, 0)); + EXPECT_EQ(L"FRED", fred.Mid(0)); + EXPECT_EQ(L"RED", fred.Mid(1)); + EXPECT_EQ(L"ED", fred.Mid(2)); + EXPECT_EQ(L"D", fred.Mid(3)); + EXPECT_EQ(L"F", fred.Mid(0, 1)); + EXPECT_EQ(L"R", fred.Mid(1, 1)); + EXPECT_EQ(L"E", fred.Mid(2, 1)); + EXPECT_EQ(L"D", fred.Mid(3, 1)); + EXPECT_EQ(L"FR", fred.Mid(0, 2)); + EXPECT_EQ(L"FRED", fred.Mid(0, 4)); + EXPECT_EQ(L"FRED", fred.Mid(0, 10)); + + EXPECT_EQ(L"FR", fred.Mid(-1, 2)); + EXPECT_EQ(L"RED", fred.Mid(1, 4)); + EXPECT_EQ(L"", fred.Mid(4, 1)); + + CFX_WideString empty; + EXPECT_EQ(L"", empty.Mid(0, 0)); + EXPECT_EQ(L"", empty.Mid(0)); + EXPECT_EQ(L"", empty.Mid(1)); + EXPECT_EQ(L"", empty.Mid(-1)); +} + +TEST(fxcrt, WideStringLeft) { + CFX_WideString fred(L"FRED"); + EXPECT_EQ(L"", fred.Left(0)); + EXPECT_EQ(L"F", fred.Left(1)); + EXPECT_EQ(L"FR", fred.Left(2)); + EXPECT_EQ(L"FRE", fred.Left(3)); + EXPECT_EQ(L"FRED", fred.Left(4)); + + EXPECT_EQ(L"FRED", fred.Left(5)); + EXPECT_EQ(L"", fred.Left(-1)); + + CFX_WideString empty; + EXPECT_EQ(L"", empty.Left(0)); + EXPECT_EQ(L"", empty.Left(1)); + EXPECT_EQ(L"", empty.Left(-1)); +} + +TEST(fxcrt, WideStringRight) { + CFX_WideString fred(L"FRED"); + EXPECT_EQ(L"", fred.Right(0)); + EXPECT_EQ(L"D", fred.Right(1)); + EXPECT_EQ(L"ED", fred.Right(2)); + EXPECT_EQ(L"RED", fred.Right(3)); + EXPECT_EQ(L"FRED", fred.Right(4)); + + EXPECT_EQ(L"FRED", fred.Right(5)); + EXPECT_EQ(L"", fred.Right(-1)); + + CFX_WideString empty; + EXPECT_EQ(L"", empty.Right(0)); + EXPECT_EQ(L"", empty.Right(1)); + EXPECT_EQ(L"", empty.Right(-1)); +} + +TEST(fxcrt, WideStringUpperLower) { + CFX_WideString fred(L"F-Re.42D"); + fred.MakeLower(); + EXPECT_EQ(L"f-re.42d", fred); + fred.MakeUpper(); + EXPECT_EQ(L"F-RE.42D", fred); + + CFX_WideString empty; + empty.MakeLower(); + EXPECT_EQ(L"", empty); + empty.MakeUpper(); + EXPECT_EQ(L"", empty); +} + +TEST(fxcrt, WideStringTrimRight) { + CFX_WideString fred(L" FRED "); + fred.TrimRight(); + EXPECT_EQ(L" FRED", fred); + fred.TrimRight(L'E'); + EXPECT_EQ(L" FRED", fred); + fred.TrimRight(L'D'); + EXPECT_EQ(L" FRE", fred); + fred.TrimRight(L"ERP"); + EXPECT_EQ(L" F", fred); + + CFX_WideString blank(L" "); + blank.TrimRight(L"ERP"); + EXPECT_EQ(L" ", blank); + blank.TrimRight(L'E'); + EXPECT_EQ(L" ", blank); + blank.TrimRight(); + EXPECT_EQ(L"", blank); + + CFX_WideString empty; + empty.TrimRight(L"ERP"); + EXPECT_EQ(L"", empty); + empty.TrimRight(L'E'); + EXPECT_EQ(L"", empty); + empty.TrimRight(); + EXPECT_EQ(L"", empty); +} + +TEST(fxcrt, WideStringTrimRightCopies) { + { + // With a single reference, no copy takes place. + CFX_WideString fred(L" FRED "); + const wchar_t* old_buffer = fred.c_str(); + fred.TrimRight(); + EXPECT_EQ(L" FRED", fred); + EXPECT_EQ(old_buffer, fred.c_str()); + } + { + // With multiple references, we must copy. + CFX_WideString fred(L" FRED "); + CFX_WideString other_fred = fred; + const wchar_t* old_buffer = fred.c_str(); + fred.TrimRight(); + EXPECT_EQ(L" FRED", fred); + EXPECT_EQ(L" FRED ", other_fred); + EXPECT_NE(old_buffer, fred.c_str()); + } + { + // With multiple references, but no modifications, no copy. + CFX_WideString fred(L"FRED"); + CFX_WideString other_fred = fred; + const wchar_t* old_buffer = fred.c_str(); + fred.TrimRight(); + EXPECT_EQ(L"FRED", fred); + EXPECT_EQ(L"FRED", other_fred); + EXPECT_EQ(old_buffer, fred.c_str()); + } +} + +TEST(fxcrt, WideStringTrimLeft) { + CFX_WideString fred(L" FRED "); + fred.TrimLeft(); + EXPECT_EQ(L"FRED ", fred); + fred.TrimLeft(L'E'); + EXPECT_EQ(L"FRED ", fred); + fred.TrimLeft(L'F'); + EXPECT_EQ(L"RED ", fred); + fred.TrimLeft(L"ERP"); + EXPECT_EQ(L"D ", fred); + + CFX_WideString blank(L" "); + blank.TrimLeft(L"ERP"); + EXPECT_EQ(L" ", blank); + blank.TrimLeft(L'E'); + EXPECT_EQ(L" ", blank); + blank.TrimLeft(); + EXPECT_EQ(L"", blank); + + CFX_WideString empty; + empty.TrimLeft(L"ERP"); + EXPECT_EQ(L"", empty); + empty.TrimLeft(L'E'); + EXPECT_EQ(L"", empty); + empty.TrimLeft(); + EXPECT_EQ(L"", empty); +} + +TEST(fxcrt, WideStringTrimLeftCopies) { + { + // With a single reference, no copy takes place. + CFX_WideString fred(L" FRED "); + const wchar_t* old_buffer = fred.c_str(); + fred.TrimLeft(); + EXPECT_EQ(L"FRED ", fred); + EXPECT_EQ(old_buffer, fred.c_str()); + } + { + // With multiple references, we must copy. + CFX_WideString fred(L" FRED "); + CFX_WideString other_fred = fred; + const wchar_t* old_buffer = fred.c_str(); + fred.TrimLeft(); + EXPECT_EQ(L"FRED ", fred); + EXPECT_EQ(L" FRED ", other_fred); + EXPECT_NE(old_buffer, fred.c_str()); + } + { + // With multiple references, but no modifications, no copy. + CFX_WideString fred(L"FRED"); + CFX_WideString other_fred = fred; + const wchar_t* old_buffer = fred.c_str(); + fred.TrimLeft(); + EXPECT_EQ(L"FRED", fred); + EXPECT_EQ(L"FRED", other_fred); + EXPECT_EQ(old_buffer, fred.c_str()); + } +} + +TEST(fxcrt, WideStringReserve) { + { + CFX_WideString str; + str.Reserve(6); + const wchar_t* old_buffer = str.c_str(); + str += L"ABCDEF"; + EXPECT_EQ(old_buffer, str.c_str()); + str += L"Blah Blah Blah Blah Blah Blah"; + EXPECT_NE(old_buffer, str.c_str()); + } + { + CFX_WideString str(L"A"); + str.Reserve(6); + const wchar_t* old_buffer = str.c_str(); + str += L"BCDEF"; + EXPECT_EQ(old_buffer, str.c_str()); + str += L"Blah Blah Blah Blah Blah Blah"; + EXPECT_NE(old_buffer, str.c_str()); + } +} + +TEST(fxcrt, WideStringGetBuffer) { + { + CFX_WideString str; + wchar_t* buffer = str.GetBuffer(12); + wcscpy(buffer, L"clams"); + str.ReleaseBuffer(); + EXPECT_EQ(L"clams", str); + } + { + CFX_WideString str(L"cl"); + wchar_t* buffer = str.GetBuffer(12); + wcscpy(buffer + 2, L"ams"); + str.ReleaseBuffer(); + EXPECT_EQ(L"clams", str); + } +} + +TEST(fxcrt, WideStringReleaseBuffer) { + { + CFX_WideString str; + str.Reserve(12); + str += L"clams"; + const wchar_t* old_buffer = str.c_str(); + str.ReleaseBuffer(4); + EXPECT_EQ(old_buffer, str.c_str()); + EXPECT_EQ(L"clam", str); + } + { + CFX_WideString str(L"c"); + str.Reserve(12); + str += L"lams"; + const wchar_t* old_buffer = str.c_str(); + str.ReleaseBuffer(4); + EXPECT_EQ(old_buffer, str.c_str()); + EXPECT_EQ(L"clam", str); + } + { + CFX_WideString str; + str.Reserve(200); + str += L"clams"; + const wchar_t* old_buffer = str.c_str(); + str.ReleaseBuffer(4); + EXPECT_NE(old_buffer, str.c_str()); + EXPECT_EQ(L"clam", str); + } + { + CFX_WideString str(L"c"); + str.Reserve(200); + str += L"lams"; + const wchar_t* old_buffer = str.c_str(); + str.ReleaseBuffer(4); + EXPECT_NE(old_buffer, str.c_str()); + EXPECT_EQ(L"clam", str); + } +} + +TEST(fxcrt, WideStringUTF16LE_Encode) { + struct UTF16LEEncodeCase { + CFX_WideString ws; + CFX_ByteString bs; + } utf16le_encode_cases[] = { + {L"", CFX_ByteString("\0\0", 2)}, + {L"abc", CFX_ByteString("a\0b\0c\0\0\0", 8)}, + {L"abcdef", CFX_ByteString("a\0b\0c\0d\0e\0f\0\0\0", 14)}, + {L"abc\0def", CFX_ByteString("a\0b\0c\0\0\0", 8)}, + {L"\xaabb\xccdd", CFX_ByteString("\xbb\xaa\xdd\xcc\0\0", 6)}, + {L"\x3132\x6162", CFX_ByteString("\x32\x31\x62\x61\0\0", 6)}, + }; + + for (size_t i = 0; i < FX_ArraySize(utf16le_encode_cases); ++i) { + EXPECT_EQ(utf16le_encode_cases[i].bs, + utf16le_encode_cases[i].ws.UTF16LE_Encode()) + << " for case number " << i; + } +} + +TEST(fxcrt, WideStringCOperatorSubscript) { + // CFX_WideStringC includes the NUL terminator for non-empty strings. + CFX_WideStringC abc(L"abc"); + EXPECT_EQ(L'a', abc.CharAt(0)); + EXPECT_EQ(L'b', abc.CharAt(1)); + EXPECT_EQ(L'c', abc.CharAt(2)); + EXPECT_EQ(L'\0', abc.CharAt(3)); +} + +TEST(fxcrt, WideStringCOperatorLT) { + CFX_WideStringC empty; + CFX_WideStringC a(L"a"); + CFX_WideStringC abc(L"\x0110qq"); // Comes before despite endianness. + CFX_WideStringC def(L"\x1001qq"); // Comes after despite endianness. + + EXPECT_FALSE(empty < empty); + EXPECT_FALSE(a < a); + EXPECT_FALSE(abc < abc); + EXPECT_FALSE(def < def); + + EXPECT_TRUE(empty < a); + EXPECT_FALSE(a < empty); + + EXPECT_TRUE(empty < abc); + EXPECT_FALSE(abc < empty); + + EXPECT_TRUE(empty < def); + EXPECT_FALSE(def < empty); + + EXPECT_TRUE(a < abc); + EXPECT_FALSE(abc < a); + + EXPECT_TRUE(a < def); + EXPECT_FALSE(def < a); + + EXPECT_TRUE(abc < def); + EXPECT_FALSE(def < abc); +} + +TEST(fxcrt, WideStringCOperatorEQ) { + CFX_WideStringC wide_string_c(L"hello"); + EXPECT_TRUE(wide_string_c == wide_string_c); + + CFX_WideStringC wide_string_c_same1(L"hello"); + EXPECT_TRUE(wide_string_c == wide_string_c_same1); + EXPECT_TRUE(wide_string_c_same1 == wide_string_c); + + CFX_WideStringC wide_string_c_same2(wide_string_c); + EXPECT_TRUE(wide_string_c == wide_string_c_same2); + EXPECT_TRUE(wide_string_c_same2 == wide_string_c); + + CFX_WideStringC wide_string_c1(L"he"); + CFX_WideStringC wide_string_c2(L"hellp"); + CFX_WideStringC wide_string_c3(L"hellod"); + EXPECT_FALSE(wide_string_c == wide_string_c1); + EXPECT_FALSE(wide_string_c == wide_string_c2); + EXPECT_FALSE(wide_string_c == wide_string_c3); + EXPECT_FALSE(wide_string_c1 == wide_string_c); + EXPECT_FALSE(wide_string_c2 == wide_string_c); + EXPECT_FALSE(wide_string_c3 == wide_string_c); + + CFX_WideString wide_string_same1(L"hello"); + EXPECT_TRUE(wide_string_c == wide_string_same1); + EXPECT_TRUE(wide_string_same1 == wide_string_c); + + CFX_WideString wide_string1(L"he"); + CFX_WideString wide_string2(L"hellp"); + CFX_WideString wide_string3(L"hellod"); + EXPECT_FALSE(wide_string_c == wide_string1); + EXPECT_FALSE(wide_string_c == wide_string2); + EXPECT_FALSE(wide_string_c == wide_string3); + EXPECT_FALSE(wide_string1 == wide_string_c); + EXPECT_FALSE(wide_string2 == wide_string_c); + EXPECT_FALSE(wide_string3 == wide_string_c); + + const wchar_t* c_string_same1 = L"hello"; + EXPECT_TRUE(wide_string_c == c_string_same1); + EXPECT_TRUE(c_string_same1 == wide_string_c); + + const wchar_t* c_string1 = L"he"; + const wchar_t* c_string2 = L"hellp"; + const wchar_t* c_string3 = L"hellod"; + EXPECT_FALSE(wide_string_c == c_string1); + EXPECT_FALSE(wide_string_c == c_string2); + EXPECT_FALSE(wide_string_c == c_string3); + + EXPECT_FALSE(c_string1 == wide_string_c); + EXPECT_FALSE(c_string2 == wide_string_c); + EXPECT_FALSE(c_string3 == wide_string_c); +} + +TEST(fxcrt, WideStringCOperatorNE) { + CFX_WideStringC wide_string_c(L"hello"); + EXPECT_FALSE(wide_string_c != wide_string_c); + + CFX_WideStringC wide_string_c_same1(L"hello"); + EXPECT_FALSE(wide_string_c != wide_string_c_same1); + EXPECT_FALSE(wide_string_c_same1 != wide_string_c); + + CFX_WideStringC wide_string_c_same2(wide_string_c); + EXPECT_FALSE(wide_string_c != wide_string_c_same2); + EXPECT_FALSE(wide_string_c_same2 != wide_string_c); + + CFX_WideStringC wide_string_c1(L"he"); + CFX_WideStringC wide_string_c2(L"hellp"); + CFX_WideStringC wide_string_c3(L"hellod"); + EXPECT_TRUE(wide_string_c != wide_string_c1); + EXPECT_TRUE(wide_string_c != wide_string_c2); + EXPECT_TRUE(wide_string_c != wide_string_c3); + EXPECT_TRUE(wide_string_c1 != wide_string_c); + EXPECT_TRUE(wide_string_c2 != wide_string_c); + EXPECT_TRUE(wide_string_c3 != wide_string_c); + + CFX_WideString wide_string_same1(L"hello"); + EXPECT_FALSE(wide_string_c != wide_string_same1); + EXPECT_FALSE(wide_string_same1 != wide_string_c); + + CFX_WideString wide_string1(L"he"); + CFX_WideString wide_string2(L"hellp"); + CFX_WideString wide_string3(L"hellod"); + EXPECT_TRUE(wide_string_c != wide_string1); + EXPECT_TRUE(wide_string_c != wide_string2); + EXPECT_TRUE(wide_string_c != wide_string3); + EXPECT_TRUE(wide_string1 != wide_string_c); + EXPECT_TRUE(wide_string2 != wide_string_c); + EXPECT_TRUE(wide_string3 != wide_string_c); + + const wchar_t* c_string_same1 = L"hello"; + EXPECT_FALSE(wide_string_c != c_string_same1); + EXPECT_FALSE(c_string_same1 != wide_string_c); + + const wchar_t* c_string1 = L"he"; + const wchar_t* c_string2 = L"hellp"; + const wchar_t* c_string3 = L"hellod"; + EXPECT_TRUE(wide_string_c != c_string1); + EXPECT_TRUE(wide_string_c != c_string2); + EXPECT_TRUE(wide_string_c != c_string3); + + EXPECT_TRUE(c_string1 != wide_string_c); + EXPECT_TRUE(c_string2 != wide_string_c); + EXPECT_TRUE(c_string3 != wide_string_c); +} + +TEST(fxcrt, WideStringCFind) { + CFX_WideStringC null_string; + EXPECT_EQ(-1, null_string.Find(L'a')); + EXPECT_EQ(-1, null_string.Find(0)); + + CFX_WideStringC empty_string(L""); + EXPECT_EQ(-1, empty_string.Find(L'a')); + EXPECT_EQ(-1, empty_string.Find(0)); + + CFX_WideStringC single_string(L"a"); + EXPECT_EQ(0, single_string.Find(L'a')); + EXPECT_EQ(-1, single_string.Find(L'b')); + EXPECT_EQ(-1, single_string.Find(0)); + + CFX_WideStringC longer_string(L"abccc"); + EXPECT_EQ(0, longer_string.Find(L'a')); + EXPECT_EQ(2, longer_string.Find(L'c')); + EXPECT_EQ(-1, longer_string.Find(0)); + + CFX_WideStringC hibyte_string( + L"ab\xff08" + L"def"); + EXPECT_EQ(2, hibyte_string.Find(L'\xff08')); +} + +TEST(fxcrt, WideStringFormatWidth) { + { + CFX_WideString str; + str.Format(L"%5d", 1); + EXPECT_EQ(L" 1", str); + } + + { + CFX_WideString str; + str.Format(L"%d", 1); + EXPECT_EQ(L"1", str); + } + + { + CFX_WideString str; + str.Format(L"%*d", 5, 1); + EXPECT_EQ(L" 1", str); + } + + { + CFX_WideString str; + str.Format(L"%-1d", 1); + EXPECT_EQ(L"1", str); + } + + { + CFX_WideString str; + str.Format(L"%0d", 1); + EXPECT_EQ(L"1", str); + } + + { + CFX_WideString str; + str.Format(L"%1048576d", 1); + EXPECT_EQ(L"", str); + } +} + +TEST(fxcrt, WideStringFormatPrecision) { + { + CFX_WideString str; + str.Format(L"%.2f", 1.12345); + EXPECT_EQ(L"1.12", str); + } + + { + CFX_WideString str; + str.Format(L"%.*f", 3, 1.12345); + EXPECT_EQ(L"1.123", str); + } + + { + CFX_WideString str; + str.Format(L"%f", 1.12345); + EXPECT_EQ(L"1.123450", str); + } + + { + CFX_WideString str; + str.Format(L"%-1f", 1.12345); + EXPECT_EQ(L"1.123450", str); + } + + { + CFX_WideString str; + str.Format(L"%0f", 1.12345); + EXPECT_EQ(L"1.123450", str); + } + + { + CFX_WideString str; + str.Format(L"%.1048576f", 1.2); + EXPECT_EQ(L"", str); + } +} + +TEST(fxcrt, EmptyWideString) { + CFX_WideString empty_str; + EXPECT_TRUE(empty_str.IsEmpty()); + EXPECT_EQ(0, empty_str.GetLength()); + const wchar_t* cstr = empty_str.c_str(); + EXPECT_EQ(0, FXSYS_wcslen(cstr)); +} diff --git a/core/fxcrt/fx_basic_bstring.cpp b/core/fxcrt/fx_basic_bstring.cpp deleted file mode 100644 index 1ce1ffa563..0000000000 --- a/core/fxcrt/fx_basic_bstring.cpp +++ /dev/null @@ -1,818 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include - -#include -#include - -#include "core/fxcrt/cfx_string_pool_template.h" -#include "core/fxcrt/fx_basic.h" -#include "third_party/base/numerics/safe_math.h" -#include "third_party/base/stl_util.h" - -template class CFX_StringDataTemplate; -template class CFX_StringCTemplate; -template class CFX_StringPoolTemplate; -template struct std::hash; - -namespace { - -int Buffer_itoa(char* buf, int i, uint32_t flags) { - if (i == 0) { - buf[0] = '0'; - return 1; - } - char buf1[32]; - int buf_pos = 31; - uint32_t u = i; - if ((flags & FXFORMAT_SIGNED) && i < 0) { - u = -i; - } - int base = 10; - const char* str = "0123456789abcdef"; - if (flags & FXFORMAT_HEX) { - base = 16; - if (flags & FXFORMAT_CAPITAL) { - str = "0123456789ABCDEF"; - } - } - while (u != 0) { - buf1[buf_pos--] = str[u % base]; - u = u / base; - } - if ((flags & FXFORMAT_SIGNED) && i < 0) { - buf1[buf_pos--] = '-'; - } - int len = 31 - buf_pos; - for (int ii = 0; ii < len; ii++) { - buf[ii] = buf1[ii + buf_pos + 1]; - } - return len; -} - -const char* FX_strstr(const char* haystack, - int haystack_len, - const char* needle, - int needle_len) { - if (needle_len > haystack_len || needle_len == 0) { - return nullptr; - } - const char* end_ptr = haystack + haystack_len - needle_len; - while (haystack <= end_ptr) { - int i = 0; - while (1) { - if (haystack[i] != needle[i]) { - break; - } - i++; - if (i == needle_len) { - return haystack; - } - } - haystack++; - } - return nullptr; -} - -} // namespace - -static_assert(sizeof(CFX_ByteString) <= sizeof(char*), - "Strings must not require more space than pointers"); - -CFX_ByteString::CFX_ByteString(const char* pStr, FX_STRSIZE nLen) { - if (nLen < 0) - nLen = pStr ? FXSYS_strlen(pStr) : 0; - - if (nLen) - m_pData.Reset(StringData::Create(pStr, nLen)); -} - -CFX_ByteString::CFX_ByteString(const uint8_t* pStr, FX_STRSIZE nLen) { - if (nLen > 0) { - m_pData.Reset( - StringData::Create(reinterpret_cast(pStr), nLen)); - } -} - -CFX_ByteString::CFX_ByteString() {} - -CFX_ByteString::CFX_ByteString(const CFX_ByteString& other) - : m_pData(other.m_pData) {} - -CFX_ByteString::CFX_ByteString(CFX_ByteString&& other) noexcept { - m_pData.Swap(other.m_pData); -} - -CFX_ByteString::CFX_ByteString(char ch) { - m_pData.Reset(StringData::Create(1)); - m_pData->m_String[0] = ch; -} - -CFX_ByteString::CFX_ByteString(const char* ptr) - : CFX_ByteString(ptr, ptr ? FXSYS_strlen(ptr) : 0) {} - -CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& stringSrc) { - if (!stringSrc.IsEmpty()) - m_pData.Reset(StringData::Create(stringSrc.c_str(), stringSrc.GetLength())); -} - -CFX_ByteString::CFX_ByteString(const CFX_ByteStringC& str1, - const CFX_ByteStringC& str2) { - int nNewLen = str1.GetLength() + str2.GetLength(); - if (nNewLen == 0) - return; - - m_pData.Reset(StringData::Create(nNewLen)); - m_pData->CopyContents(str1.c_str(), str1.GetLength()); - m_pData->CopyContentsAt(str1.GetLength(), str2.c_str(), str2.GetLength()); -} - -CFX_ByteString::~CFX_ByteString() {} - -const CFX_ByteString& CFX_ByteString::operator=(const char* pStr) { - if (!pStr || !pStr[0]) - clear(); - else - AssignCopy(pStr, FXSYS_strlen(pStr)); - - return *this; -} - -const CFX_ByteString& CFX_ByteString::operator=( - const CFX_ByteStringC& stringSrc) { - if (stringSrc.IsEmpty()) - clear(); - else - AssignCopy(stringSrc.c_str(), stringSrc.GetLength()); - - return *this; -} - -const CFX_ByteString& CFX_ByteString::operator=( - const CFX_ByteString& stringSrc) { - if (m_pData != stringSrc.m_pData) - m_pData = stringSrc.m_pData; - - return *this; -} - -const CFX_ByteString& CFX_ByteString::operator+=(const char* pStr) { - if (pStr) - Concat(pStr, FXSYS_strlen(pStr)); - - return *this; -} - -const CFX_ByteString& CFX_ByteString::operator+=(char ch) { - Concat(&ch, 1); - return *this; -} - -const CFX_ByteString& CFX_ByteString::operator+=(const CFX_ByteString& str) { - if (str.m_pData) - Concat(str.m_pData->m_String, str.m_pData->m_nDataLength); - - return *this; -} - -const CFX_ByteString& CFX_ByteString::operator+=(const CFX_ByteStringC& str) { - if (!str.IsEmpty()) - Concat(str.c_str(), str.GetLength()); - - return *this; -} - -bool CFX_ByteString::operator==(const char* ptr) const { - if (!m_pData) - return !ptr || !ptr[0]; - - if (!ptr) - return m_pData->m_nDataLength == 0; - - return FXSYS_strlen(ptr) == m_pData->m_nDataLength && - FXSYS_memcmp(ptr, m_pData->m_String, m_pData->m_nDataLength) == 0; -} - -bool CFX_ByteString::operator==(const CFX_ByteStringC& str) const { - if (!m_pData) - return str.IsEmpty(); - - return m_pData->m_nDataLength == str.GetLength() && - FXSYS_memcmp(m_pData->m_String, str.c_str(), str.GetLength()) == 0; -} - -bool CFX_ByteString::operator==(const CFX_ByteString& other) const { - if (m_pData == other.m_pData) - return true; - - if (IsEmpty()) - return other.IsEmpty(); - - if (other.IsEmpty()) - return false; - - return other.m_pData->m_nDataLength == m_pData->m_nDataLength && - FXSYS_memcmp(other.m_pData->m_String, m_pData->m_String, - m_pData->m_nDataLength) == 0; -} - -bool CFX_ByteString::operator<(const CFX_ByteString& str) const { - if (m_pData == str.m_pData) - return false; - - int result = FXSYS_memcmp(c_str(), str.c_str(), - std::min(GetLength(), str.GetLength())); - return result < 0 || (result == 0 && GetLength() < str.GetLength()); -} - -bool CFX_ByteString::EqualNoCase(const CFX_ByteStringC& str) const { - if (!m_pData) - return str.IsEmpty(); - - FX_STRSIZE len = str.GetLength(); - if (m_pData->m_nDataLength != len) - return false; - - const uint8_t* pThis = (const uint8_t*)m_pData->m_String; - const uint8_t* pThat = str.raw_str(); - for (FX_STRSIZE i = 0; i < len; i++) { - if ((*pThis) != (*pThat)) { - uint8_t bThis = *pThis; - if (bThis >= 'A' && bThis <= 'Z') - bThis += 'a' - 'A'; - - uint8_t bThat = *pThat; - if (bThat >= 'A' && bThat <= 'Z') - bThat += 'a' - 'A'; - - if (bThis != bThat) - return false; - } - pThis++; - pThat++; - } - return true; -} - -void CFX_ByteString::AssignCopy(const char* pSrcData, FX_STRSIZE nSrcLen) { - AllocBeforeWrite(nSrcLen); - m_pData->CopyContents(pSrcData, nSrcLen); - m_pData->m_nDataLength = nSrcLen; -} - -void CFX_ByteString::ReallocBeforeWrite(FX_STRSIZE nNewLength) { - if (m_pData && m_pData->CanOperateInPlace(nNewLength)) - return; - - if (nNewLength <= 0) { - clear(); - return; - } - - CFX_RetainPtr pNewData(StringData::Create(nNewLength)); - if (m_pData) { - FX_STRSIZE nCopyLength = std::min(m_pData->m_nDataLength, nNewLength); - pNewData->CopyContents(m_pData->m_String, nCopyLength); - pNewData->m_nDataLength = nCopyLength; - } else { - pNewData->m_nDataLength = 0; - } - pNewData->m_String[pNewData->m_nDataLength] = 0; - m_pData.Swap(pNewData); -} - -void CFX_ByteString::AllocBeforeWrite(FX_STRSIZE nNewLength) { - if (m_pData && m_pData->CanOperateInPlace(nNewLength)) - return; - - if (nNewLength <= 0) { - clear(); - return; - } - - m_pData.Reset(StringData::Create(nNewLength)); -} - -void CFX_ByteString::ReleaseBuffer(FX_STRSIZE nNewLength) { - if (!m_pData) - return; - - if (nNewLength == -1) - nNewLength = FXSYS_strlen(m_pData->m_String); - - nNewLength = std::min(nNewLength, m_pData->m_nAllocLength); - if (nNewLength == 0) { - clear(); - return; - } - - ASSERT(m_pData->m_nRefs == 1); - m_pData->m_nDataLength = nNewLength; - m_pData->m_String[nNewLength] = 0; - if (m_pData->m_nAllocLength - nNewLength >= 32) { - // Over arbitrary threshold, so pay the price to relocate. Force copy to - // always occur by holding a second reference to the string. - CFX_ByteString preserve(*this); - ReallocBeforeWrite(nNewLength); - } -} - -void CFX_ByteString::Reserve(FX_STRSIZE len) { - GetBuffer(len); -} - -char* CFX_ByteString::GetBuffer(FX_STRSIZE nMinBufLength) { - if (!m_pData) { - if (nMinBufLength == 0) - return nullptr; - - m_pData.Reset(StringData::Create(nMinBufLength)); - m_pData->m_nDataLength = 0; - m_pData->m_String[0] = 0; - return m_pData->m_String; - } - - if (m_pData->CanOperateInPlace(nMinBufLength)) - return m_pData->m_String; - - nMinBufLength = std::max(nMinBufLength, m_pData->m_nDataLength); - if (nMinBufLength == 0) - return nullptr; - - CFX_RetainPtr pNewData(StringData::Create(nMinBufLength)); - pNewData->CopyContents(*m_pData); - pNewData->m_nDataLength = m_pData->m_nDataLength; - m_pData.Swap(pNewData); - return m_pData->m_String; -} - -FX_STRSIZE CFX_ByteString::Delete(FX_STRSIZE nIndex, FX_STRSIZE nCount) { - if (!m_pData) - return 0; - - if (nIndex < 0) - nIndex = 0; - - FX_STRSIZE nOldLength = m_pData->m_nDataLength; - if (nCount > 0 && nIndex < nOldLength) { - FX_STRSIZE mLength = nIndex + nCount; - if (mLength >= nOldLength) { - m_pData->m_nDataLength = nIndex; - return m_pData->m_nDataLength; - } - ReallocBeforeWrite(nOldLength); - int nCharsToCopy = nOldLength - mLength + 1; - FXSYS_memmove(m_pData->m_String + nIndex, m_pData->m_String + mLength, - nCharsToCopy); - m_pData->m_nDataLength = nOldLength - nCount; - } - return m_pData->m_nDataLength; -} - -void CFX_ByteString::Concat(const char* pSrcData, FX_STRSIZE nSrcLen) { - if (!pSrcData || nSrcLen <= 0) - return; - - if (!m_pData) { - m_pData.Reset(StringData::Create(pSrcData, nSrcLen)); - return; - } - - if (m_pData->CanOperateInPlace(m_pData->m_nDataLength + nSrcLen)) { - m_pData->CopyContentsAt(m_pData->m_nDataLength, pSrcData, nSrcLen); - m_pData->m_nDataLength += nSrcLen; - return; - } - - CFX_RetainPtr pNewData( - StringData::Create(m_pData->m_nDataLength + nSrcLen)); - pNewData->CopyContents(*m_pData); - pNewData->CopyContentsAt(m_pData->m_nDataLength, pSrcData, nSrcLen); - m_pData.Swap(pNewData); -} - -CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst) const { - if (!m_pData) - return CFX_ByteString(); - - return Mid(nFirst, m_pData->m_nDataLength - nFirst); -} - -CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const { - if (!m_pData) - return CFX_ByteString(); - - nFirst = pdfium::clamp(nFirst, 0, m_pData->m_nDataLength); - nCount = pdfium::clamp(nCount, 0, m_pData->m_nDataLength - nFirst); - if (nCount == 0) - return CFX_ByteString(); - - if (nFirst == 0 && nCount == m_pData->m_nDataLength) - return *this; - - CFX_ByteString dest; - AllocCopy(dest, nCount, nFirst); - return dest; -} - -void CFX_ByteString::AllocCopy(CFX_ByteString& dest, - FX_STRSIZE nCopyLen, - FX_STRSIZE nCopyIndex) const { - if (nCopyLen <= 0) - return; - - CFX_RetainPtr pNewData( - StringData::Create(m_pData->m_String + nCopyIndex, nCopyLen)); - dest.m_pData.Swap(pNewData); -} - -#define FORCE_ANSI 0x10000 -#define FORCE_UNICODE 0x20000 -#define FORCE_INT64 0x40000 - -CFX_ByteString CFX_ByteString::FormatInteger(int i, uint32_t flags) { - char buf[32]; - return CFX_ByteString(buf, Buffer_itoa(buf, i, flags)); -} - -void CFX_ByteString::FormatV(const char* pFormat, va_list argList) { - va_list argListSave; - FX_VA_COPY(argListSave, argList); - FX_STRSIZE nMaxLen = vsnprintf(nullptr, 0, pFormat, argList); - 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); - vsnprintf(m_pData->m_String, nMaxLen + 1, pFormat, argListSave); - ReleaseBuffer(); - } - } - va_end(argListSave); -} - -void CFX_ByteString::Format(const char* pFormat, ...) { - va_list argList; - va_start(argList, pFormat); - FormatV(pFormat, argList); - va_end(argList); -} - -FX_STRSIZE CFX_ByteString::Insert(FX_STRSIZE nIndex, char ch) { - FX_STRSIZE nNewLength = m_pData ? m_pData->m_nDataLength : 0; - nIndex = std::max(nIndex, 0); - nIndex = std::min(nIndex, nNewLength); - nNewLength++; - - ReallocBeforeWrite(nNewLength); - FXSYS_memmove(m_pData->m_String + nIndex + 1, m_pData->m_String + nIndex, - nNewLength - nIndex); - m_pData->m_String[nIndex] = ch; - m_pData->m_nDataLength = nNewLength; - return nNewLength; -} - -CFX_ByteString CFX_ByteString::Right(FX_STRSIZE nCount) const { - if (!m_pData) - return CFX_ByteString(); - - nCount = std::max(nCount, 0); - if (nCount >= m_pData->m_nDataLength) - return *this; - - CFX_ByteString dest; - AllocCopy(dest, nCount, m_pData->m_nDataLength - nCount); - return dest; -} - -CFX_ByteString CFX_ByteString::Left(FX_STRSIZE nCount) const { - if (!m_pData) - return CFX_ByteString(); - - nCount = std::max(nCount, 0); - if (nCount >= m_pData->m_nDataLength) - return *this; - - CFX_ByteString dest; - AllocCopy(dest, nCount, 0); - return dest; -} - -FX_STRSIZE CFX_ByteString::Find(char ch, FX_STRSIZE nStart) const { - if (!m_pData) - return -1; - - if (nStart < 0 || nStart >= m_pData->m_nDataLength) - return -1; - - const char* pStr = static_cast( - memchr(m_pData->m_String + nStart, ch, m_pData->m_nDataLength - nStart)); - return pStr ? pStr - m_pData->m_String : -1; -} - -FX_STRSIZE CFX_ByteString::ReverseFind(char ch) const { - if (!m_pData) - return -1; - - FX_STRSIZE nLength = m_pData->m_nDataLength; - while (nLength--) { - if (m_pData->m_String[nLength] == ch) - return nLength; - } - return -1; -} - -FX_STRSIZE CFX_ByteString::Find(const CFX_ByteStringC& pSub, - FX_STRSIZE nStart) const { - if (!m_pData) - return -1; - - FX_STRSIZE nLength = m_pData->m_nDataLength; - if (nStart > nLength) - return -1; - - const char* pStr = - FX_strstr(m_pData->m_String + nStart, m_pData->m_nDataLength - nStart, - pSub.c_str(), pSub.GetLength()); - return pStr ? (int)(pStr - m_pData->m_String) : -1; -} - -void CFX_ByteString::MakeLower() { - if (!m_pData) - return; - - ReallocBeforeWrite(m_pData->m_nDataLength); - FXSYS_strlwr(m_pData->m_String); -} - -void CFX_ByteString::MakeUpper() { - if (!m_pData) - return; - - ReallocBeforeWrite(m_pData->m_nDataLength); - FXSYS_strupr(m_pData->m_String); -} - -FX_STRSIZE CFX_ByteString::Remove(char chRemove) { - if (!m_pData || m_pData->m_nDataLength < 1) - return 0; - - char* pstrSource = m_pData->m_String; - char* pstrEnd = m_pData->m_String + m_pData->m_nDataLength; - while (pstrSource < pstrEnd) { - if (*pstrSource == chRemove) - break; - pstrSource++; - } - if (pstrSource == pstrEnd) - return 0; - - ptrdiff_t copied = pstrSource - m_pData->m_String; - ReallocBeforeWrite(m_pData->m_nDataLength); - pstrSource = m_pData->m_String + copied; - pstrEnd = m_pData->m_String + m_pData->m_nDataLength; - - char* pstrDest = pstrSource; - while (pstrSource < pstrEnd) { - if (*pstrSource != chRemove) { - *pstrDest = *pstrSource; - pstrDest++; - } - pstrSource++; - } - - *pstrDest = 0; - FX_STRSIZE nCount = (FX_STRSIZE)(pstrSource - pstrDest); - m_pData->m_nDataLength -= nCount; - return nCount; -} - -FX_STRSIZE CFX_ByteString::Replace(const CFX_ByteStringC& pOld, - const CFX_ByteStringC& pNew) { - if (!m_pData || pOld.IsEmpty()) - return 0; - - FX_STRSIZE nSourceLen = pOld.GetLength(); - FX_STRSIZE nReplacementLen = pNew.GetLength(); - FX_STRSIZE nCount = 0; - const char* pStart = m_pData->m_String; - char* pEnd = m_pData->m_String + m_pData->m_nDataLength; - while (1) { - const char* pTarget = FX_strstr(pStart, (FX_STRSIZE)(pEnd - pStart), - pOld.c_str(), nSourceLen); - if (!pTarget) - break; - - nCount++; - pStart = pTarget + nSourceLen; - } - if (nCount == 0) - return 0; - - FX_STRSIZE nNewLength = - m_pData->m_nDataLength + (nReplacementLen - nSourceLen) * nCount; - - if (nNewLength == 0) { - clear(); - return nCount; - } - - CFX_RetainPtr pNewData(StringData::Create(nNewLength)); - pStart = m_pData->m_String; - char* pDest = pNewData->m_String; - for (FX_STRSIZE i = 0; i < nCount; i++) { - const char* pTarget = FX_strstr(pStart, (FX_STRSIZE)(pEnd - pStart), - pOld.c_str(), nSourceLen); - FXSYS_memcpy(pDest, pStart, pTarget - pStart); - pDest += pTarget - pStart; - FXSYS_memcpy(pDest, pNew.c_str(), pNew.GetLength()); - pDest += pNew.GetLength(); - pStart = pTarget + nSourceLen; - } - FXSYS_memcpy(pDest, pStart, pEnd - pStart); - m_pData.Swap(pNewData); - return nCount; -} - -void CFX_ByteString::SetAt(FX_STRSIZE nIndex, char ch) { - if (!m_pData) { - return; - } - ASSERT(nIndex >= 0); - ASSERT(nIndex < m_pData->m_nDataLength); - ReallocBeforeWrite(m_pData->m_nDataLength); - m_pData->m_String[nIndex] = ch; -} - -CFX_WideString CFX_ByteString::UTF8Decode() const { - CFX_UTF8Decoder decoder; - for (FX_STRSIZE i = 0; i < GetLength(); i++) { - decoder.Input((uint8_t)m_pData->m_String[i]); - } - return CFX_WideString(decoder.GetResult()); -} - -// static -CFX_ByteString CFX_ByteString::FromUnicode(const wchar_t* str, FX_STRSIZE len) { - FX_STRSIZE str_len = len >= 0 ? len : FXSYS_wcslen(str); - return FromUnicode(CFX_WideString(str, str_len)); -} - -// static -CFX_ByteString CFX_ByteString::FromUnicode(const CFX_WideString& str) { - return CFX_CharMap::GetByteString(0, str.AsStringC()); -} - -int CFX_ByteString::Compare(const CFX_ByteStringC& str) const { - if (!m_pData) { - return str.IsEmpty() ? 0 : -1; - } - int this_len = m_pData->m_nDataLength; - int that_len = str.GetLength(); - int min_len = this_len < that_len ? this_len : that_len; - for (int i = 0; i < min_len; i++) { - if ((uint8_t)m_pData->m_String[i] < str.GetAt(i)) { - return -1; - } - if ((uint8_t)m_pData->m_String[i] > str.GetAt(i)) { - return 1; - } - } - if (this_len < that_len) { - return -1; - } - if (this_len > that_len) { - return 1; - } - return 0; -} - -void CFX_ByteString::TrimRight(const CFX_ByteStringC& pTargets) { - if (!m_pData || pTargets.IsEmpty()) { - return; - } - FX_STRSIZE pos = GetLength(); - if (pos < 1) { - return; - } - while (pos) { - FX_STRSIZE i = 0; - while (i < pTargets.GetLength() && - pTargets[i] != m_pData->m_String[pos - 1]) { - i++; - } - if (i == pTargets.GetLength()) { - break; - } - pos--; - } - if (pos < m_pData->m_nDataLength) { - ReallocBeforeWrite(m_pData->m_nDataLength); - m_pData->m_String[pos] = 0; - m_pData->m_nDataLength = pos; - } -} - -void CFX_ByteString::TrimRight(char chTarget) { - TrimRight(CFX_ByteStringC(chTarget)); -} - -void CFX_ByteString::TrimRight() { - TrimRight("\x09\x0a\x0b\x0c\x0d\x20"); -} - -void CFX_ByteString::TrimLeft(const CFX_ByteStringC& pTargets) { - if (!m_pData || pTargets.IsEmpty()) - return; - - FX_STRSIZE len = GetLength(); - if (len < 1) - return; - - FX_STRSIZE pos = 0; - while (pos < len) { - FX_STRSIZE i = 0; - while (i < pTargets.GetLength() && pTargets[i] != m_pData->m_String[pos]) { - i++; - } - if (i == pTargets.GetLength()) { - break; - } - pos++; - } - if (pos) { - ReallocBeforeWrite(len); - FX_STRSIZE nDataLength = len - pos; - FXSYS_memmove(m_pData->m_String, m_pData->m_String + pos, - (nDataLength + 1) * sizeof(char)); - m_pData->m_nDataLength = nDataLength; - } -} - -void CFX_ByteString::TrimLeft(char chTarget) { - TrimLeft(CFX_ByteStringC(chTarget)); -} - -void CFX_ByteString::TrimLeft() { - TrimLeft("\x09\x0a\x0b\x0c\x0d\x20"); -} - -uint32_t CFX_ByteString::GetID(FX_STRSIZE start_pos) const { - return AsStringC().GetID(start_pos); -} -FX_STRSIZE FX_ftoa(float d, char* buf) { - buf[0] = '0'; - buf[1] = '\0'; - if (d == 0.0f) { - return 1; - } - bool bNegative = false; - if (d < 0) { - bNegative = true; - d = -d; - } - int scale = 1; - int scaled = FXSYS_round(d); - while (scaled < 100000) { - if (scale == 1000000) { - break; - } - scale *= 10; - scaled = FXSYS_round(d * scale); - } - if (scaled == 0) { - return 1; - } - char buf2[32]; - int buf_size = 0; - if (bNegative) { - buf[buf_size++] = '-'; - } - int i = scaled / scale; - FXSYS_itoa(i, buf2, 10); - FX_STRSIZE len = FXSYS_strlen(buf2); - FXSYS_memcpy(buf + buf_size, buf2, len); - buf_size += len; - int fraction = scaled % scale; - if (fraction == 0) { - return buf_size; - } - buf[buf_size++] = '.'; - scale /= 10; - while (fraction) { - buf[buf_size++] = '0' + fraction / scale; - fraction %= scale; - scale /= 10; - } - return buf_size; -} -CFX_ByteString CFX_ByteString::FormatFloat(float d, int precision) { - char buf[32]; - FX_STRSIZE len = FX_ftoa(d, buf); - return CFX_ByteString(buf, len); -} diff --git a/core/fxcrt/fx_basic_bstring_unittest.cpp b/core/fxcrt/fx_basic_bstring_unittest.cpp deleted file mode 100644 index e234989bf0..0000000000 --- a/core/fxcrt/fx_basic_bstring_unittest.cpp +++ /dev/null @@ -1,1090 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "core/fxcrt/fx_string.h" -#include "testing/fx_string_testhelpers.h" -#include "testing/gtest/include/gtest/gtest.h" - -TEST(fxcrt, ByteStringOperatorSubscript) { - // CFX_ByteString includes the NUL terminator for non-empty strings. - CFX_ByteString abc("abc"); - EXPECT_EQ('a', abc[0]); - EXPECT_EQ('b', abc[1]); - EXPECT_EQ('c', abc[2]); - EXPECT_EQ(0, abc[3]); -} - -TEST(fxcrt, ByteStringOperatorLT) { - CFX_ByteString empty; - CFX_ByteString a("a"); - CFX_ByteString abc("abc"); - CFX_ByteString def("def"); - - EXPECT_FALSE(empty < empty); - EXPECT_FALSE(a < a); - EXPECT_FALSE(abc < abc); - EXPECT_FALSE(def < def); - - EXPECT_TRUE(empty < a); - EXPECT_FALSE(a < empty); - - EXPECT_TRUE(empty < abc); - EXPECT_FALSE(abc < empty); - - EXPECT_TRUE(empty < def); - EXPECT_FALSE(def < empty); - - EXPECT_TRUE(a < abc); - EXPECT_FALSE(abc < a); - - EXPECT_TRUE(a < def); - EXPECT_FALSE(def < a); - - EXPECT_TRUE(abc < def); - EXPECT_FALSE(def < abc); -} - -TEST(fxcrt, ByteStringOperatorEQ) { - CFX_ByteString null_string; - EXPECT_TRUE(null_string == null_string); - - CFX_ByteString empty_string(""); - EXPECT_TRUE(empty_string == empty_string); - EXPECT_TRUE(empty_string == null_string); - EXPECT_TRUE(null_string == empty_string); - - CFX_ByteString deleted_string("hello"); - deleted_string.Delete(0, 5); - EXPECT_TRUE(deleted_string == deleted_string); - EXPECT_TRUE(deleted_string == null_string); - EXPECT_TRUE(deleted_string == empty_string); - EXPECT_TRUE(null_string == deleted_string); - EXPECT_TRUE(empty_string == deleted_string); - - CFX_ByteString byte_string("hello"); - EXPECT_TRUE(byte_string == byte_string); - EXPECT_FALSE(byte_string == null_string); - EXPECT_FALSE(byte_string == empty_string); - EXPECT_FALSE(byte_string == deleted_string); - EXPECT_FALSE(null_string == byte_string); - EXPECT_FALSE(empty_string == byte_string); - EXPECT_FALSE(deleted_string == byte_string); - - CFX_ByteString byte_string_same1("hello"); - EXPECT_TRUE(byte_string == byte_string_same1); - EXPECT_TRUE(byte_string_same1 == byte_string); - - CFX_ByteString byte_string_same2(byte_string); - EXPECT_TRUE(byte_string == byte_string_same2); - EXPECT_TRUE(byte_string_same2 == byte_string); - - CFX_ByteString byte_string1("he"); - CFX_ByteString byte_string2("hellp"); - CFX_ByteString byte_string3("hellod"); - EXPECT_FALSE(byte_string == byte_string1); - EXPECT_FALSE(byte_string == byte_string2); - EXPECT_FALSE(byte_string == byte_string3); - EXPECT_FALSE(byte_string1 == byte_string); - EXPECT_FALSE(byte_string2 == byte_string); - EXPECT_FALSE(byte_string3 == byte_string); - - CFX_ByteStringC null_string_c; - CFX_ByteStringC empty_string_c(""); - EXPECT_TRUE(null_string == null_string_c); - EXPECT_TRUE(null_string == empty_string_c); - EXPECT_TRUE(empty_string == null_string_c); - EXPECT_TRUE(empty_string == empty_string_c); - EXPECT_TRUE(deleted_string == null_string_c); - EXPECT_TRUE(deleted_string == empty_string_c); - EXPECT_TRUE(null_string_c == null_string); - EXPECT_TRUE(empty_string_c == null_string); - EXPECT_TRUE(null_string_c == empty_string); - EXPECT_TRUE(empty_string_c == empty_string); - EXPECT_TRUE(null_string_c == deleted_string); - EXPECT_TRUE(empty_string_c == deleted_string); - - CFX_ByteStringC byte_string_c_same1("hello"); - EXPECT_TRUE(byte_string == byte_string_c_same1); - EXPECT_TRUE(byte_string_c_same1 == byte_string); - - CFX_ByteStringC byte_string_c1("he"); - CFX_ByteStringC byte_string_c2("hellp"); - CFX_ByteStringC byte_string_c3("hellod"); - EXPECT_FALSE(byte_string == byte_string_c1); - EXPECT_FALSE(byte_string == byte_string_c2); - EXPECT_FALSE(byte_string == byte_string_c3); - EXPECT_FALSE(byte_string_c1 == byte_string); - EXPECT_FALSE(byte_string_c2 == byte_string); - EXPECT_FALSE(byte_string_c3 == byte_string); - - const char* c_null_string = nullptr; - const char* c_empty_string = ""; - EXPECT_TRUE(null_string == c_null_string); - EXPECT_TRUE(null_string == c_empty_string); - EXPECT_TRUE(empty_string == c_null_string); - EXPECT_TRUE(empty_string == c_empty_string); - EXPECT_TRUE(deleted_string == c_null_string); - EXPECT_TRUE(deleted_string == c_empty_string); - EXPECT_TRUE(c_null_string == null_string); - EXPECT_TRUE(c_empty_string == null_string); - EXPECT_TRUE(c_null_string == empty_string); - EXPECT_TRUE(c_empty_string == empty_string); - EXPECT_TRUE(c_null_string == deleted_string); - EXPECT_TRUE(c_empty_string == deleted_string); - - const char* c_string_same1 = "hello"; - EXPECT_TRUE(byte_string == c_string_same1); - EXPECT_TRUE(c_string_same1 == byte_string); - - const char* c_string1 = "he"; - const char* c_string2 = "hellp"; - const char* c_string3 = "hellod"; - EXPECT_FALSE(byte_string == c_string1); - EXPECT_FALSE(byte_string == c_string2); - EXPECT_FALSE(byte_string == c_string3); - EXPECT_FALSE(c_string1 == byte_string); - EXPECT_FALSE(c_string2 == byte_string); - EXPECT_FALSE(c_string3 == byte_string); -} - -TEST(fxcrt, ByteStringOperatorNE) { - CFX_ByteString null_string; - EXPECT_FALSE(null_string != null_string); - - CFX_ByteString empty_string(""); - EXPECT_FALSE(empty_string != empty_string); - EXPECT_FALSE(empty_string != null_string); - EXPECT_FALSE(null_string != empty_string); - - CFX_ByteString deleted_string("hello"); - deleted_string.Delete(0, 5); - EXPECT_FALSE(deleted_string != deleted_string); - EXPECT_FALSE(deleted_string != null_string); - EXPECT_FALSE(deleted_string != empty_string); - EXPECT_FALSE(deleted_string != deleted_string); - EXPECT_FALSE(null_string != deleted_string); - EXPECT_FALSE(empty_string != deleted_string); - EXPECT_FALSE(deleted_string != deleted_string); - - CFX_ByteString byte_string("hello"); - EXPECT_FALSE(byte_string != byte_string); - EXPECT_TRUE(byte_string != null_string); - EXPECT_TRUE(byte_string != empty_string); - EXPECT_TRUE(byte_string != deleted_string); - EXPECT_TRUE(null_string != byte_string); - EXPECT_TRUE(empty_string != byte_string); - EXPECT_TRUE(deleted_string != byte_string); - - CFX_ByteString byte_string_same1("hello"); - EXPECT_FALSE(byte_string != byte_string_same1); - EXPECT_FALSE(byte_string_same1 != byte_string); - - CFX_ByteString byte_string_same2(byte_string); - EXPECT_FALSE(byte_string != byte_string_same2); - EXPECT_FALSE(byte_string_same2 != byte_string); - - CFX_ByteString byte_string1("he"); - CFX_ByteString byte_string2("hellp"); - CFX_ByteString byte_string3("hellod"); - EXPECT_TRUE(byte_string != byte_string1); - EXPECT_TRUE(byte_string != byte_string2); - EXPECT_TRUE(byte_string != byte_string3); - EXPECT_TRUE(byte_string1 != byte_string); - EXPECT_TRUE(byte_string2 != byte_string); - EXPECT_TRUE(byte_string3 != byte_string); - - CFX_ByteStringC null_string_c; - CFX_ByteStringC empty_string_c(""); - EXPECT_FALSE(null_string != null_string_c); - EXPECT_FALSE(null_string != empty_string_c); - EXPECT_FALSE(empty_string != null_string_c); - EXPECT_FALSE(empty_string != empty_string_c); - EXPECT_FALSE(null_string_c != null_string); - EXPECT_FALSE(empty_string_c != null_string); - EXPECT_FALSE(null_string_c != empty_string); - EXPECT_FALSE(empty_string_c != empty_string); - - CFX_ByteStringC byte_string_c_same1("hello"); - EXPECT_FALSE(byte_string != byte_string_c_same1); - EXPECT_FALSE(byte_string_c_same1 != byte_string); - - CFX_ByteStringC byte_string_c1("he"); - CFX_ByteStringC byte_string_c2("hellp"); - CFX_ByteStringC byte_string_c3("hellod"); - EXPECT_TRUE(byte_string != byte_string_c1); - EXPECT_TRUE(byte_string != byte_string_c2); - EXPECT_TRUE(byte_string != byte_string_c3); - EXPECT_TRUE(byte_string_c1 != byte_string); - EXPECT_TRUE(byte_string_c2 != byte_string); - EXPECT_TRUE(byte_string_c3 != byte_string); - - const char* c_null_string = nullptr; - const char* c_empty_string = ""; - EXPECT_FALSE(null_string != c_null_string); - EXPECT_FALSE(null_string != c_empty_string); - EXPECT_FALSE(empty_string != c_null_string); - EXPECT_FALSE(empty_string != c_empty_string); - EXPECT_FALSE(deleted_string != c_null_string); - EXPECT_FALSE(deleted_string != c_empty_string); - EXPECT_FALSE(c_null_string != null_string); - EXPECT_FALSE(c_empty_string != null_string); - EXPECT_FALSE(c_null_string != empty_string); - EXPECT_FALSE(c_empty_string != empty_string); - EXPECT_FALSE(c_null_string != deleted_string); - EXPECT_FALSE(c_empty_string != deleted_string); - - const char* c_string_same1 = "hello"; - EXPECT_FALSE(byte_string != c_string_same1); - EXPECT_FALSE(c_string_same1 != byte_string); - - const char* c_string1 = "he"; - const char* c_string2 = "hellp"; - const char* c_string3 = "hellod"; - EXPECT_TRUE(byte_string != c_string1); - EXPECT_TRUE(byte_string != c_string2); - EXPECT_TRUE(byte_string != c_string3); - EXPECT_TRUE(c_string1 != byte_string); - EXPECT_TRUE(c_string2 != byte_string); - EXPECT_TRUE(c_string3 != byte_string); -} - -TEST(fxcrt, ByteStringCNull) { - CFX_ByteStringC null_string; - EXPECT_FALSE(null_string.raw_str()); - EXPECT_EQ(null_string.GetLength(), 0); - EXPECT_TRUE(null_string.IsEmpty()); - - CFX_ByteStringC another_null_string; - EXPECT_EQ(null_string, another_null_string); - - CFX_ByteStringC copied_null_string(null_string); - EXPECT_FALSE(copied_null_string.raw_str()); - EXPECT_EQ(copied_null_string.GetLength(), 0); - EXPECT_TRUE(copied_null_string.IsEmpty()); - EXPECT_EQ(null_string, copied_null_string); - - CFX_ByteStringC empty_string(""); // Pointer to NUL, not NULL pointer. - EXPECT_TRUE(empty_string.raw_str()); - EXPECT_EQ(empty_string.GetLength(), 0); - EXPECT_TRUE(empty_string.IsEmpty()); - EXPECT_EQ(null_string, empty_string); - - CFX_ByteStringC assigned_null_string("initially not nullptr"); - assigned_null_string = null_string; - EXPECT_FALSE(assigned_null_string.raw_str()); - EXPECT_EQ(assigned_null_string.GetLength(), 0); - EXPECT_TRUE(assigned_null_string.IsEmpty()); - EXPECT_EQ(null_string, assigned_null_string); - - CFX_ByteStringC assigned_nullptr_string("initially not nullptr"); - assigned_nullptr_string = (const char*)nullptr; - EXPECT_FALSE(assigned_nullptr_string.raw_str()); - EXPECT_EQ(assigned_nullptr_string.GetLength(), 0); - EXPECT_TRUE(assigned_nullptr_string.IsEmpty()); - EXPECT_EQ(null_string, assigned_nullptr_string); - - CFX_ByteStringC non_null_string("a"); - EXPECT_NE(null_string, non_null_string); -} - -TEST(fxcrt, ByteStringConcat) { - CFX_ByteString fred; - fred.Concat("FRED", 4); - EXPECT_EQ("FRED", fred); - - fred.Concat("DY", 2); - EXPECT_EQ("FREDDY", fred); - - fred.Delete(3, 3); - EXPECT_EQ("FRE", fred); - - fred.Concat("D", 1); - EXPECT_EQ("FRED", fred); - - CFX_ByteString copy = fred; - fred.Concat("DY", 2); - EXPECT_EQ("FREDDY", fred); - EXPECT_EQ("FRED", copy); - - // Test invalid arguments. - copy = fred; - fred.Concat("freddy", -6); - CFX_ByteString not_aliased("xxxxxx"); - EXPECT_EQ("FREDDY", fred); - EXPECT_EQ("xxxxxx", not_aliased); -} - -TEST(fxcrt, ByteStringRemove) { - CFX_ByteString freed("FREED"); - freed.Remove('E'); - EXPECT_EQ("FRD", freed); - freed.Remove('F'); - EXPECT_EQ("RD", freed); - freed.Remove('D'); - EXPECT_EQ("R", freed); - freed.Remove('X'); - EXPECT_EQ("R", freed); - freed.Remove('R'); - EXPECT_EQ("", freed); - - CFX_ByteString empty; - empty.Remove('X'); - EXPECT_EQ("", empty); -} - -TEST(fxcrt, ByteStringRemoveCopies) { - CFX_ByteString freed("FREED"); - const char* old_buffer = freed.c_str(); - - // No change with single reference - no copy. - freed.Remove('Q'); - EXPECT_EQ("FREED", freed); - EXPECT_EQ(old_buffer, freed.c_str()); - - // Change with single reference - no copy. - freed.Remove('E'); - EXPECT_EQ("FRD", freed); - EXPECT_EQ(old_buffer, freed.c_str()); - - // No change with multiple references - no copy. - CFX_ByteString shared(freed); - freed.Remove('Q'); - EXPECT_EQ("FRD", freed); - EXPECT_EQ(old_buffer, freed.c_str()); - EXPECT_EQ(old_buffer, shared.c_str()); - - // Change with multiple references -- must copy. - freed.Remove('D'); - EXPECT_EQ("FR", freed); - EXPECT_NE(old_buffer, freed.c_str()); - EXPECT_EQ("FRD", shared); - EXPECT_EQ(old_buffer, shared.c_str()); -} - -TEST(fxcrt, ByteStringReplace) { - CFX_ByteString fred("FRED"); - fred.Replace("FR", "BL"); - EXPECT_EQ("BLED", fred); - fred.Replace("D", "DDY"); - EXPECT_EQ("BLEDDY", fred); - fred.Replace("LEDD", ""); - EXPECT_EQ("BY", fred); - fred.Replace("X", "CLAMS"); - EXPECT_EQ("BY", fred); - fred.Replace("BY", "HI"); - EXPECT_EQ("HI", fred); - fred.Replace("", "CLAMS"); - EXPECT_EQ("HI", fred); - fred.Replace("HI", ""); - EXPECT_EQ("", fred); -} - -TEST(fxcrt, ByteStringInsert) { - CFX_ByteString fred("FRED"); - fred.Insert(-1, 'X'); - EXPECT_EQ("XFRED", fred); - fred.Insert(0, 'S'); - EXPECT_EQ("SXFRED", fred); - fred.Insert(2, 'T'); - EXPECT_EQ("SXTFRED", fred); - fred.Insert(5, 'U'); - EXPECT_EQ("SXTFRUED", fred); - fred.Insert(8, 'V'); - EXPECT_EQ("SXTFRUEDV", fred); - fred.Insert(12, 'P'); - EXPECT_EQ("SXTFRUEDVP", fred); - { - CFX_ByteString empty; - empty.Insert(-1, 'X'); - EXPECT_EQ("X", empty); - } - { - CFX_ByteString empty; - empty.Insert(0, 'X'); - EXPECT_EQ("X", empty); - } - { - CFX_ByteString empty; - empty.Insert(5, 'X'); - EXPECT_EQ("X", empty); - } -} - -TEST(fxcrt, ByteStringDelete) { - CFX_ByteString fred("FRED"); - fred.Delete(0, 2); - EXPECT_EQ("ED", fred); - fred.Delete(1); - EXPECT_EQ("E", fred); - fred.Delete(-1); - EXPECT_EQ("", fred); - fred.Delete(1); - EXPECT_EQ("", fred); - - CFX_ByteString empty; - empty.Delete(0); - EXPECT_EQ("", empty); - empty.Delete(-1); - EXPECT_EQ("", empty); - empty.Delete(1); - EXPECT_EQ("", empty); -} - -TEST(fxcrt, ByteStringMid) { - CFX_ByteString fred("FRED"); - EXPECT_EQ("", fred.Mid(0, 0)); - EXPECT_EQ("", fred.Mid(3, 0)); - EXPECT_EQ("FRED", fred.Mid(0)); - EXPECT_EQ("RED", fred.Mid(1)); - EXPECT_EQ("ED", fred.Mid(2)); - EXPECT_EQ("D", fred.Mid(3)); - EXPECT_EQ("F", fred.Mid(0, 1)); - EXPECT_EQ("R", fred.Mid(1, 1)); - EXPECT_EQ("E", fred.Mid(2, 1)); - EXPECT_EQ("D", fred.Mid(3, 1)); - EXPECT_EQ("FR", fred.Mid(0, 2)); - EXPECT_EQ("FRED", fred.Mid(0, 4)); - EXPECT_EQ("FRED", fred.Mid(0, 10)); - - EXPECT_EQ("FR", fred.Mid(-1, 2)); - EXPECT_EQ("RED", fred.Mid(1, 4)); - EXPECT_EQ("", fred.Mid(4, 1)); - - CFX_ByteString empty; - EXPECT_EQ("", empty.Mid(0, 0)); - EXPECT_EQ("", empty.Mid(0)); - EXPECT_EQ("", empty.Mid(1)); - EXPECT_EQ("", empty.Mid(-1)); -} - -TEST(fxcrt, ByteStringLeft) { - CFX_ByteString fred("FRED"); - EXPECT_EQ("", fred.Left(0)); - EXPECT_EQ("F", fred.Left(1)); - EXPECT_EQ("FR", fred.Left(2)); - EXPECT_EQ("FRE", fred.Left(3)); - EXPECT_EQ("FRED", fred.Left(4)); - - EXPECT_EQ("FRED", fred.Left(5)); - EXPECT_EQ("", fred.Left(-1)); - - CFX_ByteString empty; - EXPECT_EQ("", empty.Left(0)); - EXPECT_EQ("", empty.Left(1)); - EXPECT_EQ("", empty.Left(-1)); -} - -TEST(fxcrt, ByteStringRight) { - CFX_ByteString fred("FRED"); - EXPECT_EQ("", fred.Right(0)); - EXPECT_EQ("D", fred.Right(1)); - EXPECT_EQ("ED", fred.Right(2)); - EXPECT_EQ("RED", fred.Right(3)); - EXPECT_EQ("FRED", fred.Right(4)); - - EXPECT_EQ("FRED", fred.Right(5)); - EXPECT_EQ("", fred.Right(-1)); - - CFX_ByteString empty; - EXPECT_EQ("", empty.Right(0)); - EXPECT_EQ("", empty.Right(1)); - EXPECT_EQ("", empty.Right(-1)); -} - -TEST(fxcrt, ByteStringUpperLower) { - CFX_ByteString fred("F-Re.42D"); - fred.MakeLower(); - EXPECT_EQ("f-re.42d", fred); - fred.MakeUpper(); - EXPECT_EQ("F-RE.42D", fred); - - CFX_ByteString empty; - empty.MakeLower(); - EXPECT_EQ("", empty); - empty.MakeUpper(); - EXPECT_EQ("", empty); -} - -TEST(fxcrt, ByteStringTrimRight) { - CFX_ByteString fred(" FRED "); - fred.TrimRight(); - EXPECT_EQ(" FRED", fred); - fred.TrimRight('E'); - EXPECT_EQ(" FRED", fred); - fred.TrimRight('D'); - EXPECT_EQ(" FRE", fred); - fred.TrimRight("ERP"); - EXPECT_EQ(" F", fred); - - CFX_ByteString blank(" "); - blank.TrimRight("ERP"); - EXPECT_EQ(" ", blank); - blank.TrimRight('E'); - EXPECT_EQ(" ", blank); - blank.TrimRight(); - EXPECT_EQ("", blank); - - CFX_ByteString empty; - empty.TrimRight("ERP"); - EXPECT_EQ("", empty); - empty.TrimRight('E'); - EXPECT_EQ("", empty); - empty.TrimRight(); - EXPECT_EQ("", empty); -} - -TEST(fxcrt, ByteStringTrimRightCopies) { - { - // With a single reference, no copy takes place. - CFX_ByteString fred(" FRED "); - const char* old_buffer = fred.c_str(); - fred.TrimRight(); - EXPECT_EQ(" FRED", fred); - EXPECT_EQ(old_buffer, fred.c_str()); - } - { - // With multiple references, we must copy. - CFX_ByteString fred(" FRED "); - CFX_ByteString other_fred = fred; - const char* old_buffer = fred.c_str(); - fred.TrimRight(); - EXPECT_EQ(" FRED", fred); - EXPECT_EQ(" FRED ", other_fred); - EXPECT_NE(old_buffer, fred.c_str()); - } - { - // With multiple references, but no modifications, no copy. - CFX_ByteString fred("FRED"); - CFX_ByteString other_fred = fred; - const char* old_buffer = fred.c_str(); - fred.TrimRight(); - EXPECT_EQ("FRED", fred); - EXPECT_EQ("FRED", other_fred); - EXPECT_EQ(old_buffer, fred.c_str()); - } -} - -TEST(fxcrt, ByteStringTrimLeft) { - CFX_ByteString fred(" FRED "); - fred.TrimLeft(); - EXPECT_EQ("FRED ", fred); - fred.TrimLeft('E'); - EXPECT_EQ("FRED ", fred); - fred.TrimLeft('F'); - EXPECT_EQ("RED ", fred); - fred.TrimLeft("ERP"); - EXPECT_EQ("D ", fred); - - CFX_ByteString blank(" "); - blank.TrimLeft("ERP"); - EXPECT_EQ(" ", blank); - blank.TrimLeft('E'); - EXPECT_EQ(" ", blank); - blank.TrimLeft(); - EXPECT_EQ("", blank); - - CFX_ByteString empty; - empty.TrimLeft("ERP"); - EXPECT_EQ("", empty); - empty.TrimLeft('E'); - EXPECT_EQ("", empty); - empty.TrimLeft(); - EXPECT_EQ("", empty); -} - -TEST(fxcrt, ByteStringTrimLeftCopies) { - { - // With a single reference, no copy takes place. - CFX_ByteString fred(" FRED "); - const char* old_buffer = fred.c_str(); - fred.TrimLeft(); - EXPECT_EQ("FRED ", fred); - EXPECT_EQ(old_buffer, fred.c_str()); - } - { - // With multiple references, we must copy. - CFX_ByteString fred(" FRED "); - CFX_ByteString other_fred = fred; - const char* old_buffer = fred.c_str(); - fred.TrimLeft(); - EXPECT_EQ("FRED ", fred); - EXPECT_EQ(" FRED ", other_fred); - EXPECT_NE(old_buffer, fred.c_str()); - } - { - // With multiple references, but no modifications, no copy. - CFX_ByteString fred("FRED"); - CFX_ByteString other_fred = fred; - const char* old_buffer = fred.c_str(); - fred.TrimLeft(); - EXPECT_EQ("FRED", fred); - EXPECT_EQ("FRED", other_fred); - EXPECT_EQ(old_buffer, fred.c_str()); - } -} - -TEST(fxcrt, ByteStringReserve) { - { - CFX_ByteString str; - str.Reserve(6); - const char* old_buffer = str.c_str(); - str += "ABCDEF"; - EXPECT_EQ(old_buffer, str.c_str()); - str += "Blah Blah Blah Blah Blah Blah"; - EXPECT_NE(old_buffer, str.c_str()); - } - { - CFX_ByteString str("A"); - str.Reserve(6); - const char* old_buffer = str.c_str(); - str += "BCDEF"; - EXPECT_EQ(old_buffer, str.c_str()); - str += "Blah Blah Blah Blah Blah Blah"; - EXPECT_NE(old_buffer, str.c_str()); - } -} - -TEST(fxcrt, ByteStringGetBuffer) { - { - CFX_ByteString str; - char* buffer = str.GetBuffer(12); - // NOLINTNEXTLINE(runtime/printf) - strcpy(buffer, "clams"); - str.ReleaseBuffer(); - EXPECT_EQ("clams", str); - } - { - CFX_ByteString str("cl"); - char* buffer = str.GetBuffer(12); - // NOLINTNEXTLINE(runtime/printf) - strcpy(buffer + 2, "ams"); - str.ReleaseBuffer(); - EXPECT_EQ("clams", str); - } -} - -TEST(fxcrt, ByteStringReleaseBuffer) { - { - CFX_ByteString str; - str.Reserve(12); - str += "clams"; - const char* old_buffer = str.c_str(); - str.ReleaseBuffer(4); - EXPECT_EQ(old_buffer, str.c_str()); - EXPECT_EQ("clam", str); - } - { - CFX_ByteString str("c"); - str.Reserve(12); - str += "lams"; - const char* old_buffer = str.c_str(); - str.ReleaseBuffer(4); - EXPECT_EQ(old_buffer, str.c_str()); - EXPECT_EQ("clam", str); - } - { - CFX_ByteString str; - str.Reserve(200); - str += "clams"; - const char* old_buffer = str.c_str(); - str.ReleaseBuffer(4); - EXPECT_NE(old_buffer, str.c_str()); - EXPECT_EQ("clam", str); - } - { - CFX_ByteString str("c"); - str.Reserve(200); - str += "lams"; - const char* old_buffer = str.c_str(); - str.ReleaseBuffer(4); - EXPECT_NE(old_buffer, str.c_str()); - EXPECT_EQ("clam", str); - } -} - -TEST(fxcrt, ByteStringCNotNull) { - CFX_ByteStringC string3("abc"); - CFX_ByteStringC string6("abcdef"); - CFX_ByteStringC alternate_string3("abcdef", 3); - CFX_ByteStringC embedded_nul_string7("abc\0def", 7); - CFX_ByteStringC illegal_string7("abcdef", 7); - - EXPECT_EQ(3, string3.GetLength()); - EXPECT_EQ(6, string6.GetLength()); - EXPECT_EQ(3, alternate_string3.GetLength()); - EXPECT_EQ(7, embedded_nul_string7.GetLength()); - EXPECT_EQ(7, illegal_string7.GetLength()); - - EXPECT_NE(string3, string6); - EXPECT_EQ(string3, alternate_string3); - EXPECT_NE(string3, embedded_nul_string7); - EXPECT_NE(string3, illegal_string7); - EXPECT_NE(string6, alternate_string3); - EXPECT_NE(string6, embedded_nul_string7); - EXPECT_NE(string6, illegal_string7); - EXPECT_NE(alternate_string3, embedded_nul_string7); - EXPECT_NE(alternate_string3, illegal_string7); - EXPECT_NE(embedded_nul_string7, illegal_string7); - - CFX_ByteStringC copied_string3(string3); - CFX_ByteStringC copied_alternate_string3(alternate_string3); - CFX_ByteStringC copied_embedded_nul_string7(embedded_nul_string7); - - EXPECT_EQ(string3, copied_string3); - EXPECT_EQ(alternate_string3, copied_alternate_string3); - EXPECT_EQ(embedded_nul_string7, copied_embedded_nul_string7); - - CFX_ByteStringC assigned_string3("intially something else"); - CFX_ByteStringC assigned_alternate_string3("initally something else"); - CFX_ByteStringC assigned_ptr_string3("initially something else"); - CFX_ByteStringC assigned_embedded_nul_string7("initially something else"); - - assigned_string3 = string3; - assigned_alternate_string3 = alternate_string3; - assigned_ptr_string3 = "abc"; - assigned_embedded_nul_string7 = embedded_nul_string7; - EXPECT_EQ(string3, assigned_string3); - EXPECT_EQ(alternate_string3, assigned_alternate_string3); - EXPECT_EQ(alternate_string3, assigned_ptr_string3); - EXPECT_EQ(embedded_nul_string7, assigned_embedded_nul_string7); -} - -TEST(fxcrt, ByteStringCFromChar) { - CFX_ByteStringC null_string; - CFX_ByteStringC lower_a_string("a"); - - // Must have lvalues that outlive the corresponding ByteStringC. - char nul = '\0'; - char lower_a = 'a'; - CFX_ByteStringC nul_string_from_char(nul); - CFX_ByteStringC lower_a_string_from_char(lower_a); - - // Pointer to nul, not nullptr ptr, hence length 1 ... - EXPECT_EQ(1, nul_string_from_char.GetLength()); - EXPECT_NE(null_string, nul_string_from_char); - - EXPECT_EQ(1, lower_a_string_from_char.GetLength()); - EXPECT_EQ(lower_a_string, lower_a_string_from_char); - EXPECT_NE(nul_string_from_char, lower_a_string_from_char); - - CFX_ByteStringC longer_string("ab"); - EXPECT_NE(longer_string, lower_a_string_from_char); -} - -TEST(fxcrt, ByteStringCGetID) { - CFX_ByteStringC null_string; - EXPECT_EQ(0u, null_string.GetID()); - EXPECT_EQ(0u, null_string.GetID(1)); - EXPECT_EQ(0u, null_string.GetID(-1)); - EXPECT_EQ(0u, null_string.GetID(-1000000)); - - CFX_ByteStringC empty_string(""); - EXPECT_EQ(0u, empty_string.GetID()); - EXPECT_EQ(0u, empty_string.GetID(1)); - EXPECT_EQ(0u, empty_string.GetID(-1)); - EXPECT_EQ(0u, empty_string.GetID(-1000000)); - - CFX_ByteStringC short_string("ab"); - EXPECT_EQ(FXBSTR_ID('a', 'b', 0, 0), short_string.GetID()); - EXPECT_EQ(FXBSTR_ID('b', 0, 0, 0), short_string.GetID(1)); - EXPECT_EQ(0u, short_string.GetID(2)); - EXPECT_EQ(0u, short_string.GetID(-1)); - EXPECT_EQ(0u, short_string.GetID(-1000000)); - - CFX_ByteStringC longer_string("abcdef"); - EXPECT_EQ(FXBSTR_ID('a', 'b', 'c', 'd'), longer_string.GetID()); - EXPECT_EQ(FXBSTR_ID('b', 'c', 'd', 'e'), longer_string.GetID(1)); - EXPECT_EQ(FXBSTR_ID('c', 'd', 'e', 'f'), longer_string.GetID(2)); - EXPECT_EQ(FXBSTR_ID('d', 'e', 'f', 0), longer_string.GetID(3)); - EXPECT_EQ(FXBSTR_ID('e', 'f', 0, 0), longer_string.GetID(4)); - EXPECT_EQ(FXBSTR_ID('f', 0, 0, 0), longer_string.GetID(5)); - EXPECT_EQ(0u, longer_string.GetID(6)); - EXPECT_EQ(0u, longer_string.GetID(-1)); - EXPECT_EQ(0u, longer_string.GetID(-1000000)); -} - -TEST(fxcrt, ByteStringCFind) { - CFX_ByteStringC null_string; - EXPECT_EQ(-1, null_string.Find('a')); - EXPECT_EQ(-1, null_string.Find(0)); - - CFX_ByteStringC empty_string(""); - EXPECT_EQ(-1, empty_string.Find('a')); - EXPECT_EQ(-1, empty_string.Find(0)); - - CFX_ByteStringC single_string("a"); - EXPECT_EQ(0, single_string.Find('a')); - EXPECT_EQ(-1, single_string.Find('b')); - EXPECT_EQ(-1, single_string.Find(0)); - - CFX_ByteStringC longer_string("abccc"); - EXPECT_EQ(0, longer_string.Find('a')); - EXPECT_EQ(2, longer_string.Find('c')); - EXPECT_EQ(-1, longer_string.Find(0)); - - CFX_ByteStringC hibyte_string( - "ab\x8c" - "def"); - EXPECT_EQ(2, hibyte_string.Find('\x8c')); -} - -TEST(fxcrt, ByteStringCMid) { - CFX_ByteStringC null_string; - EXPECT_EQ(null_string, null_string.Mid(0, 1)); - EXPECT_EQ(null_string, null_string.Mid(1, 1)); - - CFX_ByteStringC empty_string(""); - EXPECT_EQ(empty_string, empty_string.Mid(0, 1)); - EXPECT_EQ(empty_string, empty_string.Mid(1, 1)); - - CFX_ByteStringC single_character("a"); - EXPECT_EQ(empty_string, single_character.Mid(0, 0)); - EXPECT_EQ(single_character, single_character.Mid(0, 1)); - EXPECT_EQ(empty_string, single_character.Mid(1, 0)); - EXPECT_EQ(empty_string, single_character.Mid(1, 1)); - - CFX_ByteStringC longer_string("abcdef"); - EXPECT_EQ(longer_string, longer_string.Mid(0, 6)); - EXPECT_EQ(longer_string, longer_string.Mid(0, 187)); - EXPECT_EQ(longer_string, longer_string.Mid(-42, 6)); - EXPECT_EQ(longer_string, longer_string.Mid(-42, 187)); - - CFX_ByteStringC leading_substring("ab"); - EXPECT_EQ(leading_substring, longer_string.Mid(0, 2)); - EXPECT_EQ(leading_substring, longer_string.Mid(-1, 2)); - - CFX_ByteStringC middle_substring("bcde"); - EXPECT_EQ(middle_substring, longer_string.Mid(1, 4)); - - CFX_ByteStringC trailing_substring("ef"); - EXPECT_EQ(trailing_substring, longer_string.Mid(4, 2)); - EXPECT_EQ(trailing_substring, longer_string.Mid(4, 3)); -} - -TEST(fxcrt, ByteStringCGetAt) { - CFX_ByteString short_string("a"); - CFX_ByteString longer_string("abc"); - CFX_ByteString embedded_nul_string("ab\0c", 4); - - EXPECT_EQ('a', short_string.GetAt(0)); - EXPECT_EQ('c', longer_string.GetAt(2)); - EXPECT_EQ('b', embedded_nul_string.GetAt(1)); - EXPECT_EQ('\0', embedded_nul_string.GetAt(2)); - EXPECT_EQ('c', embedded_nul_string.GetAt(3)); -} - -TEST(fxcrt, ByteStringCOperatorSubscript) { - // CFX_ByteStringC includes the NUL terminator for non-empty strings. - CFX_ByteStringC abc("abc"); - EXPECT_EQ('a', abc[0]); - EXPECT_EQ('b', abc[1]); - EXPECT_EQ('c', abc[2]); - EXPECT_EQ(0, abc[3]); -} - -TEST(fxcrt, ByteStringCOperatorLT) { - CFX_ByteStringC empty; - CFX_ByteStringC a("a"); - CFX_ByteStringC abc("abc"); - CFX_ByteStringC def("def"); - - EXPECT_FALSE(empty < empty); - EXPECT_FALSE(a < a); - EXPECT_FALSE(abc < abc); - EXPECT_FALSE(def < def); - - EXPECT_TRUE(empty < a); - EXPECT_FALSE(a < empty); - - EXPECT_TRUE(empty < abc); - EXPECT_FALSE(abc < empty); - - EXPECT_TRUE(empty < def); - EXPECT_FALSE(def < empty); - - EXPECT_TRUE(a < abc); - EXPECT_FALSE(abc < a); - - EXPECT_TRUE(a < def); - EXPECT_FALSE(def < a); - - EXPECT_TRUE(abc < def); - EXPECT_FALSE(def < abc); -} - -TEST(fxcrt, ByteStringCOperatorEQ) { - CFX_ByteStringC byte_string_c("hello"); - EXPECT_TRUE(byte_string_c == byte_string_c); - - CFX_ByteStringC byte_string_c_same1("hello"); - EXPECT_TRUE(byte_string_c == byte_string_c_same1); - EXPECT_TRUE(byte_string_c_same1 == byte_string_c); - - CFX_ByteStringC byte_string_c_same2(byte_string_c); - EXPECT_TRUE(byte_string_c == byte_string_c_same2); - EXPECT_TRUE(byte_string_c_same2 == byte_string_c); - - CFX_ByteStringC byte_string_c1("he"); - CFX_ByteStringC byte_string_c2("hellp"); - CFX_ByteStringC byte_string_c3("hellod"); - EXPECT_FALSE(byte_string_c == byte_string_c1); - EXPECT_FALSE(byte_string_c == byte_string_c2); - EXPECT_FALSE(byte_string_c == byte_string_c3); - EXPECT_FALSE(byte_string_c1 == byte_string_c); - EXPECT_FALSE(byte_string_c2 == byte_string_c); - EXPECT_FALSE(byte_string_c3 == byte_string_c); - - CFX_ByteString byte_string_same1("hello"); - EXPECT_TRUE(byte_string_c == byte_string_same1); - EXPECT_TRUE(byte_string_same1 == byte_string_c); - - CFX_ByteString byte_string1("he"); - CFX_ByteString byte_string2("hellp"); - CFX_ByteString byte_string3("hellod"); - EXPECT_FALSE(byte_string_c == byte_string1); - EXPECT_FALSE(byte_string_c == byte_string2); - EXPECT_FALSE(byte_string_c == byte_string3); - EXPECT_FALSE(byte_string1 == byte_string_c); - EXPECT_FALSE(byte_string2 == byte_string_c); - EXPECT_FALSE(byte_string3 == byte_string_c); - - const char* c_string_same1 = "hello"; - EXPECT_TRUE(byte_string_c == c_string_same1); - EXPECT_TRUE(c_string_same1 == byte_string_c); - - const char* c_string1 = "he"; - const char* c_string2 = "hellp"; - const char* c_string3 = "hellod"; - EXPECT_FALSE(byte_string_c == c_string1); - EXPECT_FALSE(byte_string_c == c_string2); - EXPECT_FALSE(byte_string_c == c_string3); - - EXPECT_FALSE(c_string1 == byte_string_c); - EXPECT_FALSE(c_string2 == byte_string_c); - EXPECT_FALSE(c_string3 == byte_string_c); -} - -TEST(fxcrt, ByteStringCOperatorNE) { - CFX_ByteStringC byte_string_c("hello"); - EXPECT_FALSE(byte_string_c != byte_string_c); - - CFX_ByteStringC byte_string_c_same1("hello"); - EXPECT_FALSE(byte_string_c != byte_string_c_same1); - EXPECT_FALSE(byte_string_c_same1 != byte_string_c); - - CFX_ByteStringC byte_string_c_same2(byte_string_c); - EXPECT_FALSE(byte_string_c != byte_string_c_same2); - EXPECT_FALSE(byte_string_c_same2 != byte_string_c); - - CFX_ByteStringC byte_string_c1("he"); - CFX_ByteStringC byte_string_c2("hellp"); - CFX_ByteStringC byte_string_c3("hellod"); - EXPECT_TRUE(byte_string_c != byte_string_c1); - EXPECT_TRUE(byte_string_c != byte_string_c2); - EXPECT_TRUE(byte_string_c != byte_string_c3); - EXPECT_TRUE(byte_string_c1 != byte_string_c); - EXPECT_TRUE(byte_string_c2 != byte_string_c); - EXPECT_TRUE(byte_string_c3 != byte_string_c); - - CFX_ByteString byte_string_same1("hello"); - EXPECT_FALSE(byte_string_c != byte_string_same1); - EXPECT_FALSE(byte_string_same1 != byte_string_c); - - CFX_ByteString byte_string1("he"); - CFX_ByteString byte_string2("hellp"); - CFX_ByteString byte_string3("hellod"); - EXPECT_TRUE(byte_string_c != byte_string1); - EXPECT_TRUE(byte_string_c != byte_string2); - EXPECT_TRUE(byte_string_c != byte_string3); - EXPECT_TRUE(byte_string1 != byte_string_c); - EXPECT_TRUE(byte_string2 != byte_string_c); - EXPECT_TRUE(byte_string3 != byte_string_c); - - const char* c_string_same1 = "hello"; - EXPECT_FALSE(byte_string_c != c_string_same1); - EXPECT_FALSE(c_string_same1 != byte_string_c); - - const char* c_string1 = "he"; - const char* c_string2 = "hellp"; - const char* c_string3 = "hellod"; - EXPECT_TRUE(byte_string_c != c_string1); - EXPECT_TRUE(byte_string_c != c_string2); - EXPECT_TRUE(byte_string_c != c_string3); - - EXPECT_TRUE(c_string1 != byte_string_c); - EXPECT_TRUE(c_string2 != byte_string_c); - EXPECT_TRUE(c_string3 != byte_string_c); -} - -TEST(fxcrt, ByteStringFormatWidth) { - { - CFX_ByteString str; - str.Format("%5d", 1); - EXPECT_EQ(" 1", str); - } - - { - CFX_ByteString str; - str.Format("%d", 1); - EXPECT_EQ("1", str); - } - - { - CFX_ByteString str; - str.Format("%*d", 5, 1); - EXPECT_EQ(" 1", str); - } - - { - CFX_ByteString str; - str.Format("%-1d", 1); - EXPECT_EQ("1", str); - } - - { - CFX_ByteString str; - str.Format("%0d", 1); - EXPECT_EQ("1", str); - } -} - -TEST(fxcrt, ByteStringFormatPrecision) { - { - CFX_ByteString str; - str.Format("%.2f", 1.12345); - EXPECT_EQ("1.12", str); - } - - { - CFX_ByteString str; - str.Format("%.*f", 3, 1.12345); - EXPECT_EQ("1.123", str); - } - - { - CFX_ByteString str; - str.Format("%f", 1.12345); - EXPECT_EQ("1.123450", str); - } - - { - CFX_ByteString str; - str.Format("%-1f", 1.12345); - EXPECT_EQ("1.123450", str); - } - - { - CFX_ByteString str; - str.Format("%0f", 1.12345); - EXPECT_EQ("1.123450", str); - } -} - -TEST(fxcrt, EmptyByteString) { - CFX_ByteString empty_str; - EXPECT_TRUE(empty_str.IsEmpty()); - EXPECT_EQ(0, empty_str.GetLength()); - const char* cstr = empty_str.c_str(); - EXPECT_EQ(0, FXSYS_strlen(cstr)); -} diff --git a/core/fxcrt/fx_basic_util.cpp b/core/fxcrt/fx_basic_util.cpp index 9f699e4da7..58d54a3b10 100644 --- a/core/fxcrt/fx_basic_util.cpp +++ b/core/fxcrt/fx_basic_util.cpp @@ -123,6 +123,10 @@ float FX_atof(const CFX_ByteStringC& strc) { return bNegative ? -value : value; } +float FX_atof(const CFX_WideStringC& wsStr) { + return FX_atof(FX_UTF8Encode(wsStr).c_str()); +} + #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ && _MSC_VER < 1900 void FXSYS_snprintf(char* str, size_t size, diff --git a/core/fxcrt/fx_basic_wstring.cpp b/core/fxcrt/fx_basic_wstring.cpp deleted file mode 100644 index 99002e581f..0000000000 --- a/core/fxcrt/fx_basic_wstring.cpp +++ /dev/null @@ -1,1042 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include - -#include -#include - -#include "core/fxcrt/cfx_string_pool_template.h" -#include "core/fxcrt/fx_basic.h" -#include "core/fxcrt/fx_ext.h" -#include "third_party/base/numerics/safe_math.h" -#include "third_party/base/stl_util.h" - -template class CFX_StringDataTemplate; -template class CFX_StringCTemplate; -template class CFX_StringPoolTemplate; -template struct std::hash; - -#define FORCE_ANSI 0x10000 -#define FORCE_UNICODE 0x20000 -#define FORCE_INT64 0x40000 - -namespace { - -#ifndef NDEBUG -bool IsValidCodePage(uint16_t codepage) { - switch (codepage) { - case 0: - case 932: - case 936: - case 949: - case 950: - return true; - - default: - return false; - } -} -#endif - -const wchar_t* FX_wcsstr(const wchar_t* haystack, - int haystack_len, - const wchar_t* needle, - int needle_len) { - if (needle_len > haystack_len || needle_len == 0) { - return nullptr; - } - const wchar_t* end_ptr = haystack + haystack_len - needle_len; - while (haystack <= end_ptr) { - int i = 0; - while (1) { - if (haystack[i] != needle[i]) { - break; - } - i++; - if (i == needle_len) { - return haystack; - } - } - haystack++; - } - return nullptr; -} - -FX_STRSIZE GuessSizeForVSWPrintf(const wchar_t* pFormat, va_list argList) { - FX_STRSIZE nMaxLen = 0; - for (const wchar_t* pStr = pFormat; *pStr != 0; pStr++) { - if (*pStr != '%' || *(pStr = pStr + 1) == '%') { - ++nMaxLen; - continue; - } - int nItemLen = 0; - int nWidth = 0; - for (; *pStr != 0; pStr++) { - if (*pStr == '#') { - nMaxLen += 2; - } else if (*pStr == '*') { - nWidth = va_arg(argList, int); - } else if (*pStr != '-' && *pStr != '+' && *pStr != '0' && *pStr != ' ') { - break; - } - } - if (nWidth == 0) { - nWidth = FXSYS_wtoi(pStr); - while (std::iswdigit(*pStr)) - ++pStr; - } - if (nWidth < 0 || nWidth > 128 * 1024) - return -1; - int nPrecision = 0; - if (*pStr == '.') { - pStr++; - if (*pStr == '*') { - nPrecision = va_arg(argList, int); - pStr++; - } else { - nPrecision = FXSYS_wtoi(pStr); - while (std::iswdigit(*pStr)) - ++pStr; - } - } - if (nPrecision < 0 || nPrecision > 128 * 1024) - return -1; - int nModifier = 0; - if (*pStr == L'I' && *(pStr + 1) == L'6' && *(pStr + 2) == L'4') { - pStr += 3; - nModifier = FORCE_INT64; - } else { - switch (*pStr) { - case 'h': - nModifier = FORCE_ANSI; - pStr++; - break; - case 'l': - nModifier = FORCE_UNICODE; - pStr++; - break; - case 'F': - case 'N': - case 'L': - pStr++; - break; - } - } - switch (*pStr | nModifier) { - case 'c': - case 'C': - nItemLen = 2; - va_arg(argList, int); - break; - case 'c' | FORCE_ANSI: - case 'C' | FORCE_ANSI: - nItemLen = 2; - va_arg(argList, int); - break; - case 'c' | FORCE_UNICODE: - case 'C' | FORCE_UNICODE: - nItemLen = 2; - va_arg(argList, int); - break; - case 's': { - const wchar_t* pstrNextArg = va_arg(argList, const wchar_t*); - if (pstrNextArg) { - nItemLen = FXSYS_wcslen(pstrNextArg); - if (nItemLen < 1) { - nItemLen = 1; - } - } else { - nItemLen = 6; - } - } break; - case 'S': { - const char* pstrNextArg = va_arg(argList, const char*); - if (pstrNextArg) { - nItemLen = FXSYS_strlen(pstrNextArg); - if (nItemLen < 1) { - nItemLen = 1; - } - } else { - nItemLen = 6; - } - } break; - case 's' | FORCE_ANSI: - case 'S' | FORCE_ANSI: { - const char* pstrNextArg = va_arg(argList, const char*); - if (pstrNextArg) { - nItemLen = FXSYS_strlen(pstrNextArg); - if (nItemLen < 1) { - nItemLen = 1; - } - } else { - nItemLen = 6; - } - } break; - case 's' | FORCE_UNICODE: - case 'S' | FORCE_UNICODE: { - const wchar_t* pstrNextArg = va_arg(argList, wchar_t*); - if (pstrNextArg) { - nItemLen = FXSYS_wcslen(pstrNextArg); - if (nItemLen < 1) { - nItemLen = 1; - } - } else { - nItemLen = 6; - } - } break; - } - if (nItemLen != 0) { - if (nPrecision != 0 && nItemLen > nPrecision) { - nItemLen = nPrecision; - } - if (nItemLen < nWidth) { - nItemLen = nWidth; - } - } else { - switch (*pStr) { - case 'd': - case 'i': - case 'u': - case 'x': - case 'X': - case 'o': - if (nModifier & FORCE_INT64) { - va_arg(argList, int64_t); - } else { - va_arg(argList, int); - } - nItemLen = 32; - if (nItemLen < nWidth + nPrecision) { - nItemLen = nWidth + nPrecision; - } - break; - case 'a': - case 'A': - case 'e': - case 'E': - case 'g': - case 'G': - va_arg(argList, double); - nItemLen = 128; - if (nItemLen < nWidth + nPrecision) { - nItemLen = nWidth + nPrecision; - } - break; - case 'f': - if (nWidth + nPrecision > 100) { - nItemLen = nPrecision + nWidth + 128; - } else { - double f; - char pszTemp[256]; - f = va_arg(argList, double); - FXSYS_snprintf(pszTemp, sizeof(pszTemp), "%*.*f", nWidth, - nPrecision + 6, f); - nItemLen = FXSYS_strlen(pszTemp); - } - break; - case 'p': - va_arg(argList, void*); - nItemLen = 32; - if (nItemLen < nWidth + nPrecision) { - nItemLen = nWidth + nPrecision; - } - break; - case 'n': - va_arg(argList, int*); - break; - } - } - nMaxLen += nItemLen; - } - nMaxLen += 32; // Fudge factor. - return nMaxLen; -} - -} // namespace - -static_assert(sizeof(CFX_WideString) <= sizeof(wchar_t*), - "Strings must not require more space than pointers"); - -CFX_WideString::CFX_WideString() {} - -CFX_WideString::CFX_WideString(const CFX_WideString& other) - : m_pData(other.m_pData) {} - -CFX_WideString::CFX_WideString(CFX_WideString&& other) noexcept { - m_pData.Swap(other.m_pData); -} - -CFX_WideString::CFX_WideString(const wchar_t* pStr, FX_STRSIZE nLen) { - if (nLen < 0) - nLen = pStr ? FXSYS_wcslen(pStr) : 0; - - if (nLen) - m_pData.Reset(StringData::Create(pStr, nLen)); -} - -CFX_WideString::CFX_WideString(wchar_t ch) { - m_pData.Reset(StringData::Create(1)); - m_pData->m_String[0] = ch; -} - -CFX_WideString::CFX_WideString(const wchar_t* ptr) - : CFX_WideString(ptr, ptr ? FXSYS_wcslen(ptr) : 0) {} - -CFX_WideString::CFX_WideString(const CFX_WideStringC& stringSrc) { - if (!stringSrc.IsEmpty()) { - m_pData.Reset(StringData::Create(stringSrc.c_str(), stringSrc.GetLength())); - } -} - -CFX_WideString::CFX_WideString(const CFX_WideStringC& str1, - const CFX_WideStringC& str2) { - int nNewLen = str1.GetLength() + str2.GetLength(); - if (nNewLen == 0) - return; - - m_pData.Reset(StringData::Create(nNewLen)); - m_pData->CopyContents(str1.c_str(), str1.GetLength()); - m_pData->CopyContentsAt(str1.GetLength(), str2.c_str(), str2.GetLength()); -} - -CFX_WideString::~CFX_WideString() {} - -const CFX_WideString& CFX_WideString::operator=(const wchar_t* pStr) { - if (!pStr || !pStr[0]) - clear(); - else - AssignCopy(pStr, FXSYS_wcslen(pStr)); - - return *this; -} - -const CFX_WideString& CFX_WideString::operator=( - const CFX_WideStringC& stringSrc) { - if (stringSrc.IsEmpty()) - clear(); - else - AssignCopy(stringSrc.c_str(), stringSrc.GetLength()); - - return *this; -} - -const CFX_WideString& CFX_WideString::operator=( - const CFX_WideString& stringSrc) { - if (m_pData != stringSrc.m_pData) - m_pData = stringSrc.m_pData; - - return *this; -} - -const CFX_WideString& CFX_WideString::operator+=(const wchar_t* pStr) { - if (pStr) - Concat(pStr, FXSYS_wcslen(pStr)); - - return *this; -} - -const CFX_WideString& CFX_WideString::operator+=(wchar_t ch) { - Concat(&ch, 1); - return *this; -} - -const CFX_WideString& CFX_WideString::operator+=(const CFX_WideString& str) { - if (str.m_pData) - Concat(str.m_pData->m_String, str.m_pData->m_nDataLength); - - return *this; -} - -const CFX_WideString& CFX_WideString::operator+=(const CFX_WideStringC& str) { - if (!str.IsEmpty()) - Concat(str.c_str(), str.GetLength()); - - return *this; -} - -bool CFX_WideString::operator==(const wchar_t* ptr) const { - if (!m_pData) - return !ptr || !ptr[0]; - - if (!ptr) - return m_pData->m_nDataLength == 0; - - return wcslen(ptr) == static_cast(m_pData->m_nDataLength) && - wmemcmp(ptr, m_pData->m_String, m_pData->m_nDataLength) == 0; -} - -bool CFX_WideString::operator==(const CFX_WideStringC& str) const { - if (!m_pData) - return str.IsEmpty(); - - return m_pData->m_nDataLength == str.GetLength() && - wmemcmp(m_pData->m_String, str.c_str(), str.GetLength()) == 0; -} - -bool CFX_WideString::operator==(const CFX_WideString& other) const { - if (m_pData == other.m_pData) - return true; - - if (IsEmpty()) - return other.IsEmpty(); - - if (other.IsEmpty()) - return false; - - return other.m_pData->m_nDataLength == m_pData->m_nDataLength && - wmemcmp(other.m_pData->m_String, m_pData->m_String, - m_pData->m_nDataLength) == 0; -} - -bool CFX_WideString::operator<(const CFX_WideString& str) const { - if (m_pData == str.m_pData) - return false; - - int result = - wmemcmp(c_str(), str.c_str(), std::min(GetLength(), str.GetLength())); - return result < 0 || (result == 0 && GetLength() < str.GetLength()); -} - -void CFX_WideString::AssignCopy(const wchar_t* pSrcData, FX_STRSIZE nSrcLen) { - AllocBeforeWrite(nSrcLen); - m_pData->CopyContents(pSrcData, nSrcLen); - m_pData->m_nDataLength = nSrcLen; -} - -void CFX_WideString::ReallocBeforeWrite(FX_STRSIZE nNewLength) { - if (m_pData && m_pData->CanOperateInPlace(nNewLength)) - return; - - if (nNewLength <= 0) { - clear(); - return; - } - - CFX_RetainPtr pNewData(StringData::Create(nNewLength)); - if (m_pData) { - FX_STRSIZE nCopyLength = std::min(m_pData->m_nDataLength, nNewLength); - pNewData->CopyContents(m_pData->m_String, nCopyLength); - pNewData->m_nDataLength = nCopyLength; - } else { - pNewData->m_nDataLength = 0; - } - pNewData->m_String[pNewData->m_nDataLength] = 0; - m_pData.Swap(pNewData); -} - -void CFX_WideString::AllocBeforeWrite(FX_STRSIZE nNewLength) { - if (m_pData && m_pData->CanOperateInPlace(nNewLength)) - return; - - if (nNewLength <= 0) { - clear(); - return; - } - - m_pData.Reset(StringData::Create(nNewLength)); -} - -void CFX_WideString::ReleaseBuffer(FX_STRSIZE nNewLength) { - if (!m_pData) - return; - - if (nNewLength == -1) - nNewLength = FXSYS_wcslen(m_pData->m_String); - - nNewLength = std::min(nNewLength, m_pData->m_nAllocLength); - if (nNewLength == 0) { - clear(); - return; - } - - ASSERT(m_pData->m_nRefs == 1); - m_pData->m_nDataLength = nNewLength; - m_pData->m_String[nNewLength] = 0; - if (m_pData->m_nAllocLength - nNewLength >= 32) { - // Over arbitrary threshold, so pay the price to relocate. Force copy to - // always occur by holding a second reference to the string. - CFX_WideString preserve(*this); - ReallocBeforeWrite(nNewLength); - } -} - -void CFX_WideString::Reserve(FX_STRSIZE len) { - GetBuffer(len); -} - -wchar_t* CFX_WideString::GetBuffer(FX_STRSIZE nMinBufLength) { - if (!m_pData) { - if (nMinBufLength == 0) - return nullptr; - - m_pData.Reset(StringData::Create(nMinBufLength)); - m_pData->m_nDataLength = 0; - m_pData->m_String[0] = 0; - return m_pData->m_String; - } - - if (m_pData->CanOperateInPlace(nMinBufLength)) - return m_pData->m_String; - - nMinBufLength = std::max(nMinBufLength, m_pData->m_nDataLength); - if (nMinBufLength == 0) - return nullptr; - - CFX_RetainPtr pNewData(StringData::Create(nMinBufLength)); - pNewData->CopyContents(*m_pData); - pNewData->m_nDataLength = m_pData->m_nDataLength; - m_pData.Swap(pNewData); - return m_pData->m_String; -} - -FX_STRSIZE CFX_WideString::Delete(FX_STRSIZE nIndex, FX_STRSIZE nCount) { - if (!m_pData) - return 0; - - if (nIndex < 0) - nIndex = 0; - - FX_STRSIZE nOldLength = m_pData->m_nDataLength; - if (nCount > 0 && nIndex < nOldLength) { - FX_STRSIZE mLength = nIndex + nCount; - if (mLength >= nOldLength) { - m_pData->m_nDataLength = nIndex; - return m_pData->m_nDataLength; - } - ReallocBeforeWrite(nOldLength); - int nCharsToCopy = nOldLength - mLength + 1; - wmemmove(m_pData->m_String + nIndex, m_pData->m_String + mLength, - nCharsToCopy); - m_pData->m_nDataLength = nOldLength - nCount; - } - return m_pData->m_nDataLength; -} - -void CFX_WideString::Concat(const wchar_t* pSrcData, FX_STRSIZE nSrcLen) { - if (!pSrcData || nSrcLen <= 0) - return; - - if (!m_pData) { - m_pData.Reset(StringData::Create(pSrcData, nSrcLen)); - return; - } - - if (m_pData->CanOperateInPlace(m_pData->m_nDataLength + nSrcLen)) { - m_pData->CopyContentsAt(m_pData->m_nDataLength, pSrcData, nSrcLen); - m_pData->m_nDataLength += nSrcLen; - return; - } - - CFX_RetainPtr pNewData( - StringData::Create(m_pData->m_nDataLength + nSrcLen)); - pNewData->CopyContents(*m_pData); - pNewData->CopyContentsAt(m_pData->m_nDataLength, pSrcData, nSrcLen); - m_pData.Swap(pNewData); -} - -CFX_ByteString CFX_WideString::UTF8Encode() const { - return FX_UTF8Encode(AsStringC()); -} - -CFX_ByteString CFX_WideString::UTF16LE_Encode() const { - if (!m_pData) { - return CFX_ByteString("\0\0", 2); - } - int len = m_pData->m_nDataLength; - CFX_ByteString result; - char* buffer = result.GetBuffer(len * 2 + 2); - for (int i = 0; i < len; i++) { - buffer[i * 2] = m_pData->m_String[i] & 0xff; - buffer[i * 2 + 1] = m_pData->m_String[i] >> 8; - } - buffer[len * 2] = 0; - buffer[len * 2 + 1] = 0; - result.ReleaseBuffer(len * 2 + 2); - return result; -} - -CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst) const { - if (!m_pData) - return CFX_WideString(); - - return Mid(nFirst, m_pData->m_nDataLength - nFirst); -} - -CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const { - if (!m_pData) - return CFX_WideString(); - - nFirst = pdfium::clamp(nFirst, 0, m_pData->m_nDataLength); - nCount = pdfium::clamp(nCount, 0, m_pData->m_nDataLength - nFirst); - if (nCount == 0) - return CFX_WideString(); - - if (nFirst == 0 && nCount == m_pData->m_nDataLength) - return *this; - - CFX_WideString dest; - AllocCopy(dest, nCount, nFirst); - return dest; -} - -void CFX_WideString::AllocCopy(CFX_WideString& dest, - FX_STRSIZE nCopyLen, - FX_STRSIZE nCopyIndex) const { - if (nCopyLen <= 0) - return; - - CFX_RetainPtr pNewData( - StringData::Create(m_pData->m_String + nCopyIndex, nCopyLen)); - dest.m_pData.Swap(pNewData); -} - -bool CFX_WideString::TryVSWPrintf(FX_STRSIZE size, - const wchar_t* pFormat, - va_list argList) { - GetBuffer(size); - if (!m_pData) - return true; - - // In the following two calls, there's always space in the buffer for - // a terminating NUL that's not included in nMaxLen. - // For vswprintf(), MSAN won't untaint the buffer on a truncated write's - // -1 return code even though the buffer is written. Probably just as well - // not to trust the vendor's implementation to write anything anyways. - // See https://crbug.com/705912. - memset(m_pData->m_String, 0, (size + 1) * sizeof(wchar_t)); - int ret = vswprintf(m_pData->m_String, size + 1, pFormat, argList); - ReleaseBuffer(); - return ret >= 0 || m_pData->m_String[size - 1] == 0; -} - -void CFX_WideString::FormatV(const wchar_t* pFormat, va_list argList) { - va_list argListCopy; - FX_VA_COPY(argListCopy, argList); - FX_STRSIZE nMaxLen = vswprintf(nullptr, 0, pFormat, argListCopy); - va_end(argListCopy); - if (nMaxLen <= 0) { - nMaxLen = GuessSizeForVSWPrintf(pFormat, argListCopy); - if (nMaxLen <= 0) - return; - } - while (nMaxLen < 32 * 1024) { - FX_VA_COPY(argListCopy, argList); - bool bRetryPointless = TryVSWPrintf(nMaxLen, pFormat, argListCopy); - va_end(argListCopy); - if (bRetryPointless) - break; - nMaxLen *= 2; - } -} - -void CFX_WideString::Format(const wchar_t* pFormat, ...) { - va_list argList; - va_start(argList, pFormat); - FormatV(pFormat, argList); - va_end(argList); -} - -FX_STRSIZE CFX_WideString::Insert(FX_STRSIZE nIndex, wchar_t ch) { - FX_STRSIZE nNewLength = m_pData ? m_pData->m_nDataLength : 0; - nIndex = std::max(nIndex, 0); - nIndex = std::min(nIndex, nNewLength); - nNewLength++; - - ReallocBeforeWrite(nNewLength); - wmemmove(m_pData->m_String + nIndex + 1, m_pData->m_String + nIndex, - nNewLength - nIndex); - m_pData->m_String[nIndex] = ch; - m_pData->m_nDataLength = nNewLength; - return nNewLength; -} - -CFX_WideString CFX_WideString::Right(FX_STRSIZE nCount) const { - if (!m_pData) - return CFX_WideString(); - - nCount = std::max(nCount, 0); - if (nCount >= m_pData->m_nDataLength) - return *this; - - CFX_WideString dest; - AllocCopy(dest, nCount, m_pData->m_nDataLength - nCount); - return dest; -} - -CFX_WideString CFX_WideString::Left(FX_STRSIZE nCount) const { - if (!m_pData) - return CFX_WideString(); - - nCount = std::max(nCount, 0); - if (nCount >= m_pData->m_nDataLength) - return *this; - - CFX_WideString dest; - AllocCopy(dest, nCount, 0); - return dest; -} - -FX_STRSIZE CFX_WideString::Find(wchar_t ch, FX_STRSIZE nStart) const { - if (!m_pData) - return -1; - - if (nStart < 0 || nStart >= m_pData->m_nDataLength) - return -1; - - const wchar_t* pStr = - wmemchr(m_pData->m_String + nStart, ch, m_pData->m_nDataLength - nStart); - return pStr ? pStr - m_pData->m_String : -1; -} - -FX_STRSIZE CFX_WideString::Find(const CFX_WideStringC& pSub, - FX_STRSIZE nStart) const { - if (!m_pData) - return -1; - - FX_STRSIZE nLength = m_pData->m_nDataLength; - if (nStart > nLength) - return -1; - - const wchar_t* pStr = - FX_wcsstr(m_pData->m_String + nStart, m_pData->m_nDataLength - nStart, - pSub.c_str(), pSub.GetLength()); - return pStr ? (int)(pStr - m_pData->m_String) : -1; -} - -void CFX_WideString::MakeLower() { - if (!m_pData) - return; - - ReallocBeforeWrite(m_pData->m_nDataLength); - FXSYS_wcslwr(m_pData->m_String); -} - -void CFX_WideString::MakeUpper() { - if (!m_pData) - return; - - ReallocBeforeWrite(m_pData->m_nDataLength); - FXSYS_wcsupr(m_pData->m_String); -} - -FX_STRSIZE CFX_WideString::Remove(wchar_t chRemove) { - if (!m_pData || m_pData->m_nDataLength < 1) - return 0; - - wchar_t* pstrSource = m_pData->m_String; - wchar_t* pstrEnd = m_pData->m_String + m_pData->m_nDataLength; - while (pstrSource < pstrEnd) { - if (*pstrSource == chRemove) - break; - pstrSource++; - } - if (pstrSource == pstrEnd) - return 0; - - ptrdiff_t copied = pstrSource - m_pData->m_String; - ReallocBeforeWrite(m_pData->m_nDataLength); - pstrSource = m_pData->m_String + copied; - pstrEnd = m_pData->m_String + m_pData->m_nDataLength; - - wchar_t* pstrDest = pstrSource; - while (pstrSource < pstrEnd) { - if (*pstrSource != chRemove) { - *pstrDest = *pstrSource; - pstrDest++; - } - pstrSource++; - } - - *pstrDest = 0; - FX_STRSIZE nCount = (FX_STRSIZE)(pstrSource - pstrDest); - m_pData->m_nDataLength -= nCount; - return nCount; -} - -FX_STRSIZE CFX_WideString::Replace(const CFX_WideStringC& pOld, - const CFX_WideStringC& pNew) { - if (!m_pData || pOld.IsEmpty()) - return 0; - - FX_STRSIZE nSourceLen = pOld.GetLength(); - FX_STRSIZE nReplacementLen = pNew.GetLength(); - FX_STRSIZE nCount = 0; - const wchar_t* pStart = m_pData->m_String; - wchar_t* pEnd = m_pData->m_String + m_pData->m_nDataLength; - while (1) { - const wchar_t* pTarget = FX_wcsstr(pStart, (FX_STRSIZE)(pEnd - pStart), - pOld.c_str(), nSourceLen); - if (!pTarget) - break; - - nCount++; - pStart = pTarget + nSourceLen; - } - if (nCount == 0) - return 0; - - FX_STRSIZE nNewLength = - m_pData->m_nDataLength + (nReplacementLen - nSourceLen) * nCount; - - if (nNewLength == 0) { - clear(); - return nCount; - } - - CFX_RetainPtr pNewData(StringData::Create(nNewLength)); - pStart = m_pData->m_String; - wchar_t* pDest = pNewData->m_String; - for (FX_STRSIZE i = 0; i < nCount; i++) { - const wchar_t* pTarget = FX_wcsstr(pStart, (FX_STRSIZE)(pEnd - pStart), - pOld.c_str(), nSourceLen); - wmemcpy(pDest, pStart, pTarget - pStart); - pDest += pTarget - pStart; - wmemcpy(pDest, pNew.c_str(), pNew.GetLength()); - pDest += pNew.GetLength(); - pStart = pTarget + nSourceLen; - } - wmemcpy(pDest, pStart, pEnd - pStart); - m_pData.Swap(pNewData); - return nCount; -} - -void CFX_WideString::SetAt(FX_STRSIZE nIndex, wchar_t ch) { - if (!m_pData) { - return; - } - ASSERT(nIndex >= 0); - ASSERT(nIndex < m_pData->m_nDataLength); - ReallocBeforeWrite(m_pData->m_nDataLength); - m_pData->m_String[nIndex] = ch; -} - -// static -CFX_WideString CFX_WideString::FromLocal(const CFX_ByteStringC& str) { - return FromCodePage(str, 0); -} - -// static -CFX_WideString CFX_WideString::FromCodePage(const CFX_ByteStringC& str, - uint16_t codepage) { - return CFX_CharMap::GetWideString(codepage, str); -} - -// static -CFX_WideString CFX_WideString::FromUTF8(const CFX_ByteStringC& str) { - if (str.IsEmpty()) - return CFX_WideString(); - - CFX_UTF8Decoder decoder; - for (FX_STRSIZE i = 0; i < str.GetLength(); i++) - decoder.Input(str[i]); - - return CFX_WideString(decoder.GetResult()); -} - -// static -CFX_WideString CFX_WideString::FromUTF16LE(const unsigned short* wstr, - FX_STRSIZE wlen) { - if (!wstr || 0 == wlen) { - return CFX_WideString(); - } - - CFX_WideString result; - wchar_t* buf = result.GetBuffer(wlen); - for (int i = 0; i < wlen; i++) { - buf[i] = wstr[i]; - } - result.ReleaseBuffer(wlen); - return result; -} - -int CFX_WideString::Compare(const wchar_t* lpsz) const { - if (m_pData) - return FXSYS_wcscmp(m_pData->m_String, lpsz); - return (!lpsz || lpsz[0] == 0) ? 0 : -1; -} - -int CFX_WideString::Compare(const CFX_WideString& str) const { - if (!m_pData) { - if (!str.m_pData) { - return 0; - } - return -1; - } - if (!str.m_pData) { - return 1; - } - int this_len = m_pData->m_nDataLength; - int that_len = str.m_pData->m_nDataLength; - int min_len = this_len < that_len ? this_len : that_len; - for (int i = 0; i < min_len; i++) { - if (m_pData->m_String[i] < str.m_pData->m_String[i]) { - return -1; - } - if (m_pData->m_String[i] > str.m_pData->m_String[i]) { - return 1; - } - } - if (this_len < that_len) { - return -1; - } - if (this_len > that_len) { - return 1; - } - return 0; -} - -int CFX_WideString::CompareNoCase(const wchar_t* lpsz) const { - if (!m_pData) { - return (!lpsz || lpsz[0] == 0) ? 0 : -1; - } - return FXSYS_wcsicmp(m_pData->m_String, lpsz); -} - -FX_STRSIZE CFX_WideString::WStringLength(const unsigned short* str) { - FX_STRSIZE len = 0; - if (str) - while (str[len]) - len++; - return len; -} - -void CFX_WideString::TrimRight(const CFX_WideStringC& pTargets) { - if (IsEmpty() || pTargets.IsEmpty()) - return; - - FX_STRSIZE pos = GetLength(); - while (pos && pTargets.Find(m_pData->m_String[pos - 1]) != -1) - pos--; - - if (pos < m_pData->m_nDataLength) { - ReallocBeforeWrite(m_pData->m_nDataLength); - m_pData->m_String[pos] = 0; - m_pData->m_nDataLength = pos; - } -} - -void CFX_WideString::TrimRight(wchar_t chTarget) { - wchar_t str[2] = {chTarget, 0}; - TrimRight(str); -} - -void CFX_WideString::TrimRight() { - TrimRight(L"\x09\x0a\x0b\x0c\x0d\x20"); -} - -void CFX_WideString::TrimLeft(const CFX_WideStringC& pTargets) { - if (!m_pData || pTargets.IsEmpty()) - return; - - FX_STRSIZE len = GetLength(); - if (len < 1) - return; - - FX_STRSIZE pos = 0; - while (pos < len) { - FX_STRSIZE i = 0; - while (i < pTargets.GetLength() && - pTargets.CharAt(i) != m_pData->m_String[pos]) { - i++; - } - if (i == pTargets.GetLength()) { - break; - } - pos++; - } - if (pos) { - ReallocBeforeWrite(len); - FX_STRSIZE nDataLength = len - pos; - FXSYS_memmove(m_pData->m_String, m_pData->m_String + pos, - (nDataLength + 1) * sizeof(wchar_t)); - m_pData->m_nDataLength = nDataLength; - } -} - -void CFX_WideString::TrimLeft(wchar_t chTarget) { - wchar_t str[2] = {chTarget, 0}; - TrimLeft(str); -} - -void CFX_WideString::TrimLeft() { - TrimLeft(L"\x09\x0a\x0b\x0c\x0d\x20"); -} -float FX_wtof(const wchar_t* str, int len) { - if (len == 0) { - return 0.0; - } - int cc = 0; - bool bNegative = false; - if (str[0] == '+') { - cc++; - } else if (str[0] == '-') { - bNegative = true; - cc++; - } - int integer = 0; - while (cc < len) { - if (str[cc] == '.') { - break; - } - integer = integer * 10 + FXSYS_toDecimalDigit(str[cc]); - cc++; - } - float fraction = 0; - if (str[cc] == '.') { - cc++; - float scale = 0.1f; - while (cc < len) { - fraction += scale * FXSYS_toDecimalDigit(str[cc]); - scale *= 0.1f; - cc++; - } - } - fraction += (float)integer; - return bNegative ? -fraction : fraction; -} - -int CFX_WideString::GetInteger() const { - return m_pData ? FXSYS_wtoi(m_pData->m_String) : 0; -} - -float CFX_WideString::GetFloat() const { - return m_pData ? FX_wtof(m_pData->m_String, m_pData->m_nDataLength) : 0.0f; -} - -// static -CFX_ByteString CFX_CharMap::GetByteString(uint16_t codepage, - const CFX_WideStringC& wstr) { - ASSERT(IsValidCodePage(codepage)); - int src_len = wstr.GetLength(); - int dest_len = FXSYS_WideCharToMultiByte(codepage, 0, wstr.c_str(), src_len, - nullptr, 0, nullptr, nullptr); - CFX_ByteString bstr; - if (dest_len) { - char* dest_buf = bstr.GetBuffer(dest_len); - FXSYS_WideCharToMultiByte(codepage, 0, wstr.c_str(), src_len, dest_buf, - dest_len, nullptr, nullptr); - bstr.ReleaseBuffer(dest_len); - } - return bstr; -} - -// static -CFX_WideString CFX_CharMap::GetWideString(uint16_t codepage, - const CFX_ByteStringC& bstr) { - ASSERT(IsValidCodePage(codepage)); - int src_len = bstr.GetLength(); - int dest_len = - FXSYS_MultiByteToWideChar(codepage, 0, bstr.c_str(), src_len, nullptr, 0); - CFX_WideString wstr; - if (dest_len) { - wchar_t* dest_buf = wstr.GetBuffer(dest_len); - FXSYS_MultiByteToWideChar(codepage, 0, bstr.c_str(), src_len, dest_buf, - dest_len); - wstr.ReleaseBuffer(dest_len); - } - return wstr; -} diff --git a/core/fxcrt/fx_basic_wstring_unittest.cpp b/core/fxcrt/fx_basic_wstring_unittest.cpp deleted file mode 100644 index c5151709e0..0000000000 --- a/core/fxcrt/fx_basic_wstring_unittest.cpp +++ /dev/null @@ -1,941 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "core/fxcrt/fx_basic.h" -#include "testing/fx_string_testhelpers.h" -#include "testing/gtest/include/gtest/gtest.h" - -TEST(fxcrt, WideStringOperatorSubscript) { - // CFX_WideString includes the NUL terminator for non-empty strings. - CFX_WideString abc(L"abc"); - EXPECT_EQ(L'a', abc[0]); - EXPECT_EQ(L'b', abc[1]); - EXPECT_EQ(L'c', abc[2]); - EXPECT_EQ(L'\0', abc[3]); -} - -TEST(fxcrt, WideStringOperatorLT) { - CFX_WideString empty; - CFX_WideString a(L"a"); - CFX_WideString abc(L"\x0110qq"); // Comes before despite endianness. - CFX_WideString def(L"\x1001qq"); // Comes after despite endianness. - - EXPECT_FALSE(empty < empty); - EXPECT_FALSE(a < a); - EXPECT_FALSE(abc < abc); - EXPECT_FALSE(def < def); - - EXPECT_TRUE(empty < a); - EXPECT_FALSE(a < empty); - - EXPECT_TRUE(empty < abc); - EXPECT_FALSE(abc < empty); - - EXPECT_TRUE(empty < def); - EXPECT_FALSE(def < empty); - - EXPECT_TRUE(a < abc); - EXPECT_FALSE(abc < a); - - EXPECT_TRUE(a < def); - EXPECT_FALSE(def < a); - - EXPECT_TRUE(abc < def); - EXPECT_FALSE(def < abc); -} - -TEST(fxcrt, WideStringOperatorEQ) { - CFX_WideString null_string; - EXPECT_TRUE(null_string == null_string); - - CFX_WideString empty_string(L""); - EXPECT_TRUE(empty_string == empty_string); - EXPECT_TRUE(empty_string == null_string); - EXPECT_TRUE(null_string == empty_string); - - CFX_WideString deleted_string(L"hello"); - deleted_string.Delete(0, 5); - EXPECT_TRUE(deleted_string == deleted_string); - EXPECT_TRUE(deleted_string == null_string); - EXPECT_TRUE(deleted_string == empty_string); - EXPECT_TRUE(null_string == deleted_string); - EXPECT_TRUE(null_string == empty_string); - - CFX_WideString wide_string(L"hello"); - EXPECT_TRUE(wide_string == wide_string); - EXPECT_FALSE(wide_string == null_string); - EXPECT_FALSE(wide_string == empty_string); - EXPECT_FALSE(wide_string == deleted_string); - EXPECT_FALSE(null_string == wide_string); - EXPECT_FALSE(empty_string == wide_string); - EXPECT_FALSE(deleted_string == wide_string); - - CFX_WideString wide_string_same1(L"hello"); - EXPECT_TRUE(wide_string == wide_string_same1); - EXPECT_TRUE(wide_string_same1 == wide_string); - - CFX_WideString wide_string_same2(wide_string); - EXPECT_TRUE(wide_string == wide_string_same2); - EXPECT_TRUE(wide_string_same2 == wide_string); - - CFX_WideString wide_string1(L"he"); - CFX_WideString wide_string2(L"hellp"); - CFX_WideString wide_string3(L"hellod"); - EXPECT_FALSE(wide_string == wide_string1); - EXPECT_FALSE(wide_string == wide_string2); - EXPECT_FALSE(wide_string == wide_string3); - EXPECT_FALSE(wide_string1 == wide_string); - EXPECT_FALSE(wide_string2 == wide_string); - EXPECT_FALSE(wide_string3 == wide_string); - - CFX_WideStringC null_string_c; - CFX_WideStringC empty_string_c(L""); - EXPECT_TRUE(null_string == null_string_c); - EXPECT_TRUE(null_string == empty_string_c); - EXPECT_TRUE(empty_string == null_string_c); - EXPECT_TRUE(empty_string == empty_string_c); - EXPECT_TRUE(deleted_string == null_string_c); - EXPECT_TRUE(deleted_string == empty_string_c); - EXPECT_TRUE(null_string_c == null_string); - EXPECT_TRUE(empty_string_c == null_string); - EXPECT_TRUE(null_string_c == empty_string); - EXPECT_TRUE(empty_string_c == empty_string); - EXPECT_TRUE(null_string_c == deleted_string); - EXPECT_TRUE(empty_string_c == deleted_string); - - CFX_WideStringC wide_string_c_same1(L"hello"); - EXPECT_TRUE(wide_string == wide_string_c_same1); - EXPECT_TRUE(wide_string_c_same1 == wide_string); - - CFX_WideStringC wide_string_c1(L"he"); - CFX_WideStringC wide_string_c2(L"hellp"); - CFX_WideStringC wide_string_c3(L"hellod"); - EXPECT_FALSE(wide_string == wide_string_c1); - EXPECT_FALSE(wide_string == wide_string_c2); - EXPECT_FALSE(wide_string == wide_string_c3); - EXPECT_FALSE(wide_string_c1 == wide_string); - EXPECT_FALSE(wide_string_c2 == wide_string); - EXPECT_FALSE(wide_string_c3 == wide_string); - - const wchar_t* c_null_string = nullptr; - const wchar_t* c_empty_string = L""; - EXPECT_TRUE(null_string == c_null_string); - EXPECT_TRUE(null_string == c_empty_string); - EXPECT_TRUE(empty_string == c_null_string); - EXPECT_TRUE(empty_string == c_empty_string); - EXPECT_TRUE(deleted_string == c_null_string); - EXPECT_TRUE(deleted_string == c_empty_string); - EXPECT_TRUE(c_null_string == null_string); - EXPECT_TRUE(c_empty_string == null_string); - EXPECT_TRUE(c_null_string == empty_string); - EXPECT_TRUE(c_empty_string == empty_string); - EXPECT_TRUE(c_null_string == deleted_string); - EXPECT_TRUE(c_empty_string == deleted_string); - - const wchar_t* c_string_same1 = L"hello"; - EXPECT_TRUE(wide_string == c_string_same1); - EXPECT_TRUE(c_string_same1 == wide_string); - - const wchar_t* c_string1 = L"he"; - const wchar_t* c_string2 = L"hellp"; - const wchar_t* c_string3 = L"hellod"; - EXPECT_FALSE(wide_string == c_string1); - EXPECT_FALSE(wide_string == c_string2); - EXPECT_FALSE(wide_string == c_string3); - EXPECT_FALSE(c_string1 == wide_string); - EXPECT_FALSE(c_string2 == wide_string); - EXPECT_FALSE(c_string3 == wide_string); -} - -TEST(fxcrt, WideStringOperatorNE) { - CFX_WideString null_string; - EXPECT_FALSE(null_string != null_string); - - CFX_WideString empty_string(L""); - EXPECT_FALSE(empty_string != empty_string); - EXPECT_FALSE(empty_string != null_string); - EXPECT_FALSE(null_string != empty_string); - - CFX_WideString deleted_string(L"hello"); - deleted_string.Delete(0, 5); - EXPECT_FALSE(deleted_string != deleted_string); - EXPECT_FALSE(deleted_string != null_string); - EXPECT_FALSE(deleted_string != empty_string); - EXPECT_FALSE(null_string != deleted_string); - EXPECT_FALSE(null_string != empty_string); - - CFX_WideString wide_string(L"hello"); - EXPECT_FALSE(wide_string != wide_string); - EXPECT_TRUE(wide_string != null_string); - EXPECT_TRUE(wide_string != empty_string); - EXPECT_TRUE(wide_string != deleted_string); - EXPECT_TRUE(null_string != wide_string); - EXPECT_TRUE(empty_string != wide_string); - EXPECT_TRUE(deleted_string != wide_string); - - CFX_WideString wide_string_same1(L"hello"); - EXPECT_FALSE(wide_string != wide_string_same1); - EXPECT_FALSE(wide_string_same1 != wide_string); - - CFX_WideString wide_string_same2(wide_string); - EXPECT_FALSE(wide_string != wide_string_same2); - EXPECT_FALSE(wide_string_same2 != wide_string); - - CFX_WideString wide_string1(L"he"); - CFX_WideString wide_string2(L"hellp"); - CFX_WideString wide_string3(L"hellod"); - EXPECT_TRUE(wide_string != wide_string1); - EXPECT_TRUE(wide_string != wide_string2); - EXPECT_TRUE(wide_string != wide_string3); - EXPECT_TRUE(wide_string1 != wide_string); - EXPECT_TRUE(wide_string2 != wide_string); - EXPECT_TRUE(wide_string3 != wide_string); - - CFX_WideStringC null_string_c; - CFX_WideStringC empty_string_c(L""); - EXPECT_FALSE(null_string != null_string_c); - EXPECT_FALSE(null_string != empty_string_c); - EXPECT_FALSE(empty_string != null_string_c); - EXPECT_FALSE(empty_string != empty_string_c); - EXPECT_FALSE(deleted_string != null_string_c); - EXPECT_FALSE(deleted_string != empty_string_c); - EXPECT_FALSE(null_string_c != null_string); - EXPECT_FALSE(empty_string_c != null_string); - EXPECT_FALSE(null_string_c != empty_string); - EXPECT_FALSE(empty_string_c != empty_string); - - CFX_WideStringC wide_string_c_same1(L"hello"); - EXPECT_FALSE(wide_string != wide_string_c_same1); - EXPECT_FALSE(wide_string_c_same1 != wide_string); - - CFX_WideStringC wide_string_c1(L"he"); - CFX_WideStringC wide_string_c2(L"hellp"); - CFX_WideStringC wide_string_c3(L"hellod"); - EXPECT_TRUE(wide_string != wide_string_c1); - EXPECT_TRUE(wide_string != wide_string_c2); - EXPECT_TRUE(wide_string != wide_string_c3); - EXPECT_TRUE(wide_string_c1 != wide_string); - EXPECT_TRUE(wide_string_c2 != wide_string); - EXPECT_TRUE(wide_string_c3 != wide_string); - - const wchar_t* c_null_string = nullptr; - const wchar_t* c_empty_string = L""; - EXPECT_FALSE(null_string != c_null_string); - EXPECT_FALSE(null_string != c_empty_string); - EXPECT_FALSE(empty_string != c_null_string); - EXPECT_FALSE(empty_string != c_empty_string); - EXPECT_FALSE(deleted_string != c_null_string); - EXPECT_FALSE(deleted_string != c_empty_string); - EXPECT_FALSE(c_null_string != null_string); - EXPECT_FALSE(c_empty_string != null_string); - EXPECT_FALSE(c_null_string != empty_string); - EXPECT_FALSE(c_empty_string != empty_string); - EXPECT_FALSE(c_null_string != deleted_string); - EXPECT_FALSE(c_empty_string != deleted_string); - - const wchar_t* c_string_same1 = L"hello"; - EXPECT_FALSE(wide_string != c_string_same1); - EXPECT_FALSE(c_string_same1 != wide_string); - - const wchar_t* c_string1 = L"he"; - const wchar_t* c_string2 = L"hellp"; - const wchar_t* c_string3 = L"hellod"; - EXPECT_TRUE(wide_string != c_string1); - EXPECT_TRUE(wide_string != c_string2); - EXPECT_TRUE(wide_string != c_string3); - EXPECT_TRUE(c_string1 != wide_string); - EXPECT_TRUE(c_string2 != wide_string); - EXPECT_TRUE(c_string3 != wide_string); -} - -TEST(fxcrt, WideStringConcatInPlace) { - CFX_WideString fred; - fred.Concat(L"FRED", 4); - EXPECT_EQ(L"FRED", fred); - - fred.Concat(L"DY", 2); - EXPECT_EQ(L"FREDDY", fred); - - fred.Delete(3, 3); - EXPECT_EQ(L"FRE", fred); - - fred.Concat(L"D", 1); - EXPECT_EQ(L"FRED", fred); - - CFX_WideString copy = fred; - fred.Concat(L"DY", 2); - EXPECT_EQ(L"FREDDY", fred); - EXPECT_EQ(L"FRED", copy); - - // Test invalid arguments. - copy = fred; - fred.Concat(L"freddy", -6); - CFX_WideString not_aliased(L"xxxxxx"); - EXPECT_EQ(L"FREDDY", fred); - EXPECT_EQ(L"xxxxxx", not_aliased); -} - -TEST(fxcrt, WideStringRemove) { - CFX_WideString freed(L"FREED"); - freed.Remove(L'E'); - EXPECT_EQ(L"FRD", freed); - freed.Remove(L'F'); - EXPECT_EQ(L"RD", freed); - freed.Remove(L'D'); - EXPECT_EQ(L"R", freed); - freed.Remove(L'X'); - EXPECT_EQ(L"R", freed); - freed.Remove(L'R'); - EXPECT_EQ(L"", freed); - - CFX_WideString empty; - empty.Remove(L'X'); - EXPECT_EQ(L"", empty); -} - -TEST(fxcrt, WideStringRemoveCopies) { - CFX_WideString freed(L"FREED"); - const wchar_t* old_buffer = freed.c_str(); - - // No change with single reference - no copy. - freed.Remove(L'Q'); - EXPECT_EQ(L"FREED", freed); - EXPECT_EQ(old_buffer, freed.c_str()); - - // Change with single reference - no copy. - freed.Remove(L'E'); - EXPECT_EQ(L"FRD", freed); - EXPECT_EQ(old_buffer, freed.c_str()); - - // No change with multiple references - no copy. - CFX_WideString shared(freed); - freed.Remove(L'Q'); - EXPECT_EQ(L"FRD", freed); - EXPECT_EQ(old_buffer, freed.c_str()); - EXPECT_EQ(old_buffer, shared.c_str()); - - // Change with multiple references -- must copy. - freed.Remove(L'D'); - EXPECT_EQ(L"FR", freed); - EXPECT_NE(old_buffer, freed.c_str()); - EXPECT_EQ(L"FRD", shared); - EXPECT_EQ(old_buffer, shared.c_str()); -} - -TEST(fxcrt, WideStringReplace) { - CFX_WideString fred(L"FRED"); - fred.Replace(L"FR", L"BL"); - EXPECT_EQ(L"BLED", fred); - fred.Replace(L"D", L"DDY"); - EXPECT_EQ(L"BLEDDY", fred); - fred.Replace(L"LEDD", L""); - EXPECT_EQ(L"BY", fred); - fred.Replace(L"X", L"CLAMS"); - EXPECT_EQ(L"BY", fred); - fred.Replace(L"BY", L"HI"); - EXPECT_EQ(L"HI", fred); - fred.Replace(L"", L"CLAMS"); - EXPECT_EQ(L"HI", fred); - fred.Replace(L"HI", L""); - EXPECT_EQ(L"", fred); -} - -TEST(fxcrt, WideStringInsert) { - CFX_WideString fred(L"FRED"); - fred.Insert(-1, 'X'); - EXPECT_EQ(L"XFRED", fred); - - fred.Insert(0, 'S'); - EXPECT_EQ(L"SXFRED", fred); - - fred.Insert(2, 'T'); - EXPECT_EQ(L"SXTFRED", fred); - - fred.Insert(5, 'U'); - EXPECT_EQ(L"SXTFRUED", fred); - - fred.Insert(8, 'V'); - EXPECT_EQ(L"SXTFRUEDV", fred); - - fred.Insert(12, 'P'); - EXPECT_EQ(L"SXTFRUEDVP", fred); - - { - CFX_WideString empty; - empty.Insert(-1, 'X'); - EXPECT_EQ(L"X", empty); - } - { - CFX_WideString empty; - empty.Insert(0, 'X'); - EXPECT_EQ(L"X", empty); - } - { - CFX_WideString empty; - empty.Insert(5, 'X'); - EXPECT_EQ(L"X", empty); - } -} - -TEST(fxcrt, WideStringDelete) { - CFX_WideString fred(L"FRED"); - fred.Delete(0, 2); - EXPECT_EQ(L"ED", fred); - fred.Delete(1); - EXPECT_EQ(L"E", fred); - fred.Delete(-1); - EXPECT_EQ(L"", fred); - fred.Delete(1); - EXPECT_EQ(L"", fred); - - CFX_WideString empty; - empty.Delete(0); - EXPECT_EQ(L"", empty); - empty.Delete(-1); - EXPECT_EQ(L"", empty); - empty.Delete(1); - EXPECT_EQ(L"", empty); -} - -TEST(fxcrt, WideStringMid) { - CFX_WideString fred(L"FRED"); - EXPECT_EQ(L"", fred.Mid(0, 0)); - EXPECT_EQ(L"", fred.Mid(3, 0)); - EXPECT_EQ(L"FRED", fred.Mid(0)); - EXPECT_EQ(L"RED", fred.Mid(1)); - EXPECT_EQ(L"ED", fred.Mid(2)); - EXPECT_EQ(L"D", fred.Mid(3)); - EXPECT_EQ(L"F", fred.Mid(0, 1)); - EXPECT_EQ(L"R", fred.Mid(1, 1)); - EXPECT_EQ(L"E", fred.Mid(2, 1)); - EXPECT_EQ(L"D", fred.Mid(3, 1)); - EXPECT_EQ(L"FR", fred.Mid(0, 2)); - EXPECT_EQ(L"FRED", fred.Mid(0, 4)); - EXPECT_EQ(L"FRED", fred.Mid(0, 10)); - - EXPECT_EQ(L"FR", fred.Mid(-1, 2)); - EXPECT_EQ(L"RED", fred.Mid(1, 4)); - EXPECT_EQ(L"", fred.Mid(4, 1)); - - CFX_WideString empty; - EXPECT_EQ(L"", empty.Mid(0, 0)); - EXPECT_EQ(L"", empty.Mid(0)); - EXPECT_EQ(L"", empty.Mid(1)); - EXPECT_EQ(L"", empty.Mid(-1)); -} - -TEST(fxcrt, WideStringLeft) { - CFX_WideString fred(L"FRED"); - EXPECT_EQ(L"", fred.Left(0)); - EXPECT_EQ(L"F", fred.Left(1)); - EXPECT_EQ(L"FR", fred.Left(2)); - EXPECT_EQ(L"FRE", fred.Left(3)); - EXPECT_EQ(L"FRED", fred.Left(4)); - - EXPECT_EQ(L"FRED", fred.Left(5)); - EXPECT_EQ(L"", fred.Left(-1)); - - CFX_WideString empty; - EXPECT_EQ(L"", empty.Left(0)); - EXPECT_EQ(L"", empty.Left(1)); - EXPECT_EQ(L"", empty.Left(-1)); -} - -TEST(fxcrt, WideStringRight) { - CFX_WideString fred(L"FRED"); - EXPECT_EQ(L"", fred.Right(0)); - EXPECT_EQ(L"D", fred.Right(1)); - EXPECT_EQ(L"ED", fred.Right(2)); - EXPECT_EQ(L"RED", fred.Right(3)); - EXPECT_EQ(L"FRED", fred.Right(4)); - - EXPECT_EQ(L"FRED", fred.Right(5)); - EXPECT_EQ(L"", fred.Right(-1)); - - CFX_WideString empty; - EXPECT_EQ(L"", empty.Right(0)); - EXPECT_EQ(L"", empty.Right(1)); - EXPECT_EQ(L"", empty.Right(-1)); -} - -TEST(fxcrt, WideStringUpperLower) { - CFX_WideString fred(L"F-Re.42D"); - fred.MakeLower(); - EXPECT_EQ(L"f-re.42d", fred); - fred.MakeUpper(); - EXPECT_EQ(L"F-RE.42D", fred); - - CFX_WideString empty; - empty.MakeLower(); - EXPECT_EQ(L"", empty); - empty.MakeUpper(); - EXPECT_EQ(L"", empty); -} - -TEST(fxcrt, WideStringTrimRight) { - CFX_WideString fred(L" FRED "); - fred.TrimRight(); - EXPECT_EQ(L" FRED", fred); - fred.TrimRight(L'E'); - EXPECT_EQ(L" FRED", fred); - fred.TrimRight(L'D'); - EXPECT_EQ(L" FRE", fred); - fred.TrimRight(L"ERP"); - EXPECT_EQ(L" F", fred); - - CFX_WideString blank(L" "); - blank.TrimRight(L"ERP"); - EXPECT_EQ(L" ", blank); - blank.TrimRight(L'E'); - EXPECT_EQ(L" ", blank); - blank.TrimRight(); - EXPECT_EQ(L"", blank); - - CFX_WideString empty; - empty.TrimRight(L"ERP"); - EXPECT_EQ(L"", empty); - empty.TrimRight(L'E'); - EXPECT_EQ(L"", empty); - empty.TrimRight(); - EXPECT_EQ(L"", empty); -} - -TEST(fxcrt, WideStringTrimRightCopies) { - { - // With a single reference, no copy takes place. - CFX_WideString fred(L" FRED "); - const wchar_t* old_buffer = fred.c_str(); - fred.TrimRight(); - EXPECT_EQ(L" FRED", fred); - EXPECT_EQ(old_buffer, fred.c_str()); - } - { - // With multiple references, we must copy. - CFX_WideString fred(L" FRED "); - CFX_WideString other_fred = fred; - const wchar_t* old_buffer = fred.c_str(); - fred.TrimRight(); - EXPECT_EQ(L" FRED", fred); - EXPECT_EQ(L" FRED ", other_fred); - EXPECT_NE(old_buffer, fred.c_str()); - } - { - // With multiple references, but no modifications, no copy. - CFX_WideString fred(L"FRED"); - CFX_WideString other_fred = fred; - const wchar_t* old_buffer = fred.c_str(); - fred.TrimRight(); - EXPECT_EQ(L"FRED", fred); - EXPECT_EQ(L"FRED", other_fred); - EXPECT_EQ(old_buffer, fred.c_str()); - } -} - -TEST(fxcrt, WideStringTrimLeft) { - CFX_WideString fred(L" FRED "); - fred.TrimLeft(); - EXPECT_EQ(L"FRED ", fred); - fred.TrimLeft(L'E'); - EXPECT_EQ(L"FRED ", fred); - fred.TrimLeft(L'F'); - EXPECT_EQ(L"RED ", fred); - fred.TrimLeft(L"ERP"); - EXPECT_EQ(L"D ", fred); - - CFX_WideString blank(L" "); - blank.TrimLeft(L"ERP"); - EXPECT_EQ(L" ", blank); - blank.TrimLeft(L'E'); - EXPECT_EQ(L" ", blank); - blank.TrimLeft(); - EXPECT_EQ(L"", blank); - - CFX_WideString empty; - empty.TrimLeft(L"ERP"); - EXPECT_EQ(L"", empty); - empty.TrimLeft(L'E'); - EXPECT_EQ(L"", empty); - empty.TrimLeft(); - EXPECT_EQ(L"", empty); -} - -TEST(fxcrt, WideStringTrimLeftCopies) { - { - // With a single reference, no copy takes place. - CFX_WideString fred(L" FRED "); - const wchar_t* old_buffer = fred.c_str(); - fred.TrimLeft(); - EXPECT_EQ(L"FRED ", fred); - EXPECT_EQ(old_buffer, fred.c_str()); - } - { - // With multiple references, we must copy. - CFX_WideString fred(L" FRED "); - CFX_WideString other_fred = fred; - const wchar_t* old_buffer = fred.c_str(); - fred.TrimLeft(); - EXPECT_EQ(L"FRED ", fred); - EXPECT_EQ(L" FRED ", other_fred); - EXPECT_NE(old_buffer, fred.c_str()); - } - { - // With multiple references, but no modifications, no copy. - CFX_WideString fred(L"FRED"); - CFX_WideString other_fred = fred; - const wchar_t* old_buffer = fred.c_str(); - fred.TrimLeft(); - EXPECT_EQ(L"FRED", fred); - EXPECT_EQ(L"FRED", other_fred); - EXPECT_EQ(old_buffer, fred.c_str()); - } -} - -TEST(fxcrt, WideStringReserve) { - { - CFX_WideString str; - str.Reserve(6); - const wchar_t* old_buffer = str.c_str(); - str += L"ABCDEF"; - EXPECT_EQ(old_buffer, str.c_str()); - str += L"Blah Blah Blah Blah Blah Blah"; - EXPECT_NE(old_buffer, str.c_str()); - } - { - CFX_WideString str(L"A"); - str.Reserve(6); - const wchar_t* old_buffer = str.c_str(); - str += L"BCDEF"; - EXPECT_EQ(old_buffer, str.c_str()); - str += L"Blah Blah Blah Blah Blah Blah"; - EXPECT_NE(old_buffer, str.c_str()); - } -} - -TEST(fxcrt, WideStringGetBuffer) { - { - CFX_WideString str; - wchar_t* buffer = str.GetBuffer(12); - wcscpy(buffer, L"clams"); - str.ReleaseBuffer(); - EXPECT_EQ(L"clams", str); - } - { - CFX_WideString str(L"cl"); - wchar_t* buffer = str.GetBuffer(12); - wcscpy(buffer + 2, L"ams"); - str.ReleaseBuffer(); - EXPECT_EQ(L"clams", str); - } -} - -TEST(fxcrt, WideStringReleaseBuffer) { - { - CFX_WideString str; - str.Reserve(12); - str += L"clams"; - const wchar_t* old_buffer = str.c_str(); - str.ReleaseBuffer(4); - EXPECT_EQ(old_buffer, str.c_str()); - EXPECT_EQ(L"clam", str); - } - { - CFX_WideString str(L"c"); - str.Reserve(12); - str += L"lams"; - const wchar_t* old_buffer = str.c_str(); - str.ReleaseBuffer(4); - EXPECT_EQ(old_buffer, str.c_str()); - EXPECT_EQ(L"clam", str); - } - { - CFX_WideString str; - str.Reserve(200); - str += L"clams"; - const wchar_t* old_buffer = str.c_str(); - str.ReleaseBuffer(4); - EXPECT_NE(old_buffer, str.c_str()); - EXPECT_EQ(L"clam", str); - } - { - CFX_WideString str(L"c"); - str.Reserve(200); - str += L"lams"; - const wchar_t* old_buffer = str.c_str(); - str.ReleaseBuffer(4); - EXPECT_NE(old_buffer, str.c_str()); - EXPECT_EQ(L"clam", str); - } -} - -TEST(fxcrt, WideStringUTF16LE_Encode) { - struct UTF16LEEncodeCase { - CFX_WideString ws; - CFX_ByteString bs; - } utf16le_encode_cases[] = { - {L"", CFX_ByteString("\0\0", 2)}, - {L"abc", CFX_ByteString("a\0b\0c\0\0\0", 8)}, - {L"abcdef", CFX_ByteString("a\0b\0c\0d\0e\0f\0\0\0", 14)}, - {L"abc\0def", CFX_ByteString("a\0b\0c\0\0\0", 8)}, - {L"\xaabb\xccdd", CFX_ByteString("\xbb\xaa\xdd\xcc\0\0", 6)}, - {L"\x3132\x6162", CFX_ByteString("\x32\x31\x62\x61\0\0", 6)}, - }; - - for (size_t i = 0; i < FX_ArraySize(utf16le_encode_cases); ++i) { - EXPECT_EQ(utf16le_encode_cases[i].bs, - utf16le_encode_cases[i].ws.UTF16LE_Encode()) - << " for case number " << i; - } -} - -TEST(fxcrt, WideStringCOperatorSubscript) { - // CFX_WideStringC includes the NUL terminator for non-empty strings. - CFX_WideStringC abc(L"abc"); - EXPECT_EQ(L'a', abc.CharAt(0)); - EXPECT_EQ(L'b', abc.CharAt(1)); - EXPECT_EQ(L'c', abc.CharAt(2)); - EXPECT_EQ(L'\0', abc.CharAt(3)); -} - -TEST(fxcrt, WideStringCOperatorLT) { - CFX_WideStringC empty; - CFX_WideStringC a(L"a"); - CFX_WideStringC abc(L"\x0110qq"); // Comes before despite endianness. - CFX_WideStringC def(L"\x1001qq"); // Comes after despite endianness. - - EXPECT_FALSE(empty < empty); - EXPECT_FALSE(a < a); - EXPECT_FALSE(abc < abc); - EXPECT_FALSE(def < def); - - EXPECT_TRUE(empty < a); - EXPECT_FALSE(a < empty); - - EXPECT_TRUE(empty < abc); - EXPECT_FALSE(abc < empty); - - EXPECT_TRUE(empty < def); - EXPECT_FALSE(def < empty); - - EXPECT_TRUE(a < abc); - EXPECT_FALSE(abc < a); - - EXPECT_TRUE(a < def); - EXPECT_FALSE(def < a); - - EXPECT_TRUE(abc < def); - EXPECT_FALSE(def < abc); -} - -TEST(fxcrt, WideStringCOperatorEQ) { - CFX_WideStringC wide_string_c(L"hello"); - EXPECT_TRUE(wide_string_c == wide_string_c); - - CFX_WideStringC wide_string_c_same1(L"hello"); - EXPECT_TRUE(wide_string_c == wide_string_c_same1); - EXPECT_TRUE(wide_string_c_same1 == wide_string_c); - - CFX_WideStringC wide_string_c_same2(wide_string_c); - EXPECT_TRUE(wide_string_c == wide_string_c_same2); - EXPECT_TRUE(wide_string_c_same2 == wide_string_c); - - CFX_WideStringC wide_string_c1(L"he"); - CFX_WideStringC wide_string_c2(L"hellp"); - CFX_WideStringC wide_string_c3(L"hellod"); - EXPECT_FALSE(wide_string_c == wide_string_c1); - EXPECT_FALSE(wide_string_c == wide_string_c2); - EXPECT_FALSE(wide_string_c == wide_string_c3); - EXPECT_FALSE(wide_string_c1 == wide_string_c); - EXPECT_FALSE(wide_string_c2 == wide_string_c); - EXPECT_FALSE(wide_string_c3 == wide_string_c); - - CFX_WideString wide_string_same1(L"hello"); - EXPECT_TRUE(wide_string_c == wide_string_same1); - EXPECT_TRUE(wide_string_same1 == wide_string_c); - - CFX_WideString wide_string1(L"he"); - CFX_WideString wide_string2(L"hellp"); - CFX_WideString wide_string3(L"hellod"); - EXPECT_FALSE(wide_string_c == wide_string1); - EXPECT_FALSE(wide_string_c == wide_string2); - EXPECT_FALSE(wide_string_c == wide_string3); - EXPECT_FALSE(wide_string1 == wide_string_c); - EXPECT_FALSE(wide_string2 == wide_string_c); - EXPECT_FALSE(wide_string3 == wide_string_c); - - const wchar_t* c_string_same1 = L"hello"; - EXPECT_TRUE(wide_string_c == c_string_same1); - EXPECT_TRUE(c_string_same1 == wide_string_c); - - const wchar_t* c_string1 = L"he"; - const wchar_t* c_string2 = L"hellp"; - const wchar_t* c_string3 = L"hellod"; - EXPECT_FALSE(wide_string_c == c_string1); - EXPECT_FALSE(wide_string_c == c_string2); - EXPECT_FALSE(wide_string_c == c_string3); - - EXPECT_FALSE(c_string1 == wide_string_c); - EXPECT_FALSE(c_string2 == wide_string_c); - EXPECT_FALSE(c_string3 == wide_string_c); -} - -TEST(fxcrt, WideStringCOperatorNE) { - CFX_WideStringC wide_string_c(L"hello"); - EXPECT_FALSE(wide_string_c != wide_string_c); - - CFX_WideStringC wide_string_c_same1(L"hello"); - EXPECT_FALSE(wide_string_c != wide_string_c_same1); - EXPECT_FALSE(wide_string_c_same1 != wide_string_c); - - CFX_WideStringC wide_string_c_same2(wide_string_c); - EXPECT_FALSE(wide_string_c != wide_string_c_same2); - EXPECT_FALSE(wide_string_c_same2 != wide_string_c); - - CFX_WideStringC wide_string_c1(L"he"); - CFX_WideStringC wide_string_c2(L"hellp"); - CFX_WideStringC wide_string_c3(L"hellod"); - EXPECT_TRUE(wide_string_c != wide_string_c1); - EXPECT_TRUE(wide_string_c != wide_string_c2); - EXPECT_TRUE(wide_string_c != wide_string_c3); - EXPECT_TRUE(wide_string_c1 != wide_string_c); - EXPECT_TRUE(wide_string_c2 != wide_string_c); - EXPECT_TRUE(wide_string_c3 != wide_string_c); - - CFX_WideString wide_string_same1(L"hello"); - EXPECT_FALSE(wide_string_c != wide_string_same1); - EXPECT_FALSE(wide_string_same1 != wide_string_c); - - CFX_WideString wide_string1(L"he"); - CFX_WideString wide_string2(L"hellp"); - CFX_WideString wide_string3(L"hellod"); - EXPECT_TRUE(wide_string_c != wide_string1); - EXPECT_TRUE(wide_string_c != wide_string2); - EXPECT_TRUE(wide_string_c != wide_string3); - EXPECT_TRUE(wide_string1 != wide_string_c); - EXPECT_TRUE(wide_string2 != wide_string_c); - EXPECT_TRUE(wide_string3 != wide_string_c); - - const wchar_t* c_string_same1 = L"hello"; - EXPECT_FALSE(wide_string_c != c_string_same1); - EXPECT_FALSE(c_string_same1 != wide_string_c); - - const wchar_t* c_string1 = L"he"; - const wchar_t* c_string2 = L"hellp"; - const wchar_t* c_string3 = L"hellod"; - EXPECT_TRUE(wide_string_c != c_string1); - EXPECT_TRUE(wide_string_c != c_string2); - EXPECT_TRUE(wide_string_c != c_string3); - - EXPECT_TRUE(c_string1 != wide_string_c); - EXPECT_TRUE(c_string2 != wide_string_c); - EXPECT_TRUE(c_string3 != wide_string_c); -} - -TEST(fxcrt, WideStringCFind) { - CFX_WideStringC null_string; - EXPECT_EQ(-1, null_string.Find(L'a')); - EXPECT_EQ(-1, null_string.Find(0)); - - CFX_WideStringC empty_string(L""); - EXPECT_EQ(-1, empty_string.Find(L'a')); - EXPECT_EQ(-1, empty_string.Find(0)); - - CFX_WideStringC single_string(L"a"); - EXPECT_EQ(0, single_string.Find(L'a')); - EXPECT_EQ(-1, single_string.Find(L'b')); - EXPECT_EQ(-1, single_string.Find(0)); - - CFX_WideStringC longer_string(L"abccc"); - EXPECT_EQ(0, longer_string.Find(L'a')); - EXPECT_EQ(2, longer_string.Find(L'c')); - EXPECT_EQ(-1, longer_string.Find(0)); - - CFX_WideStringC hibyte_string( - L"ab\xff08" - L"def"); - EXPECT_EQ(2, hibyte_string.Find(L'\xff08')); -} - -TEST(fxcrt, WideStringFormatWidth) { - { - CFX_WideString str; - str.Format(L"%5d", 1); - EXPECT_EQ(L" 1", str); - } - - { - CFX_WideString str; - str.Format(L"%d", 1); - EXPECT_EQ(L"1", str); - } - - { - CFX_WideString str; - str.Format(L"%*d", 5, 1); - EXPECT_EQ(L" 1", str); - } - - { - CFX_WideString str; - str.Format(L"%-1d", 1); - EXPECT_EQ(L"1", str); - } - - { - CFX_WideString str; - str.Format(L"%0d", 1); - EXPECT_EQ(L"1", str); - } - - { - CFX_WideString str; - str.Format(L"%1048576d", 1); - EXPECT_EQ(L"", str); - } -} - -TEST(fxcrt, WideStringFormatPrecision) { - { - CFX_WideString str; - str.Format(L"%.2f", 1.12345); - EXPECT_EQ(L"1.12", str); - } - - { - CFX_WideString str; - str.Format(L"%.*f", 3, 1.12345); - EXPECT_EQ(L"1.123", str); - } - - { - CFX_WideString str; - str.Format(L"%f", 1.12345); - EXPECT_EQ(L"1.123450", str); - } - - { - CFX_WideString str; - str.Format(L"%-1f", 1.12345); - EXPECT_EQ(L"1.123450", str); - } - - { - CFX_WideString str; - str.Format(L"%0f", 1.12345); - EXPECT_EQ(L"1.123450", str); - } - - { - CFX_WideString str; - str.Format(L"%.1048576f", 1.2); - EXPECT_EQ(L"", str); - } -} - -TEST(fxcrt, EmptyWideString) { - CFX_WideString empty_str; - EXPECT_TRUE(empty_str.IsEmpty()); - EXPECT_EQ(0, empty_str.GetLength()); - const wchar_t* cstr = empty_str.c_str(); - EXPECT_EQ(0, FXSYS_wcslen(cstr)); -} diff --git a/core/fxcrt/fx_string.h b/core/fxcrt/fx_string.h index f276853197..9cc0b251eb 100644 --- a/core/fxcrt/fx_string.h +++ b/core/fxcrt/fx_string.h @@ -7,452 +7,17 @@ #ifndef CORE_FXCRT_FX_STRING_H_ #define CORE_FXCRT_FX_STRING_H_ -#include // For intptr_t. - -#include -#include - -#include "core/fxcrt/cfx_retain_ptr.h" -#include "core/fxcrt/cfx_string_c_template.h" -#include "core/fxcrt/cfx_string_data_template.h" -#include "core/fxcrt/fx_memory.h" -#include "core/fxcrt/fx_system.h" - -class CFX_ByteString; -class CFX_WideString; - -using CFX_ByteStringC = CFX_StringCTemplate; -using CFX_WideStringC = CFX_StringCTemplate; +#include "core/fxcrt/cfx_bytestring.h" +#include "core/fxcrt/cfx_widestring.h" #define FXBSTR_ID(c1, c2, c3, c4) \ (((uint32_t)c1 << 24) | ((uint32_t)c2 << 16) | ((uint32_t)c3 << 8) | \ ((uint32_t)c4)) -// A mutable string with shared buffers using copy-on-write semantics that -// avoids the cost of std::string's iterator stability guarantees. -class CFX_ByteString { - public: - using CharType = char; - - CFX_ByteString(); - CFX_ByteString(const CFX_ByteString& other); - CFX_ByteString(CFX_ByteString&& other) noexcept; - - // Deliberately implicit to avoid calling on every string literal. - // NOLINTNEXTLINE(runtime/explicit) - CFX_ByteString(char ch); - // NOLINTNEXTLINE(runtime/explicit) - CFX_ByteString(const char* ptr); - - CFX_ByteString(const char* ptr, FX_STRSIZE len); - CFX_ByteString(const uint8_t* ptr, FX_STRSIZE len); - - explicit CFX_ByteString(const CFX_ByteStringC& bstrc); - CFX_ByteString(const CFX_ByteStringC& bstrc1, const CFX_ByteStringC& bstrc2); - - ~CFX_ByteString(); - - void clear() { m_pData.Reset(); } - - static CFX_ByteString FromUnicode(const wchar_t* ptr, FX_STRSIZE len = -1); - static CFX_ByteString FromUnicode(const CFX_WideString& str); - - // Explicit conversion to C-style string. - // Note: Any subsequent modification of |this| will invalidate the result. - const char* c_str() const { return m_pData ? m_pData->m_String : ""; } - - // Explicit conversion to uint8_t*. - // Note: Any subsequent modification of |this| will invalidate the result. - const uint8_t* raw_str() const { - return m_pData ? reinterpret_cast(m_pData->m_String) - : nullptr; - } - - // Explicit conversion to CFX_ByteStringC. - // Note: Any subsequent modification of |this| will invalidate the result. - CFX_ByteStringC AsStringC() const { - return CFX_ByteStringC(raw_str(), GetLength()); - } - - FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } - bool IsEmpty() const { return !GetLength(); } - - int Compare(const CFX_ByteStringC& str) const; - bool EqualNoCase(const CFX_ByteStringC& str) const; - - bool operator==(const char* ptr) const; - bool operator==(const CFX_ByteStringC& str) const; - bool operator==(const CFX_ByteString& other) const; - - bool operator!=(const char* ptr) const { return !(*this == ptr); } - bool operator!=(const CFX_ByteStringC& str) const { return !(*this == str); } - bool operator!=(const CFX_ByteString& other) const { - return !(*this == other); - } - - bool operator<(const CFX_ByteString& str) const; - - const CFX_ByteString& operator=(const char* str); - const CFX_ByteString& operator=(const CFX_ByteStringC& bstrc); - const CFX_ByteString& operator=(const CFX_ByteString& stringSrc); - - const CFX_ByteString& operator+=(char ch); - const CFX_ByteString& operator+=(const char* str); - const CFX_ByteString& operator+=(const CFX_ByteString& str); - const CFX_ByteString& operator+=(const CFX_ByteStringC& bstrc); - - uint8_t GetAt(FX_STRSIZE nIndex) const { - return m_pData ? m_pData->m_String[nIndex] : 0; - } - - uint8_t operator[](FX_STRSIZE nIndex) const { - return m_pData ? m_pData->m_String[nIndex] : 0; - } - - void SetAt(FX_STRSIZE nIndex, char ch); - FX_STRSIZE Insert(FX_STRSIZE index, char ch); - FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); - - void Format(const char* lpszFormat, ...); - void FormatV(const char* lpszFormat, va_list argList); - - void Reserve(FX_STRSIZE len); - char* GetBuffer(FX_STRSIZE len); - void ReleaseBuffer(FX_STRSIZE len = -1); - - CFX_ByteString Mid(FX_STRSIZE first) const; - CFX_ByteString Mid(FX_STRSIZE first, FX_STRSIZE count) const; - CFX_ByteString Left(FX_STRSIZE count) const; - CFX_ByteString Right(FX_STRSIZE count) const; - - FX_STRSIZE Find(const CFX_ByteStringC& lpszSub, FX_STRSIZE start = 0) const; - FX_STRSIZE Find(char ch, FX_STRSIZE start = 0) const; - FX_STRSIZE ReverseFind(char ch) const; - - void MakeLower(); - void MakeUpper(); - - void TrimRight(); - void TrimRight(char chTarget); - void TrimRight(const CFX_ByteStringC& lpszTargets); - - void TrimLeft(); - void TrimLeft(char chTarget); - void TrimLeft(const CFX_ByteStringC& lpszTargets); - - FX_STRSIZE Replace(const CFX_ByteStringC& lpszOld, - const CFX_ByteStringC& lpszNew); - - FX_STRSIZE Remove(char ch); - - CFX_WideString UTF8Decode() const; - - uint32_t GetID(FX_STRSIZE start_pos = 0) const; - -#define FXFORMAT_SIGNED 1 -#define FXFORMAT_HEX 2 -#define FXFORMAT_CAPITAL 4 - - static CFX_ByteString FormatInteger(int i, uint32_t flags = 0); - static CFX_ByteString FormatFloat(float f, int precision = 0); - - protected: - using StringData = CFX_StringDataTemplate; - - void ReallocBeforeWrite(FX_STRSIZE nNewLen); - void AllocBeforeWrite(FX_STRSIZE nNewLen); - void AllocCopy(CFX_ByteString& dest, - FX_STRSIZE nCopyLen, - FX_STRSIZE nCopyIndex) const; - void AssignCopy(const char* pSrcData, FX_STRSIZE nSrcLen); - void Concat(const char* lpszSrcData, FX_STRSIZE nSrcLen); - - CFX_RetainPtr m_pData; - - friend class fxcrt_ByteStringConcat_Test; - friend class fxcrt_ByteStringPool_Test; -}; - -inline bool operator==(const char* lhs, const CFX_ByteString& rhs) { - return rhs == lhs; -} -inline bool operator==(const CFX_ByteStringC& lhs, const CFX_ByteString& rhs) { - return rhs == lhs; -} -inline bool operator!=(const char* lhs, const CFX_ByteString& rhs) { - return rhs != lhs; -} -inline bool operator!=(const CFX_ByteStringC& lhs, const CFX_ByteString& rhs) { - return rhs != lhs; -} - -inline CFX_ByteString operator+(const CFX_ByteStringC& str1, - const CFX_ByteStringC& str2) { - return CFX_ByteString(str1, str2); -} -inline CFX_ByteString operator+(const CFX_ByteStringC& str1, const char* str2) { - return CFX_ByteString(str1, str2); -} -inline CFX_ByteString operator+(const char* str1, const CFX_ByteStringC& str2) { - return CFX_ByteString(str1, str2); -} -inline CFX_ByteString operator+(const CFX_ByteStringC& str1, char ch) { - return CFX_ByteString(str1, CFX_ByteStringC(ch)); -} -inline CFX_ByteString operator+(char ch, const CFX_ByteStringC& str2) { - return CFX_ByteString(ch, str2); -} -inline CFX_ByteString operator+(const CFX_ByteString& str1, - const CFX_ByteString& str2) { - return CFX_ByteString(str1.AsStringC(), str2.AsStringC()); -} -inline CFX_ByteString operator+(const CFX_ByteString& str1, char ch) { - return CFX_ByteString(str1.AsStringC(), CFX_ByteStringC(ch)); -} -inline CFX_ByteString operator+(char ch, const CFX_ByteString& str2) { - return CFX_ByteString(ch, str2.AsStringC()); -} -inline CFX_ByteString operator+(const CFX_ByteString& str1, const char* str2) { - return CFX_ByteString(str1.AsStringC(), str2); -} -inline CFX_ByteString operator+(const char* str1, const CFX_ByteString& str2) { - return CFX_ByteString(str1, str2.AsStringC()); -} -inline CFX_ByteString operator+(const CFX_ByteString& str1, - const CFX_ByteStringC& str2) { - return CFX_ByteString(str1.AsStringC(), str2); -} -inline CFX_ByteString operator+(const CFX_ByteStringC& str1, - const CFX_ByteString& str2) { - return CFX_ByteString(str1, str2.AsStringC()); -} - -// A mutable string with shared buffers using copy-on-write semantics that -// avoids the cost of std::string's iterator stability guarantees. -class CFX_WideString { - public: - using CharType = wchar_t; - - CFX_WideString(); - CFX_WideString(const CFX_WideString& other); - CFX_WideString(CFX_WideString&& other) noexcept; - - // Deliberately implicit to avoid calling on every string literal. - // NOLINTNEXTLINE(runtime/explicit) - CFX_WideString(wchar_t ch); - // NOLINTNEXTLINE(runtime/explicit) - CFX_WideString(const wchar_t* ptr); - - CFX_WideString(const wchar_t* ptr, FX_STRSIZE len); - - explicit CFX_WideString(const CFX_WideStringC& str); - CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2); - - ~CFX_WideString(); - - static CFX_WideString FromLocal(const CFX_ByteStringC& str); - static CFX_WideString FromCodePage(const CFX_ByteStringC& str, - uint16_t codepage); - - static CFX_WideString FromUTF8(const CFX_ByteStringC& str); - static CFX_WideString FromUTF16LE(const unsigned short* str, FX_STRSIZE len); - - static FX_STRSIZE WStringLength(const unsigned short* str); - - // Explicit conversion to C-style wide string. - // Note: Any subsequent modification of |this| will invalidate the result. - const wchar_t* c_str() const { return m_pData ? m_pData->m_String : L""; } - - // Explicit conversion to CFX_WideStringC. - // Note: Any subsequent modification of |this| will invalidate the result. - CFX_WideStringC AsStringC() const { - return CFX_WideStringC(c_str(), GetLength()); - } - - void clear() { m_pData.Reset(); } - - FX_STRSIZE GetLength() const { return m_pData ? m_pData->m_nDataLength : 0; } - bool IsEmpty() const { return !GetLength(); } - - const CFX_WideString& operator=(const wchar_t* str); - const CFX_WideString& operator=(const CFX_WideString& stringSrc); - const CFX_WideString& operator=(const CFX_WideStringC& stringSrc); - - const CFX_WideString& operator+=(const wchar_t* str); - const CFX_WideString& operator+=(wchar_t ch); - const CFX_WideString& operator+=(const CFX_WideString& str); - const CFX_WideString& operator+=(const CFX_WideStringC& str); - - bool operator==(const wchar_t* ptr) const; - bool operator==(const CFX_WideStringC& str) const; - bool operator==(const CFX_WideString& other) const; - - bool operator!=(const wchar_t* ptr) const { return !(*this == ptr); } - bool operator!=(const CFX_WideStringC& str) const { return !(*this == str); } - bool operator!=(const CFX_WideString& other) const { - return !(*this == other); - } - - bool operator<(const CFX_WideString& str) const; - - wchar_t GetAt(FX_STRSIZE nIndex) const { - return m_pData ? m_pData->m_String[nIndex] : 0; - } - - wchar_t operator[](FX_STRSIZE nIndex) const { - return m_pData ? m_pData->m_String[nIndex] : 0; - } - - void SetAt(FX_STRSIZE nIndex, wchar_t ch); - - int Compare(const wchar_t* str) const; - int Compare(const CFX_WideString& str) const; - int CompareNoCase(const wchar_t* str) const; - - CFX_WideString Mid(FX_STRSIZE first) const; - CFX_WideString Mid(FX_STRSIZE first, FX_STRSIZE count) const; - CFX_WideString Left(FX_STRSIZE count) const; - CFX_WideString Right(FX_STRSIZE count) const; - - FX_STRSIZE Insert(FX_STRSIZE index, wchar_t ch); - FX_STRSIZE Delete(FX_STRSIZE index, FX_STRSIZE count = 1); - - void Format(const wchar_t* lpszFormat, ...); - void FormatV(const wchar_t* lpszFormat, va_list argList); - - void MakeLower(); - void MakeUpper(); - - void TrimRight(); - void TrimRight(wchar_t chTarget); - void TrimRight(const CFX_WideStringC& pTargets); - - void TrimLeft(); - void TrimLeft(wchar_t chTarget); - void TrimLeft(const CFX_WideStringC& pTargets); - - void Reserve(FX_STRSIZE len); - wchar_t* GetBuffer(FX_STRSIZE len); - void ReleaseBuffer(FX_STRSIZE len = -1); - - int GetInteger() const; - float GetFloat() const; - - FX_STRSIZE Find(const CFX_WideStringC& pSub, FX_STRSIZE start = 0) const; - FX_STRSIZE Find(wchar_t ch, FX_STRSIZE start = 0) const; - FX_STRSIZE Replace(const CFX_WideStringC& pOld, const CFX_WideStringC& pNew); - FX_STRSIZE Remove(wchar_t ch); - - CFX_ByteString UTF8Encode() const; - CFX_ByteString UTF16LE_Encode() const; - - protected: - using StringData = CFX_StringDataTemplate; - - void ReallocBeforeWrite(FX_STRSIZE nLen); - void AllocBeforeWrite(FX_STRSIZE nLen); - void AllocCopy(CFX_WideString& dest, - FX_STRSIZE nCopyLen, - FX_STRSIZE nCopyIndex) const; - void AssignCopy(const wchar_t* pSrcData, FX_STRSIZE nSrcLen); - void Concat(const wchar_t* lpszSrcData, FX_STRSIZE nSrcLen); - - // Returns true unless we ran out of space. - bool TryVSWPrintf(FX_STRSIZE size, const wchar_t* format, va_list argList); - - CFX_RetainPtr m_pData; - - friend class fxcrt_WideStringConcatInPlace_Test; - friend class fxcrt_WideStringPool_Test; -}; - -inline CFX_WideString operator+(const CFX_WideStringC& str1, - const CFX_WideStringC& str2) { - return CFX_WideString(str1, str2); -} -inline CFX_WideString operator+(const CFX_WideStringC& str1, - const wchar_t* str2) { - return CFX_WideString(str1, str2); -} -inline CFX_WideString operator+(const wchar_t* str1, - const CFX_WideStringC& str2) { - return CFX_WideString(str1, str2); -} -inline CFX_WideString operator+(const CFX_WideStringC& str1, wchar_t ch) { - return CFX_WideString(str1, CFX_WideStringC(ch)); -} -inline CFX_WideString operator+(wchar_t ch, const CFX_WideStringC& str2) { - return CFX_WideString(ch, str2); -} -inline CFX_WideString operator+(const CFX_WideString& str1, - const CFX_WideString& str2) { - return CFX_WideString(str1.AsStringC(), str2.AsStringC()); -} -inline CFX_WideString operator+(const CFX_WideString& str1, wchar_t ch) { - return CFX_WideString(str1.AsStringC(), CFX_WideStringC(ch)); -} -inline CFX_WideString operator+(wchar_t ch, const CFX_WideString& str2) { - return CFX_WideString(ch, str2.AsStringC()); -} -inline CFX_WideString operator+(const CFX_WideString& str1, - const wchar_t* str2) { - return CFX_WideString(str1.AsStringC(), str2); -} -inline CFX_WideString operator+(const wchar_t* str1, - const CFX_WideString& str2) { - return CFX_WideString(str1, str2.AsStringC()); -} -inline CFX_WideString operator+(const CFX_WideString& str1, - const CFX_WideStringC& str2) { - return CFX_WideString(str1.AsStringC(), str2); -} -inline CFX_WideString operator+(const CFX_WideStringC& str1, - const CFX_WideString& str2) { - return CFX_WideString(str1, str2.AsStringC()); -} -inline bool operator==(const wchar_t* lhs, const CFX_WideString& rhs) { - return rhs == lhs; -} -inline bool operator==(const CFX_WideStringC& lhs, const CFX_WideString& rhs) { - return rhs == lhs; -} -inline bool operator!=(const wchar_t* lhs, const CFX_WideString& rhs) { - return rhs != lhs; -} -inline bool operator!=(const CFX_WideStringC& lhs, const CFX_WideString& rhs) { - return rhs != lhs; -} - CFX_ByteString FX_UTF8Encode(const CFX_WideStringC& wsStr); float FX_atof(const CFX_ByteStringC& str); -inline float FX_atof(const CFX_WideStringC& wsStr) { - return FX_atof(FX_UTF8Encode(wsStr).c_str()); -} +float FX_atof(const CFX_WideStringC& wsStr); bool FX_atonum(const CFX_ByteStringC& str, void* pData); FX_STRSIZE FX_ftoa(float f, char* buf); -uint32_t FX_HashCode_GetA(const CFX_ByteStringC& str, bool bIgnoreCase); -uint32_t FX_HashCode_GetW(const CFX_WideStringC& str, bool bIgnoreCase); - -namespace std { - -template <> -struct hash { - std::size_t operator()(const CFX_ByteString& str) const { - return FX_HashCode_GetA(str.AsStringC(), false); - } -}; - -template <> -struct hash { - std::size_t operator()(const CFX_WideString& str) const { - return FX_HashCode_GetW(str.AsStringC(), false); - } -}; - -} // namespace std - -extern template struct std::hash; -extern template struct std::hash; - #endif // CORE_FXCRT_FX_STRING_H_ -- cgit v1.2.3