summaryrefslogtreecommitdiffstats
path: root/sc/source/core/tool/compare.cxx
blob: 58ed51f65f44f20665cb2941ccb2610c9c616099 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <compare.hxx>

#include <document.hxx>
#include <docoptio.hxx>

#include <unotools/collatorwrapper.hxx>
#include <unotools/textsearch.hxx>
#include <unotools/transliterationwrapper.hxx>
#include <rtl/math.hxx>
#include <osl/diagnose.h>

namespace sc {

Compare::Cell::Cell() :
    mfValue(0.0), mbValue(false), mbEmpty(false) {}

Compare::Compare() :
    meOp(SC_EQUAL), mbIgnoreCase(true) {}

CompareOptions::CompareOptions( const ScDocument& rDoc, const ScQueryEntry& rEntry, utl::SearchParam::SearchType eSrchTyp ) :
    aQueryEntry(rEntry),
    eSearchType(eSrchTyp),
    bMatchWholeCell(rDoc.GetDocOptions().IsMatchWholeCell())
{
    // Wildcard and Regex search work only with equal or not equal.
    if (eSearchType != utl::SearchParam::SearchType::Normal &&
            aQueryEntry.eOp != SC_EQUAL && aQueryEntry.eOp != SC_NOT_EQUAL)
        eSearchType = utl::SearchParam::SearchType::Normal;

    // Interpreter functions usually are case insensitive, except the simple
    // comparison operators, for which these options aren't used. Override in
    // struct if needed.
}

double CompareFunc( const Compare& rComp, CompareOptions* pOptions )
{
    const Compare::Cell& rCell1 = rComp.maCells[0];
    const Compare::Cell& rCell2 = rComp.maCells[1];

    // Keep DoubleError if encountered
    // #i40539# if bEmpty is set, bVal/nVal are uninitialized
    if (!rCell1.mbEmpty && rCell1.mbValue && !std::isfinite(rCell1.mfValue))
        return rCell1.mfValue;
    if (!rCell2.mbEmpty && rCell2.mbValue && !std::isfinite(rCell2.mfValue))
        return rCell2.mfValue;

    size_t nStringQuery = 0;    // 0:=no, 1:=0, 2:=1
    double fRes = 0;
    if (rCell1.mbEmpty)
    {
        if (rCell2.mbEmpty)
            ;       // empty cell == empty cell, fRes 0
        else if (rCell2.mbValue)
        {
            if (rCell2.mfValue != 0.0)
            {
                if (rCell2.mfValue < 0.0)
                    fRes = 1;       // empty cell > -x
                else
                    fRes = -1;      // empty cell < x
            }
            // else: empty cell == 0.0
        }
        else
        {
            if (!rCell2.maStr.isEmpty())
                fRes = -1;      // empty cell < "..."
            // else: empty cell == ""
        }
    }
    else if (rCell2.mbEmpty)
    {
        if (rCell1.mbValue)
        {
            if (rCell1.mfValue != 0.0)
            {
                if (rCell1.mfValue < 0.0)
                    fRes = -1;      // -x < empty cell
                else
                    fRes = 1;       // x > empty cell
            }
            // else: empty cell == 0.0
        }
        else
        {
            if (!rCell1.maStr.isEmpty())
                fRes = 1;       // "..." > empty cell
            // else: "" == empty cell
        }
    }
    else if (rCell1.mbValue)
    {
        if (rCell2.mbValue)
        {
            if (!rtl::math::approxEqual(rCell1.mfValue, rCell2.mfValue))
            {
                if (rCell1.mfValue - rCell2.mfValue < 0)
                    fRes = -1;
                else
                    fRes = 1;
            }
        }
        else
        {
            fRes = -1;          // number is less than string
            nStringQuery = 2;   // 1+1
        }
    }
    else if (rCell2.mbValue)
    {
        fRes = 1;               // string is greater than number
        nStringQuery = 1;       // 0+1
    }
    else
    {
        // Both strings.
        if (pOptions)
        {
            // All similar to ScTable::ValidQuery(), *rComp.pVal[1] actually
            // is/must be identical to *rEntry.pStr, which is essential for
            // regex to work through GetSearchTextPtr().
            ScQueryEntry& rEntry = pOptions->aQueryEntry;
            OSL_ENSURE(rEntry.GetQueryItem().maString == rCell2.maStr, "ScInterpreter::CompareFunc: broken options");
            if (pOptions->eSearchType != utl::SearchParam::SearchType::Normal)
            {
                sal_Int32 nStart = 0;
                sal_Int32 nStop  = rCell1.maStr.getLength();
                bool bMatch = rEntry.GetSearchTextPtr( pOptions->eSearchType, !rComp.mbIgnoreCase,
                        pOptions->bMatchWholeCell)->SearchForward( rCell1.maStr.getString(), &nStart, &nStop);
                if (bMatch && pOptions->bMatchWholeCell && (nStart != 0 || nStop != rCell1.maStr.getLength()))
                    bMatch = false;     // RegEx must match entire string.
                fRes = (bMatch ? 0 : 1);
            }
            else if (rEntry.eOp == SC_EQUAL || rEntry.eOp == SC_NOT_EQUAL)
            {
                ::utl::TransliterationWrapper& rTransliteration =
                    ScGlobal::GetTransliteration(!rComp.mbIgnoreCase);
                bool bMatch = false;
                if (pOptions->bMatchWholeCell)
                {
                    if (rComp.mbIgnoreCase)
                        bMatch = rCell1.maStr.getDataIgnoreCase() == rCell2.maStr.getDataIgnoreCase();
                    else
                        bMatch = rCell1.maStr.getData() == rCell2.maStr.getData();
                }
                else
                {
                    const LanguageType nLang = ScGlobal::oSysLocale->GetLanguageTag().getLanguageType();
                    OUString aCell( rTransliteration.transliterate(
                                rCell1.maStr.getString(), nLang, 0,
                                rCell1.maStr.getLength(), nullptr));
                    OUString aQuer( rTransliteration.transliterate(
                                rCell2.maStr.getString(), nLang, 0,
                                rCell2.maStr.getLength(), nullptr));
                    bMatch = (aCell.indexOf( aQuer ) != -1);
                }
                fRes = (bMatch ? 0 : 1);
            }
            else if (rComp.mbIgnoreCase)
                fRes = static_cast<double>(ScGlobal::GetCollator().compareString(
                        rCell1.maStr.getString(), rCell2.maStr.getString()));
            else
                fRes = static_cast<double>(ScGlobal::GetCaseCollator().compareString(
                        rCell1.maStr.getString(), rCell2.maStr.getString()));
        }
        else if (rComp.meOp == SC_EQUAL || rComp.meOp == SC_NOT_EQUAL)
        {
            if (rComp.mbIgnoreCase)
                fRes = (rCell1.maStr.getDataIgnoreCase() == rCell2.maStr.getDataIgnoreCase()) ? 0 : 1;
            else
                fRes = (rCell1.maStr.getData() == rCell2.maStr.getData()) ? 0 : 1;
        }
        else if (rComp.mbIgnoreCase)
            fRes = static_cast<double>(ScGlobal::GetCollator().compareString(
                rCell1.maStr.getString(), rCell2.maStr.getString()));
        else
            fRes = static_cast<double>(ScGlobal::GetCaseCollator().compareString(
                rCell1.maStr.getString(), rCell2.maStr.getString()));
    }

    if (nStringQuery && pOptions)
    {
        const ScQueryEntry& rEntry = pOptions->aQueryEntry;
        const ScQueryEntry::QueryItemsType& rItems = rEntry.GetQueryItems();
        if (!rItems.empty())
        {
            const ScQueryEntry::Item& rItem = rItems[0];
            if (rItem.meType != ScQueryEntry::ByString && !rItem.maString.isEmpty() &&
                (rEntry.eOp == SC_EQUAL || rEntry.eOp == SC_NOT_EQUAL))
            {
                // As in ScTable::ValidQuery() match a numeric string for a
                // number query that originated from a string, e.g. in SUMIF
                // and COUNTIF. Transliteration is not needed here.
                bool bEqual = false;
                if (nStringQuery == 1)
                    bEqual = rCell1.maStr == rItem.maString;
                else
                    bEqual = rCell2.maStr == rItem.maString;

                // match => fRes=0, else fRes=1
                fRes = double((rEntry.eOp == SC_NOT_EQUAL) ? bEqual : !bEqual);
            }
        }
    }

    return fRes;
}

double CompareFunc( const Compare::Cell& rCell1, double fCell2, const CompareOptions* pOptions )
{
    // Keep DoubleError if encountered
    // #i40539# if bEmpty is set, bVal/nVal are uninitialized
    if (!rCell1.mbEmpty && rCell1.mbValue && !std::isfinite(rCell1.mfValue))
        return rCell1.mfValue;
    if (!std::isfinite(fCell2))
        return fCell2;

    bool bStringQuery = false;
    double fRes = 0;
    if (rCell1.mbEmpty)
    {
        if (fCell2 != 0.0)
        {
            if (fCell2 < 0.0)
                fRes = 1;       // empty cell > -x
            else
                fRes = -1;      // empty cell < x
        }
        // else: empty cell == 0.0
    }
    else if (rCell1.mbValue)
    {
        if (!rtl::math::approxEqual(rCell1.mfValue, fCell2))
        {
            if (rCell1.mfValue - fCell2 < 0)
                fRes = -1;
            else
                fRes = 1;
        }
    }
    else
    {
        fRes = 1;               // string is greater than number
        bStringQuery = true;
    }

    if (bStringQuery && pOptions)
    {
        const ScQueryEntry& rEntry = pOptions->aQueryEntry;
        const ScQueryEntry::QueryItemsType& rItems = rEntry.GetQueryItems();
        if (!rItems.empty())
        {
            const ScQueryEntry::Item& rItem = rItems[0];
            if (rItem.meType != ScQueryEntry::ByString && !rItem.maString.isEmpty() &&
                (rEntry.eOp == SC_EQUAL || rEntry.eOp == SC_NOT_EQUAL))
            {
                // As in ScTable::ValidQuery() match a numeric string for a
                // number query that originated from a string, e.g. in SUMIF
                // and COUNTIF. Transliteration is not needed here.
                bool bEqual = rCell1.maStr == rItem.maString;

                // match => fRes=0, else fRes=1
                fRes = double((rEntry.eOp == SC_NOT_EQUAL) ? bEqual : !bEqual);
            }
        }
    }

    return fRes;
}

double CompareFunc( double fCell1, double fCell2 )
{
    // Keep DoubleError if encountered
    // #i40539# if bEmpty is set, bVal/nVal are uninitialized
    if (!std::isfinite(fCell1))
        return fCell1;
    if (!std::isfinite(fCell2))
        return fCell2;

    double fRes = 0.0;

    if (!rtl::math::approxEqual(fCell1, fCell2))
    {
        if (fCell1 - fCell2 < 0.0)
            fRes = -1;
        else
            fRes = 1;
    }

    return fRes;
}

double CompareEmptyToNumericFunc( double fCell2 )
{
    // Keep DoubleError if encountered
    // #i40539# if bEmpty is set, bVal/nVal are uninitialized
    if (!std::isfinite(fCell2))
        return fCell2;

    double fRes = 0;
    if (fCell2 != 0.0)
    {
        if (fCell2 < 0.0)
            fRes = 1;       // empty cell > -x
        else
            fRes = -1;      // empty cell < x
    }
    // else: empty cell == 0.0

    return fRes;
}

}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */