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