xref: /freebsd/lib/libc/locale/mbrlen.3 (revision f4937dbebcd0080c3bdf48e68a7c8bb4eebd0642)
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 October 4, 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 .
44.Pp
45It is equivalent to:
46.Pp
47.Dl "mbrtowc(NULL, s, n, ps);"
48.Pp
49Except that when
50.Fa ps
51is a NULL pointer,
52.Fn mbrlen
53uses its own static, internal
54.Ft mbstate_t
55object to keep track of shift state.
56.Sh RETURN VALUES
57The
58.Fn mbrlen
59functions returns:
60.Bl -tag -width indent
61.It 0
62The first
63.Fa n
64or fewer bytes of
65.Fa s
66represent the null wide character (L'\e0').
67.It >0
68The first
69.Fa n
70or fewer bytes of
71.Fa s
72represent a valid character,
73.Fn mbrtowc
74returns the length (in bytes) of the multibyte sequence.
75.It Xo
76.No ( Ns
77.Ft size_t Ns
78.No ) Ns \&-2
79.Xc
80The first
81.Fa n
82bytes of
83.Fa s
84are an incomplete multibyte sequence.
85.It Xo
86.No ( Ns
87.Ft size_t Ns
88.No ) Ns \&-1
89.Xc
90The byte sequence pointed to by
91.Fa s
92is an invalid multibyte sequence.
93.El
94.Sh EXAMPLES
95A function which calculates the number of characters in a multibyte
96character string:
97.Bd -literal -offset indent
98size_t
99nchars(const char *s)
100{
101	size_t charlen, chars;
102	mbstate_t mbs;
103
104	chars = 0;
105	memset(&mbs, 0, sizeof(mbs));
106	while ((charlen = mbrlen(s, MB_CUR_MAX, &mbs)) != 0 &&
107	    charlen != (size_t)-1 && charlen != (size_t)-2) {
108		s += charlen;
109		chars++;
110	}
111
112	return (chars);
113}
114.Ed
115.Sh ERRORS
116The
117.Fn mbrlen
118function will fail if:
119.Bl -tag -width Er
120.\".It Bq Er EINVAL
121.\"Invalid argument.
122.It Bq Er EILSEQ
123An invalid multibyte sequence was detected.
124.El
125.Sh SEE ALSO
126.Xr mblen 3 ,
127.Xr mbrtowc 3
128.Sh STANDARDS
129The
130.Fn mbrlen
131function conforms to
132.St -isoC-99 .
133.Sh BUGS
134The current implementation does not support shift states.
135