]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Keep the file position list in natural order, don't sort it by default.
authorBruno Haible <bruno@clisp.org>
Mon, 23 Jul 2001 13:45:54 +0000 (13:45 +0000)
committerBruno Haible <bruno@clisp.org>
Mon, 23 Jul 2001 13:45:54 +0000 (13:45 +0000)
src/ChangeLog
src/message.c
src/write-po.c

index d8fa0ea89b19859f2c3d0c3050a6b4752da698f5..120b655022d128c7401fb5b1301d97219be23f6b 100644 (file)
@@ -1,3 +1,12 @@
+2001-07-21  Bruno Haible  <haible@clisp.cons.org>
+
+       * message.c (message_comment_filepos): Don't keep the filepos[] array
+       sorted.
+       * write-po.c (cmp_by_msgid): Renamed from msgid_cmp.
+       (cmp_filepos, msgdomain_list_sort_filepos): New functions.
+       (cmp_by_filepos): Renamed from filepos_cmp.
+       (msgdomain_list_sort_by_filepos): Call msgdomain_list_sort_filepos.
+
 2001-07-12  Bruno Haible  <haible@clisp.cons.org>
 
        * msgexec.c: New file.
index 96c08f6e1fa6ef575117c0af21d47c4ce3c6fd8c..c19f1f3ce9793fddb91c6cb1ba42fafb6437ca58 100644 (file)
@@ -150,47 +150,24 @@ message_comment_filepos (mp, name, line)
      const char *name;
      size_t line;
 {
+  size_t j;
   size_t nbytes;
   lex_pos_ty *pp;
-  int min, max;
-  int j;
-
-  /* See if we have this position already.  They are kept in sorted
-     order, so use a binary chop.  */
-  /* FIXME: use bsearch */
-  min = 0;
-  max = (int) mp->filepos_count - 1;
-  while (min <= max)
+
+  /* See if we have this position already.  */
+  for (j = 0; j < mp->filepos_count; j++)
     {
-      int mid;
-      int cmp;
-
-      mid = (min + max) / 2;
-      pp = &mp->filepos[mid];
-      cmp = strcmp (pp->file_name, name);
-      if (cmp == 0)
-       cmp = (int) pp->line_number - line;
-      if (cmp == 0)
+      pp = &mp->filepos[j];
+      if (strcmp (pp->file_name, name) == 0 && pp->line_number == line)
        return;
-      if (cmp < 0)
-       min = mid + 1;
-      else
-       max = mid - 1;
     }
 
-  /* Extend the list so that we can add an position to it.  */
+  /* Extend the list so that we can add a position to it.  */
   nbytes = (mp->filepos_count + 1) * sizeof (mp->filepos[0]);
   mp->filepos = xrealloc (mp->filepos, nbytes);
 
-  /* Shuffle the rest of the list up one, so that we can insert the
-     position at 'min'.  */
-  /* FIXME: use memmove */
-  for (j = mp->filepos_count; j > min; --j)
-    mp->filepos[j] = mp->filepos[j - 1];
-  mp->filepos_count++;
-
-  /* Insert the postion into the empty slot.  */
-  pp = &mp->filepos[min];
+  /* Insert the position at the end.  Don't sort the file positions here.  */
+  pp = &mp->filepos[mp->filepos_count++];
   pp->file_name = xstrdup (name);
   pp->line_number = line;
 }
index 05b0df902d92d754393d55d3c6ea4e160e693a37..f1c9e380123e054458a78cc213e8c93771a9e62c 100644 (file)
@@ -64,8 +64,10 @@ static void message_print PARAMS ((const message_ty *mp, FILE *fp,
 static void message_print_obsolete PARAMS ((const message_ty *mp, FILE *fp,
                                            const char *charset,
                                            bool blank_line));
-static int msgid_cmp PARAMS ((const void *va, const void *vb));
-static int filepos_cmp PARAMS ((const void *va, const void *vb));
+static int cmp_by_msgid PARAMS ((const void *va, const void *vb));
+static int cmp_filepos PARAMS ((const void *va, const void *vb));
+static void msgdomain_list_sort_filepos PARAMS ((msgdomain_list_ty *mdlp));
+static int cmp_by_filepos PARAMS ((const void *va, const void *vb));
 
 
 /* This variable controls the page width when printing messages.
@@ -922,7 +924,7 @@ msgdomain_list_print (mdlp, filename, force, debug)
 
 
 static int
-msgid_cmp (va, vb)
+cmp_by_msgid (va, vb)
      const void *va;
      const void *vb;
 {
@@ -946,13 +948,55 @@ msgdomain_list_sort_by_msgid (mdlp)
       message_list_ty *mlp = mdlp->item[k]->messages;
 
       if (mlp->nitems > 0)
-       qsort (mlp->item, mlp->nitems, sizeof (mlp->item[0]), msgid_cmp);
+       qsort (mlp->item, mlp->nitems, sizeof (mlp->item[0]), cmp_by_msgid);
     }
 }
 
 
+/* Sort the file positions of every message.  */
+
+static int
+cmp_filepos (va, vb)
+     const void *va;
+     const void *vb;
+{
+  const lex_pos_ty *a = (const lex_pos_ty *) va;
+  const lex_pos_ty *b = (const lex_pos_ty *) vb;
+  int cmp;
+
+  cmp = strcmp (a->file_name, b->file_name);
+  if (cmp == 0)
+    cmp = (int) a->line_number - (int) b->line_number;
+
+  return cmp;
+}
+
+static void
+msgdomain_list_sort_filepos (mdlp)
+    msgdomain_list_ty *mdlp;
+{
+  size_t j, k;
+
+  for (k = 0; k < mdlp->nitems; k++)
+    {
+      message_list_ty *mlp = mdlp->item[k]->messages;
+
+      for (j = 0; j < mlp->nitems; j++)
+       {
+         message_ty *mp = mlp->item[j];
+
+         if (mp->filepos_count > 0)
+           qsort (mp->filepos, mp->filepos_count, sizeof (mp->filepos[0]),
+                  cmp_filepos);
+       }
+    }
+}
+
+
+/* Sort the messages according to the file position.  */
+
 static int
-filepos_cmp (va, vb)
+cmp_by_filepos (va, vb)
      const void *va;
      const void *vb;
 {
@@ -993,11 +1037,15 @@ msgdomain_list_sort_by_filepos (mdlp)
 {
   size_t k;
 
+  /* It makes sense to compare filepos[0] of different messages only after
+     the filepos[] array of each message has been sorted.  Sort it now.  */
+  msgdomain_list_sort_filepos (mdlp);
+
   for (k = 0; k < mdlp->nitems; k++)
     {
       message_list_ty *mlp = mdlp->item[k]->messages;
 
       if (mlp->nitems > 0)
-       qsort (mlp->item, mlp->nitems, sizeof (mlp->item[0]), filepos_cmp);
+       qsort (mlp->item, mlp->nitems, sizeof (mlp->item[0]), cmp_by_filepos);
     }
 }