]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - lib/libc/locale/mbrlen.3
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / lib / libc / locale / mbrlen.3
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
40 The
41 .Fn mbrlen
42 function inspects at most
43 .Fa n
44 bytes pointed to by
45 .Fa s
46 to determine the number of bytes needed to complete the next
47 multibyte character.
48 .Pp
49 The
50 .Vt mbstate_t
51 argument,
52 .Fa ps ,
53 is used to keep track of the shift state.
54 If it is
55 .Dv NULL ,
56 .Fn mbrlen
57 uses an internal, static
58 .Vt mbstate_t
59 object, which is initialized to the initial conversion state
60 at program startup.
61 .Pp
62 It is equivalent to:
63 .Pp
64 .Dl "mbrtowc(NULL, s, n, ps);"
65 .Pp
66 Except that when
67 .Fa ps
68 is a
69 .Dv NULL
70 pointer,
71 .Fn mbrlen
72 uses its own static, internal
73 .Vt mbstate_t
74 object to keep track of the shift state.
75 .Sh RETURN VALUES
76 The
77 .Fn mbrlen
78 functions returns:
79 .Bl -tag -width indent
80 .It 0
81 The next
82 .Fa n
83 or fewer bytes
84 represent the null wide character
85 .Pq Li "L'\e0'" .
86 .It >0
87 The next
88 .Fa n
89 or fewer bytes
90 represent a valid character,
91 .Fn mbrlen
92 returns the number of bytes used to complete the multibyte character.
93 .It Po Vt size_t Pc Ns \-2
94 The next
95 .Fa n
96 contribute to, but do not complete, a valid multibyte character sequence,
97 and all
98 .Fa n
99 bytes have been processed.
100 .It Po Vt size_t Pc Ns \-1
101 An encoding error has occurred.
102 The next
103 .Fa n
104 or fewer bytes do not contribute to a valid multibyte character.
105 .El
106 .Sh EXAMPLES
107 A function that calculates the number of characters in a multibyte
108 character string:
109 .Bd -literal -offset indent
110 size_t
111 nchars(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
128 The
129 .Fn mbrlen
130 function will fail if:
131 .Bl -tag -width Er
132 .It Bq Er EILSEQ
133 An invalid multibyte sequence was detected.
134 .It Bq Er EINVAL
135 The 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
142 The
143 .Fn mbrlen
144 function conforms to
145 .St -isoC-99 .