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/>. */
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. */
44 ulc_possible_linebreaks (const char *s
, size_t n
, const char *encoding
,
49 if (is_utf8_encoding (encoding
))
50 u8_possible_linebreaks ((const uint8_t *) s
, n
, encoding
, p
);
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));
62 t
= u8_conv_from_encoding (encoding
, iconveh_question_mark
,
63 s
, n
, offsets
, NULL
, &m
);
66 char *q
= (char *) (m
> 0 ? malloc (m
) : NULL
);
68 if (m
== 0 || q
!= NULL
)
72 /* Determine the possible line breaks of the UTF-8
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))
92 /* Impossible to convert. */
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
);
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
;
109 *p
= (*s
== '\n' ? UC_BREAK_MANDATORY
: UC_BREAK_PROHIBITED
);
125 /* Read the contents of an input stream, and return it, terminated with a NUL
128 read_file (FILE *stream
)
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
);
146 fprintf (stderr
, "out of memory\n");
150 count
= fread (buf
+ size
, 1, BUFSIZE
, stream
);
162 buf
= realloc (buf
, size
+ 1);
165 fprintf (stderr
, "out of memory\n");
174 main (int argc
, char * argv
[])
176 setlocale (LC_CTYPE
, "");
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
);
185 ulc_possible_linebreaks (input
, length
, locale_charset (), breaks
);
187 for (i
= 0; i
< length
; i
++)
191 case UC_BREAK_POSSIBLE
:
194 case UC_BREAK_MANDATORY
:
196 case UC_BREAK_PROHIBITED
:
201 putc (input
[i
], stdout
);