summaryrefslogtreecommitdiff
path: root/fxjs/cfxjse_formcalc_context.cpp
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2017-11-14 21:29:35 +0000
committerChromium commit bot <commit-bot@chromium.org>2017-11-14 21:29:35 +0000
commit4011677aed8b258fcf87cf52b0d541ef04c832ff (patch)
treedc5f8124fc0e81c15213e4b0ebeecc83c96049d7 /fxjs/cfxjse_formcalc_context.cpp
parentaee28693976cc246eea8230a999906802e52cab7 (diff)
downloadpdfium-4011677aed8b258fcf87cf52b0d541ef04c832ff.tar.xz
Move code out of xfa_utilschromium/3271chromium/3270chromium/3269
This CL splits the node template out of xfa_utils into its own file. The XFA_ByteStringToDouble method was moved to the only calling file. Change-Id: I85fb2dfa3afc4a675ec69574e32d643c0dca731f Reviewed-on: https://pdfium-review.googlesource.com/18490 Reviewed-by: Tom Sepez <tsepez@chromium.org> Commit-Queue: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'fxjs/cfxjse_formcalc_context.cpp')
-rw-r--r--fxjs/cfxjse_formcalc_context.cpp92
1 files changed, 89 insertions, 3 deletions
diff --git a/fxjs/cfxjse_formcalc_context.cpp b/fxjs/cfxjse_formcalc_context.cpp
index 55fab55722..30fa18737b 100644
--- a/fxjs/cfxjse_formcalc_context.cpp
+++ b/fxjs/cfxjse_formcalc_context.cpp
@@ -532,6 +532,90 @@ ByteString GUIDString(bool bSeparator) {
return bsStr;
}
+double ByteStringToDouble(const ByteStringView& szStringVal) {
+ WideString wsValue = WideString::FromUTF8(szStringVal);
+ wsValue.TrimLeft();
+ wsValue.TrimRight();
+
+ int32_t cc = 0;
+ bool bNegative = false;
+
+ const wchar_t* str = wsValue.c_str();
+ int32_t len = wsValue.GetLength();
+ if (str[0] == '+') {
+ cc++;
+ } else if (str[0] == '-') {
+ bNegative = true;
+ cc++;
+ }
+
+ int32_t nIntegralLen = 0;
+ int64_t nIntegral = 0;
+ while (cc < len) {
+ if (str[cc] == '.' || str[cc] == 'E' || str[cc] == 'e' ||
+ nIntegralLen > 17) {
+ break;
+ }
+ if (!FXSYS_isDecimalDigit(str[cc])) {
+ return 0;
+ }
+ nIntegral = nIntegral * 10 + str[cc] - '0';
+ cc++;
+ nIntegralLen++;
+ }
+ nIntegral = bNegative ? -nIntegral : nIntegral;
+
+ int32_t scale = 0;
+ double fraction = 0.0;
+ uint32_t dwFractional = 0;
+ if (cc < len && str[cc] == '.') {
+ cc++;
+ while (cc < len) {
+ fraction += XFA_GetFractionalScale(scale) * (str[cc] - '0');
+ scale++;
+ cc++;
+ if (cc == len)
+ break;
+ if (scale == XFA_GetMaxFractionalScale() || str[cc] == 'E' ||
+ str[cc] == 'e') {
+ break;
+ }
+ if (!FXSYS_isDecimalDigit(str[cc]))
+ return 0;
+ }
+ dwFractional = static_cast<uint32_t>(fraction * 4294967296.0);
+ }
+
+ int32_t nExponent = 0;
+ bool bExpSign = false;
+ if (cc < len && (str[cc] == 'E' || str[cc] == 'e')) {
+ cc++;
+ if (cc < len) {
+ if (str[cc] == '+') {
+ cc++;
+ } else if (str[cc] == '-') {
+ bExpSign = true;
+ cc++;
+ }
+ }
+ while (cc < len) {
+ if (str[cc] == '.' || !FXSYS_isDecimalDigit(str[cc]))
+ return 0;
+
+ nExponent = nExponent * 10 + str[cc] - '0';
+ cc++;
+ }
+ nExponent = bExpSign ? -nExponent : nExponent;
+ }
+
+ double dValue = dwFractional / 4294967296.0;
+ dValue = nIntegral + (nIntegral >= 0 ? dValue : -dValue);
+ if (nExponent != 0)
+ dValue *= FXSYS_pow(10, static_cast<float>(nExponent));
+
+ return dValue;
+}
+
} // namespace
// static
@@ -6005,8 +6089,10 @@ float CFXJSE_FormCalcContext::ValueToFloat(CFXJSE_Value* pThis,
GetObjectDefaultValue(arg, newPropertyValue.get());
return ValueToFloat(pThis, newPropertyValue.get());
}
- if (arg->IsString())
- return (float)XFA_ByteStringToDouble(arg->ToString().AsStringView());
+ if (arg->IsString()) {
+ return static_cast<float>(
+ ByteStringToDouble(arg->ToString().AsStringView()));
+ }
if (arg->IsUndefined())
return 0;
@@ -6040,7 +6126,7 @@ double CFXJSE_FormCalcContext::ValueToDouble(CFXJSE_Value* pThis,
return ValueToDouble(pThis, newPropertyValue.get());
}
if (arg->IsString())
- return XFA_ByteStringToDouble(arg->ToString().AsStringView());
+ return ByteStringToDouble(arg->ToString().AsStringView());
if (arg->IsUndefined())
return 0;
return arg->ToDouble();