]> git.ipfire.org Git - thirdparty/gettext.git/commitdiff
Fix "warning: cast from 'const char *' to 'char *' drops const qualifier".
authorBruno Haible <bruno@clisp.org>
Sun, 6 Sep 2020 20:42:44 +0000 (22:42 +0200)
committerBruno Haible <bruno@clisp.org>
Sun, 9 Oct 2022 07:30:42 +0000 (09:30 +0200)
* gettext-tools/src/str-list.h (string_list_append_unique_desc,
string_list_member_desc): New declarations.
* gettext-tools/src/str-list.c (string_list_append_unique_desc,
string_list_member_desc): New functions.
* gettext-tools/src/msgfmt.c (add_languages): Don't modify the contents of
'line'.

gettext-tools/src/msgfmt.c
gettext-tools/src/str-list.c
gettext-tools/src/str-list.h

index f990152d262d7b19491c297a9e47567b19146635..cabb886856338f6c9c9fc291ba9f604e82f2235d 100644 (file)
@@ -1408,12 +1408,12 @@ static void
 add_languages (string_list_ty *languages, string_list_ty *desired_languages,
                const char *line, size_t length)
 {
-  char *start;
+  const char *start;
 
   /* Split the line by whitespace and build the languages list.  */
-  for (start = (char *) line; start - line < length; )
+  for (start = line; start - line < length; )
     {
-      char *p;
+      const char *p;
 
       /* Skip whitespace before the string.  */
       while (*start == ' ' || *start == '\t')
@@ -1423,10 +1423,9 @@ add_languages (string_list_ty *languages, string_list_ty *desired_languages,
       while (*p != '\0' && *p != ' ' && *p != '\t')
         p++;
 
-      *p = '\0';
       if (desired_languages == NULL
-          || string_list_member (desired_languages, start))
-        string_list_append_unique (languages, start);
+          || string_list_member_desc (desired_languages, start, p - start))
+        string_list_append_unique_desc (languages, start, p - start);
       start = p + 1;
     }
 }
index b2c9cf24c96ab696be1fc1c5ed984cdb4bcdcf23..4909edc23751e2b44e4f6b8ec3c5fa24ccb6f15a 100644 (file)
@@ -82,7 +82,7 @@ string_list_append_unique (string_list_ty *slp, const char *s)
 {
   size_t j;
 
-  /* Do not if the string is already in the list.  */
+  /* Do nothing if the string is already in the list.  */
   for (j = 0; j < slp->nitems; ++j)
     if (strcmp (slp->item[j], s) == 0)
       return;
@@ -100,6 +100,37 @@ string_list_append_unique (string_list_ty *slp, const char *s)
   slp->item[slp->nitems++] = xstrdup (s);
 }
 
+/* Likewise with a string descriptor as argument.  */
+void
+string_list_append_unique_desc (string_list_ty *slp,
+                                const char *s, size_t s_len)
+{
+  size_t j;
+
+  /* Do nothing if the string is already in the list.  */
+  for (j = 0; j < slp->nitems; ++j)
+    if (strlen (slp->item[j]) == s_len && memcmp (slp->item[j], s, s_len) == 0)
+      return;
+
+  /* Grow the list.  */
+  if (slp->nitems >= slp->nitems_max)
+    {
+      slp->nitems_max = slp->nitems_max * 2 + 4;
+      slp->item = (const char **) xrealloc (slp->item,
+                                            slp->nitems_max
+                                            * sizeof (slp->item[0]));
+    }
+
+  /* Add a copy of the string to the end of the list.  */
+  {
+    char *copy = XNMALLOC (s_len + 1, char);
+    memcpy (copy, s, s_len);
+    copy[s_len] = '\0';
+
+    slp->item[slp->nitems++] = copy;
+  }
+}
+
 
 /* Destroy a list of strings.  */
 void
@@ -236,6 +267,18 @@ string_list_member (const string_list_ty *slp, const char *s)
   return false;
 }
 
+/* Likewise with a string descriptor as argument.  */
+bool
+string_list_member_desc (const string_list_ty *slp, const char *s, size_t s_len)
+{
+  size_t j;
+
+  for (j = 0; j < slp->nitems; ++j)
+    if (strlen (slp->item[j]) == s_len && memcmp (slp->item[j], s, s_len) == 0)
+      return true;
+  return false;
+}
+
 
 /* Remove s from the list of strings.  Return the removed string or NULL.  */
 const char *
index 483d3c2b1652ce40a4392d8178417b5c3955a5ed..a39f65823bcc5fe2528a0d231597db73778eb803 100644 (file)
@@ -54,6 +54,9 @@ extern void string_list_append (string_list_ty *slp, const char *s);
 /* Append a single string to the end of a list of strings, unless it is
    already contained in the list.  */
 extern void string_list_append_unique (string_list_ty *slp, const char *s);
+/* Likewise with a string descriptor as argument.  */
+extern void string_list_append_unique_desc (string_list_ty *slp,
+                                            const char *s, size_t s_len);
 
 /* Destroy a list of strings.  */
 extern void string_list_destroy (string_list_ty *slp);
@@ -79,6 +82,9 @@ extern char *string_list_join (const string_list_ty *slp, const char *separator,
 
 /* Return 1 if s is contained in the list of strings, 0 otherwise.  */
 extern bool string_list_member (const string_list_ty *slp, const char *s);
+/* Likewise with a string descriptor as argument.  */
+extern bool string_list_member_desc (const string_list_ty *slp,
+                                     const char *s, size_t s_len);
 
 /* Remove s from the list of strings.  Return the removed string or NULL.  */
 extern const char * string_list_remove (string_list_ty *slp, const char *s);