From 760a377f7148e623e9e16d24e66f54a401ecb946 Mon Sep 17 00:00:00 2001 From: Arkadiy Illarionov Date: Thu, 29 Aug 2019 00:51:02 +0300 Subject: Simplify Sequence iterations in lingucomponent..lotuswordpro Use range-based loops, STL and comphelper functions. Change-Id: I975a9c09265976d5ce4a1d7ac2023cbb75bb7f28 Reviewed-on: https://gerrit.libreoffice.org/78242 Reviewed-by: Mike Kaganski Tested-by: Jenkins Reviewed-by: Arkadiy Illarionov --- .../source/hyphenator/hyphen/hyphenimp.cxx | 60 ++++++++------------- .../source/languageguessing/guesslang.cxx | 18 +++---- .../source/spellcheck/spell/sspellimp.cxx | 7 +-- .../source/thesaurus/libnth/nthesimp.cxx | 63 ++++++++-------------- 4 files changed, 54 insertions(+), 94 deletions(-) (limited to 'lingucomponent/source') diff --git a/lingucomponent/source/hyphenator/hyphen/hyphenimp.cxx b/lingucomponent/source/hyphenator/hyphen/hyphenimp.cxx index bd38e3d470d4..2008395319e0 100644 --- a/lingucomponent/source/hyphenator/hyphen/hyphenimp.cxx +++ b/lingucomponent/source/hyphenator/hyphen/hyphenimp.cxx @@ -19,6 +19,7 @@ #include +#include #include #include #include @@ -51,6 +52,7 @@ #include #include +#include #include #include #include @@ -113,11 +115,10 @@ Sequence< Locale > SAL_CALL Hyphenator::getLocales() uno::Sequence< OUString > aFormatList; aLinguCfg.GetSupportedDictionaryFormatsFor( "Hyphenators", "org.openoffice.lingu.LibHnjHyphenator", aFormatList ); - sal_Int32 nLen = aFormatList.getLength(); - for (sal_Int32 i = 0; i < nLen; ++i) + for (const auto& rFormat : std::as_const(aFormatList)) { std::vector< SvtLinguConfigDictionaryEntry > aTmpDic( - aLinguCfg.GetActiveDictionariesByFormat( aFormatList[i] ) ); + aLinguCfg.GetActiveDictionariesByFormat( rFormat ) ); aDics.insert( aDics.end(), aTmpDic.begin(), aTmpDic.end() ); } @@ -132,57 +133,50 @@ Sequence< Locale > SAL_CALL Hyphenator::getLocales() // is not yet supported by the list of new style dictionaries MergeNewStyleDicsAndOldStyleDics( aDics, aOldStyleDics ); - sal_Int32 numdict = aDics.size(); - if (numdict) + if (!aDics.empty()) { // get supported locales from the dictionaries-to-use... - sal_Int32 k = 0; std::set aLocaleNamesSet; for (auto const& dict : aDics) { - uno::Sequence< OUString > aLocaleNames( dict.aLocaleNames ); - sal_Int32 nLen2 = aLocaleNames.getLength(); - for (k = 0; k < nLen2; ++k) + for (const auto& rLocaleName : dict.aLocaleNames) { - aLocaleNamesSet.insert( aLocaleNames[k] ); + aLocaleNamesSet.insert( rLocaleName ); } } // ... and add them to the resulting sequence - aSuppLocales.realloc( aLocaleNamesSet.size() ); - k = 0; - for (auto const& localeName : aLocaleNamesSet) - { - Locale aTmp( LanguageTag::convertToLocale(localeName)); - aSuppLocales[k++] = aTmp; - } + std::vector aLocalesVec; + aLocalesVec.reserve(aLocaleNamesSet.size()); + + std::transform(aLocaleNamesSet.begin(), aLocaleNamesSet.end(), std::back_inserter(aLocalesVec), + [](const OUString& localeName) { return LanguageTag::convertToLocale(localeName); }); + + aSuppLocales = comphelper::containerToSequence(aLocalesVec); //! For each dictionary and each locale we need a separate entry. //! If this results in more than one dictionary per locale than (for now) //! it is undefined which dictionary gets used. //! In the future the implementation should support using several dictionaries //! for one locale. - numdict = 0; - for (auto const& dict : aDics) - numdict = numdict + dict.aLocaleNames.getLength(); + sal_Int32 numdict = std::accumulate(aDics.begin(), aDics.end(), 0, + [](const sal_Int32 nSum, const SvtLinguConfigDictionaryEntry& dict) { + return nSum + dict.aLocaleNames.getLength(); }); // add dictionary information mvDicts.resize(numdict); - k = 0; + sal_Int32 k = 0; for (auto const& dict : aDics) { if (dict.aLocaleNames.hasElements() && dict.aLocations.hasElements()) { - uno::Sequence< OUString > aLocaleNames(dict.aLocaleNames); - sal_Int32 nLocales = aLocaleNames.getLength(); - // currently only one language per dictionary is supported in the actual implementation... // Thus here we work-around this by adding the same dictionary several times. // Once for each of its supported locales. - for (sal_Int32 i = 0; i < nLocales; ++i) + for (const auto& rLocaleName : dict.aLocaleNames) { - LanguageTag aLanguageTag(dict.aLocaleNames[i]); + LanguageTag aLanguageTag(rLocaleName); mvDicts[k].aPtr = nullptr; mvDicts[k].eEnc = RTL_TEXTENCODING_DONTKNOW; mvDicts[k].aLoc = aLanguageTag.getLocale(); @@ -204,7 +198,6 @@ Sequence< Locale > SAL_CALL Hyphenator::getLocales() else { // no dictionary found so register no dictionaries - numdict = 0; mvDicts.clear(); aSuppLocales.realloc(0); } @@ -217,21 +210,10 @@ sal_Bool SAL_CALL Hyphenator::hasLocale(const Locale& rLocale) { MutexGuard aGuard( GetLinguMutex() ); - bool bRes = false; if (!aSuppLocales.hasElements()) getLocales(); - const Locale *pLocale = aSuppLocales.getConstArray(); - sal_Int32 nLen = aSuppLocales.getLength(); - for (sal_Int32 i = 0; i < nLen; ++i) - { - if (rLocale == pLocale[i]) - { - bRes = true; - break; - } - } - return bRes; + return comphelper::findValue(aSuppLocales, rLocale) != -1; } namespace { diff --git a/lingucomponent/source/languageguessing/guesslang.cxx b/lingucomponent/source/languageguessing/guesslang.cxx index 5295a4e2902c..e25d51a413fa 100644 --- a/lingucomponent/source/languageguessing/guesslang.cxx +++ b/lingucomponent/source/languageguessing/guesslang.cxx @@ -270,15 +270,12 @@ void SAL_CALL LangGuess_Impl::disableLanguages( EnsureInitialized(); - sal_Int32 nLanguages = rLanguages.getLength(); - const Locale *pLanguages = rLanguages.getConstArray(); - - for (sal_Int32 i = 0; i < nLanguages; ++i) + for (const Locale& rLanguage : rLanguages) { string language; - OString l = OUStringToOString( pLanguages[i].Language, RTL_TEXTENCODING_ASCII_US ); - OString c = OUStringToOString( pLanguages[i].Country, RTL_TEXTENCODING_ASCII_US ); + OString l = OUStringToOString( rLanguage.Language, RTL_TEXTENCODING_ASCII_US ); + OString c = OUStringToOString( rLanguage.Country, RTL_TEXTENCODING_ASCII_US ); language += l.getStr(); language += "-"; @@ -294,15 +291,12 @@ void SAL_CALL LangGuess_Impl::enableLanguages( EnsureInitialized(); - sal_Int32 nLanguages = rLanguages.getLength(); - const Locale *pLanguages = rLanguages.getConstArray(); - - for (sal_Int32 i = 0; i < nLanguages; ++i) + for (const Locale& rLanguage : rLanguages) { string language; - OString l = OUStringToOString( pLanguages[i].Language, RTL_TEXTENCODING_ASCII_US ); - OString c = OUStringToOString( pLanguages[i].Country, RTL_TEXTENCODING_ASCII_US ); + OString l = OUStringToOString( rLanguage.Language, RTL_TEXTENCODING_ASCII_US ); + OString c = OUStringToOString( rLanguage.Country, RTL_TEXTENCODING_ASCII_US ); language += l.getStr(); language += "-"; diff --git a/lingucomponent/source/spellcheck/spell/sspellimp.cxx b/lingucomponent/source/spellcheck/spell/sspellimp.cxx index 1e88d04874c1..e6901af11577 100644 --- a/lingucomponent/source/spellcheck/spell/sspellimp.cxx +++ b/lingucomponent/source/spellcheck/spell/sspellimp.cxx @@ -50,6 +50,7 @@ #include #include +#include #include #include #include @@ -189,9 +190,9 @@ Sequence< Locale > SAL_CALL SpellChecker::getLocales() //! it is undefined which dictionary gets used. //! In the future the implementation should support using several dictionaries //! for one locale. - sal_uInt32 nDictSize = 0; - for (auto const& dict : aDics) - nDictSize += dict.aLocaleNames.getLength(); + sal_uInt32 nDictSize = std::accumulate(aDics.begin(), aDics.end(), sal_uInt32(0), + [](const sal_uInt32 nSum, const SvtLinguConfigDictionaryEntry& dict) { + return nSum + dict.aLocaleNames.getLength(); }); // add dictionary information m_DictItems.reserve(nDictSize); diff --git a/lingucomponent/source/thesaurus/libnth/nthesimp.cxx b/lingucomponent/source/thesaurus/libnth/nthesimp.cxx index 396f39e9f05f..260c51f83981 100644 --- a/lingucomponent/source/thesaurus/libnth/nthesimp.cxx +++ b/lingucomponent/source/thesaurus/libnth/nthesimp.cxx @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -48,6 +49,7 @@ #include "nthesdta.hxx" #include +#include #include #include @@ -113,11 +115,10 @@ Sequence< Locale > SAL_CALL Thesaurus::getLocales() uno::Sequence< OUString > aFormatList; aLinguCfg.GetSupportedDictionaryFormatsFor( "Thesauri", "org.openoffice.lingu.new.Thesaurus", aFormatList ); - sal_Int32 nLen = aFormatList.getLength(); - for (sal_Int32 i = 0; i < nLen; ++i) + for (const auto& rFormat : std::as_const(aFormatList)) { std::vector< SvtLinguConfigDictionaryEntry > aTmpDic( - aLinguCfg.GetActiveDictionariesByFormat( aFormatList[i] ) ); + aLinguCfg.GetActiveDictionariesByFormat( rFormat ) ); aDics.insert( aDics.end(), aTmpDic.begin(), aTmpDic.end() ); } @@ -132,61 +133,53 @@ Sequence< Locale > SAL_CALL Thesaurus::getLocales() // is not yet supported by the list of new style dictionaries MergeNewStyleDicsAndOldStyleDics( aDics, aOldStyleDics ); - sal_Int32 numthes = aDics.size(); - if (numthes) + if (!aDics.empty()) { // get supported locales from the dictionaries-to-use... - sal_Int32 k = 0; std::set aLocaleNamesSet; for (auto const& dict : aDics) { - uno::Sequence< OUString > aLocaleNames(dict.aLocaleNames); - sal_Int32 nLen2 = aLocaleNames.getLength(); - for (k = 0; k < nLen2; ++k) + for (const auto& rLocaleName : dict.aLocaleNames) { - if (!comphelper::LibreOfficeKit::isWhitelistedLanguage(aLocaleNames[k])) + if (!comphelper::LibreOfficeKit::isWhitelistedLanguage(rLocaleName)) continue; - aLocaleNamesSet.insert( aLocaleNames[k] ); + aLocaleNamesSet.insert( rLocaleName ); } } // ... and add them to the resulting sequence - aSuppLocales.realloc( aLocaleNamesSet.size() ); - std::set::const_iterator aItB; - k = 0; - for (auto const& localeName : aLocaleNamesSet) - { - Locale aTmp( LanguageTag::convertToLocale(localeName)); - aSuppLocales[k++] = aTmp; - } + std::vector aLocalesVec; + aLocalesVec.reserve(aLocaleNamesSet.size()); + + std::transform(aLocaleNamesSet.begin(), aLocaleNamesSet.end(), std::back_inserter(aLocalesVec), + [](const OUString& localeName) -> Locale { return LanguageTag::convertToLocale(localeName); }); + + aSuppLocales = comphelper::containerToSequence(aLocalesVec); //! For each dictionary and each locale we need a separate entry. //! If this results in more than one dictionary per locale than (for now) //! it is undefined which dictionary gets used. //! In the future the implementation should support using several dictionaries //! for one locale. - numthes = 0; - for (auto const& dict : aDics) - numthes = numthes + dict.aLocaleNames.getLength(); + sal_Int32 numthes = std::accumulate(aDics.begin(), aDics.end(), 0, + [](const sal_Int32 nSum, const SvtLinguConfigDictionaryEntry& dict) { + return nSum + dict.aLocaleNames.getLength(); }); // add dictionary information mvThesInfo.resize(numthes); - k = 0; + sal_Int32 k = 0; for (auto const& dict : aDics) { if (dict.aLocaleNames.hasElements() && dict.aLocations.hasElements()) { - uno::Sequence< OUString > aLocaleNames(dict.aLocaleNames); - sal_Int32 nLocales = aLocaleNames.getLength(); - // currently only one language per dictionary is supported in the actual implementation... // Thus here we work-around this by adding the same dictionary several times. // Once for each of its supported locales. - for (sal_Int32 i = 0; i < nLocales; ++i) + for (const auto& rLocaleName : dict.aLocaleNames) { - LanguageTag aLanguageTag(dict.aLocaleNames[i]); + LanguageTag aLanguageTag(rLocaleName); mvThesInfo[k].aEncoding = RTL_TEXTENCODING_DONTKNOW; mvThesInfo[k].aLocale = aLanguageTag.getLocale(); mvThesInfo[k].aCharSetInfo.reset( new CharClass( aLanguageTag ) ); @@ -219,20 +212,10 @@ sal_Bool SAL_CALL Thesaurus::hasLocale(const Locale& rLocale) { MutexGuard aGuard( GetLinguMutex() ); - bool bRes = false; if (!aSuppLocales.hasElements()) getLocales(); - sal_Int32 nLen = aSuppLocales.getLength(); - for (sal_Int32 i = 0; i < nLen; ++i) - { - const Locale *pLocale = aSuppLocales.getConstArray(); - if (rLocale == pLocale[i]) - { - bRes = true; - break; - } - } - return bRes; + + return comphelper::findValue(aSuppLocales, rLocale) != -1; } Sequence < Reference < css::linguistic2::XMeaning > > SAL_CALL Thesaurus::queryMeanings( -- cgit