tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / i18nutil / source / utility / oneToOneMapping.cxx
blobfbb7054d91cc6f8c7776e0bc07889e2d041336da
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 i18nutil {
24 oneToOneMapping::oneToOneMapping( OneToOneMappingTable_t const *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 nKey;
55 else
56 return nKey;
59 oneToOneMappingWithFlag::oneToOneMappingWithFlag( UnicodePairWithFlag const *rpTableWF, const size_t rnSize, const UnicodePairFlag rnFlag )
60 : oneToOneMapping( nullptr, rnSize, sizeof(UnicodePairWithFlag) ),
61 mpTableWF ( rpTableWF ),
62 mnFlag ( rnFlag ),
63 mbHasIndex( false )
67 oneToOneMappingWithFlag::~oneToOneMappingWithFlag()
71 void oneToOneMappingWithFlag::makeIndex()
73 if( mbHasIndex || !mpTableWF )
74 return;
76 int current = -1;
78 for( size_t k = 0; k < mnSize; k++ )
80 const int high = (mpTableWF[k].first >> 8) & 0xFF;
81 const int low = (mpTableWF[k].first) & 0xFF;
82 if( high != current )
84 current = high;
85 mpIndex[high].reset(new UnicodePairWithFlag const *[256]);
87 for (int j = 0; j < 256; ++j)
88 mpIndex[high][j] = nullptr;
90 mpIndex[high][low] = &mpTableWF[k];
93 mbHasIndex = true;
96 sal_Unicode oneToOneMappingWithFlag::find( const sal_Unicode nKey ) const
98 if( mpTableWF )
100 if( mbHasIndex )
102 // index search
103 int high, low;
104 high = (nKey >> 8) & 0xFF;
105 low = nKey & 0xFF;
106 if( mpIndex[high] != nullptr &&
107 mpIndex[high][low] != nullptr &&
108 mpIndex[high][low]->flag & mnFlag )
109 return mpIndex[high][low]->second;
110 else
111 return nKey;
113 else
115 // binary search
116 int bottom = 0;
117 int top = mnSize - 1;
119 for (;;) {
120 const int current = (top + bottom) / 2;
121 if( nKey < mpTableWF[current].first )
122 top = current - 1;
123 else if( nKey > mpTableWF[current].first )
124 bottom = current + 1;
125 else
127 if( mpTableWF[current].flag & mnFlag )
128 return mpTableWF[current].second;
129 else
130 return nKey;
133 if( bottom > top )
134 return nKey;
138 else
139 return nKey;
145 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */