From 574ee81e426a7390e5cdf28f2fe8ec03f6c2da98 Mon Sep 17 00:00:00 2001 From: dan sinclair Date: Mon, 16 Apr 2018 21:01:58 +0000 Subject: Split GetNumbericSymbol into individual methods This CL removes the GetNumbericSymbol method and adds individual methods for each symbol we retrieve. Change-Id: Id14108e5ea43a76250e18a1aa13bdbb05e928cdc Reviewed-on: https://pdfium-review.googlesource.com/30695 Commit-Queue: dsinclair Reviewed-by: Henrique Nakashima --- core/fxcrt/locale_iface.h | 16 ++---- xfa/fgas/crt/cfgas_formatstring.cpp | 64 +++++++++-------------- xfa/fxfa/parser/cxfa_localevalue.cpp | 2 +- xfa/fxfa/parser/cxfa_node.cpp | 7 ++- xfa/fxfa/parser/cxfa_nodelocale.cpp | 36 ++++++------- xfa/fxfa/parser/cxfa_nodelocale.h | 7 ++- xfa/fxfa/parser/cxfa_xmllocale.cpp | 78 ++++++++++++----------------- xfa/fxfa/parser/cxfa_xmllocale.h | 7 ++- xfa/fxfa/parser/cxfa_xmllocale_unittest.cpp | 14 +++--- 9 files changed, 98 insertions(+), 133 deletions(-) diff --git a/core/fxcrt/locale_iface.h b/core/fxcrt/locale_iface.h index 0adfb19682..7e0ca6fd32 100644 --- a/core/fxcrt/locale_iface.h +++ b/core/fxcrt/locale_iface.h @@ -10,16 +10,6 @@ #include "core/fxcrt/cfx_datetime.h" #include "core/fxcrt/fx_string.h" -enum FX_LOCALENUMSYMBOL { - FX_LOCALENUMSYMBOL_Decimal, - FX_LOCALENUMSYMBOL_Grouping, - FX_LOCALENUMSYMBOL_Percent, - FX_LOCALENUMSYMBOL_Minus, - FX_LOCALENUMSYMBOL_Zero, - FX_LOCALENUMSYMBOL_CurrencySymbol, - FX_LOCALENUMSYMBOL_CurrencyName, -}; - enum FX_LOCALEDATETIMESUBCATEGORY { FX_LOCALEDATETIMESUBCATEGORY_Default, FX_LOCALEDATETIMESUBCATEGORY_Short, @@ -59,7 +49,11 @@ class LocaleIface { virtual ~LocaleIface() {} virtual WideString GetName() const = 0; - virtual WideString GetNumbericSymbol(FX_LOCALENUMSYMBOL eType) const = 0; + virtual WideString GetDecimalSymbol() const = 0; + virtual WideString GetGroupingSymbol() const = 0; + virtual WideString GetPercentSymbol() const = 0; + virtual WideString GetMinusSymbol() const = 0; + virtual WideString GetCurrencySymbol() const = 0; virtual WideString GetDateTimeSymbols() const = 0; virtual WideString GetMonthName(int32_t nMonth, bool bAbbr) const = 0; virtual WideString GetDayName(int32_t nWeek, bool bAbbr) const = 0; diff --git a/xfa/fgas/crt/cfgas_formatstring.cpp b/xfa/fgas/crt/cfgas_formatstring.cpp index 1edcf1e51d..0162100949 100644 --- a/xfa/fgas/crt/cfgas_formatstring.cpp +++ b/xfa/fgas/crt/cfgas_formatstring.cpp @@ -1151,12 +1151,10 @@ bool CFGAS_FormatString::ParseNum(const WideString& wsSrcNum, return false; int32_t iExponent = 0; - WideString wsDotSymbol = - pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Decimal); - WideString wsGroupSymbol = - pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Grouping); + WideString wsDotSymbol = pLocale->GetDecimalSymbol(); + WideString wsGroupSymbol = pLocale->GetGroupingSymbol(); int32_t iGroupLen = wsGroupSymbol.GetLength(); - WideString wsMinus = pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Minus); + WideString wsMinus = pLocale->GetMinusSymbol(); int32_t iMinusLen = wsMinus.GetLength(); const wchar_t* str = wsSrcNum.c_str(); int len = wsSrcNum.GetLength(); @@ -1259,8 +1257,7 @@ bool CFGAS_FormatString::ParseNum(const WideString& wsSrcNum, break; } case '$': { - WideString wsSymbol = - pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_CurrencySymbol); + WideString wsSymbol = pLocale->GetCurrencySymbol(); int32_t iSymbolLen = wsSymbol.GetLength(); cc -= iSymbolLen - 1; if (cc < 0 || wcsncmp(str + cc, wsSymbol.c_str(), iSymbolLen)) @@ -1301,8 +1298,7 @@ bool CFGAS_FormatString::ParseNum(const WideString& wsSrcNum, } break; case '%': { - WideString wsSymbol = - pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Percent); + WideString wsSymbol = pLocale->GetPercentSymbol(); int32_t iSysmbolLen = wsSymbol.GetLength(); cc -= iSysmbolLen - 1; if (cc < 0 || wcsncmp(str + cc, wsSymbol.c_str(), iSysmbolLen)) @@ -1435,8 +1431,7 @@ bool CFGAS_FormatString::ParseNum(const WideString& wsSrcNum, break; } case '$': { - WideString wsSymbol = - pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_CurrencySymbol); + WideString wsSymbol = pLocale->GetCurrencySymbol(); int32_t iSymbolLen = wsSymbol.GetLength(); if (cc + iSymbolLen > len || wcsncmp(str + cc, wsSymbol.c_str(), iSymbolLen)) { @@ -1477,8 +1472,7 @@ bool CFGAS_FormatString::ParseNum(const WideString& wsSrcNum, case 'v': return false; case '%': { - WideString wsSymbol = - pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Percent); + WideString wsSymbol = pLocale->GetPercentSymbol(); int32_t iSysmbolLen = wsSymbol.GetLength(); if (cc + iSysmbolLen <= len && !wcsncmp(str + cc, wsSymbol.c_str(), iSysmbolLen)) { @@ -1932,8 +1926,7 @@ bool CFGAS_FormatString::FormatStrNum(const WideStringView& wsInputNum, wsSrcNum.TrimRight(L"."); } - WideString wsGroupSymbol = - pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Grouping); + WideString wsGroupSymbol = pLocale->GetGroupingSymbol(); bool bNeg = false; if (wsSrcNum[0] == '-') { bNeg = true; @@ -1988,8 +1981,7 @@ bool CFGAS_FormatString::FormatStrNum(const WideStringView& wsInputNum, break; case 'S': if (bNeg) { - *wsOutput = - pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Minus) + *wsOutput; + *wsOutput = pLocale->GetMinusSymbol() + *wsOutput; bAddNeg = true; } else { wsOutput->InsertAtFront(L' '); @@ -1998,8 +1990,7 @@ bool CFGAS_FormatString::FormatStrNum(const WideStringView& wsInputNum, break; case 's': if (bNeg) { - *wsOutput = - pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Minus) + *wsOutput; + *wsOutput = pLocale->GetMinusSymbol() + *wsOutput; bAddNeg = true; } ccf--; @@ -2010,9 +2001,7 @@ bool CFGAS_FormatString::FormatStrNum(const WideStringView& wsInputNum, break; } case '$': { - *wsOutput = - pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_CurrencySymbol) + - *wsOutput; + *wsOutput = pLocale->GetCurrencySymbol() + *wsOutput; ccf--; break; } @@ -2049,8 +2038,7 @@ bool CFGAS_FormatString::FormatStrNum(const WideStringView& wsInputNum, } break; case '%': { - *wsOutput = - pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Percent) + *wsOutput; + *wsOutput = pLocale->GetPercentSymbol() + *wsOutput; ccf--; break; } @@ -2089,26 +2077,23 @@ bool CFGAS_FormatString::FormatStrNum(const WideStringView& wsInputNum, *wsOutput += wsSrcNum[i]; } if (pdfium::base::checked_cast(dot_index.value()) < len) { - *wsOutput += pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Decimal); + *wsOutput += pLocale->GetDecimalSymbol(); *wsOutput += wsSrcNum.Right(len - dot_index.value() - 1); } - if (bNeg) { - *wsOutput = - pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Minus) + *wsOutput; - } + if (bNeg) + *wsOutput = pLocale->GetMinusSymbol() + *wsOutput; + return false; } if (dot_index_f == pdfium::base::checked_cast(wsNumFormat.GetLength())) { - if (!bAddNeg && bNeg) { - *wsOutput = - pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Minus) + *wsOutput; - } + if (!bAddNeg && bNeg) + *wsOutput = pLocale->GetMinusSymbol() + *wsOutput; + return true; } - WideString wsDotSymbol = - pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Decimal); + WideString wsDotSymbol = pLocale->GetDecimalSymbol(); if (strf[dot_index_f] == 'V') { *wsOutput += wsDotSymbol; } else if (strf[dot_index_f] == '.') { @@ -2166,8 +2151,7 @@ bool CFGAS_FormatString::FormatStrNum(const WideStringView& wsInputNum, break; } case '$': - *wsOutput += - pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_CurrencySymbol); + *wsOutput += pLocale->GetCurrencySymbol(); ccf++; break; case 'c': @@ -2203,7 +2187,7 @@ bool CFGAS_FormatString::FormatStrNum(const WideStringView& wsInputNum, } break; case '%': - *wsOutput += pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Percent); + *wsOutput += pLocale->GetPercentSymbol(); ccf++; break; case '8': @@ -2232,8 +2216,8 @@ bool CFGAS_FormatString::FormatStrNum(const WideStringView& wsInputNum, } } if (!bAddNeg && bNeg) { - *wsOutput = pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Minus) + - (*wsOutput)[0] + wsOutput->Right(wsOutput->GetLength() - 1); + *wsOutput = pLocale->GetMinusSymbol() + (*wsOutput)[0] + + wsOutput->Right(wsOutput->GetLength() - 1); } return true; } diff --git a/xfa/fxfa/parser/cxfa_localevalue.cpp b/xfa/fxfa/parser/cxfa_localevalue.cpp index b129960f7d..057eaeba2c 100644 --- a/xfa/fxfa/parser/cxfa_localevalue.cpp +++ b/xfa/fxfa/parser/cxfa_localevalue.cpp @@ -760,7 +760,7 @@ bool CXFA_LocaleValue::ValidateNumericTemp(const WideString& wsNumeric, WideString wsDecimalSymbol; if (pLocale) - wsDecimalSymbol = pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Decimal); + wsDecimalSymbol = pLocale->GetDecimalSymbol(); else wsDecimalSymbol = WideString(L'.'); diff --git a/xfa/fxfa/parser/cxfa_node.cpp b/xfa/fxfa/parser/cxfa_node.cpp index ff9475970f..10f0a40017 100644 --- a/xfa/fxfa/parser/cxfa_node.cpp +++ b/xfa/fxfa/parser/cxfa_node.cpp @@ -4564,8 +4564,7 @@ WideString CXFA_Node::FormatNumStr(const WideString& wsValue, return L""; WideString wsSrcNum = wsValue; - WideString wsGroupSymbol = - pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Grouping); + WideString wsGroupSymbol = pLocale->GetGroupingSymbol(); bool bNeg = false; if (wsSrcNum[0] == '-') { bNeg = true; @@ -4587,11 +4586,11 @@ WideString CXFA_Node::FormatNumStr(const WideString& wsValue, wsOutput += wsSrcNum[i]; } if (dot_index.value() < wsSrcNum.GetLength()) { - wsOutput += pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Decimal); + wsOutput += pLocale->GetDecimalSymbol(); wsOutput += wsSrcNum.Right(wsSrcNum.GetLength() - dot_index.value() - 1); } if (bNeg) - return pLocale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Minus) + wsOutput; + return pLocale->GetMinusSymbol() + wsOutput; return wsOutput; } diff --git a/xfa/fxfa/parser/cxfa_nodelocale.cpp b/xfa/fxfa/parser/cxfa_nodelocale.cpp index 71f4049b6b..4f4e3a958b 100644 --- a/xfa/fxfa/parser/cxfa_nodelocale.cpp +++ b/xfa/fxfa/parser/cxfa_nodelocale.cpp @@ -50,24 +50,24 @@ WideString CXFA_NodeLocale::GetName() const { : nullptr); } -WideString CXFA_NodeLocale::GetNumbericSymbol(FX_LOCALENUMSYMBOL eType) const { - switch (eType) { - case FX_LOCALENUMSYMBOL_Decimal: - return GetSymbol(XFA_Element::NumberSymbols, L"decimal"); - case FX_LOCALENUMSYMBOL_Grouping: - return GetSymbol(XFA_Element::NumberSymbols, L"grouping"); - case FX_LOCALENUMSYMBOL_Percent: - return GetSymbol(XFA_Element::NumberSymbols, L"percent"); - case FX_LOCALENUMSYMBOL_Minus: - return GetSymbol(XFA_Element::NumberSymbols, L"minus"); - case FX_LOCALENUMSYMBOL_Zero: - return GetSymbol(XFA_Element::NumberSymbols, L"zero"); - case FX_LOCALENUMSYMBOL_CurrencySymbol: - return GetSymbol(XFA_Element::CurrencySymbols, L"symbol"); - case FX_LOCALENUMSYMBOL_CurrencyName: - return GetSymbol(XFA_Element::CurrencySymbols, L"isoname"); - } - return WideString(); +WideString CXFA_NodeLocale::GetDecimalSymbol() const { + return GetSymbol(XFA_Element::NumberSymbols, L"decimal"); +} + +WideString CXFA_NodeLocale::GetGroupingSymbol() const { + return GetSymbol(XFA_Element::NumberSymbols, L"grouping"); +} + +WideString CXFA_NodeLocale::GetPercentSymbol() const { + return GetSymbol(XFA_Element::NumberSymbols, L"percent"); +} + +WideString CXFA_NodeLocale::GetMinusSymbol() const { + return GetSymbol(XFA_Element::NumberSymbols, L"minus"); +} + +WideString CXFA_NodeLocale::GetCurrencySymbol() const { + return GetSymbol(XFA_Element::CurrencySymbols, L"symbol"); } WideString CXFA_NodeLocale::GetDateTimeSymbols() const { diff --git a/xfa/fxfa/parser/cxfa_nodelocale.h b/xfa/fxfa/parser/cxfa_nodelocale.h index d1ed800846..e802cd9b34 100644 --- a/xfa/fxfa/parser/cxfa_nodelocale.h +++ b/xfa/fxfa/parser/cxfa_nodelocale.h @@ -23,8 +23,11 @@ class CXFA_NodeLocale : public LocaleIface { // LocaleIface WideString GetName() const override; - WideString GetNumbericSymbol(FX_LOCALENUMSYMBOL eType) const override; - + WideString GetDecimalSymbol() const override; + WideString GetGroupingSymbol() const override; + WideString GetPercentSymbol() const override; + WideString GetMinusSymbol() const override; + WideString GetCurrencySymbol() const override; WideString GetDateTimeSymbols() const override; WideString GetMonthName(int32_t nMonth, bool bAbbr) const override; WideString GetDayName(int32_t nWeek, bool bAbbr) const override; diff --git a/xfa/fxfa/parser/cxfa_xmllocale.cpp b/xfa/fxfa/parser/cxfa_xmllocale.cpp index 93b84ca367..6720d56cba 100644 --- a/xfa/fxfa/parser/cxfa_xmllocale.cpp +++ b/xfa/fxfa/parser/cxfa_xmllocale.cpp @@ -17,6 +17,15 @@ #include "xfa/fxfa/parser/cxfa_timezoneprovider.h" #include "xfa/fxfa/parser/xfa_utils.h" +namespace { + +constexpr wchar_t kNumberSymbols[] = L"numberSymbols"; +constexpr wchar_t kNumberSymbol[] = L"numberSymbol"; +constexpr wchar_t kCurrencySymbols[] = L"currencySymbols"; +constexpr wchar_t kCurrencySymbol[] = L"currencySymbol"; + +} // namespace + // static std::unique_ptr CXFA_XMLLocale::Create( pdfium::span data) { @@ -59,54 +68,29 @@ WideString CXFA_XMLLocale::GetName() const { return locale_->GetString(L"name"); } -WideString CXFA_XMLLocale::GetNumbericSymbol(FX_LOCALENUMSYMBOL eType) const { - WideStringView bsSymbols; - WideStringView bsSymbol; - WideStringView wsName; - switch (eType) { - case FX_LOCALENUMSYMBOL_Decimal: - bsSymbols = L"numberSymbols"; - bsSymbol = L"numberSymbol"; - wsName = L"decimal"; - break; - case FX_LOCALENUMSYMBOL_Grouping: - bsSymbols = L"numberSymbols"; - bsSymbol = L"numberSymbol"; - wsName = L"grouping"; - break; - case FX_LOCALENUMSYMBOL_Percent: - bsSymbols = L"numberSymbols"; - bsSymbol = L"numberSymbol"; - wsName = L"percent"; - break; - case FX_LOCALENUMSYMBOL_Minus: - bsSymbols = L"numberSymbols"; - bsSymbol = L"numberSymbol"; - wsName = L"minus"; - break; - case FX_LOCALENUMSYMBOL_Zero: - bsSymbols = L"numberSymbols"; - bsSymbol = L"numberSymbol"; - wsName = L"zero"; - break; - case FX_LOCALENUMSYMBOL_CurrencySymbol: - bsSymbols = L"currencySymbols"; - bsSymbol = L"currencySymbol"; - wsName = L"symbol"; - break; - case FX_LOCALENUMSYMBOL_CurrencyName: - bsSymbols = L"currencySymbols"; - bsSymbol = L"currencySymbol"; - wsName = L"isoname"; - break; - default: - return L""; - } - CFX_XMLElement* patterns = locale_->GetFirstChildNamed(bsSymbols); - if (!patterns) - return L""; +WideString CXFA_XMLLocale::GetDecimalSymbol() const { + CFX_XMLElement* patterns = locale_->GetFirstChildNamed(kNumberSymbols); + return patterns ? GetPattern(patterns, kNumberSymbol, L"decimal") : L""; +} + +WideString CXFA_XMLLocale::GetGroupingSymbol() const { + CFX_XMLElement* patterns = locale_->GetFirstChildNamed(kNumberSymbols); + return patterns ? GetPattern(patterns, kNumberSymbol, L"grouping") : L""; +} + +WideString CXFA_XMLLocale::GetPercentSymbol() const { + CFX_XMLElement* patterns = locale_->GetFirstChildNamed(kNumberSymbols); + return patterns ? GetPattern(patterns, kNumberSymbol, L"percent") : L""; +} + +WideString CXFA_XMLLocale::GetMinusSymbol() const { + CFX_XMLElement* patterns = locale_->GetFirstChildNamed(kNumberSymbols); + return patterns ? GetPattern(patterns, kNumberSymbol, L"minus") : L""; +} - return GetPattern(patterns, bsSymbol, wsName); +WideString CXFA_XMLLocale::GetCurrencySymbol() const { + CFX_XMLElement* patterns = locale_->GetFirstChildNamed(kCurrencySymbols); + return patterns ? GetPattern(patterns, kCurrencySymbol, L"symbol") : L""; } WideString CXFA_XMLLocale::GetDateTimeSymbols() const { diff --git a/xfa/fxfa/parser/cxfa_xmllocale.h b/xfa/fxfa/parser/cxfa_xmllocale.h index 92bd3607ec..757c5349d4 100644 --- a/xfa/fxfa/parser/cxfa_xmllocale.h +++ b/xfa/fxfa/parser/cxfa_xmllocale.h @@ -25,8 +25,11 @@ class CXFA_XMLLocale : public LocaleIface { // LocaleIface WideString GetName() const override; - WideString GetNumbericSymbol(FX_LOCALENUMSYMBOL eType) const override; - + WideString GetDecimalSymbol() const override; + WideString GetGroupingSymbol() const override; + WideString GetPercentSymbol() const override; + WideString GetMinusSymbol() const override; + WideString GetCurrencySymbol() const override; WideString GetDateTimeSymbols() const override; WideString GetMonthName(int32_t nMonth, bool bAbbr) const override; WideString GetDayName(int32_t nWeek, bool bAbbr) const override; diff --git a/xfa/fxfa/parser/cxfa_xmllocale_unittest.cpp b/xfa/fxfa/parser/cxfa_xmllocale_unittest.cpp index e2c5198eb8..32abdeb063 100644 --- a/xfa/fxfa/parser/cxfa_xmllocale_unittest.cpp +++ b/xfa/fxfa/parser/cxfa_xmllocale_unittest.cpp @@ -108,20 +108,18 @@ TEST(CXFA_XMLLocaleTest, GetName) { EXPECT_EQ(L"en_US", locale->GetName()); } -TEST(CXFA_XMLLocaleTest, GetNumbericSymbol) { +TEST(CXFA_XMLLocaleTest, GetNumericSymbols) { auto span = pdfium::make_span(reinterpret_cast(const_cast(xml_data)), strlen(xml_data)); auto locale = CXFA_XMLLocale::Create(span); ASSERT_TRUE(locale != nullptr); - EXPECT_EQ(L".", locale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Decimal)); - EXPECT_EQ(L",", locale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Grouping)); - EXPECT_EQ(L"%", locale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Percent)); - EXPECT_EQ(L"-", locale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Minus)); - EXPECT_EQ(L"0", locale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_Zero)); - EXPECT_EQ(L"$", locale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_CurrencySymbol)); - EXPECT_EQ(L"USD", locale->GetNumbericSymbol(FX_LOCALENUMSYMBOL_CurrencyName)); + EXPECT_EQ(L".", locale->GetDecimalSymbol()); + EXPECT_EQ(L",", locale->GetGroupingSymbol()); + EXPECT_EQ(L"%", locale->GetPercentSymbol()); + EXPECT_EQ(L"-", locale->GetMinusSymbol()); + EXPECT_EQ(L"$", locale->GetCurrencySymbol()); } TEST(CXFA_XMLLocaleTest, GetDateTimeSymbols) { -- cgit v1.2.3