LCOV - code coverage report
Current view: top level - libreoffice/i18nutil/source/utility - oneToOneMapping.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 39 68 57.4 %
Date: 2012-12-27 Functions: 6 9 66.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             : #include <i18nutil/oneToOneMapping.hxx>
      21             : 
      22             : namespace com { namespace sun { namespace star { namespace i18n {
      23             : 
      24           6 : oneToOneMapping::oneToOneMapping( OneToOneMappingTable_t *rpTable, const size_t rnBytes, const size_t rnUnitSize )
      25             :     : mpTable( rpTable ),
      26           6 :       mnSize( rnBytes / rnUnitSize )
      27             : {
      28           6 : }
      29             : 
      30           6 : oneToOneMapping::~oneToOneMapping()
      31             : {
      32           6 : }
      33             : 
      34           0 : sal_Unicode oneToOneMapping::find(const sal_Unicode nKey) const
      35             : {
      36           0 :     if( mpTable )
      37             :     {
      38             :         // binary search
      39           0 :         int bottom = 0;
      40           0 :         int top = mnSize - 1;
      41             :         int current;
      42             : 
      43           0 :         for (;;) {
      44           0 :             current = (top + bottom) / 2;
      45           0 :             if( nKey < mpTable[current].first )
      46           0 :                 top = current - 1;
      47           0 :             else if( nKey > mpTable[current].first )
      48           0 :                 bottom = current + 1;
      49             :             else
      50           0 :                 return mpTable[current].second;
      51             : 
      52           0 :             if( bottom > top )
      53           0 :                 return sal_Unicode( nKey );
      54             :         }
      55             :     }
      56             :     else
      57           0 :         return sal_Unicode( nKey );
      58             : }
      59             : 
      60           6 : oneToOneMappingWithFlag::oneToOneMappingWithFlag( UnicodePairWithFlag *rpTableWF, const size_t rnSize, const UnicodePairFlag rnFlag )
      61             :     : oneToOneMapping( NULL, rnSize, sizeof(UnicodePairWithFlag) ),
      62             :       mpTableWF ( rpTableWF ),
      63             :       mnFlag    ( rnFlag ),
      64           6 :       mbHasIndex( false )
      65             : {
      66           6 : }
      67             : 
      68          12 : oneToOneMappingWithFlag::~oneToOneMappingWithFlag()
      69             : {
      70           6 :     if( mbHasIndex )
      71        1542 :         for( int i = 0; i < 256; i++ )
      72        1536 :             if( mpIndex[i] )
      73          18 :                 delete [] mpIndex[i];
      74           6 : }
      75             : 
      76             : 
      77           6 : void oneToOneMappingWithFlag::makeIndex()
      78             : {
      79           6 :     if( !mbHasIndex && mpTableWF )
      80             :     {
      81           6 :         int i, j, high, low, current = -1;
      82             : 
      83        1542 :         for( i = 0; i < 256; i++ )
      84        1536 :             mpIndex[i] = NULL;
      85             : 
      86        1344 :         for( size_t k = 0; k < mnSize; k++ )
      87             :         {
      88        1338 :             high = (mpTableWF[k].first >> 8) & 0xFF;
      89        1338 :             low  = (mpTableWF[k].first)      & 0xFF;
      90        1338 :             if( high != current )
      91             :             {
      92          18 :                 current = high;
      93          18 :                 mpIndex[high] = new UnicodePairWithFlag*[256];
      94             : 
      95        4626 :                 for( j = 0; j < 256; j++ )
      96        4608 :                     mpIndex[high][j] = NULL;
      97             :             }
      98        1338 :             mpIndex[high][low] = &mpTableWF[k];
      99             :         }
     100             : 
     101           6 :         mbHasIndex = true;
     102             :     }
     103           6 : }
     104             : 
     105        2766 : sal_Unicode oneToOneMappingWithFlag::find( const sal_Unicode nKey ) const
     106             : {
     107        2766 :     if( mpTableWF )
     108             :     {
     109        2766 :         if( mbHasIndex )
     110             :         {
     111             :             // index search
     112             :             int high, low;
     113        2766 :             high = (nKey >> 8) & 0xFF;
     114        2766 :             low = nKey & 0xFF;
     115        8298 :             if( mpIndex[high] != NULL &&
     116        2766 :                 mpIndex[high][low] != NULL &&
     117        2766 :                 mpIndex[high][low]->flag & mnFlag )
     118        2766 :                 return mpIndex[high][low]->second;
     119             :             else
     120           0 :                 return sal_Unicode( nKey );
     121             :         }
     122             :         else
     123             :         {
     124             :             // binary search
     125           0 :             int bottom = 0;
     126           0 :             int top = mnSize - 1;
     127             :             int current;
     128             : 
     129           0 :             for (;;) {
     130           0 :                 current = (top + bottom) / 2;
     131           0 :                 if( nKey < mpTableWF[current].first )
     132           0 :                     top = current - 1;
     133           0 :                 else if( nKey > mpTableWF[current].first )
     134           0 :                     bottom = current + 1;
     135             :                 else
     136             :                 {
     137           0 :                     if( mpTableWF[current].flag & mnFlag )
     138           0 :                         return mpTableWF[current].second;
     139             :                     else
     140           0 :                         return sal_Unicode( nKey );
     141             :                 }
     142             : 
     143           0 :                 if( bottom > top )
     144           0 :                     return sal_Unicode( nKey );
     145             :             }
     146             :         }
     147             :     }
     148             :     else
     149           0 :         return sal_Unicode( nKey );
     150             : }
     151             : 
     152             : 
     153             : } } } }
     154             : 
     155             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10