Behdad Esfahbod | 2a74968 | 2017-10-26 19:48:33 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2017 Google, Inc. |
| 3 | * |
| 4 | * This is part of HarfBuzz, a text shaping library. |
| 5 | * |
| 6 | * Permission is hereby granted, without written agreement and without |
| 7 | * license or royalty fees, to use, copy, modify, and distribute this |
| 8 | * software and its documentation for any purpose, provided that the |
| 9 | * above copyright notice and the following two paragraphs appear in |
| 10 | * all copies of this software. |
| 11 | * |
| 12 | * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR |
| 13 | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES |
| 14 | * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN |
| 15 | * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH |
| 16 | * DAMAGE. |
| 17 | * |
| 18 | * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, |
| 19 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
| 21 | * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO |
| 22 | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
| 23 | * |
| 24 | * Google Author(s): Behdad Esfahbod |
| 25 | */ |
| 26 | |
| 27 | #ifndef HB_STRING_ARRAY_HH |
| 28 | #if 0 /* Make checks happy. */ |
| 29 | #define HB_STRING_ARRAY_HH |
| 30 | #endif |
| 31 | |
Behdad Esfahbod | c77ae40 | 2018-08-25 22:36:36 -0700 | [diff] [blame] | 32 | #include "hb.hh" |
Behdad Esfahbod | 2a74968 | 2017-10-26 19:48:33 -0600 | [diff] [blame] | 33 | |
| 34 | /* Based on Bruno Haible's code in Appendix B of Ulrich Drepper's dsohowto.pdf: |
| 35 | * https://software.intel.com/sites/default/files/m/a/1/e/dsohowto.pdf */ |
| 36 | |
| 37 | #define HB_STRING_ARRAY_TYPE_NAME HB_PASTE(HB_STRING_ARRAY_NAME, _msgstr_t) |
| 38 | #define HB_STRING_ARRAY_POOL_NAME HB_PASTE(HB_STRING_ARRAY_NAME, _msgstr) |
| 39 | #define HB_STRING_ARRAY_OFFS_NAME HB_PASTE(HB_STRING_ARRAY_NAME, _msgidx) |
blueshade7 | a759836 | 2019-11-18 15:09:24 -0800 | [diff] [blame] | 40 | #define HB_STRING_ARRAY_LENG_NAME HB_PASTE(HB_STRING_ARRAY_NAME, _length) |
Behdad Esfahbod | 2a74968 | 2017-10-26 19:48:33 -0600 | [diff] [blame] | 41 | |
| 42 | static const union HB_STRING_ARRAY_TYPE_NAME { |
| 43 | struct { |
Behdad Esfahbod | 6c738f3 | 2017-10-30 12:21:44 -0600 | [diff] [blame] | 44 | /* I like to avoid storing the nul-termination byte since we don't need it, |
| 45 | * but C++ does not allow that. |
Ebrahim Byagowi | f24b0b9 | 2018-04-12 13:40:45 +0430 | [diff] [blame] | 46 | * https://stackoverflow.com/q/28433862 |
Behdad Esfahbod | 6c738f3 | 2017-10-30 12:21:44 -0600 | [diff] [blame] | 47 | */ |
Behdad Esfahbod | 2a74968 | 2017-10-26 19:48:33 -0600 | [diff] [blame] | 48 | #define _S(s) char HB_PASTE (str, __LINE__)[sizeof (s)]; |
| 49 | #include HB_STRING_ARRAY_LIST |
| 50 | #undef _S |
| 51 | } st; |
Behdad Esfahbod | 0e294c4 | 2019-09-06 16:54:27 -0400 | [diff] [blame] | 52 | char str[HB_VAR_ARRAY]; |
Behdad Esfahbod | 2a74968 | 2017-10-26 19:48:33 -0600 | [diff] [blame] | 53 | } |
| 54 | HB_STRING_ARRAY_POOL_NAME = |
| 55 | { |
| 56 | { |
| 57 | #define _S(s) s, |
| 58 | #include HB_STRING_ARRAY_LIST |
| 59 | #undef _S |
| 60 | } |
| 61 | }; |
| 62 | static const unsigned int HB_STRING_ARRAY_OFFS_NAME[] = |
| 63 | { |
| 64 | #define _S(s) offsetof (union HB_STRING_ARRAY_TYPE_NAME, st.HB_PASTE(str, __LINE__)), |
| 65 | #include HB_STRING_ARRAY_LIST |
| 66 | #undef _S |
Behdad Esfahbod | 6c738f3 | 2017-10-30 12:21:44 -0600 | [diff] [blame] | 67 | sizeof (HB_STRING_ARRAY_TYPE_NAME) |
Behdad Esfahbod | 2a74968 | 2017-10-26 19:48:33 -0600 | [diff] [blame] | 68 | }; |
| 69 | |
blueshade7 | 5e9863f | 2019-11-18 15:40:43 -0800 | [diff] [blame] | 70 | static const unsigned int HB_STRING_ARRAY_LENG_NAME = ARRAY_LENGTH_CONST (HB_STRING_ARRAY_OFFS_NAME) - 1; |
blueshade7 | a759836 | 2019-11-18 15:09:24 -0800 | [diff] [blame] | 71 | |
Behdad Esfahbod | 08e280f | 2018-05-08 01:57:27 -0700 | [diff] [blame] | 72 | static inline hb_bytes_t |
Behdad Esfahbod | 2a74968 | 2017-10-26 19:48:33 -0600 | [diff] [blame] | 73 | HB_STRING_ARRAY_NAME (unsigned int i) |
| 74 | { |
Behdad Esfahbod | 6c738f3 | 2017-10-30 12:21:44 -0600 | [diff] [blame] | 75 | assert (i < ARRAY_LENGTH (HB_STRING_ARRAY_OFFS_NAME) - 1); |
Behdad Esfahbod | 08e280f | 2018-05-08 01:57:27 -0700 | [diff] [blame] | 76 | return hb_bytes_t (HB_STRING_ARRAY_POOL_NAME.str + HB_STRING_ARRAY_OFFS_NAME[i], |
| 77 | HB_STRING_ARRAY_OFFS_NAME[i + 1] - HB_STRING_ARRAY_OFFS_NAME[i] - 1); |
Behdad Esfahbod | 2a74968 | 2017-10-26 19:48:33 -0600 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | #undef HB_STRING_ARRAY_TYPE_NAME |
| 81 | #undef HB_STRING_ARRAY_POOL_NAME |
| 82 | #undef HB_STRING_ARRAY_OFFS_NAME |
Michiharu Ariza | 8fdafee | 2019-11-18 16:44:25 -0800 | [diff] [blame] | 83 | #undef HB_STRING_ARRAY_LENG_NAME |
Behdad Esfahbod | 2a74968 | 2017-10-26 19:48:33 -0600 | [diff] [blame] | 84 | |
| 85 | #endif /* HB_STRING_ARRAY_HH */ |