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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
/*
* Copyright (C) 2013, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef LATINIME_BYTE_ARRAY_UTILS_H
#define LATINIME_BYTE_ARRAY_UTILS_H
#include <cstdint>
#include "defines.h"
namespace latinime {
/**
* Utility methods for reading byte arrays.
*/
class ByteArrayUtils {
public:
/**
* Integer writing
*
* Each method write a corresponding size integer in a big endian manner.
*/
static AK_FORCE_INLINE void writeUintAndAdvancePosition(uint8_t *const buffer,
const uint32_t data, const int size, int *const pos) {
// size must be in 1 to 4.
ASSERT(size >= 1 && size <= 4);
switch (size) {
case 1:
ByteArrayUtils::writeUint8AndAdvancePosition(buffer, data, pos);
return;
case 2:
ByteArrayUtils::writeUint16AndAdvancePosition(buffer, data, pos);
return;
case 3:
ByteArrayUtils::writeUint24AndAdvancePosition(buffer, data, pos);
return;
case 4:
ByteArrayUtils::writeUint32AndAdvancePosition(buffer, data, pos);
return;
default:
break;
}
}
/**
* Integer reading
*
* Each method read a corresponding size integer in a big endian manner.
*/
static AK_FORCE_INLINE uint32_t readUint32(const uint8_t *const buffer, const int pos) {
return (buffer[pos] << 24) ^ (buffer[pos + 1] << 16)
^ (buffer[pos + 2] << 8) ^ buffer[pos + 3];
}
static AK_FORCE_INLINE uint32_t readUint24(const uint8_t *const buffer, const int pos) {
return (buffer[pos] << 16) ^ (buffer[pos + 1] << 8) ^ buffer[pos + 2];
}
static AK_FORCE_INLINE uint16_t readUint16(const uint8_t *const buffer, const int pos) {
return (buffer[pos] << 8) ^ buffer[pos + 1];
}
static AK_FORCE_INLINE uint8_t readUint8(const uint8_t *const buffer, const int pos) {
return buffer[pos];
}
static AK_FORCE_INLINE uint32_t readUint32AndAdvancePosition(
const uint8_t *const buffer, int *const pos) {
const uint32_t value = readUint32(buffer, *pos);
*pos += 4;
return value;
}
static AK_FORCE_INLINE int readSint24AndAdvancePosition(
const uint8_t *const buffer, int *const pos) {
const uint8_t value = readUint8(buffer, *pos);
if (value < 0x80) {
return readUint24AndAdvancePosition(buffer, pos);
} else {
(*pos)++;
return -(((value & 0x7F) << 16) ^ readUint16AndAdvancePosition(buffer, pos));
}
}
static AK_FORCE_INLINE uint32_t readUint24AndAdvancePosition(
const uint8_t *const buffer, int *const pos) {
const uint32_t value = readUint24(buffer, *pos);
*pos += 3;
return value;
}
static AK_FORCE_INLINE uint16_t readUint16AndAdvancePosition(
const uint8_t *const buffer, int *const pos) {
const uint16_t value = readUint16(buffer, *pos);
*pos += 2;
return value;
}
static AK_FORCE_INLINE uint8_t readUint8AndAdvancePosition(
const uint8_t *const buffer, int *const pos) {
return buffer[(*pos)++];
}
static AK_FORCE_INLINE uint32_t readUint(const uint8_t *const buffer,
const int size, const int pos) {
// size must be in 1 to 4.
ASSERT(size >= 1 && size <= 4);
switch (size) {
case 1:
return ByteArrayUtils::readUint8(buffer, pos);
case 2:
return ByteArrayUtils::readUint16(buffer, pos);
case 3:
return ByteArrayUtils::readUint24(buffer, pos);
case 4:
return ByteArrayUtils::readUint32(buffer, pos);
default:
return 0;
}
}
/**
* Code Point Reading
*
* 1 byte = bbbbbbbb match
* case 000xxxxx: xxxxx << 16 + next byte << 8 + next byte
* else: if 00011111 (= 0x1F) : this is the terminator. This is a relevant choice because
* unicode code points range from 0 to 0x10FFFF, so any 3-byte value starting with
* 00011111 would be outside unicode.
* else: iso-latin-1 code
* This allows for the whole unicode range to be encoded, including chars outside of
* the BMP. Also everything in the iso-latin-1 charset is only 1 byte, except control
* characters which should never happen anyway (and still work, but take 3 bytes).
*/
static AK_FORCE_INLINE int readCodePoint(const uint8_t *const buffer, const int pos) {
int p = pos;
return readCodePointAndAdvancePosition(buffer, nullptr /* codePointTable */, &p);
}
static AK_FORCE_INLINE int readCodePointAndAdvancePosition(
const uint8_t *const buffer, const int *const codePointTable, int *const pos) {
/*
* codePointTable is an array to convert the most frequent characters in this dictionary to
* 1 byte code points. It is only made of the original code points of the most frequent
* characters used in this dictionary. 0x20 - 0xFF is used for the 1 byte characters.
* The original code points are restored by picking the code points at the indices of the
* codePointTable. The indices are calculated by subtracting 0x20 from the firstByte.
*/
const uint8_t firstByte = readUint8(buffer, *pos);
if (firstByte < MINIMUM_ONE_BYTE_CHARACTER_VALUE) {
if (firstByte == CHARACTER_ARRAY_TERMINATOR) {
*pos += 1;
return NOT_A_CODE_POINT;
} else {
return readUint24AndAdvancePosition(buffer, pos);
}
} else {
*pos += 1;
if (codePointTable) {
return codePointTable[firstByte - MINIMUM_ONE_BYTE_CHARACTER_VALUE];
}
return firstByte;
}
}
/**
* String (array of code points) Reading
*
* Reads code points until the terminator is found.
*/
// Returns the length of the string.
static int readStringAndAdvancePosition(const uint8_t *const buffer,
const int maxLength, const int *const codePointTable, int *const outBuffer,
int *const pos) {
int length = 0;
int codePoint = readCodePointAndAdvancePosition(buffer, codePointTable, pos);
while (NOT_A_CODE_POINT != codePoint && length < maxLength) {
outBuffer[length++] = codePoint;
codePoint = readCodePointAndAdvancePosition(buffer, codePointTable, pos);
}
return length;
}
// Advances the position and returns the length of the string.
static int advancePositionToBehindString(
const uint8_t *const buffer, const int maxLength, int *const pos) {
int length = 0;
int codePoint = readCodePointAndAdvancePosition(buffer, nullptr /* codePointTable */, pos);
while (NOT_A_CODE_POINT != codePoint && length < maxLength) {
codePoint = readCodePointAndAdvancePosition(buffer, nullptr /* codePointTable */, pos);
length++;
}
return length;
}
/**
* String (array of code points) Writing
*/
static void writeCodePointsAndAdvancePosition(uint8_t *const buffer,
const int *const codePoints, const int codePointCount, const bool writesTerminator,
int *const pos) {
for (int i = 0; i < codePointCount; ++i) {
const int codePoint = codePoints[i];
if (codePoint == NOT_A_CODE_POINT || codePoint == CHARACTER_ARRAY_TERMINATOR) {
break;
} else if (codePoint < MINIMUM_ONE_BYTE_CHARACTER_VALUE
|| codePoint > MAXIMUM_ONE_BYTE_CHARACTER_VALUE) {
// three bytes character.
writeUint24AndAdvancePosition(buffer, codePoint, pos);
} else {
// one byte character.
writeUint8AndAdvancePosition(buffer, codePoint, pos);
}
}
if (writesTerminator) {
writeUint8AndAdvancePosition(buffer, CHARACTER_ARRAY_TERMINATOR, pos);
}
}
static int calculateRequiredByteCountToStoreCodePoints(const int *const codePoints,
const int codePointCount, const bool writesTerminator) {
int byteCount = 0;
for (int i = 0; i < codePointCount; ++i) {
const int codePoint = codePoints[i];
if (codePoint == NOT_A_CODE_POINT || codePoint == CHARACTER_ARRAY_TERMINATOR) {
break;
} else if (codePoint < MINIMUM_ONE_BYTE_CHARACTER_VALUE
|| codePoint > MAXIMUM_ONE_BYTE_CHARACTER_VALUE) {
// three bytes character.
byteCount += 3;
} else {
// one byte character.
byteCount += 1;
}
}
if (writesTerminator) {
// The terminator is one byte.
byteCount += 1;
}
return byteCount;
}
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(ByteArrayUtils);
static const uint8_t MINIMUM_ONE_BYTE_CHARACTER_VALUE;
static const uint8_t MAXIMUM_ONE_BYTE_CHARACTER_VALUE;
static const uint8_t CHARACTER_ARRAY_TERMINATOR;
static AK_FORCE_INLINE void writeUint32AndAdvancePosition(uint8_t *const buffer,
const uint32_t data, int *const pos) {
buffer[(*pos)++] = (data >> 24) & 0xFF;
buffer[(*pos)++] = (data >> 16) & 0xFF;
buffer[(*pos)++] = (data >> 8) & 0xFF;
buffer[(*pos)++] = data & 0xFF;
}
static AK_FORCE_INLINE void writeUint24AndAdvancePosition(uint8_t *const buffer,
const uint32_t data, int *const pos) {
buffer[(*pos)++] = (data >> 16) & 0xFF;
buffer[(*pos)++] = (data >> 8) & 0xFF;
buffer[(*pos)++] = data & 0xFF;
}
static AK_FORCE_INLINE void writeUint16AndAdvancePosition(uint8_t *const buffer,
const uint16_t data, int *const pos) {
buffer[(*pos)++] = (data >> 8) & 0xFF;
buffer[(*pos)++] = data & 0xFF;
}
static AK_FORCE_INLINE void writeUint8AndAdvancePosition(uint8_t *const buffer,
const uint8_t data, int *const pos) {
buffer[(*pos)++] = data & 0xFF;
}
};
} // namespace latinime
#endif /* LATINIME_BYTE_ARRAY_UTILS_H */
|