chars.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /* Copyright (C) 1995,1996,1998, 2000, 2001, 2004 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., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301 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. #include <ctype.h>
  42. #include <limits.h>
  43. #include "libguile/_scm.h"
  44. #include "libguile/validate.h"
  45. #include "libguile/chars.h"
  46. SCM_DEFINE (scm_char_p, "char?", 1, 0, 0,
  47. (SCM x),
  48. "Return @code{#t} iff @var{x} is a character, else @code{#f}.")
  49. #define FUNC_NAME s_scm_char_p
  50. {
  51. return SCM_BOOL(SCM_CHARP(x));
  52. }
  53. #undef FUNC_NAME
  54. SCM_DEFINE1 (scm_char_eq_p, "char=?", scm_tc7_rpsubr,
  55. (SCM x, SCM y),
  56. "Return @code{#t} iff @var{x} is the same character as @var{y}, else @code{#f}.")
  57. #define FUNC_NAME s_scm_char_eq_p
  58. {
  59. SCM_VALIDATE_CHAR (1, x);
  60. SCM_VALIDATE_CHAR (2, y);
  61. return SCM_BOOL (SCM_EQ_P (x, y));
  62. }
  63. #undef FUNC_NAME
  64. SCM_DEFINE1 (scm_char_less_p, "char<?", scm_tc7_rpsubr,
  65. (SCM x, SCM y),
  66. "Return @code{#t} iff @var{x} is less than @var{y} in the ASCII sequence,\n"
  67. "else @code{#f}.")
  68. #define FUNC_NAME s_scm_char_less_p
  69. {
  70. SCM_VALIDATE_CHAR (1,x);
  71. SCM_VALIDATE_CHAR (2,y);
  72. return SCM_BOOL(SCM_CHAR(x) < SCM_CHAR(y));
  73. }
  74. #undef FUNC_NAME
  75. SCM_DEFINE1 (scm_char_leq_p, "char<=?", scm_tc7_rpsubr,
  76. (SCM x, SCM y),
  77. "Return @code{#t} iff @var{x} is less than or equal to @var{y} in the\n"
  78. "ASCII sequence, else @code{#f}.")
  79. #define FUNC_NAME s_scm_char_leq_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. SCM_DEFINE1 (scm_char_gr_p, "char>?", scm_tc7_rpsubr,
  87. (SCM x, SCM y),
  88. "Return @code{#t} iff @var{x} is greater than @var{y} in the ASCII\n"
  89. "sequence, else @code{#f}.")
  90. #define FUNC_NAME s_scm_char_gr_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. SCM_DEFINE1 (scm_char_geq_p, "char>=?", scm_tc7_rpsubr,
  98. (SCM x, SCM y),
  99. "Return @code{#t} iff @var{x} is greater than or equal to @var{y} in the\n"
  100. "ASCII sequence, else @code{#f}.")
  101. #define FUNC_NAME s_scm_char_geq_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. SCM_DEFINE1 (scm_char_ci_eq_p, "char-ci=?", scm_tc7_rpsubr,
  109. (SCM x, SCM y),
  110. "Return @code{#t} iff @var{x} is the same character as @var{y} ignoring\n"
  111. "case, else @code{#f}.")
  112. #define FUNC_NAME s_scm_char_ci_eq_p
  113. {
  114. SCM_VALIDATE_CHAR (1,x);
  115. SCM_VALIDATE_CHAR (2,y);
  116. return SCM_BOOL(scm_upcase(SCM_CHAR(x))==scm_upcase(SCM_CHAR(y)));
  117. }
  118. #undef FUNC_NAME
  119. SCM_DEFINE1 (scm_char_ci_less_p, "char-ci<?", scm_tc7_rpsubr,
  120. (SCM x, SCM y),
  121. "Return @code{#t} iff @var{x} is less than @var{y} in the ASCII sequence\n"
  122. "ignoring case, else @code{#f}.")
  123. #define FUNC_NAME s_scm_char_ci_less_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. SCM_DEFINE1 (scm_char_ci_leq_p, "char-ci<=?", scm_tc7_rpsubr,
  131. (SCM x, SCM y),
  132. "Return @code{#t} iff @var{x} is less than or equal to @var{y} in the\n"
  133. "ASCII sequence ignoring case, else @code{#f}.")
  134. #define FUNC_NAME s_scm_char_ci_leq_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. SCM_DEFINE1 (scm_char_ci_gr_p, "char-ci>?", scm_tc7_rpsubr,
  142. (SCM x, SCM y),
  143. "Return @code{#t} iff @var{x} is greater than @var{y} in the ASCII\n"
  144. "sequence ignoring case, else @code{#f}.")
  145. #define FUNC_NAME s_scm_char_ci_gr_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. SCM_DEFINE1 (scm_char_ci_geq_p, "char-ci>=?", scm_tc7_rpsubr,
  153. (SCM x, SCM y),
  154. "Return @code{#t} iff @var{x} is greater than or equal to @var{y} in the\n"
  155. "ASCII sequence ignoring case, else @code{#f}.")
  156. #define FUNC_NAME s_scm_char_ci_geq_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. SCM_DEFINE (scm_char_alphabetic_p, "char-alphabetic?", 1, 0, 0,
  164. (SCM chr),
  165. "Return @code{#t} iff @var{chr} is alphabetic, else @code{#f}.\n"
  166. "Alphabetic means the same thing as the isalpha C library function.")
  167. #define FUNC_NAME s_scm_char_alphabetic_p
  168. {
  169. SCM_VALIDATE_CHAR (1,chr);
  170. return SCM_BOOL(isalpha(SCM_CHAR(chr)));
  171. }
  172. #undef FUNC_NAME
  173. SCM_DEFINE (scm_char_numeric_p, "char-numeric?", 1, 0, 0,
  174. (SCM chr),
  175. "Return @code{#t} iff @var{chr} is numeric, else @code{#f}.\n"
  176. "Numeric means the same thing as the isdigit C library function.")
  177. #define FUNC_NAME s_scm_char_numeric_p
  178. {
  179. SCM_VALIDATE_CHAR (1,chr);
  180. return SCM_BOOL(isdigit(SCM_CHAR(chr)));
  181. }
  182. #undef FUNC_NAME
  183. SCM_DEFINE (scm_char_whitespace_p, "char-whitespace?", 1, 0, 0,
  184. (SCM chr),
  185. "Return @code{#t} iff @var{chr} is whitespace, else @code{#f}.\n"
  186. "Whitespace means the same thing as the isspace C library function.")
  187. #define FUNC_NAME s_scm_char_whitespace_p
  188. {
  189. SCM_VALIDATE_CHAR (1,chr);
  190. return SCM_BOOL(isspace(SCM_CHAR(chr)));
  191. }
  192. #undef FUNC_NAME
  193. SCM_DEFINE (scm_char_upper_case_p, "char-upper-case?", 1, 0, 0,
  194. (SCM chr),
  195. "Return @code{#t} iff @var{chr} is uppercase, else @code{#f}.\n"
  196. "Uppercase means the same thing as the isupper C library function.")
  197. #define FUNC_NAME s_scm_char_upper_case_p
  198. {
  199. SCM_VALIDATE_CHAR (1,chr);
  200. return SCM_BOOL(isupper(SCM_CHAR(chr)));
  201. }
  202. #undef FUNC_NAME
  203. SCM_DEFINE (scm_char_lower_case_p, "char-lower-case?", 1, 0, 0,
  204. (SCM chr),
  205. "Return @code{#t} iff @var{chr} is lowercase, else @code{#f}.\n"
  206. "Lowercase means the same thing as the islower C library function.")
  207. #define FUNC_NAME s_scm_char_lower_case_p
  208. {
  209. SCM_VALIDATE_CHAR (1,chr);
  210. return SCM_BOOL(islower(SCM_CHAR(chr)));
  211. }
  212. #undef FUNC_NAME
  213. SCM_DEFINE (scm_char_is_both_p, "char-is-both?", 1, 0, 0,
  214. (SCM chr),
  215. "Return @code{#t} iff @var{chr} is either uppercase or lowercase, else @code{#f}.\n"
  216. "Uppercase and lowercase are as defined by the isupper and islower\n"
  217. "C library functions.")
  218. #define FUNC_NAME s_scm_char_is_both_p
  219. {
  220. SCM_VALIDATE_CHAR (1,chr);
  221. return SCM_BOOL((isupper(SCM_CHAR(chr)) || islower(SCM_CHAR(chr))));
  222. }
  223. #undef FUNC_NAME
  224. SCM_DEFINE (scm_char_to_integer, "char->integer", 1, 0, 0,
  225. (SCM chr),
  226. "Return the number corresponding to ordinal position of @var{chr} in the\n"
  227. "ASCII sequence.")
  228. #define FUNC_NAME s_scm_char_to_integer
  229. {
  230. SCM_VALIDATE_CHAR (1,chr);
  231. return scm_ulong2num((unsigned long)SCM_CHAR(chr));
  232. }
  233. #undef FUNC_NAME
  234. SCM_DEFINE (scm_integer_to_char, "integer->char", 1, 0, 0,
  235. (SCM n),
  236. "Return the character at position @var{n} in the ASCII sequence.")
  237. #define FUNC_NAME s_scm_integer_to_char
  238. {
  239. SCM_VALIDATE_INUM_RANGE (1, n, 0, 256);
  240. return SCM_MAKE_CHAR (SCM_INUM (n));
  241. }
  242. #undef FUNC_NAME
  243. SCM_DEFINE (scm_char_upcase, "char-upcase", 1, 0, 0,
  244. (SCM chr),
  245. "Return the uppercase character version of @var{chr}.")
  246. #define FUNC_NAME s_scm_char_upcase
  247. {
  248. SCM_VALIDATE_CHAR (1,chr);
  249. return SCM_MAKE_CHAR(toupper(SCM_CHAR(chr)));
  250. }
  251. #undef FUNC_NAME
  252. SCM_DEFINE (scm_char_downcase, "char-downcase", 1, 0, 0,
  253. (SCM chr),
  254. "Return the lowercase character version of @var{chr}.")
  255. #define FUNC_NAME s_scm_char_downcase
  256. {
  257. SCM_VALIDATE_CHAR (1,chr);
  258. return SCM_MAKE_CHAR(tolower(SCM_CHAR(chr)));
  259. }
  260. #undef FUNC_NAME
  261. int
  262. scm_upcase (unsigned int c)
  263. {
  264. if (c <= UCHAR_MAX)
  265. return toupper (c);
  266. else
  267. return c;
  268. }
  269. int
  270. scm_downcase (unsigned int c)
  271. {
  272. if (c <= UCHAR_MAX)
  273. return tolower (c);
  274. else
  275. return c;
  276. }
  277. #ifdef _DCC
  278. # define ASCII
  279. #else
  280. # if (('\n'=='\025') && (' '=='\100') && ('a'=='\201') && ('A'=='\301'))
  281. # define EBCDIC
  282. # endif /* (('\n'=='\025') && (' '=='\100') && ('a'=='\201') && ('A'=='\301')) */
  283. # if (('\n'=='\012') && (' '=='\040') && ('a'=='\141') && ('A'=='\101'))
  284. # define ASCII
  285. # endif /* (('\n'=='\012') && (' '=='\040') && ('a'=='\141') && ('A'=='\101')) */
  286. #endif /* def _DCC */
  287. #ifdef EBCDIC
  288. char *const scm_charnames[] =
  289. {
  290. "nul","soh","stx","etx", "pf", "ht", "lc","del",
  291. 0 , 0 ,"smm", "vt", "ff", "cr", "so", "si",
  292. "dle","dc1","dc2","dc3","res", "nl", "bs", "il",
  293. "can", "em", "cc", 0 ,"ifs","igs","irs","ius",
  294. "ds","sos", "fs", 0 ,"byp", "lf","eob","pre",
  295. 0 , 0 , "sm", 0 , 0 ,"enq","ack","bel",
  296. 0 , 0 ,"syn", 0 , "pn", "rs", "uc","eot",
  297. 0 , 0 , 0 , 0 ,"dc4","nak", 0 ,"sub",
  298. "space", scm_s_newline, "tab", "backspace", "return", "page", "null"};
  299. const char scm_charnums[] =
  300. "\000\001\002\003\004\005\006\007\
  301. \010\011\012\013\014\015\016\017\
  302. \020\021\022\023\024\025\026\027\
  303. \030\031\032\033\034\035\036\037\
  304. \040\041\042\043\044\045\046\047\
  305. \050\051\052\053\054\055\056\057\
  306. \060\061\062\063\064\065\066\067\
  307. \070\071\072\073\074\075\076\077\
  308. \n\t\b\r\f\0";
  309. #endif /* def EBCDIC */
  310. #ifdef ASCII
  311. char *const scm_charnames[] =
  312. {
  313. "nul","soh","stx","etx","eot","enq","ack","bel",
  314. "bs", "ht", "newline", "vt", "np", "cr", "so", "si",
  315. "dle","dc1","dc2","dc3","dc4","nak","syn","etb",
  316. "can", "em","sub","esc", "fs", "gs", "rs", "us",
  317. "space", "sp", "nl", "tab", "backspace", "return", "page", "null", "del"};
  318. const char scm_charnums[] =
  319. "\000\001\002\003\004\005\006\007\
  320. \010\011\012\013\014\015\016\017\
  321. \020\021\022\023\024\025\026\027\
  322. \030\031\032\033\034\035\036\037\
  323. \n\t\b\r\f\0\177";
  324. #endif /* def ASCII */
  325. int scm_n_charnames = sizeof (scm_charnames) / sizeof (char *);
  326. void
  327. scm_init_chars ()
  328. {
  329. #include "libguile/chars.x"
  330. }
  331. /*
  332. Local Variables:
  333. c-file-style: "gnu"
  334. End:
  335. */