1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SVL_SOURCE_NUMBERS_ZFORFIND_HXX
21 #define INCLUDED_SVL_SOURCE_NUMBERS_ZFORFIND_HXX
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <rtl/ustring.hxx>
29 class SvNumberFormatter
;
30 enum class SvNumFormatType
: sal_Int16
;
32 #define SV_MAX_COUNT_INPUT_STRINGS 20 // max count of substrings in input scanner
34 class ImpSvNumberInputScan
37 explicit ImpSvNumberInputScan( SvNumberFormatter
* pFormatter
);
38 ~ImpSvNumberInputScan();
40 /*!*/ void ChangeIntl(); // MUST be called if language changes
42 /// set reference date for offset calculation
43 void ChangeNullDate( const sal_uInt16 nDay
,
44 const sal_uInt16 nMonth
,
45 const sal_Int16 nYear
);
47 /// convert input string to number
48 bool IsNumberFormat( const OUString
& rString
, /// input string
49 SvNumFormatType
& F_Type
, /// format type (in + out)
50 double& fOutNumber
, /// value determined (out)
51 const SvNumberformat
* pFormat
); /// number format to which compare against
53 /// after IsNumberFormat: get decimal position
54 short GetDecPos() const { return nDecPos
; }
55 /// after IsNumberFormat: get count of numeric substrings in input string
56 sal_uInt16
GetNumericsCount() const { return nNumericsCnt
; }
58 /// set threshold of two-digit year input
59 void SetYear2000( sal_uInt16 nVal
) { nYear2000
= nVal
; }
60 /// get threshold of two-digit year input
61 sal_uInt16
GetYear2000() const { return nYear2000
; }
63 /** Whether input can be forced to ISO 8601 format.
65 Depends on locale's date separator and a specific date format order.
67 bool CanForceToIso8601( DateOrder eDateOrder
);
69 void InvalidateDateAcceptancePatterns();
72 SvNumberFormatter
* pFormatter
;
73 const SvNumberformat
* mpFormat
; //* The format to compare against, if any
74 std::unique_ptr
<OUString
[]> pUpperMonthText
; //* Array of month names, uppercase
75 std::unique_ptr
<OUString
[]> pUpperAbbrevMonthText
; //* Array of month names, abbreviated, uppercase
76 std::unique_ptr
<OUString
[]> pUpperGenitiveMonthText
; //* Array of genitive month names, uppercase
77 std::unique_ptr
<OUString
[]> pUpperGenitiveAbbrevMonthText
; //* Array of genitive month names, abbreviated, uppercase
78 std::unique_ptr
<OUString
[]> pUpperPartitiveMonthText
; //* Array of partitive month names, uppercase
79 std::unique_ptr
<OUString
[]> pUpperPartitiveAbbrevMonthText
;//* Array of partitive month names, abbreviated, uppercase
80 std::unique_ptr
<OUString
[]> pUpperDayText
; //* Array of day of week names, uppercase
81 std::unique_ptr
<OUString
[]> pUpperAbbrevDayText
; //* Array of day of week names, abbreviated, uppercase
82 OUString aUpperCurrSymbol
; //* Currency symbol, uppercase
83 bool bTextInitialized
; //* Whether days and months are initialized
84 bool bScanGenitiveMonths
; //* Whether to scan an input for genitive months
85 bool bScanPartitiveMonths
; //* Whether to scan an input for partitive months
86 std::unique_ptr
<Date
> pNullDate
; //* 30Dec1899
87 // Variables for provisional results:
88 OUString sStrArray
[SV_MAX_COUNT_INPUT_STRINGS
];//* Array of scanned substrings
89 bool IsNum
[SV_MAX_COUNT_INPUT_STRINGS
]; //* Whether a substring is numeric
90 sal_uInt16 nNums
[SV_MAX_COUNT_INPUT_STRINGS
]; //* Sequence of offsets to numeric strings
91 sal_uInt16 nStringsCnt
; //* Total count of scanned substrings
92 sal_uInt16 nNumericsCnt
; //* Count of numeric substrings
93 bool bDecSepInDateSeps
; //* True <=> DecSep in {.,-,/,DateSep}
94 sal_uInt8 nMatchedAllStrings
; //* Scan...String() matched all substrings,
96 // bit mask of nMatched... constants
97 static const sal_uInt8 nMatchedEndString
; // 0x01
98 static const sal_uInt8 nMatchedMidString
; // 0x02
99 static const sal_uInt8 nMatchedStartString
; // 0x04
100 static const sal_uInt8 nMatchedVirgin
; // 0x08
101 static const sal_uInt8 nMatchedUsedAsReturn
; // 0x10
103 int nSign
; // Sign of number
104 int nMonth
; // Month (1..x) if date
105 // negative => short format
106 short nMonthPos
; // 1 = front, 2 = middle
108 int nDayOfWeek
; // Temporary (!) day of week (1..7,-1..-7) if date
109 sal_uInt16 nTimePos
; // Index of first time separator (+1)
110 short nDecPos
; // Index of substring containing "," (+1)
111 bool bNegCheck
; // '( )' for negative
112 short nESign
; // Sign of exponent
113 short nAmPm
; // +1 AM, -1 PM, 0 if none
114 short nLogical
; // -1 => False, 1 => True
115 bool mbEraCE
; // Era if date, 0 => BCE, 1 => CE (currently only Gregorian)
116 sal_uInt16 nThousand
; // Count of group (AKA thousand) separators
117 sal_uInt16 nPosThousandString
; // Position of concatenated 000,000,000 string
118 SvNumFormatType eScannedType
; // Scanned type
119 SvNumFormatType eSetType
; // Preset Type
121 sal_uInt16 nStringScanNumFor
; // Fixed strings recognized in
122 // pFormat->NumFor[nNumForStringScan]
123 short nStringScanSign
; // Sign resulting of FixString
124 sal_uInt16 nYear2000
; // Two-digit threshold
127 // number <= nYear2000 => 20xx
128 // number > nYear2000 => 19xx
130 /** State of ISO 8601 detection.
134 2:= yes, <=2 digits in year
135 3:= yes, 3 digits in year
136 4:= yes, >=4 digits in year
140 sal_uInt8 nMayBeIso8601
;
142 /** Whether the 'T' time separator was detected in an ISO 8601 string. */
145 /** State of dd-month-yy or yy-month-dd detection, with month name.
152 @see MayBeMonthDate()
154 sal_uInt8 nMayBeMonthDate
;
156 /** Input matched this locale dependent date acceptance pattern.
157 -2 if not checked yet, -1 if no match, >=0 matched pattern.
159 @see IsAcceptedDatePattern()
161 sal_Int32 nAcceptedDatePattern
;
162 css::uno::Sequence
< OUString
> sDateAcceptancePatterns
;
164 /** If input matched a date acceptance pattern that starts at input
165 particle sStrArray[nDatePatternStart].
167 @see IsAcceptedDatePattern()
169 sal_uInt16 nDatePatternStart
;
171 /** Count of numbers that matched the accepted pattern, if any, else 0.
173 @see GetDatePatternNumbers()
175 sal_uInt16 nDatePatternNumbers
;
177 // Copy assignment is forbidden and not implemented.
178 ImpSvNumberInputScan (const ImpSvNumberInputScan
&) = delete;
179 ImpSvNumberInputScan
& operator= (const ImpSvNumberInputScan
&) = delete;
181 void Reset(); // Reset all variables before start of analysis
183 void InitText(); // Init of months and days of week
185 // Convert string to double.
186 // Only simple unsigned floating point values without any error detection,
187 // decimal separator has to be '.'
188 // If bForceFraction==true the string is taken to be the fractional part
189 // of 0.1234 without the leading 0. (thus being just "1234").
190 static double StringToDouble( const OUString
& rStr
,
191 bool bForceFraction
= false );
193 // Next number/string symbol
194 static bool NextNumberStringSymbol( const sal_Unicode
*& pStr
,
197 // Concatenate ,000,23 blocks
198 // in input to 000123
199 bool SkipThousands( const sal_Unicode
*& pStr
, OUString
& rSymbol
) const;
201 // Divide numbers/strings into
202 // arrays and variables above.
203 // Leading blanks and blanks
204 // after numbers are thrown away
205 void NumberStringDivision( const OUString
& rString
);
208 /** Whether rString contains word (!) rWhat at nPos.
209 rWhat will not be matched if it is a substring of a word.
211 bool StringContainsWord( const OUString
& rWhat
,
212 const OUString
& rString
,
213 sal_Int32 nPos
) const;
215 // optimized substring versions
217 // Whether rString contains rWhat at nPos
218 static bool StringContains( const OUString
& rWhat
,
219 const OUString
& rString
,
222 if (rWhat
.isEmpty() || rString
.getLength() <= nPos
)
226 // mostly used with one character
227 if ( rWhat
[ 0 ] != rString
[ nPos
] )
231 return StringContainsImpl( rWhat
, rString
, nPos
);
234 // Whether pString contains rWhat at nPos
235 static bool StringPtrContains( const OUString
& rWhat
,
236 const sal_Unicode
* pString
,
237 sal_Int32 nPos
) // nPos MUST be a valid offset from pString
239 // mostly used with one character
240 if ( rWhat
[ 0 ] != pString
[ nPos
] )
244 return StringPtrContainsImpl( rWhat
, pString
, nPos
);
247 //! DO NOT use directly
248 static bool StringContainsImpl( const OUString
& rWhat
,
249 const OUString
& rString
,
251 //! DO NOT use directly
252 static bool StringPtrContainsImpl( const OUString
& rWhat
,
253 const sal_Unicode
* pString
,
256 // Skip a special character
257 static inline bool SkipChar( sal_Unicode c
,
258 const OUString
& rString
,
262 static inline void SkipBlanks( const OUString
& rString
,
265 // Jump over rWhat in rString at nPos
266 static inline bool SkipString( const OUString
& rWhat
,
267 const OUString
& rString
,
270 // Recognizes exactly ,111 as group separator
271 inline bool GetThousandSep( const OUString
& rString
,
273 sal_uInt16 nStringPos
) const;
275 short GetLogical( const OUString
& rString
) const;
277 // Get month and advance string position
278 short GetMonth( const OUString
& rString
,
281 // Get day of week and advance string position
282 int GetDayOfWeek( const OUString
& rString
,
285 // Get currency symbol and advance string position
286 bool GetCurrency( const OUString
& rString
,
289 // Get symbol AM or PM and advance string position
290 bool GetTimeAmPm( const OUString
& rString
,
293 // Get decimal separator and advance string position
294 inline bool GetDecSep( const OUString
& rString
,
295 sal_Int32
& nPos
) const;
297 // Get hundredth seconds separator and advance string position
298 inline bool GetTime100SecSep( const OUString
& rString
,
299 sal_Int32
& nPos
) const;
301 // Get sign and advance string position
302 // Including special case '('
303 int GetSign( const OUString
& rString
,
306 // Get sign of exponent and advance string position
307 static short GetESign( const OUString
& rString
,
310 // Get next number as array offset
311 inline bool GetNextNumber( sal_uInt16
& i
,
312 sal_uInt16
& j
) const;
314 /** Converts time -> double (only decimals)
316 @return TRUE if time, FALSE if not (e.g. hours >12 with AM/PM)
318 bool GetTimeRef( double& fOutNumber
, // result as double
319 sal_uInt16 nIndex
, // Index of hour in input
320 sal_uInt16 nCnt
) const; // Count of time substrings in input
321 sal_uInt16
ImplGetDay ( sal_uInt16 nIndex
) const; // Day input, 0 if no match
322 sal_uInt16
ImplGetMonth( sal_uInt16 nIndex
) const; // Month input, zero based return, NumberOfMonths if no match
323 sal_uInt16
ImplGetYear ( sal_uInt16 nIndex
); // Year input, 0 if no match
325 // Conversion of date to number
326 bool GetDateRef( double& fDays
, // OUT: days diff to null date
327 sal_uInt16
& nCounter
); // Count of date substrings
329 // Analyze start of string
330 bool ScanStartString( const OUString
& rString
);
332 // Analyze middle substring
333 bool ScanMidString( const OUString
& rString
,
334 sal_uInt16 nStringPos
);
337 // Analyze end of string
338 bool ScanEndString( const OUString
& rString
);
340 // Compare rString to substring of array indexed by nString
341 // nString == 0xFFFF => last substring
342 bool ScanStringNumFor( const OUString
& rString
,
345 bool bDontDetectNegation
= false );
347 // if nMatchedAllStrings set nMatchedUsedAsReturn and return true,
348 // else do nothing and return false
349 bool MatchedReturn();
351 //! Be sure that the string to be analyzed is already converted to upper
352 //! case and if it contained native number digits that they are already
353 //! converted to ASCII.
355 // Main analyzing function
356 bool IsNumberFormatMain( const OUString
& rString
,
357 const SvNumberformat
* pFormat
); // number format to match against
359 /** Whether input matches locale dependent date acceptance pattern.
361 @param nStartPatternAt
362 The pattern matching starts at input particle
363 sStrArray[nStartPatternAt].
365 NOTE: once called the result is remembered, subsequent calls with
366 different parameters do not check for a match and do not lead to a
369 bool IsAcceptedDatePattern( sal_uInt16 nStartPatternAt
);
371 /** Sets (not advances!) rPos to sStrArray[nParticle].getLength() if string
372 matches separator in pattern at nParticle.
374 Also detects a signed year case like M/D/-Y
376 @returns TRUE if separator matched.
378 bool SkipDatePatternSeparator( sal_uInt16 nParticle
, sal_Int32
& rPos
, bool & rSignedYear
);
380 /** Returns count of numbers in accepted date pattern.
382 sal_uInt16
GetDatePatternNumbers();
384 /** Whether numeric string nNumber is of type cType in accepted date
385 pattern, 'Y', 'M' or 'D'.
387 bool IsDatePatternNumberOfType( sal_uInt16 nNumber
, sal_Unicode cType
);
389 /** Obtain order of accepted date pattern coded as, for example,
390 ('D'<<16)|('M'<<8)|'Y'
392 sal_uInt32
GetDatePatternOrder();
394 /** Obtain date format order, from accepted date pattern if available or
395 otherwise the locale's default order.
397 DateOrder
GetDateOrder();
399 /** Whether input may be an ISO 8601 date format, yyyy-mm-dd...
401 Checks if input has at least 3 numbers for yyyy-mm-dd and the separator
402 is '-', and 1<=mm<=12 and 1<=dd<=31.
408 /** Whether input may be a dd-month-yy format, with month name, not
413 bool MayBeMonthDate();
415 /** Whether input is acceptable as ISO 8601 date format in the current
416 NfEvalDateFormat setting.
418 bool IsAcceptableIso8601();
421 #endif // INCLUDED_SVL_SOURCE_NUMBERS_ZFORFIND_HXX
423 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */