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* pTransliteration =
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::xSysLocale->GetLanguageTag().getLanguageType();
168  OUString aCell( pTransliteration->transliterate(
169  rCell1.maStr.getString(), nLang, 0,
170  rCell1.maStr.getLength(), nullptr));
171  OUString aQuer( pTransliteration->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 
228 double 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 
290 double 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 
312 double 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: */
OUString getString() const
sal_Int32 compareString(const OUString &s1, const OUString &s2) const
static SC_DLLPUBLIC::utl::TransliterationWrapper * GetpTransliteration()
Definition: global.cxx:977
::utl::TransliterationWrapper * GetCaseTransliteration()
Definition: global.cxx:989
rtl_uString * getDataIgnoreCase()
Cell maCells[2]
Definition: compare.hxx:44
QueryItemsType & GetQueryItems()
Definition: queryentry.hxx:65
static SC_DLLPUBLIC CollatorWrapper * GetCollator()
Definition: global.cxx:1030
utl::SearchParam::SearchType eSearchType
Definition: compare.hxx:55
double CompareEmptyToNumericFunc(double fCell2)
Left cell is empty while the right cell is numeric.
Definition: compare.cxx:312
const Item & GetQueryItem() const
Definition: queryentry.cxx:118
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:157
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:47
static CollatorWrapper * GetCaseCollator()
Definition: global.cxx:1040
rtl_uString * getData()
svl::SharedString maString
Definition: queryentry.hxx:41
static std::unique_ptr< SvtSysLocale > xSysLocale
Definition: global.hxx:536
svl::SharedString maStr
Definition: compare.hxx:37
OUString transliterate(const OUString &rStr, sal_Int32 nStart, sal_Int32 nLen) const
ScQueryOp meOp
Definition: compare.hxx:46
std::vector< Item > QueryItemsType
Definition: queryentry.hxx:48
ScQueryOp eOp
Definition: queryentry.hxx:52
Each instance of this struct represents a single filtering criteria.
Definition: queryentry.hxx:33
ScQueryEntry aQueryEntry
Definition: compare.hxx:54