LCOV - code coverage report
Current view: top level - unotools/source/i18n - charclass.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 84 168 50.0 %
Date: 2014-04-11 Functions: 21 26 80.8 %
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             : #include <comphelper/processfactory.hxx>
      21             : #include <unotools/charclass.hxx>
      22             : #include <rtl/character.hxx>
      23             : #include <tools/debug.hxx>
      24             : 
      25             : #include <com/sun/star/i18n/CharacterClassification.hpp>
      26             : 
      27             : using namespace ::com::sun::star;
      28             : using namespace ::com::sun::star::i18n;
      29             : using namespace ::com::sun::star::uno;
      30             : 
      31        3658 : CharClass::CharClass(
      32             :             const Reference< uno::XComponentContext > & rxContext,
      33             :             const LanguageTag& rLanguageTag
      34             :             )
      35             :     :
      36        3658 :         maLanguageTag( rLanguageTag)
      37             : {
      38        3658 :     xCC = CharacterClassification::create( rxContext );
      39        3658 : }
      40             : 
      41       26467 : CharClass::CharClass(
      42             :             const LanguageTag& rLanguageTag )
      43             :     :
      44       26467 :         maLanguageTag( rLanguageTag)
      45             : {
      46       26467 :     xCC = CharacterClassification::create( comphelper::getProcessComponentContext() );
      47       26467 : }
      48             : 
      49       29687 : CharClass::~CharClass()
      50             : {
      51       29687 : }
      52             : 
      53       95928 : void CharClass::setLanguageTag( const LanguageTag& rLanguageTag )
      54             : {
      55       95928 :     ::osl::MutexGuard aGuard( aMutex );
      56       95928 :     maLanguageTag = rLanguageTag;
      57       95928 : }
      58             : 
      59       38491 : const LanguageTag& CharClass::getLanguageTag() const
      60             : {
      61       38491 :     ::osl::MutexGuard aGuard( aMutex );
      62       38491 :     return maLanguageTag;
      63             : }
      64             : 
      65     1613400 : const ::com::sun::star::lang::Locale& CharClass::getMyLocale() const
      66             : {
      67     1613400 :     ::osl::MutexGuard aGuard( aMutex );
      68     1613400 :     return maLanguageTag.getLocale();
      69             : }
      70             : 
      71             : // static
      72       48442 : bool CharClass::isAsciiNumeric( const OUString& rStr )
      73             : {
      74       48442 :     if ( rStr.isEmpty() )
      75           0 :         return false;
      76       48442 :     const sal_Unicode* p = rStr.getStr();
      77       48442 :     const sal_Unicode* const pStop = p + rStr.getLength();
      78             : 
      79          68 :     do
      80             :     {
      81       48490 :         if ( !rtl::isAsciiDigit( *p ) )
      82       48422 :             return false;
      83             :     }
      84             :     while ( ++p < pStop );
      85             : 
      86          20 :     return true;
      87             : }
      88             : 
      89             : // static
      90           0 : bool CharClass::isAsciiAlpha( const OUString& rStr )
      91             : {
      92           0 :     if ( rStr.isEmpty() )
      93           0 :         return false;
      94           0 :     const sal_Unicode* p = rStr.getStr();
      95           0 :     const sal_Unicode* const pStop = p + rStr.getLength();
      96             : 
      97           0 :     do
      98             :     {
      99           0 :         if ( !rtl::isAsciiAlpha( *p ) )
     100           0 :             return false;
     101             :     }
     102             :     while ( ++p < pStop );
     103             : 
     104           0 :     return true;
     105             : }
     106             : 
     107           0 : bool CharClass::isAlpha( const OUString& rStr, sal_Int32 nPos ) const
     108             : {
     109           0 :     sal_Unicode c = rStr[nPos];
     110           0 :     if ( c < 128 )
     111           0 :         return rtl::isAsciiAlpha( c );
     112             : 
     113             :     try
     114             :     {
     115           0 :         if ( xCC.is() )
     116           0 :             return  (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
     117           0 :                      nCharClassAlphaType) != 0;
     118             :         else
     119           0 :             return false;
     120             :     }
     121           0 :     catch ( const Exception& )
     122             :     {
     123             :         SAL_WARN( "unotools.i18n", "isAlpha: Exception caught!" );
     124           0 :         return false;
     125             :     }
     126             : }
     127             : 
     128      795714 : bool CharClass::isLetter( const OUString& rStr, sal_Int32 nPos ) const
     129             : {
     130      795714 :     sal_Unicode c = rStr[nPos];
     131      795714 :     if ( c < 128 )
     132      794702 :         return rtl::isAsciiAlpha( c );
     133             : 
     134             :     try
     135             :     {
     136        1012 :         if ( xCC.is() )
     137        1012 :             return  (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
     138        1012 :                      nCharClassLetterType) != 0;
     139             :         else
     140           0 :             return false;
     141             :     }
     142           0 :     catch ( const Exception& )
     143             :     {
     144             :         SAL_WARN( "unotools.i18n", "isLetter: Exception caught!" );
     145           0 :         return false;
     146             :     }
     147             : }
     148             : 
     149           0 : bool CharClass::isLetter( const OUString& rStr ) const
     150             : {
     151             :     try
     152             :     {
     153           0 :         if ( xCC.is() )
     154           0 :             return isLetterType( xCC->getStringType( rStr, 0, rStr.getLength(), getMyLocale() ) );
     155             :         else
     156           0 :             return false;
     157             :     }
     158           0 :     catch ( const Exception& )
     159             :     {
     160             :         SAL_WARN( "unotools.i18n", "isLetter: Exception caught!" );
     161           0 :         return false;
     162             :     }
     163             : }
     164             : 
     165         359 : bool CharClass::isDigit( const OUString& rStr, sal_Int32 nPos ) const
     166             : {
     167         359 :     sal_Unicode c = rStr[ nPos ];
     168         359 :     if ( c < 128 )
     169          22 :         return rtl::isAsciiDigit( c );
     170             : 
     171             :     try
     172             :     {
     173         337 :         if ( xCC.is() )
     174         337 :             return  (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
     175         337 :                      KCharacterType::DIGIT) != 0;
     176             :         else
     177           0 :             return false;
     178             :     }
     179           0 :     catch ( const Exception& )
     180             :     {
     181             :         SAL_WARN( "unotools.i18n", "isDigit: Exception caught!" );
     182           0 :         return false;
     183             :     }
     184             : }
     185             : 
     186           0 : bool CharClass::isNumeric( const OUString& rStr ) const
     187             : {
     188             :     try
     189             :     {
     190           0 :         if ( xCC.is() )
     191           0 :             return isNumericType( xCC->getStringType( rStr, 0, rStr.getLength(), getMyLocale() ) );
     192             :         else
     193           0 :             return false;
     194             :     }
     195           0 :     catch ( const Exception& )
     196             :     {
     197             :         SAL_WARN( "unotools.i18n", "isNumeric: Exception caught!" );
     198           0 :         return false;
     199             :     }
     200             : }
     201             : 
     202           0 : bool CharClass::isAlphaNumeric( const OUString& rStr, sal_Int32 nPos ) const
     203             : {
     204           0 :     sal_Unicode c = rStr[nPos];
     205           0 :     if ( c < 128 )
     206           0 :         return rtl::isAsciiAlphanumeric( c );
     207             : 
     208             :     try
     209             :     {
     210           0 :         if ( xCC.is() )
     211           0 :             return  (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
     212           0 :                 (nCharClassAlphaType | KCharacterType::DIGIT)) != 0;
     213             :         else
     214           0 :             return false;
     215             :     }
     216           0 :     catch ( const Exception& )
     217             :     {
     218             :         SAL_WARN( "unotools.i18n", "isAlphaNumeric: Exception caught!" );
     219           0 :         return false;
     220             :     }
     221             : }
     222             : 
     223       48762 : bool CharClass::isLetterNumeric( const OUString& rStr, sal_Int32 nPos ) const
     224             : {
     225       48762 :     sal_Unicode c = rStr[nPos];
     226       48762 :     if ( c < 128 )
     227       47600 :         return rtl::isAsciiAlphanumeric( c );
     228             : 
     229             :     try
     230             :     {
     231        1162 :         if ( xCC.is() )
     232        1162 :             return  (xCC->getCharacterType( rStr, nPos, getMyLocale() ) &
     233        1162 :                      (nCharClassLetterType | KCharacterType::DIGIT)) != 0;
     234             :         else
     235           0 :             return false;
     236             :     }
     237           0 :     catch ( const Exception& )
     238             :     {
     239             :         SAL_WARN( "unotools.i18n", "isLetterNumeric: Exception caught!" );
     240           0 :         return false;
     241             :     }
     242             : }
     243             : 
     244       10993 : bool CharClass::isLetterNumeric( const OUString& rStr ) const
     245             : {
     246             :     try
     247             :     {
     248       10993 :         if ( xCC.is() )
     249       10993 :             return isLetterNumericType( xCC->getStringType( rStr, 0, rStr.getLength(), getMyLocale() ) );
     250             :         else
     251           0 :             return false;
     252             :     }
     253           0 :     catch ( const Exception& )
     254             :     {
     255             :         SAL_WARN( "unotools.i18n", "isLetterNumeric: Exception caught!" );
     256           0 :         return false;
     257             :     }
     258             : }
     259             : 
     260           1 : OUString CharClass::titlecase(const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount) const
     261             : {
     262             :     try
     263             :     {
     264           1 :         if ( xCC.is() )
     265           1 :             return xCC->toTitle( rStr, nPos, nCount, getMyLocale() );
     266             :         else
     267           0 :             return rStr.copy( nPos, nCount );
     268             :     }
     269           0 :     catch ( const Exception& )
     270             :     {
     271             :         SAL_WARN( "unotools.i18n", "titlecase: Exception caught!" );
     272           0 :         return rStr.copy( nPos, nCount );
     273             :     }
     274             : }
     275             : 
     276     1237665 : OUString CharClass::uppercase( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const
     277             : {
     278             :     try
     279             :     {
     280     1237665 :         if ( xCC.is() )
     281     1237665 :             return xCC->toUpper( rStr, nPos, nCount, getMyLocale() );
     282             :         else
     283           0 :             return rStr.copy( nPos, nCount );
     284             :     }
     285           0 :     catch ( const Exception& )
     286             :     {
     287             :         SAL_WARN( "unotools.i18n", "uppercase: Exception caught!" );
     288           0 :         return rStr.copy( nPos, nCount );
     289             :     }
     290             : }
     291             : 
     292       30823 : OUString CharClass::lowercase( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const
     293             : {
     294             :     try
     295             :     {
     296       30823 :         if ( xCC.is() )
     297       30823 :             return xCC->toLower( rStr, nPos, nCount, getMyLocale() );
     298             :         else
     299           0 :             return rStr.copy( nPos, nCount );
     300             :     }
     301           0 :     catch ( const Exception& )
     302             :     {
     303             :         SAL_WARN( "unotools.i18n", "lowercase: Exception caught!" );
     304           0 :         return rStr.copy( nPos, nCount );
     305             :     }
     306             : }
     307             : 
     308       28444 : sal_Int16 CharClass::getType( const OUString& rStr, sal_Int32 nPos ) const
     309             : {
     310             :     try
     311             :     {
     312       28444 :         if ( xCC.is() )
     313       28444 :             return xCC->getType( rStr, nPos );
     314             :         else
     315           0 :             return 0;
     316             :     }
     317           0 :     catch ( const Exception& )
     318             :     {
     319             :         SAL_WARN( "unotools.i18n", "getType: Exception caught!" );
     320           0 :         return 0;
     321             :     }
     322             : }
     323             : 
     324       14457 : sal_Int16 CharClass::getCharacterDirection( const OUString& rStr, sal_Int32 nPos ) const
     325             : {
     326             :     try
     327             :     {
     328       14457 :         if ( xCC.is() )
     329       14457 :             return xCC->getCharacterDirection( rStr, nPos );
     330             :         else
     331           0 :             return 0;
     332             :     }
     333           0 :     catch ( const Exception& )
     334             :     {
     335             :         SAL_WARN( "unotools.i18n", "getCharacterDirection: Exception caught!" );
     336           0 :         return 0;
     337             :     }
     338             : }
     339             : 
     340          18 : sal_Int16 CharClass::getScript( const OUString& rStr, sal_Int32 nPos ) const
     341             : {
     342             :     try
     343             :     {
     344          18 :         if ( xCC.is() )
     345          18 :             return xCC->getScript( rStr, nPos );
     346             :         else
     347           0 :             return 0;
     348             :     }
     349           0 :     catch ( const Exception& )
     350             :     {
     351             :         SAL_WARN( "unotools.i18n", "getScript: Exception caught!" );
     352           0 :         return 0;
     353             :     }
     354             : }
     355             : 
     356      219559 : sal_Int32 CharClass::getCharacterType( const OUString& rStr, sal_Int32 nPos ) const
     357             : {
     358             :     try
     359             :     {
     360      219559 :         if ( xCC.is() )
     361      219559 :             return xCC->getCharacterType( rStr, nPos, getMyLocale() );
     362             :         else
     363           0 :             return 0;
     364             :     }
     365           0 :     catch ( const Exception& )
     366             :     {
     367             :         SAL_WARN( "unotools.i18n", "getCharacterType: Exception caught!" );
     368           0 :         return 0;
     369             :     }
     370             : }
     371             : 
     372        5563 : sal_Int32 CharClass::getStringType( const OUString& rStr, sal_Int32 nPos, sal_Int32 nCount ) const
     373             : {
     374             :     try
     375             :     {
     376        5563 :         if ( xCC.is() )
     377        5563 :             return xCC->getStringType( rStr, nPos, nCount, getMyLocale() );
     378             :         else
     379           0 :             return 0;
     380             :     }
     381           0 :     catch ( const Exception& )
     382             :     {
     383             :         SAL_WARN( "unotools.i18n", "getStringType: Exception caught!" );
     384           0 :         return 0;
     385             :     }
     386             : }
     387             : 
     388       18045 : ::com::sun::star::i18n::ParseResult CharClass::parseAnyToken(
     389             :             const OUString& rStr,
     390             :             sal_Int32 nPos,
     391             :             sal_Int32 nStartCharFlags,
     392             :             const OUString& userDefinedCharactersStart,
     393             :             sal_Int32 nContCharFlags,
     394             :             const OUString& userDefinedCharactersCont ) const
     395             : {
     396             :     try
     397             :     {
     398       18045 :         if ( xCC.is() )
     399       36090 :             return xCC->parseAnyToken( rStr, nPos, getMyLocale(),
     400             :                 nStartCharFlags, userDefinedCharactersStart,
     401       36090 :                 nContCharFlags, userDefinedCharactersCont );
     402             :         else
     403           0 :             return ParseResult();
     404             :     }
     405           0 :     catch ( const Exception& e )
     406             :     {
     407             :         SAL_WARN( "unotools.i18n", "parseAnyToken: Exception caught " << e.Message );
     408           0 :         return ParseResult();
     409             :     }
     410             : }
     411             : 
     412       88240 : ::com::sun::star::i18n::ParseResult CharClass::parsePredefinedToken(
     413             :             sal_Int32 nTokenType,
     414             :             const OUString& rStr,
     415             :             sal_Int32 nPos,
     416             :             sal_Int32 nStartCharFlags,
     417             :             const OUString& userDefinedCharactersStart,
     418             :             sal_Int32 nContCharFlags,
     419             :             const OUString& userDefinedCharactersCont ) const
     420             : {
     421             :     try
     422             :     {
     423       88240 :         if ( xCC.is() )
     424      176480 :             return xCC->parsePredefinedToken( nTokenType, rStr, nPos, getMyLocale(),
     425             :                 nStartCharFlags, userDefinedCharactersStart,
     426      176480 :                 nContCharFlags, userDefinedCharactersCont );
     427             :         else
     428           0 :             return ParseResult();
     429             :     }
     430           0 :     catch ( const Exception& e )
     431             :     {
     432             :         SAL_WARN( "unotools.i18n", "parsePredefinedToken: Exception caught " << e.Message );
     433           0 :         return ParseResult();
     434             :     }
     435             : }
     436             : 
     437             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10