LCOV - code coverage report
Current view: top level - i18npool/source/indexentry - indexentrysupplier_default.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 76 156 48.7 %
Date: 2014-04-11 Functions: 12 17 70.6 %
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 <indexentrysupplier_default.hxx>
      21             : #include <localedata.hxx>
      22             : #include <i18nutil/unicode.hxx>
      23             : #include <com/sun/star/i18n/CollatorOptions.hpp>
      24             : 
      25             : using namespace ::com::sun::star::uno;
      26             : using namespace ::com::sun::star::lang;
      27             : using namespace ::rtl;
      28             : 
      29             : namespace com { namespace sun { namespace star { namespace i18n {
      30             : 
      31           5 : IndexEntrySupplier_Unicode::IndexEntrySupplier_Unicode(
      32             :     const com::sun::star::uno::Reference < com::sun::star::uno::XComponentContext >& rxContext ) :
      33           5 :     IndexEntrySupplier_Common(rxContext)
      34             : {
      35           5 :     implementationName = "com.sun.star.i18n.IndexEntrySupplier_Unicode";
      36           5 :     index = new Index(rxContext);
      37           5 : }
      38             : 
      39          15 : IndexEntrySupplier_Unicode::~IndexEntrySupplier_Unicode()
      40             : {
      41           5 :     delete index;
      42          10 : }
      43             : 
      44           5 : sal_Bool SAL_CALL IndexEntrySupplier_Unicode::loadAlgorithm( const lang::Locale& rLocale,
      45             :     const OUString& rAlgorithm, sal_Int32 collatorOptions ) throw (RuntimeException, std::exception)
      46             : {
      47           5 :     index->init(rLocale, rAlgorithm);
      48           5 :     return IndexEntrySupplier_Common::loadAlgorithm(rLocale, rAlgorithm, collatorOptions);
      49             : }
      50             : 
      51           0 : OUString SAL_CALL IndexEntrySupplier_Unicode::getIndexKey( const OUString& rIndexEntry,
      52             :     const OUString& rPhoneticEntry, const lang::Locale& rLocale ) throw (RuntimeException, std::exception)
      53             : {
      54           0 :     return index->getIndexDescription(getEntry(rIndexEntry, rPhoneticEntry, rLocale));
      55             : }
      56             : 
      57           0 : sal_Int16 SAL_CALL IndexEntrySupplier_Unicode::compareIndexEntry(
      58             :     const OUString& rIndexEntry1, const OUString& rPhoneticEntry1, const lang::Locale& rLocale1,
      59             :     const OUString& rIndexEntry2, const OUString& rPhoneticEntry2, const lang::Locale& rLocale2 )
      60             :     throw (RuntimeException, std::exception)
      61             : {
      62             :     sal_Int16 result =
      63           0 :             index->getIndexWeight(getEntry(rIndexEntry1, rPhoneticEntry1, rLocale1)) -
      64           0 :             index->getIndexWeight(getEntry(rIndexEntry2, rPhoneticEntry2, rLocale2));
      65           0 :     if (result == 0)
      66             :         return IndexEntrySupplier_Common::compareIndexEntry(
      67             :                     rIndexEntry1, rPhoneticEntry1, rLocale1,
      68           0 :                     rIndexEntry2, rPhoneticEntry2, rLocale2);
      69           0 :     return result > 0 ? 1 : -1;
      70             : }
      71             : 
      72           0 : OUString SAL_CALL IndexEntrySupplier_Unicode::getIndexCharacter( const OUString& rIndexEntry,
      73             :     const lang::Locale& rLocale, const OUString& rAlgorithm ) throw (RuntimeException, std::exception) {
      74             : 
      75           0 :     if (loadAlgorithm( rLocale, rAlgorithm, CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT))
      76           0 :         return index->getIndexDescription(rIndexEntry);
      77             :     else
      78           0 :         return IndexEntrySupplier_Common::getIndexCharacter(rIndexEntry, rLocale, rAlgorithm);
      79             : }
      80             : 
      81         100 : IndexTable::IndexTable()
      82             :     : start(0)
      83             :     , end(0)
      84         100 :     , table(0)
      85             : {
      86         100 : }
      87             : 
      88         100 : IndexTable::~IndexTable()
      89             : {
      90         100 :     if (table) free(table);
      91         100 : }
      92             : 
      93           5 : void IndexTable::init(sal_Unicode start_, sal_Unicode end_, IndexKey *keys, sal_Int16 key_count, Index *index)
      94             : {
      95           5 :     start=start_;
      96           5 :     end=end_;
      97           5 :     table = (sal_uInt8*) malloc((end-start+1)*sizeof(sal_uInt8));
      98        1285 :     for (sal_Unicode i = start; i <= end; i++) {
      99             :         sal_Int16 j;
     100       36825 :         for (j = 0; j < key_count; j++) {
     101       36165 :             if (keys[j].key > 0 && (i == keys[j].key || index->compare(i, keys[j].key) == 0)) {
     102         620 :                 table[i-start] = sal::static_int_cast<sal_uInt8>(j);
     103         620 :                 break;
     104             :             }
     105             :         }
     106        1280 :         if (j == key_count)
     107         660 :             table[i-start] = 0xFF;
     108             :     }
     109           5 : }
     110             : 
     111           5 : Index::Index(const com::sun::star::uno::Reference < com::sun::star::uno::XComponentContext >& rxContext)
     112             :     : table_count(0)
     113             :     , key_count(0)
     114           5 :     , mkey_count(0)
     115             : {
     116           5 :     collator = new CollatorImpl(rxContext);
     117           5 : }
     118             : 
     119         110 : Index::~Index()
     120             : {
     121           5 :     delete collator;
     122         105 : }
     123             : 
     124       35985 : sal_Int16 Index::compare(sal_Unicode c1, sal_Unicode c2)
     125             : {
     126       35985 :     return sal::static_int_cast<sal_Int16>( collator->compareString(OUString(&c1, 1), OUString(&c2, 1)) );
     127             : }
     128             : 
     129           0 : sal_Int16 Index::getIndexWeight(const OUString& rIndexEntry)
     130             : {
     131           0 :     sal_Int32 startPos=0;
     132           0 :     if (!skipping_chars.isEmpty())
     133           0 :         while (skipping_chars.indexOf(rIndexEntry[startPos]) >= 0)
     134           0 :             startPos++;
     135           0 :     if (mkey_count > 0) {
     136           0 :         for (sal_Int16 i = 0; i < mkey_count; i++) {
     137           0 :             sal_Int32 len = keys[mkeys[i]].mkey.getLength();
     138           0 :             if (collator->compareSubstring(rIndexEntry, startPos, len,
     139           0 :                                     keys[mkeys[i]].mkey, 0, len) == 0)
     140           0 :                 return mkeys[i];
     141             :         }
     142             :     }
     143           0 :     sal_Unicode code = rIndexEntry[startPos];
     144           0 :     for (sal_Int16 i = 0; i < table_count; i++) {
     145           0 :         if (tables[i].start <= code && code <= tables[i].end)
     146           0 :             return tables[i].table[code-tables[i].start];
     147             :     }
     148           0 :     return 0xFF;
     149             : }
     150             : 
     151           0 : OUString Index::getIndexDescription(const OUString& rIndexEntry)
     152             : {
     153           0 :     sal_Int16 wgt = getIndexWeight(rIndexEntry);
     154           0 :     if (wgt < MAX_KEYS) {
     155           0 :         if (!keys[wgt].desc.isEmpty())
     156           0 :             return keys[wgt].desc;
     157           0 :         else if (keys[wgt].key > 0)
     158           0 :             return OUString(&keys[wgt].key, 1);
     159             :         else
     160           0 :             return keys[wgt].mkey;
     161             :     }
     162           0 :     sal_Int32 nPos=0;
     163           0 :     sal_uInt32 indexChar=rIndexEntry.iterateCodePoints(&nPos, 0);
     164           0 :     return OUString(&indexChar, 1);
     165             : }
     166             : 
     167             : #define LOCALE_EN lang::Locale(OUString("en"), OUString(), OUString())
     168             : 
     169           5 : void Index::makeIndexKeys(const lang::Locale &rLocale, const OUString &algorithm) throw (RuntimeException)
     170             : {
     171           5 :     OUString keyStr = LocaleDataImpl().getIndexKeysByAlgorithm(rLocale, algorithm);
     172             : 
     173           5 :     if (keyStr.isEmpty()) {
     174           0 :         keyStr = LocaleDataImpl().getIndexKeysByAlgorithm(LOCALE_EN,
     175           0 :                     LocaleDataImpl().getDefaultIndexAlgorithm(LOCALE_EN));
     176           0 :         if (keyStr.isEmpty())
     177           0 :             throw RuntimeException();
     178             :     }
     179             : 
     180           5 :     sal_Int16 len = sal::static_int_cast<sal_Int16>( keyStr.getLength() );
     181           5 :     mkey_count=key_count=0;
     182           5 :     skipping_chars=OUString();
     183             :     sal_Int16 i, j;
     184             : 
     185          25 :     for (i = 0; i < len && key_count < MAX_KEYS; i++)
     186             :     {
     187          20 :         sal_Unicode curr = keyStr[i];
     188          20 :         sal_Unicode close = ')';
     189             : 
     190          20 :         if (unicode::isWhiteSpace(curr))
     191           0 :             continue;
     192             : 
     193          20 :         switch(curr) {
     194             :             case sal_Unicode('-'):
     195          20 :                 if (key_count > 0 && i + 1 < len ) {
     196         180 :                     for (curr = keyStr[++i]; key_count < MAX_KEYS && keys[key_count-1].key < curr; key_count++) {
     197         170 :                         keys[key_count].key = keys[key_count-1].key+1;
     198         170 :                         keys[key_count].desc = OUString();
     199             :                     }
     200             :                 } else
     201           0 :                     throw RuntimeException();
     202          10 :                 break;
     203             :             case sal_Unicode('['):
     204           0 :                 for (i++; i < len && keyStr[i] != ']'; i++) {
     205           0 :                     if (unicode::isWhiteSpace(keyStr[i])) {
     206           0 :                         continue;
     207           0 :                     } else if (keyStr[i] == '_') {
     208           0 :                         for (curr=keyStr[i-1]+1;  curr <= keyStr[i+1]; curr++)
     209           0 :                             skipping_chars+=OUString(curr);
     210           0 :                         i+=2;
     211             :                     } else {
     212           0 :                         skipping_chars+=OUString(keyStr[i]);
     213             :                     }
     214             :                 }
     215           0 :                 break;
     216             :             case sal_Unicode('{'):
     217           0 :                 close = '}';
     218             :             case sal_Unicode('('):
     219           0 :                 if (key_count > 0) {
     220           0 :                     sal_Int16 end = i+1;
     221           0 :                     for (end=i+1; end < len && keyStr[end] != close; end++) ;
     222             : 
     223           0 :                     if (end >= len) // no found
     224           0 :                         throw RuntimeException();
     225           0 :                     if (close == ')')
     226           0 :                         keys[key_count-1].desc = keyStr.copy(i+1, end-i-1);
     227             :                     else {
     228           0 :                         mkeys[mkey_count++]=key_count;
     229           0 :                         keys[key_count].key = 0;
     230           0 :                         keys[key_count].mkey = keyStr.copy(i+1, end-i-1);
     231           0 :                         keys[key_count++].desc=OUString();
     232             :                     }
     233           0 :                     i=end+1;
     234             :                 } else
     235           0 :                     throw RuntimeException();
     236           0 :                 break;
     237             :             default:
     238          10 :                 keys[key_count].key = curr;
     239          10 :                 keys[key_count++].desc = OUString();
     240          10 :                 break;
     241             :         }
     242             :     }
     243           5 :     for (i = 0; i < mkey_count; i++) {
     244           0 :         for (j=i+1; j < mkey_count; j++) {
     245           0 :             if (keys[mkeys[i]].mkey.getLength() < keys[mkeys[j]].mkey.getLength()) {
     246           0 :                 sal_Int16 k = mkeys[i];
     247           0 :                 mkeys[i] = mkeys[j];
     248           0 :                 mkeys[j] = k;
     249             :             }
     250             :         }
     251           5 :     }
     252           5 : }
     253             : 
     254           5 : void Index::init(const lang::Locale &rLocale, const OUString& algorithm) throw (RuntimeException)
     255             : {
     256           5 :     makeIndexKeys(rLocale, algorithm);
     257             : 
     258           5 :     Sequence< UnicodeScript > scriptList = LocaleDataImpl().getUnicodeScripts( rLocale );
     259             : 
     260           5 :     if (scriptList.getLength() == 0) {
     261           0 :         scriptList = LocaleDataImpl().getUnicodeScripts(LOCALE_EN);
     262           0 :         if (scriptList.getLength() == 0)
     263           0 :             throw RuntimeException();
     264             :     }
     265             : 
     266           5 :     table_count = sal::static_int_cast<sal_Int16>( scriptList.getLength() );
     267           5 :     if (table_count > MAX_TABLES)
     268           0 :         throw RuntimeException();
     269             : 
     270           5 :     collator->loadCollatorAlgorithm(algorithm, rLocale, CollatorOptions::CollatorOptions_IGNORE_CASE_ACCENT);
     271           5 :     sal_Int16 j=0;
     272           5 :     sal_Unicode start = unicode::getUnicodeScriptStart((UnicodeScript)0);
     273           5 :     sal_Unicode end = unicode::getUnicodeScriptEnd((UnicodeScript)0);
     274          10 :     for (sal_Int16 i= (scriptList[0] == (UnicodeScript)0) ? 1 : 0; i< scriptList.getLength(); i++) {
     275           5 :         if (unicode::getUnicodeScriptStart(scriptList[i]) != end+1) {
     276           0 :             tables[j++].init(start, end, keys, key_count, this);
     277           0 :             start = unicode::getUnicodeScriptStart(scriptList[i]);
     278             :         }
     279           5 :         end = unicode::getUnicodeScriptEnd(scriptList[i]);
     280             :     }
     281           5 :     tables[j++].init(start, end, keys, key_count, this);
     282           5 :     table_count = j;
     283           5 : }
     284             : 
     285             : } } } }
     286             : 
     287             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10