usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / lib / unilbrk / ulc-possible-linebreaks.c
blob4be1f987416bee40f839299a3011ab09fad76d30
1 /* Line breaking of strings.
2 Copyright (C) 2001-2003, 2006-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2001.
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+".
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.
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/>. */
26 #include <config.h>
28 /* Specification. */
29 #include "unilbrk.h"
31 #include <stdlib.h>
32 #include <string.h>
34 #include "c-ctype.h"
35 #include "uniconv.h"
36 #include "unilbrk/internal.h"
37 #include "unilbrk/lbrktables.h"
38 #include "unilbrk/ulc-common.h"
40 /* Line breaking of a string in an arbitrary encoding.
42 We convert the input string to Unicode.
44 The standardized Unicode encodings are UTF-8, UCS-2, UCS-4, UTF-16,
45 UTF-16BE, UTF-16LE, UTF-7. UCS-2 supports only characters up to
46 \U0000FFFF. UTF-16 and variants support only characters up to
47 \U0010FFFF. UTF-7 is way too complex and not supported by glibc-2.1.
48 UCS-4 specification leaves doubts about endianness and byte order mark.
49 glibc currently interprets it as big endian without byte order mark,
50 but this is not backed by an RFC. So we use UTF-8. It supports
51 characters up to \U7FFFFFFF and is unambiguously defined. */
53 static void
54 ulc_possible_linebreaks_internal (const char *s, size_t n, const char *encoding,
55 int cr, char *p)
57 if (n > 0)
59 if (is_utf8_encoding (encoding))
60 u8_possible_linebreaks_loop ((const uint8_t *) s, n, encoding, cr, p);
61 else
63 /* Convert the string to UTF-8 and build a translation table
64 from offsets into s to offsets into the translated string. */
65 size_t *offsets = (size_t *) malloc (n * sizeof (size_t));
67 if (offsets != NULL)
69 uint8_t *t;
70 size_t m;
72 t = u8_conv_from_encoding (encoding, iconveh_question_mark,
73 s, n, offsets, NULL, &m);
74 if (t != NULL)
76 char *q = (char *) (m > 0 ? malloc (m) : NULL);
78 if (m == 0 || q != NULL)
80 size_t i;
82 /* Determine the possible line breaks of the UTF-8
83 string. */
84 u8_possible_linebreaks_loop (t, m, encoding, cr, q);
86 /* Translate the result back to the original string. */
87 memset (p, UC_BREAK_PROHIBITED, n);
88 for (i = 0; i < n; i++)
89 if (offsets[i] != (size_t)(-1))
90 p[i] = q[offsets[i]];
92 free (q);
93 free (t);
94 free (offsets);
95 return;
97 free (t);
99 free (offsets);
102 /* Impossible to convert. */
103 #if C_CTYPE_ASCII
104 if (is_all_ascii (s, n))
106 /* ASCII is a subset of UTF-8. */
107 u8_possible_linebreaks_loop ((const uint8_t *) s, n, encoding, cr, p);
108 return;
110 #endif
111 /* We have a non-ASCII string and cannot convert it.
112 Don't produce line breaks except those already present in the
113 input string. All we assume here is that the encoding is
114 minimally ASCII compatible. */
116 const char *s_end = s + n;
117 while (s < s_end)
119 *p = (*s == '\n'
120 ? UC_BREAK_MANDATORY
121 : ((cr >= 0
122 && *s == '\r'
123 && s + 1 < s_end
124 && *(s + 1) == '\n')
125 ? UC_BREAK_CR_BEFORE_LF
126 : UC_BREAK_PROHIBITED));
127 s++;
128 p++;
135 #if defined IN_LIBUNISTRING
136 /* For backward compatibility with older versions of libunistring. */
138 # undef ulc_possible_linebreaks
140 void
141 ulc_possible_linebreaks (const char *s, size_t n, const char *encoding,
142 char *p)
144 ulc_possible_linebreaks_internal (s, n, encoding, -1, p);
147 #endif
149 void
150 ulc_possible_linebreaks_v2 (const char *s, size_t n, const char *encoding,
151 char *p)
153 ulc_possible_linebreaks_internal (s, n, encoding, LBP_CR, p);
157 #ifdef TEST
159 #include <stdio.h>
160 #include <locale.h>
161 #include <string.h>
163 /* Read the contents of an input stream, and return it, terminated with a NUL
164 byte. */
165 char *
166 read_file (FILE *stream)
168 #define BUFSIZE 4096
169 char *buf = NULL;
170 int alloc = 0;
171 int size = 0;
172 int count;
174 while (! feof (stream))
176 if (size + BUFSIZE > alloc)
178 alloc = alloc + alloc / 2;
179 if (alloc < size + BUFSIZE)
180 alloc = size + BUFSIZE;
181 buf = realloc (buf, alloc);
182 if (buf == NULL)
184 fprintf (stderr, "out of memory\n");
185 exit (1);
188 count = fread (buf + size, 1, BUFSIZE, stream);
189 if (count == 0)
191 if (ferror (stream))
193 perror ("fread");
194 exit (1);
197 else
198 size += count;
200 buf = realloc (buf, size + 1);
201 if (buf == NULL)
203 fprintf (stderr, "out of memory\n");
204 exit (1);
206 buf[size] = '\0';
207 return buf;
208 #undef BUFSIZE
212 main (int argc, char * argv[])
214 setlocale (LC_CTYPE, "");
215 if (argc == 1)
217 /* Display all the break opportunities in the input string. */
218 char *input = read_file (stdin);
219 int length = strlen (input);
220 char *breaks = malloc (length);
221 int i;
223 ulc_possible_linebreaks_v2 (input, length, locale_charset (), breaks);
225 for (i = 0; i < length; i++)
227 switch (breaks[i])
229 case UC_BREAK_POSSIBLE:
230 putc ('|', stdout);
231 break;
232 case UC_BREAK_MANDATORY:
233 break;
234 case UC_BREAK_CR_BEFORE_LF:
235 break;
236 case UC_BREAK_PROHIBITED:
237 break;
238 default:
239 abort ();
241 putc (input[i], stdout);
244 free (breaks);
246 return 0;
248 else
249 return 1;
252 #endif /* TEST */