diff options
author | Eike Rathke <erack@redhat.com> | 2016-02-18 17:57:41 +0100 |
---|---|---|
committer | Eike Rathke <erack@redhat.com> | 2016-02-19 00:10:14 +0100 |
commit | 92173313eae76d721580120b0de2c98fd83c5384 (patch) | |
tree | 8fce56e889044fa3ab2f5a415ef734f3570e3237 /sc | |
parent | 6e2de428a4910bb3c6dda45a59da09ed4a1889db (diff) |
prepare a SearchType detection fragment, tdf#72196
to be elaborated
Change-Id: Ie8c059b5b92c5d1454ce73870582815b20567c08
Diffstat (limited to 'sc')
-rw-r--r-- | sc/source/core/inc/interpre.hxx | 15 | ||||
-rw-r--r-- | sc/source/core/tool/interpr1.cxx | 52 |
2 files changed, 63 insertions, 4 deletions
diff --git a/sc/source/core/inc/interpre.hxx b/sc/source/core/inc/interpre.hxx index f6b5bd89751e..dda96dea4968 100644 --- a/sc/source/core/inc/interpre.hxx +++ b/sc/source/core/inc/interpre.hxx @@ -23,6 +23,7 @@ #include <math.h> #include <rtl/math.hxx> #include <rtl/ustring.hxx> +#include <unotools/textsearch.hxx> #include <formula/errorcodes.hxx> #include <formula/tokenarray.hxx> #include "scdll.hxx" @@ -130,7 +131,19 @@ public: /// If pDoc!=NULL the document options are taken into account and if /// RegularExpressions are disabled the function returns false regardless /// of the string content. - static bool MayBeRegExp( const OUString& rStr, const ScDocument* pDoc ); + static bool MayBeRegExp( const OUString& rStr, const ScDocument* pDoc, bool bIgnoreWildcards = false ); + + /** Could string be a wildcard (*,?,~) expression? + If pDoc!=NULL the document options are taken into account and if + Wildcards are disabled the function returns false regardless of the + string content. + */ + static bool MayBeWildcard( const OUString& rStr, const ScDocument* pDoc ); + + /** Detect if string should be used as regular expression or wildcard + expression or literal string. + */ + static utl::SearchParam::SearchType DetectSearchType( const OUString& rStr, const ScDocument* pDoc ); /// Fail safe division, returning an errDivisionByZero coded into a double /// if denominator is 0.0 diff --git a/sc/source/core/tool/interpr1.cxx b/sc/source/core/tool/interpr1.cxx index d43f5f5b1a26..45a00c7c10e9 100644 --- a/sc/source/core/tool/interpr1.cxx +++ b/sc/source/core/tool/interpr1.cxx @@ -8488,18 +8488,20 @@ void ScInterpreter::ScErrorType_ODF() PushNA(); } -bool ScInterpreter::MayBeRegExp( const OUString& rStr, const ScDocument* pDoc ) +bool ScInterpreter::MayBeRegExp( const OUString& rStr, const ScDocument* pDoc, bool bIgnoreWildcards ) { if ( pDoc && !pDoc->GetDocOptions().IsFormulaRegexEnabled() ) return false; if ( rStr.isEmpty() || (rStr.getLength() == 1 && !rStr.startsWith(".")) ) return false; // single meta characters can not be a regexp - static const sal_Unicode cre[] = { '.','*','+','?','[',']','^','$','\\','<','>','(',')','|', 0 }; + // First two characters are wildcard '?' and '*' characters. + static const sal_Unicode cre[] = { '?','*','+','.','[',']','^','$','\\','<','>','(',')','|', 0 }; + const sal_Unicode* const pre = bIgnoreWildcards ? cre + 2 : cre; const sal_Unicode* p1 = rStr.getStr(); sal_Unicode c1; while ( ( c1 = *p1++ ) != 0 ) { - const sal_Unicode* p2 = cre; + const sal_Unicode* p2 = pre; while ( *p2 ) { if ( c1 == *p2++ ) @@ -8509,6 +8511,50 @@ bool ScInterpreter::MayBeRegExp( const OUString& rStr, const ScDocument* pDoc ) return false; } +bool ScInterpreter::MayBeWildcard( const OUString& rStr, const ScDocument* pDoc ) +{ + /* TODO: doc options will need a new enum (or a second bool that takes + * precedence over regex?) */ + (void)pDoc; + + // Wildcards without '~' escape, if there are no wildcards then an escaped + // character does not make sense. + static const sal_Unicode cw[] = { '*','?', 0 }; + const sal_Unicode* p1 = rStr.getStr(); + sal_Unicode c1; + while ( ( c1 = *p1++ ) != 0 ) + { + const sal_Unicode* p2 = cw; + while ( *p2 ) + { + if ( c1 == *p2++ ) + return true; + } + } + return false; +} + +utl::SearchParam::SearchType ScInterpreter::DetectSearchType( const OUString& rStr, const ScDocument* pDoc ) +{ + if (pDoc) + { + bool bWildcardEnabled = false; /* TODO: obtain doc option */ + if (bWildcardEnabled) + return MayBeWildcard( rStr, nullptr) ? utl::SearchParam::SRCH_WILDCARD : utl::SearchParam::SRCH_NORMAL; + if (pDoc->GetDocOptions().IsFormulaRegexEnabled()) + return MayBeRegExp( rStr, nullptr) ? utl::SearchParam::SRCH_REGEXP : utl::SearchParam::SRCH_NORMAL; + } + else + { + /* TODO: obtain the global config for this rare case? */ + if (MayBeRegExp( rStr, nullptr, true)) + return utl::SearchParam::SRCH_REGEXP; + if (MayBeWildcard( rStr, nullptr)) + return utl::SearchParam::SRCH_WILDCARD; + } + return utl::SearchParam::SRCH_NORMAL; +} + static bool lcl_LookupQuery( ScAddress & o_rResultPos, ScDocument * pDoc, const ScQueryParam & rParam, const ScQueryEntry & rEntry ) { |