LCOV - code coverage report
Current view: top level - libreoffice/unotools/source/i18n - charclass.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 65 168 38.7 %
Date: 2012-12-27 Functions: 16 26 61.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include <comphelper/processfactory.hxx>
      22             : #include <unotools/charclass.hxx>
      23             : #include <tools/string.hxx>
      24             : #include <tools/debug.hxx>
      25             : 
      26             : #include <com/sun/star/i18n/CharacterClassification.hpp>
      27             : 
      28             : using namespace ::com::sun::star;
      29             : using namespace ::com::sun::star::i18n;
      30             : using namespace ::com::sun::star::uno;
      31             : 
      32             : 
      33         423 : CharClass::CharClass(
      34             :             const Reference< uno::XComponentContext > & rxContext,
      35             :             const LanguageTag& rLanguageTag
      36             :             )
      37             :     :
      38         423 :         maLanguageTag( rLanguageTag)
      39             : {
      40         423 :     xCC = CharacterClassification::create( rxContext );
      41         423 : }
      42             : 
      43             : 
      44       11265 : CharClass::CharClass(
      45             :             const LanguageTag& rLanguageTag )
      46             :     :
      47       11265 :         maLanguageTag( rLanguageTag)
      48             : {
      49       11265 :     xCC = CharacterClassification::create( comphelper::getProcessComponentContext() );
      50       11265 : }
      51             : 
      52             : 
      53       11459 : CharClass::~CharClass()
      54             : {
      55       11459 : }
      56             : 
      57             : 
      58        9167 : void CharClass::setLanguageTag( const LanguageTag& rLanguageTag )
      59             : {
      60        9167 :     ::osl::MutexGuard aGuard( aMutex );
      61        9167 :     maLanguageTag = rLanguageTag;
      62        9167 : }
      63             : 
      64             : 
      65        2611 : const LanguageTag& CharClass::getLanguageTag() const
      66             : {
      67        2611 :     ::osl::MutexGuard aGuard( aMutex );
      68        2611 :     return maLanguageTag;
      69             : }
      70             : 
      71             : 
      72      196983 : const ::com::sun::star::lang::Locale& CharClass::getMyLocale() const
      73             : {
      74      196983 :     ::osl::MutexGuard aGuard( aMutex );
      75      196983 :     return maLanguageTag.getLocale();
      76             : }
      77             : 
      78             : 
      79             : // static
      80         949 : sal_Bool CharClass::isAsciiNumeric( const String& rStr )
      81             : {
      82         949 :     if ( !rStr.Len() )
      83           0 :         return sal_False;
      84         949 :     register const sal_Unicode* p = rStr.GetBuffer();
      85         949 :     register const sal_Unicode* const pStop = p + rStr.Len();
      86         435 :     do
      87             :     {
      88         949 :         if ( !isAsciiDigit( *p ) )
      89         514 :             return sal_False;
      90             :     } while ( ++p < pStop );
      91         435 :     return sal_True;
      92             : }
      93             : 
      94             : 
      95             : // static
      96           0 : sal_Bool CharClass::isAsciiAlpha( const String& rStr )
      97             : {
      98           0 :     if ( !rStr.Len() )
      99           0 :         return sal_False;
     100           0 :     register const sal_Unicode* p = rStr.GetBuffer();
     101           0 :     register const sal_Unicode* const pStop = p + rStr.Len();
     102           0 :     do
     103             :     {
     104           0 :         if ( !isAsciiAlpha( *p ) )
     105           0 :             return sal_False;
     106             :     } while ( ++p < pStop );
     107           0 :     return sal_True;
     108             : }
     109             : 
     110             : 
     111             : 
     112           0 : sal_Bool CharClass::isAlpha( const String& rStr, xub_StrLen nPos ) const
     113             : {
     114           0 :     sal_Unicode c = rStr.GetChar( nPos );
     115           0 :     if ( c < 128 )
     116           0 :         return isAsciiAlpha( c );
     117             : 
     118             :     try
     119             :     {
     120           0 :         if ( xCC.is() )
     121           0 :             return  (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
     122           0 :                 nCharClassAlphaType) != 0;
     123             :         else
     124           0 :             return sal_False;
     125             :     }
     126           0 :     catch ( const Exception& )
     127             :     {
     128             :         SAL_WARN( "unotools.i18n", "isAlpha: Exception caught!" );
     129           0 :         return sal_False;
     130             :     }
     131             : }
     132             : 
     133             : 
     134             : 
     135      110361 : sal_Bool CharClass::isLetter( const String& rStr, xub_StrLen nPos ) const
     136             : {
     137      110361 :     sal_Unicode c = rStr.GetChar( nPos );
     138      110361 :     if ( c < 128 )
     139      110322 :         return isAsciiAlpha( c );
     140             : 
     141             :     try
     142             :     {
     143          39 :         if ( xCC.is() )
     144          78 :             return  (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
     145          39 :                 nCharClassLetterType) != 0;
     146             :         else
     147           0 :             return sal_False;
     148             :     }
     149           0 :     catch ( const Exception& )
     150             :     {
     151             :         SAL_WARN( "unotools.i18n", "isLetter: Exception caught!" );
     152           0 :         return sal_False;
     153             :     }
     154             : }
     155             : 
     156             : 
     157           0 : sal_Bool CharClass::isLetter( const String& rStr ) const
     158             : {
     159             :     try
     160             :     {
     161           0 :         if ( xCC.is() )
     162           0 :             return isLetterType( xCC->getStringType( rStr, 0, rStr.Len(), getMyLocale() ) );
     163             :         else
     164           0 :             return sal_False;
     165             :     }
     166           0 :     catch ( const Exception& )
     167             :     {
     168             :         SAL_WARN( "unotools.i18n", "isLetter: Exception caught!" );
     169           0 :         return sal_False;
     170             :     }
     171             : }
     172             : 
     173             : 
     174           0 : sal_Bool CharClass::isDigit( const String& rStr, xub_StrLen nPos ) const
     175             : {
     176           0 :     sal_Unicode c = rStr.GetChar( nPos );
     177           0 :     if ( c < 128 )
     178           0 :         return isAsciiDigit( c );
     179             : 
     180             :     try
     181             :     {
     182           0 :         if ( xCC.is() )
     183           0 :             return  (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
     184           0 :                 KCharacterType::DIGIT) != 0;
     185             :         else
     186           0 :             return sal_False;
     187             :     }
     188           0 :     catch ( const Exception& )
     189             :     {
     190             :         SAL_WARN( "unotools.i18n", "isDigit: Exception caught!" );
     191           0 :         return sal_False;
     192             :     }
     193             : }
     194             : 
     195             : 
     196           0 : sal_Bool CharClass::isNumeric( const String& rStr ) const
     197             : {
     198             :     try
     199             :     {
     200           0 :         if ( xCC.is() )
     201           0 :             return isNumericType( xCC->getStringType( rStr, 0, rStr.Len(), getMyLocale() ) );
     202             :         else
     203           0 :             return sal_False;
     204             :     }
     205           0 :     catch ( const Exception& )
     206             :     {
     207             :         SAL_WARN( "unotools.i18n", "isNumeric: Exception caught!" );
     208           0 :         return sal_False;
     209             :     }
     210             : }
     211             : 
     212             : 
     213           0 : sal_Bool CharClass::isAlphaNumeric( const String& rStr, xub_StrLen nPos ) const
     214             : {
     215           0 :     sal_Unicode c = rStr.GetChar( nPos );
     216           0 :     if ( c < 128 )
     217           0 :         return isAsciiAlphaNumeric( c );
     218             : 
     219             :     try
     220             :     {
     221           0 :         if ( xCC.is() )
     222           0 :             return  (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
     223           0 :                 (nCharClassAlphaType | KCharacterType::DIGIT)) != 0;
     224             :         else
     225           0 :             return sal_False;
     226             :     }
     227           0 :     catch ( const Exception& )
     228             :     {
     229             :         SAL_WARN( "unotools.i18n", "isAlphaNumeric: Exception caught!" );
     230           0 :         return sal_False;
     231             :     }
     232             : }
     233             : 
     234             : 
     235        2968 : sal_Bool CharClass::isLetterNumeric( const String& rStr, xub_StrLen nPos ) const
     236             : {
     237        2968 :     sal_Unicode c = rStr.GetChar( nPos );
     238        2968 :     if ( c < 128 )
     239        2954 :         return isAsciiAlphaNumeric( c );
     240             : 
     241             :     try
     242             :     {
     243          14 :         if ( xCC.is() )
     244          28 :             return  (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
     245          14 :                 (nCharClassLetterType | KCharacterType::DIGIT)) != 0;
     246             :         else
     247           0 :             return sal_False;
     248             :     }
     249           0 :     catch ( const Exception& )
     250             :     {
     251             :         SAL_WARN( "unotools.i18n", "isLetterNumeric: Exception caught!" );
     252           0 :         return sal_False;
     253             :     }
     254             : }
     255             : 
     256             : 
     257        2603 : sal_Bool CharClass::isLetterNumeric( const String& rStr ) const
     258             : {
     259             :     try
     260             :     {
     261        2603 :         if ( xCC.is() )
     262        2603 :             return isLetterNumericType( xCC->getStringType( rStr, 0, rStr.Len(), getMyLocale() ) );
     263             :         else
     264           0 :             return sal_False;
     265             :     }
     266           0 :     catch ( const Exception& )
     267             :     {
     268             :         SAL_WARN( "unotools.i18n", "isLetterNumeric: Exception caught!" );
     269           0 :         return sal_False;
     270             :     }
     271             : }
     272             : 
     273           0 : rtl::OUString CharClass::titlecase(const rtl::OUString& rStr, sal_Int32 nPos, sal_Int32 nCount) const
     274             : {
     275             :     try
     276             :     {
     277           0 :         if ( xCC.is() )
     278           0 :             return xCC->toTitle( rStr, nPos, nCount, getMyLocale() );
     279             :         else
     280           0 :             return rStr.copy( nPos, nCount );
     281             :     }
     282           0 :     catch ( const Exception& )
     283             :     {
     284             :         SAL_WARN( "unotools.i18n", "titlecase: Exception caught!" );
     285           0 :         return rStr.copy( nPos, nCount );
     286             :     }
     287             : }
     288             : 
     289      181884 : ::rtl::OUString CharClass::uppercase( const ::rtl::OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const
     290             : {
     291             :     try
     292             :     {
     293      181884 :         if ( xCC.is() )
     294      181884 :             return xCC->toUpper( rStr, nPos, nCount, getMyLocale() );
     295             :         else
     296           0 :             return rStr.copy( nPos, nCount );
     297             :     }
     298           0 :     catch ( const Exception& )
     299             :     {
     300             :         SAL_WARN( "unotools.i18n", "uppercase: Exception caught!" );
     301           0 :         return rStr.copy( nPos, nCount );
     302             :     }
     303             : }
     304             : 
     305          17 : ::rtl::OUString CharClass::lowercase( const ::rtl::OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const
     306             : {
     307             :     try
     308             :     {
     309          17 :         if ( xCC.is() )
     310          17 :             return xCC->toLower( rStr, nPos, nCount, getMyLocale() );
     311             :         else
     312           0 :             return rStr.copy( nPos, nCount );
     313             :     }
     314           0 :     catch ( const Exception& )
     315             :     {
     316             :         SAL_WARN( "unotools.i18n", "lowercase: Exception caught!" );
     317           0 :         return rStr.copy( nPos, nCount );
     318             :     }
     319             : }
     320             : 
     321       16987 : sal_Int16 CharClass::getType( const String& rStr, xub_StrLen nPos ) const
     322             : {
     323             :     try
     324             :     {
     325       16987 :         if ( xCC.is() )
     326       16987 :             return xCC->getType( rStr, nPos );
     327             :         else
     328           0 :             return 0;
     329             :     }
     330           0 :     catch ( const Exception& )
     331             :     {
     332             :         SAL_WARN( "unotools.i18n", "getType: Exception caught!" );
     333           0 :         return 0;
     334             :     }
     335             : }
     336             : 
     337             : 
     338           1 : sal_Int16 CharClass::getCharacterDirection( const String& rStr, xub_StrLen nPos ) const
     339             : {
     340             :     try
     341             :     {
     342           1 :         if ( xCC.is() )
     343           1 :             return xCC->getCharacterDirection( rStr, nPos );
     344             :         else
     345           0 :             return 0;
     346             :     }
     347           0 :     catch ( const Exception& )
     348             :     {
     349             :         SAL_WARN( "unotools.i18n", "getCharacterDirection: Exception caught!" );
     350           0 :         return 0;
     351             :     }
     352             : }
     353             : 
     354             : 
     355           0 : sal_Int16 CharClass::getScript( const String& rStr, xub_StrLen nPos ) const
     356             : {
     357             :     try
     358             :     {
     359           0 :         if ( xCC.is() )
     360           0 :             return xCC->getScript( rStr, nPos );
     361             :         else
     362           0 :             return 0;
     363             :     }
     364           0 :     catch ( const Exception& )
     365             :     {
     366             :         SAL_WARN( "unotools.i18n", "getScript: Exception caught!" );
     367           0 :         return 0;
     368             :     }
     369             : }
     370             : 
     371             : 
     372           0 : sal_Int32 CharClass::getCharacterType( const String& rStr, xub_StrLen nPos ) const
     373             : {
     374             :     try
     375             :     {
     376           0 :         if ( xCC.is() )
     377           0 :             return xCC->getCharacterType( rStr, nPos, getMyLocale() );
     378             :         else
     379           0 :             return 0;
     380             :     }
     381           0 :     catch ( const Exception& )
     382             :     {
     383             :         SAL_WARN( "unotools.i18n", "getCharacterType: Exception caught!" );
     384           0 :         return 0;
     385             :     }
     386             : }
     387             : 
     388             : 
     389           0 : sal_Int32 CharClass::getStringType( const String& rStr, xub_StrLen nPos, xub_StrLen nCount ) const
     390             : {
     391             :     try
     392             :     {
     393           0 :         if ( xCC.is() )
     394           0 :             return xCC->getStringType( rStr, nPos, nCount, getMyLocale() );
     395             :         else
     396           0 :             return 0;
     397             :     }
     398           0 :     catch ( const Exception& )
     399             :     {
     400             :         SAL_WARN( "unotools.i18n", "getStringType: Exception caught!" );
     401           0 :         return 0;
     402             :     }
     403             : }
     404             : 
     405             : 
     406       11515 : ::com::sun::star::i18n::ParseResult CharClass::parseAnyToken(
     407             :             const String& rStr,
     408             :             sal_Int32 nPos,
     409             :             sal_Int32 nStartCharFlags,
     410             :             const String& userDefinedCharactersStart,
     411             :             sal_Int32 nContCharFlags,
     412             :             const String& userDefinedCharactersCont ) const
     413             : {
     414             :     try
     415             :     {
     416       11515 :         if ( xCC.is() )
     417       23030 :             return xCC->parseAnyToken( rStr, nPos, getMyLocale(),
     418             :                 nStartCharFlags, userDefinedCharactersStart,
     419       23030 :                 nContCharFlags, userDefinedCharactersCont );
     420             :         else
     421           0 :             return ParseResult();
     422             :     }
     423           0 :     catch ( const Exception& e )
     424             :     {
     425             :         SAL_WARN( "unotools.i18n", "parseAnyToken: Exception caught " << e.Message );
     426           0 :         return ParseResult();
     427             :     }
     428             : }
     429             : 
     430             : 
     431         911 : ::com::sun::star::i18n::ParseResult CharClass::parsePredefinedToken(
     432             :             sal_Int32 nTokenType,
     433             :             const String& rStr,
     434             :             sal_Int32 nPos,
     435             :             sal_Int32 nStartCharFlags,
     436             :             const String& userDefinedCharactersStart,
     437             :             sal_Int32 nContCharFlags,
     438             :             const String& userDefinedCharactersCont ) const
     439             : {
     440             :     try
     441             :     {
     442         911 :         if ( xCC.is() )
     443        1822 :             return xCC->parsePredefinedToken( nTokenType, rStr, nPos, getMyLocale(),
     444             :                 nStartCharFlags, userDefinedCharactersStart,
     445        1822 :                 nContCharFlags, userDefinedCharactersCont );
     446             :         else
     447           0 :             return ParseResult();
     448             :     }
     449           0 :     catch ( const Exception& e )
     450             :     {
     451             :         SAL_WARN( "unotools.i18n", "parsePredefinedToken: Exception caught " << e.Message );
     452           0 :         return ParseResult();
     453             :     }
     454             : }
     455             : 
     456             : 
     457             : 
     458             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10