usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / lib / unilbrk / u16-width-linebreaks.c
blobcdf461112c86a55970f11ea4439e8adc2670fbea
1 /* Line breaking of UTF-16 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 "unistr.h"
34 #include "uniwidth.h"
36 static int
37 u16_width_linebreaks_internal (const uint16_t *s, size_t n,
38 int width, int start_column, int at_end_columns,
39 const char *o, const char *encoding, int cr,
40 char *p)
42 const uint16_t *s_end;
43 char *last_p;
44 int last_column;
45 int piece_width;
47 u16_possible_linebreaks_loop (s, n, encoding, cr, p);
49 s_end = s + n;
50 last_p = NULL;
51 last_column = start_column;
52 piece_width = 0;
53 while (s < s_end)
55 ucs4_t uc;
56 int count = u16_mbtouc_unsafe (&uc, s, s_end - s);
58 /* Respect the override. */
59 if (o != NULL && *o != UC_BREAK_UNDEFINED)
60 *p = *o;
62 if (*p == UC_BREAK_POSSIBLE
63 || *p == UC_BREAK_MANDATORY || *p == UC_BREAK_CR_BEFORE_LF)
65 /* An atomic piece of text ends here. */
66 if (last_p != NULL && last_column + piece_width > width)
68 /* Insert a line break. */
69 *last_p = UC_BREAK_POSSIBLE;
70 last_column = 0;
74 if (*p == UC_BREAK_MANDATORY || *p == UC_BREAK_CR_BEFORE_LF)
76 /* uc is a line break character. */
77 /* Start a new piece at column 0. */
78 last_p = NULL;
79 last_column = 0;
80 piece_width = 0;
82 else
84 /* uc is not a line break character. */
85 int w;
87 if (*p == UC_BREAK_POSSIBLE)
89 /* Start a new piece. */
90 last_p = p;
91 last_column += piece_width;
92 piece_width = 0;
93 /* No line break for the moment, may be turned into
94 UC_BREAK_POSSIBLE later, via last_p. */
97 *p = UC_BREAK_PROHIBITED;
99 w = uc_width (uc, encoding);
100 if (w >= 0) /* ignore control characters in the string */
101 piece_width += w;
104 s += count;
105 p += count;
106 if (o != NULL)
107 o += count;
110 /* The last atomic piece of text ends here. */
111 if (last_p != NULL && last_column + piece_width + at_end_columns > width)
113 /* Insert a line break. */
114 *last_p = UC_BREAK_POSSIBLE;
115 last_column = 0;
118 return last_column + piece_width;
121 #if defined IN_LIBUNISTRING
122 /* For backward compatibility with older versions of libunistring. */
124 # undef u16_width_linebreaks
127 u16_width_linebreaks (const uint16_t *s, size_t n,
128 int width, int start_column, int at_end_columns,
129 const char *o, const char *encoding,
130 char *p)
132 return u16_width_linebreaks_internal (s, n,
133 width, start_column, at_end_columns,
134 o, encoding, -1, p);
137 #endif
140 u16_width_linebreaks_v2 (const uint16_t *s, size_t n,
141 int width, int start_column, int at_end_columns,
142 const char *o, const char *encoding,
143 char *p)
145 return u16_width_linebreaks_internal (s, n,
146 width, start_column, at_end_columns,
147 o, encoding, LBP_CR, p);