Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 1 | /* |
Behdad Esfahbod | 2409d5f | 2011-04-21 17:14:28 -0400 | [diff] [blame] | 2 | * Copyright © 2007,2008,2009 Red Hat, Inc. |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 3 | * Copyright © 2011,2012 Google, Inc. |
Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 4 | * |
Behdad Esfahbod | c755cb3 | 2010-04-22 00:11:43 -0400 | [diff] [blame] | 5 | * This is part of HarfBuzz, a text shaping library. |
Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 6 | * |
| 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. |
| 12 | * |
| 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 |
| 17 | * DAMAGE. |
| 18 | * |
| 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. |
| 24 | * |
| 25 | * Red Hat Author(s): Behdad Esfahbod |
Behdad Esfahbod | 2409d5f | 2011-04-21 17:14:28 -0400 | [diff] [blame] | 26 | * Google Author(s): Behdad Esfahbod |
Behdad Esfahbod | 64aef3a | 2008-01-23 16:14:38 -0500 | [diff] [blame] | 27 | */ |
| 28 | |
Behdad Esfahbod | c57d454 | 2011-04-20 18:50:27 -0400 | [diff] [blame] | 29 | #ifndef HB_PRIVATE_HH |
| 30 | #define HB_PRIVATE_HH |
Behdad Esfahbod | 5b3f770 | 2006-12-28 06:42:37 -0500 | [diff] [blame] | 31 | |
Behdad Esfahbod | 5ddd9cc | 2011-09-16 16:40:44 -0400 | [diff] [blame] | 32 | #ifdef HAVE_CONFIG_H |
Behdad Esfahbod | df66028 | 2009-08-01 20:46:02 -0400 | [diff] [blame] | 33 | #include "config.h" |
| 34 | #endif |
Behdad Esfahbod | 12360f7 | 2008-01-23 15:50:38 -0500 | [diff] [blame] | 35 | |
Behdad Esfahbod | d1c9eb4 | 2012-04-12 13:17:44 -0400 | [diff] [blame] | 36 | #include "hb.h" |
Behdad Esfahbod | d1c9eb4 | 2012-04-12 13:17:44 -0400 | [diff] [blame] | 37 | #define HB_H_IN |
Behdad Esfahbod | 3e32cd9 | 2012-04-23 13:20:52 -0400 | [diff] [blame] | 38 | #ifdef HAVE_OT |
| 39 | #include "hb-ot.h" |
Behdad Esfahbod | d1c9eb4 | 2012-04-12 13:17:44 -0400 | [diff] [blame] | 40 | #define HB_OT_H_IN |
Behdad Esfahbod | 3e32cd9 | 2012-04-23 13:20:52 -0400 | [diff] [blame] | 41 | #endif |
Behdad Esfahbod | b28815c | 2009-08-04 22:35:36 -0400 | [diff] [blame] | 42 | |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 43 | #include <stdlib.h> |
Behdad Esfahbod | b65c060 | 2011-07-28 16:48:43 -0400 | [diff] [blame] | 44 | #include <stddef.h> |
Behdad Esfahbod | f0954d1 | 2009-07-30 15:33:57 -0400 | [diff] [blame] | 45 | #include <string.h> |
| 46 | #include <assert.h> |
Behdad Esfahbod | 1d52151 | 2010-04-28 13:18:41 -0400 | [diff] [blame] | 47 | |
| 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. */ |
Behdad Esfahbod | 7acb389 | 2009-08-05 15:20:34 -0400 | [diff] [blame] | 52 | #include <stdio.h> |
| 53 | #include <errno.h> |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 54 | #include <stdarg.h> |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 55 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 56 | |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 57 | |
| 58 | /* Essentials */ |
| 59 | |
| 60 | #ifndef NULL |
| 61 | # define NULL ((void *) 0) |
| 62 | #endif |
| 63 | |
Behdad Esfahbod | a794ebf | 2009-08-06 12:32:35 -0400 | [diff] [blame] | 64 | |
Behdad Esfahbod | dabe698 | 2012-11-23 14:21:35 -0500 | [diff] [blame] | 65 | /* Void! */ |
| 66 | typedef struct {} void_t; |
| 67 | |
| 68 | |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 69 | /* Basics */ |
| 70 | |
Behdad Esfahbod | 153142d | 2011-04-27 01:49:03 -0400 | [diff] [blame] | 71 | |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 72 | #undef MIN |
Behdad Esfahbod | 25326c2 | 2012-08-04 16:43:18 -0700 | [diff] [blame] | 73 | template <typename Type> |
| 74 | static inline Type MIN (const Type &a, const Type &b) { return a < b ? a : b; } |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 75 | |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 76 | #undef MAX |
Behdad Esfahbod | 25326c2 | 2012-08-04 16:43:18 -0700 | [diff] [blame] | 77 | template <typename Type> |
| 78 | static inline Type MAX (const Type &a, const Type &b) { return a > b ? a : b; } |
Behdad Esfahbod | 153142d | 2011-04-27 01:49:03 -0400 | [diff] [blame] | 79 | |
Behdad Esfahbod | 8a3511a | 2009-11-04 19:45:39 -0500 | [diff] [blame] | 80 | |
Behdad Esfahbod | 4591753 | 2009-11-04 18:15:59 -0500 | [diff] [blame] | 81 | #undef ARRAY_LENGTH |
Behdad Esfahbod | 25326c2 | 2012-08-04 16:43:18 -0700 | [diff] [blame] | 82 | template <typename Type, unsigned int n> |
| 83 | static inline unsigned int ARRAY_LENGTH (const Type (&a)[n]) { return n; } |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 84 | /* A const version, but does not detect erratically being called on pointers. */ |
| 85 | #define ARRAY_LENGTH_CONST(__array) ((signed int) (sizeof (__array) / sizeof (__array[0]))) |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 86 | |
Behdad Esfahbod | 35a7383 | 2009-08-01 19:30:31 -0400 | [diff] [blame] | 87 | #define HB_STMT_START do |
| 88 | #define HB_STMT_END while (0) |
Behdad Esfahbod | 5b3f770 | 2006-12-28 06:42:37 -0500 | [diff] [blame] | 89 | |
Behdad Esfahbod | 73cb02d | 2012-06-06 11:29:25 -0400 | [diff] [blame] | 90 | #define _ASSERT_STATIC1(_line, _cond) typedef int _static_assert_on_line_##_line##_failed[(_cond)?1:-1] |
| 91 | #define _ASSERT_STATIC0(_line, _cond) _ASSERT_STATIC1 (_line, (_cond)) |
| 92 | #define ASSERT_STATIC(_cond) _ASSERT_STATIC0 (__LINE__, (_cond)) |
Behdad Esfahbod | 303fe62 | 2008-01-23 00:20:48 -0500 | [diff] [blame] | 93 | |
Behdad Esfahbod | 79e2b47 | 2012-06-06 11:27:17 -0400 | [diff] [blame] | 94 | #define ASSERT_STATIC_EXPR(_cond)((void) sizeof (char[(_cond) ? 1 : -1])) |
Behdad Esfahbod | 4ec30ae | 2011-06-28 14:13:38 -0400 | [diff] [blame] | 95 | #define ASSERT_STATIC_EXPR_ZERO(_cond) (0 * sizeof (char[(_cond) ? 1 : -1])) |
Behdad Esfahbod | dcb7026 | 2011-04-21 16:34:22 -0400 | [diff] [blame] | 96 | |
Behdad Esfahbod | 4be46ba | 2012-05-11 14:39:01 +0200 | [diff] [blame] | 97 | #define _PASTE1(a,b) a##b |
| 98 | #define PASTE(a,b) _PASTE1(a,b) |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 99 | |
Behdad Esfahbod | 6fd5364 | 2011-04-11 11:47:14 -0400 | [diff] [blame] | 100 | /* Lets assert int types. Saves trouble down the road. */ |
| 101 | |
| 102 | ASSERT_STATIC (sizeof (int8_t) == 1); |
| 103 | ASSERT_STATIC (sizeof (uint8_t) == 1); |
| 104 | ASSERT_STATIC (sizeof (int16_t) == 2); |
| 105 | ASSERT_STATIC (sizeof (uint16_t) == 2); |
| 106 | ASSERT_STATIC (sizeof (int32_t) == 4); |
| 107 | ASSERT_STATIC (sizeof (uint32_t) == 4); |
| 108 | ASSERT_STATIC (sizeof (int64_t) == 8); |
| 109 | ASSERT_STATIC (sizeof (uint64_t) == 8); |
| 110 | |
Behdad Esfahbod | b13640d | 2011-04-11 12:29:31 -0400 | [diff] [blame] | 111 | ASSERT_STATIC (sizeof (hb_codepoint_t) == 4); |
| 112 | ASSERT_STATIC (sizeof (hb_position_t) == 4); |
| 113 | ASSERT_STATIC (sizeof (hb_mask_t) == 4); |
Behdad Esfahbod | ae9eeaf | 2011-04-11 11:49:08 -0400 | [diff] [blame] | 114 | ASSERT_STATIC (sizeof (hb_var_int_t) == 4); |
Behdad Esfahbod | 6fd5364 | 2011-04-11 11:47:14 -0400 | [diff] [blame] | 115 | |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 116 | |
| 117 | /* We like our types POD */ |
| 118 | |
Behdad Esfahbod | 73cb02d | 2012-06-06 11:29:25 -0400 | [diff] [blame] | 119 | #define _ASSERT_TYPE_POD1(_line, _type) union _type_##_type##_on_line_##_line##_is_not_POD { _type instance; } |
| 120 | #define _ASSERT_TYPE_POD0(_line, _type) _ASSERT_TYPE_POD1 (_line, _type) |
| 121 | #define ASSERT_TYPE_POD(_type) _ASSERT_TYPE_POD0 (__LINE__, _type) |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 122 | |
| 123 | #ifdef __GNUC__ |
Behdad Esfahbod | 79e2b47 | 2012-06-06 11:27:17 -0400 | [diff] [blame] | 124 | # define _ASSERT_INSTANCE_POD1(_line, _instance) \ |
| 125 | HB_STMT_START { \ |
| 126 | typedef __typeof__(_instance) _type_##_line; \ |
| 127 | _ASSERT_TYPE_POD1 (_line, _type_##_line); \ |
| 128 | } HB_STMT_END |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 129 | #else |
Behdad Esfahbod | 73cb02d | 2012-06-06 11:29:25 -0400 | [diff] [blame] | 130 | # define _ASSERT_INSTANCE_POD1(_line, _instance) typedef int _assertion_on_line_##_line##_not_tested |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 131 | #endif |
Behdad Esfahbod | 73cb02d | 2012-06-06 11:29:25 -0400 | [diff] [blame] | 132 | # define _ASSERT_INSTANCE_POD0(_line, _instance) _ASSERT_INSTANCE_POD1 (_line, _instance) |
| 133 | # define ASSERT_INSTANCE_POD(_instance) _ASSERT_INSTANCE_POD0 (__LINE__, _instance) |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 134 | |
| 135 | /* Check _assertion in a method environment */ |
| 136 | #define _ASSERT_POD1(_line) \ |
Behdad Esfahbod | 73cb02d | 2012-06-06 11:29:25 -0400 | [diff] [blame] | 137 | inline void _static_assertion_on_line_##_line (void) const \ |
| 138 | { _ASSERT_INSTANCE_POD1 (_line, *this); /* Make sure it's POD. */ } |
| 139 | # define _ASSERT_POD0(_line) _ASSERT_POD1 (_line) |
| 140 | # define ASSERT_POD() _ASSERT_POD0 (__LINE__) |
Behdad Esfahbod | a00a63b | 2012-06-06 03:07:01 -0400 | [diff] [blame] | 141 | |
| 142 | |
| 143 | |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 144 | /* Misc */ |
| 145 | |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 146 | |
Behdad Esfahbod | df66028 | 2009-08-01 20:46:02 -0400 | [diff] [blame] | 147 | #if defined(__GNUC__) && (__GNUC__ > 2) && defined(__OPTIMIZE__) |
Behdad Esfahbod | 494d28a | 2010-05-10 23:50:07 -0400 | [diff] [blame] | 148 | #define _HB_BOOLEAN_EXPR(expr) ((expr) ? 1 : 0) |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 149 | #define likely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 1)) |
| 150 | #define unlikely(expr) (__builtin_expect (_HB_BOOLEAN_EXPR(expr), 0)) |
Behdad Esfahbod | df66028 | 2009-08-01 20:46:02 -0400 | [diff] [blame] | 151 | #else |
Behdad Esfahbod | 64d3fc8 | 2010-05-03 22:51:19 -0400 | [diff] [blame] | 152 | #define likely(expr) (expr) |
| 153 | #define unlikely(expr) (expr) |
Behdad Esfahbod | df66028 | 2009-08-01 20:46:02 -0400 | [diff] [blame] | 154 | #endif |
| 155 | |
| 156 | #ifndef __GNUC__ |
| 157 | #undef __attribute__ |
| 158 | #define __attribute__(x) |
| 159 | #endif |
| 160 | |
| 161 | #if __GNUC__ >= 3 |
Behdad Esfahbod | 33d13fd | 2010-04-29 13:56:44 -0400 | [diff] [blame] | 162 | #define HB_PURE_FUNC __attribute__((pure)) |
| 163 | #define HB_CONST_FUNC __attribute__((const)) |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 164 | #define HB_PRINTF_FUNC(format_idx, arg_idx) __attribute__((__format__ (__printf__, format_idx, arg_idx))) |
Behdad Esfahbod | df66028 | 2009-08-01 20:46:02 -0400 | [diff] [blame] | 165 | #else |
Behdad Esfahbod | 33d13fd | 2010-04-29 13:56:44 -0400 | [diff] [blame] | 166 | #define HB_PURE_FUNC |
| 167 | #define HB_CONST_FUNC |
Behdad Esfahbod | 44b4f50 | 2011-08-04 00:52:20 -0400 | [diff] [blame] | 168 | #define HB_PRINTF_FUNC(format_idx, arg_idx) |
Behdad Esfahbod | df66028 | 2009-08-01 20:46:02 -0400 | [diff] [blame] | 169 | #endif |
Behdad Esfahbod | bc7830e | 2010-02-17 15:14:57 -0500 | [diff] [blame] | 170 | #if __GNUC__ >= 4 |
Behdad Esfahbod | 33d13fd | 2010-04-29 13:56:44 -0400 | [diff] [blame] | 171 | #define HB_UNUSED __attribute__((unused)) |
Behdad Esfahbod | bc7830e | 2010-02-17 15:14:57 -0500 | [diff] [blame] | 172 | #else |
Behdad Esfahbod | 33d13fd | 2010-04-29 13:56:44 -0400 | [diff] [blame] | 173 | #define HB_UNUSED |
Behdad Esfahbod | bc7830e | 2010-02-17 15:14:57 -0500 | [diff] [blame] | 174 | #endif |
Behdad Esfahbod | df66028 | 2009-08-01 20:46:02 -0400 | [diff] [blame] | 175 | |
Behdad Esfahbod | eee8598 | 2010-05-12 23:22:55 -0400 | [diff] [blame] | 176 | #ifndef HB_INTERNAL |
Behdad Esfahbod | f60271c | 2011-08-02 09:56:30 -0400 | [diff] [blame] | 177 | # ifndef __MINGW32__ |
| 178 | # define HB_INTERNAL __attribute__((__visibility__("hidden"))) |
| 179 | # else |
| 180 | # define HB_INTERNAL |
| 181 | # endif |
Behdad Esfahbod | eee8598 | 2010-05-12 23:22:55 -0400 | [diff] [blame] | 182 | #endif |
| 183 | |
Behdad Esfahbod | df66028 | 2009-08-01 20:46:02 -0400 | [diff] [blame] | 184 | |
| 185 | #if (defined(__WIN32__) && !defined(__WINE__)) || defined(_MSC_VER) |
| 186 | #define snprintf _snprintf |
| 187 | #endif |
| 188 | |
| 189 | #ifdef _MSC_VER |
| 190 | #undef inline |
| 191 | #define inline __inline |
| 192 | #endif |
| 193 | |
| 194 | #ifdef __STRICT_ANSI__ |
| 195 | #undef inline |
| 196 | #define inline __inline__ |
| 197 | #endif |
| 198 | |
| 199 | |
Behdad Esfahbod | 7d3a126 | 2010-04-29 13:54:01 -0400 | [diff] [blame] | 200 | #if __GNUC__ >= 3 |
| 201 | #define HB_FUNC __PRETTY_FUNCTION__ |
| 202 | #elif defined(_MSC_VER) |
| 203 | #define HB_FUNC __FUNCSIG__ |
| 204 | #else |
| 205 | #define HB_FUNC __func__ |
| 206 | #endif |
| 207 | |
| 208 | |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 209 | /* Return the number of 1 bits in mask. */ |
Behdad Esfahbod | 97e7f8f | 2010-05-11 00:11:36 -0400 | [diff] [blame] | 210 | static inline HB_CONST_FUNC unsigned int |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 211 | _hb_popcount32 (uint32_t mask) |
| 212 | { |
| 213 | #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 214 | return __builtin_popcount (mask); |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 215 | #else |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 216 | /* "HACKMEM 169" */ |
| 217 | register uint32_t y; |
| 218 | y = (mask >> 1) &033333333333; |
| 219 | y = mask - y - ((y >>1) & 033333333333); |
| 220 | return (((y + (y >> 3)) & 030707070707) % 077); |
Behdad Esfahbod | c7d457a | 2009-05-21 12:46:29 -0400 | [diff] [blame] | 221 | #endif |
| 222 | } |
| 223 | |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 224 | /* Returns the number of bits needed to store number */ |
| 225 | static inline HB_CONST_FUNC unsigned int |
| 226 | _hb_bit_storage (unsigned int number) |
| 227 | { |
| 228 | #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) |
Behdad Esfahbod | f7acd8d | 2010-05-20 17:26:35 +0100 | [diff] [blame] | 229 | return likely (number) ? (sizeof (unsigned int) * 8 - __builtin_clz (number)) : 0; |
Behdad Esfahbod | 9b60233 | 2010-05-20 15:31:12 +0100 | [diff] [blame] | 230 | #else |
| 231 | register unsigned int n_bits = 0; |
| 232 | while (number) { |
| 233 | n_bits++; |
| 234 | number >>= 1; |
| 235 | } |
| 236 | return n_bits; |
| 237 | #endif |
| 238 | } |
Behdad Esfahbod | df66028 | 2009-08-01 20:46:02 -0400 | [diff] [blame] | 239 | |
Behdad Esfahbod | f7acd8d | 2010-05-20 17:26:35 +0100 | [diff] [blame] | 240 | /* Returns the number of zero bits in the least significant side of number */ |
| 241 | static inline HB_CONST_FUNC unsigned int |
| 242 | _hb_ctz (unsigned int number) |
| 243 | { |
| 244 | #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__OPTIMIZE__) |
| 245 | return likely (number) ? __builtin_ctz (number) : 0; |
| 246 | #else |
| 247 | register unsigned int n_bits = 0; |
| 248 | if (unlikely (!number)) return 0; |
| 249 | while (!(number & 1)) { |
| 250 | n_bits++; |
| 251 | number >>= 1; |
| 252 | } |
| 253 | return n_bits; |
| 254 | #endif |
| 255 | } |
| 256 | |
Behdad Esfahbod | 080a0eb | 2011-04-28 16:01:01 -0400 | [diff] [blame] | 257 | static inline bool |
| 258 | _hb_unsigned_int_mul_overflows (unsigned int count, unsigned int size) |
| 259 | { |
| 260 | return (size > 0) && (count >= ((unsigned int) -1) / size); |
| 261 | } |
| 262 | |
| 263 | |
Behdad Esfahbod | 8f08c32 | 2010-10-08 19:43:48 -0400 | [diff] [blame] | 264 | /* Type of bsearch() / qsort() compare function */ |
| 265 | typedef int (*hb_compare_func_t) (const void *, const void *); |
| 266 | |
| 267 | |
Behdad Esfahbod | a9f24c8 | 2011-04-21 17:18:22 -0400 | [diff] [blame] | 268 | |
Behdad Esfahbod | a9f24c8 | 2011-04-21 17:18:22 -0400 | [diff] [blame] | 269 | |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 270 | /* arrays and maps */ |
Behdad Esfahbod | a9f24c8 | 2011-04-21 17:18:22 -0400 | [diff] [blame] | 271 | |
Behdad Esfahbod | a9f24c8 | 2011-04-21 17:18:22 -0400 | [diff] [blame] | 272 | |
Behdad Esfahbod | 0e253e9 | 2012-06-05 15:37:19 -0400 | [diff] [blame] | 273 | #define HB_PREALLOCED_ARRAY_INIT {0} |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 274 | template <typename Type, unsigned int StaticSize> |
Behdad Esfahbod | 0e253e9 | 2012-06-05 15:37:19 -0400 | [diff] [blame] | 275 | struct hb_prealloced_array_t |
| 276 | { |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 277 | unsigned int len; |
| 278 | unsigned int allocated; |
| 279 | Type *array; |
| 280 | Type static_array[StaticSize]; |
| 281 | |
Behdad Esfahbod | bf93b63 | 2012-06-05 14:17:32 -0400 | [diff] [blame] | 282 | void init (void) { memset (this, 0, sizeof (*this)); } |
Behdad Esfahbod | efde811 | 2011-08-23 00:04:57 +0200 | [diff] [blame] | 283 | |
Behdad Esfahbod | 265ac61 | 2011-05-05 14:38:16 -0400 | [diff] [blame] | 284 | inline Type& operator [] (unsigned int i) { return array[i]; } |
| 285 | inline const Type& operator [] (unsigned int i) const { return array[i]; } |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 286 | |
| 287 | inline Type *push (void) |
| 288 | { |
| 289 | if (!array) { |
| 290 | array = static_array; |
| 291 | allocated = ARRAY_LENGTH (static_array); |
| 292 | } |
| 293 | if (likely (len < allocated)) |
| 294 | return &array[len++]; |
Behdad Esfahbod | 5a50303 | 2011-05-02 19:54:29 -0400 | [diff] [blame] | 295 | |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 296 | /* Need to reallocate */ |
| 297 | unsigned int new_allocated = allocated + (allocated >> 1) + 8; |
Behdad Esfahbod | 5a50303 | 2011-05-02 19:54:29 -0400 | [diff] [blame] | 298 | Type *new_array = NULL; |
| 299 | |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 300 | if (array == static_array) { |
| 301 | new_array = (Type *) calloc (new_allocated, sizeof (Type)); |
Behdad Esfahbod | 5a50303 | 2011-05-02 19:54:29 -0400 | [diff] [blame] | 302 | if (new_array) |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 303 | memcpy (new_array, array, len * sizeof (Type)); |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 304 | } else { |
Behdad Esfahbod | 080a0eb | 2011-04-28 16:01:01 -0400 | [diff] [blame] | 305 | bool overflows = (new_allocated < allocated) || _hb_unsigned_int_mul_overflows (new_allocated, sizeof (Type)); |
Behdad Esfahbod | 5a50303 | 2011-05-02 19:54:29 -0400 | [diff] [blame] | 306 | if (likely (!overflows)) { |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 307 | new_array = (Type *) realloc (array, new_allocated * sizeof (Type)); |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 308 | } |
| 309 | } |
Behdad Esfahbod | 5a50303 | 2011-05-02 19:54:29 -0400 | [diff] [blame] | 310 | |
| 311 | if (unlikely (!new_array)) |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 312 | return NULL; |
Behdad Esfahbod | 5a50303 | 2011-05-02 19:54:29 -0400 | [diff] [blame] | 313 | |
| 314 | array = new_array; |
| 315 | allocated = new_allocated; |
| 316 | return &array[len++]; |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | inline void pop (void) |
| 320 | { |
| 321 | len--; |
| 322 | /* TODO: shrink array if needed */ |
| 323 | } |
Behdad Esfahbod | 44b0a4d | 2011-05-05 13:42:19 -0400 | [diff] [blame] | 324 | |
| 325 | inline void shrink (unsigned int l) |
| 326 | { |
| 327 | if (l < len) |
| 328 | len = l; |
| 329 | /* TODO: shrink array if needed */ |
| 330 | } |
| 331 | |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 332 | template <typename T> |
| 333 | inline Type *find (T v) { |
| 334 | for (unsigned int i = 0; i < len; i++) |
| 335 | if (array[i] == v) |
| 336 | return &array[i]; |
| 337 | return NULL; |
| 338 | } |
| 339 | template <typename T> |
| 340 | inline const Type *find (T v) const { |
| 341 | for (unsigned int i = 0; i < len; i++) |
| 342 | if (array[i] == v) |
| 343 | return &array[i]; |
| 344 | return NULL; |
| 345 | } |
| 346 | |
Behdad Esfahbod | 44b0a4d | 2011-05-05 13:42:19 -0400 | [diff] [blame] | 347 | inline void sort (void) |
| 348 | { |
| 349 | qsort (array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); |
| 350 | } |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 351 | |
Behdad Esfahbod | b70c96d | 2011-07-07 21:07:41 -0400 | [diff] [blame] | 352 | inline void sort (unsigned int start, unsigned int end) |
| 353 | { |
| 354 | qsort (array + start, end - start, sizeof (Type), (hb_compare_func_t) Type::cmp); |
| 355 | } |
| 356 | |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 357 | template <typename T> |
| 358 | inline Type *bsearch (T *key) |
| 359 | { |
| 360 | return (Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); |
| 361 | } |
| 362 | template <typename T> |
| 363 | inline const Type *bsearch (T *key) const |
| 364 | { |
| 365 | return (const Type *) ::bsearch (key, array, len, sizeof (Type), (hb_compare_func_t) Type::cmp); |
| 366 | } |
Behdad Esfahbod | 6a7ac79 | 2011-05-11 14:19:18 -0400 | [diff] [blame] | 367 | |
| 368 | inline void finish (void) |
| 369 | { |
| 370 | if (array != static_array) |
| 371 | free (array); |
| 372 | array = NULL; |
| 373 | allocated = len = 0; |
| 374 | } |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 375 | }; |
| 376 | |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 377 | |
Behdad Esfahbod | 0e253e9 | 2012-06-05 15:37:19 -0400 | [diff] [blame] | 378 | #define HB_LOCKABLE_SET_INIT {HB_PREALLOCED_ARRAY_INIT} |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 379 | template <typename item_t, typename lock_t> |
| 380 | struct hb_lockable_set_t |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 381 | { |
Behdad Esfahbod | 0e253e9 | 2012-06-05 15:37:19 -0400 | [diff] [blame] | 382 | hb_prealloced_array_t <item_t, 2> items; |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 383 | |
Behdad Esfahbod | bf93b63 | 2012-06-05 14:17:32 -0400 | [diff] [blame] | 384 | inline void init (void) { items.init (); } |
| 385 | |
Behdad Esfahbod | 478a425 | 2011-05-05 12:39:51 -0400 | [diff] [blame] | 386 | template <typename T> |
Behdad Esfahbod | 33ccc77 | 2011-08-09 00:43:24 +0200 | [diff] [blame] | 387 | inline item_t *replace_or_insert (T v, lock_t &l, bool replace) |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 388 | { |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 389 | l.lock (); |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 390 | item_t *item = items.find (v); |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 391 | if (item) { |
Behdad Esfahbod | 33ccc77 | 2011-08-09 00:43:24 +0200 | [diff] [blame] | 392 | if (replace) { |
| 393 | item_t old = *item; |
| 394 | *item = v; |
| 395 | l.unlock (); |
| 396 | old.finish (); |
| 397 | } |
| 398 | else { |
| 399 | item = NULL; |
| 400 | l.unlock (); |
| 401 | } |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 402 | } else { |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 403 | item = items.push (); |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 404 | if (likely (item)) |
| 405 | *item = v; |
| 406 | l.unlock (); |
| 407 | } |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 408 | return item; |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 409 | } |
| 410 | |
Behdad Esfahbod | 811482b | 2011-05-05 13:21:04 -0400 | [diff] [blame] | 411 | template <typename T> |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 412 | inline void remove (T v, lock_t &l) |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 413 | { |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 414 | l.lock (); |
Behdad Esfahbod | 6843569 | 2011-05-05 14:12:37 -0400 | [diff] [blame] | 415 | item_t *item = items.find (v); |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 416 | if (item) { |
| 417 | item_t old = *item; |
| 418 | *item = items[items.len - 1]; |
| 419 | items.pop (); |
| 420 | l.unlock (); |
| 421 | old.finish (); |
| 422 | } else { |
| 423 | l.unlock (); |
| 424 | } |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 425 | } |
| 426 | |
Behdad Esfahbod | 478a425 | 2011-05-05 12:39:51 -0400 | [diff] [blame] | 427 | template <typename T> |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 428 | inline bool find (T v, item_t *i, lock_t &l) |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 429 | { |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 430 | l.lock (); |
| 431 | item_t *item = items.find (v); |
| 432 | if (item) |
| 433 | *i = *item; |
| 434 | l.unlock (); |
| 435 | return !!item; |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 436 | } |
| 437 | |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 438 | template <typename T> |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 439 | inline item_t *find_or_insert (T v, lock_t &l) |
| 440 | { |
| 441 | l.lock (); |
| 442 | item_t *item = items.find (v); |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 443 | if (!item) { |
| 444 | item = items.push (); |
| 445 | if (likely (item)) |
| 446 | *item = v; |
| 447 | } |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 448 | l.unlock (); |
Behdad Esfahbod | b45f32e | 2011-05-05 15:00:43 -0400 | [diff] [blame] | 449 | return item; |
| 450 | } |
| 451 | |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 452 | inline void finish (lock_t &l) |
| 453 | { |
Behdad Esfahbod | 3f4764b | 2012-07-30 10:06:42 -0400 | [diff] [blame] | 454 | if (!items.len) { |
| 455 | /* No need for locking. */ |
| 456 | items.finish (); |
| 457 | return; |
| 458 | } |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 459 | l.lock (); |
| 460 | while (items.len) { |
| 461 | item_t old = items[items.len - 1]; |
| 462 | items.pop (); |
| 463 | l.unlock (); |
| 464 | old.finish (); |
| 465 | l.lock (); |
| 466 | } |
Behdad Esfahbod | 6a7ac79 | 2011-05-11 14:19:18 -0400 | [diff] [blame] | 467 | items.finish (); |
Behdad Esfahbod | 45bfa99 | 2011-05-10 19:12:49 -0400 | [diff] [blame] | 468 | l.unlock (); |
| 469 | } |
| 470 | |
| 471 | }; |
| 472 | |
Behdad Esfahbod | 852e08e | 2011-04-27 21:45:51 -0400 | [diff] [blame] | 473 | |
Behdad Esfahbod | a9f24c8 | 2011-04-21 17:18:22 -0400 | [diff] [blame] | 474 | |
| 475 | |
Behdad Esfahbod | f60f216 | 2010-04-21 02:12:45 -0400 | [diff] [blame] | 476 | /* Big-endian handling */ |
| 477 | |
Behdad Esfahbod | 153142d | 2011-04-27 01:49:03 -0400 | [diff] [blame] | 478 | static inline uint16_t hb_be_uint16 (const uint16_t v) |
| 479 | { |
| 480 | const uint8_t *V = (const uint8_t *) &v; |
Behdad Esfahbod | fcd6f53 | 2012-06-08 09:59:43 -0400 | [diff] [blame] | 481 | return (V[0] << 8) | V[1]; |
| 482 | } |
| 483 | |
| 484 | static inline uint16_t hb_uint16_swap (const uint16_t v) |
| 485 | { |
| 486 | return (v >> 8) | (v << 8); |
| 487 | } |
| 488 | |
| 489 | static inline uint32_t hb_uint32_swap (const uint32_t v) |
| 490 | { |
| 491 | return (hb_uint16_swap (v) << 16) | hb_uint16_swap (v >> 16); |
Behdad Esfahbod | 153142d | 2011-04-27 01:49:03 -0400 | [diff] [blame] | 492 | } |
Behdad Esfahbod | f60f216 | 2010-04-21 02:12:45 -0400 | [diff] [blame] | 493 | |
Behdad Esfahbod | e74616b | 2012-04-15 14:12:13 -0400 | [diff] [blame] | 494 | /* Note, of the following macros, uint16_get is the one called many many times. |
| 495 | * If there is any optimizations to be done, it's in that macro. However, I |
| 496 | * already confirmed that on my T400 ThinkPad at least, using bswap_16(), which |
| 497 | * results in a single ror instruction, does NOT speed this up. In fact, it |
| 498 | * resulted in a minor slowdown. At any rate, note that v may not be correctly |
| 499 | * aligned, so I think the current implementation is optimal. |
| 500 | */ |
| 501 | |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 502 | #define hb_be_uint16_put(v,V) HB_STMT_START { v[0] = (V>>8); v[1] = (V); } HB_STMT_END |
Behdad Esfahbod | 7a52f28 | 2010-04-21 02:14:44 -0400 | [diff] [blame] | 503 | #define hb_be_uint16_get(v) (uint16_t) ((v[0] << 8) + v[1]) |
Behdad Esfahbod | 153142d | 2011-04-27 01:49:03 -0400 | [diff] [blame] | 504 | #define hb_be_uint16_eq(a,b) (a[0] == b[0] && a[1] == b[1]) |
Behdad Esfahbod | f60f216 | 2010-04-21 02:12:45 -0400 | [diff] [blame] | 505 | |
Behdad Esfahbod | e032ed9 | 2010-04-21 03:11:46 -0400 | [diff] [blame] | 506 | #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 |
Behdad Esfahbod | 7a52f28 | 2010-04-21 02:14:44 -0400 | [diff] [blame] | 507 | #define hb_be_uint32_get(v) (uint32_t) ((v[0] << 24) + (v[1] << 16) + (v[2] << 8) + v[3]) |
Behdad Esfahbod | 153142d | 2011-04-27 01:49:03 -0400 | [diff] [blame] | 508 | #define hb_be_uint32_eq(a,b) (a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3]) |
Behdad Esfahbod | f60f216 | 2010-04-21 02:12:45 -0400 | [diff] [blame] | 509 | |
| 510 | |
Behdad Esfahbod | 4188096 | 2011-04-11 14:58:28 -0400 | [diff] [blame] | 511 | /* ASCII tag/character handling */ |
Behdad Esfahbod | db5227c | 2011-04-11 13:16:08 -0400 | [diff] [blame] | 512 | |
Behdad Esfahbod | 153142d | 2011-04-27 01:49:03 -0400 | [diff] [blame] | 513 | static inline unsigned char ISALPHA (unsigned char c) |
| 514 | { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } |
| 515 | static inline unsigned char ISALNUM (unsigned char c) |
| 516 | { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'); } |
| 517 | static inline unsigned char TOUPPER (unsigned char c) |
| 518 | { return (c >= 'a' && c <= 'z') ? c - 'a' + 'A' : c; } |
| 519 | static inline unsigned char TOLOWER (unsigned char c) |
| 520 | { return (c >= 'A' && c <= 'Z') ? c - 'A' + 'a' : c; } |
Behdad Esfahbod | db5227c | 2011-04-11 13:16:08 -0400 | [diff] [blame] | 521 | |
Behdad Esfahbod | 4188096 | 2011-04-11 14:58:28 -0400 | [diff] [blame] | 522 | #define HB_TAG_CHAR4(s) (HB_TAG(((const char *) s)[0], \ |
| 523 | ((const char *) s)[1], \ |
| 524 | ((const char *) s)[2], \ |
| 525 | ((const char *) s)[3])) |
| 526 | |
Behdad Esfahbod | db5227c | 2011-04-11 13:16:08 -0400 | [diff] [blame] | 527 | |
Behdad Esfahbod | 831886a | 2011-05-11 21:27:52 -0400 | [diff] [blame] | 528 | /* C++ helpers */ |
| 529 | |
| 530 | /* Makes class uncopyable. Use in private: section. */ |
| 531 | #define NO_COPY(T) \ |
| 532 | T (const T &o); \ |
Behdad Esfahbod | 970e092 | 2011-06-14 14:35:44 -0400 | [diff] [blame] | 533 | T &operator = (const T &o) |
Behdad Esfahbod | 831886a | 2011-05-11 21:27:52 -0400 | [diff] [blame] | 534 | |
| 535 | |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 536 | /* Debug */ |
| 537 | |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 538 | |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 539 | #ifndef HB_DEBUG |
| 540 | #define HB_DEBUG 0 |
| 541 | #endif |
| 542 | |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 543 | static inline bool |
| 544 | _hb_debug (unsigned int level, |
| 545 | unsigned int max_level) |
| 546 | { |
| 547 | return level < max_level; |
| 548 | } |
| 549 | |
| 550 | #define DEBUG_LEVEL(WHAT, LEVEL) (_hb_debug ((LEVEL), HB_DEBUG_##WHAT)) |
Behdad Esfahbod | 43ff203 | 2011-07-25 17:35:24 -0400 | [diff] [blame] | 551 | #define DEBUG(WHAT) (DEBUG_LEVEL (WHAT, 0)) |
| 552 | |
Behdad Esfahbod | 2d1dcb3 | 2012-10-07 17:13:46 -0400 | [diff] [blame] | 553 | template <int max_level> static inline void |
Behdad Esfahbod | 2081097 | 2012-05-10 23:06:58 +0200 | [diff] [blame] | 554 | _hb_debug_msg_va (const char *what, |
Behdad Esfahbod | 1e08830 | 2012-05-11 00:16:40 +0200 | [diff] [blame] | 555 | const void *obj, |
| 556 | const char *func, |
| 557 | bool indented, |
| 558 | unsigned int level, |
| 559 | int level_dir, |
| 560 | const char *message, |
| 561 | va_list ap) |
Behdad Esfahbod | 2081097 | 2012-05-10 23:06:58 +0200 | [diff] [blame] | 562 | { |
Behdad Esfahbod | 6eec6f4 | 2012-05-11 00:50:38 +0200 | [diff] [blame] | 563 | if (!_hb_debug (level, max_level)) |
Behdad Esfahbod | 829e814 | 2012-05-11 00:52:16 +0200 | [diff] [blame] | 564 | return; |
Behdad Esfahbod | 6eec6f4 | 2012-05-11 00:50:38 +0200 | [diff] [blame] | 565 | |
Behdad Esfahbod | 6eec6f4 | 2012-05-11 00:50:38 +0200 | [diff] [blame] | 566 | fprintf (stderr, "%-10s", what ? what : ""); |
| 567 | |
| 568 | if (obj) |
Behdad Esfahbod | 5ccfe8e | 2012-05-11 02:19:41 +0200 | [diff] [blame] | 569 | fprintf (stderr, "(%0*lx) ", (unsigned int) (2 * sizeof (void *)), (unsigned long) obj); |
Behdad Esfahbod | 6eec6f4 | 2012-05-11 00:50:38 +0200 | [diff] [blame] | 570 | else |
| 571 | fprintf (stderr, " %*s ", (unsigned int) (2 * sizeof (void *)), ""); |
| 572 | |
Behdad Esfahbod | d7bba01 | 2012-05-11 02:46:26 +0200 | [diff] [blame] | 573 | if (indented) { |
Behdad Esfahbod | 6932a41 | 2012-06-26 10:46:31 -0400 | [diff] [blame] | 574 | /* One may want to add ASCII version of these. See: |
| 575 | * https://bugs.freedesktop.org/show_bug.cgi?id=50970 */ |
| 576 | #define VBAR "\342\224\202" /* U+2502 BOX DRAWINGS LIGHT VERTICAL */ |
| 577 | #define VRBAR "\342\224\234" /* U+251C BOX DRAWINGS LIGHT VERTICAL AND RIGHT */ |
| 578 | #define DLBAR "\342\225\256" /* U+256E BOX DRAWINGS LIGHT ARC DOWN AND LEFT */ |
| 579 | #define ULBAR "\342\225\257" /* U+256F BOX DRAWINGS LIGHT ARC UP AND LEFT */ |
| 580 | #define LBAR "\342\225\264" /* U+2574 BOX DRAWINGS LIGHT LEFT */ |
| 581 | 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; |
| 582 | fprintf (stderr, "%2d %s" VRBAR "%s", |
Behdad Esfahbod | 6eec6f4 | 2012-05-11 00:50:38 +0200 | [diff] [blame] | 583 | level, |
Behdad Esfahbod | 6932a41 | 2012-06-26 10:46:31 -0400 | [diff] [blame] | 584 | bars + sizeof (bars) - 1 - MIN ((unsigned int) sizeof (bars), (unsigned int) (sizeof (VBAR) - 1) * level), |
| 585 | level_dir ? (level_dir > 0 ? DLBAR : ULBAR) : LBAR); |
Behdad Esfahbod | d7bba01 | 2012-05-11 02:46:26 +0200 | [diff] [blame] | 586 | } else |
Behdad Esfahbod | 6932a41 | 2012-06-26 10:46:31 -0400 | [diff] [blame] | 587 | fprintf (stderr, " " VRBAR LBAR); |
Behdad Esfahbod | 6eec6f4 | 2012-05-11 00:50:38 +0200 | [diff] [blame] | 588 | |
Behdad Esfahbod | 85f73fa | 2012-05-11 02:40:42 +0200 | [diff] [blame] | 589 | if (func) { |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 590 | /* Skip return type */ |
Behdad Esfahbod | d7bba01 | 2012-05-11 02:46:26 +0200 | [diff] [blame] | 591 | const char *space = strchr (func, ' '); |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 592 | if (space) |
Behdad Esfahbod | d7bba01 | 2012-05-11 02:46:26 +0200 | [diff] [blame] | 593 | func = space + 1; |
Behdad Esfahbod | fabd311 | 2012-09-05 22:19:28 -0400 | [diff] [blame] | 594 | /* Skip parameter list */ |
| 595 | const char *paren = strchr (func, '('); |
| 596 | unsigned int func_len = paren ? paren - func : strlen (func); |
Behdad Esfahbod | d7bba01 | 2012-05-11 02:46:26 +0200 | [diff] [blame] | 597 | fprintf (stderr, "%.*s: ", func_len, func); |
Behdad Esfahbod | 85f73fa | 2012-05-11 02:40:42 +0200 | [diff] [blame] | 598 | } |
Behdad Esfahbod | 6eec6f4 | 2012-05-11 00:50:38 +0200 | [diff] [blame] | 599 | |
| 600 | if (message) |
| 601 | vfprintf (stderr, message, ap); |
| 602 | |
| 603 | fprintf (stderr, "\n"); |
Behdad Esfahbod | 2081097 | 2012-05-10 23:06:58 +0200 | [diff] [blame] | 604 | } |
Behdad Esfahbod | 829e814 | 2012-05-11 00:52:16 +0200 | [diff] [blame] | 605 | template <> inline void |
Behdad Esfahbod | 93345ed | 2012-05-13 16:01:08 +0200 | [diff] [blame] | 606 | _hb_debug_msg_va<0> (const char *what HB_UNUSED, |
| 607 | const void *obj HB_UNUSED, |
| 608 | const char *func HB_UNUSED, |
| 609 | bool indented HB_UNUSED, |
| 610 | unsigned int level HB_UNUSED, |
| 611 | int level_dir HB_UNUSED, |
| 612 | const char *message HB_UNUSED, |
| 613 | va_list ap HB_UNUSED) {} |
Behdad Esfahbod | 2081097 | 2012-05-10 23:06:58 +0200 | [diff] [blame] | 614 | |
Behdad Esfahbod | 2d1dcb3 | 2012-10-07 17:13:46 -0400 | [diff] [blame] | 615 | template <int max_level> static inline void |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 616 | _hb_debug_msg (const char *what, |
| 617 | const void *obj, |
| 618 | const char *func, |
| 619 | bool indented, |
Behdad Esfahbod | 1e08830 | 2012-05-11 00:16:40 +0200 | [diff] [blame] | 620 | unsigned int level, |
| 621 | int level_dir, |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 622 | const char *message, |
Behdad Esfahbod | 1e08830 | 2012-05-11 00:16:40 +0200 | [diff] [blame] | 623 | ...) HB_PRINTF_FUNC(7, 8); |
Behdad Esfahbod | 2d1dcb3 | 2012-10-07 17:13:46 -0400 | [diff] [blame] | 624 | template <int max_level> static inline void |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 625 | _hb_debug_msg (const char *what, |
| 626 | const void *obj, |
| 627 | const char *func, |
| 628 | bool indented, |
Behdad Esfahbod | 1e08830 | 2012-05-11 00:16:40 +0200 | [diff] [blame] | 629 | unsigned int level, |
| 630 | int level_dir, |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 631 | const char *message, |
| 632 | ...) |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 633 | { |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 634 | va_list ap; |
| 635 | va_start (ap, message); |
Behdad Esfahbod | 829e814 | 2012-05-11 00:52:16 +0200 | [diff] [blame] | 636 | _hb_debug_msg_va<max_level> (what, obj, func, indented, level, level_dir, message, ap); |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 637 | va_end (ap); |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 638 | } |
Behdad Esfahbod | 829e814 | 2012-05-11 00:52:16 +0200 | [diff] [blame] | 639 | template <> inline void |
Behdad Esfahbod | 93345ed | 2012-05-13 16:01:08 +0200 | [diff] [blame] | 640 | _hb_debug_msg<0> (const char *what HB_UNUSED, |
| 641 | const void *obj HB_UNUSED, |
| 642 | const char *func HB_UNUSED, |
| 643 | bool indented HB_UNUSED, |
| 644 | unsigned int level HB_UNUSED, |
| 645 | int level_dir HB_UNUSED, |
| 646 | const char *message HB_UNUSED, |
Behdad Esfahbod | 1e08830 | 2012-05-11 00:16:40 +0200 | [diff] [blame] | 647 | ...) HB_PRINTF_FUNC(7, 8); |
Behdad Esfahbod | 829e814 | 2012-05-11 00:52:16 +0200 | [diff] [blame] | 648 | template <> inline void |
Behdad Esfahbod | 93345ed | 2012-05-13 16:01:08 +0200 | [diff] [blame] | 649 | _hb_debug_msg<0> (const char *what HB_UNUSED, |
| 650 | const void *obj HB_UNUSED, |
| 651 | const char *func HB_UNUSED, |
| 652 | bool indented HB_UNUSED, |
| 653 | unsigned int level HB_UNUSED, |
| 654 | int level_dir HB_UNUSED, |
| 655 | const char *message HB_UNUSED, |
Behdad Esfahbod | 829e814 | 2012-05-11 00:52:16 +0200 | [diff] [blame] | 656 | ...) {} |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 657 | |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 658 | #define DEBUG_MSG_LEVEL(WHAT, OBJ, LEVEL, LEVEL_DIR, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, true, (LEVEL), (LEVEL_DIR), __VA_ARGS__) |
| 659 | #define DEBUG_MSG(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), NULL, false, 0, 0, __VA_ARGS__) |
| 660 | #define DEBUG_MSG_FUNC(WHAT, OBJ, ...) _hb_debug_msg<HB_DEBUG_##WHAT> (#WHAT, (OBJ), HB_FUNC, false, 0, 0, __VA_ARGS__) |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 661 | |
| 662 | |
| 663 | /* |
Behdad Esfahbod | dabe698 | 2012-11-23 14:21:35 -0500 | [diff] [blame] | 664 | * Printer |
| 665 | */ |
| 666 | |
| 667 | template <typename T> |
| 668 | struct hb_printer_t {}; |
| 669 | |
| 670 | template <> |
| 671 | struct hb_printer_t<bool> { |
| 672 | const char *print (bool v) { return v ? "true" : "false"; } |
| 673 | }; |
| 674 | |
| 675 | template <> |
| 676 | struct hb_printer_t<void_t> { |
| 677 | const char *print (void_t v) { return ""; } |
| 678 | }; |
| 679 | |
| 680 | |
| 681 | /* |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 682 | * Trace |
| 683 | */ |
| 684 | |
Behdad Esfahbod | 902cc8a | 2012-11-23 15:06:59 -0500 | [diff] [blame^] | 685 | template <typename T> |
| 686 | static inline void _hb_warn_no_return (bool returned) |
| 687 | { |
| 688 | if (unlikely (!returned)) { |
| 689 | fprintf (stderr, "OUCH, returned with no call to TRACE_RETURN. This is a bug, please report.\n"); |
| 690 | } |
| 691 | } |
| 692 | template <> |
| 693 | inline void _hb_warn_no_return<void_t> (bool returned) |
| 694 | {} |
| 695 | |
| 696 | template <int max_level, typename ret_t> |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 697 | struct hb_auto_trace_t { |
| 698 | explicit inline hb_auto_trace_t (unsigned int *plevel_, |
Behdad Esfahbod | 9659523 | 2012-05-11 03:33:36 +0200 | [diff] [blame] | 699 | const char *what_, |
| 700 | const void *obj_, |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 701 | const char *func, |
Behdad Esfahbod | 2081097 | 2012-05-10 23:06:58 +0200 | [diff] [blame] | 702 | const char *message, |
Behdad Esfahbod | 9659523 | 2012-05-11 03:33:36 +0200 | [diff] [blame] | 703 | ...) : plevel (plevel_), what (what_), obj (obj_), returned (false) |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 704 | { |
Behdad Esfahbod | 1e08830 | 2012-05-11 00:16:40 +0200 | [diff] [blame] | 705 | if (plevel) ++*plevel; |
Behdad Esfahbod | 2081097 | 2012-05-10 23:06:58 +0200 | [diff] [blame] | 706 | |
| 707 | va_list ap; |
| 708 | va_start (ap, message); |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 709 | _hb_debug_msg_va<max_level> (what, obj, func, true, plevel ? *plevel : 0, +1, message, ap); |
Behdad Esfahbod | 2081097 | 2012-05-10 23:06:58 +0200 | [diff] [blame] | 710 | va_end (ap); |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 711 | } |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 712 | inline ~hb_auto_trace_t (void) |
Behdad Esfahbod | 1e08830 | 2012-05-11 00:16:40 +0200 | [diff] [blame] | 713 | { |
Behdad Esfahbod | 902cc8a | 2012-11-23 15:06:59 -0500 | [diff] [blame^] | 714 | _hb_warn_no_return<ret_t> (returned); |
| 715 | if (!returned) { |
Behdad Esfahbod | 0594a24 | 2012-06-05 20:35:40 -0400 | [diff] [blame] | 716 | _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, " "); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 717 | } |
Behdad Esfahbod | 1e08830 | 2012-05-11 00:16:40 +0200 | [diff] [blame] | 718 | if (plevel) --*plevel; |
| 719 | } |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 720 | |
Behdad Esfahbod | 902cc8a | 2012-11-23 15:06:59 -0500 | [diff] [blame^] | 721 | inline ret_t ret (ret_t v, unsigned int line = 0) |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 722 | { |
| 723 | if (unlikely (returned)) { |
| 724 | fprintf (stderr, "OUCH, double calls to TRACE_RETURN. This is a bug, please report.\n"); |
| 725 | return v; |
| 726 | } |
| 727 | |
Behdad Esfahbod | 902cc8a | 2012-11-23 15:06:59 -0500 | [diff] [blame^] | 728 | _hb_debug_msg<max_level> (what, obj, NULL, true, plevel ? *plevel : 1, -1, |
| 729 | "return %s (line %d)", |
| 730 | hb_printer_t<ret_t>().print (v), line); |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 731 | if (plevel) --*plevel; |
| 732 | plevel = NULL; |
| 733 | returned = true; |
| 734 | return v; |
| 735 | } |
| 736 | |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 737 | private: |
| 738 | unsigned int *plevel; |
Behdad Esfahbod | 9659523 | 2012-05-11 03:33:36 +0200 | [diff] [blame] | 739 | const char *what; |
| 740 | const void *obj; |
Behdad Esfahbod | c779d82 | 2012-11-23 14:07:24 -0500 | [diff] [blame] | 741 | bool returned; |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 742 | }; |
Behdad Esfahbod | 902cc8a | 2012-11-23 15:06:59 -0500 | [diff] [blame^] | 743 | template <typename ret_t> /* Optimize when tracing is disabled */ |
| 744 | struct hb_auto_trace_t<0, ret_t> { |
Behdad Esfahbod | 93345ed | 2012-05-13 16:01:08 +0200 | [diff] [blame] | 745 | explicit inline hb_auto_trace_t (unsigned int *plevel_ HB_UNUSED, |
| 746 | const char *what HB_UNUSED, |
| 747 | const void *obj HB_UNUSED, |
| 748 | const char *func HB_UNUSED, |
| 749 | const char *message HB_UNUSED, |
Behdad Esfahbod | 2081097 | 2012-05-10 23:06:58 +0200 | [diff] [blame] | 750 | ...) {} |
Behdad Esfahbod | 0ab8c86 | 2012-05-11 01:25:34 +0200 | [diff] [blame] | 751 | |
| 752 | template <typename T> |
Behdad Esfahbod | ae63cf2 | 2012-07-19 20:45:41 -0400 | [diff] [blame] | 753 | inline T ret (T v, unsigned int line = 0) { return v; } |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 754 | }; |
| 755 | |
Behdad Esfahbod | ae63cf2 | 2012-07-19 20:45:41 -0400 | [diff] [blame] | 756 | #define TRACE_RETURN(RET) trace.ret (RET, __LINE__) |
Behdad Esfahbod | cc06c24 | 2011-07-25 20:25:44 -0400 | [diff] [blame] | 757 | |
| 758 | /* Misc */ |
Behdad Esfahbod | bc20045 | 2010-04-29 01:40:26 -0400 | [diff] [blame] | 759 | |
Behdad Esfahbod | f60f216 | 2010-04-21 02:12:45 -0400 | [diff] [blame] | 760 | |
Behdad Esfahbod | 7b08b0a | 2011-07-20 23:59:07 -0400 | [diff] [blame] | 761 | /* Pre-mature optimization: |
| 762 | * Checks for lo <= u <= hi but with an optimization if lo and hi |
| 763 | * are only different in a contiguous set of lower-most bits. |
| 764 | */ |
Behdad Esfahbod | b5aeb95 | 2012-07-13 09:45:54 -0400 | [diff] [blame] | 765 | template <typename T> static inline bool |
Behdad Esfahbod | 8f0b64f | 2011-07-29 17:02:48 -0400 | [diff] [blame] | 766 | hb_in_range (T u, T lo, T hi) |
Behdad Esfahbod | 7b08b0a | 2011-07-20 23:59:07 -0400 | [diff] [blame] | 767 | { |
| 768 | if ( ((lo^hi) & lo) == 0 && |
| 769 | ((lo^hi) & hi) == (lo^hi) && |
| 770 | ((lo^hi) & ((lo^hi) + 1)) == 0 ) |
| 771 | return (u & ~(lo^hi)) == lo; |
| 772 | else |
| 773 | return lo <= u && u <= hi; |
| 774 | } |
| 775 | |
Behdad Esfahbod | 4a7f4f3 | 2012-07-23 13:15:33 -0400 | [diff] [blame] | 776 | template <typename T> static inline bool |
Behdad Esfahbod | 093cd58 | 2012-07-23 14:04:42 -0400 | [diff] [blame] | 777 | hb_in_ranges (T u, T lo1, T hi1, T lo2, T hi2, T lo3, T hi3) |
Behdad Esfahbod | 4a7f4f3 | 2012-07-23 13:15:33 -0400 | [diff] [blame] | 778 | { |
Behdad Esfahbod | 093cd58 | 2012-07-23 14:04:42 -0400 | [diff] [blame] | 779 | return hb_in_range (u, lo1, hi1) || hb_in_range (u, lo2, hi2) || hb_in_range (u, lo3, hi3); |
Behdad Esfahbod | 4a7f4f3 | 2012-07-23 13:15:33 -0400 | [diff] [blame] | 780 | } |
| 781 | |
Behdad Esfahbod | 8f0b64f | 2011-07-29 17:02:48 -0400 | [diff] [blame] | 782 | |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 783 | /* Useful for set-operations on small enums. |
| 784 | * For example, for testing "x ∈ {x1, x2, x3}" use: |
| 785 | * (FLAG(x) & (FLAG(x1) | FLAG(x2) | FLAG(x3))) |
| 786 | */ |
Behdad Esfahbod | efb4ad7 | 2012-07-20 14:27:38 -0400 | [diff] [blame] | 787 | #define FLAG(x) (1<<(x)) |
Behdad Esfahbod | 2c372b8 | 2012-07-20 13:37:48 -0400 | [diff] [blame] | 788 | #define FLAG_RANGE(x,y) (ASSERT_STATIC_EXPR_ZERO ((x) < (y)) + FLAG(y+1) - FLAG(x)) |
Behdad Esfahbod | 7b08b0a | 2011-07-20 23:59:07 -0400 | [diff] [blame] | 789 | |
Behdad Esfahbod | 8f0b64f | 2011-07-29 17:02:48 -0400 | [diff] [blame] | 790 | |
Behdad Esfahbod | 39b1783 | 2012-07-17 17:09:29 -0400 | [diff] [blame] | 791 | template <typename T, typename T2> inline void |
| 792 | hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *), T2 *array2) |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 793 | { |
| 794 | if (unlikely (!len)) |
| 795 | return; |
| 796 | |
| 797 | unsigned int k = len - 1; |
| 798 | do { |
| 799 | unsigned int new_k = 0; |
| 800 | |
| 801 | for (unsigned int j = 0; j < k; j++) |
Behdad Esfahbod | 39b1783 | 2012-07-17 17:09:29 -0400 | [diff] [blame] | 802 | if (compar (&array[j], &array[j+1]) > 0) |
| 803 | { |
| 804 | { |
| 805 | T t; |
| 806 | t = array[j]; |
| 807 | array[j] = array[j + 1]; |
| 808 | array[j + 1] = t; |
| 809 | } |
| 810 | if (array2) |
| 811 | { |
| 812 | T2 t; |
| 813 | t = array2[j]; |
| 814 | array2[j] = array2[j + 1]; |
| 815 | array2[j + 1] = t; |
| 816 | } |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 817 | |
| 818 | new_k = j; |
| 819 | } |
| 820 | k = new_k; |
| 821 | } while (k); |
| 822 | } |
| 823 | |
Behdad Esfahbod | 39b1783 | 2012-07-17 17:09:29 -0400 | [diff] [blame] | 824 | template <typename T> inline void |
| 825 | hb_bubble_sort (T *array, unsigned int len, int(*compar)(const T *, const T *)) |
| 826 | { |
| 827 | hb_bubble_sort (array, len, compar, (int *) NULL); |
| 828 | } |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 829 | |
Behdad Esfahbod | 6f3a300 | 2012-08-07 22:13:25 -0400 | [diff] [blame] | 830 | static inline hb_bool_t |
| 831 | hb_codepoint_parse (const char *s, unsigned int len, int base, hb_codepoint_t *out) |
| 832 | { |
| 833 | /* Pain because we don't know whether s is nul-terminated. */ |
| 834 | char buf[64]; |
| 835 | strncpy (buf, s, MIN (ARRAY_LENGTH (buf) - 1, len)); |
| 836 | buf[MIN (ARRAY_LENGTH (buf) - 1, len)] = '\0'; |
| 837 | |
| 838 | char *end; |
| 839 | errno = 0; |
| 840 | unsigned long v = strtoul (buf, &end, base); |
| 841 | if (errno) return false; |
| 842 | if (*end) return false; |
| 843 | *out = v; |
| 844 | return true; |
| 845 | } |
Behdad Esfahbod | 45d6f29 | 2011-07-30 14:44:30 -0400 | [diff] [blame] | 846 | |
Behdad Esfahbod | acdba3f | 2010-07-23 15:11:18 -0400 | [diff] [blame] | 847 | |
Behdad Esfahbod | c57d454 | 2011-04-20 18:50:27 -0400 | [diff] [blame] | 848 | #endif /* HB_PRIVATE_HH */ |