forked from pocoproject/poco
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubleByteEncoding.cpp
More file actions
178 lines (145 loc) · 3.5 KB
/
DoubleByteEncoding.cpp
File metadata and controls
178 lines (145 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//
// DoubleByteEncoding.cpp
//
// Library: Encodings
// Package: Encodings
// Module: DoubleByteEncoding
//
// Copyright (c) 2018, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
// Workaround for an issue with the Visual C++ 2008 STL
// implementation. If iterator debugging is enabled (default
// if _DEBUG is #define'd), std::lower_bound() will fail
// to compile.
// See also: <https://github.com/pocoproject/poco/issues/2220>
#if defined(_MSC_VER) && defined(_DEBUG) && _MSC_VER <= 1500
#define _HAS_ITERATOR_DEBUGGING 0
#endif
#include "Poco/DoubleByteEncoding.h"
#include "Poco/String.h"
#include <algorithm>
namespace Poco {
DoubleByteEncoding::DoubleByteEncoding(const char* names[], const TextEncoding::CharacterMap& charMap, const Mapping mappingTable[], std::size_t mappingTableSize, const Mapping reverseMappingTable[], std::size_t reverseMappingTableSize):
_names(names),
_charMap(charMap),
_mappingTable(mappingTable),
_mappingTableSize(mappingTableSize),
_reverseMappingTable(reverseMappingTable),
_reverseMappingTableSize(reverseMappingTableSize)
{
}
DoubleByteEncoding::~DoubleByteEncoding()
{
}
const char* DoubleByteEncoding::canonicalName() const
{
return _names[0];
}
bool DoubleByteEncoding::isA(const std::string& encodingName) const
{
for (const char** name = _names; *name; ++name)
{
if (Poco::icompare(encodingName, *name) == 0)
return true;
}
return false;
}
const TextEncoding::CharacterMap& DoubleByteEncoding::characterMap() const
{
return _charMap;
}
int DoubleByteEncoding::convert(const unsigned char* bytes) const
{
int n = _charMap[*bytes];
switch (n)
{
case -1:
return -1;
case -2:
return map(static_cast<Poco::UInt16>(bytes[0] << 8) | bytes[1]);
default:
return n;
}
}
int DoubleByteEncoding::convert(int ch, unsigned char* bytes, int length) const
{
int n = reverseMap(ch);
if (n < 0) return 0;
if (!bytes || !length)
{
return n > 0xFF ? 2 : 1;
}
if (n > 0xFF && length < 2) return 0;
if (n > 0xFF)
{
bytes[0] = static_cast<unsigned char>(n >> 8);
bytes[1] = static_cast<unsigned char>(n & 0xFF);
return 2;
}
else
{
bytes[0] = static_cast<unsigned char>(n);
return 1;
}
}
int DoubleByteEncoding::queryConvert(const unsigned char* bytes, int length) const
{
int n = _charMap[*bytes];
switch (n)
{
case -1:
return -1;
case -2:
if (length >= 2)
return map((bytes[0] << 8) | bytes[1]);
else
return -2;
default:
return n;
}
}
int DoubleByteEncoding::sequenceLength(const unsigned char* bytes, int length) const
{
if (1 <= length)
{
int cc = _charMap[*bytes];
if (cc >= 0)
return 1;
else if (cc < -1)
return -cc;
else
return -1;
}
else return -1;
}
struct MappingLessThan
{
bool operator () (const DoubleByteEncoding::Mapping& mapping, const Poco::UInt16& key) const
{
return mapping.from < key;
}
};
int DoubleByteEncoding::map(Poco::UInt16 encoded) const
{
const Mapping* begin = _mappingTable;
const Mapping* end = begin + _mappingTableSize;
const Mapping* it = std::lower_bound(begin, end, encoded, MappingLessThan());
if (it != end && it->from == encoded)
return it->to;
else
return -1;
}
int DoubleByteEncoding::reverseMap(int cp) const
{
const Mapping* begin = _reverseMappingTable;
const Mapping* end = begin + _reverseMappingTableSize;
const Mapping* it = std::lower_bound(begin, end, static_cast<Poco::UInt16>(cp), MappingLessThan());
if (it != end && it->from == cp)
return it->to;
else
return -1;
}
} // namespace Poco