1 /*        $NetBSD: multibyte.h,v 1.4 2017/11/13 01:34:59 rin Exp $    */
2 #ifndef MULTIBYTE_H
3 #define MULTIBYTE_H
4 
5 /*
6  * Ex/vi commands are generally separated by whitespace characters.  We
7  * can't use the standard isspace(3) macro because it returns true for
8  * characters like ^K in the ASCII character set.  The 4.4BSD isblank(3)
9  * macro does exactly what we want, but it's not portable yet.
10  *
11  * XXX
12  * Note side effect, ch is evaluated multiple times.
13  */
14 #define ISBLANK(c)  ((c) == ' ' || (c) == '\t')
15 
16 #define ISDIGIT(c)  ((c) >= '0' && (c) <= '9')
17 #define ISXDIGIT(c) (ISDIGIT(c) || \
18                                ((c) >= 'A' && (c) <= 'F') || ((c) >= 'a' && (c) <= 'f'))
19 #define ISALPHA(c)  (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
20 #define ISALNUM(c)  (ISALPHA(c) || ISDIGIT(c))
21 
22 /*
23  * Fundamental character types.
24  *
25  * CHAR_T       An integral type that can hold any character.
26  * ARG_CHAR_T   The type of a CHAR_T when passed as an argument using
27  *              traditional promotion rules.  It should also be able
28  *              to be compared against any CHAR_T for equality without
29  *              problems.
30  *
31  * If no integral type can hold a character, don't even try the port.
32  */
33 
34 #ifdef USE_WIDECHAR
35 #include <wchar.h>
36 #include <wctype.h>
37 
38 typedef wchar_t               RCHAR_T;
39 #define REOF                  WEOF
40 typedef wchar_t               CHAR_T;
41 typedef   wint_t              ARG_CHAR_T;
42 typedef wint_t                UCHAR_T;
43 
44 #define STRLEN                wcslen
45 #define STRTOL                wcstol
46 #define STRTOUL               wcstoul
47 #define SPRINTF               swprintf
48 #define STRCHR                wcschr
49 #define STRCMP                wcscmp
50 #define STRPBRK               wcspbrk
51 #define ISBLANK2    iswblank
52 #define ISCNTRL               iswcntrl
53 #define ISGRAPH               iswgraph
54 #define ISLOWER               iswlower
55 #define ISALPHA2    iswalpha
56 #define ISPUNCT               iswpunct
57 #define ISSPACE               iswspace
58 #define ISUPPER               iswupper
59 #define TOLOWER               towlower
60 #define TOUPPER               towupper
61 #define STRSET                wmemset
62 #define STRCHR                wcschr
63 
64 #define L(ch)                 L ## ch
65 #define WS                    "%ls"
66 #define WVS                   "%*ls"
67 #define WC                    "%lc"
68 
69 #else
70 #include <stdio.h>
71 
72 #define REOF                  EOF
73 typedef   char                CHAR_T;
74 typedef   int                 ARG_CHAR_T;
75 typedef   unsigned char       UCHAR_T;
76 
77 #define STRLEN                strlen
78 #define STRTOL                strtol
79 #define STRTOUL               strtoul
80 #define SPRINTF               snprintf
81 #define STRCHR                strchr
82 #define STRCMP                strcmp
83 #define STRPBRK               strpbrk
84 #define ISBLANK2    isblank
85 #define ISCNTRL               iscntrl
86 #define ISGRAPH               isgraph
87 #define ISLOWER               islower
88 #define ISALPHA2    isalpha
89 #define ISPUNCT               ispunct
90 #define ISSPACE               isspace
91 #define ISUPPER               isupper
92 #define TOLOWER               tolower
93 #define TOUPPER               toupper
94 #define STRSET                memset
95 #define STRCHR                strchr
96 
97 #define L(ch)                 ch
98 #define WS                    "%s"
99 #define WVS                   "%*s"
100 #define WC                    "%c"
101 
102 #endif
103 
104 #define MEMCMP(to, from, n)                                                         \
105           memcmp(to, from, (n) * sizeof(*(to)))
106 #define   MEMMOVE(p, t, len)  memmove(p, t, (len) * sizeof(*(p)))
107 #define   MEMCPY(p, t, len)   memcpy(p, t, (len) * sizeof(*(p)))
108 /*
109  * Like MEMCPY but return a pointer to the end of the copied bytes.
110  * Glibc has a function mempcpy with the same purpose.
111  */
112 #define MEMPCPY(p, t, len) \
113           ((void *)((char *)MEMCPY(p, t, len) + (len) * sizeof(*(p))))
114 #define SIZE(w)               (sizeof(w)/sizeof(*w))
115 
116 #endif
117