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

Side by Side Diff: fpdfsdk/src/javascript/PublicMethods.cpp

Issue 1582013002: Cleanup CJS_PublicMethods::ParseNumber (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Update bug_361_expected.txt Created 4 years, 11 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 "PublicMethods.h" 7 #include "PublicMethods.h"
8 8
9 #include <algorithm> 9 #include <algorithm>
10 10
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 while (p > pStr && *(p - 1) == L' ') 187 while (p > pStr && *(p - 1) == L' ')
188 p--; 188 p--;
189 189
190 return CFX_ByteString(pStr, p - pStr); 190 return CFX_ByteString(pStr, p - pStr);
191 } 191 }
192 192
193 CFX_ByteString CJS_PublicMethods::StrTrim(const FX_CHAR* pStr) { 193 CFX_ByteString CJS_PublicMethods::StrTrim(const FX_CHAR* pStr) {
194 return StrRTrim(StrLTrim(pStr)); 194 return StrRTrim(StrLTrim(pStr));
195 } 195 }
196 196
197 double CJS_PublicMethods::ParseNumber(const FX_WCHAR* swSource, 197 FX_BOOL CJS_PublicMethods::ConvertStringToNumber(const FX_WCHAR* swSource,
198 FX_BOOL& bAllDigits, 198 double& dRet) {
199 FX_BOOL& bDot, 199 CFX_ByteString sDigits = CFX_WideString(swSource).UTF8Encode();
200 FX_BOOL& bSign,
201 FX_BOOL& bKXJS) {
202 bDot = FALSE;
203 bSign = FALSE;
204 bKXJS = FALSE;
205 200
206 FX_BOOL bDigitExist = FALSE; 201 // Always interpret "," as "." independent of the locale.
207 202 for (FX_STRSIZE k = 0; k < sDigits.GetLength(); k++) {
208 const FX_WCHAR* p = swSource; 203 if (sDigits[k] == ',')
209 wchar_t c; 204 sDigits.SetAt(k, '.');
210
211 const FX_WCHAR* pStart = NULL;
212 const FX_WCHAR* pEnd = NULL;
213
214 while ((c = *p)) {
215 if (!pStart && c != L' ') {
216 pStart = p;
217 }
218
219 pEnd = p;
220 p++;
221 } 205 }
222 206
223 if (!pStart) { 207 // Parse a number, ignoring leading and trailing whitespace.
224 bAllDigits = FALSE; 208 // Fail if there is trailing garbage.
225 return 0; 209 const char* pStart = sDigits.c_str();
210 const char* pEnd = NULL;
211 dRet = strtod(pStart, const_cast<char**>(&pEnd));
212 if (pEnd == pStart) {
213 return FALSE;
226 } 214 }
227 215 for (; *pEnd; pEnd++) {
228 while (pEnd != pStart) { 216 if (!isspace(*pEnd)) {
229 if (*pEnd == L' ') 217 dRet = 0;
230 pEnd--; 218 return FALSE;
231 else
232 break;
233 }
234
235 double dRet = 0;
236 p = pStart;
237 bAllDigits = TRUE;
238 CFX_WideString swDigits;
239
240 while (p <= pEnd) {
241 c = *p;
242
243 if (FXSYS_iswdigit(c)) {
244 swDigits += c;
245 bDigitExist = TRUE;
246 } else {
247 switch (c) {
248 case L' ':
249 bAllDigits = FALSE;
250 break;
251 case L'.':
252 case L',':
253 if (!bDot) {
254 if (bDigitExist) {
255 swDigits += L'.';
256 } else {
257 swDigits += L'0';
258 swDigits += L'.';
259 bDigitExist = TRUE;
260 }
261
262 bDot = TRUE;
263 break;
264 }
265 case 'e':
266 case 'E':
267 if (!bKXJS) {
268 p++;
269 c = *p;
270 if (c == '+' || c == '-') {
271 bKXJS = TRUE;
272 swDigits += 'e';
273 swDigits += c;
274 }
275 break;
276 }
277 case L'-':
278 if (!bDigitExist && !bSign) {
279 swDigits += c;
280 bSign = TRUE;
281 break;
282 }
283 default:
284 bAllDigits = FALSE;
285
286 if (p != pStart && !bDot && bDigitExist) {
287 swDigits += L'.';
288 bDot = TRUE;
289 } else {
290 bDot = FALSE;
291 bDigitExist = FALSE;
292 swDigits = L"";
293 }
294 break;
295 }
296 }
297
298 p++;
299 }
300
301 if (swDigits.GetLength() > 0 && swDigits.GetLength() < 17) {
302 CFX_ByteString sDigits = swDigits.UTF8Encode();
303
304 if (bKXJS) {
305 dRet = atof(sDigits);
306 } else {
307 if (bDot) {
308 char* pStopString;
309 dRet = ::strtod(sDigits, &pStopString);
310 } else {
311 dRet = atol(sDigits);
312 }
313 } 219 }
314 } 220 }
315 221
222 return TRUE;
223 }
224
225 double CJS_PublicMethods::ParseStringToNumber(const FX_WCHAR* swSource) {
226 double dRet;
227 ConvertStringToNumber(swSource, dRet);
316 return dRet; 228 return dRet;
317 } 229 }
318 230
319 double CJS_PublicMethods::ParseStringToNumber(const FX_WCHAR* swSource) {
320 FX_BOOL bAllDigits = FALSE;
321 FX_BOOL bDot = FALSE;
322 FX_BOOL bSign = FALSE;
323 FX_BOOL bKXJS = FALSE;
324
325 return ParseNumber(swSource, bAllDigits, bDot, bSign, bKXJS);
326 }
327
328 FX_BOOL CJS_PublicMethods::ConvertStringToNumber(const FX_WCHAR* swSource,
329 double& dRet,
330 FX_BOOL& bDot) {
331 FX_BOOL bAllDigits = FALSE;
332 FX_BOOL bSign = FALSE;
333 FX_BOOL bKXJS = FALSE;
334
335 dRet = ParseNumber(swSource, bAllDigits, bDot, bSign, bKXJS);
336
337 return bAllDigits;
338 }
339
340 CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime, 231 CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
341 CJS_Value val) { 232 CJS_Value val) {
342 CJS_Array StrArray(pRuntime); 233 CJS_Array StrArray(pRuntime);
343 if (val.IsArrayObject()) { 234 if (val.IsArrayObject()) {
344 val.ConvertToArray(StrArray); 235 val.ConvertToArray(StrArray);
345 return StrArray; 236 return StrArray;
346 } 237 }
347 CFX_WideString wsStr = val.ToCFXWideString(); 238 CFX_WideString wsStr = val.ToCFXWideString();
348 CFX_ByteString t = CFX_ByteString::FromUnicode(wsStr); 239 CFX_ByteString t = CFX_ByteString::FromUnicode(wsStr);
349 const char* p = (const char*)t; 240 const char* p = (const char*)t;
(...skipping 1708 matching lines...) Expand 10 before | Expand all | Expand 10 after
2058 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str())); 1949 nums.SetElement(nIndex, CJS_Value(pRuntime, sPart.c_str()));
2059 } 1950 }
2060 1951
2061 if (nums.GetLength() > 0) 1952 if (nums.GetLength() > 0)
2062 vRet = nums; 1953 vRet = nums;
2063 else 1954 else
2064 vRet.SetNull(); 1955 vRet.SetNull();
2065 1956
2066 return TRUE; 1957 return TRUE;
2067 } 1958 }
OLDNEW
« no previous file with comments | « fpdfsdk/src/javascript/PublicMethods.h ('k') | testing/resources/javascript/bug_361_expected.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698