]> Savannah Git Hosting - gnulib.git/commitdiff
uninorm/composition: Make more maintainable.
authorBruno Haible <bruno@clisp.org>
Fri, 13 Sep 2024 00:45:37 +0000 (02:45 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 13 Sep 2024 00:45:37 +0000 (02:45 +0200)
* lib/gen-uni-tables.c (output_composition_tables): Add a filename2
parameter. Emit definitions of UNINORM_COMPOSE_MAX_ARG1 and
UNINORM_COMPOSE_MAX_ARG2 to this file.
(main): Invoke it with additional file name
uninorm/composition-table-bounds.h.
* uninorm/composition-table-bounds.h: New generated file.
* lib/uninorm/composition.c: Include it.
(uc_composition): Use UNINORM_COMPOSE_MAX_ARG1 and
UNINORM_COMPOSE_MAX_ARG2 instead of hardcoded bounds.

ChangeLog
lib/gen-uni-tables.c
lib/uninorm/composition-table-bounds.h [new file with mode: 0644]
lib/uninorm/composition.c

index 73d43cf40b1f9c471c9a5a41634ee8224ca9e464..0734dcb5ad74022fa71ef850e44575677bc75e81 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2024-09-12  Bruno Haible  <bruno@clisp.org>
+
+       uninorm/composition: Make more maintainable.
+       * lib/gen-uni-tables.c (output_composition_tables): Add a filename2
+       parameter. Emit definitions of UNINORM_COMPOSE_MAX_ARG1 and
+       UNINORM_COMPOSE_MAX_ARG2 to this file.
+       (main): Invoke it with additional file name
+       uninorm/composition-table-bounds.h.
+       * uninorm/composition-table-bounds.h: New generated file.
+       * lib/uninorm/composition.c: Include it.
+       (uc_composition): Use UNINORM_COMPOSE_MAX_ARG1 and
+       UNINORM_COMPOSE_MAX_ARG2 instead of hardcoded bounds.
+
 2024-09-10  Bruno Haible  <bruno@clisp.org>
 
        mbrtoc16: Fix undefined behaviour.
index b4f16da5607ab0ad0067068109b58635963b1f9b..c42827ea250faef1b97129124f0f28ec36e2aac7 100644 (file)
@@ -10765,11 +10765,17 @@ debug_output_composition_tables (const char *filename)
 }
 
 static void
