LCOV - code coverage report
Current view: top level - i18npool/source/indexentry - indexentrysupplier_default.cxx (source / functions) Hit Total Coverage
Test: commit c8344322a7af75b84dd3ca8f78b05543a976dfd5 Lines: 76 156 48.7 %
Date: 2015-06-13 12:38:46 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           5 : IndexEntrySupplier_Unicode::IndexEntrySupplier_Unicode(
      31             :     const com::sun::star::uno::Reference < com::sun::star::uno::XComponentContext >& rxContext ) :
      32           5 :     IndexEntrySupplier_Common(rxContext)
      33             : {
      34           5 :     implementationName = "com.sun.star.i18n.IndexEntrySupplier_Unicode";
      35           5 :     index = new Index(rxContext);
      36           5 : }
      37             : 
      38          15 : IndexEntrySupplier_Unicode::~IndexEntrySupplier_Unicode()
      39             : {
      40           5 :     delete index;
      41          10 : }
      42             : 
      43           5 : sal_Bool SAL_CALL IndexEntrySupplier_Unicode::loadAlgorithm( const lang::Locale& rLocale,
      44             :     const OUString& rAlgorithm, sal_Int32 collatorOptions ) throw (RuntimeException, std::exception)
      45             : {
      46           5 :     index->init(rLocale, rAlgorithm);
      47           5 :     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         100 : IndexTable::IndexTable()
      81             :     : start(0)
      82             :     , end(0)
      83         100 :     , table(0)
      84             : {
      85         100 : }
      86             : 
      87         100 : IndexTable::~IndexTable()
      88             : {
      89         100 :     if (table) free(table);
      90         100 : }
      91             : 
      92           5 : void IndexTable::init(sal_Unicode start_, sal_Unicode end_, IndexKey *keys, sal_Int16 key_count, Index *index)
      93             : {
      94           5 :     start=start_;
      95           5 :     end=end_;
      96           5 :     table = static_cast<sal_uInt8*>(malloc((end-start+1)*sizeof(sal_uInt8)));
      97        1285 :     for (sal_Unicode i = start; i <= end; i++) {
      98             :         sal_Int16 j;
      99       36825 :         for (j = 0; j < key_count; j++) {
     100       36165 :             if (keys[j].key > 0 && (i == keys[j].key || index->compare(i, keys[j].key) == 0)) {
     101         620 :                 table[i-start] = sal::static_int_cast<sal_uInt8>(j);
     102         620 :                 break;
     103             :             }
     104             :         }
     105        1280 :         if (j == key_count)
     106         660 :             table[i-start] = 0xFF;
     107             :     }
     108           5 : }
     109             : 
     110           5 : Index::Index(const com::sun::star::uno::Reference < com::sun::star::uno::XComponentContext >& rxContext)
     111             :     : table_count(0)
     112             :     , key_count(0)
     113           5 :     , mkey_count(0)
     114             : {
     115           5 :     collator = new CollatorImpl(rxContext);
     116           5 : }
     117             : 
     118         110 : Index::~Index()
     119             : {
     120           5 :     delete collator;
     121         105 : }
     122             : 
     123       35985 : sal_Int16 Index::compare(sal_Unicode c1, sal_Unicode c2)
     124             : {
     125       35985 :     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           5 : void Index::makeIndexKeys(const lang::Locale &rLocale, const OUString &algorithm) throw (RuntimeException)
     169             : {
     170           5 :     OUString keyStr = LocaleDataImpl().getIndexKeysByAlgorithm(rLocale, algorithm);
     171             : 
     172           5 :     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           5 :     sal_Int16 len = sal::static_int_cast<sal_Int16>( keyStr.getLength() );
     180           5 :     mkey_count=key_count=0;
     181           5 :     skipping_chars=OUString();
     182             :     sal_Int16 i, j;
     183             : 
     184          25 :     for (i = 0; i < len && key_count < MAX_KEYS; i++)
     185             :     {
     186          20 :         sal_Unicode curr = keyStr[i];
     187          20 :         sal_Unicode close = ')';
     188             : 
     189          20 :         if (unicode::isWhiteSpace(curr))
     190           0 :             continue;
     191             : 
     192          20 :         switch(curr) {
     193             :             case sal_Unicode('-'):
     194          20 :                 if (key_count > 0 && i + 1 < len ) {
     195         180 :                     for (curr = keyStr[++i]; key_count < MAX_KEYS && keys[key_count-1].key < curr; key_count++) {
     196         170 :                         keys[key_count].key = keys[key_count-1].key+1;
     197         170 :                         keys[key_count].desc.clear();
     198             :                     }
     199             :                 } else
     200           0 :                     throw RuntimeException();
     201          10 :                 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.clear();
     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.clear();
     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_Int32 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.11