LCOV - code coverage report
Current view: top level - usr/local/src/libreoffice/i18npool/source/transliteration - transliteration_body.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 117 141 83.0 %
Date: 2013-07-09 Functions: 14 19 73.7 %
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 <rtl/ustrbuf.hxx>
      22             : #include <i18nutil/casefolding.hxx>
      23             : #include <i18nutil/unicode.hxx>
      24             : 
      25             : #include <comphelper/processfactory.hxx>
      26             : #include <comphelper/string.hxx>
      27             : #include <osl/diagnose.h>
      28             : 
      29             : #include <string.h>
      30             : 
      31             : #include "characterclassificationImpl.hxx"
      32             : #include "breakiteratorImpl.hxx"
      33             : 
      34             : #define TRANSLITERATION_ALL
      35             : #include "transliteration_body.hxx"
      36             : 
      37             : using namespace ::com::sun::star::uno;
      38             : using namespace ::com::sun::star::lang;
      39             : using namespace ::rtl;
      40             : 
      41             : namespace com { namespace sun { namespace star { namespace i18n {
      42             : 
      43             : 
      44       15827 : Transliteration_body::Transliteration_body()
      45             : {
      46       15827 :     nMappingType = 0;
      47       15827 :     transliterationName = "Transliteration_body";
      48       15827 :     implementationName = "com.sun.star.i18n.Transliteration.Transliteration_body";
      49       15827 : }
      50             : 
      51           0 : sal_Int16 SAL_CALL Transliteration_body::getType() throw(RuntimeException)
      52             : {
      53           0 :     return TransliterationType::ONE_TO_ONE;
      54             : }
      55             : 
      56           0 : sal_Bool SAL_CALL Transliteration_body::equals(
      57             :     const OUString& /*str1*/, sal_Int32 /*pos1*/, sal_Int32 /*nCount1*/, sal_Int32& /*nMatch1*/,
      58             :     const OUString& /*str2*/, sal_Int32 /*pos2*/, sal_Int32 /*nCount2*/, sal_Int32& /*nMatch2*/)
      59             :     throw(RuntimeException)
      60             : {
      61           0 :     throw RuntimeException();
      62             : }
      63             : 
      64             : Sequence< OUString > SAL_CALL
      65           0 : Transliteration_body::transliterateRange( const OUString& str1, const OUString& str2 )
      66             :     throw( RuntimeException)
      67             : {
      68           0 :     Sequence< OUString > ostr(2);
      69           0 :     ostr[0] = str1;
      70           0 :     ostr[1] = str2;
      71           0 :     return ostr;
      72             : }
      73             : 
      74             : 
      75          12 : static sal_uInt8 lcl_getMappingTypeForToggleCase( sal_uInt8 nMappingType, sal_Unicode cChar )
      76             : {
      77          12 :     sal_uInt8 nRes = nMappingType;
      78             : 
      79             :     // take care of TOGGLE_CASE transliteration:
      80             :     // nMappingType should not be a combination of flags, thuse we decide now
      81             :     // which one to use.
      82          12 :     if (nMappingType == (MappingTypeLowerToUpper | MappingTypeUpperToLower))
      83             :     {
      84          12 :         const sal_Int16 nType = unicode::getUnicodeType( cChar );
      85          12 :         if (nType & 0x02 /* lower case*/)
      86          10 :             nRes = MappingTypeLowerToUpper;
      87             :         else
      88             :         {
      89             :             // should also work properly for non-upper characters like white spacs, numbers, ...
      90           2 :             nRes = MappingTypeUpperToLower;
      91             :         }
      92             :     }
      93             : 
      94          12 :     return nRes;
      95             : }
      96             : 
      97             : 
      98             : OUString SAL_CALL
      99      756421 : Transliteration_body::transliterate(
     100             :     const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
     101             :     Sequence< sal_Int32 >& offset)
     102             :     throw(RuntimeException)
     103             : {
     104             : 
     105      756421 :     const sal_Unicode *in = inStr.getStr() + startPos;
     106             : 
     107             :     // Two different blocks to eliminate the if(useOffset) condition inside the
     108             :     // inner k loop. Yes, on massive use even such small things do count.
     109      756421 :     if ( useOffset )
     110             :     {
     111         474 :         sal_Int32 nOffCount = 0, i;
     112       14787 :         for (i = 0; i < nCount; i++)
     113             :         {
     114             :             // take care of TOGGLE_CASE transliteration:
     115       14313 :             sal_uInt8 nTmpMappingType = nMappingType;
     116       14313 :             if (nMappingType == (MappingTypeLowerToUpper | MappingTypeUpperToLower))
     117           6 :                 nTmpMappingType = lcl_getMappingTypeForToggleCase( nMappingType, in[i] );
     118             : 
     119       14313 :             const Mapping &map = casefolding::getValue( in, i, nCount, aLocale, nTmpMappingType );
     120       14313 :             nOffCount += map.nmap;
     121             :         }
     122         474 :         rtl_uString* pStr = rtl_uString_alloc(nOffCount);
     123         474 :         sal_Unicode* out = pStr->buffer;
     124             : 
     125         474 :         if ( nOffCount != offset.getLength() )
     126           0 :             offset.realloc( nOffCount );
     127             : 
     128         474 :         sal_Int32 j = 0;
     129         474 :         sal_Int32 * pArr = offset.getArray();
     130       14787 :         for (i = 0; i < nCount; i++)
     131             :         {
     132             :             // take care of TOGGLE_CASE transliteration:
     133       14313 :             sal_uInt8 nTmpMappingType = nMappingType;
     134       14313 :             if (nMappingType == (MappingTypeLowerToUpper | MappingTypeUpperToLower))
     135           6 :                 nTmpMappingType = lcl_getMappingTypeForToggleCase( nMappingType, in[i] );
     136             : 
     137       14313 :             const Mapping &map = casefolding::getValue( in, i, nCount, aLocale, nTmpMappingType );
     138       28626 :             for (sal_Int32 k = 0; k < map.nmap; k++)
     139             :             {
     140       14313 :                 pArr[j] = i + startPos;
     141       14313 :                 out[j++] = map.map[k];
     142             :             }
     143             :         }
     144         474 :         out[j] = 0;
     145             : 
     146         474 :         return OUString( pStr, SAL_NO_ACQUIRE );
     147             :     }
     148             :     else
     149             :     {
     150             :         // In the simple case of no offset sequence used we can eliminate the
     151             :         // first getValue() loop. We could also assume that most calls result
     152             :         // in identical string lengths, thus using a preallocated
     153             :         // OUStringBuffer could be an easy way to assemble the return string
     154             :         // without too much hassle. However, for single characters the
     155             :         // OUStringBuffer::append() method is quite expensive compared to a
     156             :         // simple array operation, so it pays here to copy the final result
     157             :         // instead.
     158             : 
     159             :         // Allocate the max possible buffer. Try to use stack instead of heap,
     160             :         // which would have to be reallocated most times anyways.
     161      755947 :         const sal_Int32 nLocalBuf = 2048;
     162      755947 :         sal_Unicode aLocalBuf[ nLocalBuf * NMAPPINGMAX ], *out = aLocalBuf, *pHeapBuf = NULL;
     163      755947 :         if ( nCount > nLocalBuf )
     164           0 :             out = pHeapBuf = new sal_Unicode[ nCount * NMAPPINGMAX ];
     165             : 
     166      755947 :         sal_Int32 j = 0;
     167     7103170 :         for ( sal_Int32 i = 0; i < nCount; i++)
     168             :         {
     169             :             // take care of TOGGLE_CASE transliteration:
     170     6347223 :             sal_uInt8 nTmpMappingType = nMappingType;
     171     6347223 :             if (nMappingType == (MappingTypeLowerToUpper | MappingTypeUpperToLower))
     172           0 :                 nTmpMappingType = lcl_getMappingTypeForToggleCase( nMappingType, in[i] );
     173             : 
     174     6347223 :             const Mapping &map = casefolding::getValue( in, i, nCount, aLocale, nTmpMappingType );
     175    12694446 :             for (sal_Int32 k = 0; k < map.nmap; k++)
     176             :             {
     177     6347223 :                 out[j++] = map.map[k];
     178             :             }
     179             :         }
     180             : 
     181      755947 :         OUString aRet( out, j );
     182      755947 :         if ( pHeapBuf )
     183           0 :             delete [] pHeapBuf;
     184      755947 :         return aRet;
     185             :     }
     186             : }
     187             : 
     188             : OUString SAL_CALL
     189           0 : Transliteration_body::transliterateChar2String( sal_Unicode inChar ) throw(RuntimeException)
     190             : {
     191           0 :         const Mapping &map = casefolding::getValue(&inChar, 0, 1, aLocale, nMappingType);
     192           0 :         rtl_uString* pStr = rtl_uString_alloc(map.nmap);
     193           0 :         sal_Unicode* out = pStr->buffer;
     194             :         sal_Int32 i;
     195             : 
     196           0 :         for (i = 0; i < map.nmap; i++)
     197           0 :             out[i] = map.map[i];
     198           0 :         out[i] = 0;
     199             : 
     200           0 :         return OUString( pStr, SAL_NO_ACQUIRE );
     201             : }
     202             : 
     203             : sal_Unicode SAL_CALL
     204           8 : Transliteration_body::transliterateChar2Char( sal_Unicode inChar ) throw(MultipleCharsOutputException, RuntimeException)
     205             : {
     206           8 :         const Mapping &map = casefolding::getValue(&inChar, 0, 1, aLocale, nMappingType);
     207           8 :         if (map.nmap > 1)
     208           0 :             throw MultipleCharsOutputException();
     209           8 :         return map.map[0];
     210             : }
     211             : 
     212             : OUString SAL_CALL
     213           0 : Transliteration_body::folding( const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
     214             :     Sequence< sal_Int32 >& offset) throw(RuntimeException)
     215             : {
     216           0 :     return this->transliterate(inStr, startPos, nCount, offset);
     217             : }
     218             : 
     219       15729 : Transliteration_casemapping::Transliteration_casemapping()
     220             : {
     221       15729 :     nMappingType = 0;
     222       15729 :     transliterationName = "casemapping(generic)";
     223       15729 :     implementationName = "com.sun.star.i18n.Transliteration.Transliteration_casemapping";
     224       15729 : }
     225             : 
     226             : void SAL_CALL
     227      755918 : Transliteration_casemapping::setMappingType( const sal_uInt8 rMappingType, const Locale& rLocale )
     228             : {
     229      755918 :     nMappingType = rMappingType;
     230      755918 :     aLocale = rLocale;
     231      755918 : }
     232             : 
     233           2 : Transliteration_u2l::Transliteration_u2l()
     234             : {
     235           2 :     nMappingType = MappingTypeUpperToLower;
     236           2 :     transliterationName = "upper_to_lower(generic)";
     237           2 :     implementationName = "com.sun.star.i18n.Transliteration.Transliteration_u2l";
     238           2 : }
     239             : 
     240           4 : Transliteration_l2u::Transliteration_l2u()
     241             : {
     242           4 :     nMappingType = MappingTypeLowerToUpper;
     243           4 :     transliterationName = "lower_to_upper(generic)";
     244           4 :     implementationName = "com.sun.star.i18n.Transliteration.Transliteration_l2u";
     245           4 : }
     246             : 
     247           1 : Transliteration_togglecase::Transliteration_togglecase()
     248             : {
     249             :     // usually nMappingType must NOT be a combiantion of different flages here,
     250             :     // but we take care of that problem in Transliteration_body::transliterate above
     251             :     // before that value is used. There we will decide which of both is to be used on
     252             :     // a per character basis.
     253           1 :     nMappingType = MappingTypeLowerToUpper | MappingTypeUpperToLower;
     254           1 :     transliterationName = "toggle(generic)";
     255           1 :     implementationName = "com.sun.star.i18n.Transliteration.Transliteration_togglecase";
     256           1 : }
     257             : 
     258           1 : Transliteration_titlecase::Transliteration_titlecase()
     259             : {
     260           1 :     nMappingType = MappingTypeToTitle;
     261           1 :     transliterationName = "title(generic)";
     262           1 :     implementationName = "com.sun.star.i18n.Transliteration.Transliteration_titlecase";
     263           1 : }
     264             : 
     265           2 : static OUString transliterate_titlecase_Impl(
     266             :     const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
     267             :     const Locale &rLocale,
     268             :     Sequence< sal_Int32 >& offset )
     269             :     throw(RuntimeException)
     270             : {
     271           2 :     const OUString aText( inStr.copy( startPos, nCount ) );
     272             : 
     273           2 :     OUString aRes;
     274           2 :     if (!aText.isEmpty())
     275             :     {
     276           2 :         Reference< XComponentContext > xContext = ::comphelper::getProcessComponentContext();
     277           4 :         CharacterClassificationImpl aCharClassImpl( xContext );
     278             : 
     279             :         // because aCharClassImpl.toTitle does not handle ligatures or ß but will raise
     280             :         // an exception we need to handle the first chara manually...
     281             : 
     282             :         // we don't want to change surrogates by accident, thuse we use proper code point iteration
     283           2 :         sal_Int32 nPos = 0;
     284           2 :         sal_uInt32 cFirstChar = aText.iterateCodePoints( &nPos );
     285           4 :         OUString aResolvedLigature( &cFirstChar, 1 ); //lcl_ResolveLigature( cFirstChar ) );
     286             :         // toUpper can be used to properly resolve ligatures and characters like ß
     287           2 :         aResolvedLigature = aCharClassImpl.toUpper( aResolvedLigature, 0, aResolvedLigature.getLength(), rLocale );
     288             :         // since toTitle will leave all-uppercase text unchanged we first need to
     289             :         // use toLower to bring possible 2nd and following charas in lowercase
     290           2 :         aResolvedLigature = aCharClassImpl.toLower( aResolvedLigature, 0, aResolvedLigature.getLength(), rLocale );
     291           2 :         sal_Int32 nResolvedLen = aResolvedLigature.getLength();
     292             : 
     293             :         // now we can properly use toTitle to get the expected result for the resolved string.
     294             :         // The rest of the text should just become lowercase.
     295           2 :         aRes = aCharClassImpl.toTitle( aResolvedLigature, 0, nResolvedLen, rLocale );
     296           2 :         aRes += aCharClassImpl.toLower( aText, 1, aText.getLength() - 1, rLocale );
     297           2 :         offset.realloc( aRes.getLength() );
     298             : 
     299           2 :         sal_Int32 *pOffset = offset.getArray();
     300           2 :         sal_Int32 nLen = offset.getLength();
     301          14 :         for (sal_Int32 i = 0; i < nLen; ++i)
     302             :         {
     303          12 :             sal_Int32 nIdx = 0;
     304          12 :             if (i >= nResolvedLen)
     305          10 :                 nIdx = i - nResolvedLen + 1;
     306          12 :             pOffset[i] = nIdx;
     307           2 :         }
     308             :     }
     309             : #if OSL_DEBUG_LEVEL > 1
     310             :     const sal_Int32 *pCOffset = offset.getConstArray();
     311             :     (void) pCOffset;
     312             : #endif
     313             : 
     314           2 :     return aRes;
     315             : }
     316             : 
     317             : 
     318             : // this function expects to be called on a word-by-word basis,
     319             : // namely that startPos points to the first char of the word
     320           1 : OUString SAL_CALL Transliteration_titlecase::transliterate(
     321             :     const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
     322             :     Sequence< sal_Int32 >& offset )
     323             :     throw(RuntimeException)
     324             : {
     325           1 :     return transliterate_titlecase_Impl( inStr, startPos, nCount, aLocale, offset );
     326             : }
     327             : 
     328             : 
     329           1 : Transliteration_sentencecase::Transliteration_sentencecase()
     330             : {
     331           1 :     nMappingType = MappingTypeToTitle;  // though only to be applied to the first word...
     332           1 :     transliterationName = "sentence(generic)";
     333           1 :     implementationName = "com.sun.star.i18n.Transliteration.Transliteration_sentencecase";
     334           1 : }
     335             : 
     336             : 
     337             : // this function expects to be called on a sentence-by-sentence basis,
     338             : // namely that startPos points to the first word (NOT first char!) in the sentence
     339           1 : OUString SAL_CALL Transliteration_sentencecase::transliterate(
     340             :     const OUString& inStr, sal_Int32 startPos, sal_Int32 nCount,
     341             :     Sequence< sal_Int32 >& offset )
     342             :     throw(RuntimeException)
     343             : {
     344           1 :     return transliterate_titlecase_Impl( inStr, startPos, nCount, aLocale, offset );
     345             : }
     346             : 
     347             : 
     348             : } } } }
     349             : 
     350             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10