23#include <osl/diagnose.h>
33 :m_sPattern( OUString::createFromAscii( _pAsciiPattern ) )
42 sal_Int32 nPatternIndex = sExpression.indexOf(
'$' );
43 while ( nPatternIndex > -1 )
45 const OUString* pReplace =
nullptr;
46 switch ( sExpression[ nPatternIndex + 1 ] )
48 case '$': pReplace = &_rFieldDataSource;
break;
49 case '1': pReplace = &_rLHS;
break;
50 case '2': pReplace = &_rRHS;
break;
54 if ( pReplace ==
nullptr )
56 OSL_FAIL(
"ConditionalExpression::assembleExpression: illegal pattern!" );
60 sExpression = sExpression.replaceAt( nPatternIndex, 2, *pReplace );
61 nPatternIndex = sExpression.indexOf(
'$', nPatternIndex + pReplace->getLength() + 1 );
75 sMatchExpression = sMatchExpression.replaceAll(
u"$$", _rFieldDataSource);
77 static constexpr OUStringLiteral sLHSPattern(
u"$1" );
78 static constexpr OUStringLiteral sRHSPattern(
u"$2" );
79 sal_Int32 nLHSIndex( sMatchExpression.indexOf( sLHSPattern ) );
80 sal_Int32 nRHSIndex( sMatchExpression.indexOf( sRHSPattern ) );
83 OSL_ENSURE( sMatchExpression.indexOf( sLHSPattern, nLHSIndex + 1 ) == -1,
84 "ConditionalExpression::matchExpression: unsupported pattern (more than one LHS occurrence)!" );
85 OSL_ENSURE( sMatchExpression.indexOf( sRHSPattern, nRHSIndex + 1 ) == -1,
86 "ConditionalExpression::matchExpression: unsupported pattern (more than one RHS occurrence)!" );
88 OSL_ENSURE( ( nLHSIndex != -1 ) && ( ( nLHSIndex < nRHSIndex ) || ( nRHSIndex == -1 ) ),
89 "ConditionalExpression::matchExpression: no LHS, or an RHS preceding the LHS - this is not supported!" );
93 if ( sal_Int32(_rExpression.size()) < nLHSIndex )
95 const std::u16string_view sExprPart1( _rExpression.substr( 0, nLHSIndex ) );
96 const std::u16string_view sMatchExprPart1( sMatchExpression.subView( 0, nLHSIndex ) );
97 if ( sExprPart1 != sMatchExprPart1 )
103 bool bHaveRHS( nRHSIndex != -1 );
104 sal_Int32 nRightMostIndex( bHaveRHS ? nRHSIndex : nLHSIndex );
105 const std::u16string_view sMatchExprPart3( sMatchExpression.subView( nRightMostIndex + 2 ) );
106 if ( _rExpression.size() < sMatchExprPart3.size() )
109 const std::u16string_view sExprPart3( _rExpression.substr( _rExpression.size() - sMatchExprPart3.size() ) );
110 if ( sExprPart3 != sMatchExprPart3 )
117 _out_rLHS = _rExpression.substr( sExprPart1.size(), _rExpression.size() - sExprPart1.size() - sExprPart3.size() );
122 sal_Int32 nMatchExprPart2Start( nLHSIndex + sLHSPattern.getLength() );
123 std::u16string_view sMatchExprPart2 = sMatchExpression.subView(
124 nMatchExprPart2Start,
125 sMatchExpression.getLength() - nMatchExprPart2Start - sMatchExprPart3.size() - 2
128 const std::u16string_view sExpression( _rExpression.substr(
130 _rExpression.size() - sExprPart1.size() - sExprPart3.size()
133 size_t nPart2Index = sExpression.find( sMatchExprPart2 );
134 if ( nPart2Index == std::u16string_view::npos )
138 OSL_ENSURE( sExpression.find( sMatchExprPart2, nPart2Index + 1 ) == std::u16string_view::npos,
139 "ConditionalExpression::matchExpression: ambiguous matching!" );
152 _out_rLHS = sExpression.substr( 0, nPart2Index );
153 _out_rRHS = sExpression.substr( nPart2Index + sMatchExprPart2.size() );
166 _out_rCondExp[
eBetween ] = std::make_shared<ConditionalExpression>(
"AND( ( $$ ) >= ( $1 ); ( $$ ) <= ( $2 ) )" );
167 _out_rCondExp[
eNotBetween ] = std::make_shared<ConditionalExpression>(
"NOT( AND( ( $$ ) >= ( $1 ); ( $$ ) <= ( $2 ) ) )" );
168 _out_rCondExp[
eEqualTo ] = std::make_shared<ConditionalExpression>(
"( $$ ) = ( $1 )" );
169 _out_rCondExp[
eNotEqualTo ] = std::make_shared<ConditionalExpression>(
"( $$ ) <> ( $1 )" );
170 _out_rCondExp[
eGreaterThan ] = std::make_shared<ConditionalExpression>(
"( $$ ) > ( $1 )" );
171 _out_rCondExp[
eLessThan ] = std::make_shared<ConditionalExpression>(
"( $$ ) < ( $1 )" );
172 _out_rCondExp[
eGreaterOrEqual ] = std::make_shared<ConditionalExpression>(
"( $$ ) >= ( $1 )" );
173 _out_rCondExp[
eLessOrEqual ] = std::make_shared<ConditionalExpression>(
"( $$ ) <= ( $1 )" );
175 return _out_rCondExp.size();
static size_t getKnownConditionalExpressions(ConditionalExpressions &_out_rCondExp)
fills the given map with all ConditionalExpressions which we know
ConditionalExpression(const char *_pAsciiPattern)
OUString assembleExpression(const OUString &_rFieldDataSource, const OUString &_rLHS, const OUString &_rRHS) const
assembles an expression string from a field data source, and one or two operands
bool matchExpression(std::u16string_view _rExpression, const std::u16string_view _rFieldDataSource, OUString &_out_rLHS, OUString &_out_rRHS) const
matches the given expression string to the expression pattern represented by the object
const OUString m_sPattern
::std::map< ComparisonOperation, PConditionalExpression > ConditionalExpressions