Debian Codegraph @

terps/tads/tads3/vmuni.h

raw symbols SCIP
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 vmuni.h - T3 VM Unicode-specific functions
12 Function
13
14 Notes
15
16 Modified
17 08/27/99 MJRoberts - Creation
18 */
19
20 #ifndef VMUNI_H
21 #define VMUNI_H
22
23 #include <stdlib.h>
24 #include "wchar.h"
25 #include "t3std.h"
26
27
28 /* ------------------------------------------------------------------------ */
29 /*
30 * Upper/lower case classification and conversion functions for Unicode
31 * character values. These routines are implemented in a
32 * machine-generated source file (the source file is mechanically
33 * derived from the Unicode character database).
34 */
35 int t3_is_lower(wchar_t ch);
36 int t3_is_upper(wchar_t ch);
37 int t3_is_title(wchar_t ch);
38 const wchar_t *t3_to_lower(wchar_t ch);
39 const wchar_t *t3_to_upper(wchar_t ch);
40 const wchar_t *t3_to_title(wchar_t ch);
41 const wchar_t *t3_to_fold(wchar_t ch);
42 wchar_t t3_simple_case_fold(wchar_t ch);
43
44 /*
45 * Character types. Types are mutually exclusive, so a character has
46 * exactly one type.
47 */
48 #define T3_CTYPE_UNDEF 0 /* character isn't defined */
49 #define T3_CTYPE_ALPHA 1 /* alphabetic, with no case information */
50 #define T3_CTYPE_UPPER 2 /* upper-case alphabetic */
51 #define T3_CTYPE_TITLE 3 /* title-case alphabetic */
52 #define T3_CTYPE_LOWER 4 /* lower-case alphabetic */
53 #define T3_CTYPE_DIGIT 5 /* digit */
54 #define T3_CTYPE_SPACE 6 /* horizontal whitespace */
55 #define T3_CTYPE_VSPAC 7 /* vertical whitespace */
56 #define T3_CTYPE_PUNCT 8 /* punctuation */
57 #define T3_CTYPE_OTHER 9 /* character doesn't fit any other category */
58
59 /* macro name strings, in order of appearance */
60 #define T3_CTYPE_NAMES \
61 "T3_CTYPE_UNDEF", "T3_CTYPE_ALPHA", "T3_CTYPE_UPPER", "T3_CTYPE_TITLE", \
62 "T3_CTYPE_LOWER", "T3_CTYPE_DIGIT", "T3_CTYPE_SPACE", "T3_CTYPE_VSPAC", \
63 "T3_CTYPE_PUNCT", "T3_CTYPE_OTHER"
64
65 /* get the character type */
66 int t3_get_chartype(wchar_t ch);
67
68 /*
69 * character classification functions
70 */
71
72 /* alphabetic? */
73 inline int t3_is_alpha(wchar_t ch)
74 {
75 int ctype = t3_get_chartype(ch);
76 return (ctype >= T3_CTYPE_ALPHA && ctype <= T3_CTYPE_LOWER);
77 }
78
79 /* uppercase? */
80 inline int t3_is_upper(wchar_t ch)
81 {
82 int ctype = t3_get_chartype(ch);
83 return (ctype == T3_CTYPE_UPPER || ctype == T3_CTYPE_TITLE);
84 }
85
86 /* lowercase? */
87 inline int t3_is_lower(wchar_t ch)
88 {
89 return (t3_get_chartype(ch) == T3_CTYPE_LOWER);
90 }
91
92 /* digit? */
93 inline int t3_is_digit(wchar_t ch)
94 {
95 return (t3_get_chartype(ch) == T3_CTYPE_DIGIT);
96 }
97
98 /* horizontal whitespace? */
99 inline int t3_is_space(wchar_t ch)
100 {
101 return (t3_get_chartype(ch) == T3_CTYPE_SPACE);
102 }
103
104 /* vertical whitespace? */
105 inline int t3_is_vspace(wchar_t ch)
106 {
107 return (t3_get_chartype(ch) == T3_CTYPE_VSPAC);
108 }
109
110 /* any whitespace, horizontal or vertical? */
111 inline int t3_is_whitespace(wchar_t ch)
112 {
113 int t = t3_get_chartype(ch);
114 return (t == T3_CTYPE_SPACE || t == T3_CTYPE_VSPAC);
115 }
116
117 /* punctuation? */
118 inline int t3_is_punct(wchar_t ch)
119 {
120 return (t3_get_chartype(ch) == T3_CTYPE_PUNCT);
121 }
122
123 /* is it a defined unicode character? */
124 inline int t3_is_unichar(wchar_t ch)
125 {
126 return (t3_get_chartype(ch) != T3_CTYPE_UNDEF);
127 }
128
129 /* ------------------------------------------------------------------------ */
130 /*
131 * Case folding wchar_t string reader
132 */
133 class CVmCaseFoldStr
134 {
135 public:
136 CVmCaseFoldStr(const wchar_t *s) { init(s, wcslen(s)); }
137 CVmCaseFoldStr(const wchar_t *s, size_t len) { init(s, len); }
138
139 /* get the current pointer */
140 const wchar_t *getptr() const { return p; }
141
142 /* is another character available? */
143 int more() const { return rem != 0 || *fp != 0 || fp == ie; }
144
145 /* are we at a character boundary? */
146 int at_boundary() const { return fp != fpbase && *fp == 0; }
147
148 /* get the next character */
149 wchar_t getch()
150 {
151 /* if the expansion is exhausted, expand the next source character */
152 if (fp != ie && *fp == 0)
153 {
154 /* if there's nothing left in the source string, we're done */
155 if (rem == 0)
156 return 0;
157
158 /* get the next source character */
159 wchar_t ch = *p++;
160 --rem;
161
162 /* get its case-folded expansion */
163 if ((fp = t3_to_fold(ch)) == 0)
164 {
165 ie[0] = ch;
166 fpbase = fp = ie;
167 }
168 }
169
170 /* return the next expansion character */
171 return *fp++;
172 }
173
174 /*
175 * Case-insensitive wide-string comparison routine. Returns true if
176 * the strings are equal, false if not.
177 */
178 static int wstreq(const wchar_t *a, const wchar_t *b)
179 {
180 /* loop over characters until we find a mismatch */
181 for (CVmCaseFoldStr fa(a), fb(b) ; ; )
182 {
183 /* get the next character of each folded string */
184 wchar_t ca = fa.getch();
185 wchar_t cb = fb.getch();
186
187 /* if they differ, return not equal */
188 if (ca != cb)
189 return FALSE;
190
191 /* if they both ended here, we have a match */
192 if (ca == 0)
193 return TRUE;
194 }
195 }
196
197 private:
198 void init(const wchar_t *s, size_t len)
199 {
200 /* set up at the start of the string */
201 this->p = s;
202 this->rem = len;
203
204 /* null-terminate our special one-byte identity conversion buffer */
205 ie[1] = 0;
206
207 /* start without anything loaded */
208 fpbase = ie;
209 fp = &ie[1];
210 }
211
212 /* current position in case folding expansion of current source char */
213 const wchar_t *fp;
214
215 /* start of current folding expansion */
216 const wchar_t *fpbase;
217
218 /* buffer for identity expansions */
219 wchar_t ie[2];
220
221 /* original source string and character length */
222 const wchar_t *p;
223 size_t rem;
224 };
225
226 #endif /* VMUNI_H */
227