Use spans in CFGAS_StringFormatter::SplitFormatString()
- rename to SplitByBars()
- return vector instead of out param.
- put conditional/increment in for statement.
- test bars inside quotes aren't split.
Change-Id: I9622a8a620604c119a210acc26efde5079b1a386
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/51773
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/xfa/fgas/crt/cfgas_stringformatter.cpp b/xfa/fgas/crt/cfgas_stringformatter.cpp
index 96f6fbc..ee8321f 100644
--- a/xfa/fgas/crt/cfgas_stringformatter.cpp
+++ b/xfa/fgas/crt/cfgas_stringformatter.cpp
@@ -829,27 +829,23 @@
CFGAS_StringFormatter::~CFGAS_StringFormatter() {}
-void CFGAS_StringFormatter::SplitFormatString(
- const WideString& wsFormatString,
- std::vector<WideString>* wsPatterns) const {
- int32_t iStrLen = wsFormatString.GetLength();
- const wchar_t* pStr = wsFormatString.c_str();
- const wchar_t* pToken = pStr;
- const wchar_t* pEnd = pStr + iStrLen;
+std::vector<WideString> CFGAS_StringFormatter::SplitOnBars(
+ const WideString& wsFormatString) {
+ std::vector<WideString> wsPatterns;
+ pdfium::span<const wchar_t> spFormatString = wsFormatString.AsSpan();
+ size_t index = 0;
+ size_t token = 0;
bool bQuote = false;
- while (true) {
- if (pStr >= pEnd) {
- wsPatterns->push_back(WideString(pToken, pStr - pToken));
- return;
- }
- if (*pStr == '\'') {
+ for (; index < spFormatString.size(); ++index) {
+ if (spFormatString[index] == '\'') {
bQuote = !bQuote;
- } else if (*pStr == L'|' && !bQuote) {
- wsPatterns->push_back(WideString(pToken, pStr - pToken));
- pToken = pStr + 1;
+ } else if (spFormatString[index] == L'|' && !bQuote) {
+ wsPatterns.emplace_back(spFormatString.data() + token, index - token);
+ token = index + 1;
}
- pStr++;
}
+ wsPatterns.emplace_back(spFormatString.data() + token, index - token);
+ return wsPatterns;
}
FX_LOCALECATEGORY CFGAS_StringFormatter::GetCategory(
diff --git a/xfa/fgas/crt/cfgas_stringformatter.h b/xfa/fgas/crt/cfgas_stringformatter.h
index 906508f..9583724 100644
--- a/xfa/fgas/crt/cfgas_stringformatter.h
+++ b/xfa/fgas/crt/cfgas_stringformatter.h
@@ -25,8 +25,7 @@
explicit CFGAS_StringFormatter(LocaleMgrIface* pLocaleMgr);
~CFGAS_StringFormatter();
- void SplitFormatString(const WideString& wsFormatString,
- std::vector<WideString>* wsPatterns) const;
+ std::vector<WideString> SplitOnBars(const WideString& wsFormatString);
FX_LOCALECATEGORY GetCategory(const WideString& wsPattern) const;
bool ParseText(const WideString& wsSrcText,
diff --git a/xfa/fgas/crt/cfgas_stringformatter_unittest.cpp b/xfa/fgas/crt/cfgas_stringformatter_unittest.cpp
index 478f053..c3e7c39 100644
--- a/xfa/fgas/crt/cfgas_stringformatter_unittest.cpp
+++ b/xfa/fgas/crt/cfgas_stringformatter_unittest.cpp
@@ -288,15 +288,21 @@
// }
TEST_F(CFGAS_StringFormatterTest, SplitFormatString) {
- std::vector<WideString> results;
- fmt(L"en")->SplitFormatString(
- L"null{'No data'} | null{} | text{999*9999} | text{999*999*9999}",
- &results);
+ std::vector<WideString> results = fmt(L"en")->SplitOnBars(L"");
+ EXPECT_EQ(1UL, results.size());
+ EXPECT_TRUE(results[0].IsEmpty());
+
+ results = fmt(L"en")->SplitOnBars(L"|");
+ EXPECT_EQ(2UL, results.size());
+ EXPECT_TRUE(results[0].IsEmpty());
+ EXPECT_TRUE(results[1].IsEmpty());
+
+ results = fmt(L"en")->SplitOnBars(
+ L"null{'No|data'} | null{} | text{999*9999} | text{999*999*9999}");
EXPECT_EQ(4UL, results.size());
- const wchar_t* patterns[] = {L"null{'No data'} ", L" null{} ",
+ const wchar_t* patterns[] = {L"null{'No|data'} ", L" null{} ",
L" text{999*9999} ", L" text{999*999*9999}"};
-
for (size_t i = 0; i < results.size(); ++i) {
EXPECT_STREQ(patterns[i], results[i].c_str());
}
diff --git a/xfa/fxfa/parser/cxfa_localevalue.cpp b/xfa/fxfa/parser/cxfa_localevalue.cpp
index 0b9ce1f..d9515e7 100644
--- a/xfa/fxfa/parser/cxfa_localevalue.cpp
+++ b/xfa/fxfa/parser/cxfa_localevalue.cpp
@@ -131,9 +131,7 @@
ScopedLocale scoped_locale(m_pLocaleMgr.Get(), pLocale);
auto pFormat = pdfium::MakeUnique<CFGAS_StringFormatter>(m_pLocaleMgr.Get());
- std::vector<WideString> wsPatterns;
- pFormat->SplitFormatString(wsPattern, &wsPatterns);
-
+ std::vector<WideString> wsPatterns = pFormat->SplitOnBars(wsPattern);
bool bRet = false;
size_t i = 0;
for (; !bRet && i < wsPatterns.size(); i++) {
@@ -262,11 +260,9 @@
const WideString& wsFormat,
LocaleIface* pLocale,
XFA_VALUEPICTURE eValueType) const {
- auto pFormat = pdfium::MakeUnique<CFGAS_StringFormatter>(m_pLocaleMgr.Get());
- std::vector<WideString> wsPatterns;
- pFormat->SplitFormatString(wsFormat, &wsPatterns);
wsResult.clear();
- for (const auto& pattern : wsPatterns) {
+ auto pFormat = pdfium::MakeUnique<CFGAS_StringFormatter>(m_pLocaleMgr.Get());
+ for (const auto& pattern : pFormat->SplitOnBars(wsFormat)) {
if (FormatSinglePattern(wsResult, pattern, pLocale, eValueType))
return true;
}
@@ -547,10 +543,8 @@
return false;
ScopedLocale scoped_locale(m_pLocaleMgr.Get(), pLocale);
-
auto pFormat = pdfium::MakeUnique<CFGAS_StringFormatter>(m_pLocaleMgr.Get());
- std::vector<WideString> wsPatterns;
- pFormat->SplitFormatString(wsPattern, &wsPatterns);
+ std::vector<WideString> wsPatterns = pFormat->SplitOnBars(wsPattern);
bool bRet = false;
for (size_t i = 0; !bRet && i < wsPatterns.size(); i++) {
const WideString& wsFormat = wsPatterns[i];