1 /* Word breaks in strings.
2 Copyright (C) 2001-2003, 2006-2009 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));
63 if (u8_conv_from_encoding (encoding, iconveh_question_mark,
64 s, n, offsets, &t, &m)
67 char *q = (char *) malloc (m);
73 /* Determine the word breaks of the UTF-8 string. */
74 u8_wordbreaks (t, m, q);
76 /* Translate the result back to the original string. */
78 for (i = 0; i < n; i++)
79 if (offsets[i] != (size_t)(-1))
92 /* Impossible to convert. */
94 if (is_all_ascii (s, n))
96 /* ASCII is a subset of UTF-8. */
97 u8_wordbreaks ((const uint8_t *) s, n, p);
101 /* We have a non-ASCII string and cannot convert it.
102 Don't produce any word breaks. */
115 /* Read the contents of an input stream, and return it, terminated with a NUL
118 read_file (FILE *stream)
126 while (! feof (stream))
128 if (size + BUFSIZE > alloc)
130 alloc = alloc + alloc / 2;
131 if (alloc < size + BUFSIZE)
132 alloc = size + BUFSIZE;
133 buf = realloc (buf, alloc);
136 fprintf (stderr, "out of memory\n");
140 count = fread (buf + size, 1, BUFSIZE, stream);
152 buf = realloc (buf, size + 1);
155 fprintf (stderr, "out of memory\n");
164 main (int argc, char * argv[])
166 setlocale (LC_CTYPE, "");
169 /* Display all the word breaks in the input string. */
170 char *input = read_file (stdin);
171 int length = strlen (input);
172 char *breaks = malloc (length);
175 ulc_wordbreaks (input, length, breaks);
177 for (i = 0; i < length; i++)
189 putc (input[i], stdout);