commit f7725104ef9005a6645e5bd330a31964c48728da
Author:     Laslo Hunhold <[email protected]>
AuthorDate: Fri Jan 7 18:01:01 2022 +0100
Commit:     Laslo Hunhold <[email protected]>
CommitDate: Fri Jan 7 18:02:48 2022 +0100

    Determine maximum value manually in print_lookup_table()
    
    This elegantly avoids an underflow-issue and saves us one parameter
    for print_lookup_table. This also adds detection for the 8-bit and
    64-bit-unsigned-types, whereas we previously only had 16- and 32-bit,
    which lead to larger tables than necessary.
    
    Readability is also greatly improved.
    
    Signed-off-by: Laslo Hunhold <[email protected]>

diff --git a/gen/properties.c b/gen/properties.c
index db7292d..88bcbba 100644
--- a/gen/properties.c
+++ b/gen/properties.c
@@ -280,13 +280,21 @@ get_major_minor_properties(const struct 
compressed_properties *comp,
 }
 
 static void
-print_lookup_table(char *name, size_t *data, size_t datalen, size_t maxval)
+print_lookup_table(char *name, size_t *data, size_t datalen)
 {
        char *type;
-       size_t i;
+       size_t i, maxval;
 
-       type = (maxval <= (((1 << 16) - 1)) + 0xFF) ? "uint_least16_t" :
-                                                 "uint_least32_t";
+       for (i = 0, maxval = 0; i < datalen; i++) {
+               if (data[i] > maxval) {
+                       maxval = data[i];
+               }
+       }
+
+       type = (maxval <= UINT_LEAST8_MAX)  ? "uint_least8_t"  :
+              (maxval <= UINT_LEAST16_MAX) ? "uint_least16_t" :
+              (maxval <= UINT_LEAST32_MAX) ? "uint_least32_t" :
+                                             "uint_least64_t";
 
        printf("const %s %s[] = {\n\t", type, name);
        for (i = 0; i < datalen; i++) {
@@ -382,10 +390,7 @@ main(int argc, char *argv[])
        print_enum(char_break_property, LEN(char_break_property),
                   "char_break_property", "CHAR_BREAK_PROP");
 
-       if (mm.minorlen < 0x100) {
-               fprintf(stderr, "minor-array is too short.\n");
-       }
-       print_lookup_table("major", mm.major, 0x1100, mm.minorlen - 0x100);
+       print_lookup_table("major", mm.major, 0x1100);
        printf("\n");
        print_enum_lookup_table("minor", mm.minor, mm.minorlen, comp.data, 
char_break_property, "CHAR_BREAK_PROP");
        printf("\n");

Reply via email to