2 * Copyright © 2007,2008,2009 Red Hat, Inc.
3 * Copyright © 2011,2012 Google, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Red Hat Author(s): Behdad Esfahbod
26 * Google Author(s): Behdad Esfahbod
48 /* We only use these two for debug output. However, the debug code is
49 * always seen by the compiler (and optimized out in non-debug builds.
50 * If including these becomes a problem, we can start thinking about
51 * someway around that. */
61 # define NULL ((void *) 0)
67 typedef const _hb_void_t
&hb_void_t
;
68 #define HB_VOID (* (const _hb_void_t *) NULL)
75 template <typename Type
>
76 static inline Type
MIN (const Type
&a
, const Type
&b
) { return a
< b
? a
: b
; }
79 template <typename Type
>
80 static inline Type
MAX (const Type
&a
, const Type
&b
) { return a
> b
? a
: b
; }
82 static inline unsigned int DIV_CEIL (const unsigned int a
, unsigned int b
)
83 { return (a
+ (b
- 1)) / b
; }
87 template <typename Type
, unsigned int n
>
88 static inline unsigned int ARRAY_LENGTH (const Type (&)[n
]) { return n
; }
89 /* A const version, but does not detect erratically being called on pointers. */
90 #define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0])))
92 #define HB_STMT_START do
93 #define HB_STMT_END while (0)
95 #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1]
96 #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond))
97 #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond))
99 #define ASSERT_STATIC_EXPR(_cond)((void) sizeof (char[(_cond) ? 1 : -1]))
100 #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1]))
102 #define _PASTE1(a,b) a##b
103 #define PASTE(a,b) _PASTE1(a,b)
105 /* Lets assert int types. Saves trouble down the road. */
107 ASSERT_STATIC (sizeof (int8_t) == 1);
108 ASSERT_STATIC (sizeof (uint8_t) == 1);
109 ASSERT_STATIC (sizeof (int16_t) == 2);
110 ASSERT_STATIC (sizeof (uint16_t) == 2);
111 ASSERT_STATIC (sizeof (int32_t) == 4);
112 ASSERT_STATIC (sizeof (uint32_t) == 4);
113 ASSERT_STATIC (sizeof (int64_t) == 8);
114 ASSERT_STATIC (sizeof (uint64_t) == 8);
116 ASSERT_STATIC (sizeof (hb_codepoint_t
) == 4);
117 ASSERT_STATIC (sizeof (hb_position_t
) == 4);
118 ASSERT_STATIC (sizeof (hb_mask_t
) == 4);
119 ASSERT_STATIC (sizeof (hb_var_int_t
) == 4);
122 /* We like our types POD */
124 #define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; }
125 #define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type)
126 #define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type)
129 # define _ASSERT_INSTANCE_POD1(_line, _instance) \
131 typedef __typeof__(_instance) _type_##_line; \
132 _ASSERT_TYPE_POD1 (_line, _type_##_line); \
135 # define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested
137 # define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance)
138 # define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance)
140 /* Check _assertion in a method environment */
141 #define _ASSERT_POD1(_line) \
142 inline void _static_assertion_on_line_##_line (void) const \
143 { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ }
144 # define _ASSERT_POD0(_line) _ASSERT_POD1 (_line)
145 # define ASSERT_POD() _ASSERT_POD0 (__LINE__)
152 #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__)
153 #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0)
154 #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1))
155 #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0))
157 #define likely(expr) (expr)
158 #define unlikely(expr) (expr)
163 #define __attribute__(x)
167 #define HB_PURE_FUNC __attribute__((pure))
168 #define HB_CONST_FUNC __attribute__((const))
169 #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
172 #define HB_CONST_FUNC
173 #define HB_PRINTF_FUNC(format_idx, arg_idx)
176 #define HB_UNUSED __attribute__((unused))
183 # define HB_INTERNAL __attribute__((__visibility__("hidden")))
190 #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER)
191 #define snprintf _snprintf
196 #define inline __inline
199 #ifdef __STRICT_ANSI__
201 #define inline __inline__
206 #define HB_FUNC __PRETTY_FUNCTION__
207 #elif defined(_MSC_VER)
208 #define HB_FUNC __FUNCSIG__
210 #define HB_FUNC __func__
214 /* Return the number of 1 bits in mask. */
215 static inline HB_CONST_FUNC
unsigned int
216 _hb_popcount32 (uint32_t mask
)
218 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
219 return __builtin_popcount (mask
);
223 y
= (mask
>> 1) &033333333333;
224 y
= mask
- y
- ((y
>>1) & 033333333333);
225 return (((y
+ (y
>> 3)) & 030707070707) % 077);
229 /* Returns the number of bits needed to store number */
230 static inline HB_CONST_FUNC
unsigned int
231 _hb_bit_storage (unsigned int number
)
233 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
234 return likely (number
) ? (sizeof (unsigned int) * 8 - __builtin_clz (number
)) : 0;
236 register unsigned int n_bits
= 0;
245 /* Returns the number of zero bits in the least significant side of number */
246 static inline HB_CONST_FUNC
unsigned int
247 _hb_ctz (unsigned int number
)
249 #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__)
250 return likely (number
) ? __builtin_ctz (number
) : 0;
252 register unsigned int n_bits
= 0;
253 if (unlikely (!number
)) return 0;
254 while (!(number
& 1)) {
263 _hb_unsigned_int_mul_overflows (unsigned int count
, unsigned int size
)
265 return (size
> 0) && (count
>= ((unsigned int) -1) / size
);
269 /* Type of bsearch() / qsort() compare function */
270 typedef int (*hb_compare_func_t
) (const void *, const void *);
275 /* arrays and maps */
278 #define HB_PREALLOCED_ARRAY_INIT {0}
279 template <typename Type
, unsigned int StaticSize
>
280 struct hb_prealloced_array_t
283 unsigned int allocated
;
285 Type static_array
[StaticSize
];
287 void init (void) { memset (this, 0, sizeof (*this)); }
289 inline Type
& operator [] (unsigned int i
) { return array
[i
]; }
290 inline const Type
& operator [] (unsigned int i
) const { return array
[i
]; }
292 inline Type
*push (void)
295 array
= static_array
;
296 allocated
= ARRAY_LENGTH (static_array
);
298 if (likely (len
< allocated
))
299 return &array
[len
++];
301 /* Need to reallocate */
302 unsigned int new_allocated
= allocated
+ (allocated
>> 1) + 8;
303 Type
*new_array
= NULL
;
305 if (array
== static_array
) {
306 new_array
= (Type
*) calloc (new_allocated
, sizeof (Type
));
308 memcpy (new_array
, array
, len
* sizeof (Type
));
310 bool overflows
= (new_allocated
< allocated
) || _hb_unsigned_int_mul_overflows (new_allocated
, sizeof (Type
));
311 if (likely (!overflows
)) {
312 new_array
= (Type
*) realloc (array
, new_allocated
* sizeof (Type
));
316 if (unlikely (!new_array
))
320 allocated
= new_allocated
;
321 return &array
[len
++];
324 inline void pop (void)
329 inline void remove (unsigned int i
)
331 if (unlikely (i
>= len
))
333 memmove (static_cast<void *> (&array
[i
]),
334 static_cast<void *> (&array
[i
+ 1]),
335 (len
- i
- 1) * sizeof (Type
));
339 inline void shrink (unsigned int l
)
345 template <typename T
>
346 inline Type
*find (T v
) {
347 for (unsigned int i
= 0; i
< len
; i
++)
352 template <typename T
>
353 inline const Type
*find (T v
) const {
354 for (unsigned int i
= 0; i
< len
; i
++)
360 inline void sort (void)
362 qsort (array
, len
, sizeof (Type
), (hb_compare_func_t
) Type::cmp
);
365 inline void sort (unsigned int start
, unsigned int end
)
367 qsort (array
+ start
, end
- start
, sizeof (Type
), (hb_compare_func_t
) Type::cmp
);
370 template <typename T
>
371 inline Type
*bsearch (T
*key
)
373 return (Type
*) ::bsearch (key
, array
, len
, sizeof (Type
), (hb_compare_func_t
) Type::cmp
);
375 template <typename T
>
376 inline const Type
*bsearch (T
*key
) const
378 return (const Type
*) ::bsearch (key
, array
, len
, sizeof (Type
), (hb_compare_func_t
) Type::cmp
);
381 inline void finish (void)
383 if (array
!= static_array
)
390 #define HB_AUTO_ARRAY_PREALLOCED 16
391 template <typename Type
>
392 struct hb_auto_array_t
: hb_prealloced_array_t
<Type
, HB_AUTO_ARRAY_PREALLOCED
>
394 hb_auto_array_t (void) { hb_prealloced_array_t
<Type
, HB_AUTO_ARRAY_PREALLOCED
>::init (); }
395 ~hb_auto_array_t (void) { hb_prealloced_array_t
<Type
, HB_AUTO_ARRAY_PREALLOCED
>::finish (); }
399 #define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT}
400 template <typename item_t
, typename lock_t
>
401 struct hb_lockable_set_t
403 hb_prealloced_array_t
<item_t
, 2> items
;
405 inline void init (void) { items
.init (); }
407 template <typename T
>
408 inline item_t
*replace_or_insert (T v
, lock_t
&l
, bool replace
)
411 item_t
*item
= items
.find (v
);
424 item
= items
.push ();
432 template <typename T
>
433 inline void remove (T v
, lock_t
&l
)
436 item_t
*item
= items
.find (v
);
439 *item
= items
[items
.len
- 1];
448 template <typename T
>
449 inline bool find (T v
, item_t
*i
, lock_t
&l
)
452 item_t
*item
= items
.find (v
);
459 template <typename T
>
460 inline item_t
*find_or_insert (T v
, lock_t
&l
)
463 item_t
*item
= items
.find (v
);
465 item
= items
.push ();
473 inline void finish (lock_t
&l
)
476 /* No need for locking. */
482 item_t old
= items
[items
.len
- 1];
497 /* Big-endian handling */
499 static inline uint16_t hb_be_uint16 (const uint16_t v
)
501 const uint8_t *V
= (const uint8_t *) &v
;
502 return (V
[0] << 8) | V
[1];
505 static inline uint16_t hb_uint16_swap (const uint16_t v
)
507 return (v
>> 8) | (v
<< 8);
510 static inline uint32_t hb_uint32_swap (const uint32_t v
)
512 return (hb_uint16_swap (v
) << 16) | hb_uint16_swap (v
>> 16);
515 /* Note, of the following macros, uint16_get is the one called many many times.
516 * If there is any optimizations to be done, it's in that macro. However, I
517 * already confirmed that on my T400 ThinkPad at least, using bswap_16(), which
518 * results in a single ror instruction, does NOT speed this up. In fact, it
519 * resulted in a minor slowdown. At any rate, note that v may not be correctly
520 * aligned, so I think the current implementation is optimal.
523 #define hb_be_uint16_put(v,V) HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END
524 #define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1])
525 #define hb_be_uint16_eq(a,b) (a[0] == b[0] && a[1] == b[1])
527 #define hb_be_uint32_put(v,V) HB_STMT_START { v[0] = (V>>24); v[1] = (V>>16); v[2] = (V>>8); v[3] = (V); } HB_STMT_END
528 #define hb_be_uint32_get(v) (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3])
529 #define hb_be_uint32_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3])
531 #define hb_be_uint24_put(v,V) HB_STMT_START { v[0] = (V>>16); v[1] = (V>>8); v[2] = (V); } HB_STMT_END
532 #define hb_be_uint24_get(v) (uint32_t) ((v[0] << 16) + (v[1] << 8) + v[2])
533 #define hb_be_uint24_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2])
536 /* ASCII tag/character handling */
538 static inline bool ISALPHA (unsigned char c
)
539 { return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z'); }
540 static inline bool ISALNUM (unsigned char c
)
541 { return (c
>= 'a' && c
<= 'z') || (c
>= 'A' && c
<= 'Z') || (c
>= '0' && c
<= '9'); }
542 static inline bool ISSPACE (unsigned char c
)
543 { return c
== ' ' || c
=='\f'|| c
=='\n'|| c
=='\r'|| c
=='\t'|| c
=='\v'; }
544 static inline unsigned char TOUPPER (unsigned char c
)
545 { return (c
>= 'a' && c
<= 'z') ? c
- 'a' + 'A' : c
; }
546 static inline unsigned char TOLOWER (unsigned char c
)
547 { return (c
>= 'A' && c
<= 'Z') ? c
- 'A' + 'a' : c
; }
549 #define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \
550 ((const char *) s)[1], \
551 ((const char *) s)[2], \
552 ((const char *) s)[3]))
557 /* Makes class uncopyable. Use in private: section. */
560 T &operator = (const T &o)
571 _hb_debug (unsigned int level
,
572 unsigned int max_level
)
574 return level
< max_level
;
577 #define DEBUG_LEVEL_ENABLED(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT))
578 #define DEBUG_ENABLED(WHAT) (DEBUG_LEVEL_ENABLED (WHAT, 0))
580 template <int max_level
> static inline void
581 _hb_debug_msg_va (const char *what
,
590 if (!_hb_debug (level
, max_level
))
593 fprintf (stderr
, "%-10s", what
? what
: "");
596 fprintf (stderr
, "(%0*lx) ", (unsigned int) (2 * sizeof (void *)), (unsigned long) obj
);
598 fprintf (stderr
, " %*s ", (unsigned int) (2 * sizeof (void *)), "");
601 /* One may want to add ASCII version of these. See:
602 * https://bugs.freedesktop.org/show_bug.cgi?id=50970 */
603 #define VBAR "\342\224\202" /* U+2502 BOX DRAWINGS LIGHT VERTICAL */
604 #define VRBAR "\342\224\234" /* U+251C BOX DRAWINGS LIGHT VERTICAL AND RIGHT */
605 #define DLBAR "\342\225\256" /* U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT */
606 #define ULBAR "\342\225\257" /* U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT */
607 #define LBAR "\342\225\264" /* U+2574 BOX DRAWINGS LIGHT LEFT */
608 static const char bars
[] = VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR VBAR
;
609 fprintf (stderr
, "%2u %s" VRBAR
"%s",
611 bars
+ sizeof (bars
) - 1 - MIN ((unsigned int) sizeof (bars
), (unsigned int) (sizeof (VBAR
) - 1) * level
),
612 level_dir
? (level_dir
> 0 ? DLBAR
: ULBAR
) : LBAR
);
614 fprintf (stderr
, " " VRBAR LBAR
);
618 unsigned int func_len
= strlen (func
);
619 #ifndef HB_DEBUG_VERBOSE
620 /* Skip "typename" */
621 if (0 == strncmp (func
, "typename ", 9))
623 /* Skip return type */
624 const char *space
= strchr (func
, ' ');
627 /* Skip parameter list */
628 const char *paren
= strchr (func
, '(');
630 func_len
= paren
- func
;
632 fprintf (stderr
, "%.*s: ", func_len
, func
);
636 vfprintf (stderr
, message
, ap
);
638 fprintf (stderr
, "\n");
640 template <> inline void
641 _hb_debug_msg_va
<0> (const char *what HB_UNUSED
,
642 const void *obj HB_UNUSED
,
643 const char *func HB_UNUSED
,
644 bool indented HB_UNUSED
,
645 unsigned int level HB_UNUSED
,
646 int level_dir HB_UNUSED
,
647 const char *message HB_UNUSED
,
648 va_list ap HB_UNUSED
) {}
650 template <int max_level
> static inline void
651 _hb_debug_msg (const char *what
,
658 ...) HB_PRINTF_FUNC(7, 8);
659 template <int max_level
> static inline void
660 _hb_debug_msg (const char *what
,
670 va_start (ap
, message
);
671 _hb_debug_msg_va
<max_level
> (what
, obj
, func
, indented
, level
, level_dir
, message
, ap
);
674 template <> inline void
675 _hb_debug_msg
<0> (const char *what HB_UNUSED
,
676 const void *obj HB_UNUSED
,
677 const char *func HB_UNUSED
,
678 bool indented HB_UNUSED
,
679 unsigned int level HB_UNUSED
,
680 int level_dir HB_UNUSED
,
681 const char *message HB_UNUSED
,
682 ...) HB_PRINTF_FUNC(7, 8);
683 template <> inline void
684 _hb_debug_msg
<0> (const char *what HB_UNUSED
,
685 const void *obj HB_UNUSED
,
686 const char *func HB_UNUSED
,
687 bool indented HB_UNUSED
,
688 unsigned int level HB_UNUSED
,
689 int level_dir HB_UNUSED
,
690 const char *message HB_UNUSED
,
693 #define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, true, (LEVEL), (LEVEL_DIR), __VA_ARGS__)
694 #define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, false, 0, 0, __VA_ARGS__)
695 #define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, false, 0, 0, __VA_ARGS__)
702 template <typename T
>
703 struct hb_printer_t
{};
706 struct hb_printer_t
<bool> {
707 const char *print (bool v
) { return v
? "true" : "false"; }
711 struct hb_printer_t
<hb_void_t
> {
712 const char *print (hb_void_t
) { return ""; }
720 template <typename T
>
721 static inline void _hb_warn_no_return (bool returned
)
723 if (unlikely (!returned
)) {
724 fprintf (stderr
, "OUCH, returned with no call to TRACE_RETURN. This is a bug, please report.\n");
728 inline void _hb_warn_no_return
<hb_void_t
> (bool returned HB_UNUSED
)
731 template <int max_level
, typename ret_t
>
732 struct hb_auto_trace_t
{
733 explicit inline hb_auto_trace_t (unsigned int *plevel_
,
738 ...) : plevel (plevel_
), what (what_
), obj (obj_
), returned (false)
740 if (plevel
) ++*plevel
;
743 va_start (ap
, message
);
744 _hb_debug_msg_va
<max_level
> (what
, obj
, func
, true, plevel
? *plevel
: 0, +1, message
, ap
);
747 inline ~hb_auto_trace_t (void)
749 _hb_warn_no_return
<ret_t
> (returned
);
751 _hb_debug_msg
<max_level
> (what
, obj
, NULL
, true, plevel
? *plevel
: 1, -1, " ");
753 if (plevel
) --*plevel
;
756 inline ret_t
ret (ret_t v
, unsigned int line
= 0)
758 if (unlikely (returned
)) {
759 fprintf (stderr
, "OUCH, double calls to TRACE_RETURN. This is a bug, please report.\n");
763 _hb_debug_msg
<max_level
> (what
, obj
, NULL
, true, plevel
? *plevel
: 1, -1,
764 "return %s (line %d)",
765 hb_printer_t
<ret_t
>().print (v
), line
);
766 if (plevel
) --*plevel
;
773 unsigned int *plevel
;
778 template <typename ret_t
> /* Optimize when tracing is disabled */
779 struct hb_auto_trace_t
<0, ret_t
> {
780 explicit inline hb_auto_trace_t (unsigned int *plevel_ HB_UNUSED
,
781 const char *what HB_UNUSED
,
782 const void *obj HB_UNUSED
,
783 const char *func HB_UNUSED
,
784 const char *message HB_UNUSED
,
787 inline ret_t
ret (ret_t v
, unsigned int line HB_UNUSED
= 0) { return v
; }
790 #define TRACE_RETURN(RET) trace.ret (RET, __LINE__)
795 /* Pre-mature optimization:
796 * Checks for lo <= u <= hi but with an optimization if lo and hi
797 * are only different in a contiguous set of lower-most bits.
799 template <typename T
> static inline bool
800 hb_in_range (T u
, T lo
, T hi
)
802 if ( ((lo
^hi
) & lo
) == 0 &&
803 ((lo
^hi
) & hi
) == (lo
^hi
) &&
804 ((lo
^hi
) & ((lo
^hi
) + 1)) == 0 )
805 return (u
& ~(lo
^hi
)) == lo
;
807 return lo
<= u
&& u
<= hi
;
810 template <typename T
> static inline bool
811 hb_in_ranges (T u
, T lo1
, T hi1
, T lo2
, T hi2
, T lo3
, T hi3
)
813 return hb_in_range (u
, lo1
, hi1
) || hb_in_range (u
, lo2
, hi2
) || hb_in_range (u
, lo3
, hi3
);
817 /* Useful for set-operations on small enums.
818 * For example, for testing "x ∈ {x1, x2, x3}" use:
819 * (FLAG(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3)))
821 #define FLAG(x) (1<<(x))
822 #define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x))
825 template <typename T
, typename T2
> inline void
826 hb_bubble_sort (T
*array
, unsigned int len
, int(*compar
)(const T
*, const T
*), T2
*array2
)
831 unsigned int k
= len
- 1;
833 unsigned int new_k
= 0;
835 for (unsigned int j
= 0; j
< k
; j
++)
836 if (compar (&array
[j
], &array
[j
+1]) > 0)
841 array
[j
] = array
[j
+ 1];
848 array2
[j
] = array2
[j
+ 1];
858 template <typename T
> inline void
859 hb_bubble_sort (T
*array
, unsigned int len
, int(*compar
)(const T
*, const T
*))
861 hb_bubble_sort (array
, len
, compar
, (int *) NULL
);
864 static inline hb_bool_t
865 hb_codepoint_parse (const char *s
, unsigned int len
, int base
, hb_codepoint_t
*out
)
867 /* Pain because we don't know whether s is nul-terminated. */
869 len
= MIN (ARRAY_LENGTH (buf
) - 1, len
);
870 strncpy (buf
, s
, len
);
875 unsigned long v
= strtoul (buf
, &end
, base
);
876 if (errno
) return false;
877 if (*end
) return false;
883 /* Global runtime options. */
888 int uniscribe_bug_compatible
: 1;
891 union hb_options_union_t
{
895 ASSERT_STATIC (sizeof (int) == sizeof (hb_options_union_t
));
898 _hb_options_init (void);
900 extern HB_INTERNAL hb_options_union_t _hb_options
;
902 static inline hb_options_t
905 if (unlikely (!_hb_options
.i
))
908 return _hb_options
.opts
;
912 #endif /* HB_PRIVATE_HH */