chiark / gitweb /
Switch to unsigned types for holding Unicode code points
authorBen Harris <bjh21@bjh21.me.uk>
Tue, 18 Nov 2025 22:33:13 +0000 (22:33 +0000)
committerBen Harris <bjh21@bjh21.me.uk>
Tue, 13 Jan 2026 21:55:10 +0000 (21:55 +0000)
We were only using negative numbers for the "no Unicode mapping"
constant, and even there it was mostly cast to unsigned before we used
it (because it needed to sort last).  So just using an unsigned type
throughout seems to be a bit cleaner.  It will also make me less dubious
about packing extra information in when I want to support variation
selectors.

bedstead.c

index b8a08c08f6d3d64bea7e2664534a00702160c40b..45bff4673b8d8bb1c57c429a1868e34bd360ccd0 100644 (file)
@@ -226,8 +226,8 @@ static struct glyph {
                char const *alias_of;
                int subr_idx;
        };
-       int_least32_t unicode;
-#define NU (-1)
+       uint_least32_t unicode;
+#define NU (0xFFFFFFFF)
        char const *name;
        uint_least16_t flags;
 #define SEP  0x01 /* Separated graphics */
@@ -3503,8 +3503,8 @@ compare_glyphs_by_ffid(const void *va, const void *vb)
        if (strcmp(a->name, ".notdef") == 0) return -1;
        if (strcmp(b->name, ".notdef") == 0) return +1;
        /* Then characters with Unicode code-points in order. */
-       if ((unsigned long)a->unicode < (unsigned long)b->unicode) return -1;
-       if ((unsigned long)a->unicode > (unsigned long)b->unicode) return +1;
+       if (a->unicode < b->unicode) return -1;
+       if (a->unicode > b->unicode) return +1;
        /* Finally sort by glyph name for an arbitrary stable order. */
        return namecmp(a->name, b->name);
 }
@@ -4073,10 +4073,10 @@ docmap(int pid, int eid, int format)
        }
        printf(">\n");
        for (i = 0; i < lenof(glyphs); i++)
-               if (glyphs[i].unicode >= 0 &&
-                   glyphs[i].unicode < limit)
+               if (glyphs[i].unicode < limit)
                        printf("<map code='0x%lx' name='%s'/>\n",
-                              (long)glyphs[i].unicode, glyphs[i].name);
+                              (unsigned long)glyphs[i].unicode,
+                              glyphs[i].name);
        printf("</cmap_format_%d>\n", format);
 }
 
@@ -5160,9 +5160,8 @@ byunicode(const void *va, const void *vb)
        struct glyph const *a = *(struct glyph const **)va,
                *b = *(struct glyph const **)vb;
 
-       /* Cast to unsigned long so -1 sorts last. */
-       if ((unsigned long)a->unicode < (unsigned long)b->unicode) return -1;
-       if ((unsigned long)a->unicode > (unsigned long)b->unicode) return +1;
+       if (a->unicode < b->unicode) return -1;
+       if (a->unicode > b->unicode) return +1;
        return namecmp(a->name, b->name);
 }
 
@@ -5250,7 +5249,7 @@ glyph_complement()
                printf("gsave %d %d translate ",
                       (col * 40),
                       (int)-((g->unicode == NU ?
-                              row++ : g->unicode % nrow) * 40));
+                              row++ : (int)(g->unicode % nrow)) * 40));
                if (g->unicode != NU)
                        printf("(U+%04lX)", (unsigned long)g->unicode);
                else