xref: /freebsd/lib/libc/locale/mbrlen.3 (revision 7183f43d9543e724830f9895f32fd681be00b776)
1.\" Copyright (c) 2002 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.Dd November 11, 2002
27.Dt MBRLEN 3
28.Os
29.Sh NAME
30.Nm mbrlen
31.Nd "get number of bytes in a character (restartable)"
32.Sh LIBRARY
33.Lb libc
34.Sh SYNOPSIS
35.In wchar.h
36.Ft size_t
37.Fn mbrlen "const char * restrict s" "size_t n" "mbstate_t * restrict ps"
38.Sh DESCRIPTION
39The
40.Fn mbrlen
41function determines the the number of bytes constituting the
42multibyte character sequence pointed to by
43.Fa s ,
44examining at most
45.Fa n
46bytes.
47.Pp
48The
49.Ft mbstate_t
50argument,
51.Fa ps ,
52is used to keep track of the shift state.
53If it is
54.Dv NULL ,
55.Fn mbrlen
56uses an internal, static
57.Ft mbstate_t
58object.
59.Pp
60It is equivalent to:
61.Pp
62.Dl "mbrtowc(NULL, s, n, ps);"
63.Pp
64Except that when
65.Fa ps
66is a NULL pointer,
67.Fn mbrlen
68uses its own static, internal
69.Ft mbstate_t
70object to keep track of shift state.
71.Sh RETURN VALUES
72The
73.Fn mbrlen
74functions returns:
75.Bl -tag -width indent
76.It 0
77The first
78.Fa n
79or fewer bytes of
80.Fa s
81represent the null wide character (L'\e0').
82.It >0
83The first
84.Fa n
85or fewer bytes of
86.Fa s
87represent a valid character,
88.Fn mbrtowc
89returns the length (in bytes) of the multibyte sequence.
90.It Xo
91.No ( Ns
92.Ft size_t Ns
93.No ) Ns \&-2
94.Xc
95The first
96.Fa n
97bytes of
98.Fa s
99are an incomplete multibyte sequence.
100.It Xo
101.No ( Ns
102.Ft size_t Ns
103.No ) Ns \&-1
104.Xc
105The byte sequence pointed to by
106.Fa s
107is an invalid multibyte sequence.
108.El
109.Sh EXAMPLES
110A function which calculates the number of characters in a multibyte
111character string:
112.Bd -literal -offset indent
113size_t
114nchars(const char *s)
115{
116	size_t charlen, chars;
117	mbstate_t mbs;
118
119	chars = 0;
120	memset(&mbs, 0, sizeof(mbs));
121	while ((charlen = mbrlen(s, MB_CUR_MAX, &mbs)) != 0 &&
122	    charlen != (size_t)-1 && charlen != (size_t)-2) {
123		s += charlen;
124		chars++;
125	}
126
127	return (chars);
128}
129.Ed
130.Sh ERRORS
131The
132.Fn mbrlen
133function will fail if:
134.Bl -tag -width Er
135.\".It Bq Er EINVAL
136.\"Invalid argument.
137.It Bq Er EILSEQ
138An invalid multibyte sequence was detected.
139.El
140.Sh SEE ALSO
141.Xr mblen 3 ,
142.Xr mbrtowc 3
143.Sh STANDARDS
144The
145.Fn mbrlen
146function conforms to
147.St -isoC-99 .
148.Sh BUGS
149The current implementation does not support shift states.
150