Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: core/src/fxcrt/fx_basic_wstring.cpp

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

Powered by Google App Engine
This is Rietveld 408576698