LibreOffice Module sc (master) 1
compare.cxx
Go to the documentation of this file.
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 <compare.hxx>
21
22#include <document.hxx>
23#include <docoptio.hxx>
24
28#include <rtl/math.hxx>
29#include <osl/diagnose.h>
30
31namespace sc {
32
34 mfValue(0.0), mbValue(false), mbEmpty(false) {}
35
37 meOp(SC_EQUAL), mbIgnoreCase(true) {}
38
40 aQueryEntry(rEntry),
41 eSearchType(eSrchTyp),
42 bMatchWholeCell(rDoc.GetDocOptions().IsMatchWholeCell())
43{
44 // Wildcard and Regex search work only with equal or not equal.
48
49 // Interpreter functions usually are case insensitive, except the simple
50 // comparison operators, for which these options aren't used. Override in
51 // struct if needed.
52}
53
54double CompareFunc( const Compare& rComp, CompareOptions* pOptions )
55{
56 const Compare::Cell& rCell1 = rComp.maCells[0];
57 const Compare::Cell& rCell2 = rComp.maCells[1];
58
59 // Keep DoubleError if encountered
60 // #i40539# if bEmpty is set, bVal/nVal are uninitialized
61 if (!rCell1.mbEmpty && rCell1.mbValue && !std::isfinite(rCell1.mfValue))
62 return rCell1.mfValue;
63 if (!rCell2.mbEmpty && rCell2.mbValue && !std::isfinite(rCell2.mfValue))
64 return rCell2.mfValue;
65
66 size_t nStringQuery = 0; // 0:=no, 1:=0, 2:=1
67 double fRes = 0;
68 if (rCell1.mbEmpty)
69 {
70 if (rCell2.mbEmpty)
71 ; // empty cell == empty cell, fRes 0
72 else if (rCell2.mbValue)
73 {
74 if (rCell2.mfValue != 0.0)
75 {
76 if (rCell2.mfValue < 0.0)
77 fRes = 1; // empty cell > -x
78 else
79 fRes = -1; // empty cell < x
80 }
81 // else: empty cell == 0.0
82 }
83 else
84 {
85 if (!rCell2.maStr.isEmpty())
86 fRes = -1; // empty cell < "..."
87 // else: empty cell == ""
88 }
89 }
90 else if (rCell2.mbEmpty)
91 {
92 if (rCell1.mbValue)
93 {
94 if (rCell1.mfValue != 0.0)
95 {
96 if (rCell1.mfValue < 0.0)
97 fRes = -1; // -x < empty cell
98 else
99 fRes = 1; // x > empty cell
100 }
101 // else: empty cell == 0.0
102 }
103 else
104 {
105 if (!rCell1.maStr.isEmpty())
106 fRes = 1; // "..." > empty cell
107 // else: "" == empty cell
108 }
109 }
110 else if (rCell1.mbValue)
111 {
112 if (rCell2.mbValue)
113 {
114 if (!rtl::math::approxEqual(rCell1.mfValue, rCell2.mfValue))
115 {
116 if (rCell1.mfValue - rCell2.mfValue < 0)
117 fRes = -1;
118 else
119 fRes = 1;
120 }
121 }
122 else
123 {
124 fRes = -1; // number is less than string
125 nStringQuery = 2; // 1+1
126 }
127 }
128 else if (rCell2.mbValue)
129 {
130 fRes = 1; // string is greater than number
131 nStringQuery = 1; // 0+1
132 }
133 else
134 {
135 // Both strings.
136 if (pOptions)
137 {
138 // All similar to ScTable::ValidQuery(), *rComp.pVal[1] actually
139 // is/must be identical to *rEntry.pStr, which is essential for
140 // regex to work through GetSearchTextPtr().
141 ScQueryEntry& rEntry = pOptions->aQueryEntry;
142 OSL_ENSURE(rEntry.GetQueryItem().maString == rCell2.maStr, "ScInterpreter::CompareFunc: broken options");
144 {
145 sal_Int32 nStart = 0;
146 sal_Int32 nStop = rCell1.maStr.getLength();
147 bool bMatch = rEntry.GetSearchTextPtr( pOptions->eSearchType, !rComp.mbIgnoreCase,
148 pOptions->bMatchWholeCell)->SearchForward( rCell1.maStr.getString(), &nStart, &nStop);
149 if (bMatch && pOptions->bMatchWholeCell && (nStart != 0 || nStop != rCell1.maStr.getLength()))
150 bMatch = false; // RegEx must match entire string.
151 fRes = (bMatch ? 0 : 1);
152 }
153 else if (rEntry.eOp == SC_EQUAL || rEntry.eOp == SC_NOT_EQUAL)
154 {
155 ::utl::TransliterationWrapper& rTransliteration =
157 bool bMatch = false;
158 if (pOptions->bMatchWholeCell)
159 {
160 if (rComp.mbIgnoreCase)
161 bMatch = rCell1.maStr.getDataIgnoreCase() == rCell2.maStr.getDataIgnoreCase();
162 else
163 bMatch = rCell1.maStr.getData() == rCell2.maStr.getData();
164 }
165 else
166 {
167 const LanguageType nLang = ScGlobal::oSysLocale->GetLanguageTag().getLanguageType();
168 OUString aCell( rTransliteration.transliterate(
169 rCell1.maStr.getString(), nLang, 0,
170 rCell1.maStr.getLength(), nullptr));
171 OUString aQuer( rTransliteration.transliterate(
172 rCell2.maStr.getString(), nLang, 0,
173 rCell2.maStr.getLength(), nullptr));
174 bMatch = (aCell.indexOf( aQuer ) != -1);
175 }
176 fRes = (bMatch ? 0 : 1);
177 }
178 else if (rComp.mbIgnoreCase)
179 fRes = static_cast<double>(ScGlobal::GetCollator().compareString(
180 rCell1.maStr.getString(), rCell2.maStr.getString()));
181 else
182 fRes = static_cast<double>(ScGlobal::GetCaseCollator().compareString(
183 rCell1.maStr.getString(), rCell2.maStr.getString()));
184 }
185 else if (rComp.meOp == SC_EQUAL || rComp.meOp == SC_NOT_EQUAL)
186 {
187 if (rComp.mbIgnoreCase)
188 fRes = (rCell1.maStr.getDataIgnoreCase() == rCell2.maStr.getDataIgnoreCase()) ? 0 : 1;
189 else
190 fRes = (rCell1.maStr.getData() == rCell2.maStr.getData()) ? 0 : 1;
191 }
192 else if (rComp.mbIgnoreCase)
193 fRes = static_cast<double>(ScGlobal::GetCollator().compareString(
194 rCell1.maStr.getString(), rCell2.maStr.getString()));
195 else
196 fRes = static_cast<double>(ScGlobal::GetCaseCollator().compareString(
197 rCell1.maStr.getString(), rCell2.maStr.getString()));
198 }
199
200 if (nStringQuery && pOptions)
201 {
202 const ScQueryEntry& rEntry = pOptions->aQueryEntry;
203 const ScQueryEntry::QueryItemsType& rItems = rEntry.GetQueryItems();
204 if (!rItems.empty())
205 {
206 const ScQueryEntry::Item& rItem = rItems[0];
207 if (rItem.meType != ScQueryEntry::ByString && !rItem.maString.isEmpty() &&
208 (rEntry.eOp == SC_EQUAL || rEntry.eOp == SC_NOT_EQUAL))
209 {
210 // As in ScTable::ValidQuery() match a numeric string for a
211 // number query that originated from a string, e.g. in SUMIF
212 // and COUNTIF. Transliteration is not needed here.
213 bool bEqual = false;
214 if (nStringQuery == 1)
215 bEqual = rCell1.maStr == rItem.maString;
216 else
217 bEqual = rCell2.maStr == rItem.maString;
218
219 // match => fRes=0, else fRes=1
220 fRes = double((rEntry.eOp == SC_NOT_EQUAL) ? bEqual : !bEqual);
221 }
222 }
223 }
224
225 return fRes;
226}
227
228double CompareFunc( const Compare::Cell& rCell1, double fCell2, const CompareOptions* pOptions )
229{
230 // Keep DoubleError if encountered
231 // #i40539# if bEmpty is set, bVal/nVal are uninitialized
232 if (!rCell1.mbEmpty && rCell1.mbValue && !std::isfinite(rCell1.mfValue))
233 return rCell1.mfValue;
234 if (!std::isfinite(fCell2))
235 return fCell2;
236
237 bool bStringQuery = false;
238 double fRes = 0;
239 if (rCell1.mbEmpty)
240 {
241 if (fCell2 != 0.0)
242 {
243 if (fCell2 < 0.0)
244 fRes = 1; // empty cell > -x
245 else
246 fRes = -1; // empty cell < x
247 }
248 // else: empty cell == 0.0
249 }
250 else if (rCell1.mbValue)
251 {
252 if (!rtl::math::approxEqual(rCell1.mfValue, fCell2))
253 {
254 if (rCell1.mfValue - fCell2 < 0)
255 fRes = -1;
256 else
257 fRes = 1;
258 }
259 }
260 else
261 {
262 fRes = 1; // string is greater than number
263 bStringQuery = true;
264 }
265
266 if (bStringQuery && pOptions)
267 {
268 const ScQueryEntry& rEntry = pOptions->aQueryEntry;
269 const ScQueryEntry::QueryItemsType& rItems = rEntry.GetQueryItems();
270 if (!rItems.empty())
271 {
272 const ScQueryEntry::Item& rItem = rItems[0];
273 if (rItem.meType != ScQueryEntry::ByString && !rItem.maString.isEmpty() &&
274 (rEntry.eOp == SC_EQUAL || rEntry.eOp == SC_NOT_EQUAL))
275 {
276 // As in ScTable::ValidQuery() match a numeric string for a
277 // number query that originated from a string, e.g. in SUMIF
278 // and COUNTIF. Transliteration is not needed here.
279 bool bEqual = rCell1.maStr == rItem.maString;
280
281 // match => fRes=0, else fRes=1
282 fRes = double((rEntry.eOp == SC_NOT_EQUAL) ? bEqual : !bEqual);
283 }
284 }
285 }
286
287 return fRes;
288}
289
290double CompareFunc( double fCell1, double fCell2 )
291{
292 // Keep DoubleError if encountered
293 // #i40539# if bEmpty is set, bVal/nVal are uninitialized
294 if (!std::isfinite(fCell1))
295 return fCell1;
296 if (!std::isfinite(fCell2))
297 return fCell2;
298
299 double fRes = 0.0;
300
301 if (!rtl::math::approxEqual(fCell1, fCell2))
302 {
303 if (fCell1 - fCell2 < 0.0)
304 fRes = -1;
305 else
306 fRes = 1;
307 }
308
309 return fRes;
310}
311
312double CompareEmptyToNumericFunc( double fCell2 )
313{
314 // Keep DoubleError if encountered
315 // #i40539# if bEmpty is set, bVal/nVal are uninitialized
316 if (!std::isfinite(fCell2))
317 return fCell2;
318
319 double fRes = 0;
320 if (fCell2 != 0.0)
321 {
322 if (fCell2 < 0.0)
323 fRes = 1; // empty cell > -x
324 else
325 fRes = -1; // empty cell < x
326 }
327 // else: empty cell == 0.0
328
329 return fRes;
330}
331
332}
333
334/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_Int32 compareString(const OUString &s1, const OUString &s2) const
static SC_DLLPUBLIC CollatorWrapper & GetCollator()
case-insensitive collator
Definition: global.cxx:1095
static SC_DLLPUBLIC ::utl::TransliterationWrapper & GetTransliteration()
Definition: global.cxx:1026
static std::optional< SvtSysLocale > oSysLocale
Definition: global.hxx:544
static CollatorWrapper & GetCaseCollator()
case-sensitive collator
Definition: global.cxx:1106
const OUString & getString() const
rtl_uString * getDataIgnoreCase()
bool isEmpty() const
sal_Int32 getLength() const
rtl_uString * getData()
bool SearchForward(const OUString &rStr, sal_Int32 *pStart, sal_Int32 *pEnd, css::util::SearchResult *pRes=nullptr)
OUString transliterate(const OUString &rStr, sal_Int32 nStart, sal_Int32 nLen) const
@ SC_EQUAL
Definition: global.hxx:835
@ SC_NOT_EQUAL
Definition: global.hxx:840
CAUTION! The following defines must be in the same namespace as the respective type.
Definition: broadcast.cxx:15
double CompareFunc(const Compare &rComp, CompareOptions *pOptions)
Definition: compare.cxx:54
double CompareEmptyToNumericFunc(double fCell2)
Left cell is empty while the right cell is numeric.
Definition: compare.cxx:312
svl::SharedString maString
Definition: queryentry.hxx:49
Each instance of this struct represents a single filtering criteria.
Definition: queryentry.hxx:34
const Item & GetQueryItem() const
Definition: queryentry.hxx:85
std::vector< Item > QueryItemsType
Definition: queryentry.hxx:58
utl::TextSearch * GetSearchTextPtr(utl::SearchParam::SearchType eSearchType, bool bCaseSens, bool bWildMatchSel) const
creates pSearchParam and pSearchText if necessary
Definition: queryentry.cxx:194
ScQueryOp eOp
Definition: queryentry.hxx:62
QueryItemsType & GetQueryItems()
Definition: queryentry.hxx:75
CompareOptions(const ScDocument &rDoc, const ScQueryEntry &rEntry, utl::SearchParam::SearchType eSrchTyp)
Definition: compare.cxx:39
utl::SearchParam::SearchType eSearchType
Definition: compare.hxx:54
ScQueryEntry aQueryEntry
Definition: compare.hxx:53
svl::SharedString maStr
Definition: compare.hxx:36
Cell maCells[2]
Definition: compare.hxx:43
bool mbIgnoreCase
Definition: compare.hxx:46
ScQueryOp meOp
Definition: compare.hxx:45