Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / i18nutil / source / utility / oneToOneMapping.cxx
blob39c74ded40bf53692df3ae17e9dfb49dbff21c97
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <i18nutil/oneToOneMapping.hxx>
22 namespace com { namespace sun { namespace star { namespace i18n {
24 oneToOneMapping::oneToOneMapping( OneToOneMappingTable_t *rpTable, const size_t rnBytes, const size_t rnUnitSize )
25 : mpTable( rpTable ),
26 mnSize( rnBytes / rnUnitSize )
30 oneToOneMapping::~oneToOneMapping()
34 sal_Unicode oneToOneMapping::find(const sal_Unicode nKey) const
36 if( mpTable )
38 // binary search
39 int bottom = 0;
40 int top = mnSize - 1;
41 int current;
43 for (;;) {
44 current = (top + bottom) / 2;
45 if( nKey < mpTable[current].first )
46 top = current - 1;
47 else if( nKey > mpTable[current].first )
48 bottom = current + 1;
49 else
50 return mpTable[current].second;
52 if( bottom > top )
53 return sal_Unicode( nKey );
56 else
57 return sal_Unicode( nKey );
60 oneToOneMappingWithFlag::oneToOneMappingWithFlag( UnicodePairWithFlag *rpTableWF, const size_t rnSize, const UnicodePairFlag rnFlag )
61 : oneToOneMapping( NULL, rnSize, sizeof(UnicodePairWithFlag) ),
62 mpTableWF ( rpTableWF ),
63 mnFlag ( rnFlag ),
64 mbHasIndex( false )
68 oneToOneMappingWithFlag::~oneToOneMappingWithFlag()
70 if( mbHasIndex )
71 for( int i = 0; i < 256; i++ )
72 if( mpIndex[i] )
73 delete [] mpIndex[i];
77 void oneToOneMappingWithFlag::makeIndex()
79 if( !mbHasIndex && mpTableWF )
81 int i, j, high, low, current = -1;
83 for( i = 0; i < 256; i++ )
84 mpIndex[i] = NULL;
86 for( size_t k = 0; k < mnSize; k++ )
88 high = (mpTableWF[k].first >> 8) & 0xFF;
89 low = (mpTableWF[k].first) & 0xFF;
90 if( high != current )
92 current = high;
93 mpIndex[high] = new UnicodePairWithFlag*[256];
95 for( j = 0; j < 256; j++ )
96 mpIndex[high][j] = NULL;
98 mpIndex[high][low] = &mpTableWF[k];
101 mbHasIndex = true;
105 sal_Unicode oneToOneMappingWithFlag::find( const sal_Unicode nKey ) const
107 if( mpTableWF )
109 if( mbHasIndex )
111 // index search
112 int high, low;
113 high = (nKey >> 8) & 0xFF;
114 low = nKey & 0xFF;
115 if( mpIndex[high] != NULL &&
116 mpIndex[high][low] != NULL &&
117 mpIndex[high][low]->flag & mnFlag )
118 return mpIndex[high][low]->second;
119 else
120 return sal_Unicode( nKey );
122 else
124 // binary search
125 int bottom = 0;
126 int top = mnSize - 1;
127 int current;
129 for (;;) {
130 current = (top + bottom) / 2;
131 if( nKey < mpTableWF[current].first )
132 top = current - 1;
133 else if( nKey > mpTableWF[current].first )
134 bottom = current + 1;
135 else
137 if( mpTableWF[current].flag & mnFlag )
138 return mpTableWF[current].second;
139 else
140 return sal_Unicode( nKey );
143 if( bottom > top )
144 return sal_Unicode( nKey );
148 else
149 return sal_Unicode( nKey );
153 } } } }
155 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */