xref: /netbsd-src/lib/libc/locale/wcwidth.3 (revision 01869ca4d24a86379a68731bf9706a9f0820fe4e)
1*01869ca4Swiz.\" $NetBSD: wcwidth.3,v 1.4 2017/07/03 21:32:49 wiz Exp $
2a55e9cdfStnozaki.\" FreeBSD: src/lib/libc/locale/wcwidth.3,v 1.6 2004/08/17 04:56:03 trhodes Exp
3a55e9cdfStnozaki.\"
4a55e9cdfStnozaki.\" Copyright (c) 2002 Tim J. Robbins
5a55e9cdfStnozaki.\" All rights reserved.
6a55e9cdfStnozaki.\"
7a55e9cdfStnozaki.\" Redistribution and use in source and binary forms, with or without
8a55e9cdfStnozaki.\" modification, are permitted provided that the following conditions
9a55e9cdfStnozaki.\" are met:
10a55e9cdfStnozaki.\" 1. Redistributions of source code must retain the above copyright
11a55e9cdfStnozaki.\"    notice, this list of conditions and the following disclaimer.
12a55e9cdfStnozaki.\" 2. Redistributions in binary form must reproduce the above copyright
13a55e9cdfStnozaki.\"    notice, this list of conditions and the following disclaimer in the
14a55e9cdfStnozaki.\"    documentation and/or other materials provided with the distribution.
15a55e9cdfStnozaki.\"
16a55e9cdfStnozaki.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a55e9cdfStnozaki.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a55e9cdfStnozaki.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a55e9cdfStnozaki.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a55e9cdfStnozaki.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a55e9cdfStnozaki.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a55e9cdfStnozaki.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a55e9cdfStnozaki.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a55e9cdfStnozaki.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a55e9cdfStnozaki.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a55e9cdfStnozaki.\" SUCH DAMAGE.
27a55e9cdfStnozaki.\"
28a55e9cdfStnozaki.\"
29a55e9cdfStnozaki.Dd August 17, 2004
30a55e9cdfStnozaki.Dt WCWIDTH 3
31a55e9cdfStnozaki.Os
32a55e9cdfStnozaki.Sh NAME
33a55e9cdfStnozaki.Nm wcwidth
34a55e9cdfStnozaki.Nd "number of column positions of a wide-character code"
35a55e9cdfStnozaki.Sh LIBRARY
36a55e9cdfStnozaki.Lb libc
37a55e9cdfStnozaki.Sh SYNOPSIS
38a55e9cdfStnozaki.In wchar.h
39a55e9cdfStnozaki.Ft int
40a55e9cdfStnozaki.Fn wcwidth "wchar_t wc"
41a55e9cdfStnozaki.Sh DESCRIPTION
42a55e9cdfStnozakiThe
43a55e9cdfStnozaki.Fn wcwidth
44a55e9cdfStnozakifunction determines the number of column positions required to
45a55e9cdfStnozakidisplay the wide character
46a55e9cdfStnozaki.Fa wc .
47a55e9cdfStnozaki.Sh RETURN VALUES
48a55e9cdfStnozakiThe
49a55e9cdfStnozaki.Fn wcwidth
50a55e9cdfStnozakifunction returns 0 if the
51a55e9cdfStnozaki.Fa wc
52f80b3487Swizargument is a nul wide character (L'\e0'),
53a55e9cdfStnozaki\-1 if
54a55e9cdfStnozaki.Fa wc
55a55e9cdfStnozakiis not printable,
56a55e9cdfStnozakiotherwise it returns the number of column positions the
57a55e9cdfStnozakicharacter occupies.
58a55e9cdfStnozaki.Sh EXAMPLES
59a55e9cdfStnozakiThis code fragment reads text from standard input and
60a55e9cdfStnozakibreaks lines that are more than 20 column positions wide,
61a55e9cdfStnozakisimilar to the
62a55e9cdfStnozaki.Xr fold 1
63a55e9cdfStnozakiutility:
64a55e9cdfStnozaki.Bd -literal -offset indent
65a55e9cdfStnozakiwint_t ch;
66a55e9cdfStnozakiint column, w;
67a55e9cdfStnozaki
68a55e9cdfStnozakicolumn = 0;
69a55e9cdfStnozakiwhile ((ch = getwchar()) != WEOF) {
70a55e9cdfStnozaki	w = wcwidth(ch);
71*01869ca4Swiz	if (w > 0 && column + w >= 20) {
72a55e9cdfStnozaki		putwchar(L'\en');
73a55e9cdfStnozaki		column = 0;
74a55e9cdfStnozaki	}
75a55e9cdfStnozaki	putwchar(ch);
76a55e9cdfStnozaki	if (ch == L'\en')
77a55e9cdfStnozaki		column = 0;
78*01869ca4Swiz	else if (w > 0)
79a55e9cdfStnozaki		column += w;
80a55e9cdfStnozaki}
81a55e9cdfStnozaki.Ed
82a55e9cdfStnozaki.Sh SEE ALSO
83a55e9cdfStnozaki.Xr iswprint 3 ,
84a55e9cdfStnozaki.Xr wcswidth 3
85a55e9cdfStnozaki.Sh STANDARDS
86a55e9cdfStnozakiThe
87a55e9cdfStnozaki.Fn wcwidth
88a55e9cdfStnozakifunction conforms to
89a55e9cdfStnozaki.St -p1003.1-2001 .
90