textandlocutils/numbergrouping/src/StateMachine.cpp
branchRCL_3
changeset 54 748ec5531811
equal deleted inserted replaced
36:f902e87c146f 54:748ec5531811
       
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "StateMachine.h"
       
    20 
       
    21 // CStateMachine
       
    22 CStateMachine::CStateMachine(TInt aMaxNumberChars, TInt aMaxNumberStates) : iMaxNumberChars(static_cast<TInt>(aMaxNumberChars)),
       
    23 																			iMaxNumberStates(static_cast<TInt>(aMaxNumberStates))
       
    24 {
       
    25 }
       
    26 
       
    27 CStateMachine* CStateMachine::NewL(TInt aMaxNumberChars, TInt aMaxNumberStates)
       
    28 {
       
    29 	CStateMachine* s = NewLC(aMaxNumberChars, aMaxNumberStates);
       
    30 	CleanupStack::Pop();
       
    31 	return s;
       
    32 }
       
    33 
       
    34 CStateMachine* CStateMachine::NewLC(TInt aMaxNumberChars, TInt aMaxNumberStates)
       
    35 {
       
    36 	CStateMachine* s = new(ELeave)CStateMachine(aMaxNumberChars, aMaxNumberStates);
       
    37 	CleanupStack::PushL(s);
       
    38 	s->ConstructL();
       
    39 	return s;
       
    40 }
       
    41 
       
    42 CStateMachine::~CStateMachine()
       
    43 {
       
    44 	for(TInt i = 0; i < iMaxNumberChars; ++i)
       
    45 		delete iStateTable[i];
       
    46 	
       
    47 	delete iStateTable;
       
    48 }
       
    49 
       
    50 void CStateMachine::ConstructL()
       
    51 {
       
    52 	iStateTable = new TInt*[iMaxNumberChars];	
       
    53 
       
    54 	for(TInt i = 0; i < iMaxNumberChars; ++i)
       
    55 	{
       
    56 		iStateTable[i] = new TInt[iMaxNumberStates];
       
    57 		for(TInt j = 0; j < iMaxNumberStates; j++)
       
    58 			iStateTable[i][j] = 0;
       
    59 	}
       
    60 }
       
    61 
       
    62 void CStateMachine::AddStateTransistionL(TChar aChar, TInt aState, TInt aNextState)
       
    63 {
       
    64 	RArray<TInt> Dummy;
       
    65 	TInt CharIndex = MapIndex(aChar, Dummy);
       
    66 	Dummy.Close();
       
    67 	
       
    68 	AddStateTransistionL(CharIndex, aState, aNextState);
       
    69 }
       
    70 
       
    71 void CStateMachine::AddStateTransistionL(TInt aIndex, TInt aState, TInt aNextState)
       
    72 {
       
    73     if(aIndex < 0 || aIndex > iMaxNumberChars || aState > iMaxNumberStates || aNextState > iMaxNumberStates)
       
    74 		User::Leave(KErrGeneral);
       
    75 
       
    76 	iStateTable[static_cast<TInt>(aIndex)][static_cast<TInt>(aState)] = static_cast<TInt>(aNextState);
       
    77 }
       
    78 
       
    79 TBool CStateMachine::Matches(const TDesC& aString)
       
    80 {
       
    81 	// Walk the state table and return true if the string matches the pattern.
       
    82 	
       
    83 	TInt			nTableState = KStateNoMatch;
       
    84 	RArray<TInt>	arrIndices;
       
    85 	TBool			bFound = EFalse;
       
    86 
       
    87 	for(TInt nStringIndex = 0; nStringIndex < aString.Length(); ++nStringIndex)
       
    88 	{
       
    89 		arrIndices.Reset();
       
    90 		
       
    91 		TChar	charChar = aString[nStringIndex];
       
    92 		TInt	nTableIndex = MapIndex(charChar, arrIndices);
       
    93 
       
    94 		if(nTableIndex == -1) 
       
    95 			return EFalse;
       
    96 		
       
    97 		TInt nCount = arrIndices.Count();
       
    98 		
       
    99 		if(nCount)
       
   100 		{
       
   101 			for(TInt i = 0; i < nCount; i++)
       
   102 			{
       
   103 				nTableIndex = arrIndices[i];
       
   104 				TInt NewTableState = iStateTable[nTableIndex][nTableState];
       
   105 				
       
   106 				if( NewTableState != KStateNoMatch || 
       
   107 					(NewTableState == KStateNoMatch && i == (arrIndices.Count() - 1)) ||
       
   108 					NewTableState == KStateMatched)
       
   109 				{
       
   110 					nTableState = NewTableState;
       
   111 					break;
       
   112 				}
       
   113 			}
       
   114 		}
       
   115 		else
       
   116 		{
       
   117 			nTableState = iStateTable[nTableIndex][nTableState];
       
   118 		}
       
   119 
       
   120 		if(nTableState == KStateNoMatch)
       
   121 			break;
       
   122 
       
   123 		if(nTableState == KStateMatched)
       
   124 		{
       
   125 			bFound = ETrue;
       
   126 			break;
       
   127 		}
       
   128 	}
       
   129 
       
   130 	arrIndices.Close();
       
   131 	
       
   132 	return bFound;
       
   133 }
       
   134 
       
   135 TInt CStateMachine::MapIndex(TChar aChar, RArray<TInt>& aIndices)
       
   136 {
       
   137     // Check the type of aChar and return the relevant index or indicies
       
   138 	if(aChar.IsDigit())
       
   139 	{
       
   140 		TInt nIndex = static_cast<TInt>(aChar.GetNumericValue());
       
   141 		
       
   142 		TInt ret = KErrNone;
       
   143 		ret |= aIndices.Append(nIndex);
       
   144 		ret |= aIndices.Append(KCharacterDot);
       
   145 		__ASSERT_DEBUG(!ret, User::Panic(_L("RArray append failure"), ret));
       
   146 		if (KErrNone != ret) return ret;
       
   147 
       
   148 		return nIndex;
       
   149 	}
       
   150 
       
   151 	if(aChar == '+')	return KCharacterPlus;
       
   152 	if(aChar == '.')	return KCharacterDot;
       
   153 
       
   154     return -1;  // TO DO : define 
       
   155 }
       
   156 
       
   157 void CStateMachine::GetWildcardVersionOfPattern( 
       
   158     TText aWildcardChar, 
       
   159     TDes& aWildcardedPattern ) const
       
   160     {
       
   161     aWildcardedPattern.SetLength(0);
       
   162     TInt maxLength = aWildcardedPattern.MaxLength();
       
   163     // There is a column in the StateTable for each character in the regular expression. The first character
       
   164     // of the regexp in column [0], last in column [Length()-1]
       
   165     // The non-zero values found within a column of the StateTable represent the characters
       
   166     // that are valid at that position in a candidate match.
       
   167     // An example pattern is calculated by examining the StateTable.
       
   168     // For each column, count the number of matching ( i.e. not KStateNoMatch) states
       
   169     // If only one, then place that character in the example pattern.
       
   170     // If more than one, put the wildcard in.
       
   171     for(TInt stateIndex = 0; stateIndex < iMaxNumberStates && stateIndex < maxLength; stateIndex++)
       
   172         {
       
   173         TInt matches = 0;
       
   174         TInt matchedIndex = -1;
       
   175         for (TInt charIndex = 0; charIndex < iMaxNumberChars; charIndex++ )
       
   176             {
       
   177             TInt nextState = iStateTable[charIndex][stateIndex];
       
   178 
       
   179             if ( nextState != KStateNoMatch )
       
   180                 {
       
   181                 matches++;
       
   182                 matchedIndex = charIndex;
       
   183                 if (matches > 1 )
       
   184                     break;
       
   185                 }
       
   186             }
       
   187         if ( matches == 0 ) // Have found an empty column.  Unused part of state machine. Stop filling
       
   188             {
       
   189             break;
       
   190             }
       
   191         else if ( matches > 1 )
       
   192             aWildcardedPattern.Append(aWildcardChar);
       
   193         else
       
   194             {
       
   195             if ( 0 <= matchedIndex && matchedIndex <= 9 ) // Must be a numeric digit
       
   196                 {
       
   197                 aWildcardedPattern.Append((TText)(matchedIndex+'0'));
       
   198                 }
       
   199             else if ( matchedIndex == KCharacterDot )
       
   200                 {
       
   201                 aWildcardedPattern.Append(aWildcardChar);
       
   202                 }
       
   203             else if ( matchedIndex == KCharacterPlus )
       
   204                 {
       
   205                 aWildcardedPattern.Append('+');
       
   206                 }
       
   207             else
       
   208                 {
       
   209                 aWildcardedPattern.Append(aWildcardChar);
       
   210                 }
       
   211             }
       
   212         }
       
   213 
       
   214     }
       
   215 // End of File
       
   216