maint: update copyright
[gnulib.git] / lib / uniwbrk / ulc-wordbreaks.c
1 /* Word breaks in strings.
2    Copyright (C) 2001-2003, 2006-2014 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2009.
4
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.
9
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.
14
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/>.  */
17
18 #include <config.h>
19
20 /* Specification.  */
21 #include "uniwbrk.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "c-ctype.h"
27 #include "localcharset.h"
28 #include "uniconv.h"
29 #include "unilbrk/ulc-common.h"
30
31 /* Word breaks of a string in an arbitrary encoding.
32
33    We convert the input string to Unicode.
34
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.  */
43
44 void
45 ulc_wordbreaks (const char *s, size_t n, char *p)
46 {
47   if (n > 0)
48     {
49       const char *encoding = locale_charset ();
50
51       if (is_utf8_encoding (encoding))
52         u8_wordbreaks ((const uint8_t *) s, n, p);
53       else
54         {
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));
58
59           if (offsets != NULL)
60             {
61               uint8_t *t;
62               size_t m;
63
64               t = u8_conv_from_encoding (encoding, iconveh_question_mark,
65                                          s, n, offsets, NULL, &m);
66               if (t != NULL)
67                 {
68                   char *q = (char *) (m > 0 ? malloc (m) : NULL);
69
70                   if (m == 0 || q != NULL)
71                     {
72                       size_t i;
73
74                       /* Determine the word breaks of the UTF-8 string.  */
75                       u8_wordbreaks (t, m, q);
76
77                       /* Translate the result back to the original string.  */
78                       memset (p, 0, n);
79                       for (i = 0; i < n; i++)
80                         if (offsets[i] != (size_t)(-1))
81                           p[i] = q[offsets[i]];
82
83                       free (q);
84                       free (t);
85                       free (offsets);
86                       return;
87                     }
88                   free (t);
89                 }
90               free (offsets);
91             }
92
93           /* Impossible to convert.  */
94 #if C_CTYPE_ASCII
95           if (is_all_ascii (s, n))
96             {
97               /* ASCII is a subset of UTF-8.  */
98               u8_wordbreaks ((const uint8_t *) s, n, p);
99               return;
100             }
101 #endif
102           /* We have a non-ASCII string and cannot convert it.
103              Don't produce any word breaks.  */
104           memset (p, 0, n);
105         }
106     }
107 }
108
109
110 #ifdef TEST
111
112 #include <locale.h>
113 #include <stdio.h>
114 #include <stdlib.h>
115
116 /* Read the contents of an input stream, and return it, terminated with a NUL
117    byte. */
118 char *
119 read_file (FILE *stream)
120 {
121 #define BUFSIZE 4096
122   char *buf = NULL;
123   int alloc = 0;
124   int size = 0;
125   int count;
126
127   while (! feof (stream))
128     {
129       if (size + BUFSIZE > alloc)
130         {
131           alloc = alloc + alloc / 2;
132           if (alloc < size + BUFSIZE)
133             alloc = size + BUFSIZE;
134           buf = realloc (buf, alloc);
135           if (buf == NULL)
136             {
137               fprintf (stderr, "out of memory\n");
138               exit (1);
139             }
140         }
141       count = fread (buf + size, 1, BUFSIZE, stream);
142       if (count == 0)
143         {
144           if (ferror (stream))
145             {
146               perror ("fread");
147               exit (1);
148             }
149         }
150       else
151         size += count;
152     }
153   buf = realloc (buf, size + 1);
154   if (buf == NULL)
155     {
156       fprintf (stderr, "out of memory\n");
157       exit (1);
158     }
159   buf[size] = '\0';
160   return buf;
161 #undef BUFSIZE
162 }
163
164 int
165 main (int argc, char * argv[])
166 {
167   setlocale (LC_CTYPE, "");
168   if (argc == 1)
169     {
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);
174       int i;
175
176       ulc_wordbreaks (input, length, breaks);
177
178       for (i = 0; i < length; i++)
179         {
180           switch (breaks[i])
181             {
182             case 1:
183               putc ('|', stdout);
184               break;
185             case 0:
186               break;
187             default:
188               abort ();
189             }
190           putc (input[i], stdout);
191         }
192
193       free (breaks);
194
195       return 0;
196     }
197   else
198     return 1;
199 }
200
201 #endif /* TEST */