Skip to content

Commit 12c91df

Browse files
committed
Improve surrogate handling readability
- add inline function to test and convert surrogates is_surrogate(c), is_hi_surrogate(c), is_lo_surrogate(c), get_hi_surrogate(c), get_lo_surrogate(c), from_surrogate(hi, lo) - use names for BC header offsets and lengths in libregexp.c - remove strict aliasing violations in `lre_exec_backtrack()` - pass all context variables to XXX_CHAR macros in `lre_exec_backtrack()`
1 parent 8d932de commit 12c91df

File tree

3 files changed

+150
-124
lines changed

3 files changed

+150
-124
lines changed

cutils.h

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@
4545
#ifndef countof
4646
#define countof(x) (sizeof(x) / sizeof((x)[0]))
4747
#endif
48-
48+
#ifndef container_of
4949
/* return the pointer of type 'type *' containing 'ptr' as field 'member' */
5050
#define container_of(ptr, type, member) ((type *)((uint8_t *)(ptr) - offsetof(type, member)))
51+
#endif
5152

5253
typedef int BOOL;
5354

@@ -207,17 +208,22 @@ static inline void put_u8(uint8_t *tab, uint8_t val)
207208
*tab = val;
208209
}
209210

211+
#ifndef bswap16
210212
static inline uint16_t bswap16(uint16_t x)
211213
{
212214
return (x >> 8) | (x << 8);
213215
}
216+
#endif
214217

218+
#ifndef bswap32
215219
static inline uint32_t bswap32(uint32_t v)
216220
{
217221
return ((v & 0xff000000) >> 24) | ((v & 0x00ff0000) >> 8) |
218222
((v & 0x0000ff00) << 8) | ((v & 0x000000ff) << 24);
219223
}
224+
#endif
220225

226+
#ifndef bswap64
221227
static inline uint64_t bswap64(uint64_t v)
222228
{
223229
return ((v & ((uint64_t)0xff << (7 * 8))) >> (7 * 8)) |
@@ -229,6 +235,7 @@ static inline uint64_t bswap64(uint64_t v)
229235
((v & ((uint64_t)0xff << (1 * 8))) << (5 * 8)) |
230236
((v & ((uint64_t)0xff << (0 * 8))) << (7 * 8));
231237
}
238+
#endif
232239

233240
/* XXX: should take an extra argument to pass slack information to the caller */
234241
typedef void *DynBufReallocFunc(void *opaque, void *ptr, size_t size);
@@ -278,6 +285,36 @@ static inline void dbuf_set_error(DynBuf *s)
278285
int unicode_to_utf8(uint8_t *buf, unsigned int c);
279286
int unicode_from_utf8(const uint8_t *p, int max_len, const uint8_t **pp);
280287

288+
static inline BOOL is_surrogate(uint32_t c)
289+
{
290+
return (c >> 11) == (0xD800 >> 11); // 0xD800-0xDFFF
291+
}
292+
293+
static inline BOOL is_hi_surrogate(uint32_t c)
294+
{
295+
return (c >> 10) == (0xD800 >> 10); // 0xD800-0xDBFF
296+
}
297+
298+
static inline BOOL is_lo_surrogate(uint32_t c)
299+
{
300+
return (c >> 10) == (0xDC00 >> 10); // 0xDC00-0xDFFF
301+
}
302+
303+
static inline uint32_t get_hi_surrogate(uint32_t c)
304+
{
305+
return (c >> 10) - (0x10000 >> 10) + 0xD800;
306+
}
307+
308+
static inline uint32_t get_lo_surrogate(uint32_t c)
309+
{
310+
return (c & 0x3FF) | 0xDC00;
311+
}
312+
313+
static inline uint32_t from_surrogate(uint32_t hi, uint32_t lo)
314+
{
315+
return 0x10000 + 0x400 * (hi - 0xD800) + (lo - 0xDC00);
316+
}
317+
281318
static inline int from_hex(int c)
282319
{
283320
if (c >= '0' && c <= '9')

0 commit comments

Comments
 (0)