1 /* Convert ASCII quotations to Unicode quotations.
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
3 Written by Daiki Ueno <ueno@gnu.org>, 2014.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
31 #define BOLD_START "\x1b[1m"
32 #define BOLD_END "\x1b[0m"
42 convert_quote_callback (char quote, const char *quoted, size_t quoted_length,
45 struct result *result = data;
50 memcpy (result->offset, quoted, quoted_length);
51 result->offset += quoted_length;
55 /* U+201C: LEFT DOUBLE QUOTATION MARK */
56 memcpy (result->offset, "\xe2\x80\x9c", 3);
60 memcpy (result->offset, BOLD_START, 4);
63 memcpy (result->offset, quoted, quoted_length);
64 result->offset += quoted_length;
67 memcpy (result->offset, BOLD_END, 4);
70 /* U+201D: RIGHT DOUBLE QUOTATION MARK */
71 memcpy (result->offset, "\xe2\x80\x9d", 3);
76 /* U+2018: LEFT SINGLE QUOTATION MARK */
77 memcpy (result->offset, "\xe2\x80\x98", 3);
81 memcpy (result->offset, BOLD_START, 4);
84 memcpy (result->offset, quoted, quoted_length);
85 result->offset += quoted_length;
88 memcpy (result->offset, BOLD_END, 4);
91 /* U+2019: RIGHT SINGLE QUOTATION MARK */
92 memcpy (result->offset, "\xe2\x80\x99", 3);
98 /* This is a direct translation of po/quot.sed and po/boldquot.sed. */
100 convert_ascii_quote_to_unicode (const char *input, size_t input_len,
101 char **output_p, size_t *output_len_p,
106 struct result result;
108 /* Count the number of quotation characters. */
110 for (p = input; p < input + input_len; p++)
114 p = strpbrk (p, "`'\"");
118 len = strspn (p, "`'\"");
124 result.output = XNMALLOC (input_len - quote_count
125 + (bold ? 7 : 3) * quote_count + 1,
127 result.offset = result.output;
130 scan_quoted (input, input_len, convert_quote_callback, &result);
132 *output_p = result.output;
133 *output_len_p = result.offset - result.output;
137 ascii_quote_to_unicode (const char *input, size_t input_len,
138 char **output_p, size_t *output_len_p)
140 convert_ascii_quote_to_unicode (input, input_len,
141 output_p, output_len_p,
146 ascii_quote_to_unicode_bold (const char *input, size_t input_len,
147 char **output_p, size_t *output_len_p)
149 convert_ascii_quote_to_unicode (input, input_len,
150 output_p, output_len_p,