xref: /freebsd/lib/libc/locale/wcwidth.c (revision 1da6b56aca11f8f1e287a432951ea7e35f274f0c)
1d8d0cebeSAndrey A. Chernov /*
2d8d0cebeSAndrey A. Chernov  * Copyright (c) 1989, 1993
3d8d0cebeSAndrey A. Chernov  *	The Regents of the University of California.  All rights reserved.
4d8d0cebeSAndrey A. Chernov  * (c) UNIX System Laboratories, Inc.
5d8d0cebeSAndrey A. Chernov  * All or some portions of this file are derived from material licensed
6d8d0cebeSAndrey A. Chernov  * to the University of California by American Telephone and Telegraph
7d8d0cebeSAndrey A. Chernov  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8d8d0cebeSAndrey A. Chernov  * the permission of UNIX System Laboratories, Inc.
9d8d0cebeSAndrey A. Chernov  *
10d8d0cebeSAndrey A. Chernov  * This code is derived from software contributed to Berkeley by
11d8d0cebeSAndrey A. Chernov  * Paul Borman at Krystal Technologies.
12d8d0cebeSAndrey A. Chernov  *
13d8d0cebeSAndrey A. Chernov  * Redistribution and use in source and binary forms, with or without
14d8d0cebeSAndrey A. Chernov  * modification, are permitted provided that the following conditions
15d8d0cebeSAndrey A. Chernov  * are met:
16d8d0cebeSAndrey A. Chernov  * 1. Redistributions of source code must retain the above copyright
17d8d0cebeSAndrey A. Chernov  *    notice, this list of conditions and the following disclaimer.
18d8d0cebeSAndrey A. Chernov  * 2. Redistributions in binary form must reproduce the above copyright
19d8d0cebeSAndrey A. Chernov  *    notice, this list of conditions and the following disclaimer in the
20d8d0cebeSAndrey A. Chernov  *    documentation and/or other materials provided with the distribution.
21d8d0cebeSAndrey A. Chernov  * 3. All advertising materials mentioning features or use of this software
22d8d0cebeSAndrey A. Chernov  *    must display the following acknowledgement:
23d8d0cebeSAndrey A. Chernov  *	This product includes software developed by the University of
24d8d0cebeSAndrey A. Chernov  *	California, Berkeley and its contributors.
25d8d0cebeSAndrey A. Chernov  * 4. Neither the name of the University nor the names of its contributors
26d8d0cebeSAndrey A. Chernov  *    may be used to endorse or promote products derived from this software
27d8d0cebeSAndrey A. Chernov  *    without specific prior written permission.
28d8d0cebeSAndrey A. Chernov  *
29d8d0cebeSAndrey A. Chernov  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30d8d0cebeSAndrey A. Chernov  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31d8d0cebeSAndrey A. Chernov  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32d8d0cebeSAndrey A. Chernov  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33d8d0cebeSAndrey A. Chernov  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34d8d0cebeSAndrey A. Chernov  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35d8d0cebeSAndrey A. Chernov  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36d8d0cebeSAndrey A. Chernov  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37d8d0cebeSAndrey A. Chernov  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38d8d0cebeSAndrey A. Chernov  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39d8d0cebeSAndrey A. Chernov  * SUCH DAMAGE.
40d8d0cebeSAndrey A. Chernov  */
41d8d0cebeSAndrey A. Chernov 
42d8d0cebeSAndrey A. Chernov #include <sys/cdefs.h>
43d8d0cebeSAndrey A. Chernov __FBSDID("$FreeBSD$");
44d8d0cebeSAndrey A. Chernov 
45d8d0cebeSAndrey A. Chernov #include <wchar.h>
46d8d0cebeSAndrey A. Chernov #include <wctype.h>
47d8d0cebeSAndrey A. Chernov 
489424df44SAndrey A. Chernov #define _CTYPE_SWM 0xe0000000L	/* Mask to get screen width data */
499424df44SAndrey A. Chernov #define _CTYPE_SWS 30 		/* Bits to shift to get width */
509424df44SAndrey A. Chernov 
51d8d0cebeSAndrey A. Chernov int
521da6b56aSAndrey A. Chernov wcwidth(wchar_t wc)
53d8d0cebeSAndrey A. Chernov {
541e2cd544SAndrey A. Chernov 	int width;
551e2cd544SAndrey A. Chernov 
56853c779dSAndrey A. Chernov 	if (wc == L'\0')
571e2cd544SAndrey A. Chernov 		return (0);
581e2cd544SAndrey A. Chernov 
591e2cd544SAndrey A. Chernov 	width = __maskrune(wc, _CTYPE_SWM);
60d8d0cebeSAndrey A. Chernov 
619424df44SAndrey A. Chernov 	/* 0 is autowidth (default) */
62d8d0cebeSAndrey A. Chernov 	return (width ? (int)((unsigned)width >> _CTYPE_SWS)
63d8d0cebeSAndrey A. Chernov 		      : (iswprint(wc) ? 1 : -1));
64d8d0cebeSAndrey A. Chernov }
65d8d0cebeSAndrey A. Chernov 
66