xref: /freebsd/lib/libc/locale/multibyte.3 (revision 734e82fe33aa764367791a7d603b383996c6b40b)
1.\" Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
2.\" Copyright (c) 1993
3.\"	The Regents of the University of California.  All rights reserved.
4.\"
5.\" This code is derived from software contributed to Berkeley by
6.\" Donn Seeley of BSDI.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"	@(#)multibyte.3	8.1 (Berkeley) 6/4/93
33.\"
34.Dd September 9, 2019
35.Dt MULTIBYTE 3
36.Os
37.Sh NAME
38.Nm multibyte
39.Nd multibyte and wide character manipulation functions
40.Sh LIBRARY
41.Lb libc
42.Sh SYNOPSIS
43.In limits.h
44.In stdlib.h
45.In wchar.h
46.Sh DESCRIPTION
47The basic elements of some written natural languages, such as Chinese,
48cannot be represented uniquely with single C
49.Vt char Ns s .
50The C standard supports two different ways of dealing with
51extended natural language encodings:
52wide characters and
53multibyte characters.
54Wide characters are an internal representation
55which allows each basic element to map
56to a single object of type
57.Vt wchar_t .
58Multibyte characters are used for input and output
59and code each basic element as a sequence of C
60.Vt char Ns s .
61Individual basic elements may map into one or more
62(up to
63.Dv MB_LEN_MAX )
64bytes in a multibyte character.
65.Pp
66The current locale
67.Pq Xr setlocale 3
68governs the interpretation of wide and multibyte characters.
69The locale category
70.Dv LC_CTYPE
71specifically controls this interpretation.
72The
73.Vt wchar_t
74type is wide enough to hold the largest value
75in the wide character representations for all locales.
76.Pp
77Multibyte strings may contain
78.Sq shift
79indicators to switch to and from
80particular modes within the given representation.
81If explicit bytes are used to signal shifting,
82these are not recognized as separate characters
83but are lumped with a neighboring character.
84There is always a distinguished
85.Sq initial
86shift state.
87Some functions (e.g.,
88.Xr mblen 3 ,
89.Xr mbtowc 3
90and
91.Xr wctomb 3 )
92maintain static shift state internally, whereas
93others store it in an
94.Vt mbstate_t
95object passed by the caller.
96Shift states are undefined after a call to
97.Xr setlocale 3
98with the
99.Dv LC_CTYPE
100or
101.Dv LC_ALL
102categories.
103.Pp
104For convenience in processing,
105the wide character with value 0
106(the null wide character)
107is recognized as the wide character string terminator,
108and the character with value 0
109(the null byte)
110is recognized as the multibyte character string terminator.
111Null bytes are not permitted within multibyte characters.
112.Pp
113The C library provides the following functions for dealing with
114multibyte characters:
115.Bl -column "Description"
116.It Sy "Function	Description"
117.It Xr mblen 3 Ta "get number of bytes in a character"
118.It Xr mbrlen 3 Ta "get number of bytes in a character (restartable)"
119.It Xr mbrtowc 3 Ta "convert a character to a wide-character code (restartable)"
120.It Xr mbsrtowcs 3 Ta "convert a character string to a wide-character string (restartable)"
121.It Xr mbstowcs 3 Ta "convert a character string to a wide-character string"
122.It Xr mbtowc 3 Ta "convert a character to a wide-character code"
123.It Xr wcrtomb 3 Ta "convert a wide-character code to a character (restartable)"
124.It Xr wcstombs 3 Ta "convert a wide-character string to a character string"
125.It Xr wcsrtombs 3 Ta "convert a wide-character string to a character string (restartable)"
126.It Xr wctomb 3 Ta "convert a wide-character code to a character"
127.El
128.Sh SEE ALSO
129.Xr localedef 1 ,
130.Xr setlocale 3 ,
131.Xr stdio 3 ,
132.Xr big5 5 ,
133.Xr euc 5 ,
134.Xr gb18030 5 ,
135.Xr gb2312 5 ,
136.Xr gbk 5 ,
137.Xr mskanji 5 ,
138.Xr utf8 5
139.Sh STANDARDS
140These functions conform to
141.St -isoC-99 .
142