quotearg: don't attempt to store 1 << 31 into an "int"
[gnulib.git] / lib / quotearg.c
index e43b0c1..e280e6e 100644 (file)
@@ -1,6 +1,6 @@
 /* quotearg.c - quote arguments for output
 
-   Copyright (C) 1998-2002, 2004-2012 Free Software Foundation, Inc.
+   Copyright (C) 1998-2002, 2004-2013 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -27,6 +27,7 @@
 #include <config.h>
 
 #include "quotearg.h"
+#include "quote.h"
 
 #include "xalloc.h"
 #include "c-strcaseeq.h"
@@ -177,7 +178,7 @@ set_custom_quoting (struct quoting_options *o,
 static struct quoting_options /* NOT PURE!! */
 quoting_options_from_style (enum quoting_style style)
 {
-  struct quoting_options o = { 0 };
+  struct quoting_options o = { 0, 0, { 0 }, NULL, NULL };
   if (style == custom_quoting_style)
     abort ();
   o.style = style;
@@ -221,10 +222,7 @@ gettext_quote (char const *msgid, enum quoting_style s)
   if (STRCASEEQ (locale_code, "GB18030", 'G','B','1','8','0','3','0',0,0))
     return msgid[0] == '`' ? "\xa1\ae": "\xa1\xaf";
 
-  if (s == clocale_quoting_style)
-    return "\"";
-
-  return translation;
+  return (s == clocale_quoting_style ? "\"" : "'");
 }
 
 /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of
@@ -303,7 +301,7 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
                use Unicode U+2018 (LEFT SINGLE QUOTATION MARK) and
                Unicode U+2019 (RIGHT SINGLE QUOTATION MARK).  If the
                current locale is not Unicode, locale_quoting_style
-               will quote `like this', and clocale_quoting_style will
+               will quote 'like this', and clocale_quoting_style will
                quote "like this".  You should always include translations
                for "`" and "'" even if U+2018 and U+2019 are appropriate
                for your locale.
@@ -350,7 +348,12 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
 
       if (backslash_escapes
           && quote_string_len
-          && i + quote_string_len <= argsize
+          && (i + quote_string_len
+              <= (argsize == SIZE_MAX && 1 < quote_string_len
+                  /* Use strlen only if we must: when argsize is SIZE_MAX,
+                     and when the quote string is more than 1 byte long.
+                     If we do call strlen, save the result.  */
+                  ? (argsize = strlen (arg)) : argsize))
           && memcmp (arg + i, quote_string, quote_string_len) == 0)
         {
           if (elide_outer_quotes)
@@ -623,7 +626,7 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize,
 
       if (! ((backslash_escapes || elide_outer_quotes)
              && quote_these_too
-             && quote_these_too[c / INT_BITS] & (1 << (c % INT_BITS)))
+             && quote_these_too[c / INT_BITS] >> (c % INT_BITS) & 1)
           && !is_right_quote)
         goto store_c;
 
@@ -929,3 +932,37 @@ quotearg_custom_mem (char const *left_quote, char const *right_quote,
   return quotearg_n_custom_mem (0, left_quote, right_quote, arg,
                                 argsize);
 }
+
+
+/* The quoting option used by the functions of quote.h.  */
+struct quoting_options quote_quoting_options =
+  {
+    locale_quoting_style,
+    0,
+    { 0 },
+    NULL, NULL
+  };
+
+char const *
+quote_n_mem (int n, char const *arg, size_t argsize)
+{
+  return quotearg_n_options (n, arg, argsize, &quote_quoting_options);
+}
+
+char const *
+quote_mem (char const *arg, size_t argsize)
+{
+  return quote_n_mem (0, arg, argsize);
+}
+
+char const *
+quote_n (int n, char const *arg)
+{
+  return quote_n_mem (n, arg, SIZE_MAX);
+}
+
+char const *
+quote (char const *arg)
+{
+  return quote_n (0, arg);
+}