-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathctype.h
545 lines (518 loc) · 22.6 KB
/
ctype.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#ifndef RBIMPL_CTYPE_H /*-*-C++-*-vi:se ft=cpp:*/
#define RBIMPL_CTYPE_H
/**
* @file
* @author Ruby developers <ruby-core@ruby-lang.org>
* @copyright This file is a part of the programming language Ruby.
* Permission is hereby granted, to either redistribute and/or
* modify this file, provided that the conditions mentioned in the
* file COPYING are met. Consult the file for details.
* @warning Symbols prefixed with either `RBIMPL` or `rbimpl` are
* implementation details. Don't take them as canon. They could
* rapidly appear then vanish. The name (path) of this header file
* is also an implementation detail. Do not expect it to persist
* at the place it is now. Developers are free to move it anywhere
* anytime at will.
* @note To ruby-core: remember that this header can be possibly
* recursively included from extension libraries written in C++.
* Do not expect for instance `__VA_ARGS__` is always available.
* We assume C99 for ruby itself but we don't assume languages of
* extension libraries. They could be written in C++98.
* @brief Our own, locale independent, character handling routines.
*/
#include "ruby/internal/config.h"
#ifdef STDC_HEADERS
# include <ctype.h>
#endif
#include "ruby/internal/attr/artificial.h"
#include "ruby/internal/attr/const.h"
#include "ruby/internal/attr/constexpr.h"
#include "ruby/internal/attr/nonnull.h"
#include "ruby/internal/dllexport.h"
/**
* @name Old character classification macros
*
* What is this #ISPRINT business? Well, according to our VCS and some
* internet surfing, it appears that the initial intent of these macros were to
* mimic codes appear in common in several GNU projects. As far as @shyouhei
* detects they seem to originate GNU regex (that standalone one rather than
* Gnulib or Glibc), and at least date back to 1995.
*
* Let me lawfully quote from a GNU coreutils commit
* https://git.savannah.gnu.org/cgit/coreutils.git/commit/?id=49803907f5dbd7646184a8912c9db9b09dcd0f22
*
* > Jim Meyering writes:
* >
* > "... Some ctype macros are valid only for character codes that
* > isascii says are ASCII (SGI's IRIX-4.0.5 is one such system --when
* > using /bin/cc or gcc but without giving an ansi option). So, all
* > ctype uses should be through macros like ISPRINT... If
* > STDC_HEADERS is defined, then autoconf has verified that the ctype
* > macros don't need to be guarded with references to isascii. ...
* > Defining isascii to 1 should let any compiler worth its salt
* > eliminate the && through constant folding."
* >
* > Bruno Haible adds:
* >
* > "... Furthermore, isupper(c) etc. have an undefined result if c is
* > outside the range -1 <= c <= 255. One is tempted to write isupper(c)
* > with c being of type `char', but this is wrong if c is an 8-bit
* > character >= 128 which gets sign-extended to a negative value.
* > The macro ISUPPER protects against this as well."
*
* So the intent was to reroute old problematic systems that no longer exist.
* At the same time the problems described above no longer hurt us, because we
* decided to completely avoid using system-provided isupper etc. to reinvent
* the wheel. These macros are entirely legacy; please ignore them.
*
* But let me also put stress that GNU people are wise; they use those macros
* only inside of their own implementations and never let them be public. On
* the other hand ruby has thoughtlessly publicised them to 3rd party libraries
* since its beginning, which is a very bad idea. These macros are too easy to
* get conflicted with definitions elsewhere.
*
* New programs should stick to the `rb_` prefixed names.
*
* @note It seems we just mimic the API. We do not share their implementation
* with GPL-ed programs.
*
* @{
*/
#ifndef ISPRINT
# define ISASCII rb_isascii /**< @old{rb_isascii}*/
# define ISPRINT rb_isprint /**< @old{rb_isprint}*/
# define ISGRAPH rb_isgraph /**< @old{rb_isgraph}*/
# define ISSPACE rb_isspace /**< @old{rb_isspace}*/
# define ISUPPER rb_isupper /**< @old{rb_isupper}*/
# define ISLOWER rb_islower /**< @old{rb_islower}*/
# define ISALNUM rb_isalnum /**< @old{rb_isalnum}*/
# define ISALPHA rb_isalpha /**< @old{rb_isalpha}*/
# define ISDIGIT rb_isdigit /**< @old{rb_isdigit}*/
# define ISXDIGIT rb_isxdigit /**< @old{rb_isxdigit}*/
# define ISBLANK rb_isblank /**< @old{rb_isblank}*/
# define ISCNTRL rb_iscntrl /**< @old{rb_iscntrl}*/
# define ISPUNCT rb_ispunct /**< @old{rb_ispunct}*/
#endif
#define TOUPPER rb_toupper /**< @old{rb_toupper}*/
#define TOLOWER rb_tolower /**< @old{rb_tolower}*/
#define STRCASECMP st_locale_insensitive_strcasecmp /**< @old{st_locale_insensitive_strcasecmp}*/
#define STRNCASECMP st_locale_insensitive_strncasecmp /**< @old{st_locale_insensitive_strncasecmp}*/
#define STRTOUL ruby_strtoul /**< @old{ruby_strtoul}*/
/** @} */
RBIMPL_SYMBOL_EXPORT_BEGIN()
/** @name locale insensitive functions
* @{
*/
/* In descriptions below, `the POSIX Locale` and `the "C" locale` are tactfully
* used as to whether the described function mimics POSIX or C99. */
RBIMPL_ATTR_NONNULL(())
/**
* Our own locale-insensitive version of `strcasecmp(3)`. The "case" here
* always means that of the POSIX Locale. It doesn't depend on runtime locale
* settings.
*
* @param[in] s1 Comparison LHS.
* @param[in] s2 Comparison RHS.
* @retval -1 `s1` is "less" than `s2`.
* @retval 0 Both strings converted into lowercase would be identical.
* @retval 1 `s1` is "greater" than `s2`.
* @note Not only does this function works under the POSIX Locale, but
* also assumes its execution character set be what ruby calls an
* ASCII-compatible character set; which does not include for
* instance EBCDIC or UTF-16LE.
*/
int st_locale_insensitive_strcasecmp(const char *s1, const char *s2);
RBIMPL_ATTR_NONNULL(())
/**
* Our own locale-insensitive version of `strcnasecmp(3)`. The "case" here
* always means that of the POSIX Locale. It doesn't depend on runtime locale
* settings.
*
* @param[in] s1 Comparison LHS.
* @param[in] s2 Comparison RHS.
* @param[in] n Comparison shall stop after first `n` bytes are scanned.
* @retval -1 `s1` is "less" than `s2`.
* @retval 0 Both strings converted into lowercase would be identical.
* @retval 1 `s1` is "greater" than `s2`.
* @note Not only does this function works under the POSIX Locale, but
* also assumes its execution character set be what ruby calls an
* ASCII-compatible character set; which does not include for
* instance EBCDIC or UTF-16LE.
* @warning This function is _not_ timing safe.
*/
int st_locale_insensitive_strncasecmp(const char *s1, const char *s2, size_t n);
RBIMPL_ATTR_NONNULL((1))
/**
* Our own locale-insensitive version of `strtoul(3)`. The conversion is done
* as if the current locale is set to the "C" locale, no matter actual runtime
* locale settings.
*
* @note This is needed because `strtoul("i", 0, 36)` would return zero
* if it is locale sensitive and the current locale is `tr_TR`.
* @param[in] str String of digits, optionally preceded with whitespaces
* (ignored) and optionally `+` or `-` sign.
* @param[out] endptr NULL, or an arbitrary pointer (overwritten on return).
* @param[in] base `2` to `36` inclusive for each base, or special case
* `0` to detect the base from the contents of the string.
* @return Converted integer, casted to unsigned long.
* @post If `endptr` is not NULL, it is updated to point the first such
* byte where conversion failed.
* @note This function sets `errno` on failure.
* - `EINVAL`: Passed `base` is out of range.
* - `ERANGE`: Converted integer is out of range of `long`.
* @warning As far as @shyouhei reads ISO/IEC 9899:2018 section 7.22.1.4, a
* conforming `strtoul` implementation shall render `ERANGE`
* whenever it finds the input string represents a negative
* integer. Such thing can never be representable using `unsigned
* long`. However this implementation does not honour that
* language. It just casts such negative value to the return
* type, resulting a very big return value. This behaviour is at
* least questionable. But we can no longer change that at this
* point.
* @note Not only does this function works under the "C" locale, but
* also assumes its execution character set be what ruby calls an
* ASCII-compatible character set; which does not include for
* instance EBCDIC or UTF-16LE.
*/
unsigned long ruby_strtoul(const char *str, char **endptr, int base);
RBIMPL_SYMBOL_EXPORT_END()
/*
* We are making the functions below to return `int` instead of `bool`. They
* have been as such since their birth at 5f237d79033b2109afb768bc889611fa9630.
*/
RBIMPL_ATTR_CONST()
RBIMPL_ATTR_CONSTEXPR(CXX11)
RBIMPL_ATTR_ARTIFICIAL()
/**
* Our own locale-insensitive version of `isascii(3)`.
*
* @param[in] c Byte in question to query.
* @retval false `c` is out of range of ASCII character set.
* @retval true Yes it is.
* @warning `c` is an int. This means that when you pass a `char` value
* here, it experiences "integer promotion" as defined in ISO/IEC
* 9899:2018 section 6.3.1.1 paragraph 1.
*/
static inline int
rb_isascii(int c)
{
return '\0' <= c && c <= '\x7f';
}
RBIMPL_ATTR_CONST()
RBIMPL_ATTR_CONSTEXPR(CXX11)
RBIMPL_ATTR_ARTIFICIAL()
/**
* Our own locale-insensitive version of `isupper(3)`.
*
* @param[in] c Byte in question to query.
* @retval true `c` is listed in IEEE 1003.1 section 7.3.1.1 "upper".
* @retval false Anything else.
* @note Not only does this function works under the POSIX Locale, but
* also assumes its execution character set be what ruby calls an
* ASCII-compatible character set; which does not include for
* instance EBCDIC or UTF-16LE.
* @warning `c` is an int. This means that when you pass a `char` value
* here, it experiences "integer promotion" as defined in ISO/IEC
* 9899:2018 section 6.3.1.1 paragraph 1.
*/
static inline int
rb_isupper(int c)
{
return 'A' <= c && c <= 'Z';
}
RBIMPL_ATTR_CONST()
RBIMPL_ATTR_CONSTEXPR(CXX11)
RBIMPL_ATTR_ARTIFICIAL()
/**
* Our own locale-insensitive version of `islower(3)`.
*
* @param[in] c Byte in question to query.
* @retval true `c` is listed in IEEE 1003.1 section 7.3.1.1 "lower".
* @retval false Anything else.
* @note Not only does this function works under the POSIX Locale, but
* also assumes its execution character set be what ruby calls an
* ASCII-compatible character set; which does not include for
* instance EBCDIC or UTF-16LE.
* @warning `c` is an int. This means that when you pass a `char` value
* here, it experiences "integer promotion" as defined in ISO/IEC
* 9899:2018 section 6.3.1.1 paragraph 1.
*/
static inline int
rb_islower(int c)
{
return 'a' <= c && c <= 'z';
}
RBIMPL_ATTR_CONST()
RBIMPL_ATTR_CONSTEXPR(CXX11)
RBIMPL_ATTR_ARTIFICIAL()
/**
* Our own locale-insensitive version of `isalpha(3)`.
*
* @param[in] c Byte in question to query.
* @retval true `c` is listed in either IEEE 1003.1 section 7.3.1.1
* "upper" or "lower".
* @retval false Anything else.
* @note Not only does this function works under the POSIX Locale, but
* also assumes its execution character set be what ruby calls an
* ASCII-compatible character set; which does not include for
* instance EBCDIC or UTF-16LE.
* @warning `c` is an int. This means that when you pass a `char` value
* here, it experiences "integer promotion" as defined in ISO/IEC
* 9899:2018 section 6.3.1.1 paragraph 1.
*/
static inline int
rb_isalpha(int c)
{
return rb_isupper(c) || rb_islower(c);
}
RBIMPL_ATTR_CONST()
RBIMPL_ATTR_CONSTEXPR(CXX11)
RBIMPL_ATTR_ARTIFICIAL()
/**
* Our own locale-insensitive version of `isdigit(3)`.
*
* @param[in] c Byte in question to query.
* @retval true `c` is listed in IEEE 1003.1 section 7.3.1.1 "digit".
* @retval false Anything else.
* @note Not only does this function works under the POSIX Locale, but
* also assumes its execution character set be what ruby calls an
* ASCII-compatible character set; which does not include for
* instance EBCDIC or UTF-16LE.
* @warning `c` is an int. This means that when you pass a `char` value
* here, it experiences "integer promotion" as defined in ISO/IEC
* 9899:2018 section 6.3.1.1 paragraph 1.
*/
static inline int
rb_isdigit(int c)
{
return '0' <= c && c <= '9';
}
RBIMPL_ATTR_CONST()
RBIMPL_ATTR_CONSTEXPR(CXX11)
RBIMPL_ATTR_ARTIFICIAL()
/**
* Our own locale-insensitive version of `isalnum(3)`.
*
* @param[in] c Byte in question to query.
* @retval true `c` is listed in either IEEE 1003.1 section 7.3.1.1
* "upper", "lower", or "digit".
* @retval false Anything else.
* @note Not only does this function works under the POSIX Locale, but
* also assumes its execution character set be what ruby calls an
* ASCII-compatible character set; which does not include for
* instance EBCDIC or UTF-16LE.
* @warning `c` is an int. This means that when you pass a `char` value
* here, it experiences "integer promotion" as defined in ISO/IEC
* 9899:2018 section 6.3.1.1 paragraph 1.
*/
static inline int
rb_isalnum(int c)
{
return rb_isalpha(c) || rb_isdigit(c);
}
RBIMPL_ATTR_CONST()
RBIMPL_ATTR_CONSTEXPR(CXX11)
RBIMPL_ATTR_ARTIFICIAL()
/**
* Our own locale-insensitive version of `isxdigit(3)`.
*
* @param[in] c Byte in question to query.
* @retval true `c` is listed in IEEE 1003.1 section 7.3.1.1 "xdigit".
* @retval false Anything else.
* @note Not only does this function works under the POSIX Locale, but
* also assumes its execution character set be what ruby calls an
* ASCII-compatible character set; which does not include for
* instance EBCDIC or UTF-16LE.
* @warning `c` is an int. This means that when you pass a `char` value
* here, it experiences "integer promotion" as defined in ISO/IEC
* 9899:2018 section 6.3.1.1 paragraph 1.
*/
static inline int
rb_isxdigit(int c)
{
return rb_isdigit(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
}
RBIMPL_ATTR_CONST()
RBIMPL_ATTR_CONSTEXPR(CXX11)
RBIMPL_ATTR_ARTIFICIAL()
/**
* Our own locale-insensitive version of `isblank(3)`.
*
* @param[in] c Byte in question to query.
* @retval true `c` is listed in IEEE 1003.1 section 7.3.1.1 "blank".
* @retval false Anything else.
* @note Not only does this function works under the POSIX Locale, but
* also assumes its execution character set be what ruby calls an
* ASCII-compatible character set; which does not include for
* instance EBCDIC or UTF-16LE.
* @warning `c` is an int. This means that when you pass a `char` value
* here, it experiences "integer promotion" as defined in ISO/IEC
* 9899:2018 section 6.3.1.1 paragraph 1.
*/
static inline int
rb_isblank(int c)
{
return c == ' ' || c == '\t';
}
RBIMPL_ATTR_CONST()
RBIMPL_ATTR_CONSTEXPR(CXX11)
RBIMPL_ATTR_ARTIFICIAL()
/**
* Our own locale-insensitive version of `isspace(3)`.
*
* @param[in] c Byte in question to query.
* @retval true `c` is listed in IEEE 1003.1 section 7.3.1.1 "space".
* @retval false Anything else.
* @note Not only does this function works under the POSIX Locale, but
* also assumes its execution character set be what ruby calls an
* ASCII-compatible character set; which does not include for
* instance EBCDIC or UTF-16LE.
* @warning `c` is an int. This means that when you pass a `char` value
* here, it experiences "integer promotion" as defined in ISO/IEC
* 9899:2018 section 6.3.1.1 paragraph 1.
*/
static inline int
rb_isspace(int c)
{
return c == ' ' || ('\t' <= c && c <= '\r');
}
RBIMPL_ATTR_CONST()
RBIMPL_ATTR_CONSTEXPR(CXX11)
RBIMPL_ATTR_ARTIFICIAL()
/**
* Our own locale-insensitive version of `iscntrl(3)`.
*
* @param[in] c Byte in question to query.
* @retval true `c` is listed in IEEE 1003.1 section 7.3.1.1 "cntrl".
* @retval false Anything else.
* @note Not only does this function works under the POSIX Locale, but
* also assumes its execution character set be what ruby calls an
* ASCII-compatible character set; which does not include for
* instance EBCDIC or UTF-16LE.
* @warning `c` is an int. This means that when you pass a `char` value
* here, it experiences "integer promotion" as defined in ISO/IEC
* 9899:2018 section 6.3.1.1 paragraph 1.
*/
static inline int
rb_iscntrl(int c)
{
return ('\0' <= c && c < ' ') || c == '\x7f';
}
RBIMPL_ATTR_CONST()
RBIMPL_ATTR_CONSTEXPR(CXX11)
RBIMPL_ATTR_ARTIFICIAL()
/**
* Identical to rb_isgraph(), except it also returns true for `' '`.
*
* @param[in] c Byte in question to query.
* @retval true `c` is listed in either IEEE 1003.1 section 7.3.1.1
* "upper", "lower", "digit", "punct", or a `' '`.
* @retval false Anything else.
* @note Not only does this function works under the POSIX Locale, but
* also assumes its execution character set be what ruby calls an
* ASCII-compatible character set; which does not include for
* instance EBCDIC or UTF-16LE.
* @warning `c` is an int. This means that when you pass a `char` value
* here, it experiences "integer promotion" as defined in ISO/IEC
* 9899:2018 section 6.3.1.1 paragraph 1.
*/
static inline int
rb_isprint(int c)
{
return ' ' <= c && c <= '\x7e';
}
RBIMPL_ATTR_CONST()
RBIMPL_ATTR_CONSTEXPR(CXX11)
RBIMPL_ATTR_ARTIFICIAL()
/**
* Our own locale-insensitive version of `ispunct(3)`.
*
* @param[in] c Byte in question to query.
* @retval true `c` is listed in IEEE 1003.1 section 7.3.1.1 "punct".
* @retval false Anything else.
* @note Not only does this function works under the POSIX Locale, but
* also assumes its execution character set be what ruby calls an
* ASCII-compatible character set; which does not include for
* instance EBCDIC or UTF-16LE.
* @warning `c` is an int. This means that when you pass a `char` value
* here, it experiences "integer promotion" as defined in ISO/IEC
* 9899:2018 section 6.3.1.1 paragraph 1.
*/
static inline int
rb_ispunct(int c)
{
return !rb_isalnum(c);
}
RBIMPL_ATTR_CONST()
RBIMPL_ATTR_CONSTEXPR(CXX11)
RBIMPL_ATTR_ARTIFICIAL()
/**
* Our own locale-insensitive version of `isgraph(3)`.
*
* @param[in] c Byte in question to query.
* @retval true `c` is listed in either IEEE 1003.1 section 7.3.1.1
* "upper", "lower", "digit", or "punct".
* @retval false Anything else.
* @note Not only does this function works under the POSIX Locale, but
* also assumes its execution character set be what ruby calls an
* ASCII-compatible character set; which does not include for
* instance EBCDIC or UTF-16LE.
* @warning `c` is an int. This means that when you pass a `char` value
* here, it experiences "integer promotion" as defined in ISO/IEC
* 9899:2018 section 6.3.1.1 paragraph 1.
*/
static inline int
rb_isgraph(int c)
{
return '!' <= c && c <= '\x7e';
}
RBIMPL_ATTR_CONST()
RBIMPL_ATTR_CONSTEXPR(CXX11)
RBIMPL_ATTR_ARTIFICIAL()
/**
* Our own locale-insensitive version of `tolower(3)`.
*
* @param[in] c Byte in question to convert.
* @retval c The byte is not listed in in IEEE 1003.1 section
* 7.3.1.1 "upper".
* @retval otherwise Byte converted using the map defined in IEEE 1003.1
* section 7.3.1 "tolower".
* @note Not only does this function works under the POSIX Locale, but
* also assumes its execution character set be what ruby calls an
* ASCII-compatible character set; which does not include for
* instance EBCDIC or UTF-16LE.
* @warning `c` is an int. This means that when you pass a `char` value
* here, it experiences "integer promotion" as defined in ISO/IEC
* 9899:2018 section 6.3.1.1 paragraph 1.
*/
static inline int
rb_tolower(int c)
{
return rb_isupper(c) ? (c|0x20) : c;
}
RBIMPL_ATTR_CONST()
RBIMPL_ATTR_CONSTEXPR(CXX11)
RBIMPL_ATTR_ARTIFICIAL()
/**
* Our own locale-insensitive version of `toupper(3)`.
*
* @param[in] c Byte in question to convert.
* @retval c The byte is not listed in in IEEE 1003.1 section
* 7.3.1.1 "lower".
* @retval otherwise Byte converted using the map defined in IEEE 1003.1
* section 7.3.1 "toupper".
* @note Not only does this function works under the POSIX Locale, but
* also assumes its execution character set be what ruby calls an
* ASCII-compatible character set; which does not include for
* instance EBCDIC or UTF-16LE.
* @warning `c` is an int. This means that when you pass a `char` value
* here, it experiences "integer promotion" as defined in ISO/IEC
* 9899:2018 section 6.3.1.1 paragraph 1.
*/
static inline int
rb_toupper(int c)
{
return rb_islower(c) ? (c&0x5f) : c;
}
/** @} */
#endif /* RBIMPL_CTYPE_H */