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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
/** @file
* IPRT - Unicode Code Points.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
#ifndef ___iprt_uni_h
#define ___iprt_uni_h
/** @defgroup grp_rt_uni RTUniCp - Unicode Code Points
* @ingroup grp_rt
* @{
*/
/** @def RTUNI_USE_WCTYPE
* Define RTUNI_USE_WCTYPE to not use the IPRT unicode data but the
* data which the C runtime library provides. */
#ifdef DOXYGEN_RUNNING
# define RTUNI_USE_WCTYPE
#endif
#include <iprt/types.h>
#ifdef RTUNI_USE_WCTYPE
# include <wctype.h>
#endif
RT_C_DECLS_BEGIN
#ifndef RTUNI_USE_WCTYPE
/**
* A unicode flags range.
* @internal
*/
typedef struct RTUNIFLAGSRANGE
{
/** The first code point of the range. */
RTUNICP BeginCP;
/** The last + 1 code point of the range. */
RTUNICP EndCP;
/** Pointer to the array of case folded code points. */
const uint8_t *pafFlags;
} RTUNIFLAGSRANGE;
/** Pointer to a flags range.
* @internal */
typedef RTUNIFLAGSRANGE *PRTUNIFLAGSRANGE;
/** Pointer to a const flags range.
* @internal */
typedef const RTUNIFLAGSRANGE *PCRTUNIFLAGSRANGE;
/**
* A unicode case folded range.
* @internal
*/
typedef struct RTUNICASERANGE
{
/** The first code point of the range. */
RTUNICP BeginCP;
/** The last + 1 code point of the range. */
RTUNICP EndCP;
/** Pointer to the array of case folded code points. */
PCRTUNICP paFoldedCPs;
} RTUNICASERANGE;
/** Pointer to a case folded range.
* @internal */
typedef RTUNICASERANGE *PRTUNICASERANGE;
/** Pointer to a const case folded range.
* @internal */
typedef const RTUNICASERANGE *PCRTUNICASERANGE;
/** @name Unicode Code Point Flags.
* @internal
* @{ */
#define RTUNI_UPPER RT_BIT(0)
#define RTUNI_LOWER RT_BIT(1)
#define RTUNI_ALPHA RT_BIT(2)
#define RTUNI_XDIGIT RT_BIT(3)
#define RTUNI_DDIGIT RT_BIT(4)
#define RTUNI_WSPACE RT_BIT(5)
/*#define RTUNI_BSPACE RT_BIT(6) - later */
/** When set, the codepoint requires further checking wrt NFC and NFD
* normalization. I.e. set when either of QC_NFD and QC_NFC are not Y. */
#define RTUNI_QC_NFX RT_BIT(7)
/** @} */
/**
* Array of flags ranges.
* @internal
*/
extern RTDATADECL(const RTUNIFLAGSRANGE) g_aRTUniFlagsRanges[];
/**
* Gets the flags for a unicode code point.
*
* @returns The flag mask. (RTUNI_*)
* @param CodePoint The unicode code point.
* @internal
*/
DECLINLINE(RTUNICP) rtUniCpFlags(RTUNICP CodePoint)
{
PCRTUNIFLAGSRANGE pCur = &g_aRTUniFlagsRanges[0];
do
{
if (pCur->EndCP > CodePoint)
{
if (pCur->BeginCP <= CodePoint)
return pCur->pafFlags[CodePoint - pCur->BeginCP];
break;
}
pCur++;
} while (pCur->EndCP != RTUNICP_MAX);
return 0;
}
/**
* Checks if a unicode code point is upper case.
*
* @returns true if it is.
* @returns false if it isn't.
* @param CodePoint The code point.
*/
DECLINLINE(bool) RTUniCpIsUpper(RTUNICP CodePoint)
{
return (rtUniCpFlags(CodePoint) & RTUNI_UPPER) != 0;
}
/**
* Checks if a unicode code point is lower case.
*
* @returns true if it is.
* @returns false if it isn't.
* @param CodePoint The code point.
*/
DECLINLINE(bool) RTUniCpIsLower(RTUNICP CodePoint)
{
return (rtUniCpFlags(CodePoint) & RTUNI_LOWER) != 0;
}
/**
* Checks if a unicode code point is case foldable.
*
* @returns true if it is.
* @returns false if it isn't.
* @param CodePoint The code point.
*/
DECLINLINE(bool) RTUniCpIsFoldable(RTUNICP CodePoint)
{
/* Right enough. */
return (rtUniCpFlags(CodePoint) & (RTUNI_LOWER | RTUNI_UPPER)) != 0;
}
/**
* Checks if a unicode code point is alphabetic.
*
* @returns true if it is.
* @returns false if it isn't.
* @param CodePoint The code point.
*/
DECLINLINE(bool) RTUniCpIsAlphabetic(RTUNICP CodePoint)
{
return (rtUniCpFlags(CodePoint) & RTUNI_ALPHA) != 0;
}
/**
* Checks if a unicode code point is a decimal digit.
*
* @returns true if it is.
* @returns false if it isn't.
* @param CodePoint The code point.
*/
DECLINLINE(bool) RTUniCpIsDecDigit(RTUNICP CodePoint)
{
return (rtUniCpFlags(CodePoint) & RTUNI_DDIGIT) != 0;
}
/**
* Checks if a unicode code point is a hexadecimal digit.
*
* @returns true if it is.
* @returns false if it isn't.
* @param CodePoint The code point.
*/
DECLINLINE(bool) RTUniCpIsHexDigit(RTUNICP CodePoint)
{
return (rtUniCpFlags(CodePoint) & RTUNI_XDIGIT) != 0;
}
/**
* Checks if a unicode code point is white space.
*
* @returns true if it is.
* @returns false if it isn't.
* @param CodePoint The code point.
*/
DECLINLINE(bool) RTUniCpIsSpace(RTUNICP CodePoint)
{
return (rtUniCpFlags(CodePoint) & RTUNI_WSPACE) != 0;
}
/**
* Array of uppercase ranges.
* @internal
*/
extern RTDATADECL(const RTUNICASERANGE) g_aRTUniUpperRanges[];
/**
* Array of lowercase ranges.
* @internal
*/
extern RTDATADECL(const RTUNICASERANGE) g_aRTUniLowerRanges[];
/**
* Folds a unicode code point using the specified range array.
*
* @returns FOlded code point.
* @param CodePoint The unicode code point to fold.
* @param pCur The case folding range to use.
*/
DECLINLINE(RTUNICP) rtUniCpFold(RTUNICP CodePoint, PCRTUNICASERANGE pCur)
{
do
{
if (pCur->EndCP > CodePoint)
{
if (pCur->BeginCP <= CodePoint)
CodePoint = pCur->paFoldedCPs[CodePoint - pCur->BeginCP];
break;
}
pCur++;
} while (pCur->EndCP != RTUNICP_MAX);
return CodePoint;
}
/**
* Folds a unicode code point to upper case.
*
* @returns Folded code point.
* @param CodePoint The unicode code point to fold.
*/
DECLINLINE(RTUNICP) RTUniCpToUpper(RTUNICP CodePoint)
{
return rtUniCpFold(CodePoint, &g_aRTUniUpperRanges[0]);
}
/**
* Folds a unicode code point to lower case.
*
* @returns Folded code point.
* @param CodePoint The unicode code point to fold.
*/
DECLINLINE(RTUNICP) RTUniCpToLower(RTUNICP CodePoint)
{
return rtUniCpFold(CodePoint, &g_aRTUniLowerRanges[0]);
}
#else /* RTUNI_USE_WCTYPE */
/**
* Checks if a unicode code point is upper case.
*
* @returns true if it is.
* @returns false if it isn't.
* @param CodePoint The code point.
*/
DECLINLINE(bool) RTUniCpIsUpper(RTUNICP CodePoint)
{
return !!iswupper(CodePoint);
}
/**
* Checks if a unicode code point is lower case.
*
* @returns true if it is.
* @returns false if it isn't.
* @param CodePoint The code point.
*/
DECLINLINE(bool) RTUniCpIsLower(RTUNICP CodePoint)
{
return !!iswlower(CodePoint);
}
/**
* Checks if a unicode code point is case foldable.
*
* @returns true if it is.
* @returns false if it isn't.
* @param CodePoint The code point.
*/
DECLINLINE(bool) RTUniCpIsFoldable(RTUNICP CodePoint)
{
/* Right enough. */
return iswupper(CodePoint) || iswlower(CodePoint);
}
/**
* Checks if a unicode code point is alphabetic.
*
* @returns true if it is.
* @returns false if it isn't.
* @param CodePoint The code point.
*/
DECLINLINE(bool) RTUniCpIsAlphabetic(RTUNICP CodePoint)
{
return !!iswalpha(CodePoint);
}
/**
* Checks if a unicode code point is a decimal digit.
*
* @returns true if it is.
* @returns false if it isn't.
* @param CodePoint The code point.
*/
DECLINLINE(bool) RTUniCpIsDecDigit(RTUNICP CodePoint)
{
return !!iswdigit(CodePoint);
}
/**
* Checks if a unicode code point is a hexadecimal digit.
*
* @returns true if it is.
* @returns false if it isn't.
* @param CodePoint The code point.
*/
DECLINLINE(bool) RTUniCpIsHexDigit(RTUNICP CodePoint)
{
return !!iswxdigit(CodePoint);
}
/**
* Checks if a unicode code point is white space.
*
* @returns true if it is.
* @returns false if it isn't.
* @param CodePoint The code point.
*/
DECLINLINE(bool) RTUniCpIsSpace(RTUNICP CodePoint)
{
return !!iswspace(CodePoint);
}
/**
* Folds a unicode code point to upper case.
*
* @returns Folded code point.
* @param CodePoint The unicode code point to fold.
*/
DECLINLINE(RTUNICP) RTUniCpToUpper(RTUNICP CodePoint)
{
return towupper(CodePoint);
}
/**
* Folds a unicode code point to lower case.
*
* @returns Folded code point.
* @param CodePoint The unicode code point to fold.
*/
DECLINLINE(RTUNICP) RTUniCpToLower(RTUNICP CodePoint)
{
return towlower(CodePoint);
}
#endif /* RTUNI_USE_WCTYPE */
/**
* Frees a unicode string.
*
* @param pusz The string to free.
*/
RTDECL(void) RTUniFree(PRTUNICP pusz);
/**
* Checks if a code point valid.
*
* Any code point (defined or not) within the 17 unicode planes (0 thru 16),
* except surrogates will be considered valid code points by this function.
*
* @returns true if in range, false if not.
* @param CodePoint The unicode code point to validate.
*/
DECLINLINE(bool) RTUniCpIsValid(RTUNICP CodePoint)
{
return CodePoint <= 0x00d7ff
|| ( CodePoint <= 0x10ffff
&& CodePoint >= 0x00e000);
}
/**
* Checks if the given code point is in the BMP range.
*
* Surrogates are not considered in the BMP range by this function.
*
* @returns true if in BMP, false if not.
* @param CodePoint The unicode code point to consider.
*/
DECLINLINE(bool) RTUniCpIsBMP(RTUNICP CodePoint)
{
return CodePoint <= 0xd7ff
|| ( CodePoint <= 0xffff
&& CodePoint >= 0xe000);
}
/**
* Folds a unicode code point to lower case.
*
* @returns Folded code point.
* @param CodePoint The unicode code point to fold.
*/
DECLINLINE(size_t) RTUniCpCalcUtf8Len(RTUNICP CodePoint)
{
if (CodePoint < 0x80)
return 1;
return 2
+ (CodePoint >= 0x00000800)
+ (CodePoint >= 0x00010000)
+ (CodePoint >= 0x00200000)
+ (CodePoint >= 0x04000000)
+ (CodePoint >= 0x80000000) /* illegal */;
}
RT_C_DECLS_END
/** @} */
#endif
|