1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../include/fxcrt/fx_basic.h"
8 #include "../../../third_party/numerics/safe_math.h"
10 static CFX_StringDataW* FX_AllocStringW(int nLen)
12 if (nLen == 0 || nLen < 0) {
16 base::CheckedNumeric<int> iSize = static_cast<int>(sizeof(FX_WCHAR));
18 iSize += sizeof(long) * 3;
19 CFX_StringDataW* pData = (CFX_StringDataW*)FX_Alloc(FX_BYTE, iSize.ValueOrDie());
24 // TODO(palmer): |nLen| should really be declared as |size_t|, but for
25 // now I just want to fix the overflow without changing any interfaces.
26 // Declaring |nLen| as |size_t| will also simplify the above code
28 pData->m_nAllocLength = nLen;
29 pData->m_nDataLength = nLen;
31 pData->m_String[nLen] = 0;
34 static void FX_ReleaseStringW(CFX_StringDataW* pData)
40 if (pData->m_nRefs <= 0) {
44 CFX_WideString::~CFX_WideString()
46 if (m_pData == NULL) {
50 if (m_pData->m_nRefs < 1) {
54 void CFX_WideString::InitStr(FX_LPCWSTR lpsz, FX_STRSIZE nLen)
57 nLen = lpsz ? (FX_STRSIZE)FXSYS_wcslen(lpsz) : 0;
60 m_pData = FX_AllocStringW(nLen);
64 FXSYS_memcpy32(m_pData->m_String, lpsz, nLen * sizeof(FX_WCHAR));
69 CFX_WideString::CFX_WideString(const CFX_WideString& stringSrc)
71 if (stringSrc.m_pData == NULL) {
75 if (stringSrc.m_pData->m_nRefs >= 0) {
76 m_pData = stringSrc.m_pData;
83 CFX_WideString::CFX_WideString(FX_WCHAR ch)
85 m_pData = FX_AllocStringW(1);
87 m_pData->m_String[0] = ch;
90 CFX_WideString::CFX_WideString(const CFX_WideStringC& str)
96 m_pData = FX_AllocStringW(str.GetLength());
98 FXSYS_memcpy32(m_pData->m_String, str.GetPtr(), str.GetLength()*sizeof(FX_WCHAR));
101 CFX_WideString::CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2)
104 int nNewLen = str1.GetLength() + str2.GetLength();
108 m_pData = FX_AllocStringW(nNewLen);
110 FXSYS_memcpy32(m_pData->m_String, str1.GetPtr(), str1.GetLength()*sizeof(FX_WCHAR));
111 FXSYS_memcpy32(m_pData->m_String + str1.GetLength(), str2.GetPtr(), str2.GetLength()*sizeof(FX_WCHAR));
114 void CFX_WideString::ReleaseBuffer(FX_STRSIZE nNewLength)
116 if (m_pData == NULL) {
120 if (nNewLength == -1) {
121 nNewLength = m_pData ? (FX_STRSIZE)FXSYS_wcslen(m_pData->m_String) : 0;
123 if (nNewLength == 0) {
127 FXSYS_assert(nNewLength <= m_pData->m_nAllocLength);
128 m_pData->m_nDataLength = nNewLength;
129 m_pData->m_String[nNewLength] = 0;
131 const CFX_WideString& CFX_WideString::operator=(FX_LPCWSTR lpsz)
133 if (lpsz == NULL || lpsz[0] == 0) {
136 AssignCopy((FX_STRSIZE)FXSYS_wcslen(lpsz), lpsz);
140 const CFX_WideString& CFX_WideString::operator=(const CFX_WideStringC& stringSrc)
142 if (stringSrc.IsEmpty()) {
145 AssignCopy(stringSrc.GetLength(), stringSrc.GetPtr());
149 const CFX_WideString& CFX_WideString::operator=(const CFX_WideString& stringSrc)
151 if (m_pData == stringSrc.m_pData) {
154 if (stringSrc.IsEmpty()) {
156 } else if ((m_pData && m_pData->m_nRefs < 0) ||
157 (stringSrc.m_pData && stringSrc.m_pData->m_nRefs < 0)) {
158 AssignCopy(stringSrc.m_pData->m_nDataLength, stringSrc.m_pData->m_String);
161 m_pData = stringSrc.m_pData;
168 const CFX_WideString& CFX_WideString::operator+=(FX_WCHAR ch)
170 ConcatInPlace(1, &ch);
173 const CFX_WideString& CFX_WideString::operator+=(FX_LPCWSTR lpsz)
176 ConcatInPlace((FX_STRSIZE)FXSYS_wcslen(lpsz), lpsz);
180 const CFX_WideString& CFX_WideString::operator+=(const CFX_WideString& string)
182 if (string.m_pData == NULL) {
185 ConcatInPlace(string.m_pData->m_nDataLength, string.m_pData->m_String);
188 const CFX_WideString& CFX_WideString::operator+=(const CFX_WideStringC& string)
190 if (string.IsEmpty()) {
193 ConcatInPlace(string.GetLength(), string.GetPtr());
196 bool operator==(const CFX_WideString& s1, FX_LPCWSTR s2)
200 bool operator==(FX_LPCWSTR s1, const CFX_WideString& s2)
204 bool operator==(const CFX_WideString& s1, const CFX_WideString& s2)
208 bool operator==(const CFX_WideString& s1, const CFX_WideStringC& s2)
212 bool operator==(const CFX_WideStringC& s1, const CFX_WideString& s2)
216 bool operator != (const CFX_WideString& s1, FX_LPCWSTR s2)
218 return !s1.Equal(s2);
220 bool operator!=(const CFX_WideString& s1, const CFX_WideString& s2)
222 return !s1.Equal(s2);
224 bool operator!=(const CFX_WideString& s1, const CFX_WideStringC& s2)
226 return !s1.Equal(s2);
228 bool operator!=(const CFX_WideStringC& s1, const CFX_WideString& s2)
230 return !s2.Equal(s1);
232 bool CFX_WideString::Equal(const CFX_WideStringC& str) const
234 if (m_pData == NULL) {
235 return str.IsEmpty();
237 return str.GetLength() == m_pData->m_nDataLength &&
238 FXSYS_memcmp32(str.GetPtr(), m_pData->m_String, m_pData->m_nDataLength * sizeof(FX_WCHAR)) == 0;
240 void CFX_WideString::Empty()
242 if (m_pData == NULL) {
245 if (m_pData->m_nRefs > 1) {
252 void CFX_WideString::ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData)
254 if (nSrcLen == 0 || lpszSrcData == NULL) {
257 if (m_pData == NULL) {
258 m_pData = FX_AllocStringW(nSrcLen);
260 FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(FX_WCHAR));
264 if (m_pData->m_nRefs > 1 || m_pData->m_nDataLength + nSrcLen > m_pData->m_nAllocLength) {
265 CFX_StringDataW* pOldData = m_pData;
266 ConcatCopy(m_pData->m_nDataLength, m_pData->m_String, nSrcLen, lpszSrcData);
267 FX_ReleaseStringW(pOldData);
269 FXSYS_memcpy32(m_pData->m_String + m_pData->m_nDataLength, lpszSrcData, nSrcLen * sizeof(FX_WCHAR));
270 m_pData->m_nDataLength += nSrcLen;
271 m_pData->m_String[m_pData->m_nDataLength] = 0;
274 void CFX_WideString::ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCWSTR lpszSrc1Data,
275 FX_STRSIZE nSrc2Len, FX_LPCWSTR lpszSrc2Data)
277 FX_STRSIZE nNewLen = nSrc1Len + nSrc2Len;
281 m_pData = FX_AllocStringW(nNewLen);
283 FXSYS_memcpy32(m_pData->m_String, lpszSrc1Data, nSrc1Len * sizeof(FX_WCHAR));
284 FXSYS_memcpy32(m_pData->m_String + nSrc1Len, lpszSrc2Data, nSrc2Len * sizeof(FX_WCHAR));
287 void CFX_WideString::CopyBeforeWrite()
289 if (m_pData == NULL || m_pData->m_nRefs <= 1) {
292 CFX_StringDataW* pData = m_pData;
294 FX_STRSIZE nDataLength = pData->m_nDataLength;
295 m_pData = FX_AllocStringW(nDataLength);
296 if (m_pData != NULL) {
297 FXSYS_memcpy32(m_pData->m_String, pData->m_String, (nDataLength + 1) * sizeof(FX_WCHAR));
300 void CFX_WideString::AllocBeforeWrite(FX_STRSIZE nLen)
302 if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nLen) {
306 m_pData = FX_AllocStringW(nLen);
308 void CFX_WideString::AssignCopy(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData)
310 AllocBeforeWrite(nSrcLen);
311 FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(FX_WCHAR));
312 m_pData->m_nDataLength = nSrcLen;
313 m_pData->m_String[nSrcLen] = 0;
315 int CFX_WideString::Compare(FX_LPCWSTR lpsz) const
317 if (m_pData == NULL) {
318 return (lpsz == NULL || lpsz[0] == 0) ? 0 : -1;
320 return FXSYS_wcscmp(m_pData->m_String, lpsz);
322 CFX_ByteString CFX_WideString::UTF8Encode() const
324 return FX_UTF8Encode(*this);
326 CFX_ByteString CFX_WideString::UTF16LE_Encode(FX_BOOL bTerminate) const
328 if (m_pData == NULL) {
329 return bTerminate ? CFX_ByteString(FX_BSTRC("\0\0")) : CFX_ByteString();
331 int len = m_pData->m_nDataLength;
332 CFX_ByteString result;
333 FX_LPSTR buffer = result.GetBuffer(len * 2 + (bTerminate ? 2 : 0));
334 for (int i = 0; i < len; i ++) {
335 buffer[i * 2] = m_pData->m_String[i] & 0xff;
336 buffer[i * 2 + 1] = m_pData->m_String[i] >> 8;
340 buffer[len * 2 + 1] = 0;
341 result.ReleaseBuffer(len * 2 + 2);
343 result.ReleaseBuffer(len * 2);
347 void CFX_WideString::ConvertFrom(const CFX_ByteString& str, CFX_CharMap* pCharMap)
349 if (pCharMap == NULL) {
350 pCharMap = CFX_CharMap::GetDefaultMapper();
352 *this = pCharMap->m_GetWideString(pCharMap, str);
354 void CFX_WideString::Reserve(FX_STRSIZE len)
357 ReleaseBuffer(GetLength());
359 FX_LPWSTR CFX_WideString::GetBuffer(FX_STRSIZE nMinBufLength)
361 if (m_pData == NULL && nMinBufLength == 0) {
364 if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nMinBufLength) {
365 return m_pData->m_String;
367 if (m_pData == NULL) {
368 m_pData = FX_AllocStringW(nMinBufLength);
372 m_pData->m_nDataLength = 0;
373 m_pData->m_String[0] = 0;
374 return m_pData->m_String;
376 CFX_StringDataW* pOldData = m_pData;
377 FX_STRSIZE nOldLen = pOldData->m_nDataLength;
378 if (nMinBufLength < nOldLen) {
379 nMinBufLength = nOldLen;
381 m_pData = FX_AllocStringW(nMinBufLength);
385 FXSYS_memcpy32(m_pData->m_String, pOldData->m_String, (nOldLen + 1)*sizeof(FX_WCHAR));
386 m_pData->m_nDataLength = nOldLen;
387 pOldData->m_nRefs --;
388 if (pOldData->m_nRefs <= 0) {
391 return m_pData->m_String;
393 CFX_WideString CFX_WideString::FromLocal(const char* str, FX_STRSIZE len)
395 CFX_WideString result;
396 result.ConvertFrom(CFX_ByteString(str, len));
399 CFX_WideString CFX_WideString::FromUTF8(const char* str, FX_STRSIZE len)
401 if (!str || 0 == len) {
402 return CFX_WideString();
405 CFX_UTF8Decoder decoder;
406 for (FX_STRSIZE i = 0; i < len; i ++) {
407 decoder.Input(str[i]);
409 return decoder.GetResult();
411 CFX_WideString CFX_WideString::FromUTF16LE(const unsigned short* wstr, FX_STRSIZE wlen)
413 if (!wstr || 0 == wlen) {
414 return CFX_WideString();
417 CFX_WideString result;
418 FX_WCHAR* buf = result.GetBuffer(wlen);
419 for (int i = 0; i < wlen; i ++) {
422 result.ReleaseBuffer(wlen);
425 FX_STRSIZE CFX_WideString::WStringLength(const unsigned short* str)
429 while (str[len]) len++;
435 void CFX_WideString::AllocCopy(CFX_WideString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const
437 // |FX_STRSIZE| is currently typedef'd as in |int|. TODO(palmer): It
438 // should be a |size_t|, or at least unsigned.
439 if (nCopyLen == 0 || nCopyLen < 0) {
442 base::CheckedNumeric<FX_STRSIZE> iSize = static_cast<FX_STRSIZE>(sizeof(FX_WCHAR));
444 ASSERT(dest.m_pData == NULL);
445 dest.m_pData = FX_AllocStringW(nCopyLen);
447 FXSYS_memcpy32(dest.m_pData->m_String, m_pData->m_String + nCopyIndex, iSize.ValueOrDie());
450 CFX_WideString CFX_WideString::Left(FX_STRSIZE nCount) const
452 if (m_pData == NULL) {
453 return CFX_WideString();
458 if (nCount >= m_pData->m_nDataLength) {
462 AllocCopy(dest, nCount, 0);
465 CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst) const
467 return Mid(nFirst, m_pData->m_nDataLength - nFirst);
469 CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const
471 if (m_pData == NULL) {
472 return CFX_WideString();
480 if (nFirst + nCount > m_pData->m_nDataLength) {
481 nCount = m_pData->m_nDataLength - nFirst;
483 if (nFirst > m_pData->m_nDataLength) {
486 if (nFirst == 0 && nFirst + nCount == m_pData->m_nDataLength) {
490 AllocCopy(dest, nCount, nFirst);
493 CFX_WideString CFX_WideString::Right(FX_STRSIZE nCount) const
495 if (m_pData == NULL) {
496 return CFX_WideString();
501 if (nCount >= m_pData->m_nDataLength) {
505 AllocCopy(dest, nCount, m_pData->m_nDataLength - nCount);
508 int CFX_WideString::CompareNoCase(FX_LPCWSTR lpsz) const
510 if (m_pData == NULL) {
511 return (lpsz == NULL || lpsz[0] == 0) ? 0 : -1;
513 return FXSYS_wcsicmp(m_pData->m_String, lpsz);
515 int CFX_WideString::Compare(const CFX_WideString& str) const
517 if (m_pData == NULL) {
518 if (str.m_pData == NULL) {
522 } else if (str.m_pData == NULL) {
525 int this_len = m_pData->m_nDataLength;
526 int that_len = str.m_pData->m_nDataLength;
527 int min_len = this_len < that_len ? this_len : that_len;
528 for (int i = 0; i < min_len; i ++) {
529 if (m_pData->m_String[i] < str.m_pData->m_String[i]) {
531 } else if (m_pData->m_String[i] > str.m_pData->m_String[i]) {
535 if (this_len < that_len) {
537 } else if (this_len > that_len) {
542 FX_LPWSTR CFX_WideString::LockBuffer()
544 if (m_pData == NULL) {
547 FX_LPWSTR lpsz = GetBuffer(0);
548 m_pData->m_nRefs = -1;
551 void CFX_WideString::SetAt(FX_STRSIZE nIndex, FX_WCHAR ch)
553 if (m_pData == NULL) {
557 ASSERT(nIndex < m_pData->m_nDataLength);
559 m_pData->m_String[nIndex] = ch;
561 void CFX_WideString::MakeLower()
563 if (m_pData == NULL) {
567 if (GetLength() < 1) {
570 FXSYS_wcslwr(m_pData->m_String);
572 void CFX_WideString::MakeUpper()
574 if (m_pData == NULL) {
578 if (GetLength() < 1) {
581 FXSYS_wcsupr(m_pData->m_String);
583 FX_STRSIZE CFX_WideString::Find(FX_LPCWSTR lpszSub, FX_STRSIZE nStart) const
585 FX_STRSIZE nLength = GetLength();
586 if (nLength < 1 || nStart > nLength) {
589 FX_LPCWSTR lpsz = (FX_LPCWSTR)FXSYS_wcsstr(m_pData->m_String + nStart, lpszSub);
590 return (lpsz == NULL) ? -1 : (int)(lpsz - m_pData->m_String);
592 FX_STRSIZE CFX_WideString::Find(FX_WCHAR ch, FX_STRSIZE nStart) const
594 if (m_pData == NULL) {
597 FX_STRSIZE nLength = m_pData->m_nDataLength;
598 if (nStart >= nLength) {
601 FX_LPCWSTR lpsz = (FX_LPCWSTR)FXSYS_wcschr(m_pData->m_String + nStart, ch);
602 return (lpsz == NULL) ? -1 : (int)(lpsz - m_pData->m_String);
604 void CFX_WideString::TrimRight(FX_LPCWSTR lpszTargetList)
606 FXSYS_assert(lpszTargetList != NULL);
607 if (m_pData == NULL || *lpszTargetList == 0) {
611 FX_STRSIZE len = GetLength();
615 FX_STRSIZE pos = len;
617 if (FXSYS_wcschr(lpszTargetList, m_pData->m_String[pos - 1]) == NULL) {
623 m_pData->m_String[pos] = 0;
624 m_pData->m_nDataLength = pos;
627 void CFX_WideString::TrimRight(FX_WCHAR chTarget)
629 FX_WCHAR str[2] = {chTarget, 0};
632 void CFX_WideString::TrimRight()
634 TrimRight(L"\x09\x0a\x0b\x0c\x0d\x20");
636 void CFX_WideString::TrimLeft(FX_LPCWSTR lpszTargets)
638 FXSYS_assert(lpszTargets != NULL);
639 if (m_pData == NULL || *lpszTargets == 0) {
643 if (GetLength() < 1) {
646 FX_LPCWSTR lpsz = m_pData->m_String;
648 if (FXSYS_wcschr(lpszTargets, *lpsz) == NULL) {
653 if (lpsz != m_pData->m_String) {
654 int nDataLength = m_pData->m_nDataLength - (FX_STRSIZE)(lpsz - m_pData->m_String);
655 FXSYS_memmove32(m_pData->m_String, lpsz, (nDataLength + 1)*sizeof(FX_WCHAR));
656 m_pData->m_nDataLength = nDataLength;
659 void CFX_WideString::TrimLeft(FX_WCHAR chTarget)
661 FX_WCHAR str[2] = {chTarget, 0};
664 void CFX_WideString::TrimLeft()
666 TrimLeft(L"\x09\x0a\x0b\x0c\x0d\x20");
668 FX_STRSIZE CFX_WideString::Replace(FX_LPCWSTR lpszOld, FX_LPCWSTR lpszNew)
670 if (GetLength() < 1) {
673 if (lpszOld == NULL) {
676 FX_STRSIZE nSourceLen = (FX_STRSIZE)FXSYS_wcslen(lpszOld);
677 if (nSourceLen == 0) {
680 FX_STRSIZE nReplacementLen = lpszNew ? (FX_STRSIZE)FXSYS_wcslen(lpszNew) : 0;
681 FX_STRSIZE nCount = 0;
682 FX_LPWSTR lpszStart = m_pData->m_String;
683 FX_LPWSTR lpszEnd = m_pData->m_String + m_pData->m_nDataLength;
684 FX_LPWSTR lpszTarget;
686 while ((lpszTarget = (FX_LPWSTR)FXSYS_wcsstr(lpszStart, lpszOld)) != NULL && lpszStart < lpszEnd) {
688 lpszStart = lpszTarget + nSourceLen;
693 FX_STRSIZE nOldLength = m_pData->m_nDataLength;
694 FX_STRSIZE nNewLength = nOldLength + (nReplacementLen - nSourceLen) * nCount;
695 if (m_pData->m_nAllocLength < nNewLength || m_pData->m_nRefs > 1) {
696 CFX_StringDataW* pOldData = m_pData;
697 FX_LPCWSTR pstr = m_pData->m_String;
698 m_pData = FX_AllocStringW(nNewLength);
702 FXSYS_memcpy32(m_pData->m_String, pstr, pOldData->m_nDataLength * sizeof(FX_WCHAR));
703 FX_ReleaseStringW(pOldData);
705 lpszStart = m_pData->m_String;
706 lpszEnd = m_pData->m_String + FX_MAX(m_pData->m_nDataLength, nNewLength);
708 while ((lpszTarget = (FX_LPWSTR)FXSYS_wcsstr(lpszStart, lpszOld)) != NULL && lpszStart < lpszEnd) {
709 FX_STRSIZE nBalance = nOldLength - (FX_STRSIZE)(lpszTarget - m_pData->m_String + nSourceLen);
710 FXSYS_memmove32(lpszTarget + nReplacementLen, lpszTarget + nSourceLen, nBalance * sizeof(FX_WCHAR));
711 FXSYS_memcpy32(lpszTarget, lpszNew, nReplacementLen * sizeof(FX_WCHAR));
712 lpszStart = lpszTarget + nReplacementLen;
713 lpszStart[nBalance] = 0;
714 nOldLength += (nReplacementLen - nSourceLen);
717 ASSERT(m_pData->m_String[nNewLength] == 0);
718 m_pData->m_nDataLength = nNewLength;
722 FX_STRSIZE CFX_WideString::Insert(FX_STRSIZE nIndex, FX_WCHAR ch)
728 FX_STRSIZE nNewLength = GetLength();
729 if (nIndex > nNewLength) {
733 if (m_pData == NULL || m_pData->m_nAllocLength < nNewLength) {
734 CFX_StringDataW* pOldData = m_pData;
735 FX_LPCWSTR pstr = m_pData->m_String;
736 m_pData = FX_AllocStringW(nNewLength);
740 if(pOldData != NULL) {
741 FXSYS_memmove32(m_pData->m_String, pstr, (pOldData->m_nDataLength + 1)*sizeof(FX_WCHAR));
742 FX_ReleaseStringW(pOldData);
744 m_pData->m_String[0] = 0;
747 FXSYS_memmove32(m_pData->m_String + nIndex + 1,
748 m_pData->m_String + nIndex, (nNewLength - nIndex)*sizeof(FX_WCHAR));
749 m_pData->m_String[nIndex] = ch;
750 m_pData->m_nDataLength = nNewLength;
753 FX_STRSIZE CFX_WideString::Delete(FX_STRSIZE nIndex, FX_STRSIZE nCount)
755 if (GetLength() < 1) {
761 FX_STRSIZE nOldLength = m_pData->m_nDataLength;
762 if (nCount > 0 && nIndex < nOldLength) {
764 int nBytesToCopy = nOldLength - (nIndex + nCount) + 1;
765 FXSYS_memmove32(m_pData->m_String + nIndex,
766 m_pData->m_String + nIndex + nCount, nBytesToCopy * sizeof(FX_WCHAR));
767 m_pData->m_nDataLength = nOldLength - nCount;
769 return m_pData->m_nDataLength;
771 FX_STRSIZE CFX_WideString::Remove(FX_WCHAR chRemove)
773 if (m_pData == NULL) {
777 if (GetLength() < 1) {
780 FX_LPWSTR pstrSource = m_pData->m_String;
781 FX_LPWSTR pstrDest = m_pData->m_String;
782 FX_LPWSTR pstrEnd = m_pData->m_String + m_pData->m_nDataLength;
783 while (pstrSource < pstrEnd) {
784 if (*pstrSource != chRemove) {
785 *pstrDest = *pstrSource;
791 FX_STRSIZE nCount = (FX_STRSIZE)(pstrSource - pstrDest);
792 m_pData->m_nDataLength -= nCount;
795 #define FORCE_ANSI 0x10000
796 #define FORCE_UNICODE 0x20000
797 #define FORCE_INT64 0x40000
798 void CFX_WideString::FormatV(FX_LPCWSTR lpszFormat, va_list argList)
801 #if defined(__ARMCC_VERSION) || (!defined(_MSC_VER) && (_FX_CPU_ == _FX_X64_ || _FX_CPU_ == _FX_IA64_ || _FX_CPU_ == _FX_ARM64_)) || defined(__native_client__)
802 va_copy(argListSave, argList);
804 argListSave = argList;
807 for (FX_LPCWSTR lpsz = lpszFormat; *lpsz != 0; lpsz ++) {
808 if (*lpsz != '%' || *(lpsz = lpsz + 1) == '%') {
809 nMaxLen += (FX_STRSIZE)FXSYS_wcslen(lpsz);
814 for (; *lpsz != 0; lpsz ++) {
817 } else if (*lpsz == '*') {
818 nWidth = va_arg(argList, int);
819 } else if (*lpsz == '-' || *lpsz == '+' || *lpsz == '0' ||
827 nWidth = FXSYS_wtoi(lpsz);
828 for (; *lpsz != 0 && (*lpsz) <= '9' && (*lpsz) >= '0'; lpsz ++)
831 if (nWidth < 0 || nWidth > 128 * 1024) {
832 lpszFormat = (FX_LPCWSTR)L"Bad width";
840 nPrecision = va_arg(argList, int);
843 nPrecision = FXSYS_wtoi(lpsz);
844 for (; *lpsz != 0 && (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz ++)
848 if (nPrecision < 0 || nPrecision > 128 * 1024) {
849 lpszFormat = (FX_LPCWSTR)L"Bad precision";
854 if (*lpsz == L'I' && *(lpsz + 1) == L'6' && *(lpsz + 2) == L'4') {
856 nModifier = FORCE_INT64;
860 nModifier = FORCE_ANSI;
864 nModifier = FORCE_UNICODE;
874 switch (*lpsz | nModifier) {
878 va_arg(argList, int);
883 va_arg(argList, int);
885 case 'c'|FORCE_UNICODE:
886 case 'C'|FORCE_UNICODE:
888 va_arg(argList, int);
891 FX_LPCWSTR pstrNextArg = va_arg(argList, FX_LPCWSTR);
892 if (pstrNextArg == NULL) {
895 nItemLen = (FX_STRSIZE)FXSYS_wcslen(pstrNextArg);
903 FX_LPCSTR pstrNextArg = va_arg(argList, FX_LPCSTR);
904 if (pstrNextArg == NULL) {
907 nItemLen = (FX_STRSIZE)FXSYS_strlen(pstrNextArg);
915 case 'S'|FORCE_ANSI: {
916 FX_LPCSTR pstrNextArg = va_arg(argList, FX_LPCSTR);
917 if (pstrNextArg == NULL) {
920 nItemLen = (FX_STRSIZE)FXSYS_strlen(pstrNextArg);
927 case 's'|FORCE_UNICODE:
928 case 'S'|FORCE_UNICODE: {
929 FX_LPWSTR pstrNextArg = va_arg(argList, FX_LPWSTR);
930 if (pstrNextArg == NULL) {
933 nItemLen = (FX_STRSIZE)FXSYS_wcslen(pstrNextArg);
942 if (nPrecision != 0 && nItemLen > nPrecision) {
943 nItemLen = nPrecision;
945 if (nItemLen < nWidth) {
956 if (nModifier & FORCE_INT64) {
957 va_arg(argList, FX_INT64);
959 va_arg(argList, int);
962 if (nItemLen < nWidth + nPrecision) {
963 nItemLen = nWidth + nPrecision;
972 va_arg(argList, double);
974 if (nItemLen < nWidth + nPrecision) {
975 nItemLen = nWidth + nPrecision;
979 if (nWidth + nPrecision > 100) {
980 nItemLen = nPrecision + nWidth + 128;
984 f = va_arg(argList, double);
985 FXSYS_snprintf(pszTemp, sizeof(pszTemp), "%*.*f", nWidth, nPrecision + 6, f );
986 nItemLen = (FX_STRSIZE)FXSYS_strlen(pszTemp);
990 va_arg(argList, void*);
992 if (nItemLen < nWidth + nPrecision) {
993 nItemLen = nWidth + nPrecision;
997 va_arg(argList, int*);
1001 nMaxLen += nItemLen;
1005 FXSYS_vswprintf((wchar_t*)m_pData->m_String, nMaxLen + 1, (const wchar_t*)lpszFormat, argListSave);
1008 va_end(argListSave);
1010 void CFX_WideString::Format(FX_LPCWSTR lpszFormat, ...)
1013 va_start(argList, lpszFormat);
1014 FormatV(lpszFormat, argList);
1017 FX_FLOAT FX_wtof(FX_LPCWSTR str, int len)
1023 FX_BOOL bNegative = FALSE;
1024 if (str[0] == '+') {
1026 } else if (str[0] == '-') {
1032 if (str[cc] == '.') {
1035 integer = integer * 10 + str[cc] - '0';
1038 FX_FLOAT fraction = 0;
1039 if (str[cc] == '.') {
1041 FX_FLOAT scale = 0.1f;
1043 fraction += scale * (str[cc] - '0');
1048 fraction += (FX_FLOAT)integer;
1049 return bNegative ? -fraction : fraction;
1051 int CFX_WideString::GetInteger() const
1053 if (m_pData == NULL) {
1056 return FXSYS_wtoi(m_pData->m_String);
1058 FX_FLOAT CFX_WideString::GetFloat() const
1060 if (m_pData == NULL) {
1063 return FX_wtof(m_pData->m_String, m_pData->m_nDataLength);
1065 static CFX_ByteString _DefMap_GetByteString(CFX_CharMap* pCharMap, const CFX_WideString& widestr)
1067 int src_len = widestr.GetLength();
1068 int codepage = pCharMap->m_GetCodePage ? pCharMap->m_GetCodePage() : 0;
1069 int dest_len = FXSYS_WideCharToMultiByte(codepage, 0, widestr, src_len, NULL, 0, NULL, NULL);
1070 if (dest_len == 0) {
1071 return CFX_ByteString();
1073 CFX_ByteString bytestr;
1074 FX_LPSTR dest_buf = bytestr.GetBuffer(dest_len);
1075 FXSYS_WideCharToMultiByte(codepage, 0, widestr, src_len, dest_buf, dest_len, NULL, NULL);
1076 bytestr.ReleaseBuffer(dest_len);
1079 static CFX_WideString _DefMap_GetWideString(CFX_CharMap* pCharMap, const CFX_ByteString& bytestr)
1081 int src_len = bytestr.GetLength();
1082 int codepage = pCharMap->m_GetCodePage ? pCharMap->m_GetCodePage() : 0;
1083 int dest_len = FXSYS_MultiByteToWideChar(codepage, 0, bytestr, src_len, NULL, 0);
1084 if (dest_len == 0) {
1085 return CFX_WideString();
1087 CFX_WideString widestr;
1088 FX_LPWSTR dest_buf = widestr.GetBuffer(dest_len);
1089 FXSYS_MultiByteToWideChar(codepage, 0, bytestr, src_len, dest_buf, dest_len);
1090 widestr.ReleaseBuffer(dest_len);
1093 static int _DefMap_GetGBKCodePage()
1097 static int _DefMap_GetUHCCodePage()
1101 static int _DefMap_GetJISCodePage()
1105 static int _DefMap_GetBig5CodePage()
1109 static const CFX_CharMap g_DefaultMapper = {&_DefMap_GetWideString, &_DefMap_GetByteString, NULL};
1110 static const CFX_CharMap g_DefaultGBKMapper = {&_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetGBKCodePage};
1111 static const CFX_CharMap g_DefaultJISMapper = {&_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetJISCodePage};
1112 static const CFX_CharMap g_DefaultUHCMapper = {&_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetUHCCodePage};
1113 static const CFX_CharMap g_DefaultBig5Mapper = {&_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetBig5CodePage};
1114 CFX_CharMap* CFX_CharMap::GetDefaultMapper(FX_INT32 codepage)
1118 return (CFX_CharMap*)&g_DefaultMapper;
1120 return (CFX_CharMap*)&g_DefaultJISMapper;
1122 return (CFX_CharMap*)&g_DefaultGBKMapper;
1124 return (CFX_CharMap*)&g_DefaultUHCMapper;
1126 return (CFX_CharMap*)&g_DefaultBig5Mapper;