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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « fpdfsdk/src/javascript/PublicMethods.h ('k') | testing/resources/javascript/bug_361_expected.txt » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: fpdfsdk/src/javascript/PublicMethods.cpp
diff --git a/fpdfsdk/src/javascript/PublicMethods.cpp b/fpdfsdk/src/javascript/PublicMethods.cpp
index d2f7fb09ac674078624f8104476ec95bd78f0422..9e5ad5d1825bf930e864903491feead8b2c7a14f 100644
--- a/fpdfsdk/src/javascript/PublicMethods.cpp
+++ b/fpdfsdk/src/javascript/PublicMethods.cpp
@@ -194,147 +194,38 @@ CFX_ByteString CJS_PublicMethods::StrTrim(const FX_CHAR* pStr) {
return StrRTrim(StrLTrim(pStr));
}
-double CJS_PublicMethods::ParseNumber(const FX_WCHAR* swSource,
- FX_BOOL& bAllDigits,
- FX_BOOL& bDot,
- FX_BOOL& bSign,
- FX_BOOL& bKXJS) {
- bDot = FALSE;
- bSign = FALSE;
- bKXJS = FALSE;
-
- FX_BOOL bDigitExist = FALSE;
-
- const FX_WCHAR* p = swSource;
- wchar_t c;
-
- const FX_WCHAR* pStart = NULL;
- const FX_WCHAR* pEnd = NULL;
-
- while ((c = *p)) {
- if (!pStart && c != L' ') {
- pStart = p;
- }
-
- pEnd = p;
- p++;
- }
-
- if (!pStart) {
- bAllDigits = FALSE;
- return 0;
- }
+FX_BOOL CJS_PublicMethods::ConvertStringToNumber(const FX_WCHAR* swSource,
+ double& dRet) {
+ CFX_ByteString sDigits = CFX_WideString(swSource).UTF8Encode();
- while (pEnd != pStart) {
- if (*pEnd == L' ')
- pEnd--;
- else
- break;
+ // Always interpret "," as "." independent of the locale.
+ for (FX_STRSIZE k = 0; k < sDigits.GetLength(); k++) {
+ if (sDigits[k] == ',')
+ sDigits.SetAt(k, '.');
}
- double dRet = 0;
- p = pStart;
- bAllDigits = TRUE;
- CFX_WideString swDigits;
-
- while (p <= pEnd) {
- c = *p;
-
- if (FXSYS_iswdigit(c)) {
- swDigits += c;
- bDigitExist = TRUE;
- } else {
- switch (c) {
- case L' ':
- bAllDigits = FALSE;
- break;
- case L'.':
- case L',':
- if (!bDot) {
- if (bDigitExist) {
- swDigits += L'.';
- } else {
- swDigits += L'0';
- swDigits += L'.';
- bDigitExist = TRUE;
- }
-
- bDot = TRUE;
- break;
- }
- case 'e':
- case 'E':
- if (!bKXJS) {
- p++;
- c = *p;
- if (c == '+' || c == '-') {
- bKXJS = TRUE;
- swDigits += 'e';
- swDigits += c;
- }
- break;
- }
- case L'-':
- if (!bDigitExist && !bSign) {
- swDigits += c;
- bSign = TRUE;
- break;
- }
- default:
- bAllDigits = FALSE;
-
- if (p != pStart && !bDot && bDigitExist) {
- swDigits += L'.';
- bDot = TRUE;
- } else {
- bDot = FALSE;
- bDigitExist = FALSE;
- swDigits = L"";
- }
- break;
- }
- }
-
- p++;
+ // Parse a number, ignoring leading and trailing whitespace.
+ // Fail if there is trailing garbage.
+ const char* pStart = sDigits.c_str();
+ const char* pEnd = NULL;
+ dRet = strtod(pStart, const_cast<char**>(&pEnd));
+ if (pEnd == pStart) {
+ return FALSE;
}
-
- if (swDigits.GetLength() > 0 && swDigits.GetLength() < 17) {
- CFX_ByteString sDigits = swDigits.UTF8Encode();
-
- if (bKXJS) {
- dRet = atof(sDigits);
- } else {
- if (bDot) {
- char* pStopString;
- dRet = ::strtod(sDigits, &pStopString);
- } else {
- dRet = atol(sDigits);
- }
+ for (; *pEnd; pEnd++) {
+ if (!isspace(*pEnd)) {
+ dRet = 0;
+ return FALSE;
}
}
- return dRet;
+ return TRUE;
}
double CJS_PublicMethods::ParseStringToNumber(const FX_WCHAR* swSource) {
- FX_BOOL bAllDigits = FALSE;
- FX_BOOL bDot = FALSE;
- FX_BOOL bSign = FALSE;
- FX_BOOL bKXJS = FALSE;
-
- return ParseNumber(swSource, bAllDigits, bDot, bSign, bKXJS);
-}
-
-FX_BOOL CJS_PublicMethods::ConvertStringToNumber(const FX_WCHAR* swSource,
- double& dRet,
- FX_BOOL& bDot) {
- FX_BOOL bAllDigits = FALSE;
- FX_BOOL bSign = FALSE;
- FX_BOOL bKXJS = FALSE;
-
- dRet = ParseNumber(swSource, bAllDigits, bDot, bSign, bKXJS);
-
- return bAllDigits;
+ double dRet;
+ ConvertStringToNumber(swSource, dRet);
+ return dRet;
}
CJS_Array CJS_PublicMethods::AF_MakeArrayFromList(CJS_Runtime* pRuntime,
« 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