Debian Codegraph @

src/c/character.d

raw
1 /* -*- Mode: C; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /* vim: set filetype=c tabstop=2 shiftwidth=2 expandtab: */
3
4 /*
5 * character.d - character routines
6 *
7 * Copyright (c) 1984 Taiichi Yuasa and Masami Hagiya
8 * Copyright (c) 1990 Giuseppe Attardi
9 * Copyright (c) 2015 Daniel Kochmański
10 *
11 * See file 'LICENSE' for the copyright details.
12 *
13 */
14
15 #include <stdio.h>
16 #include <ecl/ecl.h>
17
18 #include "char_ctype.d"
19
20 ecl_character
21 ecl_char_code(cl_object c)
22 {
23 if (ecl_unlikely(!ECL_CHARACTERP(c)))
24 FEwrong_type_only_arg(@[char-code], c, @[character]);
25 return ECL_CHAR_CODE(c);
26 }
27
28 ecl_base_char
29 ecl_base_char_code(cl_object c)
30 {
31 #ifdef ECL_UNICODE
32 if (ECL_CHARACTERP(c)) {
33 cl_fixnum code = ECL_CHAR_CODE(c);
34 if (code <= 255) {
35 return (int)code;
36 }
37 }
38 FEwrong_type_only_arg(@[char-code], c, @[base-char]);
39 #else
40 return ecl_char_code(c);
41 #endif
42 }
43
44 cl_object
45 cl_standard_char_p(cl_object c)
46 {
47 /* INV: ecl_char_code() checks the type */
48 cl_fixnum i = ecl_char_code(c);
49 @(return (ecl_standard_char_p(i)? ECL_T : ECL_NIL));
50 }
51
52 bool
53 ecl_standard_char_p(ecl_character code)
54 {
55 return ((' ' <= code) && (code < '\177')) || (code == '\n');
56 }
57
58 bool
59 ecl_base_char_p(ecl_character c)
60 {
61 return c <= 255;
62 }
63
64 cl_object
65 cl_graphic_char_p(cl_object c)
66 {
67 /* INV: ecl_char_code() checks the type */
68 @(return (ecl_graphic_char_p(ecl_char_code(c))? ECL_T : ECL_NIL));
69 }
70
71 cl_object
72 cl_alpha_char_p(cl_object c)
73 {
74 /* INV: ecl_char_code() checks the type */
75 @(return (ecl_alpha_char_p(ecl_char_code(c))? ECL_T : ECL_NIL));
76 }
77
78 cl_object
79 cl_upper_case_p(cl_object c)
80 {
81 /* INV: ecl_char_code() checks the type */
82 @(return (ecl_upper_case_p(ecl_char_code(c))? ECL_T : ECL_NIL));
83 }
84
85 cl_object
86 cl_lower_case_p(cl_object c)
87 {
88 /* INV: ecl_char_code() checks the type */
89 @(return (ecl_lower_case_p(ecl_char_code(c))? ECL_T : ECL_NIL));
90 }
91
92 cl_object
93 cl_both_case_p(cl_object c)
94 {
95 /* INV: ecl_char_code() checks the type */
96 @(return (ecl_both_case_p(ecl_char_code(c))? ECL_T : ECL_NIL));
97 }
98
99 int
100 ecl_string_case(cl_object s)
101 {
102 /* Returns 1 if string is all uppercase, -1 if all lowercase, and 0 if mixed case */
103 int upcase;
104 cl_index i;
105
106 switch (ecl_t_of(s)) {
107 #ifdef ECL_UNICODE
108 case t_string:
109 #endif
110 case t_base_string:
111 for (i = 0, upcase = 0; i < s->base_string.dim; i++) {
112 ecl_character c = ecl_char(s, i);
113
114 if (ecl_upper_case_p(c)) {
115 if (upcase < 0)
116 return 0;
117 upcase = +1;
118 } else if (ecl_lower_case_p(c)) {
119 if (upcase > 0)
120 return 0;
121 upcase = -1;
122 }
123 }
124 break;
125 default:
126 FEwrong_type_argument(@[string], s);
127 }
128 return upcase;
129 }
130
131 @(defun digit_char_p (c &optional (radix ecl_make_fixnum(10)))
132 @ {
133 cl_fixnum basis, value;
134 if (ecl_unlikely(!ECL_FIXNUMP(radix) ||
135 ecl_fixnum_lower(radix, ecl_make_fixnum(2)) ||
136 ecl_fixnum_greater(radix, ecl_make_fixnum(36)))) {
137 FEwrong_type_nth_arg(@[digit-char-p], 2, radix,
138 ecl_make_integer_type(ecl_make_fixnum(2),
139 ecl_make_fixnum(36)));
140 }
141 basis = ecl_fixnum(radix);
142 value = ecl_digitp(ecl_char_code(c), basis);
143 @(return ((value < 0)? ECL_NIL: ecl_make_fixnum(value)));
144 } @)
145
146 /*
147 Ecl_Digitp(i, r) returns the weight of code i
148 as a digit of radix r, which must be 1 < r <= 36.
149 If i is not a digit, -1 is returned.
150 */
151 int
152 ecl_digitp(ecl_character i, int r)
153 {
154 if (('0' <= i) && (i <= '9') && (i < '0' + r))
155 return i - '0';
156 if (('A' <= i) && (10 < r) && (i < 'A' + (r - 10)))
157 return i - 'A' + 10;
158 if (('a' <= i) && (10 < r) && (i < 'a' + (r - 10)))
159 return i - 'a' + 10;
160 #ifdef ECL_UNICODE
161 if (i > 255) {
162 int number = ucd_decimal_digit(i);
163 if (number < r)
164 return number;
165 }
166 #endif
167 return -1;
168 }
169
170 cl_object
171 cl_alphanumericp(cl_object c)
172 {
173 /* INV: ecl_char_code() checks type of `c' */
174 cl_fixnum i = ecl_char_code(c);
175 @(return (ecl_alphanumericp(i)? ECL_T : ECL_NIL));
176 }
177
178 @(defun char= (c &rest cs)
179 @ {
180 /* INV: ecl_char_eq() checks types of `c' and `cs' */
181 while (--narg) {
182 if (!ecl_char_eq(c, ecl_va_arg(cs))) {
183 @(return ECL_NIL);
184 }
185 }
186 @(return ECL_T);
187 } @)
188
189 bool
190 ecl_char_eq(cl_object x, cl_object y)
191 {
192 return ecl_char_code(x) == ecl_char_code(y);
193 }
194
195 @(defun char/= (&rest cs)
196 int i, j;
197 cl_object c;
198 @ {
199 /* INV: ecl_char_eq() checks types of its arguments */
200 if (narg == 0)
201 FEwrong_num_arguments(@[char/=]);
202 c = ecl_va_arg(cs);
203 for (i = 2; i<=narg; i++) {
204 ecl_va_list ds;
205 ecl_va_start(ds, narg, narg, 0);
206 c = ecl_va_arg(cs);
207 for (j = 1; j<i; j++)
208 if (ecl_char_eq(ecl_va_arg(ds), c)) {
209 ecl_va_end(ds);
210 @(return ECL_NIL);
211 }
212 ecl_va_end(ds);
213 }
214 @(return ECL_T);
215 } @)
216
217 static cl_object
218 Lchar_cmp(cl_env_ptr env, cl_narg narg, int s, int t, ecl_va_list args)
219 {
220 cl_object c, d;
221
222 if (narg == 0)
223 FEwrong_num_arguments_anonym();
224 c = ecl_va_arg(args);
225 for (; --narg; c = d) {
226 d = ecl_va_arg(args);
227 if (s*ecl_char_cmp(d, c) < t)
228 ecl_return1(env, ECL_NIL);
229 }
230 ecl_return1(env, ECL_T);
231 }
232
233 int
234 ecl_char_cmp(cl_object x, cl_object y)
235 {
236 /* ecl_char_code(x) returns an integer which is well in the range
237 * of positive fixnums. Therefore, this subtraction never
238 * oveflows. */
239 return ecl_char_code(x) - ecl_char_code(y);
240 }
241
242 @(defun char< (&rest args)
243 @
244 @(return Lchar_cmp(the_env, narg, 1, 1, args));
245 @)
246
247 @(defun char> (&rest args)
248 @
249 @(return Lchar_cmp(the_env, narg,-1, 1, args));
250 @)
251
252 @(defun char<= (&rest args)
253 @
254 @(return Lchar_cmp(the_env, narg, 1, 0, args));
255 @)
256
257 @(defun char>= (&rest args)
258 @
259 @(return Lchar_cmp(the_env, narg,-1, 0, args));
260 @)
261
262 @(defun char_equal (c &rest cs)
263 int i;
264 @ {
265 /* INV: ecl_char_equal() checks the type of its arguments */
266 for (narg--, i = 0; i < narg; i++) {
267 if (!ecl_char_equal(c, ecl_va_arg(cs))) {
268 @(return ECL_NIL);
269 }
270 }
271 @(return ECL_T);
272 } @)
273
274 #define char_equal_code(x) ecl_char_upcase(ecl_char_code(x))
275
276 bool
277 ecl_char_equal(cl_object x, cl_object y)
278 {
279 return char_equal_code(x) == char_equal_code(y);
280 }
281
282 @(defun char-not-equal (&rest cs)
283 int i, j;
284 cl_object c;
285 @ {
286 /* INV: ecl_char_equal() checks the type of its arguments */
287 if (narg == 0)
288 FEwrong_num_arguments(@[char-not-equal]);
289 c = ecl_va_arg(cs);
290 for (i = 2; i<=narg; i++) {
291 ecl_va_list ds;
292 ecl_va_start(ds, narg, narg, 0);
293 c = ecl_va_arg(cs);
294 for (j=1; j<i; j++)
295 if (ecl_char_equal(c, ecl_va_arg(ds))) {
296 ecl_va_end(ds);
297 @(return ECL_NIL);
298 }
299 }
300 @(return ECL_T);
301 } @)
302
303 static cl_object
304 Lchar_compare(cl_env_ptr env, cl_narg narg, int s, int t, ecl_va_list args)
305 {
306 cl_object c, d;
307
308 /* INV: ecl_char_compare() checks the types of its arguments */
309 if (narg == 0)
310 FEwrong_num_arguments_anonym();
311 c = ecl_va_arg(args);
312 for (; --narg; c = d) {
313 d = ecl_va_arg(args);
314 if (s*ecl_char_compare(d, c) < t)
315 ecl_return1(env, ECL_NIL);
316 }
317 ecl_return1(env, ECL_T);
318 }
319
320 int
321 ecl_char_compare(cl_object x, cl_object y)
322 {
323 cl_fixnum i = char_equal_code(x);
324 cl_fixnum j = char_equal_code(y);
325
326 if (i < j)
327 return(-1);
328 else if (i == j)
329 return(0);
330 else
331 return(1);
332 }
333
334 @(defun char-lessp (&rest args)
335 @
336 @(return Lchar_compare(the_env, narg, 1, 1, args));
337 @)
338
339 @(defun char-greaterp (&rest args)
340 @
341 @(return Lchar_compare(the_env, narg,-1, 1, args));
342 @)
343
344 @(defun char-not-greaterp (&rest args)
345 @
346 @(return Lchar_compare(the_env, narg, 1, 0, args));
347 @)
348
349 @(defun char-not-lessp (&rest args)
350 @
351 @(return Lchar_compare(the_env, narg,-1, 0, args));
352 @)
353
354
355 cl_object
356 cl_character(cl_object x)
357 {
358 switch (ecl_t_of(x)) {
359 case t_character:
360 break;
361 case t_symbol:
362 return cl_character(x->symbol.name);
363 #ifdef ECL_UNICODE
364 case t_string:
365 if (x->string.fillp == 1) {
366 x = ECL_CODE_CHAR(x->string.self[0]);
367 break;
368 }
369 goto ERROR;
370 #endif
371 case t_base_string:
372 if (x->base_string.fillp == 1) {
373 x = ECL_CODE_CHAR(x->base_string.self[0]);
374 break;
375 }
376 default: ERROR:
377 FEwrong_type_nth_arg
378 (@[character],
379 1, x,
380 ecl_read_from_cstring("(OR CHARACTER SYMBOL (ARRAY CHARACTER (1)) (ARRAY BASE-CHAR (1)))"));
381 }
382 @(return x);
383 }
384
385 cl_object
386 cl_char_code(cl_object c)
387 {
388 /* INV: ecl_char_code() checks the type of `c' */
389 @(return ecl_make_fixnum(ecl_char_code(c)));
390 }
391
392 cl_object
393 cl_code_char(cl_object c)
394 {
395 cl_fixnum fc;
396
397 switch (ecl_t_of(c)) {
398 case t_fixnum:
399 fc = ecl_fixnum(c);
400 if (fc < ECL_CHAR_CODE_LIMIT && fc >= 0) {
401 c = ECL_CODE_CHAR(fc);
402 break;
403 }
404 case t_bignum:
405 c = ECL_NIL;
406 break;
407 default:
408 FEwrong_type_only_arg(@[code-char], c, @[integer]);
409 }
410 @(return c);
411 }
412
413 cl_object
414 cl_char_upcase(cl_object c)
415 {
416 /* INV: ecl_char_code() checks the type of `c' */
417 cl_fixnum code = ecl_char_code(c);
418 @(return ECL_CODE_CHAR(ecl_char_upcase(code)));
419 }
420
421 cl_object
422 cl_char_downcase(cl_object c)
423 {
424 /* INV: ecl_char_code() checks the type of `c' */
425 cl_fixnum code = ecl_char_code(c);
426 @(return ECL_CODE_CHAR(ecl_char_downcase(code)));
427 }
428
429 @(defun digit_char (weight &optional (radix ecl_make_fixnum(10)))
430 @ {
431 cl_fixnum basis;
432 cl_object output = ECL_NIL;
433 if (ecl_unlikely(!ECL_FIXNUMP(radix) ||
434 ecl_fixnum_lower(radix, ecl_make_fixnum(2)) ||
435 ecl_fixnum_greater(radix, ecl_make_fixnum(36)))) {
436 FEwrong_type_nth_arg(@[digit-char], 2, radix,
437 ecl_make_integer_type(ecl_make_fixnum(2),
438 ecl_make_fixnum(36)));
439 }
440 basis = ecl_fixnum(radix);
441 switch (ecl_t_of(weight)) {
442 case t_fixnum: {
443 cl_fixnum value = ecl_fixnum(weight);
444 if (value >= 0) {
445 int dw = ecl_digit_char(value, basis);
446 if (dw >= 0) {
447 output = ECL_CODE_CHAR(dw);
448 }
449 }
450 break;
451 }
452 case t_bignum:
453 break;
454 default:
455 FEwrong_type_nth_arg(@[digit-char],1,weight,@[integer]);
456 }
457 @(return output);
458 } @)
459
460 short
461 ecl_digit_char(cl_fixnum w, cl_fixnum r)
462 {
463 if (r < 2 || r > 36 || w < 0 || w >= r)
464 return(-1);
465 if (w < 10)
466 return(w + '0');
467 else
468 return(w - 10 + 'A');
469 }
470
471 cl_object
472 cl_char_int(cl_object c)
473 {
474 const cl_env_ptr the_env = ecl_process_env();
475 /* INV: ecl_char_code() checks the type of `c' */
476 ecl_return1(the_env, ecl_make_fixnum(ecl_char_code(c)));
477 }
478
479 /* here we give every character an implicit name of the form 'u#' where # is a hexadecimal number,
480 corresponding to a unicode code point.
481 #\u14ea should work, for example
482 */
483
484 cl_object
485 cl_char_name(cl_object c)
486 {
487 ecl_character code = ecl_char_code(c);
488 cl_object output;
489 if (code <= 127) {
490 output = ecl_gethash_safe(ecl_make_fixnum(code), cl_core.char_names, ECL_NIL);
491 #ifdef ECL_UNICODE
492 } else if (!Null(output = _ecl_ucd_code_to_name(code))) {
493 (void)0;
494 #endif
495 } else {
496 ecl_base_char name[8];
497 ecl_base_char *start;
498 name[7] = 0;
499 name[6] = ecl_digit_char(code & 0xF, 16); code >>= 4;
500 name[5] = ecl_digit_char(code & 0xF, 16); code >>= 4;
501 name[4] = ecl_digit_char(code & 0xF, 16); code >>= 4;
502 name[3] = ecl_digit_char(code & 0xF, 16); code >>= 4;
503 if (code == 0) {
504 start = name + 2;
505 } else {
506 name[2] = ecl_digit_char(code & 0xF, 16); code >>= 4;
507 name[1] = ecl_digit_char(code & 0xF, 16);
508 start = name;
509 }
510 start[0] = 'U';
511 output = ecl_make_simple_base_string((const char*)start,-1);
512 }
513 @(return output);
514 }
515
516 cl_object
517 cl_name_char(cl_object name)
518 {
519 const cl_env_ptr the_env = ecl_process_env();
520 cl_object c;
521 cl_index l;
522 name = cl_string(name);
523 c = ecl_gethash_safe(name, cl_core.char_names, ECL_NIL);
524 if (c != ECL_NIL) {
525 ecl_return1(the_env, ECL_CODE_CHAR(ecl_fixnum(c)));
526 }
527 #ifdef ECL_UNICODE
528 c = _ecl_ucd_name_to_code(name);
529 if (c != ECL_NIL) {
530 ecl_return1(the_env, cl_code_char(c));
531 }
532 #endif
533 if (ecl_stringp(name) && (l = ecl_length(name))) {
534 c = cl_char(name, ecl_make_fixnum(0));
535 if (l == 1) {
536 (void)0;
537 } else if (c != ECL_CODE_CHAR('u') && c != ECL_CODE_CHAR('U')) {
538 c = ECL_NIL;
539 } else {
540 cl_index used_l;
541 cl_index end = name->base_string.fillp;
542 cl_index real_end = end;
543 c = ecl_parse_integer(name, 1, end, &real_end, 16);
544 used_l = real_end;
545 if (!ECL_FIXNUMP(c) || (used_l == (l - 1))) {
546 c = ECL_NIL;
547 } else {
548 c = ECL_CODE_CHAR(ecl_fixnum(c));
549 }
550 }
551 }
552 ecl_return1(the_env, c);
553 }