• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include <stddef.h>  // For offsetof().
8 
9 #include <algorithm>
10 #include <cctype>
11 
12 #include "core/include/fxcrt/fx_basic.h"
13 #include "core/include/fxcrt/fx_ext.h"
14 #include "third_party/base/numerics/safe_math.h"
15 
16 // static
Create(int nLen)17 CFX_WideString::StringData* CFX_WideString::StringData::Create(int nLen) {
18   // TODO(palmer): |nLen| should really be declared as |size_t|, or
19   // at least unsigned.
20   if (nLen == 0 || nLen < 0) {
21     return NULL;
22   }
23 
24   // Fixed portion of header plus a NUL wide char not in m_nAllocLength.
25   int overhead = offsetof(StringData, m_String) + sizeof(FX_WCHAR);
26   pdfium::base::CheckedNumeric<int> iSize = nLen;
27   iSize *= sizeof(FX_WCHAR);
28   iSize += overhead;
29 
30   // Now round to an 8-byte boundary. We'd expect that this is the minimum
31   // granularity of any of the underlying allocators, so there may be cases
32   // where we can save a re-alloc when adding a few characters to a string
33   // by using this otherwise wasted space.
34   iSize += 7;
35   int totalSize = iSize.ValueOrDie() & ~7;
36   int usableLen = (totalSize - overhead) / sizeof(FX_WCHAR);
37   FXSYS_assert(usableLen >= nLen);
38 
39   void* pData = FX_Alloc(uint8_t, totalSize);
40   return new (pData) StringData(nLen, usableLen);
41 }
~CFX_WideString()42 CFX_WideString::~CFX_WideString() {
43   if (m_pData) {
44     m_pData->Release();
45   }
46 }
CFX_WideString(const CFX_WideString & stringSrc)47 CFX_WideString::CFX_WideString(const CFX_WideString& stringSrc) {
48   if (!stringSrc.m_pData) {
49     m_pData = NULL;
50     return;
51   }
52   if (stringSrc.m_pData->m_nRefs >= 0) {
53     m_pData = stringSrc.m_pData;
54     m_pData->Retain();
55   } else {
56     m_pData = NULL;
57     *this = stringSrc;
58   }
59 }
CFX_WideString(const FX_WCHAR * lpsz,FX_STRSIZE nLen)60 CFX_WideString::CFX_WideString(const FX_WCHAR* lpsz, FX_STRSIZE nLen) {
61   if (nLen < 0) {
62     nLen = lpsz ? FXSYS_wcslen(lpsz) : 0;
63   }
64   if (nLen) {
65     m_pData = StringData::Create(nLen);
66     if (m_pData) {
67       FXSYS_memcpy(m_pData->m_String, lpsz, nLen * sizeof(FX_WCHAR));
68     }
69   } else {
70     m_pData = NULL;
71   }
72 }
CFX_WideString(FX_WCHAR ch)73 CFX_WideString::CFX_WideString(FX_WCHAR ch) {
74   m_pData = StringData::Create(1);
75   if (m_pData) {
76     m_pData->m_String[0] = ch;
77   }
78 }
CFX_WideString(const CFX_WideStringC & str)79 CFX_WideString::CFX_WideString(const CFX_WideStringC& str) {
80   if (str.IsEmpty()) {
81     m_pData = NULL;
82     return;
83   }
84   m_pData = StringData::Create(str.GetLength());
85   if (m_pData) {
86     FXSYS_memcpy(m_pData->m_String, str.GetPtr(),
87                  str.GetLength() * sizeof(FX_WCHAR));
88   }
89 }
CFX_WideString(const CFX_WideStringC & str1,const CFX_WideStringC & str2)90 CFX_WideString::CFX_WideString(const CFX_WideStringC& str1,
91                                const CFX_WideStringC& str2) {
92   m_pData = NULL;
93   int nNewLen = str1.GetLength() + str2.GetLength();
94   if (nNewLen == 0) {
95     return;
96   }
97   m_pData = StringData::Create(nNewLen);
98   if (m_pData) {
99     FXSYS_memcpy(m_pData->m_String, str1.GetPtr(),
100                  str1.GetLength() * sizeof(FX_WCHAR));
101     FXSYS_memcpy(m_pData->m_String + str1.GetLength(), str2.GetPtr(),
102                  str2.GetLength() * sizeof(FX_WCHAR));
103   }
104 }
ReleaseBuffer(FX_STRSIZE nNewLength)105 void CFX_WideString::ReleaseBuffer(FX_STRSIZE nNewLength) {
106   if (!m_pData) {
107     return;
108   }
109   CopyBeforeWrite();
110   if (nNewLength == -1) {
111     nNewLength = m_pData ? FXSYS_wcslen(m_pData->m_String) : 0;
112   }
113   if (nNewLength == 0) {
114     Empty();
115     return;
116   }
117   FXSYS_assert(nNewLength <= m_pData->m_nAllocLength);
118   m_pData->m_nDataLength = nNewLength;
119   m_pData->m_String[nNewLength] = 0;
120 }
operator =(const FX_WCHAR * lpsz)121 const CFX_WideString& CFX_WideString::operator=(const FX_WCHAR* lpsz) {
122   if (!lpsz || lpsz[0] == 0) {
123     Empty();
124   } else {
125     AssignCopy(FXSYS_wcslen(lpsz), lpsz);
126   }
127   return *this;
128 }
operator =(const CFX_WideStringC & stringSrc)129 const CFX_WideString& CFX_WideString::operator=(
130     const CFX_WideStringC& stringSrc) {
131   if (stringSrc.IsEmpty()) {
132     Empty();
133   } else {
134     AssignCopy(stringSrc.GetLength(), stringSrc.GetPtr());
135   }
136   return *this;
137 }
operator =(const CFX_WideString & stringSrc)138 const CFX_WideString& CFX_WideString::operator=(
139     const CFX_WideString& stringSrc) {
140   if (m_pData == stringSrc.m_pData) {
141     return *this;
142   }
143   if (stringSrc.IsEmpty()) {
144     Empty();
145   } else if ((m_pData && m_pData->m_nRefs < 0) ||
146              (stringSrc.m_pData && stringSrc.m_pData->m_nRefs < 0)) {
147     AssignCopy(stringSrc.m_pData->m_nDataLength, stringSrc.m_pData->m_String);
148   } else {
149     Empty();
150     m_pData = stringSrc.m_pData;
151     if (m_pData) {
152       m_pData->Retain();
153     }
154   }
155   return *this;
156 }
operator +=(FX_WCHAR ch)157 const CFX_WideString& CFX_WideString::operator+=(FX_WCHAR ch) {
158   ConcatInPlace(1, &ch);
159   return *this;
160 }
operator +=(const FX_WCHAR * lpsz)161 const CFX_WideString& CFX_WideString::operator+=(const FX_WCHAR* lpsz) {
162   if (lpsz) {
163     ConcatInPlace(FXSYS_wcslen(lpsz), lpsz);
164   }
165   return *this;
166 }
operator +=(const CFX_WideString & string)167 const CFX_WideString& CFX_WideString::operator+=(const CFX_WideString& string) {
168   if (!string.m_pData) {
169     return *this;
170   }
171   ConcatInPlace(string.m_pData->m_nDataLength, string.m_pData->m_String);
172   return *this;
173 }
operator +=(const CFX_WideStringC & string)174 const CFX_WideString& CFX_WideString::operator+=(
175     const CFX_WideStringC& string) {
176   if (string.IsEmpty()) {
177     return *this;
178   }
179   ConcatInPlace(string.GetLength(), string.GetPtr());
180   return *this;
181 }
Equal(const wchar_t * ptr) const182 bool CFX_WideString::Equal(const wchar_t* ptr) const {
183   if (!m_pData) {
184     return !ptr || ptr[0] == L'\0';
185   }
186   if (!ptr) {
187     return m_pData->m_nDataLength == 0;
188   }
189   return wcslen(ptr) == m_pData->m_nDataLength &&
190          wmemcmp(ptr, m_pData->m_String, m_pData->m_nDataLength) == 0;
191 }
Equal(const CFX_WideStringC & str) const192 bool CFX_WideString::Equal(const CFX_WideStringC& str) const {
193   if (!m_pData) {
194     return str.IsEmpty();
195   }
196   return str.GetLength() == m_pData->m_nDataLength &&
197          wmemcmp(str.GetPtr(), m_pData->m_String, m_pData->m_nDataLength) == 0;
198 }
Equal(const CFX_WideString & other) const199 bool CFX_WideString::Equal(const CFX_WideString& other) const {
200   if (IsEmpty()) {
201     return other.IsEmpty();
202   }
203   if (other.IsEmpty()) {
204     return false;
205   }
206   return other.m_pData->m_nDataLength == m_pData->m_nDataLength &&
207          wmemcmp(other.m_pData->m_String, m_pData->m_String,
208                  m_pData->m_nDataLength) == 0;
209 }
Empty()210 void CFX_WideString::Empty() {
211   if (m_pData) {
212     m_pData->Release();
213     m_pData = NULL;
214   }
215 }
ConcatInPlace(FX_STRSIZE nSrcLen,const FX_WCHAR * lpszSrcData)216 void CFX_WideString::ConcatInPlace(FX_STRSIZE nSrcLen,
217                                    const FX_WCHAR* lpszSrcData) {
218   if (nSrcLen == 0 || !lpszSrcData) {
219     return;
220   }
221   if (!m_pData) {
222     m_pData = StringData::Create(nSrcLen);
223     if (m_pData) {
224       FXSYS_memcpy(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(FX_WCHAR));
225     }
226     return;
227   }
228   if (m_pData->m_nRefs > 1 ||
229       m_pData->m_nDataLength + nSrcLen > m_pData->m_nAllocLength) {
230     ConcatCopy(m_pData->m_nDataLength, m_pData->m_String, nSrcLen, lpszSrcData);
231   } else {
232     FXSYS_memcpy(m_pData->m_String + m_pData->m_nDataLength, lpszSrcData,
233                  nSrcLen * sizeof(FX_WCHAR));
234     m_pData->m_nDataLength += nSrcLen;
235     m_pData->m_String[m_pData->m_nDataLength] = 0;
236   }
237 }
ConcatCopy(FX_STRSIZE nSrc1Len,const FX_WCHAR * lpszSrc1Data,FX_STRSIZE nSrc2Len,const FX_WCHAR * lpszSrc2Data)238 void CFX_WideString::ConcatCopy(FX_STRSIZE nSrc1Len,
239                                 const FX_WCHAR* lpszSrc1Data,
240                                 FX_STRSIZE nSrc2Len,
241                                 const FX_WCHAR* lpszSrc2Data) {
242   FX_STRSIZE nNewLen = nSrc1Len + nSrc2Len;
243   if (nNewLen <= 0) {
244     return;
245   }
246   // Don't release until done copying, might be one of the arguments.
247   StringData* pOldData = m_pData;
248   m_pData = StringData::Create(nNewLen);
249   if (m_pData) {
250     wmemcpy(m_pData->m_String, lpszSrc1Data, nSrc1Len);
251     wmemcpy(m_pData->m_String + nSrc1Len, lpszSrc2Data, nSrc2Len);
252   }
253   pOldData->Release();
254 }
CopyBeforeWrite()255 void CFX_WideString::CopyBeforeWrite() {
256   if (!m_pData || m_pData->m_nRefs <= 1) {
257     return;
258   }
259   StringData* pData = m_pData;
260   m_pData->Release();
261   FX_STRSIZE nDataLength = pData->m_nDataLength;
262   m_pData = StringData::Create(nDataLength);
263   if (m_pData) {
264     FXSYS_memcpy(m_pData->m_String, pData->m_String,
265                  (nDataLength + 1) * sizeof(FX_WCHAR));
266   }
267 }
AllocBeforeWrite(FX_STRSIZE nLen)268 void CFX_WideString::AllocBeforeWrite(FX_STRSIZE nLen) {
269   if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nLen) {
270     return;
271   }
272   Empty();
273   m_pData = StringData::Create(nLen);
274 }
AssignCopy(FX_STRSIZE nSrcLen,const FX_WCHAR * lpszSrcData)275 void CFX_WideString::AssignCopy(FX_STRSIZE nSrcLen,
276                                 const FX_WCHAR* lpszSrcData) {
277   AllocBeforeWrite(nSrcLen);
278   FXSYS_memcpy(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(FX_WCHAR));
279   m_pData->m_nDataLength = nSrcLen;
280   m_pData->m_String[nSrcLen] = 0;
281 }
Compare(const FX_WCHAR * lpsz) const282 int CFX_WideString::Compare(const FX_WCHAR* lpsz) const {
283   if (m_pData)
284     return FXSYS_wcscmp(m_pData->m_String, lpsz);
285   return (!lpsz || lpsz[0] == 0) ? 0 : -1;
286 }
UTF8Encode() const287 CFX_ByteString CFX_WideString::UTF8Encode() const {
288   return FX_UTF8Encode(*this);
289 }
UTF16LE_Encode() const290 CFX_ByteString CFX_WideString::UTF16LE_Encode() const {
291   if (!m_pData) {
292     return CFX_ByteString("\0\0", 2);
293   }
294   int len = m_pData->m_nDataLength;
295   CFX_ByteString result;
296   FX_CHAR* buffer = result.GetBuffer(len * 2 + 2);
297   for (int i = 0; i < len; i++) {
298     buffer[i * 2] = m_pData->m_String[i] & 0xff;
299     buffer[i * 2 + 1] = m_pData->m_String[i] >> 8;
300   }
301   buffer[len * 2] = 0;
302   buffer[len * 2 + 1] = 0;
303   result.ReleaseBuffer(len * 2 + 2);
304   return result;
305 }
ConvertFrom(const CFX_ByteString & str,CFX_CharMap * pCharMap)306 void CFX_WideString::ConvertFrom(const CFX_ByteString& str,
307                                  CFX_CharMap* pCharMap) {
308   if (!pCharMap) {
309     pCharMap = CFX_CharMap::GetDefaultMapper();
310   }
311   *this = pCharMap->m_GetWideString(pCharMap, str);
312 }
Reserve(FX_STRSIZE len)313 void CFX_WideString::Reserve(FX_STRSIZE len) {
314   GetBuffer(len);
315   ReleaseBuffer(GetLength());
316 }
GetBuffer(FX_STRSIZE nMinBufLength)317 FX_WCHAR* CFX_WideString::GetBuffer(FX_STRSIZE nMinBufLength) {
318   if (!m_pData && nMinBufLength == 0) {
319     return NULL;
320   }
321   if (m_pData && m_pData->m_nRefs <= 1 &&
322       m_pData->m_nAllocLength >= nMinBufLength) {
323     return m_pData->m_String;
324   }
325   if (!m_pData) {
326     m_pData = StringData::Create(nMinBufLength);
327     if (!m_pData) {
328       return NULL;
329     }
330     m_pData->m_nDataLength = 0;
331     m_pData->m_String[0] = 0;
332     return m_pData->m_String;
333   }
334   StringData* pOldData = m_pData;
335   FX_STRSIZE nOldLen = pOldData->m_nDataLength;
336   if (nMinBufLength < nOldLen) {
337     nMinBufLength = nOldLen;
338   }
339   m_pData = StringData::Create(nMinBufLength);
340   if (!m_pData) {
341     return NULL;
342   }
343   FXSYS_memcpy(m_pData->m_String, pOldData->m_String,
344                (nOldLen + 1) * sizeof(FX_WCHAR));
345   m_pData->m_nDataLength = nOldLen;
346   pOldData->Release();
347   return m_pData->m_String;
348 }
FromLocal(const char * str,FX_STRSIZE len)349 CFX_WideString CFX_WideString::FromLocal(const char* str, FX_STRSIZE len) {
350   CFX_WideString result;
351   result.ConvertFrom(CFX_ByteString(str, len));
352   return result;
353 }
FromUTF8(const char * str,FX_STRSIZE len)354 CFX_WideString CFX_WideString::FromUTF8(const char* str, FX_STRSIZE len) {
355   if (!str || 0 == len) {
356     return CFX_WideString();
357   }
358 
359   CFX_UTF8Decoder decoder;
360   for (FX_STRSIZE i = 0; i < len; i++) {
361     decoder.Input(str[i]);
362   }
363   return decoder.GetResult();
364 }
FromUTF16LE(const unsigned short * wstr,FX_STRSIZE wlen)365 CFX_WideString CFX_WideString::FromUTF16LE(const unsigned short* wstr,
366                                            FX_STRSIZE wlen) {
367   if (!wstr || 0 == wlen) {
368     return CFX_WideString();
369   }
370 
371   CFX_WideString result;
372   FX_WCHAR* buf = result.GetBuffer(wlen);
373   for (int i = 0; i < wlen; i++) {
374     buf[i] = wstr[i];
375   }
376   result.ReleaseBuffer(wlen);
377   return result;
378 }
WStringLength(const unsigned short * str)379 FX_STRSIZE CFX_WideString::WStringLength(const unsigned short* str) {
380   FX_STRSIZE len = 0;
381   if (str)
382     while (str[len])
383       len++;
384   return len;
385 }
386 
AllocCopy(CFX_WideString & dest,FX_STRSIZE nCopyLen,FX_STRSIZE nCopyIndex) const387 void CFX_WideString::AllocCopy(CFX_WideString& dest,
388                                FX_STRSIZE nCopyLen,
389                                FX_STRSIZE nCopyIndex) const {
390   // |FX_STRSIZE| is currently typedef'd as in |int|. TODO(palmer): It
391   // should be a |size_t|, or at least unsigned.
392   if (nCopyLen == 0 || nCopyLen < 0) {
393     return;
394   }
395   pdfium::base::CheckedNumeric<FX_STRSIZE> iSize =
396       static_cast<FX_STRSIZE>(sizeof(FX_WCHAR));
397   iSize *= nCopyLen;
398   ASSERT(!dest.m_pData);
399   dest.m_pData = StringData::Create(nCopyLen);
400   if (dest.m_pData) {
401     FXSYS_memcpy(dest.m_pData->m_String, m_pData->m_String + nCopyIndex,
402                  iSize.ValueOrDie());
403   }
404 }
Left(FX_STRSIZE nCount) const405 CFX_WideString CFX_WideString::Left(FX_STRSIZE nCount) const {
406   if (!m_pData) {
407     return CFX_WideString();
408   }
409   if (nCount < 0) {
410     nCount = 0;
411   }
412   if (nCount >= m_pData->m_nDataLength) {
413     return *this;
414   }
415   CFX_WideString dest;
416   AllocCopy(dest, nCount, 0);
417   return dest;
418 }
Mid(FX_STRSIZE nFirst) const419 CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst) const {
420   return Mid(nFirst, m_pData->m_nDataLength - nFirst);
421 }
Mid(FX_STRSIZE nFirst,FX_STRSIZE nCount) const422 CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const {
423   if (!m_pData) {
424     return CFX_WideString();
425   }
426   if (nFirst < 0) {
427     nFirst = 0;
428   }
429   if (nCount < 0) {
430     nCount = 0;
431   }
432   if (nFirst + nCount > m_pData->m_nDataLength) {
433     nCount = m_pData->m_nDataLength - nFirst;
434   }
435   if (nFirst > m_pData->m_nDataLength) {
436     nCount = 0;
437   }
438   if (nFirst == 0 && nFirst + nCount == m_pData->m_nDataLength) {
439     return *this;
440   }
441   CFX_WideString dest;
442   AllocCopy(dest, nCount, nFirst);
443   return dest;
444 }
Right(FX_STRSIZE nCount) const445 CFX_WideString CFX_WideString::Right(FX_STRSIZE nCount) const {
446   if (!m_pData) {
447     return CFX_WideString();
448   }
449   if (nCount < 0) {
450     nCount = 0;
451   }
452   if (nCount >= m_pData->m_nDataLength) {
453     return *this;
454   }
455   CFX_WideString dest;
456   AllocCopy(dest, nCount, m_pData->m_nDataLength - nCount);
457   return dest;
458 }
CompareNoCase(const FX_WCHAR * lpsz) const459 int CFX_WideString::CompareNoCase(const FX_WCHAR* lpsz) const {
460   if (!m_pData) {
461     return (!lpsz || lpsz[0] == 0) ? 0 : -1;
462   }
463   return FXSYS_wcsicmp(m_pData->m_String, lpsz);
464 }
Compare(const CFX_WideString & str) const465 int CFX_WideString::Compare(const CFX_WideString& str) const {
466   if (!m_pData) {
467     if (!str.m_pData) {
468       return 0;
469     }
470     return -1;
471   }
472   if (!str.m_pData) {
473     return 1;
474   }
475   int this_len = m_pData->m_nDataLength;
476   int that_len = str.m_pData->m_nDataLength;
477   int min_len = this_len < that_len ? this_len : that_len;
478   for (int i = 0; i < min_len; i++) {
479     if (m_pData->m_String[i] < str.m_pData->m_String[i]) {
480       return -1;
481     }
482     if (m_pData->m_String[i] > str.m_pData->m_String[i]) {
483       return 1;
484     }
485   }
486   if (this_len < that_len) {
487     return -1;
488   }
489   if (this_len > that_len) {
490     return 1;
491   }
492   return 0;
493 }
SetAt(FX_STRSIZE nIndex,FX_WCHAR ch)494 void CFX_WideString::SetAt(FX_STRSIZE nIndex, FX_WCHAR ch) {
495   if (!m_pData) {
496     return;
497   }
498   ASSERT(nIndex >= 0);
499   ASSERT(nIndex < m_pData->m_nDataLength);
500   CopyBeforeWrite();
501   m_pData->m_String[nIndex] = ch;
502 }
MakeLower()503 void CFX_WideString::MakeLower() {
504   if (!m_pData) {
505     return;
506   }
507   CopyBeforeWrite();
508   if (GetLength() < 1) {
509     return;
510   }
511   FXSYS_wcslwr(m_pData->m_String);
512 }
MakeUpper()513 void CFX_WideString::MakeUpper() {
514   if (!m_pData) {
515     return;
516   }
517   CopyBeforeWrite();
518   if (GetLength() < 1) {
519     return;
520   }
521   FXSYS_wcsupr(m_pData->m_String);
522 }
Find(const FX_WCHAR * lpszSub,FX_STRSIZE nStart) const523 FX_STRSIZE CFX_WideString::Find(const FX_WCHAR* lpszSub,
524                                 FX_STRSIZE nStart) const {
525   FX_STRSIZE nLength = GetLength();
526   if (nLength < 1 || nStart > nLength) {
527     return -1;
528   }
529   const FX_WCHAR* lpsz = FXSYS_wcsstr(m_pData->m_String + nStart, lpszSub);
530   return lpsz ? (int)(lpsz - m_pData->m_String) : -1;
531 }
Find(FX_WCHAR ch,FX_STRSIZE nStart) const532 FX_STRSIZE CFX_WideString::Find(FX_WCHAR ch, FX_STRSIZE nStart) const {
533   if (!m_pData) {
534     return -1;
535   }
536   FX_STRSIZE nLength = m_pData->m_nDataLength;
537   if (nStart >= nLength) {
538     return -1;
539   }
540   const FX_WCHAR* lpsz = FXSYS_wcschr(m_pData->m_String + nStart, ch);
541   return (lpsz) ? (int)(lpsz - m_pData->m_String) : -1;
542 }
TrimRight(const FX_WCHAR * lpszTargetList)543 void CFX_WideString::TrimRight(const FX_WCHAR* lpszTargetList) {
544   FXSYS_assert(lpszTargetList);
545   if (!m_pData || *lpszTargetList == 0) {
546     return;
547   }
548   CopyBeforeWrite();
549   FX_STRSIZE len = GetLength();
550   if (len < 1) {
551     return;
552   }
553   FX_STRSIZE pos = len;
554   while (pos) {
555     if (!FXSYS_wcschr(lpszTargetList, m_pData->m_String[pos - 1])) {
556       break;
557     }
558     pos--;
559   }
560   if (pos < len) {
561     m_pData->m_String[pos] = 0;
562     m_pData->m_nDataLength = pos;
563   }
564 }
TrimRight(FX_WCHAR chTarget)565 void CFX_WideString::TrimRight(FX_WCHAR chTarget) {
566   FX_WCHAR str[2] = {chTarget, 0};
567   TrimRight(str);
568 }
TrimRight()569 void CFX_WideString::TrimRight() {
570   TrimRight(L"\x09\x0a\x0b\x0c\x0d\x20");
571 }
TrimLeft(const FX_WCHAR * lpszTargets)572 void CFX_WideString::TrimLeft(const FX_WCHAR* lpszTargets) {
573   FXSYS_assert(lpszTargets);
574   if (!m_pData || *lpszTargets == 0) {
575     return;
576   }
577   CopyBeforeWrite();
578   if (GetLength() < 1) {
579     return;
580   }
581   const FX_WCHAR* lpsz = m_pData->m_String;
582   while (*lpsz != 0) {
583     if (!FXSYS_wcschr(lpszTargets, *lpsz)) {
584       break;
585     }
586     lpsz++;
587   }
588   if (lpsz != m_pData->m_String) {
589     int nDataLength =
590         m_pData->m_nDataLength - (FX_STRSIZE)(lpsz - m_pData->m_String);
591     FXSYS_memmove(m_pData->m_String, lpsz,
592                   (nDataLength + 1) * sizeof(FX_WCHAR));
593     m_pData->m_nDataLength = nDataLength;
594   }
595 }
TrimLeft(FX_WCHAR chTarget)596 void CFX_WideString::TrimLeft(FX_WCHAR chTarget) {
597   FX_WCHAR str[2] = {chTarget, 0};
598   TrimLeft(str);
599 }
TrimLeft()600 void CFX_WideString::TrimLeft() {
601   TrimLeft(L"\x09\x0a\x0b\x0c\x0d\x20");
602 }
Replace(const FX_WCHAR * lpszOld,const FX_WCHAR * lpszNew)603 FX_STRSIZE CFX_WideString::Replace(const FX_WCHAR* lpszOld,
604                                    const FX_WCHAR* lpszNew) {
605   if (GetLength() < 1) {
606     return 0;
607   }
608   if (!lpszOld) {
609     return 0;
610   }
611   FX_STRSIZE nSourceLen = FXSYS_wcslen(lpszOld);
612   if (nSourceLen == 0) {
613     return 0;
614   }
615   FX_STRSIZE nReplacementLen = lpszNew ? FXSYS_wcslen(lpszNew) : 0;
616   FX_STRSIZE nCount = 0;
617   FX_WCHAR* lpszStart = m_pData->m_String;
618   FX_WCHAR* lpszEnd = m_pData->m_String + m_pData->m_nDataLength;
619   FX_WCHAR* lpszTarget;
620   {
621     while ((lpszTarget = (FX_WCHAR*)FXSYS_wcsstr(lpszStart, lpszOld)) &&
622            lpszStart < lpszEnd) {
623       nCount++;
624       lpszStart = lpszTarget + nSourceLen;
625     }
626   }
627   if (nCount > 0) {
628     CopyBeforeWrite();
629     FX_STRSIZE nOldLength = m_pData->m_nDataLength;
630     FX_STRSIZE nNewLength =
631         nOldLength + (nReplacementLen - nSourceLen) * nCount;
632     if (m_pData->m_nAllocLength < nNewLength || m_pData->m_nRefs > 1) {
633       StringData* pOldData = m_pData;
634       const FX_WCHAR* pstr = m_pData->m_String;
635       m_pData = StringData::Create(nNewLength);
636       if (!m_pData) {
637         return 0;
638       }
639       FXSYS_memcpy(m_pData->m_String, pstr,
640                    pOldData->m_nDataLength * sizeof(FX_WCHAR));
641       pOldData->Release();
642     }
643     lpszStart = m_pData->m_String;
644     lpszEnd = m_pData->m_String + std::max(m_pData->m_nDataLength, nNewLength);
645     {
646       while ((lpszTarget = (FX_WCHAR*)FXSYS_wcsstr(lpszStart, lpszOld)) !=
647                  NULL &&
648              lpszStart < lpszEnd) {
649         FX_STRSIZE nBalance =
650             nOldLength -
651             (FX_STRSIZE)(lpszTarget - m_pData->m_String + nSourceLen);
652         FXSYS_memmove(lpszTarget + nReplacementLen, lpszTarget + nSourceLen,
653                       nBalance * sizeof(FX_WCHAR));
654         FXSYS_memcpy(lpszTarget, lpszNew, nReplacementLen * sizeof(FX_WCHAR));
655         lpszStart = lpszTarget + nReplacementLen;
656         lpszStart[nBalance] = 0;
657         nOldLength += (nReplacementLen - nSourceLen);
658       }
659     }
660     ASSERT(m_pData->m_String[nNewLength] == 0);
661     m_pData->m_nDataLength = nNewLength;
662   }
663   return nCount;
664 }
Insert(FX_STRSIZE nIndex,FX_WCHAR ch)665 FX_STRSIZE CFX_WideString::Insert(FX_STRSIZE nIndex, FX_WCHAR ch) {
666   CopyBeforeWrite();
667   if (nIndex < 0) {
668     nIndex = 0;
669   }
670   FX_STRSIZE nNewLength = GetLength();
671   if (nIndex > nNewLength) {
672     nIndex = nNewLength;
673   }
674   nNewLength++;
675   if (!m_pData || m_pData->m_nAllocLength < nNewLength) {
676     StringData* pOldData = m_pData;
677     const FX_WCHAR* pstr = m_pData->m_String;
678     m_pData = StringData::Create(nNewLength);
679     if (!m_pData) {
680       return 0;
681     }
682     if (pOldData) {
683       FXSYS_memmove(m_pData->m_String, pstr,
684                     (pOldData->m_nDataLength + 1) * sizeof(FX_WCHAR));
685       pOldData->Release();
686     } else {
687       m_pData->m_String[0] = 0;
688     }
689   }
690   FXSYS_memmove(m_pData->m_String + nIndex + 1, m_pData->m_String + nIndex,
691                 (nNewLength - nIndex) * sizeof(FX_WCHAR));
692   m_pData->m_String[nIndex] = ch;
693   m_pData->m_nDataLength = nNewLength;
694   return nNewLength;
695 }
Delete(FX_STRSIZE nIndex,FX_STRSIZE nCount)696 FX_STRSIZE CFX_WideString::Delete(FX_STRSIZE nIndex, FX_STRSIZE nCount) {
697   if (GetLength() < 1) {
698     return 0;
699   }
700   if (nIndex < 0) {
701     nIndex = 0;
702   }
703   FX_STRSIZE nOldLength = m_pData->m_nDataLength;
704   if (nCount > 0 && nIndex < nOldLength) {
705     CopyBeforeWrite();
706     int nBytesToCopy = nOldLength - (nIndex + nCount) + 1;
707     FXSYS_memmove(m_pData->m_String + nIndex,
708                   m_pData->m_String + nIndex + nCount,
709                   nBytesToCopy * sizeof(FX_WCHAR));
710     m_pData->m_nDataLength = nOldLength - nCount;
711   }
712   return m_pData->m_nDataLength;
713 }
Remove(FX_WCHAR chRemove)714 FX_STRSIZE CFX_WideString::Remove(FX_WCHAR chRemove) {
715   if (!m_pData) {
716     return 0;
717   }
718   CopyBeforeWrite();
719   if (GetLength() < 1) {
720     return 0;
721   }
722   FX_WCHAR* pstrSource = m_pData->m_String;
723   FX_WCHAR* pstrDest = m_pData->m_String;
724   FX_WCHAR* pstrEnd = m_pData->m_String + m_pData->m_nDataLength;
725   while (pstrSource < pstrEnd) {
726     if (*pstrSource != chRemove) {
727       *pstrDest = *pstrSource;
728       pstrDest++;
729     }
730     pstrSource++;
731   }
732   *pstrDest = 0;
733   FX_STRSIZE nCount = (FX_STRSIZE)(pstrSource - pstrDest);
734   m_pData->m_nDataLength -= nCount;
735   return nCount;
736 }
737 #define FORCE_ANSI 0x10000
738 #define FORCE_UNICODE 0x20000
739 #define FORCE_INT64 0x40000
FormatV(const FX_WCHAR * lpszFormat,va_list argList)740 void CFX_WideString::FormatV(const FX_WCHAR* lpszFormat, va_list argList) {
741   va_list argListSave;
742 #if defined(__ARMCC_VERSION) ||                                              \
743     (!defined(_MSC_VER) && (_FX_CPU_ == _FX_X64_ || _FX_CPU_ == _FX_IA64_ || \
744                             _FX_CPU_ == _FX_ARM64_)) ||                      \
745     defined(__native_client__)
746   va_copy(argListSave, argList);
747 #else
748   argListSave = argList;
749 #endif
750   int nMaxLen = 0;
751   for (const FX_WCHAR* lpsz = lpszFormat; *lpsz != 0; lpsz++) {
752     if (*lpsz != '%' || *(lpsz = lpsz + 1) == '%') {
753       nMaxLen += FXSYS_wcslen(lpsz);
754       continue;
755     }
756     int nItemLen = 0;
757     int nWidth = 0;
758     for (; *lpsz != 0; lpsz++) {
759       if (*lpsz == '#') {
760         nMaxLen += 2;
761       } else if (*lpsz == '*') {
762         nWidth = va_arg(argList, int);
763       } else if (*lpsz != '-' && *lpsz != '+' && *lpsz != '0' && *lpsz != ' ') {
764         break;
765       }
766     }
767     if (nWidth == 0) {
768       nWidth = FXSYS_wtoi(lpsz);
769       while (std::iswdigit(*lpsz))
770         ++lpsz;
771     }
772     if (nWidth < 0 || nWidth > 128 * 1024) {
773       lpszFormat = L"Bad width";
774       nMaxLen = 10;
775       break;
776     }
777     int nPrecision = 0;
778     if (*lpsz == '.') {
779       lpsz++;
780       if (*lpsz == '*') {
781         nPrecision = va_arg(argList, int);
782         lpsz++;
783       } else {
784         nPrecision = FXSYS_wtoi(lpsz);
785         while (std::iswdigit(*lpsz))
786           ++lpsz;
787       }
788     }
789     if (nPrecision < 0 || nPrecision > 128 * 1024) {
790       lpszFormat = L"Bad precision";
791       nMaxLen = 14;
792       break;
793     }
794     int nModifier = 0;
795     if (*lpsz == L'I' && *(lpsz + 1) == L'6' && *(lpsz + 2) == L'4') {
796       lpsz += 3;
797       nModifier = FORCE_INT64;
798     } else {
799       switch (*lpsz) {
800         case 'h':
801           nModifier = FORCE_ANSI;
802           lpsz++;
803           break;
804         case 'l':
805           nModifier = FORCE_UNICODE;
806           lpsz++;
807           break;
808         case 'F':
809         case 'N':
810         case 'L':
811           lpsz++;
812           break;
813       }
814     }
815     switch (*lpsz | nModifier) {
816       case 'c':
817       case 'C':
818         nItemLen = 2;
819         va_arg(argList, int);
820         break;
821       case 'c' | FORCE_ANSI:
822       case 'C' | FORCE_ANSI:
823         nItemLen = 2;
824         va_arg(argList, int);
825         break;
826       case 'c' | FORCE_UNICODE:
827       case 'C' | FORCE_UNICODE:
828         nItemLen = 2;
829         va_arg(argList, int);
830         break;
831       case 's': {
832         const FX_WCHAR* pstrNextArg = va_arg(argList, const FX_WCHAR*);
833         if (pstrNextArg) {
834           nItemLen = FXSYS_wcslen(pstrNextArg);
835           if (nItemLen < 1) {
836             nItemLen = 1;
837           }
838         } else {
839           nItemLen = 6;
840         }
841       } break;
842       case 'S': {
843         const FX_CHAR* pstrNextArg = va_arg(argList, const FX_CHAR*);
844         if (pstrNextArg) {
845           nItemLen = FXSYS_strlen(pstrNextArg);
846           if (nItemLen < 1) {
847             nItemLen = 1;
848           }
849         } else {
850           nItemLen = 6;
851         }
852       } break;
853       case 's' | FORCE_ANSI:
854       case 'S' | FORCE_ANSI: {
855         const FX_CHAR* pstrNextArg = va_arg(argList, const FX_CHAR*);
856         if (pstrNextArg) {
857           nItemLen = FXSYS_strlen(pstrNextArg);
858           if (nItemLen < 1) {
859             nItemLen = 1;
860           }
861         } else {
862           nItemLen = 6;
863         }
864       } break;
865       case 's' | FORCE_UNICODE:
866       case 'S' | FORCE_UNICODE: {
867         FX_WCHAR* pstrNextArg = va_arg(argList, FX_WCHAR*);
868         if (pstrNextArg) {
869           nItemLen = FXSYS_wcslen(pstrNextArg);
870           if (nItemLen < 1) {
871             nItemLen = 1;
872           }
873         } else {
874           nItemLen = 6;
875         }
876       } break;
877     }
878     if (nItemLen != 0) {
879       if (nPrecision != 0 && nItemLen > nPrecision) {
880         nItemLen = nPrecision;
881       }
882       if (nItemLen < nWidth) {
883         nItemLen = nWidth;
884       }
885     } else {
886       switch (*lpsz) {
887         case 'd':
888         case 'i':
889         case 'u':
890         case 'x':
891         case 'X':
892         case 'o':
893           if (nModifier & FORCE_INT64) {
894             va_arg(argList, int64_t);
895           } else {
896             va_arg(argList, int);
897           }
898           nItemLen = 32;
899           if (nItemLen < nWidth + nPrecision) {
900             nItemLen = nWidth + nPrecision;
901           }
902           break;
903         case 'a':
904         case 'A':
905         case 'e':
906         case 'E':
907         case 'g':
908         case 'G':
909           va_arg(argList, double);
910           nItemLen = 128;
911           if (nItemLen < nWidth + nPrecision) {
912             nItemLen = nWidth + nPrecision;
913           }
914           break;
915         case 'f':
916           if (nWidth + nPrecision > 100) {
917             nItemLen = nPrecision + nWidth + 128;
918           } else {
919             double f;
920             char pszTemp[256];
921             f = va_arg(argList, double);
922             FXSYS_snprintf(pszTemp, sizeof(pszTemp), "%*.*f", nWidth,
923                            nPrecision + 6, f);
924             nItemLen = FXSYS_strlen(pszTemp);
925           }
926           break;
927         case 'p':
928           va_arg(argList, void*);
929           nItemLen = 32;
930           if (nItemLen < nWidth + nPrecision) {
931             nItemLen = nWidth + nPrecision;
932           }
933           break;
934         case 'n':
935           va_arg(argList, int*);
936           break;
937       }
938     }
939     nMaxLen += nItemLen;
940   }
941   GetBuffer(nMaxLen);
942   if (m_pData) {
943     FXSYS_vswprintf((wchar_t*)m_pData->m_String, nMaxLen + 1,
944                     (const wchar_t*)lpszFormat, argListSave);
945     ReleaseBuffer();
946   }
947   va_end(argListSave);
948 }
Format(const FX_WCHAR * lpszFormat,...)949 void CFX_WideString::Format(const FX_WCHAR* lpszFormat, ...) {
950   va_list argList;
951   va_start(argList, lpszFormat);
952   FormatV(lpszFormat, argList);
953   va_end(argList);
954 }
FX_wtof(const FX_WCHAR * str,int len)955 FX_FLOAT FX_wtof(const FX_WCHAR* str, int len) {
956   if (len == 0) {
957     return 0.0;
958   }
959   int cc = 0;
960   FX_BOOL bNegative = FALSE;
961   if (str[0] == '+') {
962     cc++;
963   } else if (str[0] == '-') {
964     bNegative = TRUE;
965     cc++;
966   }
967   int integer = 0;
968   while (cc < len) {
969     if (str[cc] == '.') {
970       break;
971     }
972     integer = integer * 10 + FXSYS_toDecimalDigitWide(str[cc]);
973     cc++;
974   }
975   FX_FLOAT fraction = 0;
976   if (str[cc] == '.') {
977     cc++;
978     FX_FLOAT scale = 0.1f;
979     while (cc < len) {
980       fraction += scale * FXSYS_toDecimalDigitWide(str[cc]);
981       scale *= 0.1f;
982       cc++;
983     }
984   }
985   fraction += (FX_FLOAT)integer;
986   return bNegative ? -fraction : fraction;
987 }
GetInteger() const988 int CFX_WideString::GetInteger() const {
989   return m_pData ? FXSYS_wtoi(m_pData->m_String) : 0;
990 }
GetFloat() const991 FX_FLOAT CFX_WideString::GetFloat() const {
992   return m_pData ? FX_wtof(m_pData->m_String, m_pData->m_nDataLength) : 0.0f;
993 }
_DefMap_GetByteString(CFX_CharMap * pCharMap,const CFX_WideString & widestr)994 static CFX_ByteString _DefMap_GetByteString(CFX_CharMap* pCharMap,
995                                             const CFX_WideString& widestr) {
996   int src_len = widestr.GetLength();
997   int codepage = pCharMap->m_GetCodePage ? pCharMap->m_GetCodePage() : 0;
998   int dest_len = FXSYS_WideCharToMultiByte(codepage, 0, widestr.c_str(),
999                                            src_len, NULL, 0, NULL, NULL);
1000   if (dest_len == 0) {
1001     return CFX_ByteString();
1002   }
1003   CFX_ByteString bytestr;
1004   FX_CHAR* dest_buf = bytestr.GetBuffer(dest_len);
1005   FXSYS_WideCharToMultiByte(codepage, 0, widestr.c_str(), src_len, dest_buf,
1006                             dest_len, NULL, NULL);
1007   bytestr.ReleaseBuffer(dest_len);
1008   return bytestr;
1009 }
_DefMap_GetWideString(CFX_CharMap * pCharMap,const CFX_ByteString & bytestr)1010 static CFX_WideString _DefMap_GetWideString(CFX_CharMap* pCharMap,
1011                                             const CFX_ByteString& bytestr) {
1012   int src_len = bytestr.GetLength();
1013   int codepage = pCharMap->m_GetCodePage ? pCharMap->m_GetCodePage() : 0;
1014   int dest_len =
1015       FXSYS_MultiByteToWideChar(codepage, 0, bytestr, src_len, NULL, 0);
1016   if (dest_len == 0) {
1017     return CFX_WideString();
1018   }
1019   CFX_WideString widestr;
1020   FX_WCHAR* dest_buf = widestr.GetBuffer(dest_len);
1021   FXSYS_MultiByteToWideChar(codepage, 0, bytestr, src_len, dest_buf, dest_len);
1022   widestr.ReleaseBuffer(dest_len);
1023   return widestr;
1024 }
_DefMap_GetGBKCodePage()1025 static int _DefMap_GetGBKCodePage() {
1026   return 936;
1027 }
_DefMap_GetUHCCodePage()1028 static int _DefMap_GetUHCCodePage() {
1029   return 949;
1030 }
_DefMap_GetJISCodePage()1031 static int _DefMap_GetJISCodePage() {
1032   return 932;
1033 }
_DefMap_GetBig5CodePage()1034 static int _DefMap_GetBig5CodePage() {
1035   return 950;
1036 }
1037 static const CFX_CharMap g_DefaultMapper = {&_DefMap_GetWideString,
1038                                             &_DefMap_GetByteString, NULL};
1039 static const CFX_CharMap g_DefaultGBKMapper = {
1040     &_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetGBKCodePage};
1041 static const CFX_CharMap g_DefaultJISMapper = {
1042     &_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetJISCodePage};
1043 static const CFX_CharMap g_DefaultUHCMapper = {
1044     &_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetUHCCodePage};
1045 static const CFX_CharMap g_DefaultBig5Mapper = {
1046     &_DefMap_GetWideString, &_DefMap_GetByteString, &_DefMap_GetBig5CodePage};
GetDefaultMapper(int32_t codepage)1047 CFX_CharMap* CFX_CharMap::GetDefaultMapper(int32_t codepage) {
1048   switch (codepage) {
1049     case 0:
1050       return (CFX_CharMap*)&g_DefaultMapper;
1051     case 932:
1052       return (CFX_CharMap*)&g_DefaultJISMapper;
1053     case 936:
1054       return (CFX_CharMap*)&g_DefaultGBKMapper;
1055     case 949:
1056       return (CFX_CharMap*)&g_DefaultUHCMapper;
1057     case 950:
1058       return (CFX_CharMap*)&g_DefaultBig5Mapper;
1059   }
1060   return NULL;
1061 }
1062