Files
harfbuzz/src/hb-string-array.hh
T
Behdad EsfahbodandEbrahim c956a11cc7 [algs] Unify ARRAY_LENGTH with compile-time type information (#6539)
* [algs] Declare ARRAY_LENGTH also constexpr so can be used in static contexts

* [algs] Remove more use of ARRAY_LENGTH_CONST

And rename it to ARRAY_LENGTH_UNCHECKED so it will be more obvious
this is the last resort.

* [bit] Avoid use of ARRAY_LENGTH_UNCHECKED

* [algs] Move ARRAY_LENGTH_UNCHECKED to its only caller

* [algs] Return size_t from ARRAY_LENGTH

* [algs] Remove last use of ARRAY_LENGTH_UNCHECKED

* [algs] Use std::extent to get template parameter array length

It makes more sense I think.

* [algs] Make ARRAY_LENGTH usable through template reference parameters

Evaluating ARRAY_LENGTH on a reference parameter is not a constant
expression in C++11. Return std::integral_constant from a function
object so callers can extract the length from the return type without
evaluating the array expression. Ordinary calls still convert to size_t.

Use this form for all three Arabic fallback ligature table dimensions,
replacing std::extent. Document both forms and add compile-time checks
for ordinary calls, string literals, and nested template array arguments.

Validated with GCC and Clang C++11 syntax checks, a full Meson build,
and meson test -C build (296 passed, 1 skipped).

Assisted-by: OpenAI Codex

---------

Co-authored-by: Ebrahim <ebrahim@gnu.org>
2026-11-01 09:48:25 -07:00

86 lines
2.9 KiB
C++

/*
* Copyright © 2017 Google, Inc.
*
* This is part of HarfBuzz, a text shaping library.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* Google Author(s): Behdad Esfahbod
*/
#ifndef HB_STRING_ARRAY_HH
#if 0 /* Make checks happy. */
#define HB_STRING_ARRAY_HH
#endif
#include "hb.hh"
/* Based on Bruno Haible's code in Appendix B of Ulrich Drepper's dsohowto.pdf:
* https://software.intel.com/sites/default/files/m/a/1/e/dsohowto.pdf */
#define HB_STRING_ARRAY_TYPE_NAME HB_PASTE(HB_STRING_ARRAY_NAME, _msgstr_t)
#define HB_STRING_ARRAY_POOL_NAME HB_PASTE(HB_STRING_ARRAY_NAME, _msgstr)
#define HB_STRING_ARRAY_OFFS_NAME HB_PASTE(HB_STRING_ARRAY_NAME, _msgidx)
#define HB_STRING_ARRAY_LENG_NAME HB_PASTE(HB_STRING_ARRAY_NAME, _length)
static const union HB_STRING_ARRAY_TYPE_NAME {
struct {
/* I like to avoid storing the nul-termination byte since we don't need it,
* but C++ does not allow that.
* https://stackoverflow.com/q/28433862
*/
#define HB_STR(s) char HB_PASTE (str, __LINE__)[sizeof (s)];
#include HB_STRING_ARRAY_LIST
#undef HB_STR
} st;
char str[HB_VAR_ARRAY];
}
HB_STRING_ARRAY_POOL_NAME =
{
{
#define HB_STR(s) s,
#include HB_STRING_ARRAY_LIST
#undef HB_STR
}
};
static const unsigned int HB_STRING_ARRAY_OFFS_NAME[] =
{
#define HB_STR(s) offsetof (union HB_STRING_ARRAY_TYPE_NAME, st.HB_PASTE(str, __LINE__)),
#include HB_STRING_ARRAY_LIST
#undef HB_STR
sizeof (HB_STRING_ARRAY_TYPE_NAME)
};
static const unsigned int HB_STRING_ARRAY_LENG_NAME = ARRAY_LENGTH (HB_STRING_ARRAY_OFFS_NAME) - 1;
static inline hb_bytes_t
HB_STRING_ARRAY_NAME (unsigned int i)
{
assert (i < ARRAY_LENGTH (HB_STRING_ARRAY_OFFS_NAME) - 1);
return hb_bytes_t (HB_STRING_ARRAY_POOL_NAME.str + HB_STRING_ARRAY_OFFS_NAME[i],
HB_STRING_ARRAY_OFFS_NAME[i + 1] - HB_STRING_ARRAY_OFFS_NAME[i] - 1);
}
#undef HB_STRING_ARRAY_TYPE_NAME
#undef HB_STRING_ARRAY_POOL_NAME
#undef HB_STRING_ARRAY_OFFS_NAME
#undef HB_STRING_ARRAY_LENG_NAME
#endif /* HB_STRING_ARRAY_HH */