* Eliminate hard-coded value of scm_tc7_smob.
[bpt/guile.git] / libguile / chars.c
CommitLineData
f2c9fcb0 1/* Copyright (C) 1995,1996,1998, 2000 Free Software Foundation, Inc.
0f2d19dd
JB
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
82892bed
JB
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
0f2d19dd
JB
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.
82892bed 40 * If you do not wish that, delete this exception notice. */
1bbd0b84
GB
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
0f2d19dd
JB
45\f
46
47#include <stdio.h>
48#include <ctype.h>
a0599745
MD
49#include "libguile/_scm.h"
50#include "libguile/validate.h"
0f2d19dd 51
a0599745 52#include "libguile/chars.h"
0f2d19dd
JB
53\f
54
a1ec6916 55SCM_DEFINE (scm_char_p, "char?", 1, 0, 0,
1bbd0b84 56 (SCM x),
cdbc7418 57 "Return @code{#t} iff @var{x} is a character, else @code{#f}.")
1bbd0b84 58#define FUNC_NAME s_scm_char_p
0f2d19dd 59{
7866a09b 60 return SCM_BOOL(SCM_CHARP(x));
0f2d19dd 61}
1bbd0b84 62#undef FUNC_NAME
0f2d19dd 63
c3ee7520 64SCM_DEFINE1 (scm_char_eq_p, "char=?", scm_tc7_rpsubr,
1bbd0b84 65 (SCM x, SCM y),
cdbc7418 66 "Return @code{#t} iff @var{x} is the same character as @var{y}, else @code{#f}.")
1bbd0b84 67#define FUNC_NAME s_scm_char_eq_p
0f2d19dd 68{
7866a09b
GB
69 SCM_VALIDATE_CHAR (1,x);
70 SCM_VALIDATE_CHAR (2,y);
71 return SCM_BOOL(SCM_CHAR(x) == SCM_CHAR(y));
0f2d19dd 72}
1bbd0b84 73#undef FUNC_NAME
0f2d19dd
JB
74
75
c3ee7520 76SCM_DEFINE1 (scm_char_less_p, "char<?", scm_tc7_rpsubr,
1bbd0b84 77 (SCM x, SCM y),
cdbc7418
NJ
78 "Return @code{#t} iff @var{x} is less than @var{y} in the ASCII sequence,\n"
79 "else @code{#f}.")
1bbd0b84 80#define FUNC_NAME s_scm_char_less_p
0f2d19dd 81{
7866a09b
GB
82 SCM_VALIDATE_CHAR (1,x);
83 SCM_VALIDATE_CHAR (2,y);
84 return SCM_BOOL(SCM_CHAR(x) < SCM_CHAR(y));
0f2d19dd 85}
1bbd0b84 86#undef FUNC_NAME
0f2d19dd 87
c3ee7520 88SCM_DEFINE1 (scm_char_leq_p, "char<=?", scm_tc7_rpsubr,
1bbd0b84 89 (SCM x, SCM y),
cdbc7418
NJ
90 "Return @code{#t} iff @var{x} is less than or equal to @var{y} in the\n"
91 "ASCII sequence, else @code{#f}.")
1bbd0b84 92#define FUNC_NAME s_scm_char_leq_p
0f2d19dd 93{
7866a09b
GB
94 SCM_VALIDATE_CHAR (1,x);
95 SCM_VALIDATE_CHAR (2,y);
96 return SCM_BOOL(SCM_CHAR(x) <= SCM_CHAR(y));
0f2d19dd 97}
1bbd0b84 98#undef FUNC_NAME
0f2d19dd 99
c3ee7520 100SCM_DEFINE1 (scm_char_gr_p, "char>?", scm_tc7_rpsubr,
1bbd0b84 101 (SCM x, SCM y),
cdbc7418
NJ
102 "Return @code{#t} iff @var{x} is greater than @var{y} in the ASCII\n"
103 "sequence, else @code{#f}.")
1bbd0b84 104#define FUNC_NAME s_scm_char_gr_p
0f2d19dd 105{
7866a09b
GB
106 SCM_VALIDATE_CHAR (1,x);
107 SCM_VALIDATE_CHAR (2,y);
108 return SCM_BOOL(SCM_CHAR(x) > SCM_CHAR(y));
0f2d19dd 109}
1bbd0b84 110#undef FUNC_NAME
0f2d19dd 111
c3ee7520 112SCM_DEFINE1 (scm_char_geq_p, "char>=?", scm_tc7_rpsubr,
1bbd0b84 113 (SCM x, SCM y),
cdbc7418
NJ
114 "Return @code{#t} iff @var{x} is greater than or equal to @var{y} in the\n"
115 "ASCII sequence, else @code{#f}.")
1bbd0b84 116#define FUNC_NAME s_scm_char_geq_p
0f2d19dd 117{
7866a09b
GB
118 SCM_VALIDATE_CHAR (1,x);
119 SCM_VALIDATE_CHAR (2,y);
120 return SCM_BOOL(SCM_CHAR(x) >= SCM_CHAR(y));
0f2d19dd 121}
1bbd0b84 122#undef FUNC_NAME
0f2d19dd 123
c3ee7520 124SCM_DEFINE1 (scm_char_ci_eq_p, "char-ci=?", scm_tc7_rpsubr,
1bbd0b84 125 (SCM x, SCM y),
cdbc7418
NJ
126 "Return @code{#t} iff @var{x} is the same character as @var{y} ignoring\n"
127 "case, else @code{#f}.")
1bbd0b84 128#define FUNC_NAME s_scm_char_ci_eq_p
0f2d19dd 129{
7866a09b
GB
130 SCM_VALIDATE_CHAR (1,x);
131 SCM_VALIDATE_CHAR (2,y);
132 return SCM_BOOL(scm_upcase(SCM_CHAR(x))==scm_upcase(SCM_CHAR(y)));
0f2d19dd 133}
1bbd0b84 134#undef FUNC_NAME
0f2d19dd 135
c3ee7520 136SCM_DEFINE1 (scm_char_ci_less_p, "char-ci<?", scm_tc7_rpsubr,
1bbd0b84 137 (SCM x, SCM y),
cdbc7418
NJ
138 "Return @code{#t} iff @var{x} is less than @var{y} in the ASCII sequence\n"
139 "ignoring case, else @code{#f}.")
1bbd0b84 140#define FUNC_NAME s_scm_char_ci_less_p
0f2d19dd 141{
7866a09b
GB
142 SCM_VALIDATE_CHAR (1,x);
143 SCM_VALIDATE_CHAR (2,y);
144 return SCM_BOOL((scm_upcase(SCM_CHAR(x))) < scm_upcase(SCM_CHAR(y)));
0f2d19dd 145}
1bbd0b84 146#undef FUNC_NAME
0f2d19dd 147
c3ee7520 148SCM_DEFINE1 (scm_char_ci_leq_p, "char-ci<=?", scm_tc7_rpsubr,
1bbd0b84 149 (SCM x, SCM y),
cdbc7418
NJ
150 "Return @code{#t} iff @var{x} is less than or equal to @var{y} in the\n"
151 "ASCII sequence ignoring case, else @code{#f}.")
1bbd0b84 152#define FUNC_NAME s_scm_char_ci_leq_p
0f2d19dd 153{
7866a09b
GB
154 SCM_VALIDATE_CHAR (1,x);
155 SCM_VALIDATE_CHAR (2,y);
156 return SCM_BOOL(scm_upcase(SCM_CHAR(x)) <= scm_upcase(SCM_CHAR(y)));
0f2d19dd 157}
1bbd0b84 158#undef FUNC_NAME
0f2d19dd 159
c3ee7520 160SCM_DEFINE1 (scm_char_ci_gr_p, "char-ci>?", scm_tc7_rpsubr,
1bbd0b84 161 (SCM x, SCM y),
cdbc7418
NJ
162 "Return @code{#t} iff @var{x} is greater than @var{y} in the ASCII\n"
163 "sequence ignoring case, else @code{#f}.")
1bbd0b84 164#define FUNC_NAME s_scm_char_ci_gr_p
0f2d19dd 165{
7866a09b
GB
166 SCM_VALIDATE_CHAR (1,x);
167 SCM_VALIDATE_CHAR (2,y);
168 return SCM_BOOL(scm_upcase(SCM_CHAR(x)) > scm_upcase(SCM_CHAR(y)));
0f2d19dd 169}
1bbd0b84 170#undef FUNC_NAME
0f2d19dd 171
c3ee7520 172SCM_DEFINE1 (scm_char_ci_geq_p, "char-ci>=?", scm_tc7_rpsubr,
1bbd0b84 173 (SCM x, SCM y),
cdbc7418
NJ
174 "Return @code{#t} iff @var{x} is greater than or equal to @var{y} in the\n"
175 "ASCII sequence ignoring case, else @code{#f}.")
1bbd0b84 176#define FUNC_NAME s_scm_char_ci_geq_p
0f2d19dd 177{
7866a09b
GB
178 SCM_VALIDATE_CHAR (1,x);
179 SCM_VALIDATE_CHAR (2,y);
180 return SCM_BOOL(scm_upcase(SCM_CHAR(x)) >= scm_upcase(SCM_CHAR(y)));
0f2d19dd 181}
1bbd0b84 182#undef FUNC_NAME
0f2d19dd
JB
183
184
3b3b36dd 185SCM_DEFINE (scm_char_alphabetic_p, "char-alphabetic?", 1, 0, 0,
1bbd0b84 186 (SCM chr),
cdbc7418 187 "Return @code{#t} iff @var{chr} is alphabetic, else @code{#f}.\n"
b380b885 188 "Alphabetic means the same thing as the isalpha C library function.")
1bbd0b84 189#define FUNC_NAME s_scm_char_alphabetic_p
0f2d19dd 190{
7866a09b
GB
191 SCM_VALIDATE_CHAR (1,chr);
192 return SCM_BOOL(isascii(SCM_CHAR(chr)) && isalpha(SCM_CHAR(chr)));
0f2d19dd 193}
1bbd0b84 194#undef FUNC_NAME
0f2d19dd 195
3b3b36dd 196SCM_DEFINE (scm_char_numeric_p, "char-numeric?", 1, 0, 0,
1bbd0b84 197 (SCM chr),
cdbc7418 198 "Return @code{#t} iff @var{chr} is numeric, else @code{#f}.\n"
b380b885 199 "Numeric means the same thing as the isdigit C library function.")
1bbd0b84 200#define FUNC_NAME s_scm_char_numeric_p
0f2d19dd 201{
7866a09b
GB
202 SCM_VALIDATE_CHAR (1,chr);
203 return SCM_BOOL(isascii(SCM_CHAR(chr)) && isdigit(SCM_CHAR(chr)));
0f2d19dd 204}
1bbd0b84 205#undef FUNC_NAME
0f2d19dd 206
3b3b36dd 207SCM_DEFINE (scm_char_whitespace_p, "char-whitespace?", 1, 0, 0,
1bbd0b84 208 (SCM chr),
cdbc7418 209 "Return @code{#t} iff @var{chr} is whitespace, else @code{#f}.\n"
b380b885 210 "Whitespace means the same thing as the isspace C library function.")
1bbd0b84 211#define FUNC_NAME s_scm_char_whitespace_p
0f2d19dd 212{
7866a09b
GB
213 SCM_VALIDATE_CHAR (1,chr);
214 return SCM_BOOL(isascii(SCM_CHAR(chr)) && isspace(SCM_CHAR(chr)));
0f2d19dd 215}
1bbd0b84 216#undef FUNC_NAME
0f2d19dd
JB
217
218
219
3b3b36dd 220SCM_DEFINE (scm_char_upper_case_p, "char-upper-case?", 1, 0, 0,
1bbd0b84 221 (SCM chr),
cdbc7418 222 "Return @code{#t} iff @var{chr} is uppercase, else @code{#f}.\n"
b380b885 223 "Uppercase means the same thing as the isupper C library function.")
1bbd0b84 224#define FUNC_NAME s_scm_char_upper_case_p
0f2d19dd 225{
7866a09b
GB
226 SCM_VALIDATE_CHAR (1,chr);
227 return SCM_BOOL(isascii(SCM_CHAR(chr)) && isupper(SCM_CHAR(chr)));
0f2d19dd 228}
1bbd0b84 229#undef FUNC_NAME
0f2d19dd
JB
230
231
3b3b36dd 232SCM_DEFINE (scm_char_lower_case_p, "char-lower-case?", 1, 0, 0,
1bbd0b84 233 (SCM chr),
cdbc7418 234 "Return @code{#t} iff @var{chr} is lowercase, else @code{#f}.\n"
b380b885 235 "Lowercase means the same thing as the islower C library function.")
1bbd0b84 236#define FUNC_NAME s_scm_char_lower_case_p
0f2d19dd 237{
7866a09b
GB
238 SCM_VALIDATE_CHAR (1,chr);
239 return SCM_BOOL(isascii(SCM_CHAR(chr)) && islower(SCM_CHAR(chr)));
0f2d19dd 240}
1bbd0b84 241#undef FUNC_NAME
0f2d19dd
JB
242
243
244
a1ec6916 245SCM_DEFINE (scm_char_is_both_p, "char-is-both?", 1, 0, 0,
1bbd0b84 246 (SCM chr),
cdbc7418 247 "Return @code{#t} iff @var{chr} is either uppercase or lowercase, else @code{#f}.\n"
b380b885
MD
248 "Uppercase and lowercase are as defined by the isupper and islower\n"
249 "C library functions.")
1bbd0b84 250#define FUNC_NAME s_scm_char_is_both_p
0f2d19dd 251{
7866a09b
GB
252 SCM_VALIDATE_CHAR (1,chr);
253 return SCM_BOOL(isascii(SCM_CHAR(chr)) && (isupper(SCM_CHAR(chr)) || islower(SCM_CHAR(chr))));
0f2d19dd 254}
1bbd0b84 255#undef FUNC_NAME
0f2d19dd
JB
256
257
258
259
a1ec6916 260SCM_DEFINE (scm_char_to_integer, "char->integer", 1, 0, 0,
1bbd0b84 261 (SCM chr),
cdbc7418
NJ
262 "Return the number corresponding to ordinal position of @var{chr} in the\n"
263 "ASCII sequence.")
1bbd0b84 264#define FUNC_NAME s_scm_char_to_integer
0f2d19dd 265{
7866a09b
GB
266 SCM_VALIDATE_CHAR (1,chr);
267 return scm_ulong2num((unsigned long)SCM_CHAR(chr));
0f2d19dd 268}
1bbd0b84 269#undef FUNC_NAME
0f2d19dd
JB
270
271
272
3b3b36dd 273SCM_DEFINE (scm_integer_to_char, "integer->char", 1, 0, 0,
1bbd0b84 274 (SCM n),
cdbc7418 275 "Return the character at position @var{n} in the ASCII sequence.")
1bbd0b84 276#define FUNC_NAME s_scm_integer_to_char
0f2d19dd 277{
90e49489 278 SCM_VALIDATE_INUM_RANGE (1, n, 0, 256);
7866a09b 279 return SCM_MAKE_CHAR (SCM_INUM (n));
0f2d19dd 280}
1bbd0b84 281#undef FUNC_NAME
0f2d19dd
JB
282
283
3b3b36dd 284SCM_DEFINE (scm_char_upcase, "char-upcase", 1, 0, 0,
1bbd0b84 285 (SCM chr),
cdbc7418 286 "Return the uppercase character version of @var{chr}.")
1bbd0b84 287#define FUNC_NAME s_scm_char_upcase
0f2d19dd 288{
7866a09b
GB
289 SCM_VALIDATE_CHAR (1,chr);
290 return SCM_MAKE_CHAR(scm_upcase(SCM_CHAR(chr)));
0f2d19dd 291}
1bbd0b84 292#undef FUNC_NAME
0f2d19dd
JB
293
294
3b3b36dd 295SCM_DEFINE (scm_char_downcase, "char-downcase", 1, 0, 0,
1bbd0b84 296 (SCM chr),
cdbc7418 297 "Return the lowercase character version of @var{chr}.")
1bbd0b84 298#define FUNC_NAME s_scm_char_downcase
0f2d19dd 299{
7866a09b
GB
300 SCM_VALIDATE_CHAR (1,chr);
301 return SCM_MAKE_CHAR(scm_downcase(SCM_CHAR(chr)));
0f2d19dd 302}
1bbd0b84 303#undef FUNC_NAME
0f2d19dd
JB
304
305\f
306
307
308
e2806c10
MD
309static unsigned char scm_upcase_table[SCM_CHAR_CODE_LIMIT];
310static unsigned char scm_downcase_table[SCM_CHAR_CODE_LIMIT];
57e3a543
JB
311static const unsigned char scm_lowers[] = "abcdefghijklmnopqrstuvwxyz";
312static const unsigned char scm_uppers[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
0f2d19dd 313
1cc91f1b 314
0f2d19dd
JB
315void
316scm_tables_prehistory ()
0f2d19dd
JB
317{
318 int i;
e2806c10 319 for (i = 0; i < SCM_CHAR_CODE_LIMIT; i++)
0f2d19dd 320 scm_upcase_table[i] = scm_downcase_table[i] = i;
77364130 321 for (i = 0; i < (int) (sizeof scm_lowers / sizeof (scm_lowers[0])); i++)
0f2d19dd
JB
322 {
323 scm_upcase_table[scm_lowers[i]] = scm_uppers[i];
324 scm_downcase_table[scm_uppers[i]] = scm_lowers[i];
325 }
326}
327
1cc91f1b 328
0f2d19dd 329int
6e8d25a6 330scm_upcase (unsigned int c)
0f2d19dd
JB
331{
332 if (c < sizeof (scm_upcase_table))
333 return scm_upcase_table[c];
334 else
335 return c;
336}
337
1cc91f1b 338
0f2d19dd 339int
6e8d25a6 340scm_downcase (unsigned int c)
0f2d19dd
JB
341{
342 if (c < sizeof (scm_downcase_table))
343 return scm_downcase_table[c];
344 else
345 return c;
346}
347
348
349#ifdef _DCC
350# define ASCII
351#else
352# if (('\n'=='\025') && (' '=='\100') && ('a'=='\201') && ('A'=='\301'))
353# define EBCDIC
354# endif /* (('\n'=='\025') && (' '=='\100') && ('a'=='\201') && ('A'=='\301')) */
355# if (('\n'=='\012') && (' '=='\040') && ('a'=='\141') && ('A'=='\101'))
356# define ASCII
357# endif /* (('\n'=='\012') && (' '=='\040') && ('a'=='\141') && ('A'=='\101')) */
358#endif /* def _DCC */
359
360
361#ifdef EBCDIC
57e3a543 362char *const scm_charnames[] =
0f2d19dd
JB
363{
364 "nul","soh","stx","etx", "pf", "ht", "lc","del",
365 0 , 0 ,"smm", "vt", "ff", "cr", "so", "si",
366 "dle","dc1","dc2","dc3","res", "nl", "bs", "il",
367 "can", "em", "cc", 0 ,"ifs","igs","irs","ius",
368 "ds","sos", "fs", 0 ,"byp", "lf","eob","pre",
369 0 , 0 , "sm", 0 , 0 ,"enq","ack","bel",
370 0 , 0 ,"syn", 0 , "pn", "rs", "uc","eot",
371 0 , 0 , 0 , 0 ,"dc4","nak", 0 ,"sub",
372 "space", scm_s_newline, "tab", "backspace", "return", "page", "null"};
373
57e3a543 374const char scm_charnums[] =
0f2d19dd
JB
375"\000\001\002\003\004\005\006\007\
376\010\011\012\013\014\015\016\017\
377\020\021\022\023\024\025\026\027\
378\030\031\032\033\034\035\036\037\
379\040\041\042\043\044\045\046\047\
380\050\051\052\053\054\055\056\057\
381\060\061\062\063\064\065\066\067\
382\070\071\072\073\074\075\076\077\
383 \n\t\b\r\f\0";
384#endif /* def EBCDIC */
385#ifdef ASCII
57e3a543 386char *const scm_charnames[] =
0f2d19dd
JB
387{
388 "nul","soh","stx","etx","eot","enq","ack","bel",
35fd4394 389 "bs", "ht", "newline", "vt", "np", "cr", "so", "si",
0f2d19dd
JB
390 "dle","dc1","dc2","dc3","dc4","nak","syn","etb",
391 "can", "em","sub","esc", "fs", "gs", "rs", "us",
35fd4394 392 "space", "nl", "tab", "backspace", "return", "page", "null", "del"};
57e3a543 393const char scm_charnums[] =
0f2d19dd
JB
394"\000\001\002\003\004\005\006\007\
395\010\011\012\013\014\015\016\017\
396\020\021\022\023\024\025\026\027\
397\030\031\032\033\034\035\036\037\
398 \n\t\b\r\f\0\177";
399#endif /* def ASCII */
400
401int scm_n_charnames = sizeof (scm_charnames) / sizeof (char *);
402
403
404\f
405
1cc91f1b 406
0f2d19dd
JB
407void
408scm_init_chars ()
0f2d19dd 409{
8dc9439f 410#ifndef SCM_MAGIC_SNARFER
a0599745 411#include "libguile/chars.x"
8dc9439f 412#endif
0f2d19dd
JB
413}
414
89e00824
ML
415
416/*
417 Local Variables:
418 c-file-style: "gnu"
419 End:
420*/