uiutils/Findutil/src/FindUtilJapanese.cpp
changeset 0 2f259fa3e83a
child 4 8ca85d2f0db7
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2002-2005 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 "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:         Japanese Find Utilities implementation file.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 
       
    26 
       
    27 // INCLUDE FILES
       
    28 #include "FindUtilJapanese.h"
       
    29 #include <JPLangUtil.h>
       
    30 
       
    31 // CONSTANTS
       
    32 _LIT(KDesWildCard, "*");
       
    33 
       
    34 const TInt KLitTab('\t');
       
    35 const TInt KLitSpace(' ');
       
    36 const TInt KLitHyphen('-');
       
    37 const TInt KLitQuestion('?');
       
    38 const TInt KLitWildCard('*');
       
    39 const TInt KLitFullWidthSpace(0x3000);
       
    40 const TInt KLitFullWidthHyphen(0x2015);
       
    41 const TInt KLitFullWidthQuestion(0xff1f);
       
    42 const TInt KLitFullWidthWildCard(0xff0a);
       
    43 const TInt KLitStartHiragana(0x3041);
       
    44 const TInt KLitEndHiragana(0x3096);
       
    45 const TInt KLitStartKatakana(0x30A1);
       
    46 const TInt KLitLineFeed(8233);
       
    47 
       
    48 const TInt KMaxWordLength = 200;
       
    49 const TUint KFullWidthSpace = 0x3000;
       
    50 
       
    51 // ============================ MEMBER FUNCTIONS ===============================
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CFindUtilJapanese::CFindUtilJapanese
       
    55 // C++ default constructor can NOT contain any code, that
       
    56 // might leave.
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 CFindUtilJapanese::CFindUtilJapanese()
       
    60     {
       
    61     }
       
    62 
       
    63 // Destructor
       
    64 CFindUtilJapanese::~CFindUtilJapanese()
       
    65     {
       
    66     }
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // CFindUtilJapanese::IsFindWordSeparator
       
    70 // (other items were commented in a header).
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 TBool CFindUtilJapanese::IsFindWordSeparator(TChar aCh)
       
    74     {
       
    75     return aCh == KLitSpace || aCh == KLitHyphen || aCh == KLitTab ||
       
    76         aCh == KLitFullWidthSpace || aCh == KLitFullWidthHyphen ||
       
    77         aCh == KLitLineFeed;
       
    78     }
       
    79 
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // CFindUtilJapanese::RemoveWordSeparatorsAndWildChars
       
    83 // (other items were commented in a header).
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 void CFindUtilJapanese::RemoveWordSeparatorsAndWildChars( TDes16& aSearchPtr )
       
    87     {
       
    88     TInt len = aSearchPtr.Length();
       
    89 
       
    90     for ( TInt ii(0); ii < len; ii++ )
       
    91         {
       
    92         if ( IsFindWordSeparator(static_cast<TChar>(aSearchPtr[ii])) || 
       
    93             aSearchPtr[ii] == KLitWildCard || aSearchPtr[ii] == KLitQuestion ||
       
    94             aSearchPtr[ii] == KLitFullWidthWildCard || 
       
    95             aSearchPtr[ii] == KLitFullWidthQuestion)
       
    96             {
       
    97             aSearchPtr.Delete(ii, 1);
       
    98             len--;
       
    99             continue;
       
   100             }
       
   101         }
       
   102 
       
   103     }
       
   104 
       
   105 inline TBool IsFindWordSeparator(TChar aCh)
       
   106     {
       
   107     return (aCh == ' '|| aCh == '-' || aCh == '\t' || aCh==KFullWidthSpace);
       
   108     }
       
   109 
       
   110 void ReplaceCharacters(TDes &aDes, const TDesC &aChars, TChar aReplacement)
       
   111     //
       
   112     // Running time O(aDes.Length() * aChars.Length())
       
   113     // Does not change length of the string.
       
   114     // 
       
   115     {
       
   116     TInt src = 0;
       
   117     TInt srclength = aDes.Length();
       
   118     while(src < srclength)
       
   119         {
       
   120         TChar c = aDes[src];
       
   121         if (aChars.LocateF(c) != KErrNotFound)
       
   122             aDes[src] = TUint16(aReplacement);
       
   123         ++src;
       
   124         }
       
   125     }
       
   126 
       
   127 
       
   128 // -----------------------------------------------------------------------------
       
   129 // CFindUtilJapanese::MyFindC
       
   130 // (other items were commented in a header).
       
   131 // This method is called by MatchRefineL().
       
   132 // -----------------------------------------------------------------------------
       
   133 //
       
   134 TInt CFindUtilJapanese::MyFindC(const TDesC &aItemString, const TDesC &aSearchText)
       
   135     {
       
   136     TBuf<KMaxWordLength> searchText;
       
   137     TBuf<KMaxWordLength> itemString;
       
   138     searchText.Append(aSearchText);
       
   139     // The replacecharacters are to disable regexp matching provided
       
   140     // by MatchC().
       
   141     _LIT(KQuestion,"?");
       
   142     _LIT(KStar,"*");
       
   143     ReplaceCharacters(searchText, KQuestion, TChar('\t'));
       
   144     ReplaceCharacters(searchText, KStar, TChar('\r'));
       
   145     searchText.Append(KStar);
       
   146     if (aItemString.Length() < KMaxWordLength)
       
   147         {
       
   148         itemString.Append(aItemString);
       
   149         }
       
   150     else
       
   151         {
       
   152         itemString.Append(aItemString.Left(KMaxWordLength));
       
   153         }
       
   154     ReplaceCharacters(itemString, KQuestion, TChar('\t'));
       
   155     ReplaceCharacters(itemString, KStar, TChar('\r'));
       
   156 
       
   157     CnvKatakana(searchText);
       
   158     CnvKatakana(itemString);
       
   159 
       
   160     TInt length = itemString.Length();
       
   161     for(int i=0;i<length;i++)
       
   162         {
       
   163         if (i==0 || IsFindWordSeparator(aItemString[i-1]))
       
   164             {
       
   165             if (itemString.Mid(i).MatchC(searchText) != KErrNotFound)
       
   166                 {
       
   167                 return i;
       
   168                 }
       
   169             }
       
   170         }
       
   171     return KErrNotFound;
       
   172     }
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // CFindUtilJapanese::IsMatch
       
   176 // (other items were commented in a header).
       
   177 // This method is called by MatchRefineL(). 
       
   178 // This method separates by 4 characters, and this method calls MyFindC().
       
   179 // -----------------------------------------------------------------------------
       
   180 //
       
   181 TBool CFindUtilJapanese::IsMatch( const TDesC& aItemString, const TDesC& aSearchText)
       
   182     {
       
   183     TPtrC itemptr(aItemString);
       
   184     TPtrC searchptr(aSearchText);
       
   185 
       
   186     TBool match = EFalse;
       
   187     
       
   188     FOREVER 
       
   189         {
       
   190         // Loop invariant: itemptr is next character from ' ' or '-'
       
   191         // Loop invariant: seachptr is at beginning of searched item
       
   192         TInt val = MyFindC(itemptr,searchptr);
       
   193         if (val == 0)
       
   194             {
       
   195             match = ETrue;
       
   196             break;
       
   197             }
       
   198         if (val != KErrNotFound && IsFindWordSeparator(itemptr[val-1]))
       
   199             {
       
   200             match = ETrue;
       
   201             break;
       
   202             }
       
   203 
       
   204         // find the word separator characters from list item
       
   205         const TInt maxFindChar = 4;
       
   206         TChar findchar = KFullWidthSpace;
       
   207         TInt findindex;
       
   208         for (findindex=0; findindex < maxFindChar; findindex++)
       
   209             {
       
   210             switch(findindex)
       
   211                 {
       
   212                 case 0: findchar = (' ');  break;   // space
       
   213                 case 1: findchar = KFullWidthSpace; break;   // full space
       
   214                 case 2: findchar = ('-');  break;   // minus
       
   215                 case 3: findchar = ('\t'); break;   // tab
       
   216                 }
       
   217             const TInt findpos = itemptr.LocateF(findchar);
       
   218             if (findpos != KErrNotFound)
       
   219                 {
       
   220                 itemptr.Set(itemptr.Mid(findpos+1));
       
   221                 break;
       
   222                 }
       
   223             }
       
   224         if (findindex >= maxFindChar)
       
   225             {
       
   226             match = EFalse;
       
   227             break;
       
   228             }
       
   229         if (itemptr.Length() == 0)
       
   230             {
       
   231             match = EFalse;
       
   232             break;
       
   233             }
       
   234         }
       
   235     return match;
       
   236     }
       
   237 
       
   238 // -----------------------------------------------------------------------------
       
   239 // CFindUtilJapanese::CnvKatakana
       
   240 // (other items were commented in a header).
       
   241 // -----------------------------------------------------------------------------
       
   242 //
       
   243 void CFindUtilJapanese::CnvKatakana(TDes& aDes)
       
   244     {
       
   245     TInt len = aDes.Length();
       
   246     for (TInt i(0); i < len; i++)
       
   247         {
       
   248         TUint16& ch = aDes[i];
       
   249         if (ch >= KLitStartHiragana && ch <= KLitEndHiragana)
       
   250             {
       
   251             ch += (KLitStartKatakana - KLitStartHiragana);
       
   252             }
       
   253         }
       
   254     }
       
   255 
       
   256 // -----------------------------------------------------------------------------
       
   257 // CFindUtilJapanese::Match
       
   258 // (other items were commented in a header).
       
   259 // -----------------------------------------------------------------------------
       
   260 //
       
   261 
       
   262 TBool CFindUtilJapanese::Match(const TDesC& aContactsField, const TDesC& aWord)
       
   263     {
       
   264     if (!aContactsField.Length())
       
   265         {
       
   266         return EFalse;
       
   267         }
       
   268 
       
   269     TBuf<KMaxWordLength> searchText(aWord);
       
   270     TBuf<KMaxWordLength> itemString(aContactsField);
       
   271 
       
   272     searchText.Append(KDesWildCard);
       
   273     CnvKatakana(searchText);
       
   274     CnvKatakana(itemString);
       
   275 
       
   276     if (itemString.MatchC(searchText) != KErrNotFound)
       
   277         {
       
   278         return ETrue;
       
   279         }
       
   280 
       
   281     return EFalse;
       
   282     }
       
   283 
       
   284 // -----------------------------------------------------------------------------
       
   285 // CFindUtilJapanese::MatchRefineL
       
   286 // (other items were commented in a header).
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 TBool CFindUtilJapanese::MatchRefineL( const TDesC& aItemString, const TDesC &aSearchText)
       
   290     {
       
   291     if ( aItemString.Length() == 0 )
       
   292         {
       
   293         return EFalse;
       
   294         }
       
   295 
       
   296     if ( aSearchText.Length() == 0 )
       
   297         {
       
   298         return ETrue;
       
   299         }
       
   300 
       
   301     return IsMatch(aItemString, aSearchText);
       
   302     }
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // CFindUtilJapanese::IsWordValidForMatching
       
   306 // (other items were commented in a header).
       
   307 // -----------------------------------------------------------------------------
       
   308 //
       
   309 TBool CFindUtilJapanese::IsWordValidForMatching(const TDesC& /*aWord*/)
       
   310     {
       
   311     return ETrue;
       
   312     }
       
   313 
       
   314 
       
   315 // -----------------------------------------------------------------------------
       
   316 // CFindUtilJapanese::MatchAdaptiveRefineL
       
   317 // (other items were commented in a header).
       
   318 // -----------------------------------------------------------------------------
       
   319 //  
       
   320 TBool CFindUtilJapanese::MatchAdaptiveRefineL( const TDesC& /*aItemString*/, 
       
   321 	                                           const TDesC& /*aSearchText*/,
       
   322 	                                           HBufC*& /*aNextChars*/ )
       
   323 	{	
       
   324   	return EFalse;	
       
   325 	}
       
   326 	
       
   327 // ---------------------------------------------------------
       
   328 // Match arithmetic for accurate search, special conversion 
       
   329 // for aItemString is implemented with MFindStringConverter
       
   330 // before the final match
       
   331 // ---------------------------------------------------------
       
   332 //
       
   333 TBool CFindUtilJapanese::MatchRefineL( const TDesC& /*aItemString*/, const TDesC& /*aSearchText*/, 
       
   334                                        TMatchPolicy /*aPolicy*/, MFindStringConverter* /*aConverter*/)
       
   335     {
       
   336     return EFalse;
       
   337     }	
       
   338 
       
   339 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   340 
       
   341 // -----------------------------------------------------------------------------
       
   342 // FindUtilFactoryFunctionL
       
   343 // Factory function at first ordinal
       
   344 // Returns: MFindUtil: It returns MFindUtil I/F.
       
   345 // -----------------------------------------------------------------------------
       
   346 //
       
   347 EXPORT_C MFindUtil* FindUtilFactoryFunctionL()
       
   348     {
       
   349     return new( ELeave ) CFindUtilJapanese();
       
   350     }
       
   351 
       
   352 //  End of File