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 <stddef.h> // For offsetof().
9 #include "../../include/fxcrt/fx_basic.h"
10 #include "../../../third_party/base/numerics/safe_math.h"
13 CFX_WideString::StringData* CFX_WideString::StringData::Create(int nLen) {
14 // TODO(palmer): |nLen| should really be declared as |size_t|, or
16 if (nLen == 0 || nLen < 0) {
20 // Fixed portion of header plus a NUL wide char not in m_nAllocLength.
21 int overhead = offsetof(StringData, m_String) + sizeof(FX_WCHAR);
22 pdfium::base::CheckedNumeric<int> iSize = nLen;
23 iSize *= sizeof(FX_WCHAR);
26 // Now round to an 8-byte boundary. We'd expect that this is the minimum
27 // granularity of any of the underlying allocators, so there may be cases
28 // where we can save a re-alloc when adding a few characters to a string
29 // by using this otherwise wasted space.
31 int totalSize = iSize.ValueOrDie() & ~7;
32 int usableLen = (totalSize - overhead) / sizeof(FX_WCHAR);
33 FXSYS_assert(usableLen >= nLen);
35 void* pData = FX_Alloc(uint8_t, iSize.ValueOrDie());
36 return new (pData) StringData(nLen, usableLen);
38 CFX_WideString::~CFX_WideString() {
43 CFX_WideString::CFX_WideString(const CFX_WideString& stringSrc) {
44 if (stringSrc.m_pData == NULL) {
48 if (stringSrc.m_pData->m_nRefs >= 0) {
49 m_pData = stringSrc.m_pData;
56 CFX_WideString::CFX_WideString(const FX_WCHAR* lpsz, FX_STRSIZE nLen) {
58 nLen = lpsz ? FXSYS_wcslen(lpsz) : 0;
61 m_pData = StringData::Create(nLen);
63 FXSYS_memcpy(m_pData->m_String, lpsz, nLen * sizeof(FX_WCHAR));
69 CFX_WideString::CFX_WideString(FX_WCHAR ch) {
70 m_pData = StringData::Create(1);
72 m_pData->m_String[0] = ch;
75 CFX_WideString::CFX_WideString(const CFX_WideStringC& str) {
80 m_pData = StringData::Create(str.GetLength());
82 FXSYS_memcpy(m_pData->m_String, str.GetPtr(),
83 str.GetLength() * sizeof(FX_WCHAR));
86 CFX_WideString::CFX_WideString(const CFX_WideStringC& str1,
87 const CFX_WideStringC& str2) {
89 int nNewLen = str1.GetLength() + str2.GetLength();
93 m_pData = StringData::Create(nNewLen);
95 FXSYS_memcpy(m_pData->m_String, str1.GetPtr(),
96 str1.GetLength() * sizeof(FX_WCHAR));
97 FXSYS_memcpy(m_pData->m_String + str1.GetLength(), str2.GetPtr(),
98 str2.GetLength() * sizeof(FX_WCHAR));
101 void CFX_WideString::ReleaseBuffer(FX_STRSIZE nNewLength) {
102 if (m_pData == NULL) {
106 if (nNewLength == -1) {
107 nNewLength = m_pData ? FXSYS_wcslen(m_pData->m_String) : 0;
109 if (nNewLength == 0) {
113 FXSYS_assert(nNewLength <= m_pData->m_nAllocLength);
114 m_pData->m_nDataLength = nNewLength;
115 m_pData->m_String[nNewLength] = 0;
117 const CFX_WideString& CFX_WideString::operator=(const FX_WCHAR* lpsz) {
118 if (lpsz == NULL || lpsz[0] == 0) {
121 AssignCopy(FXSYS_wcslen(lpsz), lpsz);
125 const CFX_WideString& CFX_WideString::operator=(
126 const CFX_WideStringC& stringSrc) {
127 if (stringSrc.IsEmpty()) {
130 AssignCopy(stringSrc.GetLength(), stringSrc.GetPtr());
134 const CFX_WideString& CFX_WideString::operator=(
135 const CFX_WideString& stringSrc) {
136 if (m_pData == stringSrc.m_pData) {
139 if (stringSrc.IsEmpty()) {
141 } else if ((m_pData && m_pData->m_nRefs < 0) ||
142 (stringSrc.m_pData && stringSrc.m_pData->m_nRefs < 0)) {
143 AssignCopy(stringSrc.m_pData->m_nDataLength, stringSrc.m_pData->m_String);
146 m_pData = stringSrc.m_pData;
153 const CFX_WideString& CFX_WideString::operator+=(FX_WCHAR ch) {
154 ConcatInPlace(1, &ch);
157 const CFX_WideString& CFX_WideString::operator+=(const FX_WCHAR* lpsz) {
159 ConcatInPlace(FXSYS_wcslen(lpsz), lpsz);
163 const CFX_WideString& CFX_WideString::operator+=(const CFX_WideString& string) {
164 if (string.m_pData == NULL) {
167 ConcatInPlace(string.m_pData->m_nDataLength, string.m_pData->m_String);
170 const CFX_WideString& CFX_WideString::operator+=(
171 const CFX_WideStringC& string) {
172 if (string.IsEmpty()) {
175 ConcatInPlace(string.GetLength(), string.GetPtr());
178 bool CFX_WideString::Equal(const wchar_t* ptr) const {
180 return !ptr || ptr[0] == L'\0';
183 return m_pData->m_nDataLength == 0;
185 return wcslen(ptr) == m_pData->m_nDataLength &&
186 wmemcmp(ptr, m_pData->m_String, m_pData->m_nDataLength) == 0;
188 bool CFX_WideString::Equal(const CFX_WideStringC& str) const {
189 if (m_pData == NULL) {
190 return str.IsEmpty();
192 return str.GetLength() == m_pData->m_nDataLength &&
193 wmemcmp(str.GetPtr(), m_pData->m_String, m_pData->m_nDataLength) == 0;
195 bool CFX_WideString::Equal(const CFX_WideString& other) const {
197 return other.IsEmpty();
199 if (other.IsEmpty()) {
202 return other.m_pData->m_nDataLength == m_pData->m_nDataLength &&
203 wmemcmp(other.m_pData->m_String, m_pData->m_String,
204 m_pData->m_nDataLength) == 0;
206 void CFX_WideString::Empty() {
212 void CFX_WideString::ConcatInPlace(FX_STRSIZE nSrcLen,
213 const FX_WCHAR* lpszSrcData) {
214 if (nSrcLen == 0 || lpszSrcData == NULL) {
217 if (m_pData == NULL) {
218 m_pData = StringData::Create(nSrcLen);
220 FXSYS_memcpy(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(FX_WCHAR));
224 if (m_pData->m_nRefs > 1 ||
225 m_pData->m_nDataLength + nSrcLen > m_pData->m_nAllocLength) {
226 ConcatCopy(m_pData->m_nDataLength, m_pData->m_String, nSrcLen, lpszSrcData);
228 FXSYS_memcpy(m_pData->m_String + m_pData->m_nDataLength, lpszSrcData,
229 nSrcLen * sizeof(FX_WCHAR));
230 m_pData->m_nDataLength += nSrcLen;
231 m_pData->m_String[m_pData->m_nDataLength] = 0;
234 void CFX_WideString::ConcatCopy(FX_STRSIZE nSrc1Len,
235 const FX_WCHAR* lpszSrc1Data,
237 const FX_WCHAR* lpszSrc2Data) {
238 FX_STRSIZE nNewLen = nSrc1Len + nSrc2Len;
242 // Don't release until done copying, might be one of the arguments.
243 StringData* pOldData = m_pData;
244 m_pData = StringData::Create(nNewLen);
246 wmemcpy(m_pData->m_String, lpszSrc1Data, nSrc1Len);
247 wmemcpy(m_pData->m_String + nSrc1Len, lpszSrc2Data, nSrc2Len);
251 void CFX_WideString::CopyBeforeWrite() {
252 if (m_pData == NULL || m_pData->m_nRefs <= 1) {
255 StringData* pData = m_pData;
257 FX_STRSIZE nDataLength = pData->m_nDataLength;
258 m_pData = StringData::Create(nDataLength);
259 if (m_pData != NULL) {
260 FXSYS_memcpy(m_pData->m_String, pData->m_String,
261 (nDataLength + 1) * sizeof(FX_WCHAR));
264 void CFX_WideString::AllocBeforeWrite(FX_STRSIZE nLen) {
265 if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nLen) {
269 m_pData = StringData::Create(nLen);
271 void CFX_WideString::AssignCopy(FX_STRSIZE nSrcLen,
272 const FX_WCHAR* lpszSrcData) {
273 AllocBeforeWrite(nSrcLen);
274 FXSYS_memcpy(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(FX_WCHAR));
275 m_pData->m_nDataLength = nSrcLen;
276 m_pData->m_String[nSrcLen] = 0;
278 int CFX_WideString::Compare(const FX_WCHAR* lpsz) const {
279 if (m_pData == NULL) {
280 return (lpsz == NULL || lpsz[0] == 0) ? 0 : -1;
282 return FXSYS_wcscmp(m_pData->m_String, lpsz);
284 CFX_ByteString CFX_WideString::UTF8Encode() const {
285 return FX_UTF8Encode(*this);
287 CFX_ByteString CFX_WideString::UTF16LE_Encode() const {
288 if (m_pData == NULL) {
289 return CFX_ByteString(FX_BSTRC("\0\0"));
291 int len = m_pData->m_nDataLength;
292 CFX_ByteString result;
293 FX_CHAR* buffer = result.GetBuffer(len * 2 + 2);
294 for (int i = 0; i < len; i++) {
295 buffer[i * 2] = m_pData->m_String[i] & 0xff;
296 buffer[i * 2 + 1] = m_pData->m_String[i] >> 8;
299 buffer[len * 2 + 1] = 0;
300 result.ReleaseBuffer(len * 2 + 2);
303 void CFX_WideString::ConvertFrom(const CFX_ByteString& str,
304 CFX_CharMap* pCharMap) {
305 if (pCharMap == NULL) {
306 pCharMap = CFX_CharMap::GetDefaultMapper();
308 *this = pCharMap->m_GetWideString(pCharMap, str);
310 void CFX_WideString::Reserve(FX_STRSIZE len) {
312 ReleaseBuffer(GetLength());
314 FX_WCHAR* CFX_WideString::GetBuffer(FX_STRSIZE nMinBufLength) {
315 if (m_pData == NULL && nMinBufLength == 0) {
318 if (m_pData && m_pData->m_nRefs <= 1 &&
319 m_pData->m_nAllocLength >= nMinBufLength) {
320 return m_pData->m_String;
322 if (m_pData == NULL) {
323 m_pData = StringData::Create(nMinBufLength);
327 m_pData->m_nDataLength = 0;
328 m_pData->m_String[0] = 0;
329 return m_pData->m_String;
331 StringData* pOldData = m_pData;
332 FX_STRSIZE nOldLen = pOldData->m_nDataLength;
333 if (nMinBufLength < nOldLen) {
334 nMinBufLength = nOldLen;
336 m_pData = StringData::Create(nMinBufLength);
340 FXSYS_memcpy(m_pData->m_String, pOldData->m_String,
341 (nOldLen + 1) * sizeof(FX_WCHAR));
342 m_pData->m_nDataLength = nOldLen;
344 return m_pData->m_String;
346 CFX_WideString CFX_WideString::FromLocal(const char* str, FX_STRSIZE len) {
347 CFX_WideString result;
348 result.ConvertFrom(CFX_ByteString(str, len));
351 CFX_WideString CFX_WideString::FromUTF8(const char* str, FX_STRSIZE len) {
352 if (!str || 0 == len) {
353 return CFX_WideString();
356 CFX_UTF8Decoder decoder;
357 for (FX_STRSIZE i = 0; i < len; i++) {
358 decoder.Input(str[i]);
360 return decoder.GetResult();
362 CFX_WideString CFX_WideString::FromUTF16LE(const unsigned short* wstr,
364 if (!wstr || 0 == wlen) {
365 return CFX_WideString();
368 CFX_WideString result;
369 FX_WCHAR* buf = result.GetBuffer(wlen);
370 for (int i = 0; i < wlen; i++) {
373 result.ReleaseBuffer(wlen);
376 FX_STRSIZE CFX_WideString::WStringLength(const unsigned short* str) {
384 void CFX_WideString::AllocCopy(CFX_WideString& dest,
386 FX_STRSIZE nCopyIndex) const {
387 // |FX_STRSIZE| is currently typedef'd as in |int|. TODO(palmer): It
388 // should be a |size_t|, or at least unsigned.
389 if (nCopyLen == 0 || nCopyLen < 0) {
392 pdfium::base::CheckedNumeric<FX_STRSIZE> iSize =
393 static_cast<FX_STRSIZE>(sizeof(FX_WCHAR));
395 ASSERT(dest.m_pData == NULL);
396 dest.m_pData = StringData::Create(nCopyLen);
398 FXSYS_memcpy(dest.m_pData->m_String, m_pData->m_String + nCopyIndex,
402 CFX_WideString CFX_WideString::Left(FX_STRSIZE nCount) const {
403 if (m_pData == NULL) {
404 return CFX_WideString();
409 if (nCount >= m_pData->m_nDataLength) {
413 AllocCopy(dest, nCount, 0);
416 CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst) const {
417 return Mid(nFirst, m_pData->m_nDataLength - nFirst);
419 CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const {
420 if (m_pData == NULL) {
421 return CFX_WideString();
429 if (nFirst + nCount > m_pData->m_nDataLength) {
430 nCount = m_pData->m_nDataLength - nFirst;
432 if (nFirst > m_pData->m_nDataLength) {
435 if (nFirst == 0 && nFirst + nCount == m_pData->m_nDataLength) {
439 AllocCopy(dest, nCount, nFirst);
442 CFX_WideString CFX_WideString::Right(FX_STRSIZE nCount) const {
443 if (m_pData == NULL) {
444 return CFX_WideString();
449 if (nCount >= m_pData->m_nDataLength) {
453 AllocCopy(dest, nCount, m_pData->m_nDataLength - nCount);
456 int CFX_WideString::CompareNoCase(const FX_WCHAR* lpsz) const {
457 if (m_pData == NULL) {
458 return (lpsz == NULL || lpsz[0] == 0) ? 0 : -1;
460 return FXSYS_wcsicmp(m_pData->m_String, lpsz);
462 int CFX_WideString::Compare(const CFX_WideString& str) const {
463 if (m_pData == NULL) {
464 if (str.m_pData == NULL) {
469 if (str.m_pData == NULL) {
472 int this_len = m_pData->m_nDataLength;
473 int that_len = str.m_pData->m_nDataLength;
474 int min_len = this_len < that_len ? this_len : that_len;
475 for (int i = 0; i < min_len; i++) {
476 if (m_pData->m_String[i] < str.m_pData->m_String[i]) {
479 if (m_pData->m_String[i] > str.m_pData->m_String[i]) {
483 if (this_len < that_len) {
486 if (this_len > that_len) {
491 void CFX_WideString::SetAt(FX_STRSIZE nIndex, FX_WCHAR ch) {
492 if (m_pData == NULL) {
496 ASSERT(nIndex < m_pData->m_nDataLength);
498 m_pData->m_String[nIndex] = ch;
500 void CFX_WideString::MakeLower() {
501 if (m_pData == NULL) {
505 if (GetLength() < 1) {
508 FXSYS_wcslwr(m_pData->m_String);
510 void CFX_WideString::MakeUpper() {
511 if (m_pData == NULL) {
515 if (GetLength() < 1) {
518 FXSYS_wcsupr(m_pData->m_String);
520 FX_STRSIZE CFX_WideString::Find(const FX_WCHAR* lpszSub,
521 FX_STRSIZE nStart) const {
522 FX_STRSIZE nLength = GetLength();
523 if (nLength < 1 || nStart > nLength) {
526 const FX_WCHAR* lpsz = FXSYS_wcsstr(m_pData->m_String + nStart, lpszSub);
527 return (lpsz == NULL) ? -1 : (int)(lpsz - m_pData->m_String);
529 FX_STRSIZE CFX_WideString::Find(FX_WCHAR ch, FX_STRSIZE nStart) const {
530 if (m_pData == NULL) {
533 FX_STRSIZE nLength = m_pData->m_nDataLength;
534 if (nStart >= nLength) {
537 const FX_WCHAR* lpsz = FXSYS_wcschr(m_pData->m_String + nStart, ch);
538 return (lpsz == NULL) ? -1 : (int)(lpsz - m_pData->m_String);
540 void CFX_WideString::TrimRight(const FX_WCHAR* lpszTargetList) {
541 FXSYS_assert(lpszTargetList != NULL);
542 if (m_pData == NULL || *lpszTargetList == 0) {
546 FX_STRSIZE len = GetLength();
550 FX_STRSIZE pos = len;
552 if (FXSYS_wcschr(lpszTargetList, m_pData->m_String[pos - 1]) == NULL) {
558 m_pData->m_String[pos] = 0;
559 m_pData->m_nDataLength = pos;
562 void CFX_WideString::TrimRight(FX_WCHAR chTarget) {
563 FX_WCHAR str[2] = {chTarget, 0};
566 void CFX_WideString::TrimRight() {
567 TrimRight(L"\x09\x0a\x0b\x0c\x0d\x20");
569 void CFX_WideString::TrimLeft(const FX_WCHAR* lpszTargets) {
570 FXSYS_assert(lpszTargets != NULL);
571 if (m_pData == NULL || *lpszTargets == 0) {
575 if (GetLength() < 1) {
578 const FX_WCHAR* lpsz = m_pData->m_String;
580 if (FXSYS_wcschr(lpszTargets, *lpsz) == NULL) {
585 if (lpsz != m_pData->m_String) {
587 m_pData->m_nDataLength - (FX_STRSIZE)(lpsz - m_pData->m_String);
588 FXSYS_memmove(m_pData->m_String, lpsz,
589 (nDataLength + 1) * sizeof(FX_WCHAR));
590 m_pData->m_nDataLength = nDataLength;
593 void CFX_WideString::TrimLeft(FX_WCHAR chTarget) {
594 FX_WCHAR str[2] = {chTarget, 0};
597 void CFX_WideString::TrimLeft() {
598 TrimLeft(L"\x09\x0a\x0b\x0c\x0d\x20");
600 FX_STRSIZE CFX_WideString::Replace(const FX_WCHAR* lpszOld,
601 const FX_WCHAR* lpszNew) {
602 if (GetLength() < 1) {
605 if (lpszOld == NULL) {
608 FX_STRSIZE nSourceLen = FXSYS_wcslen(lpszOld);
609 if (nSourceLen == 0) {
612 FX_STRSIZE nReplacementLen = lpszNew ? FXSYS_wcslen(lpszNew) : 0;
613 FX_STRSIZE nCount = 0;
614 FX_WCHAR* lpszStart = m_pData->m_String;
615 FX_WCHAR* lpszEnd = m_pData->m_String + m_pData->m_nDataLength;
616 FX_WCHAR* lpszTarget;
618 while ((lpszTarget = (FX_WCHAR*)FXSYS_wcsstr(lpszStart, lpszOld)) != NULL &&
619 lpszStart < lpszEnd) {
621 lpszStart = lpszTarget + nSourceLen;
626 FX_STRSIZE nOldLength = m_pData->m_nDataLength;
627 FX_STRSIZE nNewLength =
628 nOldLength + (nReplacementLen - nSourceLen) * nCount;
629 if (m_pData->m_nAllocLength < nNewLength || m_pData->m_nRefs > 1) {
630 StringData* pOldData = m_pData;
631 const FX_WCHAR* pstr = m_pData->m_String;
632 m_pData = StringData::Create(nNewLength);
636 FXSYS_memcpy(m_pData->m_String, pstr,
637 pOldData->m_nDataLength * sizeof(FX_WCHAR));
640 lpszStart = m_pData->m_String;
641 lpszEnd = m_pData->m_String + FX_MAX(m_pData->m_nDataLength, nNewLength);
643 while ((lpszTarget = (FX_WCHAR*)FXSYS_wcsstr(lpszStart, lpszOld)) !=
645 lpszStart < lpszEnd) {
646 FX_STRSIZE nBalance =
648 (FX_STRSIZE)(lpszTarget - m_pData->m_String + nSourceLen);
649 FXSYS_memmove(lpszTarget + nReplacementLen, lpszTarget + nSourceLen,
650 nBalance * sizeof(FX_WCHAR));
651 FXSYS_memcpy(lpszTarget, lpszNew, nReplacementLen * sizeof(FX_WCHAR));
652 lpszStart = lpszTarget + nReplacementLen;
653 lpszStart[nBalance] = 0;
654 nOldLength += (nReplacementLen - nSourceLen);
657 ASSERT(m_pData->m_String[nNewLength] == 0);
658 m_pData->m_nDataLength = nNewLength;
662 FX_STRSIZE CFX_WideString::Insert(FX_STRSIZE nIndex, FX_WCHAR ch) {
667 FX_STRSIZE nNewLength = GetLength();
668 if (nIndex > nNewLength) {
672 if (m_pData == NULL || m_pData->m_nAllocLength < nNewLength) {
673 StringData* pOldData = m_pData;
674 const FX_WCHAR* pstr = m_pData->m_String;
675 m_pData = StringData::Create(nNewLength);
679 if (pOldData != NULL) {
680 FXSYS_memmove(m_pData->m_String, pstr,
681 (pOldData->m_nDataLength + 1) * sizeof(FX_WCHAR));
684 m_pData->m_String[0] = 0;
687 FXSYS_memmove(m_pData->m_String + nIndex + 1, m_pData->m_String + nIndex,
688 (nNewLength - nIndex) * sizeof(FX_WCHAR));
689 m_pData->m_String[nIndex] = ch;
690 m_pData->m_nDataLength = nNewLength;
693 FX_STRSIZE CFX_WideString::Delete(FX_STRSIZE nIndex, FX_STRSIZE nCount) {
694 if (GetLength() < 1) {
700 FX_STRSIZE nOldLength = m_pData->m_nDataLength;
701 if (nCount > 0 && nIndex < nOldLength) {
703 int nBytesToCopy = nOldLength - (nIndex + nCount) + 1;
704 FXSYS_memmove(m_pData->m_String + nIndex,
705 m_pData->m_String + nIndex + nCount,
706 nBytesToCopy * sizeof(FX_WCHAR));
707 m_pData->m_nDataLength = nOldLength - nCount;
709 return m_pData->m_nDataLength;
711 FX_STRSIZE CFX_WideString::Remove(FX_WCHAR chRemove) {
712 if (m_pData == NULL) {
716 if (GetLength() < 1) {
719 FX_WCHAR* pstrSource = m_pData->m_String;
720 FX_WCHAR* pstrDest = m_pData->m_String;
721 FX_WCHAR* pstrEnd = m_pData->m_String + m_pData->m_nDataLength;
722 while (pstrSource < pstrEnd) {
723 if (*pstrSource != chRemove) {
724 *pstrDest = *pstrSource;
730 FX_STRSIZE nCount = (FX_STRSIZE)(pstrSource - pstrDest);
731 m_pData->m_nDataLength -= nCount;
734 #define FORCE_ANSI 0x10000
735 #define FORCE_UNICODE 0x20000
736 #define FORCE_INT64 0x40000
737 void CFX_WideString::FormatV(const FX_WCHAR* lpszFormat, va_list argList) {
739 #if defined(__ARMCC_VERSION) || \
740 (!defined(_MSC_VER) && (_FX_CPU_ == _FX_X64_ || _FX_CPU_ == _FX_IA64_ || \
741 _FX_CPU_ == _FX_ARM64_)) || \
742 defined(__native_client__)
743 va_copy(argListSave, argList);
745 argListSave = argList;
748 for (const FX_WCHAR* lpsz = lpszFormat; *lpsz != 0; lpsz++) {
749 if (*lpsz != '%' || *(lpsz = lpsz + 1) == '%') {
750 nMaxLen += FXSYS_wcslen(lpsz);
755 for (; *lpsz != 0; lpsz++) {
758 } else if (*lpsz == '*') {
759 nWidth = va_arg(argList, int);
760 } else if (*lpsz == '-' || *lpsz == '+' || *lpsz == '0' || *lpsz == ' ')
767 nWidth = FXSYS_wtoi(lpsz);
768 for (; *lpsz != 0 && (*lpsz) <= '9' && (*lpsz) >= '0'; lpsz++)
771 if (nWidth < 0 || nWidth > 128 * 1024) {
772 lpszFormat = L"Bad width";
780 nPrecision = va_arg(argList, int);
783 nPrecision = FXSYS_wtoi(lpsz);
784 for (; *lpsz != 0 && (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz++)
788 if (nPrecision < 0 || nPrecision > 128 * 1024) {
789 lpszFormat = L"Bad precision";
794 if (*lpsz == L'I' && *(lpsz + 1) == L'6' && *(lpsz + 2) == L'4') {
796 nModifier = FORCE_INT64;
800 nModifier = FORCE_ANSI;
804 nModifier = FORCE_UNICODE;
814 switch (*lpsz | nModifier) {
818 va_arg(argList, int);
820 case 'c' | FORCE_ANSI:
821 case 'C' | FORCE_ANSI:
823 va_arg(argList, int);
825 case 'c' | FORCE_UNICODE:
826 case 'C' | FORCE_UNICODE:
828 va_arg(argList, int);
831 const FX_WCHAR* pstrNextArg = va_arg(argList, const FX_WCHAR*);
832 if (pstrNextArg == NULL) {
835 nItemLen = FXSYS_wcslen(pstrNextArg);
842 const FX_CHAR* pstrNextArg = va_arg(argList, const FX_CHAR*);
843 if (pstrNextArg == NULL) {
846 nItemLen = FXSYS_strlen(pstrNextArg);
852 case 's' | FORCE_ANSI:
853 case 'S' | FORCE_ANSI: {
854 const FX_CHAR* pstrNextArg = va_arg(argList, const FX_CHAR*);
855 if (pstrNextArg == NULL) {
858 nItemLen = FXSYS_strlen(pstrNextArg);
864 case 's' | FORCE_UNICODE:
865 case 'S' | FORCE_UNICODE: {
866 FX_WCHAR* pstrNextArg = va_arg(argList, FX_WCHAR*);
867 if (pstrNextArg == NULL) {
870 nItemLen = FXSYS_wcslen(pstrNextArg);
878 if (nPrecision != 0 && nItemLen > nPrecision) {
879 nItemLen = nPrecision;
881 if (nItemLen < nWidth) {
892 if (nModifier & FORCE_INT64) {
893 va_arg(argList, int64_t);
895 va_arg(argList, int);
898 if (nItemLen < nWidth + nPrecision) {
899 nItemLen = nWidth + nPrecision;
908 va_arg(argList, double);
910 if (nItemLen < nWidth + nPrecision) {
911 nItemLen = nWidth + nPrecision;
915 if (nWidth + nPrecision > 100) {
916 nItemLen = nPrecision + nWidth + 128;
920 f = va_arg(argList, double);
921 FXSYS_snprintf(pszTemp, sizeof(pszTemp), "%*.*f", nWidth,
923 nItemLen = FXSYS_strlen(pszTemp);
927 va_arg(argList, void*);
929 if (nItemLen < nWidth + nPrecision) {
930 nItemLen = nWidth + nPrecision;
934 va_arg(argList, int*);
942 FXSYS_vswprintf((wchar_t*)m_pData->m_String, nMaxLen + 1,
943 (const wchar_t*)lpszFormat, argListSave);
948 void CFX_WideString::Format(const FX_WCHAR* lpszFormat, ...) {
950 va_start(argList, lpszFormat);
951 FormatV(lpszFormat, argList);
954 FX_FLOAT FX_wtof(const FX_WCHAR* str, int len) {
959 FX_BOOL bNegative = FALSE;
962 } else if (str[0] == '-') {
968 if (str[cc] == '.') {
971 integer = integer * 10 + str[cc] - '0';
974 FX_FLOAT fraction = 0;
975 if (str[cc] == '.') {
977 FX_FLOAT scale = 0.1f;
979 fraction += scale * (str[cc] - '0');
984 fraction += (FX_FLOAT)integer;
985 return bNegative ? -fraction : fraction;
987 int CFX_WideString::GetInteger() const {
988 if (m_pData == NULL) {
991 return FXSYS_wtoi(m_pData->m_String);
993 FX_FLOAT CFX_WideString::GetFloat() const {
994 if (m_pData == NULL) {
997 return FX_wtof(m_pData->m_String, m_pData->m_nDataLength);
999 static CFX_ByteString _DefMap_GetByteString(CFX_CharMap* pCharMap,
1000 const CFX_WideString& widestr) {
1001 int src_len = widestr.GetLength();
1002 int codepage = pCharMap->m_GetCodePage ? pCharMap->m_GetCodePage() : 0;
1003 int dest_len = FXSYS_WideCharToMultiByte(codepage, 0, widestr.c_str(),
1004 src_len, NULL, 0, NULL, NULL);
1005 if (dest_len == 0) {
1006 return CFX_ByteString();
1008 CFX_ByteString bytestr;
1009 FX_CHAR* dest_buf = bytestr.GetBuffer(dest_len);
1010 FXSYS_WideCharToMultiByte(codepage, 0, widestr.c_str(), src_len, dest_buf,
1011 dest_len, NULL, NULL);
1012 bytestr.ReleaseBuffer(dest_len);
1015 static CFX_WideString _DefMap_GetWideString(CFX_CharMap* pCharMap,
1016 const CFX_ByteString& bytestr) {
1017 int src_len = bytestr.GetLength();
1018 int codepage = pCharMap->m_GetCodePage ? pCharMap->m_GetCodePage() : 0;
1020 FXSYS_MultiByteToWideChar(codepage, 0, bytestr, src_len, NULL, 0);
1021 if (dest_len == 0) {
1022 return CFX_WideString();
1024 CFX_WideString widestr;
1025 FX_WCHAR* dest_buf = widestr.GetBuffer(dest_len);
1026 FXSYS_MultiByteToWideChar(codepage, 0, bytestr, src_len, dest_buf, dest_len);
1027 widestr.ReleaseBuffer(dest_len);
1030 static int _DefMap_GetGBKCodePage() {
1033 static int _DefMap_GetUHCCodePage() {
1036 static int _DefMap_GetJISCodePage() {
1039 static int _DefMap_GetBig5CodePage() {
1042 static const CFX_CharMap g_DefaultMapper = {&_DefMap_GetWideString,
1043 &_DefMap_GetByteString, NULL};
1044 static const CFX_CharMap g_DefaultGBKMapper = {
1045 &_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetGBKCodePage};
1046 static const CFX_CharMap g_DefaultJISMapper = {
1047 &_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetJISCodePage};
1048 static const CFX_CharMap g_DefaultUHCMapper = {
1049 &_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetUHCCodePage};
1050 static const CFX_CharMap g_DefaultBig5Mapper = {
1051 &_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetBig5CodePage};
1052 CFX_CharMap* CFX_CharMap::GetDefaultMapper(int32_t codepage) {
1055 return (CFX_CharMap*)&g_DefaultMapper;
1057 return (CFX_CharMap*)&g_DefaultJISMapper;
1059 return (CFX_CharMap*)&g_DefaultGBKMapper;
1061 return (CFX_CharMap*)&g_DefaultUHCMapper;
1063 return (CFX_CharMap*)&g_DefaultBig5Mapper;