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)
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
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/>. */
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. */
54 ulc_possible_linebreaks_internal (const char *s
, size_t n
, const char *encoding
,
59 if (is_utf8_encoding (encoding
))
60 u8_possible_linebreaks_loop ((const uint8_t *) s
, n
, encoding
, cr
, p
);
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));
72 t
= u8_conv_from_encoding (encoding
, iconveh_question_mark
,
73 s
, n
, offsets
, NULL
, &m
);
76 char *q
= (char *) (m
> 0 ? malloc (m
) : NULL
);
78 if (m
== 0 || q
!= NULL
)
82 /* Determine the possible line breaks of the UTF-8
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))
102 /* Impossible to convert. */
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
);
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
;
125 ? UC_BREAK_CR_BEFORE_LF
126 : UC_BREAK_PROHIBITED
));
135 #if defined IN_LIBUNISTRING
136 /* For backward compatibility with older versions of libunistring. */
138 # undef ulc_possible_linebreaks
141 ulc_possible_linebreaks (const char *s
, size_t n
, const char *encoding
,
144 ulc_possible_linebreaks_internal (s
, n
, encoding
, -1, p
);
150 ulc_possible_linebreaks_v2 (const char *s
, size_t n
, const char *encoding
,
153 ulc_possible_linebreaks_internal (s
, n
, encoding
, LBP_CR
, p
);
163 /* Read the contents of an input stream, and return it, terminated with a NUL
166 read_file (FILE *stream
)
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
);
184 fprintf (stderr
, "out of memory\n");
188 count
= fread (buf
+ size
, 1, BUFSIZE
, stream
);
200 buf
= realloc (buf
, size
+ 1);
203 fprintf (stderr
, "out of memory\n");
212 main (int argc
, char * argv
[])
214 setlocale (LC_CTYPE
, "");
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
);
223 ulc_possible_linebreaks_v2 (input
, length
, locale_charset (), breaks
);
225 for (i
= 0; i
< length
; i
++)
229 case UC_BREAK_POSSIBLE
:
232 case UC_BREAK_MANDATORY
:
234 case UC_BREAK_CR_BEFORE_LF
:
236 case UC_BREAK_PROHIBITED
:
241 putc (input
[i
], stdout
);