Update contrib.
2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
18 #include "RegularExpression.h"
20 #define KNumOfRecognisedChars 13
22 // Panic is implemented in NumberGrouping.cpp
23 GLDEF_C void Panic(TNumberGroupingPanic aPanic);
25 CRegularExpression::CRegularExpression()
29 CRegularExpression* CRegularExpression::NewL(RPointerArray<TDesC>* aPatterns)
31 CRegularExpression* s = NewLC(aPatterns);
36 CRegularExpression* CRegularExpression::NewLC(RPointerArray<TDesC>* aPatterns)
38 CRegularExpression* s = new(ELeave)CRegularExpression();
39 CleanupStack::PushL(s);
40 s->ConstructL(aPatterns);
44 void CRegularExpression::ConstructL(RPointerArray<TDesC>* aPatterns)
46 GenerateStateTablesL(aPatterns);
49 CRegularExpression::~CRegularExpression()
51 for(TInt i = 0; i < iStateMachines.Count(); i++)
52 delete iStateMachines[i];
54 iStateMachines.Close();
57 TInt CRegularExpression::Search(const TDesC& aString)
59 return SearchFrom(0, aString);
62 TInt CRegularExpression::SearchFrom(TInt aIndex, const TDesC& aString)
64 for(TInt i = aIndex; i < iStateMachines.Count(); ++i)
66 if(iStateMachines[i]->Matches(aString))
74 void CRegularExpression::GenerateStateTablesL(RPointerArray<TDesC>* aPatterns)
76 for(TInt nPatternCount = 0; nPatternCount < aPatterns->Count(); ++nPatternCount)
78 const TDesC& desPattern = *((*aPatterns)[nPatternCount]) ; // I know it looks horrific, but it makes the
79 // rest of the method _so_ much easier to read
81 CStateMachine* StateMachine = CStateMachine::NewLC(KNumOfRecognisedChars, desPattern.Length());
82 User::LeaveIfError(iStateMachines.Append(StateMachine));
83 CleanupStack::Pop(); // now the ownership transfers to iStateMachines
85 // parse the desPattern
94 ELookingForCloseBracket
97 TParseState parseState = EAny;
99 TInt nPatternIndex = 0;
105 TBuf<20> bufParsingError = _L("Parsing syntax error");
107 while(nPatternIndex < desPattern.Length() )
109 TChar cChar = desPattern[nPatternIndex];
110 TBool bEndOfParse = EFalse;
112 if(nPatternIndex == desPattern.Length() - 1) // end of the parse
130 iStateMachines[nPatternCount]->AddStateTransistionL(cChar, nState, bEndOfParse?
135 case EOpenBracketFound:
136 bufChoice.Append(cChar);
137 parseState = EChoiceOrRange;
141 bufChoice.Append(cChar);
142 parseState = EChoice;
145 bufChoice.Append(cChar);
146 parseState = ELookingForCloseBracket;
148 //case ELookingForCloseBracket:
150 User::Panic(bufParsingError, KErrSyntaxError);
159 parseState = EOpenBracketFound;
161 //case EOpenBracketFound:
162 //case EChoiceOrRange:
165 //case ELookingForCloseBracket:
167 User::Panic(bufParsingError, KErrSyntaxError);
177 for(TInt i = 0; i < bufChoice.Length(); ++i)
179 TChar cChar = bufChoice[i];
181 iStateMachines[nPatternCount]->AddStateTransistionL(cChar.GetNumericValue(),
192 case ELookingForCloseBracket:
194 TChar cTopOfRange = bufChoice[1];
195 TChar cBottomOfRange = bufChoice[0];
197 TInt nTopOfRange = cTopOfRange.GetNumericValue(); // if they're not there then something
198 TInt nBottomOfRange = cBottomOfRange.GetNumericValue(); // is seriously amiss...
200 if(nTopOfRange < nBottomOfRange)
201 User::Panic(bufParsingError, KErrSyntaxError);
203 for(TInt i = nBottomOfRange; i <= nTopOfRange; ++i)
205 iStateMachines[nPatternCount]->AddStateTransistionL(i, nState, bEndOfParse?
214 //case EOpenBracketFound:
215 //case EChoiceOrRange:
218 User::Panic(bufParsingError, KErrSyntaxError);
229 //case EOpenBracketFound:
230 //case ELookingForCloseBracket:
234 User::Panic(bufParsingError, KErrSyntaxError);
243 iStateMachines[nPatternCount]->AddStateTransistionL(cChar, nState, bEndOfParse?
248 //case EOpenBracketFound:
249 //case EChoiceOrRange:
250 //case ELookingForCloseBracket:
254 User::Panic(bufParsingError, KErrSyntaxError);
259 User::Panic(bufParsingError, KErrSyntaxError);
266 if(parseState != EAny)
267 User::Panic(bufParsingError, KErrSyntaxError);
271 void CRegularExpression::GetWildcardVersionOfPattern(
272 TInt aPatternIndex, TText aWildcard, TDes& aWildcardedPattern) const
274 __ASSERT_ALWAYS( (0 <= aPatternIndex) && (aPatternIndex < iStateMachines.Count()),
275 Panic(ENumberGroupingNoSuchStateMachine) );
276 iStateMachines[aPatternIndex]->GetWildcardVersionOfPattern( aWildcard, aWildcardedPattern );