xref: /illumos-gate/usr/src/lib/libc/port/locale/isdigit.c (revision 5f82aa32fbc5dc2c59bca6ff315f44a4c4c9ea86)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2013 Garrett D'Amore <garrett@damore.org>
14  * Copyright 2017 Nexenta Systems, Inc.
15  */
16 
17 /*
18  * This file contains the implementation of various functional forms
19  * of the ctype tests, specifically the required by ISO C.  These are defined
20  * in the "C" (POSIX) locale.
21  */
22 
23 #include "lint.h"
24 #include <ctype.h>
25 #include <locale.h>
26 #include "localeimpl.h"
27 #include "_ctype.h"
28 #include "lctype.h"
29 
30 /*
31  * As far as we know, *every* encoding we support is a strict superset of ASCII,
32  * so we can make things faster by trying ASCII first.  Next check if argument
33  * can be represented as unsigned char, and that locale is not multibyte - every
34  * multibyte encoding we support has non-ASCII code points undefined.  Finally,
35  * lookup the result in locale specific table.
36  */
37 #define	ISTYPE_L(c, mask, loc)	\
38 	(isascii(c) ? (__ctype_mask[c] & (mask)) : \
39 	((unsigned)c > 255 || loc->ctype->lc_max_mblen > 1) ? 0 : \
40 	(loc->ctype->lc_ctype_mask[c] & mask))
41 
42 #define	ISTYPE(c, mask)	ISTYPE_L(c, mask, uselocale(NULL))
43 
44 #define	DEFN_ISTYPE(type, mask)		\
45 int					\
46 is##type##_l(int c, locale_t l)	\
47 {					\
48 	return (ISTYPE_L(c, mask, l));	\
49 }					\
50 					\
51 int					\
52 is##type(int c)				\
53 {					\
54 	return (ISTYPE(c, mask));	\
55 }
56 
57 /*
58  * We are supplying functional forms, so make sure to suppress any macros
59  * we might have imported.
60  */
61 #undef	isblank
62 #undef	isupper
63 #undef	islower
64 #undef	isdigit
65 #undef	isxdigit
66 #undef	isalpha
67 #undef	isalnum
68 #undef	isspace
69 #undef	iscntrl
70 #undef	isgraph
71 #undef	ispunct
72 #undef	isprint
73 
74 DEFN_ISTYPE(blank, _ISBLANK)
75 DEFN_ISTYPE(upper, _ISUPPER)
76 DEFN_ISTYPE(lower, _ISLOWER)
77 DEFN_ISTYPE(digit, _ISDIGIT)
78 DEFN_ISTYPE(xdigit, _ISXDIGIT)
79 DEFN_ISTYPE(alpha, _ISALPHA)
80 DEFN_ISTYPE(alnum, _ISALNUM)
81 DEFN_ISTYPE(space, _ISSPACE)
82 DEFN_ISTYPE(cntrl, _ISCNTRL)
83 DEFN_ISTYPE(graph, _ISGRAPH)
84 DEFN_ISTYPE(punct, _ISPUNCT)
85 DEFN_ISTYPE(print, _ISPRINT)
86