usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / lib / unilbrk / u32-width-linebreaks.c
blobc731ff5ef4c7dcb5a2ab1a9dd3e95df026c13ac6
1 /* Line breaking of UTF-32 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 "unilbrk/internal.h"
32 #include "unilbrk/lbrktables.h"
33 #include "uniwidth.h"
35 static int
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,
39 char *p)
41 const uint32_t *s_end;
42 char *last_p;
43 int last_column;
44 int piece_width;
46 u32_possible_linebreaks_loop (s, n, encoding, cr, p);
48 s_end = s + n;
49 last_p = NULL;
50 last_column = start_column;
51 piece_width = 0;
52 while (s < s_end)
54 ucs4_t uc = *s;
56 /* Respect the override. */
57 if (o != NULL && *o != UC_BREAK_UNDEFINED)
58 *p = *o;
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;
68 last_column = 0;
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. */
76 last_p = NULL;
77 last_column = 0;
78 piece_width = 0;
80 else
82 /* uc is not a line break character. */
83 int w;
85 if (*p == UC_BREAK_POSSIBLE)
87 /* Start a new piece. */
88 last_p = p;
89 last_column += piece_width;
90 piece_width = 0;
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 */
99 piece_width += w;
102 s++;
103 p++;
104 if (o != NULL)
105 o++;
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;
113 last_column = 0;
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,
128 char *p)
130 return u32_width_linebreaks_internal (s, n,
131 width, start_column, at_end_columns,
132 o, encoding, -1, p);
135 #endif
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,
141 char *p)
143 return u32_width_linebreaks_internal (s, n,
144 width, start_column, at_end_columns,
145 o, encoding, LBP_CR, p);