Debian Codegraph @

Xtree-house/bindings/vendor/src/unicode.h

raw
1 #ifndef TREE_SITTER_UNICODE_H_
2 #define TREE_SITTER_UNICODE_H_
3
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7
8 #include <limits.h>
9 #include <stdint.h>
10
11 #define U_EXPORT
12 #define U_EXPORT2
13 #include "unicode/utf8.h"
14 #include "unicode/utf16.h"
15 #include "portable/endian.h"
16
17 #define U16_NEXT_LE(s, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \
18 (c)=le16toh((s)[(i)++]); \
19 if(U16_IS_LEAD(c)) { \
20 uint16_t __c2; \
21 if((i)!=(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \
22 ++(i); \
23 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
24 } \
25 } \
26 } UPRV_BLOCK_MACRO_END
27
28 #define U16_NEXT_BE(s, i, length, c) UPRV_BLOCK_MACRO_BEGIN { \
29 (c)=be16toh((s)[(i)++]); \
30 if(U16_IS_LEAD(c)) { \
31 uint16_t __c2; \
32 if((i)!=(length) && U16_IS_TRAIL(__c2=(s)[(i)])) { \
33 ++(i); \
34 (c)=U16_GET_SUPPLEMENTARY((c), __c2); \
35 } \
36 } \
37 } UPRV_BLOCK_MACRO_END
38
39 static const int32_t TS_DECODE_ERROR = U_SENTINEL;
40
41 static inline uint32_t ts_decode_utf8(
42 const uint8_t *string,
43 uint32_t length,
44 int32_t *code_point
45 ) {
46 uint32_t i = 0;
47 U8_NEXT(string, i, length, *code_point);
48 return i;
49 }
50
51 static inline uint32_t ts_decode_utf16_le(
52 const uint8_t *string,
53 uint32_t length,
54 int32_t *code_point
55 ) {
56 if (length < 2) {
57 *code_point = TS_DECODE_ERROR;
58 return length;
59 }
60 uint32_t i = 0;
61 // length is in bytes; U16_NEXT indexes into uint16_t*, so its length
62 // parameter must be in code units (length / 2), not bytes.
63 U16_NEXT_LE(((uint16_t *)string), i, length / 2, *code_point);
64 return i * 2;
65 }
66
67 static inline uint32_t ts_decode_utf16_be(
68 const uint8_t *string,
69 uint32_t length,
70 int32_t *code_point
71 ) {
72 if (length < 2) {
73 *code_point = TS_DECODE_ERROR;
74 return length;
75 }
76 uint32_t i = 0;
77 // length is in bytes; U16_NEXT indexes into uint16_t*, so its length
78 // parameter must be in code units (length / 2), not bytes.
79 U16_NEXT_BE(((uint16_t *)string), i, length / 2, *code_point);
80 return i * 2;
81 }
82
83 #ifdef __cplusplus
84 }
85 #endif
86
87 #endif // TREE_SITTER_UNICODE_H_