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

Generated by: LCOV version 1.10