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