chars.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* Copyright (C) 1995,1996,1998, 2000, 2002 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. #include <stdio.h>
  42. #include <ctype.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 #t iff X is a character, else #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 #t iff X is the same character as Y, else #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_CHAR(x) == SCM_CHAR(y));
  62. }
  63. #undef FUNC_NAME
  64. SCM_DEFINE1 (scm_char_less_p, "char<?", scm_tc7_rpsubr,
  65. (SCM x, SCM y),
  66. "Return #t iff X is less than Y in the Ascii sequence, else #f.")
  67. #define FUNC_NAME s_scm_char_less_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. SCM_DEFINE1 (scm_char_leq_p, "char<=?", scm_tc7_rpsubr,
  75. (SCM x, SCM y),
  76. "Return #t iff X is less than or equal to Y in the Ascii sequence, else #f.")
  77. #define FUNC_NAME s_scm_char_leq_p
  78. {
  79. SCM_VALIDATE_CHAR (1,x);
  80. SCM_VALIDATE_CHAR (2,y);
  81. return SCM_BOOL(SCM_CHAR(x) <= SCM_CHAR(y));
  82. }
  83. #undef FUNC_NAME
  84. SCM_DEFINE1 (scm_char_gr_p, "char>?", scm_tc7_rpsubr,
  85. (SCM x, SCM y),
  86. "Return #t iff X is greater than Y in the Ascii sequence, else #f.")
  87. #define FUNC_NAME s_scm_char_gr_p
  88. {
  89. SCM_VALIDATE_CHAR (1,x);
  90. SCM_VALIDATE_CHAR (2,y);
  91. return SCM_BOOL(SCM_CHAR(x) > SCM_CHAR(y));
  92. }
  93. #undef FUNC_NAME
  94. SCM_DEFINE1 (scm_char_geq_p, "char>=?", scm_tc7_rpsubr,
  95. (SCM x, SCM y),
  96. "Return #t iff X is greater than or equal to Y in the Ascii sequence, else #f.")
  97. #define FUNC_NAME s_scm_char_geq_p
  98. {
  99. SCM_VALIDATE_CHAR (1,x);
  100. SCM_VALIDATE_CHAR (2,y);
  101. return SCM_BOOL(SCM_CHAR(x) >= SCM_CHAR(y));
  102. }
  103. #undef FUNC_NAME
  104. SCM_DEFINE1 (scm_char_ci_eq_p, "char-ci=?", scm_tc7_rpsubr,
  105. (SCM x, SCM y),
  106. "Return #t iff X is the same character as Y ignoring case, else #f.")
  107. #define FUNC_NAME s_scm_char_ci_eq_p
  108. {
  109. SCM_VALIDATE_CHAR (1,x);
  110. SCM_VALIDATE_CHAR (2,y);
  111. return SCM_BOOL(scm_upcase(SCM_CHAR(x))==scm_upcase(SCM_CHAR(y)));
  112. }
  113. #undef FUNC_NAME
  114. SCM_DEFINE1 (scm_char_ci_less_p, "char-ci<?", scm_tc7_rpsubr,
  115. (SCM x, SCM y),
  116. "Return #t iff X is less than Y in the Ascii sequence ignoring case, else #f.")
  117. #define FUNC_NAME s_scm_char_ci_less_p
  118. {
  119. SCM_VALIDATE_CHAR (1,x);
  120. SCM_VALIDATE_CHAR (2,y);
  121. return SCM_BOOL((scm_upcase(SCM_CHAR(x))) < scm_upcase(SCM_CHAR(y)));
  122. }
  123. #undef FUNC_NAME
  124. SCM_DEFINE1 (scm_char_ci_leq_p, "char-ci<=?", scm_tc7_rpsubr,
  125. (SCM x, SCM y),
  126. "Return #t iff X is less than or equal to Y in the Ascii sequence ignoring case, else #f.")
  127. #define FUNC_NAME s_scm_char_ci_leq_p
  128. {
  129. SCM_VALIDATE_CHAR (1,x);
  130. SCM_VALIDATE_CHAR (2,y);
  131. return SCM_BOOL(scm_upcase(SCM_CHAR(x)) <= scm_upcase(SCM_CHAR(y)));
  132. }
  133. #undef FUNC_NAME
  134. SCM_DEFINE1 (scm_char_ci_gr_p, "char-ci>?", scm_tc7_rpsubr,
  135. (SCM x, SCM y),
  136. "Return #t iff X is greater than Y in the Ascii sequence ignoring case, else #f.")
  137. #define FUNC_NAME s_scm_char_ci_gr_p
  138. {
  139. SCM_VALIDATE_CHAR (1,x);
  140. SCM_VALIDATE_CHAR (2,y);
  141. return SCM_BOOL(scm_upcase(SCM_CHAR(x)) > scm_upcase(SCM_CHAR(y)));
  142. }
  143. #undef FUNC_NAME
  144. SCM_DEFINE1 (scm_char_ci_geq_p, "char-ci>=?", scm_tc7_rpsubr,
  145. (SCM x, SCM y),
  146. "Return #t iff X is greater than or equal to Y in the Ascii sequence ignoring case, else #f.")
  147. #define FUNC_NAME s_scm_char_ci_geq_p
  148. {
  149. SCM_VALIDATE_CHAR (1,x);
  150. SCM_VALIDATE_CHAR (2,y);
  151. return SCM_BOOL(scm_upcase(SCM_CHAR(x)) >= scm_upcase(SCM_CHAR(y)));
  152. }
  153. #undef FUNC_NAME
  154. SCM_DEFINE (scm_char_alphabetic_p, "char-alphabetic?", 1, 0, 0,
  155. (SCM chr),
  156. "Return #t iff CHR is alphabetic, else #f.\n"
  157. "Alphabetic means the same thing as the isalpha C library function.")
  158. #define FUNC_NAME s_scm_char_alphabetic_p
  159. {
  160. SCM_VALIDATE_CHAR (1,chr);
  161. return SCM_BOOL(isascii(SCM_CHAR(chr)) && isalpha(SCM_CHAR(chr)));
  162. }
  163. #undef FUNC_NAME
  164. SCM_DEFINE (scm_char_numeric_p, "char-numeric?", 1, 0, 0,
  165. (SCM chr),
  166. "Return #t iff CHR is numeric, else #f.\n"
  167. "Numeric means the same thing as the isdigit C library function.")
  168. #define FUNC_NAME s_scm_char_numeric_p
  169. {
  170. SCM_VALIDATE_CHAR (1,chr);
  171. return SCM_BOOL(isascii(SCM_CHAR(chr)) && isdigit(SCM_CHAR(chr)));
  172. }
  173. #undef FUNC_NAME
  174. SCM_DEFINE (scm_char_whitespace_p, "char-whitespace?", 1, 0, 0,
  175. (SCM chr),
  176. "Return #t iff CHR is whitespace, else #f.\n"
  177. "Whitespace means the same thing as the isspace C library function.")
  178. #define FUNC_NAME s_scm_char_whitespace_p
  179. {
  180. SCM_VALIDATE_CHAR (1,chr);
  181. return SCM_BOOL(isascii(SCM_CHAR(chr)) && isspace(SCM_CHAR(chr)));
  182. }
  183. #undef FUNC_NAME
  184. SCM_DEFINE (scm_char_upper_case_p, "char-upper-case?", 1, 0, 0,
  185. (SCM chr),
  186. "Return #t iff CHR is uppercase, else #f.\n"
  187. "Uppercase means the same thing as the isupper C library function.")
  188. #define FUNC_NAME s_scm_char_upper_case_p
  189. {
  190. SCM_VALIDATE_CHAR (1,chr);
  191. return SCM_BOOL(isascii(SCM_CHAR(chr)) && isupper(SCM_CHAR(chr)));
  192. }
  193. #undef FUNC_NAME
  194. SCM_DEFINE (scm_char_lower_case_p, "char-lower-case?", 1, 0, 0,
  195. (SCM chr),
  196. "Return #t iff CHR is lowercase, else #f.\n"
  197. "Lowercase means the same thing as the islower C library function.")
  198. #define FUNC_NAME s_scm_char_lower_case_p
  199. {
  200. SCM_VALIDATE_CHAR (1,chr);
  201. return SCM_BOOL(isascii(SCM_CHAR(chr)) && islower(SCM_CHAR(chr)));
  202. }
  203. #undef FUNC_NAME
  204. SCM_DEFINE (scm_char_is_both_p, "char-is-both?", 1, 0, 0,
  205. (SCM chr),
  206. "Return #t iff CHR is either uppercase or lowercase, else #f.\n"
  207. "Uppercase and lowercase are as defined by the isupper and islower\n"
  208. "C library functions.")
  209. #define FUNC_NAME s_scm_char_is_both_p
  210. {
  211. SCM_VALIDATE_CHAR (1,chr);
  212. return SCM_BOOL(isascii(SCM_CHAR(chr)) && (isupper(SCM_CHAR(chr)) || islower(SCM_CHAR(chr))));
  213. }
  214. #undef FUNC_NAME
  215. SCM_DEFINE (scm_char_to_integer, "char->integer", 1, 0, 0,
  216. (SCM chr),
  217. "Return the number corresponding to ordinal position of CHR in the Ascii sequence.")
  218. #define FUNC_NAME s_scm_char_to_integer
  219. {
  220. SCM_VALIDATE_CHAR (1,chr);
  221. return scm_ulong2num((unsigned long)SCM_CHAR(chr));
  222. }
  223. #undef FUNC_NAME
  224. SCM_DEFINE (scm_integer_to_char, "integer->char", 1, 0, 0,
  225. (SCM n),
  226. "Return the character at position N in the Ascii sequence.")
  227. #define FUNC_NAME s_scm_integer_to_char
  228. {
  229. SCM_VALIDATE_INUM_RANGE (1, n, 0, 256);
  230. return SCM_MAKE_CHAR (SCM_INUM (n));
  231. }
  232. #undef FUNC_NAME
  233. SCM_DEFINE (scm_char_upcase, "char-upcase", 1, 0, 0,
  234. (SCM chr),
  235. "Return the uppercase character version of CHR.")
  236. #define FUNC_NAME s_scm_char_upcase
  237. {
  238. SCM_VALIDATE_CHAR (1,chr);
  239. return SCM_MAKE_CHAR(scm_upcase(SCM_CHAR(chr)));
  240. }
  241. #undef FUNC_NAME
  242. SCM_DEFINE (scm_char_downcase, "char-downcase", 1, 0, 0,
  243. (SCM chr),
  244. "Return the lowercase character version of CHR.")
  245. #define FUNC_NAME s_scm_char_downcase
  246. {
  247. SCM_VALIDATE_CHAR (1,chr);
  248. return SCM_MAKE_CHAR(scm_downcase(SCM_CHAR(chr)));
  249. }
  250. #undef FUNC_NAME
  251. static unsigned char scm_upcase_table[SCM_CHAR_CODE_LIMIT];
  252. static unsigned char scm_downcase_table[SCM_CHAR_CODE_LIMIT];
  253. static const unsigned char scm_lowers[] = "abcdefghijklmnopqrstuvwxyz";
  254. static const unsigned char scm_uppers[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  255. void
  256. scm_tables_prehistory ()
  257. {
  258. int i;
  259. for (i = 0; i < SCM_CHAR_CODE_LIMIT; i++)
  260. scm_upcase_table[i] = scm_downcase_table[i] = i;
  261. for (i = 0; i < (int) (sizeof scm_lowers / sizeof (scm_lowers[0])); i++)
  262. {
  263. scm_upcase_table[scm_lowers[i]] = scm_uppers[i];
  264. scm_downcase_table[scm_uppers[i]] = scm_lowers[i];
  265. }
  266. }
  267. int
  268. scm_upcase (unsigned int c)
  269. {
  270. if (c < sizeof (scm_upcase_table))
  271. return scm_upcase_table[c];
  272. else
  273. return c;
  274. }
  275. int
  276. scm_downcase (unsigned int c)
  277. {
  278. if (c < sizeof (scm_downcase_table))
  279. return scm_downcase_table[c];
  280. else
  281. return c;
  282. }
  283. #ifdef _DCC
  284. # define ASCII
  285. #else
  286. # if (('\n'=='\025') && (' '=='\100') && ('a'=='\201') && ('A'=='\301'))
  287. # define EBCDIC
  288. # endif /* (('\n'=='\025') && (' '=='\100') && ('a'=='\201') && ('A'=='\301')) */
  289. # if (('\n'=='\012') && (' '=='\040') && ('a'=='\141') && ('A'=='\101'))
  290. # define ASCII
  291. # endif /* (('\n'=='\012') && (' '=='\040') && ('a'=='\141') && ('A'=='\101')) */
  292. #endif /* def _DCC */
  293. #ifdef EBCDIC
  294. char *const scm_charnames[] =
  295. {
  296. "nul","soh","stx","etx", "pf", "ht", "lc","del",
  297. 0 , 0 ,"smm", "vt", "ff", "cr", "so", "si",
  298. "dle","dc1","dc2","dc3","res", "nl", "bs", "il",
  299. "can", "em", "cc", 0 ,"ifs","igs","irs","ius",
  300. "ds","sos", "fs", 0 ,"byp", "lf","eob","pre",
  301. 0 , 0 , "sm", 0 , 0 ,"enq","ack","bel",
  302. 0 , 0 ,"syn", 0 , "pn", "rs", "uc","eot",
  303. 0 , 0 , 0 , 0 ,"dc4","nak", 0 ,"sub",
  304. "space", scm_s_newline, "tab", "backspace", "return", "page", "null"};
  305. const char scm_charnums[] =
  306. "\000\001\002\003\004\005\006\007\
  307. \010\011\012\013\014\015\016\017\
  308. \020\021\022\023\024\025\026\027\
  309. \030\031\032\033\034\035\036\037\
  310. \040\041\042\043\044\045\046\047\
  311. \050\051\052\053\054\055\056\057\
  312. \060\061\062\063\064\065\066\067\
  313. \070\071\072\073\074\075\076\077\
  314. \n\t\b\r\f\0";
  315. #endif /* def EBCDIC */
  316. #ifdef ASCII
  317. char *const scm_charnames[] =
  318. {
  319. "nul","soh","stx","etx","eot","enq","ack","bel",
  320. "bs", "ht", "newline", "vt", "np", "cr", "so", "si",
  321. "dle","dc1","dc2","dc3","dc4","nak","syn","etb",
  322. "can", "em","sub","esc", "fs", "gs", "rs", "us",
  323. "space", "nl", "tab", "backspace", "return", "page", "null", "del"};
  324. const char scm_charnums[] =
  325. "\000\001\002\003\004\005\006\007\
  326. \010\011\012\013\014\015\016\017\
  327. \020\021\022\023\024\025\026\027\
  328. \030\031\032\033\034\035\036\037\
  329. \n\t\b\r\f\0\177";
  330. #endif /* def ASCII */
  331. int scm_n_charnames = sizeof (scm_charnames) / sizeof (char *);
  332. void
  333. scm_init_chars ()
  334. {
  335. #include "libguile/chars.x"
  336. }
  337. /*
  338. Local Variables:
  339. c-file-style: "gnu"
  340. End:
  341. */