xref: /freebsd/lib/libc/locale/mbrlen.3 (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1.\" Copyright (c) 2002-2004 Tim J. Robbins
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.\" $FreeBSD$
26.\"
27.Dd April 7, 2004
28.Dt MBRLEN 3
29.Os
30.Sh NAME
31.Nm mbrlen
32.Nd "get number of bytes in a character (restartable)"
33.Sh LIBRARY
34.Lb libc
35.Sh SYNOPSIS
36.In wchar.h
37.Ft size_t
38.Fn mbrlen "const char * restrict s" "size_t n" "mbstate_t * restrict ps"
39.Sh DESCRIPTION
40The
41.Fn mbrlen
42function inspects at most
43.Fa n
44bytes pointed to by
45.Fa s
46to determine the number of bytes needed to complete the next
47multibyte character.
48.Pp
49The
50.Vt mbstate_t
51argument,
52.Fa ps ,
53is used to keep track of the shift state.
54If it is
55.Dv NULL ,
56.Fn mbrlen
57uses an internal, static
58.Vt mbstate_t
59object, which is initialized to the initial conversion state
60at program startup.
61.Pp
62It is equivalent to:
63.Pp
64.Dl "mbrtowc(NULL, s, n, ps);"
65.Pp
66Except that when
67.Fa ps
68is a
69.Dv NULL
70pointer,
71.Fn mbrlen
72uses its own static, internal
73.Vt mbstate_t
74object to keep track of the shift state.
75.Sh RETURN VALUES
76The
77.Fn mbrlen
78functions returns:
79.Bl -tag -width indent
80.It 0
81The next
82.Fa n
83or fewer bytes
84represent the null wide character
85.Pq Li "L'\e0'" .
86.It >0
87The next
88.Fa n
89or fewer bytes
90represent a valid character,
91.Fn mbrlen
92returns the number of bytes used to complete the multibyte character.
93.It Po Vt size_t Pc Ns \-2
94The next
95.Fa n
96contribute to, but do not complete, a valid multibyte character sequence,
97and all
98.Fa n
99bytes have been processed.
100.It Po Vt size_t Pc Ns \-1
101An encoding error has occurred.
102The next
103.Fa n
104or fewer bytes do not contribute to a valid multibyte character.
105.El
106.Sh EXAMPLES
107A function that calculates the number of characters in a multibyte
108character string:
109.Bd -literal -offset indent
110size_t
111nchars(const char *s)
112{
113	size_t charlen, chars;
114	mbstate_t mbs;
115
116	chars = 0;
117	memset(&mbs, 0, sizeof(mbs));
118	while ((charlen = mbrlen(s, MB_CUR_MAX, &mbs)) != 0 &&
119	    charlen != (size_t)-1 && charlen != (size_t)-2) {
120		s += charlen;
121		chars++;
122	}
123
124	return (chars);
125}
126.Ed
127.Sh ERRORS
128The
129.Fn mbrlen
130function will fail if:
131.Bl -tag -width Er
132.It Bq Er EILSEQ
133An invalid multibyte sequence was detected.
134.It Bq Er EINVAL
135The conversion state is invalid.
136.El
137.Sh SEE ALSO
138.Xr mblen 3 ,
139.Xr mbrtowc 3 ,
140.Xr multibyte 3
141.Sh STANDARDS
142The
143.Fn mbrlen
144function conforms to
145.St -isoC-99 .
146