rbtree: add rb_search_exact()
[nasm.git] / nasmlib / nctype.c
blobf30f37e055f867e9193a6e4def62a6312ec1bd14
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2018 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
34 #include "nctype.h"
35 #include <ctype.h>
38 * Table of tolower() results. This avoids function calls
39 * on some platforms.
41 unsigned char nasm_tolower_tab[256];
43 static void tolower_tab_init(void)
45 int i;
47 for (i = 0; i < 256; i++)
48 nasm_tolower_tab[i] = tolower(i);
52 * Table of character type flags; some are simply <ctype.h>,
53 * some are NASM-specific.
56 uint16_t nasm_ctype_tab[256];
58 #if !defined(HAVE_ISCNTRL) && !defined(iscntrl)
59 # define iscntrl(x) ((x) < 32)
60 #endif
61 #if !defined(HAVE_ISASCII) && !defined(isascii)
62 # define isascii(x) ((x) < 128)
63 #endif
65 static void ctype_tab_init(void)
67 int i;
69 for (i = 0; i < 256; i++) {
70 enum nasm_ctype ct = 0;
72 if (iscntrl(i))
73 ct |= NCT_CTRL;
75 if (isascii(i))
76 ct |= NCT_ASCII;
78 if (isspace(i) && i != '\n')
79 ct |= NCT_SPACE;
81 if (isalpha(i)) {
82 ct |= (nasm_tolower(i) == i) ? NCT_LOWER : NCT_UPPER;
83 ct |= NCT_ID|NCT_IDSTART;
86 if (isdigit(i))
87 ct |= NCT_DIGIT|NCT_ID;
89 if (isxdigit(i))
90 ct |= NCT_HEX;
92 /* Non-ASCII character, but no ctype returned (e.g. Unicode) */
93 if (!ct && !ispunct(i))
94 ct |= NCT_ID|NCT_IDSTART;
96 nasm_ctype_tab[i] = ct;
99 nasm_ctype_tab['-'] |= NCT_MINUS;
100 nasm_ctype_tab['$'] |= NCT_DOLLAR|NCT_ID;
101 nasm_ctype_tab['_'] |= NCT_UNDER|NCT_ID|NCT_IDSTART;
102 nasm_ctype_tab['.'] |= NCT_ID|NCT_IDSTART;
103 nasm_ctype_tab['@'] |= NCT_ID|NCT_IDSTART;
104 nasm_ctype_tab['?'] |= NCT_ID|NCT_IDSTART;
105 nasm_ctype_tab['#'] |= NCT_ID;
106 nasm_ctype_tab['~'] |= NCT_ID;
107 nasm_ctype_tab['\''] |= NCT_QUOTE;
108 nasm_ctype_tab['\"'] |= NCT_QUOTE;
109 nasm_ctype_tab['`'] |= NCT_QUOTE;
112 void nasm_ctype_init(void)
114 tolower_tab_init();
115 ctype_tab_init();