Many functions map an enum or exception number to a name, either
with a switch statement or by indexing a string table after an
open-coded bounds check. Add STR_ARRAY_ELEM_OR_NULL(array, index),
which returns array[index], or NULL when index is out of bounds,
so such code can use a static table and a single call.
The array is passed as 'const char *const *', so only arrays of
strings are accepted; hence the STR_ prefix rather than a generic
name.
Misuse is caught at build time:
- pointer instead of array: "negative width in bit-field" error,
plus -Wsizeof-pointer-div (GCC);
- array of non-strings (int[], char[][N], int *[]):
-Wincompatible-pointer-types;
- array of non-const 'char *': -Wincompatible-pointer-types (GCC),
-Wincompatible-pointer-types-discards-qualifiers (clang);
- pointer as index: -Wint-conversion.
Suggested-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
Reviewed-by: Pierrick Bouvier <pierrick.bouvier@oss.qualcomm.com>
Message-ID: <20261022215115.19680-3-philmd@oss.qualcomm.com>