findprog-in: Relicense under LGPLv2+.
[gnulib.git] / lib / unilbrk / ulc-possible-linebreaks.c
blob0f1f851aaa1c5571c5d256738a1ed45a51733717
1 /* Line breaking of strings.
2 Copyright (C) 2001-2003, 2006-2020 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2001.
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 <https://www.gnu.org/licenses/>. */
18 #include <config.h>
20 /* Specification. */
21 #include "unilbrk.h"
23 #include <stdlib.h>
24 #include <string.h>
26 #include "c-ctype.h"
27 #include "uniconv.h"
28 #include "unilbrk/ulc-common.h"
30 /* Line breaking of a string in an arbitrary encoding.
32 We convert the input string to Unicode.
34 The standardized Unicode encodings are UTF-8, UCS-2, UCS-4, UTF-16,
35 UTF-16BE, UTF-16LE, UTF-7. UCS-2 supports only characters up to
36 \U0000FFFF. UTF-16 and variants support only characters up to
37 \U0010FFFF. UTF-7 is way too complex and not supported by glibc-2.1.
38 UCS-4 specification leaves doubts about endianness and byte order mark.
39 glibc currently interprets it as big endian without byte order mark,
40 but this is not backed by an RFC. So we use UTF-8. It supports
41 characters up to \U7FFFFFFF and is unambiguously defined. */
43 void
44 ulc_possible_linebreaks (const char *s, size_t n, const char *encoding,
45 char *p)
47 if (n > 0)
49 if (is_utf8_encoding (encoding))
50 u8_possible_linebreaks ((const uint8_t *) s, n, encoding, p);
51 else
53 /* Convert the string to UTF-8 and build a translation table
54 from offsets into s to offsets into the translated string. */
55 size_t *offsets = (size_t *) malloc (n * sizeof (size_t));
57 if (offsets != NULL)
59 uint8_t *t;
60 size_t m;
62 t = u8_conv_from_encoding (encoding, iconveh_question_mark,
63 s, n, offsets, NULL, &m);
64 if (t != NULL)
66 char *q = (char *) (m > 0 ? malloc (m) : NULL);
68 if (m == 0 || q != NULL)
70 size_t i;
72 /* Determine the possible line breaks of the UTF-8
73 string. */
74 u8_possible_linebreaks (t, m, encoding, q);
76 /* Translate the result back to the original string. */
77 memset (p, UC_BREAK_PROHIBITED, n);
78 for (i = 0; i < n; i++)
79 if (offsets[i] != (size_t)(-1))
80 p[i] = q[offsets[i]];
82 free (q);
83 free (t);
84 free (offsets);
85 return;
87 free (t);
89 free (offsets);
92 /* Impossible to convert. */
93 #if C_CTYPE_ASCII
94 if (is_all_ascii (s, n))
96 /* ASCII is a subset of UTF-8. */
97 u8_possible_linebreaks ((const uint8_t *) s, n, encoding, p);
98 return;
100 #endif
101 /* We have a non-ASCII string and cannot convert it.
102 Don't produce line breaks except those already present in the
103 input string. All we assume here is that the encoding is
104 minimally ASCII compatible. */
106 const char *s_end = s + n;
107 while (s < s_end)
109 *p = (*s == '\n' ? UC_BREAK_MANDATORY : UC_BREAK_PROHIBITED);
110 s++;
111 p++;
119 #ifdef TEST
121 #include <stdio.h>
122 #include <locale.h>
123 #include <string.h>
125 /* Read the contents of an input stream, and return it, terminated with a NUL
126 byte. */
127 char *
128 read_file (FILE *stream)
130 #define BUFSIZE 4096
131 char *buf = NULL;
132 int alloc = 0;
133 int size = 0;
134 int count;
136 while (! feof (stream))
138 if (size + BUFSIZE > alloc)
140 alloc = alloc + alloc / 2;
141 if (alloc < size + BUFSIZE)
142 alloc = size + BUFSIZE;
143 buf = realloc (buf, alloc);
144 if (buf == NULL)
146 fprintf (stderr, "out of memory\n");
147 exit (1);
150 count = fread (buf + size, 1, BUFSIZE, stream);
151 if (count == 0)
153 if (ferror (stream))
155 perror ("fread");
156 exit (1);
159 else
160 size += count;
162 buf = realloc (buf, size + 1);
163 if (buf == NULL)
165 fprintf (stderr, "out of memory\n");
166 exit (1);
168 buf[size] = '\0';
169 return buf;
170 #undef BUFSIZE
174 main (int argc, char * argv[])
176 setlocale (LC_CTYPE, "");
177 if (argc == 1)
179 /* Display all the break opportunities in the input string. */
180 char *input = read_file (stdin);
181 int length = strlen (input);
182 char *breaks = malloc (length);
183 int i;
185 ulc_possible_linebreaks (input, length, locale_charset (), breaks);
187 for (i = 0; i < length; i++)
189 switch (breaks[i])
191 case UC_BREAK_POSSIBLE:
192 putc ('|', stdout);
193 break;
194 case UC_BREAK_MANDATORY:
195 break;
196 case UC_BREAK_PROHIBITED:
197 break;
198 default:
199 abort ();
201 putc (input[i], stdout);
204 free (breaks);
206 return 0;
208 else
209 return 1;
212 #endif /* TEST */