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

Generated by: LCOV version 1.10