xref: /freebsd/contrib/nvi/common/multibyte.h (revision 1603881667360c015f6685131f2f25474fa67a72)
1 /*-
2  * Copyright (c) 1992, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5  *	Keith Bostic.  All rights reserved.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9 
10 #ifndef MULTIBYTE_H
11 #define MULTIBYTE_H
12 
13 /*
14  * Fundamental character types.
15  *
16  * CHAR_T	An integral type that can hold any character.
17  * ARG_CHAR_T	The type of a CHAR_T when passed as an argument using
18  *		traditional promotion rules.  It should also be able
19  *		to be compared against any CHAR_T for equality without
20  *		problems.
21  * UCHAR_T	The shortest unified character type (8-bit clean).
22  * RCHAR_T	The character type used by the internal regex engine.
23  *
24  * If no integral type can hold a character, don't even try the port.
25  */
26 typedef	int		ARG_CHAR_T;
27 
28 #ifdef USE_WIDECHAR
29 #include <wchar.h>
30 #include <wctype.h>
31 
32 typedef	wchar_t		CHAR_T;
33 typedef	wint_t		UCHAR_T;
34 typedef wchar_t 	RCHAR_T;
35 #define REOF		WEOF
36 
37 #define STRLEN		wcslen
38 #define STRTOL		wcstol
39 #define STRTOUL		wcstoul
40 #define SPRINTF		swprintf
41 #define STRCMP		wcscmp
42 #define STRPBRK		wcspbrk
43 #define ISBLANK		iswblank
44 #define ISCNTRL		iswcntrl
45 #define ISDIGIT		iswdigit
46 #define ISXDIGIT	iswxdigit
47 #define ISGRAPH		iswgraph
48 #define ISLOWER		iswlower
49 #define ISPRINT		iswprint
50 #define ISPUNCT		iswpunct
51 #define ISSPACE		iswspace
52 #define ISUPPER		iswupper
53 #define TOLOWER		towlower
54 #define TOUPPER		towupper
55 #define STRSET		wmemset
56 #define STRCHR		wcschr
57 #define STRRCHR		wcsrchr
58 #define GETC		getwc
59 
60 #define L(ch)		L ## ch
61 #define WS		"%ls"
62 #define WVS		"%*ls"
63 #define WC		"%lc"
64 
65 #else
66 typedef	u_char		CHAR_T;
67 typedef	u_char		UCHAR_T;
68 typedef	char		RCHAR_T;
69 #define REOF		EOF
70 
71 #define STRLEN		strlen
72 #define STRTOL(a,b,c)	(strtol(a,(char**)b,c))
73 #define STRTOUL(a,b,c)	(strtoul(a,(char**)b,c))
74 #define SPRINTF		snprintf
75 #define STRCMP		strcmp
76 #define STRPBRK		strpbrk
77 #define ISBLANK		isblank
78 #define ISCNTRL		iscntrl
79 #define ISDIGIT		isdigit
80 #define ISXDIGIT	isxdigit
81 #define ISGRAPH		isgraph
82 #define ISLOWER		islower
83 #define ISPRINT		isprint
84 #define ISPUNCT		ispunct
85 #define ISSPACE		isspace
86 #define ISUPPER		isupper
87 #define TOLOWER		tolower
88 #define TOUPPER		toupper
89 #define STRSET		memset
90 #define STRCHR		strchr
91 #define STRRCHR		strrchr
92 #define GETC		getc
93 
94 #define L(ch)		ch
95 #define WS		"%s"
96 #define WVS		"%*s"
97 #define WC		"%c"
98 
99 #endif
100 
101 #if defined(USE_WIDECHAR) && defined(DEBUG)
102 #define MEMCPY			wmemcpy
103 #define MEMMOVE			wmemmove
104 #define MEMCMP			wmemcmp
105 #else
106 #define MEMCPY(p, t, len)	memcpy(p, t, (len) * sizeof(CHAR_T))
107 #define MEMMOVE(p, t, len)	memmove(p, t, (len) * sizeof(CHAR_T))
108 #define MEMCMP(p, t, len)	memcmp(p, t, (len) * sizeof(CHAR_T))
109 #endif
110 
111 #define SIZE(w)			(sizeof(w) / sizeof(*w))
112 
113 /*
114  * Locale insensitive character category detection.
115  */
116 
117 static __inline int
118 isatoz(CHAR_T c)
119 {
120 	return 'a' <= c && c <= 'z';
121 }
122 
123 static __inline int
124 isAtoZ(CHAR_T c)
125 {
126 	return 'A' <= c && c <= 'Z';
127 }
128 
129 static __inline int
130 is0to9(CHAR_T c)
131 {
132 	return '0' <= c && c <= '9';
133 }
134 
135 static __inline int
136 isazAZ(CHAR_T c)
137 {
138 	return isatoz(c) || isAtoZ(c);
139 }
140 
141 static __inline int
142 is09azAZ(CHAR_T c)
143 {
144 	return is0to9(c) || isazAZ(c);
145 }
146 
147 #endif
148