Bump version to 5.0-14
[LibreOffice.git] / i18nutil / source / utility / oneToOneMapping.cxx
blob3c735427dd536a987964bc589318357624ee5cfd
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;
42 for (;;) {
43 const int current = (top + bottom) / 2;
44 if( nKey < mpTable[current].first )
45 top = current - 1;
46 else if( nKey > mpTable[current].first )
47 bottom = current + 1;
48 else
49 return mpTable[current].second;
51 if( bottom > top )
52 return sal_Unicode( nKey );
55 else
56 return sal_Unicode( nKey );
59 oneToOneMappingWithFlag::oneToOneMappingWithFlag( UnicodePairWithFlag *rpTableWF, const size_t rnSize, const UnicodePairFlag rnFlag )
60 : oneToOneMapping( NULL, rnSize, sizeof(UnicodePairWithFlag) ),
61 mpTableWF ( rpTableWF ),
62 mnFlag ( rnFlag ),
63 mbHasIndex( false )
65 memset(mpIndex, 0, sizeof(mpIndex));
68 oneToOneMappingWithFlag::~oneToOneMappingWithFlag()
70 if( mbHasIndex )
72 for (size_t i = 0; i < SAL_N_ELEMENTS(mpIndex); ++i)
73 delete [] mpIndex[i];
77 void oneToOneMappingWithFlag::makeIndex()
79 if( !mbHasIndex && mpTableWF )
81 int current = -1;
83 for (size_t i = 0; i < SAL_N_ELEMENTS(mpIndex); ++i)
84 mpIndex[i] = NULL;
86 for( size_t k = 0; k < mnSize; k++ )
88 const int high = (mpTableWF[k].first >> 8) & 0xFF;
89 const int low = (mpTableWF[k].first) & 0xFF;
90 if( high != current )
92 current = high;
93 mpIndex[high] = new UnicodePairWithFlag*[256];
95 for (int 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;
128 for (;;) {
129 const int current = (top + bottom) / 2;
130 if( nKey < mpTableWF[current].first )
131 top = current - 1;
132 else if( nKey > mpTableWF[current].first )
133 bottom = current + 1;
134 else
136 if( mpTableWF[current].flag & mnFlag )
137 return mpTableWF[current].second;
138 else
139 return sal_Unicode( nKey );
142 if( bottom > top )
143 return sal_Unicode( nKey );
147 else
148 return sal_Unicode( nKey );
152 } } } }
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */