1
/* Line breaking of UTF-8 strings.
2
Copyright (C) 2001-2003, 2006-2010 Free Software Foundation, Inc.
3
Written by Bruno Haible <bruno@clisp.org>, 2001.
5
This program is free software: you can redistribute it and/or modify it
6
under the terms of the GNU General Public License as published
7
by 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 GNU
13
Lesser 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/>. */
27
u8_width_linebreaks (const uint8_t *s, size_t n,
28
int width, int start_column, int at_end_columns,
29
const char *o, const char *encoding,
37
u8_possible_linebreaks (s, n, encoding, p);
41
last_column = start_column;
46
int count = u8_mbtouc_unsafe (&uc, s, s_end - s);
48
/* Respect the override. */
49
if (o != NULL && *o != UC_BREAK_UNDEFINED)
52
if (*p == UC_BREAK_POSSIBLE || *p == UC_BREAK_MANDATORY)
54
/* An atomic piece of text ends here. */
55
if (last_p != NULL && last_column + piece_width > width)
57
/* Insert a line break. */
58
*last_p = UC_BREAK_POSSIBLE;
63
if (*p == UC_BREAK_MANDATORY)
65
/* uc is a line break character. */
66
/* Start a new piece at column 0. */
73
/* uc is not a line break character. */
76
if (*p == UC_BREAK_POSSIBLE)
78
/* Start a new piece. */
80
last_column += piece_width;
82
/* No line break for the moment, may be turned into
83
UC_BREAK_POSSIBLE later, via last_p. */
86
*p = UC_BREAK_PROHIBITED;
88
w = uc_width (uc, encoding);
89
if (w >= 0) /* ignore control characters in the string */
99
/* The last atomic piece of text ends here. */
100
if (last_p != NULL && last_column + piece_width + at_end_columns > width)
102
/* Insert a line break. */
103
*last_p = UC_BREAK_POSSIBLE;
107
return last_column + piece_width;
117
/* Read the contents of an input stream, and return it, terminated with a NUL
120
read_file (FILE *stream)
128
while (! feof (stream))
130
if (size + BUFSIZE > alloc)
132
alloc = alloc + alloc / 2;
133
if (alloc < size + BUFSIZE)
134
alloc = size + BUFSIZE;
135
buf = realloc (buf, alloc);
138
fprintf (stderr, "out of memory\n");
142
count = fread (buf + size, 1, BUFSIZE, stream);
154
buf = realloc (buf, size + 1);
157
fprintf (stderr, "out of memory\n");
166
main (int argc, char * argv[])
170
/* Insert line breaks for a given width. */
171
int width = atoi (argv[1]);
172
char *input = read_file (stdin);
173
int length = strlen (input);
174
char *breaks = malloc (length);
177
u8_width_linebreaks ((uint8_t *) input, length, width, 0, 0, NULL, "UTF-8", breaks);
179
for (i = 0; i < length; i++)
183
case UC_BREAK_POSSIBLE:
186
case UC_BREAK_MANDATORY:
188
case UC_BREAK_PROHIBITED:
193
putc (input[i], stdout);