LCOV - code coverage report
Current view: top level - i18nutil/source/utility - oneToOneMapping.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 40 68 58.8 %
Date: 2014-11-03 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         134 : oneToOneMapping::oneToOneMapping( OneToOneMappingTable_t *rpTable, const size_t rnBytes, const size_t rnUnitSize )
      25             :     : mpTable( rpTable ),
      26         134 :       mnSize( rnBytes / rnUnitSize )
      27             : {
      28         134 : }
      29             : 
      30         134 : oneToOneMapping::~oneToOneMapping()
      31             : {
      32         134 : }
      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             :         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           0 :         }
      55             :     }
      56             :     else
      57           0 :         return sal_Unicode( nKey );
      58             : }
      59             : 
      60         134 : oneToOneMappingWithFlag::oneToOneMappingWithFlag( UnicodePairWithFlag *rpTableWF, const size_t rnSize, const UnicodePairFlag rnFlag )
      61             :     : oneToOneMapping( NULL, rnSize, sizeof(UnicodePairWithFlag) ),
      62             :       mpTableWF ( rpTableWF ),
      63             :       mnFlag    ( rnFlag ),
      64         134 :       mbHasIndex( false )
      65             : {
      66         134 : }
      67             : 
      68         268 : oneToOneMappingWithFlag::~oneToOneMappingWithFlag()
      69             : {
      70         134 :     if( mbHasIndex )
      71       34438 :         for( int i = 0; i < 256; i++ )
      72       34304 :             if( mpIndex[i] )
      73         420 :                 delete [] mpIndex[i];
      74         134 : }
      75             : 
      76             : 
      77         152 : void oneToOneMappingWithFlag::makeIndex()
      78             : {
      79         152 :     if( !mbHasIndex && mpTableWF )
      80             :     {
      81         134 :         int i, j, high, low, current = -1;
      82             : 
      83       34438 :         for( i = 0; i < 256; i++ )
      84       34304 :             mpIndex[i] = NULL;
      85             : 
      86       30058 :         for( size_t k = 0; k < mnSize; k++ )
      87             :         {
      88       29924 :             high = (mpTableWF[k].first >> 8) & 0xFF;
      89       29924 :             low  = (mpTableWF[k].first)      & 0xFF;
      90       29924 :             if( high != current )
      91             :             {
      92         420 :                 current = high;
      93         420 :                 mpIndex[high] = new UnicodePairWithFlag*[256];
      94             : 
      95      107940 :                 for( j = 0; j < 256; j++ )
      96      107520 :                     mpIndex[high][j] = NULL;
      97             :             }
      98       29924 :             mpIndex[high][low] = &mpTableWF[k];
      99             :         }
     100             : 
     101         134 :         mbHasIndex = true;
     102             :     }
     103         152 : }
     104             : 
     105      156740 : sal_Unicode oneToOneMappingWithFlag::find( const sal_Unicode nKey ) const
     106             : {
     107      156740 :     if( mpTableWF )
     108             :     {
     109      156740 :         if( mbHasIndex )
     110             :         {
     111             :             // index search
     112             :             int high, low;
     113      156740 :             high = (nKey >> 8) & 0xFF;
     114      156740 :             low = nKey & 0xFF;
     115      313144 :             if( mpIndex[high] != NULL &&
     116      311628 :                 mpIndex[high][low] != NULL &&
     117      155224 :                 mpIndex[high][low]->flag & mnFlag )
     118      155224 :                 return mpIndex[high][low]->second;
     119             :             else
     120        1516 :                 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             :             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           0 :             }
     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