1 /* Line breaking of strings.
2 Copyright (C) 2001-2003, 2006-2008 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 Lesser 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 Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28 #include "unilbrk/ulc-common.h"
30 /* Line breaking of a string in an arbitrary encoding.
32 We convert the input string to Unicode.
34 The standardized Unicode encodings are UTF-8, UCS-2, UCS-4, UTF-16,
35 UTF-16BE, UTF-16LE, UTF-7. UCS-2 supports only characters up to
36 \U0000FFFF. UTF-16 and variants support only characters up to
37 \U0010FFFF. UTF-7 is way too complex and not supported by glibc-2.1.
38 UCS-4 specification leaves doubts about endianness and byte order mark.
39 glibc currently interprets it as big endian without byte order mark,
40 but this is not backed by an RFC. So we use UTF-8. It supports
41 characters up to \U7FFFFFFF and is unambiguously defined. */
44 ulc_possible_linebreaks (const char *s, size_t n, const char *encoding,
49 if (is_utf8_encoding (encoding))
50 u8_possible_linebreaks ((const uint8_t *) s, n, encoding, p);
53 /* Convert the string to UTF-8 and build a translation table
54 from offsets into s to offsets into the translated string. */
55 size_t *offsets = (size_t *) malloc (n * sizeof (size_t));
61 if (u8_conv_from_encoding (encoding, iconveh_question_mark,
62 s, n, offsets, &t, &m)
65 char *q = (char *) malloc (m);
71 /* Determine the possible line breaks of the UTF-8
73 u8_possible_linebreaks (t, m, encoding, q);
75 /* Translate the result back to the original string. */
76 memset (p, UC_BREAK_PROHIBITED, n);
77 for (i = 0; i < n; i++)
78 if (offsets[i] != (size_t)(-1))
91 /* Impossible to convert. */
93 if (is_all_ascii (s, n))
95 /* ASCII is a subset of UTF-8. */
96 u8_possible_linebreaks ((const uint8_t *) s, n, encoding, p);
100 /* We have a non-ASCII string and cannot convert it.
101 Don't produce line breaks except those already present in the
102 input string. All we assume here is that the encoding is
103 minimally ASCII compatible. */
105 const char *s_end = s + n;
108 *p = (*s == '\n' ? UC_BREAK_MANDATORY : UC_BREAK_PROHIBITED);
124 /* Read the contents of an input stream, and return it, terminated with a NUL
127 read_file (FILE *stream)
135 while (! feof (stream))
137 if (size + BUFSIZE > alloc)
139 alloc = alloc + alloc / 2;
140 if (alloc < size + BUFSIZE)
141 alloc = size + BUFSIZE;
142 buf = realloc (buf, alloc);
145 fprintf (stderr, "out of memory\n");
149 count = fread (buf + size, 1, BUFSIZE, stream);
161 buf = realloc (buf, size + 1);
164 fprintf (stderr, "out of memory\n");
173 main (int argc, char * argv[])
175 setlocale (LC_CTYPE, "");
178 /* Display all the break opportunities in the input string. */
179 char *input = read_file (stdin);
180 int length = strlen (input);
181 char *breaks = malloc (length);
184 ulc_possible_linebreaks (input, length, locale_charset (), breaks);
186 for (i = 0; i < length; i++)
190 case UC_BREAK_POSSIBLE:
193 case UC_BREAK_MANDATORY:
195 case UC_BREAK_PROHIBITED:
200 putc (input[i], stdout);