-output_composition_tables (const char *filename, const char *version)
+output_composition_tables (const char *filename, const char *filename2,
+                           const char *version)
 {
+  unsigned int max_code1;
+  unsigned int max_code2;
   FILE *stream;
   unsigned int ch;
 
+  max_code1 = 0;
+  max_code2 = 0;
+
   stream = fopen (filename, "w");
   if (stream == NULL)
     {
@@ -10844,6 +10850,11 @@ output_composition_tables (const char *filename, const char *version)
                  Verify this.  */
               assert (strcmp (unicode_attributes[combined].combining, "0") == 0);
 
+              if (max_code1 < code1)
+                max_code1 = code1;
+              if (max_code2 < code2)
+                max_code2 = code2;
+
               fprintf (stream, "\"\\x%02x\\x%02x\\x%02x\\x%02x\\x%02x\\x%02x\", 0x%04x\n",
                        (code1 >> 16) & 0xff, (code1 >> 8) & 0xff, code1 & 0xff,
                        (code2 >> 16) & 0xff, (code2 >> 8) & 0xff, code2 & 0xff,
@@ -10857,6 +10868,37 @@ output_composition_tables (const char *filename, const char *version)
       fprintf (stderr, "error writing to '%s'\n", filename);
       exit (1);
     }
+
+  stream = fopen (filename2, "w");
+  if (stream == NULL)
+    {
+      fprintf (stderr, "cannot open '%s' for writing\n", filename2);
+      exit (1);
+    }
+
+  fprintf (stream, "/* DO NOT EDIT! GENERATED AUTOMATICALLY! */\n");
+  fprintf (stream, "/* Canonical composition of Unicode characters.  */\n");
+  fprintf (stream, "/* Generated automatically by gen-uni-tables.c for Unicode %s.  */\n",
+           version);
+  fprintf (stream, "\n");
+
+  fprintf (stream, "/* Copyright (C) 2009-2024 Free Software Foundation, Inc.\n");
+  fprintf (stream, "\n");
+  output_library_license (stream, true);
+  fprintf (stream, "\n");
+
+  fprintf (stream, "/* Maximum value of the first argument for which gl_uninorm_compose_lookup\n"
+                   "   can return a non-NULL value.  */\n");
+  fprintf (stream, "#define UNINORM_COMPOSE_MAX_ARG1 0x%x\n", max_code1);
+  fprintf (stream, "/* Maximum value of the second argument for which gl_uninorm_compose_lookup\n"
+                   "   can return a non-NULL value.  */\n");
+  fprintf (stream, "#define UNINORM_COMPOSE_MAX_ARG2 0x%x\n", max_code2);
+
+  if (ferror (stream) || fclose (stream))
+    {
+      fprintf (stderr, "error writing to '%s'\n", filename2);
+      exit (1);
+    }
 }
 
 /* ========================================================================= */
@@ -12031,7 +12073,7 @@ main (int argc, char * argv[])
 
   output_decomposition_tables ("uninorm/decomposition-table1.h", "uninorm/decomposition-table2.h", version);
   debug_output_composition_tables ("uninorm/composition.txt");
-  output_composition_tables ("uninorm/composition-table.gperf", version);
+  output_composition_tables ("uninorm/composition-table.gperf", "uninorm/composition-table-bounds.h", version);
 
   output_simple_mapping_test ("../tests/unicase/test-uc_toupper.c", "uc_toupper", to_upper, version);
   output_simple_mapping_test ("../tests/unicase/test-uc_tolower.c", "uc_tolower", to_lower, version);
diff --git a/lib/uninorm/composition-table-bounds.h b/lib/uninorm/composition-table-bounds.h
new file mode 100644 (file)
index 0000000..5eafc47
--- /dev/null
@@ -0,0 +1,25 @@
+/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
+/* Canonical composition of Unicode characters.  */
+/* Generated automatically by gen-uni-tables.c for Unicode 15.1.0.  */
+
+/* Copyright (C) 2009-2024 Free Software Foundation, Inc.
+
+   This file is free software: you can redistribute it and/or modify
+   it under the terms of the GNU Lesser General Public License as
+   published by the Free Software Foundation; either version 2.1 of the
+   License, or (at your option) any later version.
+
+   This file is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public License
+   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
+
+/* Maximum value of the first argument for which gl_uninorm_compose_lookup
+   can return a non-NULL value.  */
+#define UNINORM_COMPOSE_MAX_ARG1 0x11935
+/* Maximum value of the second argument for which gl_uninorm_compose_lookup
+   can return a non-NULL value.  */
+#define UNINORM_COMPOSE_MAX_ARG2 0x11930
index df662895dfba454017be060a2f5357239c3f86ac..ecc4ed21e0bbef14323263c77b4b9fee4a1830d1 100644 (file)
 struct composition_rule { char codes[6]; unsigned int combined; };
 
 #include "composition-table.h"
+#include "composition-table-bounds.h"
 
 ucs4_t
 uc_composition (ucs4_t uc1, ucs4_t uc2)
 {
-  if (uc1 < 0x12000 && uc2 < 0x12000)
+  if (uc1 <= UNINORM_COMPOSE_MAX_ARG1 && uc2 <= UNINORM_COMPOSE_MAX_ARG2)
     {
       if (uc2 >= 0x1161 && uc2 < 0x1161 + 21
           && uc1 >= 0x1100 && uc1 < 0x1100 + 19)