* list.c: Moved append docs to append! Thanks Dirk Hermann. Also,
[bpt/guile.git] / libguile / chars.c
... / ...
CommitLineData
1/* Copyright (C) 1995,1996,1998 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45\f
46
47#include <stdio.h>
48#include <ctype.h>
49#include "_scm.h"
50#include "scm_validate.h"
51
52#include "chars.h"
53\f
54
55SCM_DEFINE (scm_char_p, "char?", 1, 0, 0,
56 (SCM x),
57 "Return #t iff X is a character, else #f.")
58#define FUNC_NAME s_scm_char_p
59{
60 return SCM_BOOL(SCM_CHARP(x));
61}
62#undef FUNC_NAME
63
64SCM_DEFINE1 (scm_char_eq_p, "char=?", scm_tc7_rpsubr,
65 (SCM x, SCM y),
66 "Return #t iff X is the same character as Y, else #f.")
67#define FUNC_NAME s_scm_char_eq_p
68{
69 SCM_VALIDATE_CHAR (1,x);
70 SCM_VALIDATE_CHAR (2,y);
71 return SCM_BOOL(SCM_CHAR(x) == SCM_CHAR(y));
72}
73#undef FUNC_NAME
74
75
76SCM_DEFINE1 (scm_char_less_p, "char<?", scm_tc7_rpsubr,
77 (SCM x, SCM y),
78 "Return #t iff X is less than Y in the Ascii sequence, else #f.")
79#define FUNC_NAME s_scm_char_less_p
80{
81 SCM_VALIDATE_CHAR (1,x);
82 SCM_VALIDATE_CHAR (2,y);
83 return SCM_BOOL(SCM_CHAR(x) < SCM_CHAR(y));
84}
85#undef FUNC_NAME
86
87SCM_DEFINE1 (scm_char_leq_p, "char<=?", scm_tc7_rpsubr,
88 (SCM x, SCM y),
89 "Return #t iff X is less than or equal to Y in the Ascii sequence, else #f.")
90#define FUNC_NAME s_scm_char_leq_p
91{
92 SCM_VALIDATE_CHAR (1,x);
93 SCM_VALIDATE_CHAR (2,y);
94 return SCM_BOOL(SCM_CHAR(x) <= SCM_CHAR(y));
95}
96#undef FUNC_NAME
97
98SCM_DEFINE1 (scm_char_gr_p, "char>?", scm_tc7_rpsubr,
99 (SCM x, SCM y),
100 "Return #t iff X is greater than Y in the Ascii sequence, else #f.")
101#define FUNC_NAME s_scm_char_gr_p
102{
103 SCM_VALIDATE_CHAR (1,x);
104 SCM_VALIDATE_CHAR (2,y);
105 return SCM_BOOL(SCM_CHAR(x) > SCM_CHAR(y));
106}
107#undef FUNC_NAME
108
109SCM_DEFINE1 (scm_char_geq_p, "char>=?", scm_tc7_rpsubr,
110 (SCM x, SCM y),
111 "Return #t iff X is greater than or equal to Y in the Ascii sequence, else #f.")
112#define FUNC_NAME s_scm_char_geq_p
113{
114 SCM_VALIDATE_CHAR (1,x);
115 SCM_VALIDATE_CHAR (2,y);
116 return SCM_BOOL(SCM_CHAR(x) >= SCM_CHAR(y));
117}
118#undef FUNC_NAME
119
120SCM_DEFINE1 (scm_char_ci_eq_p, "char-ci=?", scm_tc7_rpsubr,
121 (SCM x, SCM y),
122 "Return #t iff X is the same character as Y ignoring case, else #f.")
123#define FUNC_NAME s_scm_char_ci_eq_p
124{
125 SCM_VALIDATE_CHAR (1,x);
126 SCM_VALIDATE_CHAR (2,y);
127 return SCM_BOOL(scm_upcase(SCM_CHAR(x))==scm_upcase(SCM_CHAR(y)));
128}
129#undef FUNC_NAME
130
131SCM_DEFINE1 (scm_char_ci_less_p, "char-ci<?", scm_tc7_rpsubr,
132 (SCM x, SCM y),
133 "Return #t iff X is less than Y in the Ascii sequence ignoring case, else #f.")
134#define FUNC_NAME s_scm_char_ci_less_p
135{
136 SCM_VALIDATE_CHAR (1,x);
137 SCM_VALIDATE_CHAR (2,y);
138 return SCM_BOOL((scm_upcase(SCM_CHAR(x))) < scm_upcase(SCM_CHAR(y)));
139}
140#undef FUNC_NAME
141
142SCM_DEFINE1 (scm_char_ci_leq_p, "char-ci<=?", scm_tc7_rpsubr,
143 (SCM x, SCM y),
144 "Return #t iff X is less than or equal to Y in the Ascii sequence ignoring case, else #f.")
145#define FUNC_NAME s_scm_char_ci_leq_p
146{
147 SCM_VALIDATE_CHAR (1,x);
148 SCM_VALIDATE_CHAR (2,y);
149 return SCM_BOOL(scm_upcase(SCM_CHAR(x)) <= scm_upcase(SCM_CHAR(y)));
150}
151#undef FUNC_NAME
152
153SCM_DEFINE1 (scm_char_ci_gr_p, "char-ci>?", scm_tc7_rpsubr,
154 (SCM x, SCM y),
155 "Return #t iff X is greater than Y in the Ascii sequence ignoring case, else #f.")
156#define FUNC_NAME s_scm_char_ci_gr_p
157{
158 SCM_VALIDATE_CHAR (1,x);
159 SCM_VALIDATE_CHAR (2,y);
160 return SCM_BOOL(scm_upcase(SCM_CHAR(x)) > scm_upcase(SCM_CHAR(y)));
161}
162#undef FUNC_NAME
163
164SCM_DEFINE1 (scm_char_ci_geq_p, "char-ci>=?", scm_tc7_rpsubr,
165 (SCM x, SCM y),
166 "Return #t iff X is greater than or equal to Y in the Ascii sequence ignoring case, else #f.")
167#define FUNC_NAME s_scm_char_ci_geq_p
168{
169 SCM_VALIDATE_CHAR (1,x);
170 SCM_VALIDATE_CHAR (2,y);
171 return SCM_BOOL(scm_upcase(SCM_CHAR(x)) >= scm_upcase(SCM_CHAR(y)));
172}
173#undef FUNC_NAME
174
175
176SCM_DEFINE (scm_char_alphabetic_p, "char-alphabetic?", 1, 0, 0,
177 (SCM chr),
178 "Return #t iff CHR is alphabetic, else #f.\n"
179 "Alphabetic means the same thing as the isalpha C library function.")
180#define FUNC_NAME s_scm_char_alphabetic_p
181{
182 SCM_VALIDATE_CHAR (1,chr);
183 return SCM_BOOL(isascii(SCM_CHAR(chr)) && isalpha(SCM_CHAR(chr)));
184}
185#undef FUNC_NAME
186
187SCM_DEFINE (scm_char_numeric_p, "char-numeric?", 1, 0, 0,
188 (SCM chr),
189 "Return #t iff CHR is numeric, else #f.\n"
190 "Numeric means the same thing as the isdigit C library function.")
191#define FUNC_NAME s_scm_char_numeric_p
192{
193 SCM_VALIDATE_CHAR (1,chr);
194 return SCM_BOOL(isascii(SCM_CHAR(chr)) && isdigit(SCM_CHAR(chr)));
195}
196#undef FUNC_NAME
197
198SCM_DEFINE (scm_char_whitespace_p, "char-whitespace?", 1, 0, 0,
199 (SCM chr),
200 "Return #t iff CHR is whitespace, else #f.\n"
201 "Whitespace means the same thing as the isspace C library function.")
202#define FUNC_NAME s_scm_char_whitespace_p
203{
204 SCM_VALIDATE_CHAR (1,chr);
205 return SCM_BOOL(isascii(SCM_CHAR(chr)) && isspace(SCM_CHAR(chr)));
206}
207#undef FUNC_NAME
208
209
210
211SCM_DEFINE (scm_char_upper_case_p, "char-upper-case?", 1, 0, 0,
212 (SCM chr),
213 "Return #t iff CHR is uppercase, else #f.\n"
214 "Uppercase means the same thing as the isupper C library function.")
215#define FUNC_NAME s_scm_char_upper_case_p
216{
217 SCM_VALIDATE_CHAR (1,chr);
218 return SCM_BOOL(isascii(SCM_CHAR(chr)) && isupper(SCM_CHAR(chr)));
219}
220#undef FUNC_NAME
221
222
223SCM_DEFINE (scm_char_lower_case_p, "char-lower-case?", 1, 0, 0,
224 (SCM chr),
225 "Return #t iff CHR is lowercase, else #f.\n"
226 "Lowercase means the same thing as the islower C library function.")
227#define FUNC_NAME s_scm_char_lower_case_p
228{
229 SCM_VALIDATE_CHAR (1,chr);
230 return SCM_BOOL(isascii(SCM_CHAR(chr)) && islower(SCM_CHAR(chr)));
231}
232#undef FUNC_NAME
233
234
235
236SCM_DEFINE (scm_char_is_both_p, "char-is-both?", 1, 0, 0,
237 (SCM chr),
238 "Return #t iff CHR is either uppercase or lowercase, else #f.\n"
239 "Uppercase and lowercase are as defined by the isupper and islower\n"
240 "C library functions.")
241#define FUNC_NAME s_scm_char_is_both_p
242{
243 SCM_VALIDATE_CHAR (1,chr);
244 return SCM_BOOL(isascii(SCM_CHAR(chr)) && (isupper(SCM_CHAR(chr)) || islower(SCM_CHAR(chr))));
245}
246#undef FUNC_NAME
247
248
249
250
251SCM_DEFINE (scm_char_to_integer, "char->integer", 1, 0, 0,
252 (SCM chr),
253 "Return the number corresponding to ordinal position of CHR in the Ascii sequence.")
254#define FUNC_NAME s_scm_char_to_integer
255{
256 SCM_VALIDATE_CHAR (1,chr);
257 return scm_ulong2num((unsigned long)SCM_CHAR(chr));
258}
259#undef FUNC_NAME
260
261
262
263SCM_DEFINE (scm_integer_to_char, "integer->char", 1, 0, 0,
264 (SCM n),
265 "Return the character at position N in the Ascii sequence.")
266#define FUNC_NAME s_scm_integer_to_char
267{
268 SCM_VALIDATE_INUM_RANGE (1, n, 0, 256);
269 return SCM_MAKE_CHAR (SCM_INUM (n));
270}
271#undef FUNC_NAME
272
273
274SCM_DEFINE (scm_char_upcase, "char-upcase", 1, 0, 0,
275 (SCM chr),
276 "Return the uppercase character version of CHR.")
277#define FUNC_NAME s_scm_char_upcase
278{
279 SCM_VALIDATE_CHAR (1,chr);
280 return SCM_MAKE_CHAR(scm_upcase(SCM_CHAR(chr)));
281}
282#undef FUNC_NAME
283
284
285SCM_DEFINE (scm_char_downcase, "char-downcase", 1, 0, 0,
286 (SCM chr),
287 "Return the lowercase character version of CHR.")
288#define FUNC_NAME s_scm_char_downcase
289{
290 SCM_VALIDATE_CHAR (1,chr);
291 return SCM_MAKE_CHAR(scm_downcase(SCM_CHAR(chr)));
292}
293#undef FUNC_NAME
294
295\f
296
297
298
299static unsigned char scm_upcase_table[SCM_CHAR_CODE_LIMIT];
300static unsigned char scm_downcase_table[SCM_CHAR_CODE_LIMIT];
301static const unsigned char scm_lowers[] = "abcdefghijklmnopqrstuvwxyz";
302static const unsigned char scm_uppers[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
303
304
305void
306scm_tables_prehistory ()
307{
308 int i;
309 for (i = 0; i < SCM_CHAR_CODE_LIMIT; i++)
310 scm_upcase_table[i] = scm_downcase_table[i] = i;
311 for (i = 0; i < (int) (sizeof scm_lowers / sizeof (scm_lowers[0])); i++)
312 {
313 scm_upcase_table[scm_lowers[i]] = scm_uppers[i];
314 scm_downcase_table[scm_uppers[i]] = scm_lowers[i];
315 }
316}
317
318
319int
320scm_upcase (unsigned int c)
321{
322 if (c < sizeof (scm_upcase_table))
323 return scm_upcase_table[c];
324 else
325 return c;
326}
327
328
329int
330scm_downcase (unsigned int c)
331{
332 if (c < sizeof (scm_downcase_table))
333 return scm_downcase_table[c];
334 else
335 return c;
336}
337
338
339#ifdef _DCC
340# define ASCII
341#else
342# if (('\n'=='\025') && (' '=='\100') && ('a'=='\201') && ('A'=='\301'))
343# define EBCDIC
344# endif /* (('\n'=='\025') && (' '=='\100') && ('a'=='\201') && ('A'=='\301')) */
345# if (('\n'=='\012') && (' '=='\040') && ('a'=='\141') && ('A'=='\101'))
346# define ASCII
347# endif /* (('\n'=='\012') && (' '=='\040') && ('a'=='\141') && ('A'=='\101')) */
348#endif /* def _DCC */
349
350
351#ifdef EBCDIC
352char *const scm_charnames[] =
353{
354 "nul","soh","stx","etx", "pf", "ht", "lc","del",
355 0 , 0 ,"smm", "vt", "ff", "cr", "so", "si",
356 "dle","dc1","dc2","dc3","res", "nl", "bs", "il",
357 "can", "em", "cc", 0 ,"ifs","igs","irs","ius",
358 "ds","sos", "fs", 0 ,"byp", "lf","eob","pre",
359 0 , 0 , "sm", 0 , 0 ,"enq","ack","bel",
360 0 , 0 ,"syn", 0 , "pn", "rs", "uc","eot",
361 0 , 0 , 0 , 0 ,"dc4","nak", 0 ,"sub",
362 "space", scm_s_newline, "tab", "backspace", "return", "page", "null"};
363
364const char scm_charnums[] =
365"\000\001\002\003\004\005\006\007\
366\010\011\012\013\014\015\016\017\
367\020\021\022\023\024\025\026\027\
368\030\031\032\033\034\035\036\037\
369\040\041\042\043\044\045\046\047\
370\050\051\052\053\054\055\056\057\
371\060\061\062\063\064\065\066\067\
372\070\071\072\073\074\075\076\077\
373 \n\t\b\r\f\0";
374#endif /* def EBCDIC */
375#ifdef ASCII
376char *const scm_charnames[] =
377{
378 "nul","soh","stx","etx","eot","enq","ack","bel",
379 "bs", "ht", "newline", "vt", "np", "cr", "so", "si",
380 "dle","dc1","dc2","dc3","dc4","nak","syn","etb",
381 "can", "em","sub","esc", "fs", "gs", "rs", "us",
382 "space", "nl", "tab", "backspace", "return", "page", "null", "del"};
383const char scm_charnums[] =
384"\000\001\002\003\004\005\006\007\
385\010\011\012\013\014\015\016\017\
386\020\021\022\023\024\025\026\027\
387\030\031\032\033\034\035\036\037\
388 \n\t\b\r\f\0\177";
389#endif /* def ASCII */
390
391int scm_n_charnames = sizeof (scm_charnames) / sizeof (char *);
392
393
394\f
395
396
397void
398scm_init_chars ()
399{
400#include "chars.x"
401}
402