LibreOffice Module i18nutil (master)  1
oneToOneMapping.cxx
Go to the documentation of this file.
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 
21 
22 namespace i18nutil {
23 
24 oneToOneMapping::oneToOneMapping( OneToOneMappingTable_t const *rpTable, const size_t rnBytes, const size_t rnUnitSize )
25  : mpTable( rpTable ),
26  mnSize( rnBytes / rnUnitSize )
27 {
28 }
29 
30 oneToOneMapping::~oneToOneMapping()
31 {
32 }
33 
34 sal_Unicode oneToOneMapping::find(const sal_Unicode nKey) const
35 {
36  if( mpTable )
37  {
38  // binary search
39  int bottom = 0;
40  int top = mnSize - 1;
41 
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;
50 
51  if( bottom > top )
52  return nKey;
53  }
54  }
55  else
56  return nKey;
57 }
58 
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 )
64 {
65 }
66 
68 {
69 }
70 
72 {
73  if( mbHasIndex || !mpTableWF )
74  return;
75 
76  int current = -1;
77 
78  for( size_t k = 0; k < mnSize; k++ )
79  {
80  const int high = (mpTableWF[k].first >> 8) & 0xFF;
81  const int low = (mpTableWF[k].first) & 0xFF;
82  if( high != current )
83  {
84  current = high;
85  mpIndex[high].reset(new UnicodePairWithFlag const *[256]);
86 
87  for (int j = 0; j < 256; ++j)
88  mpIndex[high][j] = nullptr;
89  }
90  mpIndex[high][low] = &mpTableWF[k];
91  }
92 
93  mbHasIndex = true;
94 }
95 
97 {
98  if( mpTableWF )
99  {
100  if( mbHasIndex )
101  {
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;
112  }
113  else
114  {
115  // binary search
116  int bottom = 0;
117  int top = mnSize - 1;
118 
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
126  {
127  if( mpTableWF[current].flag & mnFlag )
128  return mpTableWF[current].second;
129  else
130  return nKey;
131  }
132 
133  if( bottom > top )
134  return nKey;
135  }
136  }
137  }
138  else
139  return nKey;
140 }
141 
142 
143 }
144 
145 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual sal_Unicode find(const sal_Unicode nKey) const override
virtual ~oneToOneMappingWithFlag() override
sal_uInt16 sal_Unicode
UnicodePairWithFlag const * mpTableWF
std::unique_ptr< UnicodePairWithFlag const *[]> mpIndex[256]
sal_Int8 UnicodePairFlag
constexpr OUStringLiteral first
sal_uInt32 mnSize