1 /* Line breaking of UTF-32 strings.
2 Copyright (C) 2001-2003, 2006-2025 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/>. */
31 #include "unilbrk/internal.h"
32 #include "unilbrk/lbrktables.h"
36 u32_width_linebreaks_internal (const uint32_t *s
, size_t n
,
37 int width
, int start_column
, int at_end_columns
,
38 const char *o
, const char *encoding
, int cr
,
41 const uint32_t *s_end
;
46 u32_possible_linebreaks_loop (s
, n
, encoding
, cr
, p
);
50 last_column
= start_column
;
56 /* Respect the override. */
57 if (o
!= NULL
&& *o
!= UC_BREAK_UNDEFINED
)
60 if (*p
== UC_BREAK_POSSIBLE
61 || *p
== UC_BREAK_MANDATORY
|| *p
== UC_BREAK_CR_BEFORE_LF
)
63 /* An atomic piece of text ends here. */
64 if (last_p
!= NULL
&& last_column
+ piece_width
> width
)
66 /* Insert a line break. */
67 *last_p
= UC_BREAK_POSSIBLE
;
72 if (*p
== UC_BREAK_MANDATORY
|| *p
== UC_BREAK_CR_BEFORE_LF
)
74 /* uc is a line break character. */
75 /* Start a new piece at column 0. */
82 /* uc is not a line break character. */
85 if (*p
== UC_BREAK_POSSIBLE
)
87 /* Start a new piece. */
89 last_column
+= piece_width
;
91 /* No line break for the moment, may be turned into
92 UC_BREAK_POSSIBLE later, via last_p. */
95 *p
= UC_BREAK_PROHIBITED
;
97 w
= uc_width (uc
, encoding
);
98 if (w
>= 0) /* ignore control characters in the string */
108 /* The last atomic piece of text ends here. */
109 if (last_p
!= NULL
&& last_column
+ piece_width
+ at_end_columns
> width
)
111 /* Insert a line break. */
112 *last_p
= UC_BREAK_POSSIBLE
;
116 return last_column
+ piece_width
;
119 #if defined IN_LIBUNISTRING
120 /* For backward compatibility with older versions of libunistring. */
122 # undef u32_width_linebreaks
125 u32_width_linebreaks (const uint32_t *s
, size_t n
,
126 int width
, int start_column
, int at_end_columns
,
127 const char *o
, const char *encoding
,
130 return u32_width_linebreaks_internal (s
, n
,
131 width
, start_column
, at_end_columns
,
138 u32_width_linebreaks_v2 (const uint32_t *s
, size_t n
,
139 int width
, int start_column
, int at_end_columns
,
140 const char *o
, const char *encoding
,
143 return u32_width_linebreaks_internal (s
, n
,
144 width
, start_column
, at_end_columns
,
145 o
, encoding
, LBP_CR
, p
);