diff options
author | Lei Zhang <thestig@chromium.org> | 2016-02-25 14:35:23 -0800 |
---|---|---|
committer | Lei Zhang <thestig@chromium.org> | 2016-02-25 14:35:23 -0800 |
commit | f8e21f34e9b67c5b9e754e949c58a5113ea4d746 (patch) | |
tree | d6d2c774ae099320c41fab43cfa4857f1274aa25 /xfa/src/fxfa | |
parent | 198362223d2257ded96895bd91df2bea422f361e (diff) | |
download | pdfium-f8e21f34e9b67c5b9e754e949c58a5113ea4d746.tar.xz |
Use std::lower_bound() in a couple places.
Do some cleanups along the way.
R=tsepez@chromium.org
Review URL: https://codereview.chromium.org/1732223002 .
Diffstat (limited to 'xfa/src/fxfa')
-rw-r--r-- | xfa/src/fxfa/src/fm2js/xfa_simpleexpression.cpp | 99 | ||||
-rw-r--r-- | xfa/src/fxfa/src/fm2js/xfa_simpleexpression.h | 141 |
2 files changed, 129 insertions, 111 deletions
diff --git a/xfa/src/fxfa/src/fm2js/xfa_simpleexpression.cpp b/xfa/src/fxfa/src/fm2js/xfa_simpleexpression.cpp index b0ce3ad4ca..68faa7ae0c 100644 --- a/xfa/src/fxfa/src/fm2js/xfa_simpleexpression.cpp +++ b/xfa/src/fxfa/src/fm2js/xfa_simpleexpression.cpp @@ -5,7 +5,10 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com #include "xfa/src/fxfa/src/fm2js/xfa_fm2js.h" -static CFX_WideStringC gs_lpStrExpFuncName[] = { + +namespace { + +const CFX_WideStringC gs_lpStrExpFuncName[] = { FX_WSTRC(L"foxit_xfa_formcalc_runtime.assign_value_operator"), FX_WSTRC(L"foxit_xfa_formcalc_runtime.logical_or_operator"), FX_WSTRC(L"foxit_xfa_formcalc_runtime.logical_and_operator"), @@ -32,11 +35,12 @@ static CFX_WideStringC gs_lpStrExpFuncName[] = { FX_WSTRC(L"foxit_xfa_formcalc_runtime.get_fm_jsobj"), FX_WSTRC(L"foxit_xfa_formcalc_runtime.fm_var_filter"), }; -CFX_WideStringC XFA_FM_EXPTypeToString( - XFA_FM_SimpleExpressionType simpleExpType) { - return gs_lpStrExpFuncName[simpleExpType]; -} -static XFA_FMBuildInFunc buildInFuncs[] = { + +struct XFA_FMBuildInFunc { + uint32_t m_uHash; + const FX_WCHAR* m_buildinfunc; +}; +const XFA_FMBuildInFunc g_BuildInFuncs[] = { {0x0001f1f5, L"At"}, {0x00020b9c, L"FV"}, {0x00021aef, L"If"}, {0x00023ee6, L"PV"}, {0x04b5c9ee, L"Encode"}, {0x08e96685, L"DateFmt"}, @@ -71,7 +75,13 @@ static XFA_FMBuildInFunc buildInFuncs[] = { {0xea18d121, L"Choose"}, {0xebfef69c, L"Replace"}, {0xf5ad782b, L"Left"}, {0xf7bb2248, L"Ltrim"}, }; -static const XFA_FMSOMMethod gs_FMSomMethods[] = { + +struct XFA_FMSOMMethod { + uint32_t m_uHash; + const FX_WCHAR* m_wsSomMethodName; + FX_DWORD m_dParameters; +}; +const XFA_FMSOMMethod gs_FMSomMethods[] = { {0x00000068, L"h", 0x01}, {0x00000077, L"w", 0x01}, {0x00000078, L"x", 0x01}, @@ -94,6 +104,14 @@ static const XFA_FMSOMMethod gs_FMSomMethods[] = { {0xda12e518, L"append", 0x01}, {0xe74f0653, L"absPage", 0x01}, }; + +} // namespace + +CFX_WideStringC XFA_FM_EXPTypeToString( + XFA_FM_SimpleExpressionType simpleExpType) { + return gs_lpStrExpFuncName[simpleExpType]; +} + CXFA_FMSimpleExpression::CXFA_FMSimpleExpression(FX_DWORD line, XFA_FM_TOKEN op) : m_line(line), m_op(op) {} void CXFA_FMSimpleExpression::ToJavaScript(CFX_WideTextBuf& javascript) {} @@ -174,10 +192,7 @@ CXFA_FMUnaryExpression::CXFA_FMUnaryExpression(FX_DWORD line, CXFA_FMSimpleExpression* pExp) : CXFA_FMSimpleExpression(line, op), m_pExp(pExp) {} CXFA_FMUnaryExpression::~CXFA_FMUnaryExpression() { - if (m_pExp != 0) { - delete m_pExp; - m_pExp = 0; - } + delete m_pExp; } void CXFA_FMUnaryExpression::ToJavaScript(CFX_WideTextBuf& javascript) {} CXFA_FMBinExpression::CXFA_FMBinExpression(FX_DWORD line, @@ -186,14 +201,8 @@ CXFA_FMBinExpression::CXFA_FMBinExpression(FX_DWORD line, CXFA_FMSimpleExpression* pExp2) : CXFA_FMSimpleExpression(line, op), m_pExp1(pExp1), m_pExp2(pExp2) {} CXFA_FMBinExpression::~CXFA_FMBinExpression() { - if (m_pExp1 != 0) { - delete m_pExp1; - m_pExp1 = 0; - } - if (m_pExp2 != 0) { - delete m_pExp2; - m_pExp2 = 0; - } + delete m_pExp1; + delete m_pExp2; } void CXFA_FMBinExpression::ToJavaScript(CFX_WideTextBuf& javascript) {} CXFA_FMAssignExpression::CXFA_FMAssignExpression(FX_DWORD line, @@ -446,29 +455,25 @@ CXFA_FMCallExpression::~CXFA_FMCallExpression() { m_pArguments = 0; } } -FX_BOOL CXFA_FMCallExpression::IsBuildInFunc(CFX_WideTextBuf& funcName) { - uint32_t uHash = - FX_HashCode_String_GetW(funcName.GetBuffer(), funcName.GetLength(), TRUE); - XFA_FMBuildInFunc buildinfunction; - int32_t iStart = 0, - iEnd = (sizeof(buildInFuncs) / sizeof(buildInFuncs[0])) - 1; - int32_t iMid = (iStart + iEnd) / 2; - do { - iMid = (iStart + iEnd) / 2; - buildinfunction = buildInFuncs[iMid]; - if (uHash == buildinfunction.m_uHash) { - funcName.Clear(); - funcName << buildinfunction.m_buildinfunc; - return TRUE; - } else if (uHash < buildinfunction.m_uHash) { - iEnd = iMid - 1; - } else { - iStart = iMid + 1; - } - } while (iStart <= iEnd); - return FALSE; + +bool CXFA_FMCallExpression::IsBuildInFunc(CFX_WideTextBuf* funcName) { + uint32_t uHash = FX_HashCode_String_GetW(funcName->GetBuffer(), + funcName->GetLength(), TRUE); + const XFA_FMBuildInFunc* pEnd = g_BuildInFuncs + FX_ArraySize(g_BuildInFuncs); + const XFA_FMBuildInFunc* pFunc = + std::lower_bound(g_BuildInFuncs, pEnd, uHash, + [](const XFA_FMBuildInFunc& func, uint32_t hash) { + return func.m_uHash < hash; + }); + if (pFunc < pEnd && uHash == pFunc->m_uHash) { + funcName->Clear(); + *funcName << pFunc->m_buildinfunc; + return true; + } + return false; } -FX_DWORD CXFA_FMCallExpression::IsSomMethodWithObjPara( + +FX_DWORD CXFA_FMCallExpression::IsMethodWithObjParam( const CFX_WideStringC& methodName) { int32_t iLength = methodName.GetLength(); uint32_t uHash = FX_HashCode_String_GetW(methodName.GetPtr(), iLength); @@ -500,7 +505,7 @@ void CXFA_FMCallExpression::ToJavaScript(CFX_WideTextBuf& javascript) { if (m_pArguments) { int32_t argc = m_pArguments->GetSize(); int32_t index = 0; - FX_DWORD methodPara = IsSomMethodWithObjPara(funcName.GetWideString()); + FX_DWORD methodPara = IsMethodWithObjParam(funcName.GetWideString()); if (methodPara > 0) { CXFA_FMSimpleExpression* e = 0; while (index < argc) { @@ -535,16 +540,16 @@ void CXFA_FMCallExpression::ToJavaScript(CFX_WideTextBuf& javascript) { } javascript << FX_WSTRC(L")"); } else { - FX_BOOL isEvalFunc = FALSE; - FX_BOOL isExistsFunc = FALSE; - if (IsBuildInFunc(funcName)) { + bool isEvalFunc = false; + bool isExistsFunc = false; + if (IsBuildInFunc(&funcName)) { if (funcName.GetWideString() == FX_WSTRC(L"Eval")) { - isEvalFunc = TRUE; + isEvalFunc = true; javascript << FX_WSTRC(L"eval.call(this, "); javascript << gs_lpStrExpFuncName[CALL]; javascript << FX_WSTRC(L"Translate"); } else if (funcName.GetWideString() == FX_WSTRC(L"Exists")) { - isExistsFunc = TRUE; + isExistsFunc = true; javascript << gs_lpStrExpFuncName[CALL]; javascript << funcName; } else { diff --git a/xfa/src/fxfa/src/fm2js/xfa_simpleexpression.h b/xfa/src/fxfa/src/fm2js/xfa_simpleexpression.h index f474004300..2fb9b8fc1c 100644 --- a/xfa/src/fxfa/src/fm2js/xfa_simpleexpression.h +++ b/xfa/src/fxfa/src/fm2js/xfa_simpleexpression.h @@ -36,27 +36,21 @@ enum XFA_FM_SimpleExpressionType { GETFMJSOBJ, VARFILTER }; + CFX_WideStringC XFA_FM_EXPTypeToString( XFA_FM_SimpleExpressionType simpleExpType); -struct XFA_FMBuildInFunc { - uint32_t m_uHash; - const FX_WCHAR* m_buildinfunc; -}; -struct XFA_FMSOMMethod { - uint32_t m_uHash; - const FX_WCHAR* m_wsSomMethodName; - FX_DWORD m_dParameters; -}; + enum XFA_FM_AccessorIndex { ACCESSOR_NO_INDEX, ACCESSOR_NO_RELATIVEINDEX, ACCESSOR_POSITIVE_INDEX, ACCESSOR_NEGATIVE_INDEX }; + class CXFA_FMSimpleExpression { public: CXFA_FMSimpleExpression(FX_DWORD line, XFA_FM_TOKEN op); - virtual ~CXFA_FMSimpleExpression(){}; + virtual ~CXFA_FMSimpleExpression() {} virtual void ToJavaScript(CFX_WideTextBuf& javascript); virtual void ToImpliedReturnJS(CFX_WideTextBuf& javascript); @@ -66,160 +60,178 @@ class CXFA_FMSimpleExpression { FX_DWORD m_line; XFA_FM_TOKEN m_op; }; + class CXFA_FMNullExpression : public CXFA_FMSimpleExpression { public: - CXFA_FMNullExpression(FX_DWORD line); - virtual ~CXFA_FMNullExpression(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + explicit CXFA_FMNullExpression(FX_DWORD line); + ~CXFA_FMNullExpression() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; }; + class CXFA_FMNumberExpression : public CXFA_FMSimpleExpression { public: CXFA_FMNumberExpression(FX_DWORD line, CFX_WideStringC wsNumber); - virtual ~CXFA_FMNumberExpression(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMNumberExpression() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; - protected: + private: CFX_WideStringC m_wsNumber; }; + class CXFA_FMStringExpression : public CXFA_FMSimpleExpression { public: CXFA_FMStringExpression(FX_DWORD line, CFX_WideStringC wsString); - virtual ~CXFA_FMStringExpression(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMStringExpression() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; - protected: + private: CFX_WideStringC m_wsString; }; + class CXFA_FMIdentifierExpressionn : public CXFA_FMSimpleExpression { public: CXFA_FMIdentifierExpressionn(FX_DWORD line, CFX_WideStringC wsIdentifier); - virtual ~CXFA_FMIdentifierExpressionn(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMIdentifierExpressionn() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; - protected: + private: CFX_WideStringC m_wsIdentifier; }; + class CXFA_FMUnaryExpression : public CXFA_FMSimpleExpression { public: CXFA_FMUnaryExpression(FX_DWORD line, XFA_FM_TOKEN op, CXFA_FMSimpleExpression* pExp); - virtual ~CXFA_FMUnaryExpression(); - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMUnaryExpression() override; + void ToJavaScript(CFX_WideTextBuf& javascript) override; protected: CXFA_FMSimpleExpression* m_pExp; }; + class CXFA_FMBinExpression : public CXFA_FMSimpleExpression { public: CXFA_FMBinExpression(FX_DWORD line, XFA_FM_TOKEN op, CXFA_FMSimpleExpression* pExp1, CXFA_FMSimpleExpression* pExp2); - virtual ~CXFA_FMBinExpression(); - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMBinExpression() override; + void ToJavaScript(CFX_WideTextBuf& javascript) override; protected: CXFA_FMSimpleExpression* m_pExp1; CXFA_FMSimpleExpression* m_pExp2; }; + class CXFA_FMAssignExpression : public CXFA_FMBinExpression { public: CXFA_FMAssignExpression(FX_DWORD line, XFA_FM_TOKEN op, CXFA_FMSimpleExpression* pExp1, CXFA_FMSimpleExpression* pExp2); - virtual ~CXFA_FMAssignExpression(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); - virtual void ToImpliedReturnJS(CFX_WideTextBuf& javascript); + ~CXFA_FMAssignExpression() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; + void ToImpliedReturnJS(CFX_WideTextBuf& javascript) override; }; + class CXFA_FMLogicalOrExpression : public CXFA_FMBinExpression { public: CXFA_FMLogicalOrExpression(FX_DWORD line, XFA_FM_TOKEN op, CXFA_FMSimpleExpression* pExp1, CXFA_FMSimpleExpression* pExp2); - virtual ~CXFA_FMLogicalOrExpression(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMLogicalOrExpression() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; }; + class CXFA_FMLogicalAndExpression : public CXFA_FMBinExpression { public: CXFA_FMLogicalAndExpression(FX_DWORD line, XFA_FM_TOKEN op, CXFA_FMSimpleExpression* pExp1, CXFA_FMSimpleExpression* pExp2); - virtual ~CXFA_FMLogicalAndExpression(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMLogicalAndExpression() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; }; + class CXFA_FMEqualityExpression : public CXFA_FMBinExpression { public: CXFA_FMEqualityExpression(FX_DWORD line, XFA_FM_TOKEN op, CXFA_FMSimpleExpression* pExp1, CXFA_FMSimpleExpression* pExp2); - virtual ~CXFA_FMEqualityExpression(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMEqualityExpression() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; }; + class CXFA_FMRelationalExpression : public CXFA_FMBinExpression { public: CXFA_FMRelationalExpression(FX_DWORD line, XFA_FM_TOKEN op, CXFA_FMSimpleExpression* pExp1, CXFA_FMSimpleExpression* pExp2); - virtual ~CXFA_FMRelationalExpression(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMRelationalExpression() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; }; + class CXFA_FMAdditiveExpression : public CXFA_FMBinExpression { public: CXFA_FMAdditiveExpression(FX_DWORD line, XFA_FM_TOKEN op, CXFA_FMSimpleExpression* pExp1, CXFA_FMSimpleExpression* pExp2); - virtual ~CXFA_FMAdditiveExpression(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMAdditiveExpression() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; }; + class CXFA_FMMultiplicativeExpression : public CXFA_FMBinExpression { public: CXFA_FMMultiplicativeExpression(FX_DWORD line, XFA_FM_TOKEN op, CXFA_FMSimpleExpression* pExp1, CXFA_FMSimpleExpression* pExp2); - virtual ~CXFA_FMMultiplicativeExpression(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMMultiplicativeExpression() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; }; + class CXFA_FMPosExpression : public CXFA_FMUnaryExpression { public: CXFA_FMPosExpression(FX_DWORD line, CXFA_FMSimpleExpression* pExp); - virtual ~CXFA_FMPosExpression(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMPosExpression() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; }; + class CXFA_FMNegExpression : public CXFA_FMUnaryExpression { public: CXFA_FMNegExpression(FX_DWORD line, CXFA_FMSimpleExpression* pExp); - virtual ~CXFA_FMNegExpression(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMNegExpression() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; }; + class CXFA_FMNotExpression : public CXFA_FMUnaryExpression { public: CXFA_FMNotExpression(FX_DWORD line, CXFA_FMSimpleExpression* pExp); - virtual ~CXFA_FMNotExpression(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMNotExpression() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; }; + class CXFA_FMCallExpression : public CXFA_FMUnaryExpression { public: CXFA_FMCallExpression(FX_DWORD line, CXFA_FMSimpleExpression* pExp, CFX_PtrArray* pArguments, FX_BOOL bIsSomMethod); - virtual ~CXFA_FMCallExpression(); - virtual FX_BOOL IsBuildInFunc(CFX_WideTextBuf& funcName); - virtual FX_DWORD IsSomMethodWithObjPara(const CFX_WideStringC& methodName); - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMCallExpression() override; + bool IsBuildInFunc(CFX_WideTextBuf* funcName); + FX_DWORD IsMethodWithObjParam(const CFX_WideStringC& methodName); + void ToJavaScript(CFX_WideTextBuf& javascript) override; private: FX_BOOL m_bIsSomMethod; CFX_PtrArray* m_pArguments; }; + class CXFA_FMDotAccessorExpression : public CXFA_FMBinExpression { public: CXFA_FMDotAccessorExpression(FX_DWORD line, @@ -227,25 +239,27 @@ class CXFA_FMDotAccessorExpression : public CXFA_FMBinExpression { XFA_FM_TOKEN op, CFX_WideStringC wsIdentifier, CXFA_FMSimpleExpression* pIndexExp); - virtual ~CXFA_FMDotAccessorExpression(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMDotAccessorExpression() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; - protected: + private: CFX_WideStringC m_wsIdentifier; }; + class CXFA_FMIndexExpression : public CXFA_FMUnaryExpression { public: CXFA_FMIndexExpression(FX_DWORD line, XFA_FM_AccessorIndex accessorIndex, CXFA_FMSimpleExpression* pIndexExp, FX_BOOL bIsStarIndex); - virtual ~CXFA_FMIndexExpression(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMIndexExpression() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; - protected: + private: XFA_FM_AccessorIndex m_accessorIndex; FX_BOOL m_bIsStarIndex; }; + class CXFA_FMDotDotAccessorExpression : public CXFA_FMBinExpression { public: CXFA_FMDotDotAccessorExpression(FX_DWORD line, @@ -253,21 +267,20 @@ class CXFA_FMDotDotAccessorExpression : public CXFA_FMBinExpression { XFA_FM_TOKEN op, CFX_WideStringC wsIdentifier, CXFA_FMSimpleExpression* pIndexExp); - virtual ~CXFA_FMDotDotAccessorExpression(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); + ~CXFA_FMDotDotAccessorExpression() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; - protected: + private: CFX_WideStringC m_wsIdentifier; }; + class CXFA_FMMethodCallExpression : public CXFA_FMBinExpression { public: CXFA_FMMethodCallExpression(FX_DWORD line, CXFA_FMSimpleExpression* pAccessorExp1, CXFA_FMSimpleExpression* pCallExp); - virtual ~CXFA_FMMethodCallExpression(){}; - virtual void ToJavaScript(CFX_WideTextBuf& javascript); - - protected: + ~CXFA_FMMethodCallExpression() override {} + void ToJavaScript(CFX_WideTextBuf& javascript) override; }; #endif // XFA_SRC_FXFA_SRC_FM2JS_XFA_SIMPLEEXPRESSION_H_ |