summaryrefslogtreecommitdiff
path: root/static/netbsd/man3/mbrtoc16.3
blob: 757b032498bfc46ba15adea324fa090fb9712886 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
.\"	$NetBSD: mbrtoc16.3,v 1.10 2024/08/23 12:59:49 riastradh Exp $
.\"
.\" Copyright (c) 2024 The NetBSD Foundation, Inc.
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
.\" POSSIBILITY OF SUCH DAMAGE.
.\"
.Dd August 14, 2024
.Dt MBRTOC16 3
.Os
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh NAME
.Nm mbrtoc16
.Nd Restartable multibyte to UTF-16 conversion
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh LIBRARY
.Lb libc
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh SYNOPSIS
.
.In uchar.h
.
.Ft size_t
.Fo mbrtoc16
.Fa "char16_t * restrict pc16"
.Fa "const char * restrict s"
.Fa "size_t n"
.Fa "mbstate_t * restrict ps"
.Fc
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh DESCRIPTION
The
.Nm
function decodes multibyte characters in the current locale and
converts them to UTF-16, keeping state so it can restart after
incremental progress.
.Pp
Each call to
.Nm :
.Bl -enum -compact
.It
examines up to
.Fa n
bytes starting at
.Fa s ,
.It
yields a UTF-16 code unit if available by storing it at
.Li * Ns Fa pc16 ,
.It
saves state at
.Fa ps ,
and
.It
returns either the number of bytes consumed if any or a special return
value.
.El
.Pp
Specifically:
.Bl -bullet
.It
If the multibyte sequence at
.Fa s
is invalid after any previous input saved at
.Fa ps ,
or if an error occurs in decoding,
.Nm
returns
.Li (size_t)-1
and sets
.Xr errno 2
to indicate the error.
.It
If the multibyte sequence at
.Fa s
is still incomplete after
.Fa n
bytes, including any previous input saved in
.Fa ps ,
.Nm
saves its state in
.Fa ps
after all the input so far and returns
.Li "(size_t)-2".
.Sy All
.Fa n
bytes of input are consumed in this case.
.It
If
.Nm
had previously decoded a multibyte character but has not yet yielded
all the code units of its UTF-16 encoding, it stores the next UTF-16
code unit at
.Li * Ns Fa pc16
and returns
.Li "(size_t)-3" .
.Sy \&No
bytes of input are consumed in this case.
.It
If
.Nm
decodes the null multibyte character, then it stores zero at
.Li * Ns Fa pc16
and returns zero.
.It
Otherwise,
.Nm
decodes a single multibyte character, stores the first (and possibly
only) code unit in its UTF-16 encoding at
.Li * Ns Fa pc16 ,
and returns the number of bytes consumed to decode the first multibyte
character.
.El
.Pp
If
.Fa pc16
is a null pointer, nothing is stored, but the effects on
.Fa ps
and the return value are unchanged.
.Pp
If
.Fa s
is a null pointer, the
.Nm
call is equivalent to:
.Bd -ragged -offset indent
.Fo mbrtoc16
.Li NULL ,
.Li \*q\*q ,
.Li 1 ,
.Fa ps
.Fc
.Ed
.Pp
This always returns zero, and has the effect of resetting
.Fa ps
to the initial conversion state, without writing to
.Fa pc16 ,
even if it is nonnull.
.Pp
If
.Fa ps
is a null pointer,
.Nm
uses an internal
.Vt mbstate_t
object with static storage duration, distinct from all other
.Vt mbstate_t
objects
.Po
including those used by
.Xr mbrtoc8 3 ,
.Xr mbrtoc32 3 ,
.Xr c8rtomb 3 ,
.Xr c16rtomb 3 ,
and
.Xr c32rtomb 3
.Pc ,
which is initialized at program startup to the initial conversion
state.
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh IMPLEMENTATION NOTES
On well-formed input, the
.Nm
function yields either a Unicode scalar value in the Basic Multilingual
Plane (BMP), i.e., a 16-bit Unicode code point that is not a surrogate
code point, or, over two successive calls, yields the high and low
surrogate code points (in that order) of a Unicode scalar value outside
the BMP.
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh RETURN VALUES
The
.Nm
function returns:
.Bl -tag -width Li
.It Li 0
.Bq null
if
.Nm
decoded a null multibyte character.
.It Ar i
.Bq code unit
where
.Li 1
\*(Le
.Ar i
\*(Le
.Fa n ,
if
.Nm
consumed
.Ar i
bytes of input to decode the next multibyte character, yielding a
UTF-16 code unit.
.It Li (size_t)-3
.Bq continuation
if
.Nm
consumed no new bytes of input but yielded a UTF-16 code unit that was
pending from previous input.
.It Li (size_t)-2
.Bq incomplete
if
.Nm
found only an incomplete multibyte sequence after all
.Fa n
bytes of input and any previous input, and saved its state to restart
in the next call with
.Fa ps .
.It Li (size_t)-1
.Bq error
if any encoding error was detected;
.Xr errno 2
is set to reflect the error.
.El
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh EXAMPLES
Print the UTF-16 code units of a multibyte string in hexadecimal text:
.Bd -literal -offset indent
char *s = ...;
size_t n = ...;
mbstate_t mbs = {0};    /* initial conversion state */

while (n) {
        char16_t c16;
        size_t len;

        len = mbrtoc16(&c16, s, n, &mbs);
        switch (len) {
        case 0:         /* NUL terminator */
                assert(c16 == 0);
                goto out;
        default:        /* scalar value or high surrogate */
                printf("U+%04"PRIx16"\en", (uint16_t)c16);
                break;
        case (size_t)-3: /* low surrogate */
                printf("continue U+%04"PRIx16"\en", (uint16_t)c16);
                break;
        case (size_t)-2: /* incomplete */
                printf("incomplete\en");
                goto readmore;
        case (size_t)-1: /* error */
                printf("error: %d\en", errno);
                goto out;
        }
        s += len;
        n -= len;
}
.Ed
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh ERRORS
.Bl -tag -width Bq
.It Bq Er EILSEQ
The multibyte sequence cannot be decoded in the current locale as a
Unicode scalar value.
.It Bq Er EIO
An error occurred in loading the locale's character conversions.
.El
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh SEE ALSO
.Xr c16rtomb 3 ,
.Xr c32rtomb 3 ,
.Xr c8rtomb 3 ,
.Xr mbrtoc32 3 ,
.Xr mbrtoc8 3 ,
.Xr uchar 3
.Rs
.%B The Unicode Standard
.%O Version 15.0 \(em Core Specification
.%Q The Unicode Consortium
.%D September 2022
.%U https://www.unicode.org/versions/Unicode15.0.0/UnicodeStandard-15.0.pdf
.Re
.Rs
.%A P. Hoffman
.%A F. Yergeau
.%T UTF-16, an encoding of ISO 10646
.%R RFC 2781
.%D February 2000
.%I Internet Engineering Task Force
.%U https://datatracker.ietf.org/doc/html/rfc2781
.Re
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh STANDARDS
The
.Nm
function conforms to
.St -isoC-2011 .
.\"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
.Sh HISTORY
The
.Nm
function first appeared in
.Nx 11.0 .