chiark / gitweb /
Use a conventional lenof() macro for lengths of static arrays
authorBen Harris <bjh21@bjh21.me.uk>
Wed, 29 Jan 2025 13:57:28 +0000 (13:57 +0000)
committerBen Harris <bjh21@bjh21.me.uk>
Wed, 29 Jan 2025 13:57:28 +0000 (13:57 +0000)
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.

bedstead.c

index a7fc90f2e5b0a3e3dc2798fae2e841f3ad148818..3c5c890201c47d98c4739bed9ab4771ebd067289 100644 (file)
 #include <string.h>
 #include <time.h>
 
+/* 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("<?xml version='1.0'?>\n");
        printf("<ttFont sfntVersion='OTTO'>\n");
 /* Convenient macros for TTX values. */
@@ -3463,7 +3456,7 @@ main(int argc, char **argv)
 
        printf("<maxp>\n");
        TTXS("tableVersion", "0x5000");
-       TTXI("numGlyphs", nglyphs);
+       TTXI("numGlyphs", lenof(glyphs));
        printf("</maxp>\n");
 
        printf("<OS_2>\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("</post>\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("<GlyphOrder>\n");
-       for (i = 0; i < nglyphs; i++)
+       for (i = 0; i < lenof(glyphs); i++)
                printf("<GlyphID name='%s'/>\n", glyphs[i].name);
        printf("</GlyphOrder>\n");
 
@@ -3677,7 +3670,7 @@ main(int argc, char **argv)
        /* } */
        printf("<Subrs>\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("</Subrs>\n");
        printf("</Private>\n");
        printf("<CharStrings>\n");
-       for (i = 0; i < nglyphs; i++) {
+       for (i = 0; i < lenof(glyphs); i++) {
                printf("<CharString name='%s'>", glyphs[i].name);
                doglyph(&glyphs[i]);
                printf("</CharString>\n");
@@ -3701,7 +3694,7 @@ main(int argc, char **argv)
        printf("</CFFFont>\n");
        printf("</CFF>\n");
        printf("<hmtx>\n");
-       for (i = 0; i < nglyphs; i++)
+       for (i = 0; i < lenof(glyphs); i++)
                printf("<mtx name='%s' width='%d' lsb='%d'/>\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("<map code='0x%lx' name='%s'/>\n",
@@ -3890,12 +3883,12 @@ dogsub(void)
        printf("<GSUB>\n");
        TTXS("Version", "0x00010000");
        printf("<ScriptList>\n");
-       for (i = 0; i < ngsub_scripts; i++) {
+       for (i = 0; i < lenof(gsub_scripts); i++) {
                printf("<ScriptRecord>\n");
                TTXS("ScriptTag", gsub_scripts[i].tag);
                printf("<Script><DefaultLangSys>\n");
                TTXI("ReqFeatureIndex", 0xffff); /* No required feature. */
-               for (j = 0; j < ngsub_features; j++)
+               for (j = 0; j < lenof(gsub_features); j++)
                        if (gsub_features[j].scripts & gsub_scripts[i].flag)
                                TTXI("FeatureIndex", j);
                printf("</DefaultLangSys></Script>\n");
@@ -3903,7 +3896,7 @@ dogsub(void)
        }
        printf("</ScriptList>\n");
        printf("<FeatureList>\n");
-       for (i = 0; i < ngsub_features; i++) {
+       for (i = 0; i < lenof(gsub_features); i++) {
                printf("<FeatureRecord>\n");
                TTXS("FeatureTag", gsub_features[i].tag);
                printf("<Feature>\n");
@@ -3936,7 +3929,7 @@ dogsub(void)
        }
        printf("</FeatureList>\n");
        printf("<LookupList>\n");
-       for (i = 0; i < ngsub_features; i++) {
+       for (i = 0; i < lenof(gsub_features); i++) {
                printf("<Lookup> <!-- '%s' -->\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<AlternateSet glyph='%s'>",
                               overridden ? "<!-- " : "",
                               glyphs_by_name[i-1]->name);
-                       for (; i < nglyphs && HASDOT(i); i++)
+                       for (; i < lenof(glyphs_by_name) && HASDOT(i); i++)
                                printf("<Alternate glyph='%s'/>",
                                       glyphs_by_name[i]->name);
                        printf("</AlternateSet>%s\n",
@@ -4012,7 +4005,7 @@ dosinglesubs(char const *suffix)
        int i;
        char *dot;
 
-       for (i = 0; i < nglyphs; i++)
+       for (i = 0; i < lenof(glyphs); i++)
                if ((dot = strchr(glyphs[i].name, '.')) != NULL &&
                    strcmp(dot, suffix) == 0)
                        printf("<Substitution in='%.*s' out='%s'/>\n",
@@ -4075,7 +4068,7 @@ dogpos(void)
                        printf("<SinglePos Format='1'>\n");
                        printf("<!-- dx = %d; dh = %d -->\n", dx, dh);
                        printf("<Coverage>\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);