/* -*- 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 .
*/
/* It is not clear how we want timezones to be handled. Convert them to local * time isn't wanted, as it isn't done in any other place and timezone * information isn't stored anywhere. Ignoring them and pretending local time * may be wrong too and might not be what the user expects. Keep the input as * string so that no information is lost. * Anyway, defining NF_RECOGNIZE_ISO8601_TIMEZONES to 1 would be the way how it
* would work, together with the nTimezonePos handling in GetTimeRef(). */ #define NF_RECOGNIZE_ISO8601_TIMEZONES 0
const sal_Unicode cNoBreakSpace = 0xA0; const sal_Unicode cNarrowNoBreakSpace = 0x202F; constbool kDefaultEra = true; // Gregorian CE, positive year
/** * Only simple unsigned floating point values without any error detection, * decimal separator has to be '.'
*/ double ImpSvNumberInputScan::StringToDouble( std::u16string_view aStr, bool bForceFraction )
{
std::unique_ptr<char[]> bufInHeap;
constexpr int bufOnStackSize = 256; char bufOnStack[bufOnStackSize]; char* buf = bufOnStack; const sal_Int32 bufsize = aStr.size() + (bForceFraction ? 2 : 1); if (bufsize > bufOnStackSize)
{
bufInHeap = std::make_unique<char[]>(bufsize);
buf = bufInHeap.get();
} char* p = buf; if (bForceFraction)
*p++ = '.'; for (size_t nPos = 0; nPos < aStr.size(); ++nPos)
{
sal_Unicode c = aStr[nPos]; if (c == '.' || (c >= '0' && c <= '9'))
*p++ = static_cast<char>(c); else break;
}
*p = '\0';
return strtod_nolocale(buf, nullptr);
}
namespace {
/** * Splits up the input into numbers and strings for further processing * (by the Turing machine). * * Starting state = GetChar * ---------------+-------------------+-----------------------------+--------------- * Old State | Character read | Event | New state * ---------------+-------------------+-----------------------------+--------------- * GetChar | Number | Symbol = Character | GetValue * | Else | Symbol = Character | GetString * ---------------|-------------------+-----------------------------+--------------- * GetValue | Number | Symbol = Symbol + Character | GetValue * | Else | Dec(CharPos) | Stop * ---------------+-------------------+-----------------------------+--------------- * GetString | Number | Dec(CharPos) | Stop * | Else | Symbol = Symbol + Character | GetString * ---------------+-------------------+-----------------------------+---------------
*/ enum ScanState // States of the Turing machine
{
SsStop = 0,
SsStart = 1,
SsGetValue = 2,
SsGetString = 3
};
if (StringPtrContainsImpl( rWhat, rString.getStr(), nPos))
{
nPos += rWhat.getLength(); if (nPos == rString.getLength()) returntrue; // word at end of string
/* TODO: we COULD invoke bells and whistles word break iterator to find * the next boundary, but really ... this is called for date input, so * how many languages do not separate the day and month names in some
* form? */
// Check simple ASCII first before invoking i18n or anything else. const sal_Unicode c = rString[nPos];
// Common separating ASCII characters in date context. switch (c)
{ case' ': case'-': case'.': case'/': returntrue; default:
; // nothing
}
if (rtl::isAsciiAlphanumeric( c )) returnfalse; // Alpha or numeric is not word gap.
sal_Int32 nIndex = nPos;
rString.iterateCodePoints( &nIndex); if (nPos+1 < nIndex) returntrue; // Surrogate, assume these to be new words.
/** * jump over rWhat in rString at nPos
*/ inlinebool ImpSvNumberInputScan::SkipString( const OUString& rWhat, const OUString& rString, sal_Int32& nPos )
{ if ( StringContains( rWhat, rString, nPos ) )
{
nPos = nPos + rWhat.getLength(); returntrue;
} returnfalse;
}
/** * Recognizes exactly ,111 in {3} and {3,2} or ,11 in {3,2} grouping
*/ inlinebool ImpSvNumberInputScan::GetThousandSep( std::u16string_view rString,
sal_Int32& nPos,
sal_uInt16 nStringPos ) const
{ const OUString& rSep = mrCurrentLanguageData.GetNumThousandSep(); // Is it an ordinary space instead of a no-break space? bool bSpaceBreak = (rSep[0] == cNoBreakSpace || rSep[0] == cNarrowNoBreakSpace) &&
rString[0] == u' ' &&
rSep.getLength() == 1 && rString.size() == 1; if (!((rString == rSep || bSpaceBreak) && // nothing else
nStringPos < nStringsCnt - 1 && // safety first!
IsNum[ nStringPos + 1 ] )) // number follows
{ returnfalse; // no? => out
}
utl::DigitGroupingIterator aGrouping( mrCurrentLanguageData.GetLocaleData()->getDigitGrouping()); // Match ,### in {3} or ,## in {3,2} /* FIXME: this could be refined to match ,## in {3,2} only if ,##,## or * ,##,### and to match ,### in {3,2} only if it's the last. However, * currently there is no track kept where group separators occur. In {3,2} * #,###,### and #,##,## would be valid input, which maybe isn't even bad
* for #,###,###. Other combinations such as #,###,## maybe not. */
sal_Int32 nLen = sStrArray[ nStringPos + 1 ].getLength(); if (nLen == aGrouping.get() || // with 3 (or so) digits
nLen == aGrouping.advance().get() || // or with 2 (or 3 or so) digits
nPosThousandString == nStringPos + 1 ) // or concatenated
{
nPos = nPos + rSep.getLength(); returntrue;
} returnfalse;
}
/** * Conversion of text to logical value * "true" => 1: * "false"=> -1: * else => 0:
*/ short ImpSvNumberInputScan::GetLogical( std::u16string_view rString ) const
{ short res;
const ImpSvNumberformatScan* pFS = mrCurrentLanguageData.GetFormatScanner(); if ( rString == pFS->GetTrueString() )
{
res = 1;
} elseif ( rString == pFS->GetFalseString() )
{
res = -1;
} else
{
res = 0;
} return res;
}
/** * Converts a string containing a month name (JAN, January) at nPos into the * month number (negative if abbreviated), returns 0 if nothing found
*/ short ImpSvNumberInputScan::GetMonth( const OUString& rString, sal_Int32& nPos )
{ short res = 0; // no month found
if (rString.getLength() > nPos) // only if needed
{ if ( !bTextInitialized )
{
InitText();
}
sal_Int16 nMonths = mrCurrentLanguageData.GetCalendar()->getNumberOfMonthsInYear(); for ( sal_Int16 i = 0; i < nMonths; i++ )
{ if ( bScanGenitiveMonths && StringContainsWord( pUpperGenitiveMonthText[i], rString, nPos ) )
{ // genitive full names first
nPos = nPos + pUpperGenitiveMonthText[i].getLength();
res = i + 1; break; // for
} elseif ( bScanGenitiveMonths && StringContainsWord( pUpperGenitiveAbbrevMonthText[i], rString, nPos ) )
{ // genitive abbreviated
nPos = nPos + pUpperGenitiveAbbrevMonthText[i].getLength();
res = sal::static_int_cast< short >(-(i+1)); // negative break; // for
} elseif ( bScanPartitiveMonths && StringContainsWord( pUpperPartitiveMonthText[i], rString, nPos ) )
{ // partitive full names
nPos = nPos + pUpperPartitiveMonthText[i].getLength();
res = i+1; break; // for
} elseif ( bScanPartitiveMonths && StringContainsWord( pUpperPartitiveAbbrevMonthText[i], rString, nPos ) )
{ // partitive abbreviated
nPos = nPos + pUpperPartitiveAbbrevMonthText[i].getLength();
res = sal::static_int_cast< short >(-(i+1)); // negative break; // for
} elseif ( StringContainsWord( pUpperMonthText[i], rString, nPos ) )
{ // noun full names
nPos = nPos + pUpperMonthText[i].getLength();
res = i+1; break; // for
} elseif ( StringContainsWord( pUpperAbbrevMonthText[i], rString, nPos ) )
{ // noun abbreviated
nPos = nPos + pUpperAbbrevMonthText[i].getLength();
res = sal::static_int_cast< short >(-(i+1)); // negative break; // for
} elseif (i == 2 && mrCurrentLanguageData.GetLanguageTag().getLanguage() == "de")
{ if (pUpperAbbrevMonthText[i] == u"M\u00C4R" && StringContainsWord( u"MRZ"_ustr, rString, nPos))
{ // Accept MRZ for MÄR
nPos = nPos + 3;
res = sal::static_int_cast< short >(-(i+1)); // negative break; // for
} elseif (pUpperAbbrevMonthText[i] == "MRZ" && StringContainsWord( u"M\u00C4R"_ustr, rString, nPos))
{ // And vice versa, accept MÄR for MRZ
nPos = nPos + 3;
res = sal::static_int_cast< short >(-(i+1)); // negative break; // for
}
} elseif (i == 8)
{ // This assumes the weirdness is applicable to all locales. // It is the case for at least en-* and de-* locales. if (pUpperAbbrevMonthText[i] == "SEPT" && StringContainsWord( u"SEP"_ustr, rString, nPos))
{ // #102136# The correct English form of month September abbreviated is // SEPT, but almost every data contains SEP instead.
nPos = nPos + 3;
res = sal::static_int_cast< short >(-(i+1)); // negative break; // for
} elseif (pUpperAbbrevMonthText[i] == "SEP" && StringContainsWord( u"SEPT"_ustr, rString, nPos))
{ // And vice versa, accept SEPT for SEP
nPos = nPos + 4;
res = sal::static_int_cast< short >(-(i+1)); // negative break; // for
}
}
} if (!res)
{ // Brutal hack for German locales that know "Januar" or "Jänner". /* TODO: add alternative month names to locale data? if there are
* more languages... */ const LanguageTag& rLanguageTag = mrCurrentLanguageData.GetLanguageTag(); if (rLanguageTag.getLanguage() == "de")
{ if (rLanguageTag.getCountry() == "AT")
{ // Locale data has Jänner/Jän
assert(pUpperMonthText[0] == u"J\u00C4NNER"); if (StringContainsWord( u"JANUAR"_ustr, rString, nPos))
{
nPos += 6;
res = 1;
} elseif (StringContainsWord( u"JAN"_ustr, rString, nPos))
{
nPos += 3;
res = -1;
}
} else
{ // Locale data has Januar/Jan
assert(pUpperMonthText[0] == "JANUAR"); if (StringContainsWord( u"J\u00C4NNER"_ustr, rString, nPos))
{
nPos += 6;
res = 1;
} elseif (StringContainsWord( u"J\u00C4N"_ustr, rString, nPos))
{
nPos += 3;
res = -1;
}
}
}
}
}
return res;
}
/** * Converts a string containing a DayOfWeek name (Mon, Monday) at nPos into the * DayOfWeek number + 1 (negative if abbreviated), returns 0 if nothing found
*/ int ImpSvNumberInputScan::GetDayOfWeek( const OUString& rString, sal_Int32& nPos )
{ int res = 0; // no day found
if (rString.getLength() > nPos) // only if needed
{ if ( !bTextInitialized )
{
InitText();
}
sal_Int16 nDays = mrCurrentLanguageData.GetCalendar()->getNumberOfDaysInWeek(); for ( sal_Int16 i = 0; i < nDays; i++ )
{ if ( StringContainsWord( pUpperDayText[i], rString, nPos ) )
{ // full names first
nPos = nPos + pUpperDayText[i].getLength();
res = i + 1; break; // for
} if ( StringContainsWord( pUpperAbbrevDayText[i], rString, nPos ) )
{ // abbreviated
nPos = nPos + pUpperAbbrevDayText[i].getLength();
res = -(i + 1); // negative break; // for
}
}
}
return res;
}
/** * Reading a currency symbol * '$' => true * else => false
*/ bool ImpSvNumberInputScan::GetCurrency( const OUString& rString, sal_Int32& nPos )
{ if ( rString.getLength() > nPos )
{ if ( !aUpperCurrSymbol.getLength() )
{ // If no format specified the currency of the currently active locale.
LanguageType eLang = (mpFormat ? mpFormat->GetLanguage() :
mrCurrentLanguageData.GetLocaleData()->getLanguageTag().getLanguageType());
aUpperCurrSymbol = mrCurrentLanguageData.GetCharClass()->uppercase(
SvNumberFormatter::GetCurrencyEntry( eLang ).GetSymbol() );
} if ( StringContains( aUpperCurrSymbol, rString, nPos ) )
{
nPos = nPos + aUpperCurrSymbol.getLength(); returntrue;
} if ( mpFormat )
{
OUString aSymbol, aExtension; if ( mpFormat->GetNewCurrencySymbol( aSymbol, aExtension ) )
{ if ( aSymbol.getLength() <= rString.getLength() - nPos )
{
aSymbol = mrCurrentLanguageData.GetCharClass()->uppercase(aSymbol); if ( StringContains( aSymbol, rString, nPos ) )
{
nPos = nPos + aSymbol.getLength(); returntrue;
}
}
}
}
}
returnfalse;
}
/** * Reading the time period specifier (AM/PM) for the 12 hour clock * * Returns: * "AM" or "PM" => true * else => false * * nAmPos: * "AM" => 1 * "PM" => -1 * else => 0
*/ bool ImpSvNumberInputScan::GetTimeAmPm( const OUString& rString, sal_Int32& nPos )
{
/** * i counts string portions, j counts numbers thereof. * It should had been called SkipNumber instead.
*/ inlinebool ImpSvNumberInputScan::GetNextNumber( sal_uInt16& i, sal_uInt16& j ) const
{ if ( i < nStringsCnt && IsNum[i] )
{
j++;
i++; returntrue;
} returnfalse;
}
bool ImpSvNumberInputScan::GetTimeRef( double& fOutNumber,
sal_uInt16 nIndex, // j-value of the first numeric time part of input, default 0
sal_uInt16 nCnt, // count of numeric time parts
SvNumInputOptions eInputOptions
) const
{ bool bRet = true;
sal_Int32 nHour;
sal_Int32 nMinute = 0;
sal_Int32 nSecond = 0; double fSecond100 = 0.0;
sal_uInt16 nStartIndex = nIndex;
sal_Int32 nLen = sStrArray[nNums[nIndex]].getLength(); // 16-bit integer year width can have 5 digits, allow for one additional // leading zero as convention. if (nLen <= 6)
{
nYear = static_cast<sal_uInt16>(sStrArray[nNums[nIndex]].toInt32()); // A year in another, not Gregorian CE era is never expanded. // A year < 100 entered with at least 3 digits with leading 0 is taken // as is without expansion. if (mbEraCE == kDefaultEra && nYear < 100 && nLen < 3)
{
nYear = SvNumberFormatter::ExpandTwoDigitYear( nYear, nYear2000 );
}
}
return nYear;
}
bool ImpSvNumberInputScan::MayBeIso8601()
{ if (nMayBeIso8601 == 0)
{
nMayBeIso8601 = 1;
sal_Int32 nLen = ((nNumericsCnt >= 1 && nNums[0] < nStringsCnt) ? sStrArray[nNums[0]].getLength() : 0); if (nLen)
{
sal_Int32 n; if (nNumericsCnt >= 3 && nNums[2] < nStringsCnt &&
sStrArray[nNums[0]+1] == "-" && // separator year-month
(n = sStrArray[nNums[1]].toInt32()) >= 1 && n <= 12 && // month
sStrArray[nNums[1]+1] == "-" && // separator month-day
(n = sStrArray[nNums[2]].toInt32()) >= 1 && n <= 31) // day
{ // Year (nNums[0]) value not checked, may be anything, but // length (number of digits) is checked.
nMayBeIso8601 = (nLen >= 4 ? 4 : (nLen == 3 ? 3 : (nLen > 0 ? 2 : 1)));
}
}
} return nMayBeIso8601 > 1;
}
bool ImpSvNumberInputScan::CanForceToIso8601( DateOrder eDateOrder )
{ int nCanForceToIso8601 = 0; if (!MayBeIso8601())
{ returnfalse;
} elseif (nMayBeIso8601 >= 3)
{ returntrue; // at least 3 digits in year
} else
{ if (eDateOrder == DateOrder::Invalid)
{ // As if any of the cases below can be applied, but only if a // locale dependent date pattern was not matched. if ((GetDatePatternNumbers() == nNumericsCnt) && IsDatePatternNumberOfType(0,'Y')) returnfalse;
eDateOrder = GetDateOrder();
}
// No date pattern matched at all can be forced to ISO 8601 here as is. if (GetDatePatternNumbers() == 0) returntrue;
nCanForceToIso8601 = 1;
}
sal_Int32 n; switch (eDateOrder)
{ case DateOrder::DMY: // "day" value out of range => ISO 8601 year
n = sStrArray[nNums[0]].toInt32(); if (n < 1 || n > 31)
{
nCanForceToIso8601 = 2;
} break; case DateOrder::MDY: // "month" value out of range => ISO 8601 year
n = sStrArray[nNums[0]].toInt32(); if (n < 1 || n > 12)
{
nCanForceToIso8601 = 2;
} break; case DateOrder::YMD: // always possible
nCanForceToIso8601 = 2; break; default: break;
} return nCanForceToIso8601 > 1;
}
bool ImpSvNumberInputScan::MayBeMonthDate()
{ if (nMayBeMonthDate == 0)
{
nMayBeMonthDate = 1; if (nNumericsCnt >= 2 && nNums[1] < nStringsCnt)
{ // "-Jan-" const OUString& rM = sStrArray[ nNums[ 0 ] + 1 ]; if (rM.getLength() >= 3 && rM[0] == '-' && rM[ rM.getLength() - 1] == '-')
{ // Check year length assuming at least 3 digits (including // leading zero). Two digit years 1..31 are out of luck here // and may be taken as day of month. bool bYear1 = (sStrArray[nNums[0]].getLength() >= 3); bool bYear2 = (sStrArray[nNums[1]].getLength() >= 3);
sal_Int32 n; bool bDay1 = !bYear1; if (bDay1)
{
n = sStrArray[nNums[0]].toInt32();
bDay1 = n >= 1 && n <= 31;
} bool bDay2 = !bYear2; if (bDay2)
{
n = sStrArray[nNums[1]].toInt32();
bDay2 = n >= 1 && n <= 31;
}
if (bDay1 && !bDay2)
{
nMayBeMonthDate = 2; // dd-month-yy
} elseif (!bDay1 && bDay2)
{
nMayBeMonthDate = 3; // yy-month-dd
} elseif (bDay1 && bDay2)
{ // Ambiguous ##-MMM-## date, but some big vendor's database // reports write this crap, assume this always to be
nMayBeMonthDate = 2; // dd-month-yy
}
}
}
} return nMayBeMonthDate > 1;
}
/** If a string is a separator plus '-' minus sign preceding a 'Y' year in a date pattern at position nPat.
*/ staticbool lcl_IsSignedYearSep( std::u16string_view rStr, std::u16string_view rPat, sal_Int32 nPat )
{ bool bOk = false;
sal_Int32 nLen = rStr.size(); if (nLen > 1 && rStr[nLen-1] == '-')
{
--nLen; if (nPat + nLen < static_cast<sal_Int32>(rPat.size()) && rPat[nPat+nLen] == 'Y')
{ // Signed year is possible.
bOk = (rPat.find( rStr.substr( 0, nLen), nPat) == static_cast<size_t>(nPat));
}
} return bOk;
}
/** Length of separator usually is 1 but theoretically could be anything. */ static sal_Int32 lcl_getPatternSeparatorLength( std::u16string_view rPat, sal_Int32 nPat )
{
sal_Int32 nSep = nPat;
sal_Unicode c; while (nSep < static_cast<sal_Int32>(rPat.size()) && (c = rPat[nSep]) != 'D' && c != 'M' && c != 'Y')
++nSep; return nSep - nPat;
}
bool ImpSvNumberInputScan::IsAcceptedDatePattern( sal_uInt16 nStartPatternAt )
{ if (nAcceptedDatePattern >= -1)
{ return (nAcceptedDatePattern >= 0);
} if (!nNumericsCnt)
{
nAcceptedDatePattern = -1;
} elseif (!sDateAcceptancePatterns.hasElements())
{ // The current locale is the format's locale, if a format is present. const NfEvalDateFormat eEDF = mrCurrentLanguageData.GetEvalDateFormat(); if (!mpFormat || eEDF == NF_EVALDATEFORMAT_FORMAT || mpFormat->GetLanguage() == mrCurrentLanguageData.GetIniLanguage())
{
sDateAcceptancePatterns = mrCurrentLanguageData.GetLocaleData()->getDateAcceptancePatterns();
} else
{
OnDemandLocaleDataWrapper& xLocaleData = mrCurrentLanguageData.GetOnDemandLocaleDataWrapper(
SvNFLanguageData::InputScannerPrivateAccess()); const LanguageTag aSaveLocale( xLocaleData->getLanguageTag() );
assert(mpFormat->GetLanguage() == aSaveLocale.getLanguageType()); // prerequisite // Obtain formatter's locale's (e.g. system) patterns.
xLocaleData.changeLocale( LanguageTag( mrCurrentLanguageData.GetIniLanguage())); const css::uno::Sequence<OUString> aLocalePatterns( xLocaleData->getDateAcceptancePatterns()); // Reset to format's locale.
xLocaleData.changeLocale( aSaveLocale); // When concatenating don't care about duplicates, combining // weeding those out reallocs yet another time and probably doesn't // take less time than looping over two additional patterns below... switch (eEDF)
{ case NF_EVALDATEFORMAT_FORMAT:
assert(!"shouldn't reach here"); break; case NF_EVALDATEFORMAT_INTL:
sDateAcceptancePatterns = aLocalePatterns; break; case NF_EVALDATEFORMAT_INTL_FORMAT:
sDateAcceptancePatterns = comphelper::concatSequences(
aLocalePatterns,
xLocaleData->getDateAcceptancePatterns()); break; case NF_EVALDATEFORMAT_FORMAT_INTL:
sDateAcceptancePatterns = comphelper::concatSequences(
xLocaleData->getDateAcceptancePatterns(),
aLocalePatterns); break;
}
}
SAL_WARN_IF( !sDateAcceptancePatterns.hasElements(), "svl.numbers", "ImpSvNumberInputScan::IsAcceptedDatePattern: no date acceptance patterns");
nAcceptedDatePattern = (sDateAcceptancePatterns.hasElements() ? -2 : -1);
}
for (sal_Int32 nPattern=0; nPattern < sDateAcceptancePatterns.getLength(); ++nPattern)
{ const OUString& rPat = sDateAcceptancePatterns[nPattern]; if (rPat.getLength() == 3)
{ // Ignore a pattern that would match numeric input with decimal // separator. It may had been read from configuration or resulted // from the locales' patterns concatenation above. if ( rPat[1] == mrCurrentLanguageData.GetLocaleData()->getNumDecimalSep().toChar()
|| rPat[1] == mrCurrentLanguageData.GetLocaleData()->getNumDecimalSepAlt().toChar())
{
SAL_WARN("svl.numbers", "ignoring date acceptance pattern with decimal separator ambiguity: " << rPat); continue; // for, next pattern
}
}
sal_uInt16 nNext = nDatePatternStart;
nDatePatternNumbers = 0; bool bOk = true;
sal_Int32 nPat = 0; for ( ; nPat < rPat.getLength() && bOk && nNext < nStringsCnt; ++nPat, ++nNext)
{ const sal_Unicode c = rPat[nPat]; switch (c)
{ case'Y': case'M': case'D':
bOk = IsNum[nNext]; if (bOk && (c == 'M' || c == 'D'))
{ // Check the D and M cases for plausibility. This also // prevents recognition of date instead of number with a // numeric group input if date separator is identical to // group separator, for example with D.M as a pattern and // #.### as a group.
sal_Int32 nMaxLen, nMaxVal; switch (c)
{ case'M':
nMaxLen = 2;
nMaxVal = nMonthsInYear; break; case'D':
nMaxLen = 2;
nMaxVal = 31; break; default: // This merely exists against // -Werror=maybe-uninitialized, which is nonsense // after the (c == 'M' || c == 'D') check above, // but ...
nMaxLen = 2;
nMaxVal = 31;
}
bOk = (sStrArray[nNext].getLength() <= nMaxLen); if (bOk)
{
sal_Int32 nNum = sStrArray[nNext].toInt32();
bOk = (1 <= nNum && nNum <= nMaxVal);
}
} if (bOk)
++nDatePatternNumbers; break; default:
bOk = !IsNum[nNext]; if (bOk)
{ const sal_Int32 nSepLen = lcl_getPatternSeparatorLength( rPat, nPat); // Non-numeric input must match separator exactly to be // accepted as such. const sal_Int32 nLen = sStrArray[nNext].getLength();
bOk = (nLen == nSepLen && rPat.indexOf( sStrArray[nNext], nPat) == nPat); if (bOk)
{
nPat += nLen - 1;
} elseif ((bOk = lcl_IsSignedYearSep( sStrArray[nNext], rPat, nPat)))
{
nPat += nLen - 2;
} elseif (nPat + nLen > rPat.getLength() && sStrArray[nNext][ nLen - 1 ] == ' ')
{ usingnamespace comphelper::string; // Trailing blanks in input.
OUStringBuffer aBuf(sStrArray[nNext]);
aBuf.stripEnd(); // Expand again in case of pattern "M. D. " and // input "M. D. ", maybe fetched far, but...
padToLength(aBuf, rPat.getLength() - nPat, ' ');
bOk = (rPat.indexOf( aBuf, nPat) == nPat); if (bOk)
{
nPat += aBuf.getLength() - 1;
}
}
} break;
}
} if (bOk)
{ // Check for trailing characters mismatch. if (nNext < nStringsCnt)
{ // Pattern end but not input end. // A trailing blank may be part of the current pattern input, // if pattern is "D.M." and input is "D.M. hh:mm" last was // ". ", or may be following the current pattern input, if // pattern is "D.M" and input is "D.M hh:mm" last was "M".
sal_Int32 nPos = 0;
sal_uInt16 nCheck; if (nPat > 0 && nNext > 0)
{ // nPat is one behind after the for loop.
sal_Int32 nPatCheck = nPat - 1; switch (rPat[nPatCheck])
{ case'Y': case'M': case'D':
nCheck = nNext; break; default:
{
nCheck = nNext - 1; // Advance position in input to match length of // non-YMD (separator) characters in pattern.
sal_Unicode c; do
{
++nPos;
c = rPat[--nPatCheck];
} while (c != 'Y' && c != 'M' && c != 'D' && nPatCheck > 0);
}
}
} else
{
nCheck = nNext;
} if (!IsNum[nCheck])
{ // Trailing (or separating if time follows) blanks are ok. // No blank and a following number is not. constbool bBlanks = SkipBlanks( sStrArray[nCheck], nPos); if (nPos == sStrArray[nCheck].getLength() && (bBlanks || !IsNum[nNext]))
{
nAcceptedDatePattern = nPattern; returntrue;
}
}
} elseif (nPat == rPat.getLength())
{ // Input end and pattern end => match.
nAcceptedDatePattern = nPattern; returntrue;
} // else Input end but not pattern end, no match.
}
}
nAcceptedDatePattern = -1; returnfalse;
}
bool ImpSvNumberInputScan::SkipDatePatternSeparator( sal_uInt16 nParticle, sal_Int32 & rPos, bool & rSignedYear )
{ // If not initialized yet start with first number, if any. if (!IsAcceptedDatePattern( nNumericsCnt ? nNums[0] : 0 ))
{ returnfalse;
} if (nParticle < nDatePatternStart || nParticle >= nStringsCnt || IsNum[nParticle])
{ returnfalse;
}
sal_uInt16 nNext = nDatePatternStart; const OUString& rPat = sDateAcceptancePatterns[nAcceptedDatePattern]; for (sal_Int32 nPat = 0; nPat < rPat.getLength() && nNext < nStringsCnt; ++nPat, ++nNext)
{ switch (rPat[nPat])
{ case'Y': case'M': case'D': break; default: if (nNext == nParticle)
{ const sal_Int32 nSepLen = lcl_getPatternSeparatorLength( rPat, nPat); const sal_Int32 nLen = sStrArray[nNext].getLength(); bool bOk = (nLen == nSepLen && rPat.indexOf( sStrArray[nNext], nPat) == nPat); if (!bOk)
{
bOk = lcl_IsSignedYearSep( sStrArray[nNext], rPat, nPat); if (bOk)
rSignedYear = true;
} if (!bOk && (nPat + nLen > rPat.getLength() && sStrArray[nNext][ nLen - 1 ] == ' '))
{ // The same ugly trailing blanks check as in // IsAcceptedDatePattern(). usingnamespace comphelper::string;
OUStringBuffer aBuf(sStrArray[nNext]);
aBuf.stripEnd();
padToLength(aBuf, rPat.getLength() - nPat, ' ');
bOk = (rPat.indexOf(aBuf, nPat) == nPat);
} if (bOk)
{
rPos = nLen; // yes, set, not add! returntrue;
} else returnfalse;
}
nPat += sStrArray[nNext].getLength() - 1; break;
}
} returnfalse;
}
sal_uInt16 ImpSvNumberInputScan::GetDatePatternNumbers()
{ // If not initialized yet start with first number, if any. if (!IsAcceptedDatePattern( nNumericsCnt ? nNums[0] : 0 ))
{ return 0;
} return nDatePatternNumbers;
}
LongDateOrder eLDO; const sal_uInt32 nExactDateOrder = (bFormatTurn ? mpFormat->GetExactDateOrder() : 0); if (!nExactDateOrder)
eLDO = pLoc->getLongDateOrder(); elseif ((((nExactDateOrder >> 16) & 0xff) == 'Y') && ((nExactDateOrder & 0xff) == 'D'))
eLDO = LongDateOrder::YMD; elseif ((((nExactDateOrder >> 16) & 0xff) == 'D') && ((nExactDateOrder & 0xff) == 'Y'))
eLDO = LongDateOrder::DMY; else
eLDO = pLoc->getLongDateOrder(); if (eLDO != LongDateOrder::YMD && eLDO != LongDateOrder::DMY)
{ switch (eDateOrder)
{ case DateOrder::YMD:
eLDO = LongDateOrder::YMD; break; case DateOrder::DMY:
eLDO = LongDateOrder::DMY; break; default:
; // nothing, not a date
}
} elseif (eLDO == LongDateOrder::DMY && eDateOrder == DateOrder::YMD)
{ // Check possible order and maybe switch. if (!ImplGetDay(0) && ImplGetDay(1))
eLDO = LongDateOrder::YMD;
} elseif (eLDO == LongDateOrder::YMD && eDateOrder == DateOrder::DMY)
{ // Check possible order and maybe switch. if (!ImplGetDay(1) && ImplGetDay(0))
eLDO = LongDateOrder::DMY;
} return eLDO;
}
bool ImpSvNumberInputScan::GetDateRef( double& fDays, sal_uInt16& nCounter )
{ usingnamespace ::com::sun::star::i18n;
NfEvalDateFormat eEDF; int nFormatOrder; if ( mpFormat && (mpFormat->GetType() & SvNumFormatType::DATE) )
{
eEDF = mrCurrentLanguageData.GetEvalDateFormat(); switch ( eEDF )
{ case NF_EVALDATEFORMAT_INTL : case NF_EVALDATEFORMAT_FORMAT :
nFormatOrder = 1; // only one loop break; default:
nFormatOrder = 2; if ( nMatchedAllStrings )
{
eEDF = NF_EVALDATEFORMAT_FORMAT_INTL; // we have a complete match, use it
}
}
} else
{
eEDF = NF_EVALDATEFORMAT_INTL;
nFormatOrder = 1;
} bool res = true;
const LocaleDataWrapper* pLoc = mrCurrentLanguageData.GetLocaleData();
CalendarWrapper* pCal = mrCurrentLanguageData.GetCalendar(); const DateTime aToday{ DateTime( Date( Date::SYSTEM))}; for ( int nTryOrder = 1; nTryOrder <= nFormatOrder; nTryOrder++ )
{
pCal->setGregorianDateTime( aToday); // today
OUString aOrgCalendar; // empty => not changed yet
DateOrder DateFmt; bool bFormatTurn; switch ( eEDF )
{ case NF_EVALDATEFORMAT_INTL :
bFormatTurn = false;
DateFmt = GetDateOrder(); break; case NF_EVALDATEFORMAT_FORMAT :
bFormatTurn = true;
DateFmt = mpFormat->GetDateOrder(); break; case NF_EVALDATEFORMAT_INTL_FORMAT : if ( nTryOrder == 1 )
{
bFormatTurn = false;
DateFmt = GetDateOrder();
} else
{
bFormatTurn = true;
DateFmt = mpFormat->GetDateOrder();
} break; case NF_EVALDATEFORMAT_FORMAT_INTL : if ( nTryOrder == 2 )
{
bFormatTurn = false;
DateFmt = GetDateOrder();
} else
{
bFormatTurn = true; // Even if the format pattern is to be preferred, the input may // have matched a pattern of the current locale, which then // again is to be preferred. Both date orders can be different // so we need to obtain the actual match. For example ISO // YYYY-MM-DD format vs locale's DD.MM.YY input. // If no pattern was matched, obtain from format. // Note that patterns may have been constructed from the // format's locale and prepended to the current locale's // patterns, it doesn't necessarily mean a current locale's // pattern was matched, but may if the format's locale's // patterns didn't match, which were tried first.
DateFmt = GetDateOrder(true);
} break; default:
SAL_WARN( "svl.numbers", "ImpSvNumberInputScan::GetDateRef: unknown NfEvalDateFormat" );
DateFmt = DateOrder::YMD;
bFormatTurn = false;
} if ( bFormatTurn )
{ /* TODO: We are currently not able to fully support a switch to another calendar during input for the following reasons: 1. We do have a problem if both (locale's default and format's) calendars define the same YMD order and use the same date separator, there is no way to distinguish between them if the input results in valid calendar input for both calendars. How to solve? Would NfEvalDateFormat be sufficient? Should it always be set to NF_EVALDATEFORMAT_FORMAT_INTL and thus the format's calendar be preferred? This could be confusing if a Calc cell was formatted different to the locale's default and has no content yet, then the user has no clue about the format or calendar being set. 2. In Calc cell edit mode a date is always displayed and edited using the default edit format of the default calendar (normally being Gregorian). If input was ambiguous due to issue #1 we'd need a mechanism to tell that a date was edited and not newly entered. Not feasible. Otherwise we'd need a mechanism to use a specific edit format with a specific calendar according to the format set. 3. For some calendars like Japanese Gengou we'd need era input, which isn't implemented at all. Though this is a rare and special case, forcing a calendar dependent edit format as suggested in item #2 might require era input, if it shouldn't result in a fallback to Gregorian calendar. 4. Last and least: the GetMonth() method currently only matches month names of the default calendar. Alternating month names of the actual format's calendar would have to be implemented. No problem.
res = true;
nCounter = 0; // For incomplete dates, always assume first day of month if not specified.
pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, 1 );
switch (nNumericsCnt) // count of numbers in string
{ case 0: // none if (nMonthPos) // only month (Jan)
{
pCal->setValue( CalendarFieldIndex::MONTH, std::abs(nMonth)-1 );
} else
{
res = false;
} break;
case 1: // only one number
nCounter = 1; switch (nMonthPos) // where is the month
{ case 0: // not found
{ // If input matched a date pattern, use the pattern // to determine if it is a day, month or year. The // pattern should have only one single value then, // 'D-', 'M-' or 'Y-'. If input did not match a // pattern assume the usual day of current month.
sal_uInt32 nDateOrder = (bFormatTurn ?
mpFormat->GetExactDateOrder() :
GetDatePatternOrder()); switch (nDateOrder)
{ case'Y':
pCal->setValue( CalendarFieldIndex::YEAR, ImplGetYear(0) ); break; case'M':
pCal->setValue( CalendarFieldIndex::MONTH, ImplGetMonth(0) ); break; case'D': default:
pCal->setValue( CalendarFieldIndex::DAY_OF_MONTH, ImplGetDay(0) ); break;
} break;
} case 1: // month at the beginning (Jan 01)
pCal->setValue( CalendarFieldIndex::MONTH, std::abs(nMonth)-1 ); switch (DateFmt)
{ case DateOrder::MDY: case DateOrder::YMD: if (sal_uInt16 day = ImplGetDay(0); day > 0 && day <= 32) // Why 32?
{
pCal->setValue(CalendarFieldIndex::DAY_OF_MONTH, day);
--> --------------------
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.