src/hbcore/i18n/hbstringutil.cpp
branchGCC_SURGE
changeset 15 f378acbc9cfb
parent 7 923ff622b8b9
child 23 e6ad4ef83b23
child 34 ed14f46c0e55
equal deleted inserted replaced
9:730c025d4b77 15:f378acbc9cfb
    40 #ifndef QT_NO_REGEXP
    40 #ifndef QT_NO_REGEXP
    41 #include <QRegExp>
    41 #include <QRegExp>
    42 #endif
    42 #endif
    43 
    43 
    44 /*!
    44 /*!
    45     @beta
    45     @stable
    46     @hbcore
    46     @hbcore
    47     \class HbStringUtil
    47     \class HbStringUtil
    48     \brief The HbStringUtil class can be used to execute operations on strings,
    48     \brief The HbStringUtil class can be used to execute operations on strings,
    49     such as comparisons and finding data sequences.
    49     such as comparisons and finding data sequences.
    50 
    50 
    51     \ingroup i18n
       
    52 
       
    53     \warning This class is only useful in Symbian platforms since it uses Symbian
       
    54     methods in order to implement different functionalities.
       
    55 
       
    56     \sa HbStringUtil
    51     \sa HbStringUtil
    57 */
    52 */
    58 
    53 
    59 /*!
    54 /*!
    60     \enum HbStringUtil::Option
    55     \enum HbStringUtil::Option
    83 */
    78 */
    84 
    79 
    85 /*!
    80 /*!
    86     Searches source string's collated data for a
    81     Searches source string's collated data for a
    87     match with collated data supplied in pattern string
    82     match with collated data supplied in pattern string
       
    83       
       
    84     \attention Cross-Platform API
    88     
    85     
    89     \param strFrom Source string.
    86     \param strFrom Source string.
    90     \param strToMatch Pattern string.
    87     \param strToMatch Pattern string.
    91     \param maxLevel Determines the tightness of the collation.
    88     \param maxLevel Determines the tightness of the collation.
    92     Level 0 - Character identity; 
    89     Level 0 - Character identity; 
    95     Level 3 - Character identity, accents, case and Unicode value; 
    92     Level 3 - Character identity, accents, case and Unicode value; 
    96     \param flags The flags that will be used. Default value is Default.
    93     \param flags The flags that will be used. Default value is Default.
    97     \param wildChar Wild card character.
    94     \param wildChar Wild card character.
    98     \param wildSequenceChar Wild card sequence character.
    95     \param wildSequenceChar Wild card sequence character.
    99     \param escapeChar The escape character, for example,  '?', '*'.
    96     \param escapeChar The escape character, for example,  '?', '*'.
       
    97     
   100     \return If a match is found the offset within source string's
    98     \return If a match is found the offset within source string's
   101     data where the match first occurs. -1 if match is not found.
    99     data where the match first occurs. -1 if match is not found.
   102     
   100     
   103     Example
   101     Example:
   104     \snippet{unittest_hbstringutil/unittest_hbstringutil.cpp,3}
   102     \snippet{unittest_hbstringutil/unittest_hbstringutil.cpp,3}
   105  */
   103  */
   106 int HbStringUtil::matchC( const QString &strFrom, const QString &strToMatch,
   104 int HbStringUtil::matchC( const QString &strFrom, const QString &strToMatch,
   107                                     int maxLevel, Options flags,
   105                                     int maxLevel, Options flags,
   108                                     int wildChar, int wildSequenceChar, int escapeChar )
   106                                     int wildChar, int wildSequenceChar, int escapeChar )
   142 
   140 
   143 /*!
   141 /*!
   144     Compares source string's data with the other string's
   142     Compares source string's data with the other string's
   145     data using the specified collation method.
   143     data using the specified collation method.
   146     
   144     
       
   145     \attention Cross-Platform API
       
   146     
   147     \param string1 Source string.
   147     \param string1 Source string.
   148     \param string2 String whose data is to be compared with the source string.
   148     \param string2 String whose data is to be compared with the source string.
   149     \param maxLevel Maximum level to use for comparing.
   149     \param maxLevel Maximum level to use for comparing.
   150     Level 0 - Character identity; 
   150     Level 0 - Character identity; 
   151     Level 1 - Character identity and accents; 
   151     Level 1 - Character identity and accents; 
   152     Level 2 - Character identity, accents and case; 
   152     Level 2 - Character identity, accents and case; 
   153     Level 3 - Character identity, accents, case and Unicode value; 
   153     Level 3 - Character identity, accents, case and Unicode value; 
   154     \param flags The flags that will be used. Default value is Default.
   154     \param flags The flags that will be used. Default value is Default.
       
   155     
   155     \return Positive if source string is greater, negative if it is less and 
   156     \return Positive if source string is greater, negative if it is less and 
   156     zero	if the content of both strings match.
   157     zero	if the content of both strings match.
   157     
   158     
   158     Example
   159     Example:
   159     \snippet{unittest_hbstringutil/unittest_hbstringutil.cpp,1}
   160     \snippet{unittest_hbstringutil/unittest_hbstringutil.cpp,1}
   160  */
   161  */
   161 int HbStringUtil::compareC( const QString &string1, const QString &string2,
   162 int HbStringUtil::compareC( const QString &string1, const QString &string2,
   162                                         int maxLevel, Options flags )
   163                                         int maxLevel, Options flags )
   163 {
   164 {
   182     return string1.localeAwareCompare( string2 );
   183     return string1.localeAwareCompare( string2 );
   183 #endif    
   184 #endif    
   184 }
   185 }
   185 
   186 
   186 /*!
   187 /*!
   187     Searches for the first occurence of the specified collated 
   188     Searches for the first occurrence of the specified collated 
   188     data sequence in the aStrFrom to the specified maximum
   189     data sequence in the aStrFrom to the specified maximum
   189     collation level.
   190     collation level.
       
   191     
       
   192     \attention Cross-Platform API
   190     
   193     
   191     \param strFrom Source string.
   194     \param strFrom Source string.
   192     \param strToFind String whose data is to be compared with the source string.
   195     \param strToFind String whose data is to be compared with the source string.
   193     \param maxLevel The maximum collation level.
   196     \param maxLevel The maximum collation level.
   194     Level 0 - Character identity; 
   197     Level 0 - Character identity; 
   195     Level 1 - Character identity and accents; 
   198     Level 1 - Character identity and accents; 
   196     Level 2 - Character identity, accents and case; 
   199     Level 2 - Character identity, accents and case; 
   197     Level 3 - Character identity, accents, case and Unicode value; 
   200     Level 3 - Character identity, accents, case and Unicode value; 
       
   201     
   198     \return Offset of the data sequence from the beginning of the
   202     \return Offset of the data sequence from the beginning of the
   199     aStrFrom. -1 if the data sequence cannot be found.
   203     aStrFrom. -1 if the data sequence cannot be found.
   200     
   204     
   201     Example
   205     Example:
   202     \snippet{unittest_hbstringutil/unittest_hbstringutil.cpp,5}
   206     \snippet{unittest_hbstringutil/unittest_hbstringutil.cpp,5}
   203  */
   207  */
   204 int HbStringUtil::findC( const QString &strFrom,
   208 int HbStringUtil::findC( const QString &strFrom,
   205                          const QString &strToFind,
   209                          const QString &strToFind,
   206                          int           maxLevel )
   210                          int           maxLevel )
   223 
   227 
   224 /*!
   228 /*!
   225     Searches source string's folded data for a
   229     Searches source string's folded data for a
   226     match with folded data supplied in pattern string
   230     match with folded data supplied in pattern string
   227     
   231     
       
   232     \attention Cross-Platform API
       
   233     
   228     \param strFrom Source string.
   234     \param strFrom Source string.
   229     \param strToMatch Pattern string.
   235     \param strToMatch Pattern string.
       
   236     
   230     \return If a match is found the offset within source string's
   237     \return If a match is found the offset within source string's
   231     data where the match first occurs. -1 if match is not found.
   238     data where the match first occurs. -1 if match is not found.
   232     
   239     
   233     Example
   240     Example:
   234     \snippet{unittest_hbstringutil/unittest_hbstringutil.cpp,4}
   241     \snippet{unittest_hbstringutil/unittest_hbstringutil.cpp,4}
   235  */
   242  */
   236 int HbStringUtil::matchF( const QString &strFrom,
   243 int HbStringUtil::matchF( const QString &strFrom,
   237                           const QString &strToMatch )
   244                           const QString &strToMatch )
   238 {
   245 {
   252     
   259     
   253 #endif      
   260 #endif      
   254 }
   261 }
   255 
   262 
   256 /*!
   263 /*!
   257     Searches for the first occurence of the specified folded 
   264     Searches for the first occurrence of the specified folded 
   258     data sequence in the strFrom.
   265     data sequence in the strFrom.
       
   266     
       
   267     \attention Cross-Platform API
   259     
   268     
   260     \param strFrom Source string.
   269     \param strFrom Source string.
   261     \param strToFind String whose data is to be compared with the source string.
   270     \param strToFind String whose data is to be compared with the source string.
       
   271     
   262     \return Offset of the data sequence from the beginning of the
   272     \return Offset of the data sequence from the beginning of the
   263     strFrom. -1 if the data sequence cannot be found. Zero,
   273     strFrom. -1 if the data sequence cannot be found. Zero,
   264     if the length of search data sequence is zero.
   274     if the length of search data sequence is zero.
   265     
   275     
   266     Example
   276     Example:
   267     \snippet{unittest_hbstringutil/unittest_hbstringutil.cpp,6}
   277     \snippet{unittest_hbstringutil/unittest_hbstringutil.cpp,6}
   268  */
   278  */
   269 int HbStringUtil::findF( const QString &strFrom,
   279 int HbStringUtil::findF( const QString &strFrom,
   270                          const QString &strToFind )
   280                          const QString &strToFind )
   271 {
   281 {
   281 
   291 
   282 /*!
   292 /*!
   283     Compares source string's folded data with the other string's
   293     Compares source string's folded data with the other string's
   284     folded data.
   294     folded data.
   285     
   295     
       
   296     \attention Cross-Platform API
       
   297     
   286     \param string1 Source string.
   298     \param string1 Source string.
   287     \param string2 String whose data is to be compared with the source string.
   299     \param string2 String whose data is to be compared with the source string.
       
   300     
   288     \return Positive if source string is greater, negative if it is less and 
   301     \return Positive if source string is greater, negative if it is less and 
   289     zero if the content of both strings match.
   302     zero if the content of both strings match.
   290     
   303     
   291     Example
   304     Example:
   292     \snippet{unittest_hbstringutil/unittest_hbstringutil.cpp,2}
   305     \snippet{unittest_hbstringutil/unittest_hbstringutil.cpp,2}
   293  */
   306  */
   294 int HbStringUtil::compareF( const QString &string1,
   307 int HbStringUtil::compareF( const QString &string1,
   295                             const QString &string2 )
   308                             const QString &string2 )
   296 {
   309 {
   309     \param ch native digit
   322     \param ch native digit
   310     \return starting digit range
   323     \return starting digit range
   311  */
   324  */
   312 static QChar nativeDigitBase(QChar ch)
   325 static QChar nativeDigitBase(QChar ch)
   313 {
   326 {
   314 	DigitType d[] = { WesternDigit, ArabicIndicDigit, EasternArabicIndicDigit, DevanagariDigit, ThaiDigit };
   327     DigitType d[] = { WesternDigit, ArabicIndicDigit, EasternArabicIndicDigit, DevanagariDigit, ThaiDigit };
   315     int i = 0;
   328     int i = 0;
   316     int num = sizeof(d)/sizeof(d[0]);
   329     int num = sizeof(d)/sizeof(d[0]);
   317     while(i<num) {
   330     while(i<num) {
   318         if (ch>QChar(d[i]) && ch<QChar(d[i]+10)) { return d[i]; }
   331         if (ch>QChar(d[i]) && ch<QChar(d[i]+10)) { return d[i]; }
   319         i++;
   332         i++;
   321     return ch;
   334     return ch;
   322 }
   335 }
   323 
   336 
   324 /*!
   337 /*!
   325     Converts digits to native digits based on current UI language.
   338     Converts digits to native digits based on current UI language.
       
   339     
       
   340     \attention Cross-Platform API
       
   341     
   326     \param str digits to be converted.
   342     \param str digits to be converted.
   327  */
   343  */
   328 QString HbStringUtil::convertDigits( const QString str ) 
   344 QString HbStringUtil::convertDigits( const QString str ) 
   329 {
   345 {
   330 	HbExtendedLocale locale = HbExtendedLocale::system();
   346 	HbExtendedLocale locale = HbExtendedLocale::system();
   331 	DigitType digitType = WesternDigit;
   347 	DigitType digitType = WesternDigit;
   332 	if (locale.language() == HbExtendedLocale::Arabic) {
   348 	if (locale.language() == HbExtendedLocale::Arabic) {
   333 		digitType = ArabicIndicDigit;
   349 		digitType = ArabicIndicDigit;
   334 	}
   350 	} else if (locale.language() == HbExtendedLocale::Persian || locale.language() == HbExtendedLocale::Urdu) {
   335 	if (locale.language() == HbExtendedLocale::Persian) {
       
   336 		digitType = EasternArabicIndicDigit;
       
   337 	}
       
   338 	if (locale.language() == HbExtendedLocale::Urdu) {
       
   339 		digitType = EasternArabicIndicDigit;
   351 		digitType = EasternArabicIndicDigit;
   340 	}
   352 	}
   341 	QString converted = HbStringUtil::convertDigitsTo(str, digitType);
   353 	QString converted = HbStringUtil::convertDigitsTo(str, digitType);
   342 	return converted;
   354 	return converted;
   343 }
   355 }
   344 
   356 
   345 /*!
   357 /*!
   346     Converts the digit from Latin to native or native to latin or native to native
   358     Converts the digit from Latin to native or native to latin or native to native
       
   359     
       
   360     \attention Cross-Platform API
       
   361     
   347     \param str digits to be converted.
   362     \param str digits to be converted.
   348     \param digitType type of the digit to be converted to
   363     \param digitType type of the digit to be converted to
   349  */
   364  */
   350 QString HbStringUtil::convertDigitsTo( const QString str, const DigitType digitType ) 
   365 QString HbStringUtil::convertDigitsTo( const QString str, const DigitType digitType ) 
   351 {
   366 {
   352     QString convDigit;
   367     QString convDigit;
   353 	int length = str.length();
   368     int length = str.length();
   354     for(int i=0; i<length; i++) 
   369     for(int i=0; i<length; i++) 
   355        {
   370        {
   356        ushort digit = str[i].unicode();
   371        ushort digit = str[i].unicode();
   357        ushort digitBase = nativeDigitBase(str[i]).unicode();
   372        ushort digitBase = nativeDigitBase(str[i]).unicode();
   358        ushort convertedDigit = 0;
   373        ushort convertedDigit = 0;
   375 }
   390 }
   376 
   391 
   377 /*!
   392 /*!
   378     Sorts QStrings into alphabetically order (overwrites the strList's original content)
   393     Sorts QStrings into alphabetically order (overwrites the strList's original content)
   379     
   394     
       
   395     \attention Cross-Platform API
       
   396     
   380     \param strList List of QStrings which need to be sorted.
   397     \param strList List of QStrings which need to be sorted.
   381     
   398     
   382     Example
   399     Example:
   383     \snippet{unittest_hbstringutil/unittest_hbstringutil.cpp,7}
   400     \snippet{unittest_hbstringutil/unittest_hbstringutil.cpp,7}
   384  */
   401  */
   385 void HbStringUtil::sort( QStringList &strList )
   402 void HbStringUtil::sort( QStringList &strList )
   386 {
   403 {
   387 	if ( strList.size() > 1 ) {
   404 	if ( strList.size() > 1 ) {