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