From: Ben Harris Date: Wed, 29 Jan 2025 13:57:28 +0000 (+0000) Subject: Use a conventional lenof() macro for lengths of static arrays X-Git-Tag: bedstead-3.251~12 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~bjharris/git?a=commitdiff_plain;h=689b4a76f4e61e9dc33bba831e4652671cf95101;p=bedstead.git Use a conventional lenof() macro for lengths of static arrays It's slightly unconventional in that it casts the result to int, which is fine for all our arrays and avoids warnings when we use it. --- diff --git a/bedstead.c b/bedstead.c index a7fc90f..3c5c890 100644 --- a/bedstead.c +++ b/bedstead.c @@ -108,6 +108,9 @@ #include #include +/* This cast to "int" assumes we don't have any stupidly large arrays. */ +#define lenof(x) ((int)(sizeof(x) / sizeof((x)[0]))) + /* Font identity information. */ #define FAMILY_NAME "Bedstead" #define VERSION "3.246" @@ -164,7 +167,6 @@ static struct width { { "--semi-condensed", " Semi Condensed", 87.5, 4 }, { "--extended", " Extended", 125.0, 7 }, }; -static int const nwidths = sizeof(widths) / sizeof(widths[0]); static struct width const *width = &widths[0]; @@ -195,8 +197,6 @@ static struct weight { { "--bold", " Bold", +50, 700 }, }; -static int const nweights = sizeof(weights) / sizeof(weights[0]); - static struct weight const *weight = &weights[0]; /* U(N) sets the code point and name of a glyph not in AGLFN */ @@ -2819,10 +2819,7 @@ static struct glyph { #undef U -#define NGLYPHS (sizeof(glyphs) / sizeof(glyphs[0])) -static int const nglyphs = NGLYPHS; - -static struct glyph const *glyphs_by_name[NGLYPHS]; +static struct glyph const *glyphs_by_name[lenof(glyphs)]; /* * This array defines 'aalt' mappings that should not be automatically @@ -2951,7 +2948,7 @@ static struct gsub_feature { int noverrides; } const gsub_features[] = { { "aalt", SCRIPT_ALL, .overrides = aalt_overrides, - .noverrides = sizeof(aalt_overrides) / sizeof(aalt_overrides[0]) }, + .noverrides = lenof(aalt_overrides) }, { "smcp", SCRIPT_LATN, .suffix = ".sc" }, { "c2sc", SCRIPT_LATN, .suffix = ".c2sc" }, { "rtlm", SCRIPT_ALL, .suffix = ".rtlm" }, @@ -3047,9 +3044,6 @@ static struct gsub_feature { { "ss16", SCRIPT_ALL, ".sep6", .name = "6-cell separated graphics" }, }; -static int const ngsub_features = - sizeof(gsub_features) / sizeof(gsub_features[0]); - static struct gsub_script { char const *tag; unsigned int flag; @@ -3058,8 +3052,6 @@ static struct gsub_script { { "latn", SCRIPT_LATN }, }; -static int const ngsub_scripts = sizeof(gsub_scripts) / sizeof(gsub_scripts[0]); - static void dochar(struct glyph *g); static void dochar_plotter(struct glyph *g); static void domosaic(struct glyph *g); @@ -3283,7 +3275,7 @@ get_glyph_by_name(char const *name) { struct glyph * const *gp; - gp = bsearch(name, glyphs_by_name, nglyphs, + gp = bsearch(name, glyphs_by_name, lenof(glyphs_by_name), sizeof(glyphs_by_name[0]), &compare_glyph_to_name); assert(gp != NULL); return *gp; @@ -3331,13 +3323,13 @@ main(int argc, char **argv) } while (argc > 1) { - for (i = 0; i < nwidths; i++) + for (i = 0; i < lenof(widths); i++) if (strcmp(argv[1], widths[i].option) == 0) { width = &widths[i]; argv++; argc--; goto next; } - for (i = 0; i < nweights; i++) + for (i = 0; i < lenof(weights); i++) if (strcmp(argv[1], weights[i].option) == 0) { weight = &weights[i]; argv++; argc--; @@ -3407,7 +3399,8 @@ main(int argc, char **argv) } /* Put glyphs into FontForge-compatible order. */ - qsort(glyphs, nglyphs, sizeof(glyphs[0]), &compare_glyphs_by_ffid); + qsort(glyphs, lenof(glyphs), sizeof(glyphs[0]), + &compare_glyphs_by_ffid); printf("\n"); printf("\n"); /* Convenient macros for TTX values. */ @@ -3463,7 +3456,7 @@ main(int argc, char **argv) printf("\n"); TTXS("tableVersion", "0x5000"); - TTXI("numGlyphs", nglyphs); + TTXI("numGlyphs", lenof(glyphs)); printf("\n"); printf("\n"); @@ -3602,7 +3595,7 @@ main(int argc, char **argv) /* Stylistic set names. */ #define NAMEBASE_GSUB 0x100 #define NAMEBASE_GSUB_SUB 0x200 - for (i = 0; i < ngsub_features; i++) { + for (i = 0; i < lenof(gsub_features); i++) { if (gsub_features[i].name != NULL) NAME(NAMEBASE_GSUB + i, gsub_features[i].name); for (int j = 0; j < MAXSUBNAME; j++) @@ -3622,13 +3615,13 @@ main(int argc, char **argv) TTXI("minMemType1", 0); TTXI("maxMemType1", 0); printf("\n"); - for (i = 0; i < nglyphs; i++) + for (i = 0; i < lenof(glyphs); i++) glyphs_by_name[i] = glyphs + i; - qsort(glyphs_by_name, nglyphs, sizeof(glyphs_by_name[0]), + qsort(glyphs_by_name, lenof(glyphs_by_name), sizeof(glyphs_by_name[0]), &compare_glyphs_by_name); printf("\n"); - for (i = 0; i < nglyphs; i++) + for (i = 0; i < lenof(glyphs); i++) printf("\n", glyphs[i].name); printf("\n"); @@ -3677,7 +3670,7 @@ main(int argc, char **argv) /* } */ printf("\n"); nsubrs = 0; - for (i = 0; i < nglyphs; i++) { + for (i = 0; i < lenof(glyphs); i++) { struct glyph *g = &glyphs[i]; if (g->flags & IS_ALIAS) { g = realglyph(g); @@ -3692,7 +3685,7 @@ main(int argc, char **argv) printf("\n"); printf("\n"); printf("\n"); - for (i = 0; i < nglyphs; i++) { + for (i = 0; i < lenof(glyphs); i++) { printf("", glyphs[i].name); doglyph(&glyphs[i]); printf("\n"); @@ -3701,7 +3694,7 @@ main(int argc, char **argv) printf("\n"); printf("\n"); printf("\n"); - for (i = 0; i < nglyphs; i++) + for (i = 0; i < lenof(glyphs); i++) printf("\n", glyphs[i].name, (int)(XSIZE * XPIX), (int)realglyph(&glyphs[i])->left_sidebearing); @@ -3874,7 +3867,7 @@ docmap(int pid, int eid, int format) printf(" format='12' reserved='0' length='0' nGroups='0'"); } printf(">\n"); - for (i = 0; i < nglyphs; i++) + for (i = 0; i < lenof(glyphs); i++) if (glyphs[i].unicode >= 0 && glyphs[i].unicode < limit) printf("\n", @@ -3890,12 +3883,12 @@ dogsub(void) printf("\n"); TTXS("Version", "0x00010000"); printf("\n"); - for (i = 0; i < ngsub_scripts; i++) { + for (i = 0; i < lenof(gsub_scripts); i++) { printf("\n"); TTXS("ScriptTag", gsub_scripts[i].tag); printf("\n"); @@ -3903,7 +3896,7 @@ dogsub(void) } printf("\n"); printf("\n"); - for (i = 0; i < ngsub_features; i++) { + for (i = 0; i < lenof(gsub_features); i++) { printf("\n"); TTXS("FeatureTag", gsub_features[i].tag); printf("\n"); @@ -3936,7 +3929,7 @@ dogsub(void) } printf("\n"); printf("\n"); - for (i = 0; i < ngsub_features; i++) { + for (i = 0; i < lenof(gsub_features); i++) { printf(" \n", gsub_features[i].tag); if (gsub_features[i].suffix != NULL) { /* Single lookup for all glyphs with a suffix. */ @@ -3969,7 +3962,7 @@ doaltsubs(int noverrides, const struct alt_sub_override overrides[noverrides]) { int i, j, next_override = 0; - for (i = 1; i < nglyphs; i++) { + for (i = 1; i < lenof(glyphs_by_name); i++) { #define HASDOT(x) (strchr(glyphs_by_name[x]->name, '.') != NULL) /* * We want to map each glyph with a name that doesn't @@ -3985,7 +3978,7 @@ doaltsubs(int noverrides, const struct alt_sub_override overrides[noverrides]) printf("%s", overridden ? "\n", dx, dh); printf("\n"); - for (i = 0; i < nglyphs; i++) { + for (i = 0; i < lenof(glyphs); i++) { struct glyph *g = realglyph(&glyphs[i]); if (g->flags & (MOS6|MOS4)) continue; int fp = glyph_footprint(g->data); @@ -4918,15 +4911,15 @@ glyph_complement() int i, col = -1, row = 0; int npages = 0; bool newcol = false; - struct glyph const *sorted[NGLYPHS], *g; + struct glyph const *sorted[lenof(glyphs)], *g; - for (i = 0; i < nglyphs; i++) + for (i = 0; i < lenof(glyphs); i++) glyphs_by_name[i] = glyphs + i; - qsort(glyphs_by_name, nglyphs, sizeof(glyphs_by_name[0]), + qsort(glyphs_by_name, lenof(glyphs_by_name), sizeof(glyphs_by_name[0]), &compare_glyphs_by_name); - for (i = 0; i < nglyphs; i++) + for (i = 0; i < lenof(glyphs); i++) sorted[i] = &glyphs[i]; - qsort(sorted, nglyphs, sizeof(sorted[0]), &byunicode); + qsort(sorted, lenof(sorted), sizeof(sorted[0]), &byunicode); printf("%%!PS-Adobe\n"); printf("%%%%Creator: bedstead %s\n", VERSION); printf("%%%%Title: %s %s Glyph Complement\n", FAMILY_NAME, VERSION); @@ -4967,7 +4960,7 @@ glyph_complement() printf(" } for\n"); printf("} def\n"); printf("%%%%EndProlog\n"); - for (i = 0; i < nglyphs; i++) { + for (i = 0; i < lenof(sorted); i++) { g = sorted[i]; if (g->unicode / nrow != unicol || (g->unicode == -1 && row == nrow)) { @@ -5072,8 +5065,8 @@ bdf_gen(int px_height) printf("FONT_DESCENT %d\n", base); printf("DEFAULT_CHAR %d\n", 0xf1ff); printf("ENDPROPERTIES\n"); - printf("CHARS %d\n", nglyphs); - for (i = 0; i < nglyphs; i++) { + printf("CHARS %d\n", lenof(glyphs)); + for (i = 0; i < lenof(glyphs); i++) { struct glyph *g = &glyphs[i]; printf("STARTCHAR %s\n", g->name); printf("ENCODING %d\n", g->unicode);