Coverage Report

Created: 2026-08-14 10:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/linguistic/source/spelldta.cxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 *
9
 * This file incorporates work covered by the following license notice:
10
 *
11
 *   Licensed to the Apache Software Foundation (ASF) under one or more
12
 *   contributor license agreements. See the NOTICE file distributed
13
 *   with this work for additional information regarding copyright
14
 *   ownership. The ASF licenses this file to you under the Apache
15
 *   License, Version 2.0 (the "License"); you may not use this file
16
 *   except in compliance with the License. You may obtain a copy of
17
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18
 */
19
20
#include <com/sun/star/uno/Reference.h>
21
#include <com/sun/star/linguistic2/SpellFailure.hpp>
22
#include <com/sun/star/linguistic2/XSearchableDictionaryList.hpp>
23
#include <osl/mutex.hxx>
24
#include <i18nlangtag/languagetag.hxx>
25
26
#include <algorithm>
27
#include <utility>
28
#include <vector>
29
30
#include <linguistic/misc.hxx>
31
#include <linguistic/spelldta.hxx>
32
#include <rtl/ref.hxx>
33
34
35
using namespace osl;
36
using namespace com::sun::star;
37
using namespace com::sun::star::lang;
38
using namespace com::sun::star::uno;
39
using namespace com::sun::star::linguistic2;
40
41
42
namespace linguistic
43
{
44
45
0
#define MAX_PROPOSALS   40
46
47
static bool SeqHasEntry(
48
        const std::vector< OUString > &rSeq,
49
        std::u16string_view rTxt)
50
0
{
51
0
    bool bRes = false;
52
0
    sal_Int32 nLen = rSeq.size();
53
0
    for (sal_Int32 i = 0;  i < nLen && !bRes;  ++i)
54
0
    {
55
0
        if (rTxt == rSeq[i])
56
0
            bRes = true;
57
0
    }
58
0
    return bRes;
59
0
}
60
61
62
void SearchSimilarText( const OUString &rText, LanguageType nLanguage,
63
        Reference< XSearchableDictionaryList > const &xDicList,
64
        std::vector< OUString > & rDicListProps )
65
0
{
66
0
    if (!xDicList.is())
67
0
        return;
68
69
0
    for (const Reference<XDictionary>& xDic : xDicList->getDictionaries())
70
0
    {
71
0
        LanguageType nLang = LinguLocaleToLanguage( xDic->getLocale() );
72
73
0
        if ( xDic.is() && xDic->isActive()
74
0
            && (nLang == nLanguage  ||  LinguIsUnspecified( nLang)) )
75
0
        {
76
#if OSL_DEBUG_LEVEL > 0
77
            DictionaryType  eType = xDic->getDictionaryType();
78
            (void) eType;
79
            assert( eType != DictionaryType_MIXED && "unexpected dictionary type" );
80
#endif
81
0
            const Sequence< Reference< XDictionaryEntry > > aEntries = xDic->getEntries();
82
0
            for (const Reference<XDictionaryEntry>& rEntry : aEntries)
83
0
            {
84
0
                OUString aEntryTxt;
85
0
                if (rEntry.is())
86
0
                {
87
                    // remove characters used to determine hyphenation positions
88
0
                    aEntryTxt = rEntry->getDictionaryWord().replaceAll("=", "");
89
0
                }
90
0
                if (!aEntryTxt.isEmpty()  &&  aEntryTxt.getLength() > 1  &&  LevDistance( rText, aEntryTxt ) <= 2)
91
0
                    rDicListProps.push_back( aEntryTxt );
92
0
            }
93
0
        }
94
0
    }
95
0
}
96
97
98
void SeqRemoveNegEntries( std::vector< OUString > &rSeq,
99
        Reference< XSearchableDictionaryList > const &rxDicList,
100
        LanguageType nLanguage,
101
        std::map<LanguageType, std::vector<css::uno::Reference<css::linguistic2::XDictionary>>>& rDictionaryMap )
102
0
{
103
0
    bool bSthRemoved = false;
104
0
    sal_Int32 nLen = rSeq.size();
105
0
    for (sal_Int32 i = 0;  i < nLen;  ++i)
106
0
    {
107
0
        Reference< XDictionaryEntry > xNegEntry( SearchDicList( rxDicList,
108
0
                    rSeq[i], nLanguage, false, true, rDictionaryMap ) );
109
0
        if (xNegEntry.is())
110
0
        {
111
0
            rSeq[i].clear();
112
0
            bSthRemoved = true;
113
0
        }
114
0
    }
115
0
    if (bSthRemoved)
116
0
    {
117
0
        std::vector< OUString > aNew;
118
        // merge sequence without duplicates and empty strings in new empty sequence
119
0
        rSeq = MergeProposalSeqs( aNew, rSeq );
120
0
    }
121
0
}
122
123
124
std::vector< OUString > MergeProposalSeqs(
125
            std::vector< OUString > &rAlt1,
126
            std::vector< OUString > &rAlt2 )
127
0
{
128
0
    std::vector< OUString > aMerged;
129
130
0
    size_t nAltCount1 = rAlt1.size();
131
0
    size_t nAltCount2 = rAlt2.size();
132
133
0
    sal_Int32 nCountNew = std::min<sal_Int32>( nAltCount1 + nAltCount2, sal_Int32(MAX_PROPOSALS) );
134
0
    aMerged.resize( nCountNew );
135
136
0
    sal_Int32 nIndex = 0;
137
0
    sal_Int32 i = 0;
138
0
    for (int j = 0;  j < 2;  j++)
139
0
    {
140
0
        sal_Int32        nCount  = j == 0 ? nAltCount1 : nAltCount2;
141
0
        std::vector< OUString >& rAlt   = j == 0 ? rAlt1 : rAlt2;
142
0
        for (i = 0;  i < nCount  &&  nIndex < MAX_PROPOSALS;  i++)
143
0
        {
144
0
            if (!rAlt[i].isEmpty() &&
145
0
                !SeqHasEntry(aMerged, rAlt[i] ))
146
0
                aMerged[ nIndex++ ] = rAlt[ i ];
147
0
        }
148
0
    }
149
0
    aMerged.resize( nIndex );
150
151
0
    return aMerged;
152
0
}
153
154
155
SpellAlternatives::SpellAlternatives()
156
0
{
157
0
    nLanguage   = LANGUAGE_NONE;
158
0
    nType       = SpellFailure::IS_NEGATIVE_WORD;
159
0
}
160
161
162
SpellAlternatives::SpellAlternatives(
163
        OUString aWord_, LanguageType nLang,
164
        const Sequence< OUString > &rAlternatives ) :
165
0
    aAlt        (rAlternatives),
166
0
    aWord       (std::move(aWord_)),
167
0
    nType       (SpellFailure::IS_NEGATIVE_WORD),
168
0
    nLanguage   (nLang)
169
0
{
170
0
}
171
172
173
SpellAlternatives::~SpellAlternatives()
174
0
{
175
0
}
176
177
178
OUString SAL_CALL SpellAlternatives::getWord()
179
0
{
180
0
    MutexGuard  aGuard( GetLinguMutex() );
181
0
    return aWord;
182
0
}
183
184
185
Locale SAL_CALL SpellAlternatives::getLocale()
186
0
{
187
0
    MutexGuard  aGuard( GetLinguMutex() );
188
0
    return LanguageTag::convertToLocale( nLanguage );
189
0
}
190
191
192
sal_Int16 SAL_CALL SpellAlternatives::getFailureType()
193
0
{
194
0
    MutexGuard  aGuard( GetLinguMutex() );
195
0
    return nType;
196
0
}
197
198
199
sal_Int16 SAL_CALL SpellAlternatives::getAlternativesCount()
200
0
{
201
0
    MutexGuard  aGuard( GetLinguMutex() );
202
0
    return static_cast<sal_Int16>(aAlt.getLength());
203
0
}
204
205
206
Sequence< OUString > SAL_CALL SpellAlternatives::getAlternatives()
207
0
{
208
0
    MutexGuard  aGuard( GetLinguMutex() );
209
0
    return aAlt;
210
0
}
211
212
213
void SAL_CALL SpellAlternatives::setAlternatives( const uno::Sequence< OUString >& rAlternatives )
214
0
{
215
0
    MutexGuard  aGuard( GetLinguMutex() );
216
0
    aAlt = rAlternatives;
217
0
}
218
219
220
void SAL_CALL SpellAlternatives::setFailureType( sal_Int16 nFailureType )
221
0
{
222
0
    MutexGuard  aGuard( GetLinguMutex() );
223
0
    nType = nFailureType;
224
0
}
225
226
227
void SpellAlternatives::SetWordLanguage(const OUString &rWord, LanguageType nLang)
228
0
{
229
0
    MutexGuard  aGuard( GetLinguMutex() );
230
0
    aWord = rWord;
231
0
    nLanguage = nLang;
232
0
}
233
234
235
void SpellAlternatives::SetFailureType(sal_Int16 nTypeP)
236
0
{
237
0
    MutexGuard  aGuard( GetLinguMutex() );
238
0
    nType = nTypeP;
239
0
}
240
241
242
void SpellAlternatives::SetAlternatives( const Sequence< OUString > &rAlt )
243
0
{
244
0
    MutexGuard  aGuard( GetLinguMutex() );
245
0
    aAlt = rAlt;
246
0
}
247
248
249
css::uno::Reference < css::linguistic2::XSpellAlternatives > SpellAlternatives::CreateSpellAlternatives(
250
        const OUString &rWord, LanguageType nLang, sal_Int16 nTypeP, const css::uno::Sequence< OUString > &rAlt )
251
0
{
252
0
    rtl::Reference<SpellAlternatives> pAlt = new SpellAlternatives;
253
0
    pAlt->SetWordLanguage( rWord, nLang );
254
0
    pAlt->SetFailureType( nTypeP );
255
0
    pAlt->SetAlternatives( rAlt );
256
0
    return pAlt;
257
0
}
258
259
260
}   // namespace linguistic
261
262
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */