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 0 : oneToOneMapping::oneToOneMapping( OneToOneMappingTable_t *rpTable, const size_t rnBytes, const size_t rnUnitSize )
25 : : mpTable( rpTable ),
26 0 : mnSize( rnBytes / rnUnitSize )
27 : {
28 0 : }
29 :
30 0 : oneToOneMapping::~oneToOneMapping()
31 : {
32 0 : }
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 0 : oneToOneMappingWithFlag::oneToOneMappingWithFlag( UnicodePairWithFlag *rpTableWF, const size_t rnSize, const UnicodePairFlag rnFlag )
61 : : oneToOneMapping( NULL, rnSize, sizeof(UnicodePairWithFlag) ),
62 : mpTableWF ( rpTableWF ),
63 : mnFlag ( rnFlag ),
64 0 : mbHasIndex( false )
65 : {
66 0 : }
67 :
68 0 : oneToOneMappingWithFlag::~oneToOneMappingWithFlag()
69 : {
70 0 : if( mbHasIndex )
71 0 : for( int i = 0; i < 256; i++ )
72 0 : if( mpIndex[i] )
73 0 : delete [] mpIndex[i];
74 0 : }
75 :
76 :
77 0 : void oneToOneMappingWithFlag::makeIndex()
78 : {
79 0 : if( !mbHasIndex && mpTableWF )
80 : {
81 0 : int i, j, high, low, current = -1;
82 :
83 0 : for( i = 0; i < 256; i++ )
84 0 : mpIndex[i] = NULL;
85 :
86 0 : for( size_t k = 0; k < mnSize; k++ )
87 : {
88 0 : high = (mpTableWF[k].first >> 8) & 0xFF;
89 0 : low = (mpTableWF[k].first) & 0xFF;
90 0 : if( high != current )
91 : {
92 0 : current = high;
93 0 : mpIndex[high] = new UnicodePairWithFlag*[256];
94 :
95 0 : for( j = 0; j < 256; j++ )
96 0 : mpIndex[high][j] = NULL;
97 : }
98 0 : mpIndex[high][low] = &mpTableWF[k];
99 : }
100 :
101 0 : mbHasIndex = true;
102 : }
103 0 : }
104 :
105 0 : sal_Unicode oneToOneMappingWithFlag::find( const sal_Unicode nKey ) const
106 : {
107 0 : if( mpTableWF )
108 : {
109 0 : if( mbHasIndex )
110 : {
111 : // index search
112 : int high, low;
113 0 : high = (nKey >> 8) & 0xFF;
114 0 : low = nKey & 0xFF;
115 0 : if( mpIndex[high] != NULL &&
116 0 : mpIndex[high][low] != NULL &&
117 0 : mpIndex[high][low]->flag & mnFlag )
118 0 : 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 : 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: */
|