lingucomponent/source/hyphenator/hyphen/hyphenimp.cxx |  109 ++++++------------
 lingucomponent/source/hyphenator/hyphen/hyphenimp.hxx |    1 
 2 files changed, 42 insertions(+), 68 deletions(-)

New commits:
commit ec775c8ada237b7a861074e51caf36a79d0e5fcc
Author:     Mike Kaganski <mike.kagan...@collabora.com>
AuthorDate: Sun Nov 24 12:47:25 2024 +0500
Commit:     Mike Kaganski <mike.kagan...@collabora.com>
CommitDate: Sun Nov 24 14:44:50 2024 +0100

    Deduplicate a bit
    
    Change-Id: I9a19dcc2fbf5297fe391fcd3644843eb51afdb9f
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/177200
    Tested-by: Jenkins
    Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com>

diff --git a/lingucomponent/source/hyphenator/hyphen/hyphenimp.cxx 
b/lingucomponent/source/hyphenator/hyphen/hyphenimp.cxx
index 5ec43190a731..a9d6d779bacb 100644
--- a/lingucomponent/source/hyphenator/hyphen/hyphenimp.cxx
+++ b/lingucomponent/source/hyphenator/hyphen/hyphenimp.cxx
@@ -108,10 +108,9 @@ PropertyHelper_Hyphenation& 
Hyphenator::GetPropHelper_Impl()
     return *pPropHelper;
 }
 
+// Requires GetLinguMutex locked
 void Hyphenator::ensureLocales()
 {
-    MutexGuard  aGuard( GetLinguMutex() );
-
     // this routine should return the locales supported by the installed
     // dictionaries.
     if (mvDicts.empty())
@@ -256,6 +255,34 @@ bool LoadDictionary(HDInfo& rDict)
 }
 }
 
