Chromium Code Reviews| Index: fpdfsdk/javascript/PublicMethods.cpp |
| diff --git a/fpdfsdk/javascript/PublicMethods.cpp b/fpdfsdk/javascript/PublicMethods.cpp |
| index a84c0935bc96db014cc348389cf0626276be5930..883ae8257135c90e6b7f7c7b26056d241aa7e3c6 100644 |
| --- a/fpdfsdk/javascript/PublicMethods.cpp |
| +++ b/fpdfsdk/javascript/PublicMethods.cpp |
| @@ -7,7 +7,6 @@ |
| #include "fpdfsdk/javascript/PublicMethods.h" |
| #include <algorithm> |
| -#include <string> |
| #include <vector> |
| #include "core/fxcrt/include/fx_ext.h" |
| @@ -739,7 +738,7 @@ FX_BOOL CJS_PublicMethods::AFNumber_Format(IJS_Context* cc, |
| int iSepStyle = params[1].ToInt(); |
| int iNegStyle = params[2].ToInt(); |
| // params[3] is iCurrStyle, it's not used. |
| - std::wstring wstrCurrency(params[4].ToCFXWideString().c_str()); |
| + CFX_WideString wstrCurrency = params[4].ToCFXWideString(); |
| FX_BOOL bCurrencyPrepend = params[5].ToBool(); |
| if (iDec < 0) |
| @@ -812,21 +811,18 @@ FX_BOOL CJS_PublicMethods::AFNumber_Format(IJS_Context* cc, |
| // for processing currency string |
| Value = CFX_WideString::FromLocal(strValue.AsStringC()); |
| - std::wstring strValue2 = Value.c_str(); |
| if (bCurrencyPrepend) |
| - strValue2 = wstrCurrency + strValue2; |
| + Value = wstrCurrency + Value; |
| else |
| - strValue2 = strValue2 + wstrCurrency; |
| + Value = Value + wstrCurrency; |
| // for processing negative style |
| if (iNegative) { |
| if (iNegStyle == 0) { |
| - strValue2.insert(0, L"-"); |
| - } |
| - if (iNegStyle == 2 || iNegStyle == 3) { |
| - strValue2.insert(0, L"("); |
| - strValue2.insert(strValue2.length(), L")"); |
| + Value = L"-" + Value; |
| + } else if (iNegStyle == 2 || iNegStyle == 3) { |
| + Value = L"(" + Value + L")"; |
| } |
| if (iNegStyle == 1 || iNegStyle == 3) { |
| if (Field* fTarget = pEvent->Target_Field()) { |
| @@ -882,7 +878,6 @@ FX_BOOL CJS_PublicMethods::AFNumber_Format(IJS_Context* cc, |
| } |
| } |
| } |
| - Value = strValue2.c_str(); |
| #endif |
| return TRUE; |
| } |
| @@ -899,12 +894,10 @@ FX_BOOL CJS_PublicMethods::AFNumber_Keystroke( |
| if (params.size() < 2) |
| return FALSE; |
| - int iSepStyle = params[1].ToInt(); |
| - if (iSepStyle < 0 || iSepStyle > 3) |
| - iSepStyle = 0; |
| if (!pEvent->m_pValue) |
| return FALSE; |
| + |
| CFX_WideString& val = pEvent->Value(); |
| CFX_WideString& w_strChange = pEvent->Change(); |
| CFX_WideString w_strValue = val; |
|
Tom Sepez
2016/05/03 23:34:30
nit: can we lose the _ in these local names?
Lei Zhang
2016/05/09 19:22:35
Done.
|
| @@ -921,19 +914,17 @@ FX_BOOL CJS_PublicMethods::AFNumber_Keystroke( |
| pEvent->Rc() = FALSE; |
| sError = JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE); |
| Alert(pContext, sError.c_str()); |
| - return TRUE; |
| } |
| return TRUE; // it happens after the last keystroke and before validating, |
| } |
| - std::wstring w_strValue2 = w_strValue.c_str(); |
| - std::wstring w_strChange2 = w_strChange.c_str(); |
| - std::wstring w_strSelected; |
| - if (-1 != pEvent->SelStart()) |
| - w_strSelected = w_strValue2.substr(pEvent->SelStart(), |
| - (pEvent->SelEnd() - pEvent->SelStart())); |
| - bool bHasSign = (w_strValue2.find('-') != std::wstring::npos) && |
| - (w_strSelected.find('-') == std::wstring::npos); |
| + CFX_WideString w_strSelected; |
| + if (pEvent->SelStart() != -1) { |
| + w_strSelected = w_strValue.Mid(pEvent->SelStart(), |
| + pEvent->SelEnd() - pEvent->SelStart()); |
| + } |
| + |
| + bool bHasSign = w_strValue.Find(L'-') != -1 && w_strSelected.Find(L'-') == -1; |
| if (bHasSign) { |
| // can't insert "change" in front to sign postion. |
| if (pEvent->SelStart() == 0) { |
| @@ -943,23 +934,14 @@ FX_BOOL CJS_PublicMethods::AFNumber_Keystroke( |
| } |
| } |
| - char cSep = L'.'; |
| - |
| - switch (iSepStyle) { |
| - case 0: |
| - case 1: |
| - cSep = L'.'; |
| - break; |
| - case 2: |
| - case 3: |
| - cSep = L','; |
| - break; |
| - } |
| + int iSepStyle = params[1].ToInt(); |
| + if (iSepStyle < 0 || iSepStyle > 3) |
| + iSepStyle = 0; |
| + const FX_WCHAR cSep = iSepStyle < 2 ? L'.' : L','; |
| - bool bHasSep = (w_strValue2.find(cSep) != std::wstring::npos); |
| - for (std::wstring::iterator it = w_strChange2.begin(); |
| - it != w_strChange2.end(); it++) { |
| - if (*it == cSep) { |
| + bool bHasSep = w_strValue.Find(cSep) != -1; |
| + for (FX_STRSIZE i = 0; i < w_strChange.GetLength(); ++i) { |
| + if (w_strChange[i] == cSep) { |
| if (bHasSep) { |
| FX_BOOL& bRc = pEvent->Rc(); |
| bRc = FALSE; |
| @@ -968,14 +950,14 @@ FX_BOOL CJS_PublicMethods::AFNumber_Keystroke( |
| bHasSep = TRUE; |
| continue; |
| } |
| - if (*it == L'-') { |
| + if (w_strChange[i] == L'-') { |
| if (bHasSign) { |
| FX_BOOL& bRc = pEvent->Rc(); |
| bRc = FALSE; |
| return TRUE; |
| } |
| // sign's position is not correct |
| - if (it != w_strChange2.begin()) { |
| + if (i != 0) { |
| FX_BOOL& bRc = pEvent->Rc(); |
| bRc = FALSE; |
| return TRUE; |
| @@ -989,20 +971,18 @@ FX_BOOL CJS_PublicMethods::AFNumber_Keystroke( |
| continue; |
| } |
| - if (!FXSYS_iswdigit(*it)) { |
| + if (!FXSYS_iswdigit(w_strChange[i])) { |
| FX_BOOL& bRc = pEvent->Rc(); |
| bRc = FALSE; |
| return TRUE; |
| } |
| } |
| - std::wstring w_prefix = w_strValue2.substr(0, pEvent->SelStart()); |
| - std::wstring w_postfix; |
| - if (pEvent->SelEnd() < (int)w_strValue2.length()) |
| - w_postfix = w_strValue2.substr(pEvent->SelEnd()); |
| - w_strValue2 = w_prefix + w_strChange2 + w_postfix; |
| - w_strValue = w_strValue2.c_str(); |
| - val = w_strValue; |
| + CFX_WideString w_prefix = w_strValue.Mid(0, pEvent->SelStart()); |
| + CFX_WideString w_postfix; |
| + if (pEvent->SelEnd() < w_strValue.GetLength()) |
| + w_postfix = w_strValue.Mid(pEvent->SelEnd()); |
| + val = w_prefix + w_strChange + w_postfix; |
| return TRUE; |
| } |
| @@ -1440,21 +1420,18 @@ FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx( |
| if (wstrMask.IsEmpty()) |
| return TRUE; |
| - const size_t wstrMaskLen = wstrMask.GetLength(); |
| - const std::wstring wstrValue = valEvent.c_str(); |
| - |
| if (pEvent->WillCommit()) { |
| - if (wstrValue.empty()) |
| + if (valEvent.IsEmpty()) |
| return TRUE; |
| - size_t iIndexMask = 0; |
| - for (const auto& w_Value : wstrValue) { |
| - if (!maskSatisfied(w_Value, wstrMask[iIndexMask])) |
| + |
| + FX_STRSIZE iIndexMask = 0; |
| + for (; iIndexMask < valEvent.GetLength(); ++iIndexMask) { |
| + if (!maskSatisfied(valEvent[iIndexMask], wstrMask[iIndexMask])) |
| break; |
| - iIndexMask++; |
| } |
| - if (iIndexMask != wstrMaskLen || |
| - (iIndexMask != wstrValue.size() && wstrMaskLen != 0)) { |
| + if (iIndexMask != wstrMask.GetLength() || |
| + (iIndexMask != valEvent.GetLength() && wstrMask.GetLength() != 0)) { |
| Alert( |
| pContext, |
| JSGetStringFromID(pContext, IDS_STRING_JSAFNUMBER_KEYSTROKE).c_str()); |
| @@ -1464,48 +1441,45 @@ FX_BOOL CJS_PublicMethods::AFSpecial_KeystrokeEx( |
| } |
| CFX_WideString& wideChange = pEvent->Change(); |
| - std::wstring wChange = wideChange.c_str(); |
| - if (wChange.empty()) |
| + if (wideChange.IsEmpty()) |
| return TRUE; |
| - size_t iIndexMask = pEvent->SelStart(); |
| - |
| - size_t combined_len = wstrValue.length() + wChange.length() - |
| - (pEvent->SelEnd() - pEvent->SelStart()); |
| - if (combined_len > wstrMaskLen) { |
| + CFX_WideString wChange = wideChange; |
| + FX_STRSIZE iIndexMask = pEvent->SelStart(); |
| + FX_STRSIZE combined_len = valEvent.GetLength() + wChange.GetLength() + |
| + pEvent->SelStart() - pEvent->SelEnd(); |
| + if (combined_len > wstrMask.GetLength()) { |
| Alert(pContext, |
| JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str()); |
| pEvent->Rc() = FALSE; |
| return TRUE; |
| } |
| - if (iIndexMask >= wstrMaskLen && (!wChange.empty())) { |
| + if (iIndexMask >= wstrMask.GetLength() && !wChange.IsEmpty()) { |
| Alert(pContext, |
| JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str()); |
| pEvent->Rc() = FALSE; |
| return TRUE; |
| } |
| - for (std::wstring::iterator it = wChange.begin(); it != wChange.end(); it++) { |
| - if (iIndexMask >= wstrMaskLen) { |
| + for (FX_STRSIZE i = 0; i < wChange.GetLength(); ++i) { |
| + if (iIndexMask >= wstrMask.GetLength()) { |
| Alert(pContext, |
| JSGetStringFromID(pContext, IDS_STRING_JSPARAM_TOOLONG).c_str()); |
| pEvent->Rc() = FALSE; |
| return TRUE; |
| } |
| - wchar_t w_Mask = wstrMask[iIndexMask]; |
| - if (!isReservedMaskChar(w_Mask)) { |
| - *it = w_Mask; |
| - } |
| - wchar_t w_Change = *it; |
| - if (!maskSatisfied(w_Change, w_Mask)) { |
| + FX_WCHAR w_Mask = wstrMask[iIndexMask]; |
| + if (!isReservedMaskChar(w_Mask)) |
| + wChange.SetAt(i, w_Mask); |
| + |
| + if (!maskSatisfied(wChange[i], w_Mask)) { |
| pEvent->Rc() = FALSE; |
| return TRUE; |
| } |
| iIndexMask++; |
| } |
| - |
| - wideChange = wChange.c_str(); |
| + wideChange = wChange; |
| return TRUE; |
| } |
| @@ -1525,13 +1499,8 @@ FX_BOOL CJS_PublicMethods::AFSpecial_Keystroke( |
| if (!pEvent->m_pValue) |
| return FALSE; |
| - std::string cFormat; |
| - int iIndex = params[0].ToInt(); |
| - CFX_WideString& val = pEvent->Value(); |
| - std::string strSrc = CFX_ByteString::FromUnicode(val).c_str(); |
| - std::wstring wstrChange = pEvent->Change().c_str(); |
| - |
| - switch (iIndex) { |
| + const char* cFormat = ""; |
| + switch (params[0].ToInt()) { |
| case 0: |
| cFormat = "99999"; |
| break; |
| @@ -1539,7 +1508,7 @@ FX_BOOL CJS_PublicMethods::AFSpecial_Keystroke( |
| cFormat = "999999999"; |
| break; |
| case 2: |
| - if (strSrc.length() + wstrChange.length() > 7) |
| + if (pEvent->Value().GetLength() + pEvent->Change().GetLength() > 7) |
| cFormat = "9999999999"; |
| else |
| cFormat = "9999999"; |
| @@ -1550,7 +1519,7 @@ FX_BOOL CJS_PublicMethods::AFSpecial_Keystroke( |
| } |
| std::vector<CJS_Value> params2; |
| - params2.push_back(CJS_Value(CJS_Runtime::FromContext(cc), cFormat.c_str())); |
| + params2.push_back(CJS_Value(CJS_Runtime::FromContext(cc), cFormat)); |
| return AFSpecial_KeystrokeEx(cc, params2, vRet, sError); |
| } |