1/*
2 * Copyright 2022, Trung Nguyen, trungnt282910@gmail.com
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5
6
7#include <ctype.h>
8#include <locale.h>
9
10#include <LocaleBackend.h>
11#include <PosixCtype.h>
12
13
14namespace BPrivate {
15namespace Libroot {
16
17
18// A NULL databridge means that the object represents the POSIX locale.
19static const unsigned short*
20GetClassInfoTable(locale_t l)
21{
22	LocaleBackendData* locale = (LocaleBackendData*)l;
23
24	if (locale->databridge == NULL) {
25		return &gPosixClassInfo[128];
26	}
27	return *locale->databridge->ctypeDataBridge.addrOfClassInfoTable;
28}
29
30
31static const int*
32GetToUpperTable(locale_t l)
33{
34	LocaleBackendData* locale = (LocaleBackendData*)l;
35
36	if (locale->databridge == NULL) {
37		return &gPosixToUpperMap[128];
38	}
39	return *locale->databridge->ctypeDataBridge.addrOfToUpperTable;
40}
41
42
43static const int*
44GetToLowerTable(locale_t l)
45{
46	LocaleBackendData* locale = (LocaleBackendData*)l;
47
48	if (locale->databridge == NULL) {
49		return &gPosixToLowerMap[128];
50	}
51	return *locale->databridge->ctypeDataBridge.addrOfToLowerTable;
52}
53
54
55extern "C" {
56
57
58int
59isalnum_l(int c, locale_t l)
60{
61	if (c >= -128 && c < 256)
62		return GetClassInfoTable(l)[c] & (_ISupper | _ISlower | _ISdigit);
63
64	return 0;
65}
66
67
68int
69isalpha_l(int c, locale_t l)
70{
71	if (c >= -128 && c < 256)
72		return GetClassInfoTable(l)[c] & (_ISupper | _ISlower);
73
74	return 0;
75}
76
77
78int
79isblank_l(int c, locale_t l)
80{
81	if (c >= -128 && c < 256)
82		return GetClassInfoTable(l)[c] & _ISblank;
83
84	return 0;
85}
86
87
88int
89iscntrl_l(int c, locale_t l)
90{
91	if (c >= -128 && c < 256)
92		return GetClassInfoTable(l)[c] & _IScntrl;
93
94	return 0;
95}
96
97
98int
99isdigit_l(int c, locale_t l)
100{
101	if (c >= -128 && c < 256)
102		return GetClassInfoTable(l)[c] & _ISdigit;
103
104	return 0;
105}
106
107
108int
109isgraph_l(int c, locale_t l)
110{
111	if (c >= -128 && c < 256)
112		return GetClassInfoTable(l)[c] & _ISgraph;
113
114	return 0;
115}
116
117
118int
119islower_l(int c, locale_t l)
120{
121	if (c >= -128 && c < 256)
122		return GetClassInfoTable(l)[c] & _ISlower;
123
124	return 0;
125}
126
127
128int
129isprint_l(int c, locale_t l)
130{
131	if (c >= -128 && c < 256)
132		return GetClassInfoTable(l)[c] & _ISprint;
133
134	return 0;
135}
136
137
138int
139ispunct_l(int c, locale_t l)
140{
141	if (c >= -128 && c < 256)
142		return GetClassInfoTable(l)[c] & _ISpunct;
143
144	return 0;
145}
146
147
148int
149isspace_l(int c, locale_t l)
150{
151	if (c >= -128 && c < 256)
152		return GetClassInfoTable(l)[c] & _ISspace;
153
154	return 0;
155}
156
157
158int
159isupper_l(int c, locale_t l)
160{
161	if (c >= -128 && c < 256)
162		return GetClassInfoTable(l)[c] & _ISupper;
163
164	return 0;
165}
166
167
168int
169isxdigit_l(int c, locale_t l)
170{
171	if (c >= -128 && c < 256)
172		return GetClassInfoTable(l)[c] & _ISxdigit;
173
174	return 0;
175}
176
177
178int
179tolower_l(int c, locale_t l)
180{
181	if (c >= -128 && c < 256)
182		return GetToLowerTable(l)[c];
183
184	return c;
185}
186
187
188int
189toupper_l(int c, locale_t l)
190{
191	if (c >= -128 && c < 256)
192		return GetToUpperTable(l)[c];
193
194	return c;
195}
196
197
198}
199
200
201}	// namespace Libroot
202}	// namespace BPrivate
203