+const HDInfo* Hyphenator::getMatchingDict(const css::lang::Locale& aLocale)
+{
+    MutexGuard aGuard(GetLinguMutex());
+    ensureLocales();
+    auto it = std::find_if(mvDicts.rbegin(), mvDicts.rend(),
+                           [&aLocale](auto& el) { return el.aLoc == aLocale; 
});
+    if (it == mvDicts.rend())
+        return nullptr;
+
+    // if this dictionary has not been loaded yet do that
+    if (!it->aPtr)
+    {
+        if (!LoadDictionary(*it))
+            return nullptr;
+    }
+
+    // we don't want to work with a default text encoding since following 
incorrect
+    // results may occur only for specific text and thus may be hard to notice.
+    // Thus better always make a clean exit here if the text encoding is in 
question.
+    // Hopefully something not working at all will raise proper attention 
quickly. ;-)
+    DBG_ASSERT(it->eEnc != RTL_TEXTENCODING_DONTKNOW,
+               "failed to get text encoding! (maybe incorrect encoding string 
in file)");
+    if (it->eEnc == RTL_TEXTENCODING_DONTKNOW)
+        return nullptr;
+
+    return &*it;
+}
+
 Reference< XHyphenatedWord > SAL_CALL Hyphenator::hyphenate( const OUString& 
aWord,
        const css::lang::Locale& aLocale,
        sal_Int16 nMaxLeading,
@@ -269,36 +296,17 @@ Reference< XHyphenatedWord > SAL_CALL 
Hyphenator::hyphenate( const OUString& aWo
     sal_Int16 minLen = rHelper.GetMinWordLength();
     bool bNoHyphenateCaps = rHelper.IsNoHyphenateCaps();
 
-    rtl_TextEncoding eEnc = RTL_TEXTENCODING_DONTKNOW;
-
-    Reference< XHyphenatedWord > xRes;
-
-    ensureLocales();
-    int k = -1;
-    for (size_t j = 0; j < mvDicts.size(); ++j)
-    {
-        if (aLocale == mvDicts[j].aLoc)
-            k = j;
-    }
-
     // if we have a hyphenation dictionary matching this locale
-    if (k != -1)
+    if (auto pHDInfo = getMatchingDict(aLocale))
     {
         int nHyphenationPos = -1;
         int nHyphenationPosAlt = -1;
         int nHyphenationPosAltHyph = -1;
 
-        // if this dictionary has not been loaded yet do that
-        if (!mvDicts[k].aPtr)
-        {
-            if (!LoadDictionary(mvDicts[k]))
-                return nullptr;
-        }
-
-        // otherwise hyphenate the word with that dictionary
-        HyphenDict *dict = mvDicts[k].aPtr;
-        eEnc = mvDicts[k].eEnc;
-        CharClass * pCC =  mvDicts[k].apCC.get();
+        // hyphenate the word with that dictionary
+        HyphenDict* dict = pHDInfo->aPtr;
+        rtl_TextEncoding eEnc = pHDInfo->eEnc;
+        CharClass* pCC = pHDInfo->apCC.get();
 
         // Don't hyphenate uppercase words if requested
         if (bNoHyphenateCaps && aWord == makeUpperCase(aWord, pCC))
@@ -306,14 +314,6 @@ Reference< XHyphenatedWord > SAL_CALL 
Hyphenator::hyphenate( const OUString& aWo
             return nullptr;
         }
 
-        // we don't want to work with a default text encoding since following 
incorrect
-        // results may occur only for specific text and thus may be hard to 
notice.
-        // Thus better always make a clean exit here if the text encoding is 
in question.
-        // Hopefully something not working at all will raise proper attention 
quickly. ;-)
-        DBG_ASSERT( eEnc != RTL_TEXTENCODING_DONTKNOW, "failed to get text 
encoding! (maybe incorrect encoding string in file)" );
-        if (eEnc == RTL_TEXTENCODING_DONTKNOW)
-            return nullptr;
-
         CapType ct = capitalType(aWord, pCC);
 
         // first convert any smart quotes or apostrophes to normal ones
@@ -585,11 +585,8 @@ Reference< XHyphenatedWord > SAL_CALL 
Hyphenator::hyphenate( const OUString& aWo
             }
         }
 
-        if (nHyphenationPos  == -1)
-        {
-            xRes = nullptr;
-        }
-        else
+        Reference<XHyphenatedWord> xRes;
+        if (nHyphenationPos != -1)
         {
             if (rep && rep[nHyphenationPos])
             {
@@ -689,37 +686,13 @@ Reference< XPossibleHyphens > SAL_CALL 
Hyphenator::createPossibleHyphens( const
                       aWord, Sequence< sal_Int16 >() );
     }
 
-    ensureLocales();
-    int k = -1;
-    for (size_t j = 0; j < mvDicts.size(); ++j)
-    {
-        if (aLocale == mvDicts[j].aLoc)
-            k = j;
-    }
-
     // if we have a hyphenation dictionary matching this locale
-    if (k != -1)
+    if (auto pHDInfo = getMatchingDict(aLocale))
     {
-        HyphenDict *dict = nullptr;
-        // if this dictionary has not been loaded yet do that
-        if (!mvDicts[k].aPtr)
-        {
-            if (!LoadDictionary(mvDicts[k]))
-                return nullptr;
-        }
-
-        // otherwise hyphenate the word with that dictionary
-        dict = mvDicts[k].aPtr;
-        rtl_TextEncoding eEnc = mvDicts[k].eEnc;
-        CharClass* pCC = mvDicts[k].apCC.get();
-
-        // we don't want to work with a default text encoding since following 
incorrect
-        // results may occur only for specific text and thus may be hard to 
notice.
-        // Thus better always make a clean exit here if the text encoding is 
in question.
-        // Hopefully something not working at all will raise proper attention 
quickly. ;-)
-        DBG_ASSERT( eEnc != RTL_TEXTENCODING_DONTKNOW, "failed to get text 
encoding! (maybe incorrect encoding string in file)" );
-        if (eEnc == RTL_TEXTENCODING_DONTKNOW)
-            return nullptr;
+        // hyphenate the word with that dictionary
+        HyphenDict* dict = pHDInfo->aPtr;
+        rtl_TextEncoding eEnc = pHDInfo->eEnc;
+        CharClass* pCC = pHDInfo->apCC.get();
 
         // first handle smart quotes both single and double
         OUStringBuffer rBuf(aWord);
diff --git a/lingucomponent/source/hyphenator/hyphen/hyphenimp.hxx 
b/lingucomponent/source/hyphenator/hyphen/hyphenimp.hxx
index f60639ff2aa6..c66650891e6b 100644
--- a/lingucomponent/source/hyphenator/hyphen/hyphenimp.hxx
+++ b/lingucomponent/source/hyphenator/hyphen/hyphenimp.hxx
@@ -121,6 +121,7 @@ private:
         static OUString makeInitCap(const OUString&, CharClass const *);
 
     void ensureLocales();
+    const HDInfo* getMatchingDict(const css::lang::Locale& aLocale);
 };
 
 #endif

Reply via email to