Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / pdfium / core / src / fxcrt / fx_basic_wstring.cpp
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 "../../include/fxcrt/fx_basic.h"
8 #include "../../../third_party/numerics/safe_math.h"
9
10 static CFX_StringDataW* FX_AllocStringW(int nLen)
11 {
12     if (nLen == 0 || nLen < 0) {
13         return NULL;
14     }
15
16     base::CheckedNumeric<int> iSize = static_cast<int>(sizeof(FX_WCHAR));
17     iSize *= nLen + 1;
18     iSize += sizeof(long) * 3;
19     CFX_StringDataW* pData = (CFX_StringDataW*)FX_Alloc(FX_BYTE, iSize.ValueOrDie());
20     if (!pData) {
21         return NULL;
22     }
23
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
27     // somewhat.
28     pData->m_nAllocLength = nLen;
29     pData->m_nDataLength = nLen;
30     pData->m_nRefs = 1;
31     pData->m_String[nLen] = 0;
32     return pData;
33 }
34 static void FX_ReleaseStringW(CFX_StringDataW* pData)
35 {
36     if (pData == NULL) {
37         return;
38     }
39     pData->m_nRefs --;
40     if (pData->m_nRefs <= 0) {
41         FX_Free(pData);
42     }
43 }
44 CFX_WideString::~CFX_WideString()
45 {
46     if (m_pData == NULL) {
47         return;
48     }
49     m_pData->m_nRefs --;
50     if (m_pData->m_nRefs < 1) {
51         FX_Free(m_pData);
52     }
53 }
54 void CFX_WideString::InitStr(FX_LPCWSTR lpsz, FX_STRSIZE nLen)
55 {
56     if (nLen < 0) {
57         nLen = lpsz ? (FX_STRSIZE)FXSYS_wcslen(lpsz) : 0;
58     }
59     if (nLen) {
60         m_pData = FX_AllocStringW(nLen);
61         if (!m_pData) {
62             return;
63         }
64         FXSYS_memcpy32(m_pData->m_String, lpsz, nLen * sizeof(FX_WCHAR));
65     } else {
66         m_pData = NULL;
67     }
68 }
69 CFX_WideString::CFX_WideString(const CFX_WideString& stringSrc)
70 {
71     if (stringSrc.m_pData == NULL) {
72         m_pData = NULL;
73         return;
74     }
75     if (stringSrc.m_pData->m_nRefs >= 0) {
76         m_pData = stringSrc.m_pData;
77         m_pData->m_nRefs ++;
78     } else {
79         m_pData = NULL;
80         *this = stringSrc;
81     }
82 }
83 CFX_WideString::CFX_WideString(FX_WCHAR ch)
84 {
85     m_pData = FX_AllocStringW(1);
86     if (m_pData) {
87         m_pData->m_String[0] = ch;
88     }
89 }
90 CFX_WideString::CFX_WideString(const CFX_WideStringC& str)
91 {
92     if (str.IsEmpty()) {
93         m_pData = NULL;
94         return;
95     }
96     m_pData = FX_AllocStringW(str.GetLength());
97     if (m_pData) {
98         FXSYS_memcpy32(m_pData->m_String, str.GetPtr(), str.GetLength()*sizeof(FX_WCHAR));
99     }
100 }
101 CFX_WideString::CFX_WideString(const CFX_WideStringC& str1, const CFX_WideStringC& str2)
102 {
103     m_pData = NULL;
104     int nNewLen = str1.GetLength() + str2.GetLength();
105     if (nNewLen == 0) {
106         return;
107     }
108     m_pData = FX_AllocStringW(nNewLen);
109     if (m_pData) {
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));
112     }
113 }
114 void CFX_WideString::ReleaseBuffer(FX_STRSIZE nNewLength)
115 {
116     if (m_pData == NULL) {
117         return;
118     }
119     CopyBeforeWrite();
120     if (nNewLength == -1) {
121         nNewLength = m_pData ? (FX_STRSIZE)FXSYS_wcslen(m_pData->m_String) : 0;
122     }
123     if (nNewLength == 0) {
124         Empty();
125         return;
126     }
127     FXSYS_assert(nNewLength <= m_pData->m_nAllocLength);
128     m_pData->m_nDataLength = nNewLength;
129     m_pData->m_String[nNewLength] = 0;
130 }
131 const CFX_WideString& CFX_WideString::operator=(FX_LPCWSTR lpsz)
132 {
133     if (lpsz == NULL || lpsz[0] == 0) {
134         Empty();
135     } else {
136         AssignCopy((FX_STRSIZE)FXSYS_wcslen(lpsz), lpsz);
137     }
138     return *this;
139 }
140 const CFX_WideString& CFX_WideString::operator=(const CFX_WideStringC& stringSrc)
141 {
142     if (stringSrc.IsEmpty()) {
143         Empty();
144     } else {
145         AssignCopy(stringSrc.GetLength(), stringSrc.GetPtr());
146     }
147     return *this;
148 }
149 const CFX_WideString& CFX_WideString::operator=(const CFX_WideString& stringSrc)
150 {
151     if (m_pData == stringSrc.m_pData) {
152         return *this;
153     }
154     if (stringSrc.IsEmpty()) {
155         Empty();
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);
159     } else {
160         Empty();
161         m_pData = stringSrc.m_pData;
162         if (m_pData) {
163             m_pData->m_nRefs ++;
164         }
165     }
166     return *this;
167 }
168 const CFX_WideString& CFX_WideString::operator+=(FX_WCHAR ch)
169 {
170     ConcatInPlace(1, &ch);
171     return *this;
172 }
173 const CFX_WideString& CFX_WideString::operator+=(FX_LPCWSTR lpsz)
174 {
175     if (lpsz) {
176         ConcatInPlace((FX_STRSIZE)FXSYS_wcslen(lpsz), lpsz);
177     }
178     return *this;
179 }
180 const CFX_WideString& CFX_WideString::operator+=(const CFX_WideString& string)
181 {
182     if (string.m_pData == NULL) {
183         return *this;
184     }
185     ConcatInPlace(string.m_pData->m_nDataLength, string.m_pData->m_String);
186     return *this;
187 }
188 const CFX_WideString& CFX_WideString::operator+=(const CFX_WideStringC& string)
189 {
190     if (string.IsEmpty()) {
191         return *this;
192     }
193     ConcatInPlace(string.GetLength(), string.GetPtr());
194     return *this;
195 }
196 bool operator==(const CFX_WideString& s1, FX_LPCWSTR s2)
197 {
198     return s1.Equal(s2);
199 }
200 bool operator==(FX_LPCWSTR s1, const CFX_WideString& s2)
201 {
202     return s2.Equal(s1);
203 }
204 bool operator==(const CFX_WideString& s1, const CFX_WideString& s2)
205 {
206     return s1.Equal(s2);
207 }
208 bool operator==(const CFX_WideString& s1, const CFX_WideStringC& s2)
209 {
210     return s1.Equal(s2);
211 }
212 bool operator==(const CFX_WideStringC& s1, const CFX_WideString& s2)
213 {
214     return s2.Equal(s1);
215 }
216 bool operator != (const CFX_WideString& s1, FX_LPCWSTR s2)
217 {
218     return !s1.Equal(s2);
219 }
220 bool operator!=(const CFX_WideString& s1, const CFX_WideString& s2)
221 {
222     return !s1.Equal(s2);
223 }
224 bool operator!=(const CFX_WideString& s1, const CFX_WideStringC& s2)
225 {
226     return !s1.Equal(s2);
227 }
228 bool operator!=(const CFX_WideStringC& s1, const CFX_WideString& s2)
229 {
230     return !s2.Equal(s1);
231 }
232 bool CFX_WideString::Equal(const CFX_WideStringC& str) const
233 {
234     if (m_pData == NULL) {
235         return str.IsEmpty();
236     }
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;
239 }
240 void CFX_WideString::Empty()
241 {
242     if (m_pData == NULL) {
243         return;
244     }
245     if (m_pData->m_nRefs > 1) {
246         m_pData->m_nRefs --;
247     } else {
248         FX_Free(m_pData);
249     }
250     m_pData = NULL;
251 }
252 void CFX_WideString::ConcatInPlace(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData)
253 {
254     if (nSrcLen == 0 || lpszSrcData == NULL) {
255         return;
256     }
257     if (m_pData == NULL) {
258         m_pData = FX_AllocStringW(nSrcLen);
259         if (m_pData) {
260             FXSYS_memcpy32(m_pData->m_String, lpszSrcData, nSrcLen * sizeof(FX_WCHAR));
261         }
262         return;
263     }
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);
268     } else {
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;
272     }
273 }
274 void CFX_WideString::ConcatCopy(FX_STRSIZE nSrc1Len, FX_LPCWSTR lpszSrc1Data,
275                                 FX_STRSIZE nSrc2Len, FX_LPCWSTR lpszSrc2Data)
276 {
277     FX_STRSIZE nNewLen = nSrc1Len + nSrc2Len;
278     if (nNewLen == 0) {
279         return;
280     }
281     m_pData = FX_AllocStringW(nNewLen);
282     if (m_pData) {
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));
285     }
286 }
287 void CFX_WideString::CopyBeforeWrite()
288 {
289     if (m_pData == NULL || m_pData->m_nRefs <= 1) {
290         return;
291     }
292     CFX_StringDataW* pData = m_pData;
293     m_pData->m_nRefs --;
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));
298     }
299 }
300 void CFX_WideString::AllocBeforeWrite(FX_STRSIZE nLen)
301 {
302     if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nLen) {
303         return;
304     }
305     Empty();
306     m_pData = FX_AllocStringW(nLen);
307 }
308 void CFX_WideString::AssignCopy(FX_STRSIZE nSrcLen, FX_LPCWSTR lpszSrcData)
309 {
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;
314 }
315 int CFX_WideString::Compare(FX_LPCWSTR lpsz) const
316 {
317     if (m_pData == NULL) {
318         return (lpsz == NULL || lpsz[0] == 0) ? 0 : -1;
319     }
320     return FXSYS_wcscmp(m_pData->m_String, lpsz);
321 }
322 CFX_ByteString CFX_WideString::UTF8Encode() const
323 {
324     return FX_UTF8Encode(*this);
325 }
326 CFX_ByteString CFX_WideString::UTF16LE_Encode(FX_BOOL bTerminate) const
327 {
328     if (m_pData == NULL) {
329         return bTerminate ? CFX_ByteString(FX_BSTRC("\0\0")) : CFX_ByteString();
330     }
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;
337     }
338     if (bTerminate) {
339         buffer[len * 2] = 0;
340         buffer[len * 2 + 1] = 0;
341         result.ReleaseBuffer(len * 2 + 2);
342     } else {
343         result.ReleaseBuffer(len * 2);
344     }
345     return result;
346 }
347 void CFX_WideString::ConvertFrom(const CFX_ByteString& str, CFX_CharMap* pCharMap)
348 {
349     if (pCharMap == NULL) {
350         pCharMap = CFX_CharMap::GetDefaultMapper();
351     }
352     *this = pCharMap->m_GetWideString(pCharMap, str);
353 }
354 void CFX_WideString::Reserve(FX_STRSIZE len)
355 {
356     GetBuffer(len);
357     ReleaseBuffer(GetLength());
358 }
359 FX_LPWSTR CFX_WideString::GetBuffer(FX_STRSIZE nMinBufLength)
360 {
361     if (m_pData == NULL && nMinBufLength == 0) {
362         return NULL;
363     }
364     if (m_pData && m_pData->m_nRefs <= 1 && m_pData->m_nAllocLength >= nMinBufLength) {
365         return m_pData->m_String;
366     }
367     if (m_pData == NULL) {
368         m_pData = FX_AllocStringW(nMinBufLength);
369         if (!m_pData) {
370             return NULL;
371         }
372         m_pData->m_nDataLength = 0;
373         m_pData->m_String[0] = 0;
374         return m_pData->m_String;
375     }
376     CFX_StringDataW* pOldData = m_pData;
377     FX_STRSIZE nOldLen = pOldData->m_nDataLength;
378     if (nMinBufLength < nOldLen) {
379         nMinBufLength = nOldLen;
380     }
381     m_pData = FX_AllocStringW(nMinBufLength);
382     if (!m_pData) {
383         return NULL;
384     }
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) {
389         FX_Free(pOldData);
390     }
391     return m_pData->m_String;
392 }
393 CFX_WideString CFX_WideString::FromLocal(const char* str, FX_STRSIZE len)
394 {
395     CFX_WideString result;
396     result.ConvertFrom(CFX_ByteString(str, len));
397     return result;
398 }
399 CFX_WideString CFX_WideString::FromUTF8(const char* str, FX_STRSIZE len)
400 {
401     if (!str || 0 == len) {
402         return CFX_WideString();
403     }
404
405     CFX_UTF8Decoder decoder;
406     for (FX_STRSIZE i = 0; i < len; i ++) {
407         decoder.Input(str[i]);
408     }
409     return decoder.GetResult();
410 }
411 CFX_WideString CFX_WideString::FromUTF16LE(const unsigned short* wstr, FX_STRSIZE wlen)
412 {
413     if (!wstr || 0 == wlen) {
414         return CFX_WideString();
415     }
416
417     CFX_WideString result;
418     FX_WCHAR* buf = result.GetBuffer(wlen);
419     for (int i = 0; i < wlen; i ++) {
420         buf[i] = wstr[i];
421     }
422     result.ReleaseBuffer(wlen);
423     return result;
424 }
425 FX_STRSIZE CFX_WideString::WStringLength(const unsigned short* str)
426 {
427     FX_STRSIZE len = 0;
428     if (str)
429         while (str[len]) len++;
430     return len;
431 }
432
433
434
435 void CFX_WideString::AllocCopy(CFX_WideString& dest, FX_STRSIZE nCopyLen, FX_STRSIZE nCopyIndex) const
436 {
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) {
440         return;
441     }
442     base::CheckedNumeric<FX_STRSIZE> iSize = static_cast<FX_STRSIZE>(sizeof(FX_WCHAR));
443     iSize *= nCopyLen;
444     ASSERT(dest.m_pData == NULL);
445     dest.m_pData = FX_AllocStringW(nCopyLen);
446     if (dest.m_pData) {
447         FXSYS_memcpy32(dest.m_pData->m_String, m_pData->m_String + nCopyIndex, iSize.ValueOrDie());
448     }
449 }
450 CFX_WideString CFX_WideString::Left(FX_STRSIZE nCount) const
451 {
452     if (m_pData == NULL) {
453         return CFX_WideString();
454     }
455     if (nCount < 0) {
456         nCount = 0;
457     }
458     if (nCount >= m_pData->m_nDataLength) {
459         return *this;
460     }
461     CFX_WideString dest;
462     AllocCopy(dest, nCount, 0);
463     return dest;
464 }
465 CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst) const
466 {
467     return Mid(nFirst, m_pData->m_nDataLength - nFirst);
468 }
469 CFX_WideString CFX_WideString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const
470 {
471     if (m_pData == NULL) {
472         return CFX_WideString();
473     }
474     if (nFirst < 0) {
475         nFirst = 0;
476     }
477     if (nCount < 0) {
478         nCount = 0;
479     }
480     if (nFirst + nCount > m_pData->m_nDataLength) {
481         nCount = m_pData->m_nDataLength - nFirst;
482     }
483     if (nFirst > m_pData->m_nDataLength) {
484         nCount = 0;
485     }
486     if (nFirst == 0 && nFirst + nCount == m_pData->m_nDataLength) {
487         return *this;
488     }
489     CFX_WideString dest;
490     AllocCopy(dest, nCount, nFirst);
491     return dest;
492 }
493 CFX_WideString CFX_WideString::Right(FX_STRSIZE nCount) const
494 {
495     if (m_pData == NULL) {
496         return CFX_WideString();
497     }
498     if (nCount < 0) {
499         nCount = 0;
500     }
501     if (nCount >= m_pData->m_nDataLength) {
502         return *this;
503     }
504     CFX_WideString dest;
505     AllocCopy(dest, nCount, m_pData->m_nDataLength - nCount);
506     return dest;
507 }
508 int CFX_WideString::CompareNoCase(FX_LPCWSTR lpsz) const
509 {
510     if (m_pData == NULL) {
511         return (lpsz == NULL || lpsz[0] == 0) ? 0 : -1;
512     }
513     return FXSYS_wcsicmp(m_pData->m_String, lpsz);
514 }
515 int CFX_WideString::Compare(const CFX_WideString& str) const
516 {
517     if (m_pData == NULL) {
518         if (str.m_pData == NULL) {
519             return 0;
520         }
521         return -1;
522     } else if (str.m_pData == NULL) {
523         return 1;
524     }
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]) {
530             return -1;
531         } else if (m_pData->m_String[i] > str.m_pData->m_String[i]) {
532             return 1;
533         }
534     }
535     if (this_len < that_len) {
536         return -1;
537     } else if (this_len > that_len) {
538         return 1;
539     }
540     return 0;
541 }
542 FX_LPWSTR CFX_WideString::LockBuffer()
543 {
544     if (m_pData == NULL) {
545         return NULL;
546     }
547     FX_LPWSTR lpsz = GetBuffer(0);
548     m_pData->m_nRefs = -1;
549     return lpsz;
550 }
551 void CFX_WideString::SetAt(FX_STRSIZE nIndex, FX_WCHAR ch)
552 {
553     if (m_pData == NULL) {
554         return;
555     }
556     ASSERT(nIndex >= 0);
557     ASSERT(nIndex < m_pData->m_nDataLength);
558     CopyBeforeWrite();
559     m_pData->m_String[nIndex] = ch;
560 }
561 void CFX_WideString::MakeLower()
562 {
563     if (m_pData == NULL) {
564         return;
565     }
566     CopyBeforeWrite();
567     if (GetLength() < 1) {
568         return;
569     }
570     FXSYS_wcslwr(m_pData->m_String);
571 }
572 void CFX_WideString::MakeUpper()
573 {
574     if (m_pData == NULL) {
575         return;
576     }
577     CopyBeforeWrite();
578     if (GetLength() < 1) {
579         return;
580     }
581     FXSYS_wcsupr(m_pData->m_String);
582 }
583 FX_STRSIZE CFX_WideString::Find(FX_LPCWSTR lpszSub, FX_STRSIZE nStart) const
584 {
585     FX_STRSIZE nLength = GetLength();
586     if (nLength < 1 || nStart > nLength) {
587         return -1;
588     }
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);
591 }
592 FX_STRSIZE CFX_WideString::Find(FX_WCHAR ch, FX_STRSIZE nStart) const
593 {
594     if (m_pData == NULL) {
595         return -1;
596     }
597     FX_STRSIZE nLength = m_pData->m_nDataLength;
598     if (nStart >= nLength) {
599         return -1;
600     }
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);
603 }
604 void CFX_WideString::TrimRight(FX_LPCWSTR lpszTargetList)
605 {
606     FXSYS_assert(lpszTargetList != NULL);
607     if (m_pData == NULL || *lpszTargetList == 0) {
608         return;
609     }
610     CopyBeforeWrite();
611     FX_STRSIZE len = GetLength();
612     if (len < 1) {
613         return;
614     }
615     FX_STRSIZE pos = len;
616     while (pos) {
617         if (FXSYS_wcschr(lpszTargetList, m_pData->m_String[pos - 1]) == NULL) {
618             break;
619         }
620         pos --;
621     }
622     if (pos < len) {
623         m_pData->m_String[pos] = 0;
624         m_pData->m_nDataLength = pos;
625     }
626 }
627 void CFX_WideString::TrimRight(FX_WCHAR chTarget)
628 {
629     FX_WCHAR str[2] = {chTarget, 0};
630     TrimRight(str);
631 }
632 void CFX_WideString::TrimRight()
633 {
634     TrimRight(L"\x09\x0a\x0b\x0c\x0d\x20");
635 }
636 void CFX_WideString::TrimLeft(FX_LPCWSTR lpszTargets)
637 {
638     FXSYS_assert(lpszTargets != NULL);
639     if (m_pData == NULL || *lpszTargets == 0) {
640         return;
641     }
642     CopyBeforeWrite();
643     if (GetLength() < 1) {
644         return;
645     }
646     FX_LPCWSTR lpsz = m_pData->m_String;
647     while (*lpsz != 0) {
648         if (FXSYS_wcschr(lpszTargets, *lpsz) == NULL) {
649             break;
650         }
651         lpsz ++;
652     }
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;
657     }
658 }
659 void CFX_WideString::TrimLeft(FX_WCHAR chTarget)
660 {
661     FX_WCHAR str[2] = {chTarget, 0};
662     TrimLeft(str);
663 }
664 void CFX_WideString::TrimLeft()
665 {
666     TrimLeft(L"\x09\x0a\x0b\x0c\x0d\x20");
667 }
668 FX_STRSIZE CFX_WideString::Replace(FX_LPCWSTR lpszOld, FX_LPCWSTR lpszNew)
669 {
670     if (GetLength() < 1) {
671         return 0;
672     }
673     if (lpszOld == NULL) {
674         return 0;
675     }
676     FX_STRSIZE nSourceLen = (FX_STRSIZE)FXSYS_wcslen(lpszOld);
677     if (nSourceLen == 0) {
678         return 0;
679     }
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;
685     {
686         while ((lpszTarget = (FX_LPWSTR)FXSYS_wcsstr(lpszStart, lpszOld)) != NULL && lpszStart < lpszEnd) {
687             nCount++;
688             lpszStart = lpszTarget + nSourceLen;
689         }
690     }
691     if (nCount > 0) {
692         CopyBeforeWrite();
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);
699             if (!m_pData) {
700                 return 0;
701             }
702             FXSYS_memcpy32(m_pData->m_String, pstr, pOldData->m_nDataLength * sizeof(FX_WCHAR));
703             FX_ReleaseStringW(pOldData);
704         }
705         lpszStart = m_pData->m_String;
706         lpszEnd = m_pData->m_String + FX_MAX(m_pData->m_nDataLength, nNewLength);
707         {
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);
715             }
716         }
717         ASSERT(m_pData->m_String[nNewLength] == 0);
718         m_pData->m_nDataLength = nNewLength;
719     }
720     return nCount;
721 }
722 FX_STRSIZE CFX_WideString::Insert(FX_STRSIZE nIndex, FX_WCHAR ch)
723 {
724     CopyBeforeWrite();
725     if (nIndex < 0) {
726         nIndex = 0;
727     }
728     FX_STRSIZE nNewLength = GetLength();
729     if (nIndex > nNewLength) {
730         nIndex = nNewLength;
731     }
732     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);
737         if (!m_pData) {
738             return 0;
739         }
740         if(pOldData != NULL) {
741             FXSYS_memmove32(m_pData->m_String, pstr, (pOldData->m_nDataLength + 1)*sizeof(FX_WCHAR));
742             FX_ReleaseStringW(pOldData);
743         } else {
744             m_pData->m_String[0] = 0;
745         }
746     }
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;
751     return nNewLength;
752 }
753 FX_STRSIZE CFX_WideString::Delete(FX_STRSIZE nIndex, FX_STRSIZE nCount)
754 {
755     if (GetLength() < 1) {
756         return 0;
757     }
758     if (nIndex < 0) {
759         nIndex = 0;
760     }
761     FX_STRSIZE nOldLength = m_pData->m_nDataLength;
762     if (nCount > 0 && nIndex < nOldLength) {
763         CopyBeforeWrite();
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;
768     }
769     return m_pData->m_nDataLength;
770 }
771 FX_STRSIZE CFX_WideString::Remove(FX_WCHAR chRemove)
772 {
773     if (m_pData == NULL) {
774         return 0;
775     }
776     CopyBeforeWrite();
777     if (GetLength() < 1) {
778         return 0;
779     }
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;
786             pstrDest ++;
787         }
788         pstrSource ++;
789     }
790     *pstrDest = 0;
791     FX_STRSIZE nCount = (FX_STRSIZE)(pstrSource - pstrDest);
792     m_pData->m_nDataLength -= nCount;
793     return nCount;
794 }
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)
799 {
800     va_list argListSave;
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);
803 #else
804     argListSave = argList;
805 #endif
806     int nMaxLen = 0;
807     for (FX_LPCWSTR lpsz = lpszFormat; *lpsz != 0; lpsz ++) {
808         if (*lpsz != '%' || *(lpsz = lpsz + 1) == '%') {
809             nMaxLen += (FX_STRSIZE)FXSYS_wcslen(lpsz);
810             continue;
811         }
812         int nItemLen = 0;
813         int nWidth = 0;
814         for (; *lpsz != 0; lpsz ++) {
815             if (*lpsz == '#') {
816                 nMaxLen += 2;
817             } else if (*lpsz == '*') {
818                 nWidth = va_arg(argList, int);
819             } else if (*lpsz == '-' || *lpsz == '+' || *lpsz == '0' ||
820                        *lpsz == ' ')
821                 ;
822             else {
823                 break;
824             }
825         }
826         if (nWidth == 0) {
827             nWidth = FXSYS_wtoi(lpsz);
828             for (; *lpsz != 0 && (*lpsz) <= '9' && (*lpsz) >= '0'; lpsz ++)
829                 ;
830         }
831         if (nWidth < 0 || nWidth > 128 * 1024) {
832             lpszFormat = (FX_LPCWSTR)L"Bad width";
833             nMaxLen = 10;
834             break;
835         }
836         int nPrecision = 0;
837         if (*lpsz == '.') {
838             lpsz ++;
839             if (*lpsz == '*') {
840                 nPrecision = va_arg(argList, int);
841                 lpsz ++;
842             } else {
843                 nPrecision = FXSYS_wtoi(lpsz);
844                 for (; *lpsz != 0 && (*lpsz) >= '0' && (*lpsz) <= '9'; lpsz ++)
845                     ;
846             }
847         }
848         if (nPrecision < 0 || nPrecision > 128 * 1024) {
849             lpszFormat = (FX_LPCWSTR)L"Bad precision";
850             nMaxLen = 14;
851             break;
852         }
853         int nModifier = 0;
854         if (*lpsz == L'I' && *(lpsz + 1) == L'6' && *(lpsz + 2) == L'4') {
855             lpsz += 3;
856             nModifier = FORCE_INT64;
857         } else {
858             switch (*lpsz) {
859                 case 'h':
860                     nModifier = FORCE_ANSI;
861                     lpsz ++;
862                     break;
863                 case 'l':
864                     nModifier = FORCE_UNICODE;
865                     lpsz ++;
866                     break;
867                 case 'F':
868                 case 'N':
869                 case 'L':
870                     lpsz ++;
871                     break;
872             }
873         }
874         switch (*lpsz | nModifier) {
875             case 'c':
876             case 'C':
877                 nItemLen = 2;
878                 va_arg(argList, int);
879                 break;
880             case 'c'|FORCE_ANSI:
881             case 'C'|FORCE_ANSI:
882                 nItemLen = 2;
883                 va_arg(argList, int);
884                 break;
885             case 'c'|FORCE_UNICODE:
886             case 'C'|FORCE_UNICODE:
887                 nItemLen = 2;
888                 va_arg(argList, int);
889                 break;
890             case 's': {
891                     FX_LPCWSTR pstrNextArg = va_arg(argList, FX_LPCWSTR);
892                     if (pstrNextArg == NULL) {
893                         nItemLen = 6;
894                     } else {
895                         nItemLen = (FX_STRSIZE)FXSYS_wcslen(pstrNextArg);
896                         if (nItemLen < 1) {
897                             nItemLen = 1;
898                         }
899                     }
900                 }
901                 break;
902             case 'S': {
903                     FX_LPCSTR pstrNextArg = va_arg(argList, FX_LPCSTR);
904                     if (pstrNextArg == NULL) {
905                         nItemLen = 6;
906                     } else {
907                         nItemLen = (FX_STRSIZE)FXSYS_strlen(pstrNextArg);
908                         if (nItemLen < 1) {
909                             nItemLen = 1;
910                         }
911                     }
912                 }
913                 break;
914             case 's'|FORCE_ANSI:
915             case 'S'|FORCE_ANSI: {
916                     FX_LPCSTR pstrNextArg = va_arg(argList, FX_LPCSTR);
917                     if (pstrNextArg == NULL) {
918                         nItemLen = 6;
919                     } else {
920                         nItemLen = (FX_STRSIZE)FXSYS_strlen(pstrNextArg);
921                         if (nItemLen < 1) {
922                             nItemLen = 1;
923                         }
924                     }
925                 }
926                 break;
927             case 's'|FORCE_UNICODE:
928             case 'S'|FORCE_UNICODE: {
929                     FX_LPWSTR pstrNextArg = va_arg(argList, FX_LPWSTR);
930                     if (pstrNextArg == NULL) {
931                         nItemLen = 6;
932                     } else {
933                         nItemLen = (FX_STRSIZE)FXSYS_wcslen(pstrNextArg);
934                         if (nItemLen < 1) {
935                             nItemLen = 1;
936                         }
937                     }
938                 }
939                 break;
940         }
941         if (nItemLen != 0) {
942             if (nPrecision != 0 && nItemLen > nPrecision) {
943                 nItemLen = nPrecision;
944             }
945             if (nItemLen < nWidth) {
946                 nItemLen = nWidth;
947             }
948         } else {
949             switch (*lpsz) {
950                 case 'd':
951                 case 'i':
952                 case 'u':
953                 case 'x':
954                 case 'X':
955                 case 'o':
956                     if (nModifier & FORCE_INT64) {
957                         va_arg(argList, FX_INT64);
958                     } else {
959                         va_arg(argList, int);
960                     }
961                     nItemLen = 32;
962                     if (nItemLen < nWidth + nPrecision) {
963                         nItemLen = nWidth + nPrecision;
964                     }
965                     break;
966                 case 'a':
967                 case 'A':
968                 case 'e':
969                 case 'E':
970                 case 'g':
971                 case 'G':
972                     va_arg(argList, double);
973                     nItemLen = 128;
974                     if (nItemLen < nWidth + nPrecision) {
975                         nItemLen = nWidth + nPrecision;
976                     }
977                     break;
978                 case 'f':
979                     if (nWidth + nPrecision > 100) {
980                         nItemLen = nPrecision + nWidth + 128;
981                     } else {
982                         double f;
983                         char pszTemp[256];
984                         f = va_arg(argList, double);
985                         FXSYS_snprintf(pszTemp, sizeof(pszTemp), "%*.*f", nWidth, nPrecision + 6, f );
986                         nItemLen = (FX_STRSIZE)FXSYS_strlen(pszTemp);
987                     }
988                     break;
989                 case 'p':
990                     va_arg(argList, void*);
991                     nItemLen = 32;
992                     if (nItemLen < nWidth + nPrecision) {
993                         nItemLen = nWidth + nPrecision;
994                     }
995                     break;
996                 case 'n':
997                     va_arg(argList, int*);
998                     break;
999             }
1000         }
1001         nMaxLen += nItemLen;
1002     }
1003     GetBuffer(nMaxLen);
1004     if (m_pData) {
1005         FXSYS_vswprintf((wchar_t*)m_pData->m_String, nMaxLen + 1, (const wchar_t*)lpszFormat, argListSave);
1006         ReleaseBuffer();
1007     }
1008     va_end(argListSave);
1009 }
1010 void CFX_WideString::Format(FX_LPCWSTR lpszFormat, ...)
1011 {
1012     va_list argList;
1013     va_start(argList, lpszFormat);
1014     FormatV(lpszFormat, argList);
1015     va_end(argList);
1016 }
1017 FX_FLOAT FX_wtof(FX_LPCWSTR str, int len)
1018 {
1019     if (len == 0) {
1020         return 0.0;
1021     }
1022     int cc = 0;
1023     FX_BOOL bNegative = FALSE;
1024     if (str[0] == '+') {
1025         cc++;
1026     } else if (str[0] == '-') {
1027         bNegative = TRUE;
1028         cc++;
1029     }
1030     int integer = 0;
1031     while (cc < len) {
1032         if (str[cc] == '.') {
1033             break;
1034         }
1035         integer = integer * 10 + str[cc] - '0';
1036         cc ++;
1037     }
1038     FX_FLOAT fraction = 0;
1039     if (str[cc] == '.') {
1040         cc ++;
1041         FX_FLOAT scale = 0.1f;
1042         while (cc < len) {
1043             fraction += scale * (str[cc] - '0');
1044             scale *= 0.1f;
1045             cc ++;
1046         }
1047     }
1048     fraction += (FX_FLOAT)integer;
1049     return bNegative ? -fraction : fraction;
1050 }
1051 int CFX_WideString::GetInteger() const
1052 {
1053     if (m_pData == NULL) {
1054         return 0;
1055     }
1056     return FXSYS_wtoi(m_pData->m_String);
1057 }
1058 FX_FLOAT CFX_WideString::GetFloat() const
1059 {
1060     if (m_pData == NULL) {
1061         return 0.0;
1062     }
1063     return FX_wtof(m_pData->m_String, m_pData->m_nDataLength);
1064 }
1065 static CFX_ByteString _DefMap_GetByteString(CFX_CharMap* pCharMap, const CFX_WideString& widestr)
1066 {
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();
1072     }
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);
1077     return bytestr;
1078 }
1079 static CFX_WideString _DefMap_GetWideString(CFX_CharMap* pCharMap, const CFX_ByteString& bytestr)
1080 {
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();
1086     }
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);
1091     return widestr;
1092 }
1093 static int _DefMap_GetGBKCodePage()
1094 {
1095     return 936;
1096 }
1097 static int _DefMap_GetUHCCodePage()
1098 {
1099     return 949;
1100 }
1101 static int _DefMap_GetJISCodePage()
1102 {
1103     return 932;
1104 }
1105 static int _DefMap_GetBig5CodePage()
1106 {
1107     return 950;
1108 }
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)
1115 {
1116     switch (codepage) {
1117         case 0:
1118             return (CFX_CharMap*)&g_DefaultMapper;
1119         case 932:
1120             return (CFX_CharMap*)&g_DefaultJISMapper;
1121         case 936:
1122             return (CFX_CharMap*)&g_DefaultGBKMapper;
1123         case 949:
1124             return (CFX_CharMap*)&g_DefaultUHCMapper;
1125         case 950:
1126             return (CFX_CharMap*)&g_DefaultBig5Mapper;
1127     }
1128     return NULL;
1129 }