From 00e2f118d7f4f070ebddf16b2cf4e89cf9d551a7 Mon Sep 17 00:00:00 2001 From: Arkadiy Illarionov Date: Sat, 7 Sep 2019 23:10:33 +0300 Subject: Simplify Sequence iterations in i18npool Use range-based loops, STL and comphelper functions. Change-Id: Ibbc1c14e921585819872f26e8def2a60594e6a63 Reviewed-on: https://gerrit.libreoffice.org/78754 Tested-by: Jenkins Reviewed-by: Arkadiy Illarionov --- .../source/breakiterator/breakiterator_unicode.cxx | 2 +- i18npool/source/calendar/calendarImpl.cxx | 33 +++---- i18npool/source/calendar/calendar_gregorian.cxx | 8 +- i18npool/source/collator/collatorImpl.cxx | 39 ++++---- .../defaultnumberingprovider.cxx | 7 +- i18npool/source/indexentry/indexentrysupplier.cxx | 13 ++- i18npool/source/localedata/LocaleNode.cxx | 11 ++- i18npool/source/localedata/localedata.cxx | 45 +++------ i18npool/source/localedata/saxparser.cxx | 4 +- .../source/numberformatcode/numberformatcode.cxx | 107 ++++++++++----------- i18npool/source/ordinalsuffix/ordinalsuffix.cxx | 10 +- i18npool/source/search/textsearch.cxx | 6 +- .../source/textconversion/textconversion_ko.cxx | 19 +--- .../transliteration/ignoreDiacritics_CTL.cxx | 15 ++- .../ignoreIandEfollowedByYa_ja_JP.cxx | 15 +-- .../transliteration/ignoreIterationMark_ja_JP.cxx | 12 +-- .../ignoreKiKuFollowedBySa_ja_JP.cxx | 15 +-- .../ignoreProlongedSoundMark_ja_JP.cxx | 12 +-- .../source/transliteration/transliterationImpl.cxx | 57 ++++++----- .../transliteration/transliteration_OneToOne.cxx | 9 +- .../transliteration/transliteration_body.cxx | 98 +++++++------------ 21 files changed, 218 insertions(+), 319 deletions(-) diff --git a/i18npool/source/breakiterator/breakiterator_unicode.cxx b/i18npool/source/breakiterator/breakiterator_unicode.cxx index fee0b19dae38..e1675ec6a41d 100644 --- a/i18npool/source/breakiterator/breakiterator_unicode.cxx +++ b/i18npool/source/breakiterator/breakiterator_unicode.cxx @@ -142,7 +142,7 @@ void BreakIterator_Unicode::loadICUBreakIterator(const css::lang::Locale& rLocal icuBI->mpValue.reset(); if (!bInMap && rule) do { - uno::Sequence< OUString > breakRules = LocaleDataImpl::get()->getBreakIteratorRules(rLocale); + const uno::Sequence< OUString > breakRules = LocaleDataImpl::get()->getBreakIteratorRules(rLocale); status = U_ZERO_ERROR; udata_setAppData("OpenOffice", OpenOffice_dat, &status); diff --git a/i18npool/source/calendar/calendarImpl.cxx b/i18npool/source/calendar/calendarImpl.cxx index d2e9801338dd..2cb659560489 100644 --- a/i18npool/source/calendar/calendarImpl.cxx +++ b/i18npool/source/calendar/calendarImpl.cxx @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -42,14 +43,11 @@ CalendarImpl::~CalendarImpl() void SAL_CALL CalendarImpl::loadDefaultCalendarTZ( const css::lang::Locale& rLocale, const OUString& rTimeZone ) { - Sequence< Calendar2 > xC = LocaleDataImpl::get()->getAllCalendars2(rLocale); - for (sal_Int32 i = 0; i < xC.getLength(); i++) { - if (xC[i].Default) { - loadCalendarTZ(xC[i].Name, rLocale, rTimeZone); - return; - } - } - throw ERROR; + const Sequence< Calendar2 > xC = LocaleDataImpl::get()->getAllCalendars2(rLocale); + auto pCal = std::find_if(xC.begin(), xC.end(), [](const Calendar2& rCal) { return rCal.Default; }); + if (pCal == xC.end()) + throw ERROR; + loadCalendarTZ(pCal->Name, rLocale, rTimeZone); } void SAL_CALL @@ -74,13 +72,9 @@ CalendarImpl::loadCalendarTZ( const OUString& uniqueID, const css::lang::Locale& if ( ! xI.is() ) { // check if the calendar is defined in localedata, load gregorian calendar service. - Sequence< Calendar2 > xC = LocaleDataImpl::get()->getAllCalendars2(rLocale); - for (i = 0; i < xC.getLength(); i++) { - if (uniqueID == xC[i].Name) { - xI = m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.i18n.Calendar_gregorian", m_xContext); - break; - } - } + const Sequence< Calendar2 > xC = LocaleDataImpl::get()->getAllCalendars2(rLocale); + if (std::any_of(xC.begin(), xC.end(), [&uniqueID](const Calendar2& rCal) { return uniqueID == rCal.Name; })) + xI = m_xContext->getServiceManager()->createInstanceWithContext("com.sun.star.i18n.Calendar_gregorian", m_xContext); } if ( !xI.is() ) @@ -139,11 +133,10 @@ CalendarImpl::getLoadedCalendar() Sequence< OUString > SAL_CALL CalendarImpl::getAllCalendars( const css::lang::Locale& rLocale ) { - Sequence< Calendar2 > xC = LocaleDataImpl::get()->getAllCalendars2(rLocale); - sal_Int32 nLen = xC.getLength(); - Sequence< OUString > xSeq( nLen ); - for (sal_Int32 i = 0; i < nLen; i++) - xSeq[i] = xC[i].Name; + const Sequence< Calendar2 > xC = LocaleDataImpl::get()->getAllCalendars2(rLocale); + Sequence< OUString > xSeq( xC.getLength() ); + std::transform(xC.begin(), xC.end(), xSeq.begin(), + [](const Calendar2& rCal) { return rCal.Name; }); return xSeq; } diff --git a/i18npool/source/calendar/calendar_gregorian.cxx b/i18npool/source/calendar/calendar_gregorian.cxx index f3b228efc04e..f3dc6ede1f66 100644 --- a/i18npool/source/calendar/calendar_gregorian.cxx +++ b/i18npool/source/calendar/calendar_gregorian.cxx @@ -260,12 +260,12 @@ Calendar_gregorian::loadCalendar( const OUString& uniqueID, const css::lang::Loc getValue(); aLocale = rLocale; - Sequence< Calendar2 > xC = LocaleDataImpl::get()->getAllCalendars2(rLocale); - for (sal_Int32 i = 0; i < xC.getLength(); i++) + const Sequence< Calendar2 > xC = LocaleDataImpl::get()->getAllCalendars2(rLocale); + for (const auto& rCal : xC) { - if (uniqueID == xC[i].Name) + if (uniqueID == rCal.Name) { - aCalendar = xC[i]; + aCalendar = rCal; // setup minimalDaysInFirstWeek setMinimumNumberOfDaysForFirstWeek( aCalendar.MinimumNumberOfDaysForFirstWeek); diff --git a/i18npool/source/collator/collatorImpl.cxx b/i18npool/source/collator/collatorImpl.cxx index 155b271b1b46..450e57ad6038 100644 --- a/i18npool/source/collator/collatorImpl.cxx +++ b/i18npool/source/collator/collatorImpl.cxx @@ -21,7 +21,9 @@ #include #include #include +#include #include +#include using namespace com::sun::star; using namespace com::sun::star::i18n; @@ -69,9 +71,10 @@ sal_Int32 SAL_CALL CollatorImpl::loadDefaultCollator(const lang::Locale& rLocale, sal_Int32 collatorOptions) { const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale); - for (sal_Int32 i = 0; i < imp.getLength(); i++) - if (imp[i].isDefault) - return loadCollatorAlgorithm(imp[i].unoID, rLocale, collatorOptions); + auto pImpl = std::find_if(imp.begin(), imp.end(), + [](const Implementation& rImp) { return rImp.isDefault; }); + if (pImpl != imp.end()) + return loadCollatorAlgorithm(pImpl->unoID, rLocale, collatorOptions); throw RuntimeException(); // not default is defined //return 0; @@ -95,9 +98,8 @@ void SAL_CALL CollatorImpl::loadCollatorAlgorithmWithEndUserOption(const OUString& impl, const lang::Locale& rLocale, const Sequence< sal_Int32 >& collatorOptions) { - sal_Int32 options = 0; - for (sal_Int32 i = 0; i < collatorOptions.getLength(); i++) - options |= collatorOptions[i]; + sal_Int32 options = std::accumulate(collatorOptions.begin(), collatorOptions.end(), + sal_Int32(0), [](sal_Int32 nSum, sal_Int32 nOpt) { return nSum | nOpt; }); loadCollatorAlgorithm(impl, rLocale, options); } @@ -107,15 +109,15 @@ CollatorImpl::listCollatorAlgorithms( const lang::Locale& rLocale ) nLocale = rLocale; const Sequence< Implementation > &imp = mxLocaleData->getCollatorImplementations(rLocale); Sequence< OUString > list(imp.getLength()); + auto pBegin = list.begin(); + auto pId = pBegin; - for (sal_Int32 i = 0; i < imp.getLength(); i++) { + for (const auto& rImpl : imp) { + *pId = rImpl.unoID; //if the current algorithm is default and the position is not on the first one, then switch - if (imp[i].isDefault && i) { - list[i] = list[0]; - list[0] = imp[i].unoID; - } - else - list[i] = imp[i].unoID; + if (rImpl.isDefault && pId != pBegin) + std::swap(*pBegin, *pId); + ++pId; } return list; } @@ -123,14 +125,13 @@ CollatorImpl::listCollatorAlgorithms( const lang::Locale& rLocale ) Sequence< sal_Int32 > SAL_CALL CollatorImpl::listCollatorOptions( const OUString& /*collatorAlgorithmName*/ ) { - Sequence< OUString > option_str = mxLocaleData->getCollationOptions(nLocale); + const Sequence< OUString > option_str = mxLocaleData->getCollationOptions(nLocale); Sequence< sal_Int32 > option_int(option_str.getLength()); - for (sal_Int32 i = 0; i < option_str.getLength(); i++) - option_int[i] = - option_str[i] == "IGNORE_CASE" ? CollatorOptions::CollatorOptions_IGNORE_CASE : - option_str[i] == "IGNORE_KANA" ? CollatorOptions::CollatorOptions_IGNORE_KANA : - option_str[i] == "IGNORE_WIDTH" ? CollatorOptions::CollatorOptions_IGNORE_WIDTH : 0; + std::transform(option_str.begin(), option_str.end(), option_int.begin(), [](const OUString& rOpt) { + return rOpt == "IGNORE_CASE" ? CollatorOptions::CollatorOptions_IGNORE_CASE : + rOpt == "IGNORE_KANA" ? CollatorOptions::CollatorOptions_IGNORE_KANA : + rOpt == "IGNORE_WIDTH" ? CollatorOptions::CollatorOptions_IGNORE_WIDTH : 0; }); return option_int; } diff --git a/i18npool/source/defaultnumberingprovider/defaultnumberingprovider.cxx b/i18npool/source/defaultnumberingprovider/defaultnumberingprovider.cxx index 4a44dd841791..a81b00143989 100644 --- a/i18npool/source/defaultnumberingprovider/defaultnumberingprovider.cxx +++ b/i18npool/source/defaultnumberingprovider/defaultnumberingprovider.cxx @@ -556,9 +556,10 @@ static Any getPropertyByName( const Sequence& aProperties, const char* name, bool bRequired ) { - for( int i=0; iValue; if(bRequired) throw IllegalArgumentException(); return Any(); diff --git a/i18npool/source/indexentry/indexentrysupplier.cxx b/i18npool/source/indexentry/indexentrysupplier.cxx index 44be37a66fe0..f98856ee2227 100644 --- a/i18npool/source/indexentry/indexentrysupplier.cxx +++ b/i18npool/source/indexentry/indexentrysupplier.cxx @@ -46,13 +46,12 @@ Sequence < OUString > SAL_CALL IndexEntrySupplier::getAlgorithmList( const Local sal_Bool SAL_CALL IndexEntrySupplier::loadAlgorithm( const Locale& rLocale, const OUString& SortAlgorithm, sal_Int32 collatorOptions ) { - Sequence < OUString > algorithmList = getAlgorithmList( rLocale ); - for (sal_Int32 i = 0; i < algorithmList.getLength(); i++) { - if (algorithmList[i] == SortAlgorithm) { - if (getLocaleSpecificIndexEntrySupplier(rLocale, SortAlgorithm).is()) - return xIES->loadAlgorithm(rLocale, SortAlgorithm, collatorOptions); - } - } + const Sequence < OUString > algorithmList = getAlgorithmList( rLocale ); + if (std::any_of(algorithmList.begin(), algorithmList.end(), + [this, &SortAlgorithm, &rLocale](const OUString& rAlgorithm) { + return rAlgorithm == SortAlgorithm + && getLocaleSpecificIndexEntrySupplier(rLocale, SortAlgorithm).is(); })) + return xIES->loadAlgorithm(rLocale, SortAlgorithm, collatorOptions); return false; } diff --git a/i18npool/source/localedata/LocaleNode.cxx b/i18npool/source/localedata/LocaleNode.cxx index f9436d5ddf9a..273d40698a8f 100644 --- a/i18npool/source/localedata/LocaleNode.cxx +++ b/i18npool/source/localedata/LocaleNode.cxx @@ -2301,10 +2301,13 @@ Attr::Attr (const Reference< XAttributeList > & attr) { } OUString Attr::getValueByName (const sal_Char *str) const { - sal_Int32 len = name.getLength(); - for (sal_Int32 i = 0;i(std::distance(std::cbegin(name), pName)); + return value[i]; + } return OUString(); } diff --git a/i18npool/source/localedata/localedata.cxx b/i18npool/source/localedata/localedata.cxx index e379b403248c..0679407cc81b 100644 --- a/i18npool/source/localedata/localedata.cxx +++ b/i18npool/source/localedata/localedata.cxx @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -361,11 +362,7 @@ namespace i18npool { // static Sequence< CalendarItem > LocaleDataImpl::downcastCalendarItems( const Sequence< CalendarItem2 > & rCi ) { - Sequence< CalendarItem > aCi(rCi.getLength()); - CalendarItem* p1 = aCi.getArray(); - for (const CalendarItem2& r2 : rCi) - *p1++ = r2; - return aCi; + return comphelper::containerToSequence(rCi); } @@ -664,19 +661,16 @@ Sequence< CalendarItem2 > &LocaleDataImpl::getCalendarItemByName(const OUString& } else { cals = getAllCalendars2(loc); } - sal_Int32 index; - for (index = 0; index < cals.getLength(); index++) { - if (id == cals[index].Name) { - ref_cal = cals[index]; - break; - } - } - // Referred locale not found, return name for en_US locale. - if (index == cals.getLength()) { + auto pCal = std::find_if(std::cbegin(cals), std::cend(cals), + [&id](const Calendar2& rCal) { return id == rCal.Name; }); + if (pCal != std::cend(cals)) + ref_cal = *pCal; + else { + // Referred locale not found, return name for en_US locale. cals = getAllCalendars2( Locale("en", "US", OUString()) ); if (!cals.hasElements()) throw RuntimeException(); - ref_cal = cals[0]; + ref_cal = cals.getConstArray()[0]; } ref_name = name; } @@ -794,13 +788,11 @@ Sequence< Calendar > SAL_CALL LocaleDataImpl::getAllCalendars( const Locale& rLocale ) { const Sequence< Calendar2 > aCal2( getAllCalendars2( rLocale)); - Sequence< Calendar > aCal1( aCal2.getLength()); - Calendar* p1 = aCal1.getArray(); - for (const Calendar2& r2 : aCal2) - { - *p1++ = downcastCalendar( r2); - } - return aCal1; + std::vector aCal1; + aCal1.reserve(aCal2.getLength()); + std::transform(aCal2.begin(), aCal2.end(), std::back_inserter(aCal1), + [](const Calendar2& rCal2) { return downcastCalendar(rCal2); }); + return comphelper::containerToSequence(aCal1); } @@ -839,14 +831,7 @@ LocaleDataImpl::getAllCurrencies2( const Locale& rLocale ) Sequence< Currency > SAL_CALL LocaleDataImpl::getAllCurrencies( const Locale& rLocale ) { - const Sequence< Currency2 > aCur2( getAllCurrencies2( rLocale)); - Sequence< Currency > aCur1( aCur2.getLength()); - Currency* p1 = aCur1.getArray(); - for (const Currency2& r2 : aCur2) - { - *p1 = r2; - } - return aCur1; + return comphelper::containerToSequence(getAllCurrencies2(rLocale)); } diff --git a/i18npool/source/localedata/saxparser.cxx b/i18npool/source/localedata/saxparser.cxx index 85b4570a6684..ace747d8286f 100644 --- a/i18npool/source/localedata/saxparser.cxx +++ b/i18npool/source/localedata/saxparser.cxx @@ -61,9 +61,7 @@ public: public: virtual sal_Int32 SAL_CALL readBytes( Sequence< sal_Int8 >& aData, sal_Int32 nBytesToRead ) override { - nBytesToRead = (nBytesToRead > m_seq.getLength() - nPos ) ? - m_seq.getLength() - nPos : - nBytesToRead; + nBytesToRead = std::min(nBytesToRead, m_seq.getLength() - nPos); aData = Sequence< sal_Int8 > ( &(m_seq.getConstArray()[nPos]) , nBytesToRead ); nPos += nBytesToRead; return nBytesToRead; diff --git a/i18npool/source/numberformatcode/numberformatcode.cxx b/i18npool/source/numberformatcode/numberformatcode.cxx index 17e71719310b..c98a8b36c01e 100644 --- a/i18npool/source/numberformatcode/numberformatcode.cxx +++ b/i18npool/source/numberformatcode/numberformatcode.cxx @@ -21,6 +21,7 @@ #include #include #include +#include #include NumberFormatCodeMapper::NumberFormatCodeMapper( @@ -45,19 +46,19 @@ NumberFormatCodeMapper::getDefault( sal_Int16 formatType, sal_Int16 formatUsage, osl::MutexGuard g(maMutex); const css::uno::Sequence< css::i18n::FormatElement > &aFormatSeq = getFormats( rLocale ); - for (sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) { - if (aFormatSeq[i].isDefault && aFormatSeq[i].formatType == elementType && - aFormatSeq[i].formatUsage == elementUsage) { - css::i18n::NumberFormatCode anumberFormatCode(formatType, - formatUsage, - aFormatSeq[i].formatCode, - aFormatSeq[i].formatName, - aFormatSeq[i].formatKey, - aFormatSeq[i].formatIndex, - true); - return anumberFormatCode; - } - } + auto pFormat = std::find_if(aFormatSeq.begin(), aFormatSeq.end(), + [&elementType, &elementUsage](const css::i18n::FormatElement& rFormat) { + return rFormat.isDefault + && rFormat.formatType == elementType + && rFormat.formatUsage == elementUsage; }); + if (pFormat != aFormatSeq.end()) + return css::i18n::NumberFormatCode(formatType, + formatUsage, + pFormat->formatCode, + pFormat->formatName, + pFormat->formatKey, + pFormat->formatIndex, + true); css::i18n::NumberFormatCode defaultNumberFormatCode; return defaultNumberFormatCode; } @@ -69,18 +70,16 @@ NumberFormatCodeMapper::getFormatCode( sal_Int16 formatIndex, const css::lang::L osl::MutexGuard g(maMutex); const css::uno::Sequence< css::i18n::FormatElement > &aFormatSeq = getFormats( rLocale ); - for (sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) { - if (aFormatSeq[i].formatIndex == formatIndex) { - css::i18n::NumberFormatCode anumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType), - mapElementUsageStringToShort(aFormatSeq[i].formatUsage), - aFormatSeq[i].formatCode, - aFormatSeq[i].formatName, - aFormatSeq[i].formatKey, - aFormatSeq[i].formatIndex, - aFormatSeq[i].isDefault); - return anumberFormatCode; - } - } + auto pFormat = std::find_if(aFormatSeq.begin(), aFormatSeq.end(), + [formatIndex](const css::i18n::FormatElement& rFormat) { return rFormat.formatIndex == formatIndex; }); + if (pFormat != aFormatSeq.end()) + return css::i18n::NumberFormatCode(mapElementTypeStringToShort(pFormat->formatType), + mapElementUsageStringToShort(pFormat->formatUsage), + pFormat->formatCode, + pFormat->formatName, + pFormat->formatKey, + pFormat->formatIndex, + pFormat->isDefault); css::i18n::NumberFormatCode defaultNumberFormatCode; return defaultNumberFormatCode; } @@ -92,30 +91,22 @@ NumberFormatCodeMapper::getAllFormatCode( sal_Int16 formatUsage, const css::lang osl::MutexGuard g(maMutex); const css::uno::Sequence< css::i18n::FormatElement > &aFormatSeq = getFormats( rLocale ); - sal_Int32 i, count; - count = 0; - for (i = 0; i < aFormatSeq.getLength(); i++) { - sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage); - if ( elementUsage == formatUsage ) - count++; - } + std::vector aVec; + aVec.reserve(aFormatSeq.getLength()); - css::uno::Sequence seq(count); - sal_Int32 j = 0; - for (i = 0; i < aFormatSeq.getLength(); i++) { - sal_Int16 elementUsage = mapElementUsageStringToShort(aFormatSeq[i].formatUsage); + for (const auto& rFormat : aFormatSeq) { + sal_Int16 elementUsage = mapElementUsageStringToShort(rFormat.formatUsage); if ( elementUsage == formatUsage ) { - seq[j] = css::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType), - formatUsage, - aFormatSeq[i].formatCode, - aFormatSeq[i].formatName, - aFormatSeq[i].formatKey, - aFormatSeq[i].formatIndex, - aFormatSeq[i].isDefault); - j++; + aVec.emplace_back(mapElementTypeStringToShort(rFormat.formatType), + formatUsage, + rFormat.formatCode, + rFormat.formatName, + rFormat.formatKey, + rFormat.formatIndex, + rFormat.isDefault); } } - return seq; + return comphelper::containerToSequence(aVec); } @@ -125,18 +116,20 @@ NumberFormatCodeMapper::getAllFormatCodes( const css::lang::Locale& rLocale ) osl::MutexGuard g(maMutex); const css::uno::Sequence< css::i18n::FormatElement > &aFormatSeq = getFormats( rLocale ); - css::uno::Sequence seq(aFormatSeq.getLength()); - for (sal_Int32 i = 0; i < aFormatSeq.getLength(); i++) - { - seq[i] = css::i18n::NumberFormatCode(mapElementTypeStringToShort(aFormatSeq[i].formatType), - mapElementUsageStringToShort(aFormatSeq[i].formatUsage), - aFormatSeq[i].formatCode, - aFormatSeq[i].formatName, - aFormatSeq[i].formatKey, - aFormatSeq[i].formatIndex, - aFormatSeq[i].isDefault); - } - return seq; + std::vector aVec; + aVec.reserve(aFormatSeq.getLength()); + + std::transform(aFormatSeq.begin(), aFormatSeq.end(), std::back_inserter(aVec), + [](const css::i18n::FormatElement& rFormat) -> css::i18n::NumberFormatCode { + return { mapElementTypeStringToShort(rFormat.formatType), + mapElementUsageStringToShort(rFormat.formatUsage), + rFormat.formatCode, + rFormat.formatName, + rFormat.formatKey, + rFormat.formatIndex, + rFormat.isDefault }; + }); + return comphelper::containerToSequence(aVec); } diff --git a/i18npool/source/ordinalsuffix/ordinalsuffix.cxx b/i18npool/source/ordinalsuffix/ordinalsuffix.cxx index 8dca75c556e1..09b1a91fd2c3 100644 --- a/i18npool/source/ordinalsuffix/ordinalsuffix.cxx +++ b/i18npool/source/ordinalsuffix/ordinalsuffix.cxx @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -95,6 +96,8 @@ uno::Sequence< OUString > SAL_CALL OrdinalSuffixService::getOrdinalSuffix( sal_I return retValue; int32_t nRuleSets = formatter.getNumberOfRuleSetNames( ); + std::vector retVec; + retVec.reserve(nRuleSets); for (int32_t i = 0; i < nRuleSets; ++i) { icu::UnicodeString ruleSet = formatter.getRuleSetName(i); @@ -125,13 +128,10 @@ uno::Sequence< OUString > SAL_CALL OrdinalSuffixService::getOrdinalSuffix( sal_I // Remove the number to get the prefix sal_Int32 len = sValueWithNoOrdinal.getLength(); - - sal_Int32 newLength = retValue.getLength() + 1; - retValue.realloc( newLength ); - retValue[ newLength - 1 ] = sValueWithOrdinal.copy( len ); + retVec.push_back(sValueWithOrdinal.copy(len)); } - return retValue; + return comphelper::containerToSequence(retVec); } const sal_Char cOrdinalSuffix[] = "com.sun.star.i18n.OrdinalSuffix"; diff --git a/i18npool/source/search/textsearch.cxx b/i18npool/source/search/textsearch.cxx index ea369329542e..4174c6cd1e86 100644 --- a/i18npool/source/search/textsearch.cxx +++ b/i18npool/source/search/textsearch.cxx @@ -285,9 +285,9 @@ void TextSearch::setOptions( const SearchOptions& rOptions ) static sal_Int32 FindPosInSeq_Impl( const Sequence & rOff, sal_Int32 nPos ) { - sal_Int32 nRet = 0, nEnd = rOff.getLength(); - while( nRet < nEnd && nPos > rOff[ nRet ] ) ++nRet; - return nRet; + auto pOff = std::find_if(rOff.begin(), rOff.end(), + [nPos](const sal_Int32 nOff) { return nOff >= nPos; }); + return static_cast(std::distance(rOff.begin(), pOff)); } bool TextSearch::isCellStart(const OUString& searchStr, sal_Int32 nPos) diff --git a/i18npool/source/textconversion/textconversion_ko.cxx b/i18npool/source/textconversion/textconversion_ko.cxx index 9d88965cd89a..cfcb08d6deb6 100644 --- a/i18npool/source/textconversion/textconversion_ko.cxx +++ b/i18npool/source/textconversion/textconversion_ko.cxx @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -191,21 +192,9 @@ static Sequence< OUString >& operator += (Sequence< OUString > &rSeq1, Sequence< { if (! rSeq1.hasElements() && rSeq2.hasElements()) rSeq1 = rSeq2; - else if (rSeq2.hasElements()) { - sal_Int32 i, j, k, l; - k = l = rSeq1.getLength(); - rSeq1.realloc(l + rSeq2.getLength()); - - for (i = 0; i < rSeq2.getLength(); i++) { - for (j = 0; j < l; j++) - if (rSeq1[j] == rSeq2[i]) - break; - if (j == l) - rSeq1[k++] = rSeq2[i]; - } - if (rSeq1.getLength() > k) - rSeq1.realloc(k); - } + else if (rSeq2.hasElements()) + rSeq1 = comphelper::combineSequences(rSeq1, rSeq2); + return rSeq1; } diff --git a/i18npool/source/transliteration/ignoreDiacritics_CTL.cxx b/i18npool/source/transliteration/ignoreDiacritics_CTL.cxx index d706f59f5528..ada76a735b80 100644 --- a/i18npool/source/transliteration/ignoreDiacritics_CTL.cxx +++ b/i18npool/source/transliteration/ignoreDiacritics_CTL.cxx @@ -7,6 +7,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include #include #include #include @@ -59,10 +60,11 @@ ignoreDiacritics_CTL::foldingImpl(const OUString& rInStr, sal_Int32 nStartPos, if (useOffset) { OUStringBuffer aOutBuf(nCount); - rOffset.realloc(nCount); + + std::vector aOffset; + aOffset.reserve(nCount); sal_Int32 nPosition = nStartPos; - sal_Int32 nOffset = 0; while (nPosition < nStartPos + nCount) { sal_Int32 nIndex = nPosition; @@ -70,19 +72,14 @@ ignoreDiacritics_CTL::foldingImpl(const OUString& rInStr, sal_Int32 nStartPos, icu::UnicodeString aUStr(nChar); m_transliterator->transliterate(aUStr); - if (nOffset + aUStr.length() > rOffset.getLength()) - rOffset.realloc(rOffset.getLength() + aUStr.length()); - sal_Int32* pOffset = rOffset.getArray(); - aOutBuf.append(reinterpret_cast(aUStr.getBuffer()), aUStr.length()); - for (const sal_Int32 nOffsetEnd = nOffset+aUStr.length(); nOffset < nOffsetEnd; nOffset++) - pOffset[nOffset] = nPosition; + std::fill_n(std::back_inserter(aOffset), aUStr.length(), nPosition); nPosition = nIndex; } - rOffset.realloc(aOutBuf.getLength()); + rOffset = comphelper::containerToSequence(aOffset); return aOutBuf.makeStringAndClear(); } else diff --git a/i18npool/source/transliteration/ignoreIandEfollowedByYa_ja_JP.cxx b/i18npool/source/transliteration/ignoreIandEfollowedByYa_ja_JP.cxx index f466cf9d2693..0be8c094c876 100644 --- a/i18npool/source/transliteration/ignoreIandEfollowedByYa_ja_JP.cxx +++ b/i18npool/source/transliteration/ignoreIandEfollowedByYa_ja_JP.cxx @@ -21,6 +21,8 @@ #include +#include + using namespace com::sun::star::uno; using namespace com::sun::star::lang; @@ -72,13 +74,10 @@ ignoreIandEfollowedByYa_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 sta sal_Unicode * dst = newStr->buffer; const sal_Unicode * src = inStr.getStr() + startPos; - sal_Int32 *p = nullptr; - sal_Int32 position = 0; if (useOffset) { // Allocate nCount length to offset argument. offset.realloc( nCount ); - p = offset.getArray(); - position = startPos; + std::iota(offset.begin(), offset.end(), startPos); } @@ -96,10 +95,6 @@ ignoreIandEfollowedByYa_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 sta if (currentChar == 0x30E3 || // KATAKANA LETTER SMALL YA currentChar == 0x30E4) { // KATAKANA LETTER YA if (aTable[ previousChar ] != previousChar) { - if (useOffset) { - *p ++ = position++; - *p ++ = position++; - } *dst ++ = previousChar; *dst ++ = 0x30A2; // KATAKANA LETTER A previousChar = *src ++; @@ -108,15 +103,11 @@ ignoreIandEfollowedByYa_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 sta } } - if (useOffset) - *p ++ = position++; *dst ++ = previousChar; previousChar = currentChar; } if (nCount == 0) { - if (useOffset) - *p = position; *dst ++ = previousChar; } diff --git a/i18npool/source/transliteration/ignoreIterationMark_ja_JP.cxx b/i18npool/source/transliteration/ignoreIterationMark_ja_JP.cxx index 9395daa8ed5c..66e53845196e 100644 --- a/i18npool/source/transliteration/ignoreIterationMark_ja_JP.cxx +++ b/i18npool/source/transliteration/ignoreIterationMark_ja_JP.cxx @@ -21,6 +21,8 @@ #include +#include + using namespace com::sun::star::uno; using namespace com::sun::star::lang; @@ -90,13 +92,10 @@ ignoreIterationMark_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 startPo sal_Unicode * dst = newStr->buffer; const sal_Unicode * src = inStr.getStr() + startPos; - sal_Int32 * p = nullptr; - sal_Int32 position = 0; if (useOffset) { // Allocate nCount length to offset argument. offset.realloc( nCount ); - p = offset.getArray(); - position = startPos; + std::iota(offset.begin(), offset.end(), startPos); } @@ -118,15 +117,11 @@ ignoreIterationMark_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 startPo currentChar = aTable[ previousChar ]; break; } - if (useOffset) - *p ++ = position ++; *dst ++ = previousChar; previousChar = currentChar; } if (nCount == 0) { - if (useOffset) - *p = position; *dst ++ = previousChar; } @@ -136,7 +131,6 @@ ignoreIterationMark_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 startPo if (useOffset) offset.realloc(newStr->length); return OUString(newStr, SAL_NO_ACQUIRE); // take ownership - } } diff --git a/i18npool/source/transliteration/ignoreKiKuFollowedBySa_ja_JP.cxx b/i18npool/source/transliteration/ignoreKiKuFollowedBySa_ja_JP.cxx index 1c9c9e491eb0..53a2f058d0a9 100644 --- a/i18npool/source/transliteration/ignoreKiKuFollowedBySa_ja_JP.cxx +++ b/i18npool/source/transliteration/ignoreKiKuFollowedBySa_ja_JP.cxx @@ -19,6 +19,8 @@ #include +#include + using namespace com::sun::star::uno; using namespace com::sun::star::lang; @@ -33,13 +35,10 @@ ignoreKiKuFollowedBySa_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 star sal_Unicode * dst = newStr->buffer; const sal_Unicode * src = inStr.getStr() + startPos; - sal_Int32 *p = nullptr; - sal_Int32 position = 0; if (useOffset) { // Allocate nCount length to offset argument. offset.realloc( nCount ); - p = offset.getArray(); - position = startPos; + std::iota(offset.begin(), offset.end(), startPos); } @@ -54,10 +53,6 @@ ignoreKiKuFollowedBySa_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 star if (previousChar == 0x30AF ) { // KATAKANA LETTER KU if (0x30B5 <= currentChar && // KATAKANA LETTER SA currentChar <= 0x30BE) { // KATAKANA LETTER ZO - if (useOffset) { - *p ++ = position++; - *p ++ = position++; - } *dst ++ = 0x30AD; // KATAKANA LETTER KI *dst ++ = currentChar; previousChar = *src ++; @@ -66,15 +61,11 @@ ignoreKiKuFollowedBySa_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 star } } - if (useOffset) - *p ++ = position++; *dst ++ = previousChar; previousChar = currentChar; } if (nCount == 0) { - if (useOffset) - *p = position; *dst ++ = previousChar; } diff --git a/i18npool/source/transliteration/ignoreProlongedSoundMark_ja_JP.cxx b/i18npool/source/transliteration/ignoreProlongedSoundMark_ja_JP.cxx index ca1cb82d407a..91358dc60a3d 100644 --- a/i18npool/source/transliteration/ignoreProlongedSoundMark_ja_JP.cxx +++ b/i18npool/source/transliteration/ignoreProlongedSoundMark_ja_JP.cxx @@ -19,6 +19,8 @@ #include +#include + using namespace com::sun::star::uno; using namespace com::sun::star::lang; @@ -295,14 +297,10 @@ ignoreProlongedSoundMark_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 st sal_Unicode * dst = newStr->buffer; const sal_Unicode * src = inStr.getStr() + startPos; - sal_Int32 *p = nullptr; - sal_Int32 position = 0; - if (useOffset) { // Allocate nCount length to offset argument. offset.realloc( nCount ); - p = offset.getArray(); - position = startPos; + std::iota(offset.begin(), offset.end(), startPos); } @@ -324,15 +322,11 @@ ignoreProlongedSoundMark_ja_JP::foldingImpl( const OUString& inStr, sal_Int32 st } } - if (useOffset) - *p ++ = position ++; *dst ++ = previousChar; previousChar = currentChar; } if (nCount == 0) { - if (useOffset) - *p = position; *dst ++ = previousChar; } diff --git a/i18npool/source/transliteration/transliterationImpl.cxx b/i18npool/source/transliteration/transliterationImpl.cxx index fc51730834b1..195f7a789643 100644 --- a/i18npool/source/transliteration/transliterationImpl.cxx +++ b/i18npool/source/transliteration/transliterationImpl.cxx @@ -25,11 +25,13 @@ #include #include +#include #include #include #include #include +#include using namespace com::sun::star::uno; using namespace com::sun::star::i18n; @@ -256,8 +258,8 @@ TransliterationImpl::loadModulesByImplNames(const Sequence< OUString >& implName throw ERROR; clear(); - for (sal_Int32 i = 0; i < implNameList.getLength(); i++) - if (loadModuleByName(implNameList[i], bodyCascade[numCascade], rLocale)) + for (const auto& rName : implNameList) + if (loadModuleByName(rName, bodyCascade[numCascade], rLocale)) numCascade++; } @@ -266,19 +268,18 @@ Sequence SAL_CALL TransliterationImpl::getAvailableModules( const Locale& rLocale, sal_Int16 sType ) { const Sequence &translist = mxLocaledata->getTransliterations(rLocale); - Sequence r(translist.getLength()); + std::vector r; + r.reserve(translist.getLength()); Reference body; - sal_Int32 n = 0; - for (sal_Int32 i = 0; i < translist.getLength(); i++) + for (const auto& rTrans : translist) { - if (loadModuleByName(translist[i], body, rLocale)) { + if (loadModuleByName(rTrans, body, rLocale)) { if (body->getType() & sType) - r[n++] = translist[i]; + r.push_back(rTrans); body.clear(); } } - r.realloc(n); - return r; + return comphelper::containerToSequence(r); } @@ -310,9 +311,8 @@ TransliterationImpl::transliterate( const OUString& inStr, sal_Int32 startPos, s else { OUString tmpStr = inStr.copy(startPos, nCount); - sal_Int32 * pArr = offset.getArray(); - for (sal_Int32 j = 0; j < nCount; j++) - pArr[j] = startPos + j; + + std::iota(offset.begin(), offset.end(), startPos); sal_Int16 from = 0, to = 1; Sequence off[2]; @@ -370,11 +370,10 @@ TransliterationImpl::folding( const OUString& inStr, sal_Int32 startPos, sal_Int else { OUString tmpStr = inStr.copy(startPos, nCount); - sal_Int32 * pArr = offset.getArray(); - for (sal_Int32 j = 0; j < nCount; j++) - pArr[j] = startPos + j; - sal_Int16 from = 0, to = 1, tmp; + std::iota(offset.begin(), offset.end(), startPos); + + sal_Int16 from = 0, to = 1; Sequence off[2]; off[to] = offset; @@ -383,7 +382,7 @@ TransliterationImpl::folding( const OUString& inStr, sal_Int32 startPos, sal_Int nCount = tmpStr.getLength(); - tmp = from; from = to; to = tmp; + std::swap(from, to); for (sal_Int32 j = 0; j < nCount; j++) off[to][j] = off[from][off[to][j]]; } @@ -476,16 +475,16 @@ TransliterationImpl::equals( for (i = 0; i < nLen; ++i, ++p1, ++p2 ) { if (*p1 != *p2) { // return number of matched code points so far - nMatch1 = (i < offset1.getLength()) ? offset1[i] : i; - nMatch2 = (i < offset2.getLength()) ? offset2[i] : i; + nMatch1 = (i < offset1.getLength()) ? offset1.getConstArray()[i] : i; + nMatch2 = (i < offset2.getLength()) ? offset2.getConstArray()[i] : i; return false; } } // i==nLen if ( tmpStr1.getLength() != tmpStr2.getLength() ) { // return number of matched code points so far - nMatch1 = (i <= offset1.getLength()) ? offset1[i-1] + 1 : i; - nMatch2 = (i <= offset2.getLength()) ? offset2[i-1] + 1 : i; + nMatch1 = (i <= offset1.getLength()) ? offset1.getConstArray()[i-1] + 1 : i; + nMatch2 = (i <= offset2.getLength()) ? offset2.getConstArray()[i-1] + 1 : i; return false; } else { nMatch1 = nCount1; @@ -494,8 +493,6 @@ TransliterationImpl::equals( } } -#define MaxOutput 2 - Sequence< OUString > TransliterationImpl::getRange(const Sequence< OUString > &inStrs, const sal_Int32 length, sal_Int16 _numCascade) @@ -504,18 +501,20 @@ TransliterationImpl::getRange(const Sequence< OUString > &inStrs, return inStrs; sal_Int32 j_tmp = 0; - Sequence< OUString > ostr(MaxOutput*length); + constexpr sal_Int32 nMaxOutput = 2; + const sal_Int32 nMaxOutputLength = nMaxOutput*length; + std::vector ostr; + ostr.reserve(nMaxOutputLength); for (sal_Int32 j = 0; j < length; j+=2) { const Sequence< OUString >& temp = bodyCascade[_numCascade]->transliterateRange(inStrs[j], inStrs[j+1]); - for ( sal_Int32 k = 0; k < temp.getLength(); k++) { - if ( j_tmp >= MaxOutput*length ) throw ERROR; - ostr[j_tmp++] = temp[k]; + for (const auto& rStr : temp) { + if ( j_tmp++ >= nMaxOutputLength ) throw ERROR; + ostr.push_back(rStr); } } - ostr.realloc(j_tmp); - return getRange(ostr, j_tmp, ++_numCascade); + return getRange(comphelper::containerToSequence(ostr), j_tmp, ++_numCascade); } diff --git a/i18npool/source/transliteration/transliteration_OneToOne.cxx b/i18npool/source/transliteration/transliteration_OneToOne.cxx index 484a34db0b0f..f865a4640a80 100644 --- a/i18npool/source/transliteration/transliteration_OneToOne.cxx +++ b/i18npool/source/transliteration/transliteration_OneToOne.cxx @@ -22,6 +22,8 @@ #include #include +#include + using namespace com::sun::star::i18n; using namespace com::sun::star::uno; @@ -64,20 +66,15 @@ transliteration_OneToOne::transliterateImpl( const OUString& inStr, sal_Int32 st const sal_Unicode * src = inStr.getStr() + startPos; // Allocate nCount length to offset argument. - sal_Int32 *p = nullptr; - sal_Int32 position = 0; if (useOffset) { offset.realloc( nCount ); - p = offset.getArray(); - position = startPos; + std::iota(offset.begin(), offset.end(), startPos); } // Translation while (nCount -- > 0) { sal_Unicode c = *src++; *dst ++ = func ? func( c) : (*table)[ c ]; - if (useOffset) - *p ++ = position ++; } *dst = u'\0'; diff --git a/i18npool/source/transliteration/transliteration_body.cxx b/i18npool/source/transliteration/transliteration_body.cxx index 6d6c710b57c2..b168a5e37b3a 100644 --- a/i18npool/source/transliteration/transliteration_body.cxx +++ b/i18npool/source/transliteration/transliteration_body.cxx @@ -23,11 +23,13 @@ #include #include #include +#include #include #include #include +#include using namespace ::com::sun::star::uno; using namespace ::com::sun::star::i18n; @@ -92,80 +94,59 @@ Transliteration_body::transliterateImpl( { const sal_Unicode *in = inStr.getStr() + startPos; - // Two different blocks to eliminate the if(useOffset) condition inside the - // inner k loop. Yes, on massive use even such small things do count. - if ( useOffset ) + // We could assume that most calls result in identical string lengths, + // thus using a preallocated OUStringBuffer could be an easy way + // to assemble the return string without too much hassle. However, + // for single characters the OUStringBuffer::append() method is quite + // expensive compared to a simple array operation, so it pays here + // to copy the final result instead. + + // Allocate the max possible buffer. Try to use stack instead of heap, + // which would have to be reallocated most times anyways. + constexpr sal_Int32 nLocalBuf = 2048; + sal_Unicode aLocalBuf[ nLocalBuf * NMAPPINGMAX ], *out = aLocalBuf; + std::unique_ptr pHeapBuf; + if (nCount > nLocalBuf) { - sal_Int32 nOffCount = 0, i; - for (i = 0; i < nCount; i++) - { - // take care of TOGGLE_CASE transliteration: - MappingType nTmpMappingType = lcl_getMappingTypeForToggleCase( nMappingType, in[i] ); - - const i18nutil::Mapping &map = i18nutil::casefolding::getValue( in, i, nCount, aLocale, nTmpMappingType ); - nOffCount += map.nmap; - } - rtl_uString* pStr = rtl_uString_alloc(nOffCount); - sal_Unicode* out = pStr->buffer; + pHeapBuf.reset(new sal_Unicode[ nCount * NMAPPINGMAX ]); + out = pHeapBuf.get(); + } - if ( nOffCount != offset.getLength() ) - offset.realloc( nOffCount ); + sal_Int32 j = 0; + // Two different blocks to eliminate the if(useOffset) condition inside the loop. + // Yes, on massive use even such small things do count. + if ( useOffset ) + { + std::vector aVec; + aVec.reserve(std::max(nLocalBuf, nCount) * NMAPPINGMAX); - sal_Int32 j = 0; - sal_Int32 * pArr = offset.getArray(); - for (i = 0; i < nCount; i++) + for (sal_Int32 i = 0; i < nCount; i++) { // take care of TOGGLE_CASE transliteration: MappingType nTmpMappingType = lcl_getMappingTypeForToggleCase( nMappingType, in[i] ); const i18nutil::Mapping &map = i18nutil::casefolding::getValue( in, i, nCount, aLocale, nTmpMappingType ); - for (sal_Int32 k = 0; k < map.nmap; k++) - { - pArr[j] = i + startPos; - out[j++] = map.map[k]; - } + std::fill_n(std::back_inserter(aVec), map.nmap, i + startPos); + std::copy_n(map.map, map.nmap, out + j); + j += map.nmap; } - out[j] = 0; - return OUString( pStr, SAL_NO_ACQUIRE ); + offset = comphelper::containerToSequence(aVec); } else { - // In the simple case of no offset sequence used we can eliminate the - // first getValue() loop. We could also assume that most calls result - // in identical string lengths, thus using a preallocated - // OUStringBuffer could be an easy way to assemble the return string - // without too much hassle. However, for single characters the - // OUStringBuffer::append() method is quite expensive compared to a - // simple array operation, so it pays here to copy the final result - // instead. - - // Allocate the max possible buffer. Try to use stack instead of heap, - // which would have to be reallocated most times anyways. - const sal_Int32 nLocalBuf = 2048; - sal_Unicode aLocalBuf[ nLocalBuf * NMAPPINGMAX ], *out = aLocalBuf; - std::unique_ptr pHeapBuf; - if ( nCount > nLocalBuf ) { - pHeapBuf.reset(new sal_Unicode[ nCount * NMAPPINGMAX ]); - out = pHeapBuf.get(); - } - - sal_Int32 j = 0; for ( sal_Int32 i = 0; i < nCount; i++) { // take care of TOGGLE_CASE transliteration: MappingType nTmpMappingType = lcl_getMappingTypeForToggleCase( nMappingType, in[i] ); const i18nutil::Mapping &map = i18nutil::casefolding::getValue( in, i, nCount, aLocale, nTmpMappingType ); - for (sal_Int32 k = 0; k < map.nmap; k++) - { - out[j++] = map.map[k]; - } + std::copy_n(map.map, map.nmap, out + j); + j += map.nmap; } - - OUString aRet( out, j ); - return aRet; } + + return OUString(out, j); } OUString SAL_CALL @@ -279,15 +260,8 @@ static OUString transliterate_titlecase_Impl( aRes += xCharClassImpl->toLower( aText, 1, aText.getLength() - 1, rLocale ); offset.realloc( aRes.getLength() ); - sal_Int32 *pOffset = offset.getArray(); - sal_Int32 nLen = offset.getLength(); - for (sal_Int32 i = 0; i < nLen; ++i) - { - sal_Int32 nIdx = 0; - if (i >= nResolvedLen) - nIdx = i - nResolvedLen + 1; - pOffset[i] = nIdx; - } + sal_Int32* pOffset = std::fill_n(offset.begin(), nResolvedLen, 0); + std::iota(pOffset, offset.end(), 1); } return aRes; } -- cgit v1.2.3