1 /* Word breaks in strings.
2 Copyright (C) 2001-2003, 2006-2013 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2009.
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/>. */
27 #include "localcharset.h"
29 #include "unilbrk/ulc-common.h"
31 /* Word breaks of a string in an arbitrary encoding.
33 We convert the input string to Unicode.
35 The standardized Unicode encodings are UTF-8, UCS-2, UCS-4, UTF-16,
36 UTF-16BE, UTF-16LE, UTF-7. UCS-2 supports only characters up to
37 \U0000FFFF. UTF-16 and variants support only characters up to
38 \U0010FFFF. UTF-7 is way too complex and not supported by glibc-2.1.
39 UCS-4 specification leaves doubts about endianness and byte order mark.
40 glibc currently interprets it as big endian without byte order mark,
41 but this is not backed by an RFC. So we use UTF-8. It supports
42 characters up to \U7FFFFFFF and is unambiguously defined. */
45 ulc_wordbreaks (const char *s, size_t n, char *p)
49 const char *encoding = locale_charset ();
51 if (is_utf8_encoding (encoding))
52 u8_wordbreaks ((const uint8_t *) s, n, p);
55 /* Convert the string to UTF-8 and build a translation table
56 from offsets into s to offsets into the translated string. */
57 size_t *offsets = (size_t *) malloc (n * sizeof (size_t));
64 t = u8_conv_from_encoding (encoding, iconveh_question_mark,
65 s, n, offsets, NULL, &m);
68 char *q = (char *) (m > 0 ? malloc (m) : NULL);
70 if (m == 0 || q != NULL)
74 /* Determine the word breaks of the UTF-8 string. */
75 u8_wordbreaks (t, m, q);
77 /* Translate the result back to the original string. */
79 for (i = 0; i < n; i++)
80 if (offsets[i] != (size_t)(-1))
93 /* Impossible to convert. */
95 if (is_all_ascii (s, n))
97 /* ASCII is a subset of UTF-8. */
98 u8_wordbreaks ((const uint8_t *) s, n, p);
102 /* We have a non-ASCII string and cannot convert it.
103 Don't produce any word breaks. */
116 /* Read the contents of an input stream, and return it, terminated with a NUL
119 read_file (FILE *stream)
127 while (! feof (stream))
129 if (size + BUFSIZE > alloc)
131 alloc = alloc + alloc / 2;
132 if (alloc < size + BUFSIZE)
133 alloc = size + BUFSIZE;
134 buf = realloc (buf, alloc);
137 fprintf (stderr, "out of memory\n");
141 count = fread (buf + size, 1, BUFSIZE, stream);
153 buf = realloc (buf, size + 1);
156 fprintf (stderr, "out of memory\n");
165 main (int argc, char * argv[])
167 setlocale (LC_CTYPE, "");
170 /* Display all the word breaks in the input string. */
171 char *input = read_file (stdin);
172 int length = strlen (input);
173 char *breaks = malloc (length);
176 ulc_wordbreaks (input, length, breaks);
178 for (i = 0; i < length; i++)
190 putc (input[i], stdout);