Fix whitespace.
[gnulib.git] / lib / unilbrk / u8-possible-linebreaks.c
1 /* Line breaking of UTF-8 strings.
2    Copyright (C) 2001-2003, 2006-2008 Free Software Foundation, Inc.
3    Written by Bruno Haible <bruno@clisp.org>, 2001.
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 "unilbrk.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include "unilbrk/tables.h"
27 #include "uniwidth/cjk.h"
28 #include "unistr.h"
29
30 void
31 u8_possible_linebreaks (const uint8_t *s, size_t n, const char *encoding, char *p)
32 {
33   int LBP_AI_REPLACEMENT = (is_cjk_encoding (encoding) ? LBP_ID : LBP_AL);
34   const uint8_t *s_end = s + n;
35   int last_prop = LBP_BK; /* line break property of last non-space character */
36   char *seen_space = NULL; /* Was a space seen after the last non-space character? */
37   char *seen_space2 = NULL; /* At least two spaces after the last non-space? */
38
39   /* Don't break inside multibyte characters.  */
40   memset (p, UC_BREAK_PROHIBITED, n);
41
42   while (s < s_end)
43     {
44       ucs4_t uc;
45       int count = u8_mbtouc_unsafe (&uc, s, s_end - s);
46       int prop = unilbrkprop_lookup (uc);
47
48       if (prop == LBP_BK)
49         {
50           /* Mandatory break.  */
51           *p = UC_BREAK_MANDATORY;
52           last_prop = LBP_BK;
53           seen_space = NULL;
54           seen_space2 = NULL;
55         }
56       else
57         {
58           char *q;
59
60           /* Resolve property values whose behaviour is not fixed.  */
61           switch (prop)
62             {
63             case LBP_AI:
64               /* Resolve ambiguous.  */
65               prop = LBP_AI_REPLACEMENT;
66               break;
67             case LBP_CB:
68               /* This is arbitrary.  */
69               prop = LBP_ID;
70               break;
71             case LBP_SA:
72               /* We don't handle complex scripts yet.
73                  Treat LBP_SA like LBP_XX.  */
74             case LBP_XX:
75               /* This is arbitrary.  */
76               prop = LBP_AL;
77               break;
78             }
79
80           /* Deal with combining characters.  */
81           q = p;
82           if (prop == LBP_CM)
83             {
84               /* Don't break just before a combining character.  */
85               *p = UC_BREAK_PROHIBITED;
86               /* A combining character turns a preceding space into LBP_AL.  */
87               if (seen_space != NULL)
88                 {
89                   q = seen_space;
90                   seen_space = seen_space2;
91                   prop = LBP_AL;
92                   goto lookup_via_table;
93                 }
94             }
95           else if (prop == LBP_SP)
96             {
97               /* Don't break just before a space.  */
98               *p = UC_BREAK_PROHIBITED;
99               seen_space2 = seen_space;
100               seen_space = p;
101             }
102           else
103             {
104              lookup_via_table:
105               /* prop must be usable as an index for table 7.3 of UTR #14.  */
106               if (!(prop >= 1 && prop <= sizeof (unilbrk_table) / sizeof (unilbrk_table[0])))
107                 abort ();
108
109               if (last_prop == LBP_BK)
110                 {
111                   /* Don't break at the beginning of a line.  */
112                   *q = UC_BREAK_PROHIBITED;
113                 }
114               else
115                 {
116                   switch (unilbrk_table [last_prop-1] [prop-1])
117                     {
118                     case D:
119                       *q = UC_BREAK_POSSIBLE;
120                       break;
121                     case I:
122                       *q = (seen_space != NULL ? UC_BREAK_POSSIBLE : UC_BREAK_PROHIBITED);
123                       break;
124                     case P:
125                       *q = UC_BREAK_PROHIBITED;
126                       break;
127                     default:
128                       abort ();
129                     }
130                 }
131               last_prop = prop;
132               seen_space = NULL;
133               seen_space2 = NULL;
134             }
135         }
136
137       s += count;
138       p += count;
139     }
140 }
141
142
143 #ifdef TEST
144
145 #include <stdio.h>
146 #include <string.h>
147
148 /* Read the contents of an input stream, and return it, terminated with a NUL
149    byte. */
150 char *
151 read_file (FILE *stream)
152 {
153 #define BUFSIZE 4096
154   char *buf = NULL;
155   int alloc = 0;
156   int size = 0;
157   int count;
158
159   while (! feof (stream))
160     {
161       if (size + BUFSIZE > alloc)
162         {
163           alloc = alloc + alloc / 2;
164           if (alloc < size + BUFSIZE)
165             alloc = size + BUFSIZE;
166           buf = realloc (buf, alloc);
167           if (buf == NULL)
168             {
169               fprintf (stderr, "out of memory\n");
170               exit (1);
171             }
172         }
173       count = fread (buf + size, 1, BUFSIZE, stream);
174       if (count == 0)
175         {
176           if (ferror (stream))
177             {
178               perror ("fread");
179               exit (1);
180             }
181         }
182       else
183         size += count;
184     }
185   buf = realloc (buf, size + 1);
186   if (buf == NULL)
187     {
188       fprintf (stderr, "out of memory\n");
189       exit (1);
190     }
191   buf[size] = '\0';
192   return buf;
193 #undef BUFSIZE
194 }
195
196 int
197 main (int argc, char * argv[])
198 {
199   if (argc == 1)
200     {
201       /* Display all the break opportunities in the input string.  */
202       char *input = read_file (stdin);
203       int length = strlen (input);
204       char *breaks = malloc (length);
205       int i;
206
207       u8_possible_linebreaks ((uint8_t *) input, length, "UTF-8", breaks);
208
209       for (i = 0; i < length; i++)
210         {
211           switch (breaks[i])
212             {
213             case UC_BREAK_POSSIBLE:
214               /* U+2027 in UTF-8 encoding */
215               putc (0xe2, stdout); putc (0x80, stdout); putc (0xa7, stdout);
216               break;
217             case UC_BREAK_MANDATORY:
218               /* U+21B2 (or U+21B5) in UTF-8 encoding */
219               putc (0xe2, stdout); putc (0x86, stdout); putc (0xb2, stdout);
220               break;
221             case UC_BREAK_PROHIBITED:
222               break;
223             default:
224               abort ();
225             }
226           putc (input[i], stdout);
227         }
228
229       free (breaks);
230
231       return 0;
232     }
233   else
234     return 1;
235 }
236
237 #endif /* TEST */