[b0ce08]: src / c / character.d Maximize Restore History

Download this file

character.d    495 lines (437 with data), 9.4 kB

  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
/*
character.d -- Character routines.
*/
/*
Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya.
Copyright (c) 1990, Giuseppe Attardi.
ECL is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
See file '../Copyright' for full details.
*/
#include "ecl.h"
#include <ctype.h>
/******************************* LOCALS *******************************/
static cl_object STreturn;
static cl_object STspace;
static cl_object STrubout;
static cl_object STpage;
static cl_object STtab;
static cl_object STbackspace;
static cl_object STlinefeed;
static cl_object STnewline;
static cl_object STnull;
/******************************* ------- ******************************/
cl_fixnum
char_code(cl_object c)
{
if (CHARACTERP(c))
return CHAR_CODE(c);
FEtype_error_character(c);
}
cl_object
cl_standard_char_p(cl_object c)
{
/* INV: char_code() checks the type */
cl_fixnum i = char_code(c);
if ((' ' <= i && i < '\177') || i == '\n')
return1(Ct);
return1(Cnil);
}
cl_object
cl_graphic_char_p(cl_object c)
{
/* INV: char_code() checks the type */
cl_fixnum i = char_code(c);
if (' ' <= i && i < '\177') /* ' ' < '\177' ??? Beppe*/
return1(Ct);
return1(Cnil);
}
cl_object
cl_alpha_char_p(cl_object c)
{
/* INV: char_code() checks the type */
cl_fixnum i = char_code(c);
if (isalpha(i))
return1(Ct);
else
return1(Cnil);
}
cl_object
cl_upper_case_p(cl_object c)
{
/* INV: char_code() checks the type */
if (isupper(char_code(c)))
return1(Ct);
return1(Cnil);
}
cl_object
cl_lower_case_p(cl_object c)
{
/* INV: char_code() checks the type */
if (islower(char_code(c)))
return1(Ct);
return1(Cnil);
}
cl_object
cl_both_case_p(cl_object c)
{
/* INV: char_code() checks the type */
cl_fixnum code = char_code(c);
return1((isupper(code) || islower(code)) ? Ct : Cnil);
}
#define basep(d) (d <= 36)
@(defun digit_char_p (c &optional (r MAKE_FIXNUM(10)))
cl_fixnum d;
@
/* INV: char_code() checks `c' and fixnnint() checks `r' */
if (type_of(r) == t_bignum)
@(return Cnil)
d = fixnnint(r);
if (!basep(d) || (d = digitp(char_code(c), d)) < 0)
@(return Cnil)
@(return MAKE_FIXNUM(d))
@)
/*
Digitp(i, r) returns the weight of code i
as a digit of radix r, which must be 1 < r <= 36.
If i is not a digit, -1 is returned.
*/
int
digitp(int i, int r)
{
if (('0' <= i) && (i <= '9') && (i < '0' + r))
return(i - '0');
if (('A' <= i) && (10 < r) && (i < 'A' + (r - 10)))
return(i - 'A' + 10);
if (('a' <= i) && (10 < r) && (i < 'a' + (r - 10)))
return(i - 'a' + 10);
return(-1);
}
cl_object
cl_alphanumericp(cl_object c)
{
/* INV: char_code() checks type of `c' */
cl_fixnum i = char_code(c);
return1(isalnum(i)? Ct : Cnil);
}
@(defun char= (c &rest cs)
@
/* INV: char_eq() checks types of `c' and `cs' */
while (--narg)
if (!char_eq(c, cl_va_arg(cs)))
@(return Cnil)
@(return Ct)
@)
bool
char_eq(cl_object x, cl_object y)
{
return char_code(x) == char_code(y);
}
@(defun char/= (&rest cs)
int i, j;
cl_object c;
@
/* INV: char_eq() checks types of its arguments */
if (narg == 0)
@(return Ct)
c = cl_va_arg(cs);
for (i = 2; i<=narg; i++) {
cl_va_list ds;
cl_va_start(ds, narg, narg, 0);
c = cl_va_arg(cs);
for (j = 1; j<i; j++)
if (char_eq(cl_va_arg(ds), c))
@(return Cnil)
}
@(return Ct)
@)
static cl_return
Lchar_cmp(int narg, int s, int t, cl_va_list args)
{
cl_object c, d;
if (narg == 0)
FEtoo_few_arguments(narg);
c = cl_va_arg(args);
for (; --narg; c = d) {
d = cl_va_arg(args);
if (s*char_cmp(d, c) < t)
return1(Cnil);
}
return1(Ct);
}
int
char_cmp(cl_object x, cl_object y)
{
cl_fixnum cx = char_code(x);
cl_fixnum cy = char_code(y);
if (cx < cy)
return(-1);
if (cx > cy)
return(1);
return(0);
}
@(defun char< (&rest args)
@
@(return Lchar_cmp(narg, 1, 1, args))
@)
@(defun char> (&rest args)
@
@(return Lchar_cmp(narg,-1, 1, args))
@)
@(defun char<= (&rest args)
@
@(return Lchar_cmp(narg, 1, 0, args))
@)
@(defun char>= (&rest args)
@
@(return Lchar_cmp(narg,-1, 0, args))
@)
@(defun char_equal (c &rest cs)
int i;
@
/* INV: char_equal() checks the type of its arguments */
for (narg--, i = 0; i < narg; i++) {
if (!char_equal(c, cl_va_arg(cs)))
@(return Cnil)
}
@(return Ct)
@)
bool
char_equal(cl_object x, cl_object y)
{
cl_fixnum i = char_code(x);
cl_fixnum j = char_code(y);
if (islower(i))
i = toupper(i);
if (islower(j))
j = toupper(j);
return(i == j);
}
@(defun char-not-equal (&rest cs)
int i, j;
cl_object c;
@
if (narg == 0)
@(return Ct)
/* INV: char_equal() checks the type of its arguments */
c = cl_va_arg(cs);
for (i = 2; i<=narg; i++) {
cl_va_list ds;
cl_va_start(ds, narg, narg, 0);
c = cl_va_arg(cs);
for (j=1; j<i; j++)
if (char_equal(c, cl_va_arg(ds)))
@(return Cnil)
}
@(return Ct)
@)
static cl_return
Lchar_compare(int narg, int s, int t, cl_va_list args)
{
cl_object c, d;
/* INV: char_compare() checks the types of its arguments */
if (narg == 0)
FEtoo_few_arguments(narg);
c = cl_va_arg(args);
for (; --narg; c = d) {
d = cl_va_arg(args);
if (s*char_compare(d, c) < t)
return1(Cnil);
}
return1(Ct);
}
int
char_compare(cl_object x, cl_object y)
{
cl_fixnum i = char_code(x);
cl_fixnum j = char_code(y);
if (islower(i))
i = toupper(i);
if (islower(j))
j = toupper(j);
if (i < j)
return(-1);
else if (i == j)
return(0);
else
return(1);
}
@(defun char-lessp (&rest args)
@
@(return Lchar_compare(narg, 1, 1, args))
@)
@(defun char-greaterp (&rest args)
@
@(return Lchar_compare(narg,-1, 1, args))
@)
@(defun char-not-greaterp (&rest args)
@
@(return Lchar_compare(narg, 1, 0, args))
@)
@(defun char-not-lessp (&rest args)
@
@(return Lchar_compare(narg,-1, 0, args))
@)
cl_object
cl_character(cl_object x)
{
return1(coerce_to_character(x));
}
cl_object
coerce_to_character(cl_object x)
{
switch (type_of(x)) {
case t_character:
return x;
case t_symbol:
x = x->symbol.name;
case t_string:
if (x->string.fillp == 1)
return(CODE_CHAR(x->string.self[0]));
default:
FEtype_error_character(x);
}
}
cl_object
cl_char_code(cl_object c)
{
/* INV: char_code() checks the type of `c' */
return1(MAKE_FIXNUM(char_code(c)));
}
cl_object
cl_code_char(cl_object c)
{
cl_fixnum fc;
/* INV: fixnnint() checks the type of `c' */
if (type_of(c) == t_bignum)
return1(Cnil);
if ((fc = fixnnint(c)) >= CHAR_CODE_LIMIT)
return1(Cnil);
return1(CODE_CHAR(fc));
}
cl_object
cl_char_upcase(cl_object c)
{
/* INV: char_code() checks the type of `c' */
cl_fixnum code = char_code(c);
return1(islower(char_code(c)) ?
CODE_CHAR(toupper(char_code(c))) :
c);
}
cl_object
cl_char_downcase(cl_object c)
{
/* INV: char_code() checks the type of `c' */
cl_fixnum code = char_code(c);
return1(isupper(char_code(c)) ?
CODE_CHAR(tolower(char_code(c))) :
c);
}
@(defun digit_char (w &optional (r MAKE_FIXNUM(10)))
int dw;
@
/* INV: fixnnint() checks the types of `w' and `r' */
if (type_of(w) == t_bignum || type_of(r) == t_bignum)
@(return Cnil)
dw = digit_weight(fixnnint(w), fixnnint(r));
if (dw < 0)
@(return Cnil)
@(return CODE_CHAR(dw))
@)
short
digit_weight(int w, int r)
{
if (r < 2 || r > 36 || w < 0 || w >= r)
return(-1);
if (w < 10)
return(w + '0');
else
return(w - 10 + 'A');
}
cl_object
cl_char_int(cl_object c)
{
/* INV: char_code() checks the type of `c' */
return1(MAKE_FIXNUM(char_code(c)));
}
cl_object
cl_int_char(cl_object x)
{
/* INV: fixnnint(x) checks the type of `c' */
if (type_of(x) == t_bignum)
return1(Cnil);
return1(CODE_CHAR(fixnnint(x)));
}
cl_object
cl_char_name(cl_object c)
{
/* INV: char_code() checks the type of `c' */
switch (char_code(c)) {
case '0':
return1(STnull);
case '\r':
return1(STreturn);
case ' ':
return1(STspace);
case '\177':
return1(STrubout);
case '\f':
return1(STpage);
case '\t':
return1(STtab);
case '\b':
return1(STbackspace);
case '\n':
return1(STnewline);
}
return1(Cnil);
}
cl_object
cl_name_char(cl_object s)
{
char c;
s = cl_string(s);
if (string_equal(s, STreturn))
c = '\r'; else
if (string_equal(s, STspace))
c = ' '; else
if (string_equal(s, STrubout))
c = '\177'; else
if (string_equal(s, STpage))
c = '\f'; else
if (string_equal(s, STtab))
c = '\t'; else
if (string_equal(s, STbackspace))
c = '\b'; else
if (string_equal(s, STlinefeed) || string_equal(s, STnewline))
c = '\n'; else
if (string_equal(s, STnull))
c = '\000'; else
return1(Cnil);
return1(CODE_CHAR(c));
}
void
init_character(void)
{
SYM_VAL(@'char-code-limit') = MAKE_FIXNUM(CHAR_CODE_LIMIT);
STreturn = make_simple_string("RETURN");
register_root(&STreturn);
STspace = make_simple_string("SPACE");
register_root(&STspace);
STrubout = make_simple_string("RUBOUT");
register_root(&STrubout);
STpage = make_simple_string("PAGE");
register_root(&STpage);
STtab = make_simple_string("TAB");
register_root(&STtab);
STbackspace = make_simple_string("BACKSPACE");
register_root(&STbackspace);
STlinefeed = make_simple_string("LINEFEED");
register_root(&STlinefeed);
STnull = make_simple_string("NULL");
register_root(&STnull);
STnewline = make_simple_string("NEWLINE");
register_root(&STnewline);
}