X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fquotearg.c;h=e280e6ec2113ccd6cd08a31b4f07727e5ece585c;hb=831b84c59ef413c57a36b67344467d66a8a2ba70;hp=03fbfe79720a69f966b28d83adef00c261685e95;hpb=2ae12d77e599965beb1483c10edb1f2f290bafc1;p=gnulib.git diff --git a/lib/quotearg.c b/lib/quotearg.c index 03fbfe797..e280e6ec2 100644 --- a/lib/quotearg.c +++ b/lib/quotearg.c @@ -1,6 +1,6 @@ /* quotearg.c - quote arguments for output - Copyright (C) 1998-2002, 2004-2011 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,8 +27,11 @@ #include #include "quotearg.h" +#include "quote.h" #include "xalloc.h" +#include "c-strcaseeq.h" +#include "localcharset.h" #include #include @@ -175,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; @@ -183,14 +186,43 @@ quoting_options_from_style (enum quoting_style style) } /* MSGID approximates a quotation mark. Return its translation if it - has one; otherwise, return either it or "\"", depending on S. */ + has one; otherwise, return either it or "\"", depending on S. + + S is either clocale_quoting_style or locale_quoting_style. */ static char const * gettext_quote (char const *msgid, enum quoting_style s) { char const *translation = _(msgid); - if (translation == msgid && s == clocale_quoting_style) - translation = "\""; - return translation; + char const *locale_code; + + if (translation != msgid) + return translation; + + /* For UTF-8 and GB-18030, use single quotes U+2018 and U+2019. + Here is a list of other locales that include U+2018 and U+2019: + + ISO-8859-7 0xA1 KOI8-T 0x91 + CP869 0x8B CP874 0x91 + CP932 0x81 0x65 CP936 0xA1 0xAE + CP949 0xA1 0xAE CP950 0xA1 0xA5 + CP1250 0x91 CP1251 0x91 + CP1252 0x91 CP1253 0x91 + CP1254 0x91 CP1255 0x91 + CP1256 0x91 CP1257 0x91 + EUC-JP 0xA1 0xC6 EUC-KR 0xA1 0xAE + EUC-TW 0xA1 0xE4 BIG5 0xA1 0xA5 + BIG5-HKSCS 0xA1 0xA5 EUC-CN 0xA1 0xAE + GBK 0xA1 0xAE Georgian-PS 0x91 + PT154 0x91 + + None of these is still in wide use; using iconv is overkill. */ + locale_code = locale_charset (); + if (STRCASEEQ (locale_code, "UTF-8", 'U','T','F','-','8',0,0,0,0)) + return msgid[0] == '`' ? "\xe2\x80\x98": "\xe2\x80\x99"; + if (STRCASEEQ (locale_code, "GB18030", 'G','B','1','8','0','3','0',0,0)) + return msgid[0] == '`' ? "\xa1\ae": "\xa1\xaf"; + + return (s == clocale_quoting_style ? "\"" : "'"); } /* Place into buffer BUFFER (of size BUFFERSIZE) a quoted version of @@ -258,22 +290,24 @@ quotearg_buffer_restyled (char *buffer, size_t buffersize, { /* TRANSLATORS: Get translations for open and closing quotation marks. - The message catalog should translate "`" to a left quotation mark suitable for the locale, and similarly for - "'". If the catalog has no translation, - locale_quoting_style quotes `like this', and - clocale_quoting_style quotes "like this". - - For example, an American English Unicode locale should - translate "`" to U+201C (LEFT DOUBLE QUOTATION MARK), and - should translate "'" to U+201D (RIGHT DOUBLE QUOTATION - MARK). A British English Unicode locale should instead - translate these to U+2018 (LEFT SINGLE QUOTATION MARK) - and U+2019 (RIGHT SINGLE QUOTATION MARK), respectively. + "'". For example, a French Unicode local should translate + these to U+00AB (LEFT-POINTING DOUBLE ANGLE + QUOTATION MARK), and U+00BB (RIGHT-POINTING DOUBLE ANGLE + QUOTATION MARK), respectively. + + If the catalog has no translation, we will try to + 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 + quote "like this". You should always include translations + for "`" and "'" even if U+2018 and U+2019 are appropriate + for your locale. If you don't know what to put here, please see - + and use glyphs suitable for your language. */ left_quote = gettext_quote (N_("`"), quoting_style); right_quote = gettext_quote (N_("'"), quoting_style); @@ -314,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) @@ -587,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; @@ -737,7 +776,7 @@ quotearg_n_options (int n, char const *arg, size_t argsize, if (nslots <= n0) { - /* FIXME: technically, the type of n1 should be `unsigned int', + /* FIXME: technically, the type of n1 should be 'unsigned int', but that evokes an unsuppressible warning from gcc-4.0.1 and older. If gcc ever provides an option to suppress that warning, revert to the original type, so that the test in xalloc_oversized @@ -893,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, "e_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); +}