1 /* Line breaking of strings.
2 Copyright (C) 2001-2003, 2006-2021 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2001.
4
5 This file is free software.
6 It is dual-licensed under "the GNU LGPLv3+ or the GNU GPLv2+".
7 You can redistribute it and/or modify it under either
8 - the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version, or
11 - the terms of the GNU General Public License as published by the
12 Free Software Foundation; either version 2, or (at your option)
13 any later version, or
14 - the same dual license "the GNU LGPLv3+ or the GNU GPLv2+".
15
16 This file is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License and the GNU General Public License
20 for more details.
21
22 You should have received a copy of the GNU Lesser General Public
23 License and of the GNU General Public License along with this
24 program. If not, see <https://www.gnu.org/licenses/>. */
25
26 #include <config.h>
27
28 /* Specification. */
29 #include "unilbrk.h"
30
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "c-ctype.h"
35 #include "uniconv.h"
36 #include "unilbrk/ulc-common.h"
37
38 /* Line breaking of a string in an arbitrary encoding.
39
40 We convert the input string to Unicode.
41
42 The standardized Unicode encodings are UTF-8, UCS-2, UCS-4, UTF-16,
43 UTF-16BE, UTF-16LE, UTF-7. UCS-2 supports only characters up to
44 \U0000FFFF. UTF-16 and variants support only characters up to
45 \U0010FFFF. UTF-7 is way too complex and not supported by glibc-2.1.
46 UCS-4 specification leaves doubts about endianness and byte order mark.
47 glibc currently interprets it as big endian without byte order mark,
48 but this is not backed by an RFC. So we use UTF-8. It supports
49 characters up to \U7FFFFFFF and is unambiguously defined. */
50
51 void
52 ulc_possible_linebreaks (const char *s, size_t n, const char *encoding,
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
53 char *p)
54 {
55 if (n > 0)
56 {
57 if (is_utf8_encoding (encoding))
58 u8_possible_linebreaks ((const uint8_t *) s, n, encoding, p);
59 else
60 {
61 /* Convert the string to UTF-8 and build a translation table
62 from offsets into s to offsets into the translated string. */
63 size_t *offsets = (size_t *) malloc (n * sizeof (size_t));
64
65 if (offsets != NULL)
66 {
67 uint8_t *t;
68 size_t m;
69
70 t = u8_conv_from_encoding (encoding, iconveh_question_mark,
71 s, n, offsets, NULL, &m);
72 if (t != NULL)
73 {
74 char *q = (char *) (m > 0 ? malloc (m) : NULL);
75
76 if (m == 0 || q != NULL)
77 {
78 size_t i;
79
80 /* Determine the possible line breaks of the UTF-8
81 string. */
82 u8_possible_linebreaks (t, m, encoding, q);
83
84 /* Translate the result back to the original string. */
85 memset (p, UC_BREAK_PROHIBITED, n);
86 for (i = 0; i < n; i++)
87 if (offsets[i] != (size_t)(-1))
88 p[i] = q[offsets[i]];
89
90 free (q);
91 free (t);
92 free (offsets);
93 return;
94 }
95 free (t);
96 }
97 free (offsets);
98 }
99
100 /* Impossible to convert. */
101 #if C_CTYPE_ASCII
102 if (is_all_ascii (s, n))
103 {
104 /* ASCII is a subset of UTF-8. */
105 u8_possible_linebreaks ((const uint8_t *) s, n, encoding, p);
106 return;
107 }
108 #endif
109 /* We have a non-ASCII string and cannot convert it.
110 Don't produce line breaks except those already present in the
111 input string. All we assume here is that the encoding is
112 minimally ASCII compatible. */
113 {
114 const char *s_end = s + n;
115 while (s < s_end)
116 {
117 *p = (*s == '\n' ? UC_BREAK_MANDATORY : UC_BREAK_PROHIBITED);
118 s++;
119 p++;
120 }
121 }
122 }
123 }
124 }
125
126
127 #ifdef TEST
128
129 #include <stdio.h>
130 #include <locale.h>
131 #include <string.h>
132
133 /* Read the contents of an input stream, and return it, terminated with a NUL
134 byte. */
135 char *
136 read_file (FILE *stream)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
137 {
138 #define BUFSIZE 4096
139 char *buf = NULL;
140 int alloc = 0;
141 int size = 0;
142 int count;
143
144 while (! feof (stream))
145 {
146 if (size + BUFSIZE > alloc)
147 {
148 alloc = alloc + alloc / 2;
149 if (alloc < size + BUFSIZE)
150 alloc = size + BUFSIZE;
151 buf = realloc (buf, alloc);
152 if (buf == NULL)
153 {
154 fprintf (stderr, "out of memory\n");
155 exit (1);
156 }
157 }
158 count = fread (buf + size, 1, BUFSIZE, stream);
159 if (count == 0)
160 {
161 if (ferror (stream))
162 {
163 perror ("fread");
164 exit (1);
165 }
166 }
167 else
168 size += count;
169 }
170 buf = realloc (buf, size + 1);
171 if (buf == NULL)
172 {
173 fprintf (stderr, "out of memory\n");
174 exit (1);
175 }
176 buf[size] = '\0';
177 return buf;
178 #undef BUFSIZE
179 }
180
181 int
182 main (int argc, char * argv[])
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
183 {
184 setlocale (LC_CTYPE, "");
185 if (argc == 1)
186 {
187 /* Display all the break opportunities in the input string. */
188 char *input = read_file (stdin);
189 int length = strlen (input);
190 char *breaks = malloc (length);
191 int i;
192
193 ulc_possible_linebreaks (input, length, locale_charset (), breaks);
194
195 for (i = 0; i < length; i++)
196 {
197 switch (breaks[i])
198 {
199 case UC_BREAK_POSSIBLE:
200 putc ('|', stdout);
201 break;
202 case UC_BREAK_MANDATORY:
203 break;
204 case UC_BREAK_PROHIBITED:
205 break;
206 default:
207 abort ();
208 }
209 putc (input[i], stdout);
210 }
211
212 free (breaks);
213
214 return 0;
215 }
216 else
217 return 1;
218 }
219
220 #endif /* TEST */