chars.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* Copyright (C) 1995,1996,1998, 2000, 2001, 2004, 2006, 2008 Free Software Foundation, Inc.
  2. *
  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.
  7. *
  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.
  12. *
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. # include <config.h>
  19. #endif
  20. #include <ctype.h>
  21. #include <limits.h>
  22. #include "libguile/_scm.h"
  23. #include "libguile/validate.h"
  24. #include "libguile/chars.h"
  25. #include "libguile/srfi-14.h"
  26. SCM_DEFINE (scm_char_p, "char?", 1, 0, 0,
  27. (SCM x),
  28. "Return @code{#t} iff @var{x} is a character, else @code{#f}.")
  29. #define FUNC_NAME s_scm_char_p
  30. {
  31. return scm_from_bool (SCM_CHARP(x));
  32. }
  33. #undef FUNC_NAME
  34. SCM_DEFINE1 (scm_char_eq_p, "char=?", scm_tc7_rpsubr,
  35. (SCM x, SCM y),
  36. "Return @code{#t} iff @var{x} is the same character as @var{y}, else @code{#f}.")
  37. #define FUNC_NAME s_scm_char_eq_p
  38. {
  39. SCM_VALIDATE_CHAR (1, x);
  40. SCM_VALIDATE_CHAR (2, y);
  41. return scm_from_bool (scm_is_eq (x, y));
  42. }
  43. #undef FUNC_NAME
  44. SCM_DEFINE1 (scm_char_less_p, "char<?", scm_tc7_rpsubr,
  45. (SCM x, SCM y),
  46. "Return @code{#t} iff @var{x} is less than @var{y} in the ASCII sequence,\n"
  47. "else @code{#f}.")
  48. #define FUNC_NAME s_scm_char_less_p
  49. {
  50. SCM_VALIDATE_CHAR (1, x);
  51. SCM_VALIDATE_CHAR (2, y);
  52. return scm_from_bool (SCM_CHAR(x) < SCM_CHAR(y));
  53. }
  54. #undef FUNC_NAME
  55. SCM_DEFINE1 (scm_char_leq_p, "char<=?", scm_tc7_rpsubr,
  56. (SCM x, SCM y),
  57. "Return @code{#t} iff @var{x} is less than or equal to @var{y} in the\n"
  58. "ASCII sequence, else @code{#f}.")
  59. #define FUNC_NAME s_scm_char_leq_p
  60. {
  61. SCM_VALIDATE_CHAR (1, x);
  62. SCM_VALIDATE_CHAR (2, y);
  63. return scm_from_bool (SCM_CHAR(x) <= SCM_CHAR(y));
  64. }
  65. #undef FUNC_NAME
  66. SCM_DEFINE1 (scm_char_gr_p, "char>?", scm_tc7_rpsubr,
  67. (SCM x, SCM y),
  68. "Return @code{#t} iff @var{x} is greater than @var{y} in the ASCII\n"
  69. "sequence, else @code{#f}.")
  70. #define FUNC_NAME s_scm_char_gr_p
  71. {
  72. SCM_VALIDATE_CHAR (1, x);
  73. SCM_VALIDATE_CHAR (2, y);
  74. return scm_from_bool (SCM_CHAR(x) > SCM_CHAR(y));
  75. }
  76. #undef FUNC_NAME
  77. SCM_DEFINE1 (scm_char_geq_p, "char>=?", scm_tc7_rpsubr,
  78. (SCM x, SCM y),
  79. "Return @code{#t} iff @var{x} is greater than or equal to @var{y} in the\n"
  80. "ASCII sequence, else @code{#f}.")
  81. #define FUNC_NAME s_scm_char_geq_p
  82. {
  83. SCM_VALIDATE_CHAR (1, x);
  84. SCM_VALIDATE_CHAR (2, y);
  85. return scm_from_bool (SCM_CHAR(x) >= SCM_CHAR(y));
  86. }
  87. #undef FUNC_NAME
  88. SCM_DEFINE1 (scm_char_ci_eq_p, "char-ci=?", scm_tc7_rpsubr,
  89. (SCM x, SCM y),
  90. "Return @code{#t} iff @var{x} is the same character as @var{y} ignoring\n"
  91. "case, else @code{#f}.")
  92. #define FUNC_NAME s_scm_char_ci_eq_p
  93. {
  94. SCM_VALIDATE_CHAR (1, x);
  95. SCM_VALIDATE_CHAR (2, y);
  96. return scm_from_bool (scm_c_upcase(SCM_CHAR(x))==scm_c_upcase(SCM_CHAR(y)));
  97. }
  98. #undef FUNC_NAME
  99. SCM_DEFINE1 (scm_char_ci_less_p, "char-ci<?", scm_tc7_rpsubr,
  100. (SCM x, SCM y),
  101. "Return @code{#t} iff @var{x} is less than @var{y} in the ASCII sequence\n"
  102. "ignoring case, else @code{#f}.")
  103. #define FUNC_NAME s_scm_char_ci_less_p
  104. {
  105. SCM_VALIDATE_CHAR (1, x);
  106. SCM_VALIDATE_CHAR (2, y);
  107. return scm_from_bool ((scm_c_upcase(SCM_CHAR(x))) < scm_c_upcase(SCM_CHAR(y)));
  108. }
  109. #undef FUNC_NAME
  110. SCM_DEFINE1 (scm_char_ci_leq_p, "char-ci<=?", scm_tc7_rpsubr,
  111. (SCM x, SCM y),
  112. "Return @code{#t} iff @var{x} is less than or equal to @var{y} in the\n"
  113. "ASCII sequence ignoring case, else @code{#f}.")
  114. #define FUNC_NAME s_scm_char_ci_leq_p
  115. {
  116. SCM_VALIDATE_CHAR (1, x);
  117. SCM_VALIDATE_CHAR (2, y);
  118. return scm_from_bool (scm_c_upcase(SCM_CHAR(x)) <= scm_c_upcase(SCM_CHAR(y)));
  119. }
  120. #undef FUNC_NAME
  121. SCM_DEFINE1 (scm_char_ci_gr_p, "char-ci>?", scm_tc7_rpsubr,
  122. (SCM x, SCM y),
  123. "Return @code{#t} iff @var{x} is greater than @var{y} in the ASCII\n"
  124. "sequence ignoring case, else @code{#f}.")
  125. #define FUNC_NAME s_scm_char_ci_gr_p
  126. {
  127. SCM_VALIDATE_CHAR (1, x);
  128. SCM_VALIDATE_CHAR (2, y);
  129. return scm_from_bool (scm_c_upcase(SCM_CHAR(x)) > scm_c_upcase(SCM_CHAR(y)));
  130. }
  131. #undef FUNC_NAME
  132. SCM_DEFINE1 (scm_char_ci_geq_p, "char-ci>=?", scm_tc7_rpsubr,
  133. (SCM x, SCM y),
  134. "Return @code{#t} iff @var{x} is greater than or equal to @var{y} in the\n"
  135. "ASCII sequence ignoring case, else @code{#f}.")
  136. #define FUNC_NAME s_scm_char_ci_geq_p
  137. {
  138. SCM_VALIDATE_CHAR (1, x);
  139. SCM_VALIDATE_CHAR (2, y);
  140. return scm_from_bool (scm_c_upcase(SCM_CHAR(x)) >= scm_c_upcase(SCM_CHAR(y)));
  141. }
  142. #undef FUNC_NAME
  143. SCM_DEFINE (scm_char_alphabetic_p, "char-alphabetic?", 1, 0, 0,
  144. (SCM chr),
  145. "Return @code{#t} iff @var{chr} is alphabetic, else @code{#f}.\n")
  146. #define FUNC_NAME s_scm_char_alphabetic_p
  147. {
  148. return scm_char_set_contains_p (scm_char_set_letter, chr);
  149. }
  150. #undef FUNC_NAME
  151. SCM_DEFINE (scm_char_numeric_p, "char-numeric?", 1, 0, 0,
  152. (SCM chr),
  153. "Return @code{#t} iff @var{chr} is numeric, else @code{#f}.\n")
  154. #define FUNC_NAME s_scm_char_numeric_p
  155. {
  156. return scm_char_set_contains_p (scm_char_set_digit, chr);
  157. }
  158. #undef FUNC_NAME
  159. SCM_DEFINE (scm_char_whitespace_p, "char-whitespace?", 1, 0, 0,
  160. (SCM chr),
  161. "Return @code{#t} iff @var{chr} is whitespace, else @code{#f}.\n")
  162. #define FUNC_NAME s_scm_char_whitespace_p
  163. {
  164. return scm_char_set_contains_p (scm_char_set_whitespace, chr);
  165. }
  166. #undef FUNC_NAME
  167. SCM_DEFINE (scm_char_upper_case_p, "char-upper-case?", 1, 0, 0,
  168. (SCM chr),
  169. "Return @code{#t} iff @var{chr} is uppercase, else @code{#f}.\n")
  170. #define FUNC_NAME s_scm_char_upper_case_p
  171. {
  172. return scm_char_set_contains_p (scm_char_set_upper_case, chr);
  173. }
  174. #undef FUNC_NAME
  175. SCM_DEFINE (scm_char_lower_case_p, "char-lower-case?", 1, 0, 0,
  176. (SCM chr),
  177. "Return @code{#t} iff @var{chr} is lowercase, else @code{#f}.\n")
  178. #define FUNC_NAME s_scm_char_lower_case_p
  179. {
  180. return scm_char_set_contains_p (scm_char_set_lower_case, chr);
  181. }
  182. #undef FUNC_NAME
  183. SCM_DEFINE (scm_char_is_both_p, "char-is-both?", 1, 0, 0,
  184. (SCM chr),
  185. "Return @code{#t} iff @var{chr} is either uppercase or lowercase, else @code{#f}.\n")
  186. #define FUNC_NAME s_scm_char_is_both_p
  187. {
  188. if (scm_is_true (scm_char_set_contains_p (scm_char_set_lower_case, chr)))
  189. return SCM_BOOL_T;
  190. return scm_char_set_contains_p (scm_char_set_upper_case, chr);
  191. }
  192. #undef FUNC_NAME
  193. SCM_DEFINE (scm_char_to_integer, "char->integer", 1, 0, 0,
  194. (SCM chr),
  195. "Return the number corresponding to ordinal position of @var{chr} in the\n"
  196. "ASCII sequence.")
  197. #define FUNC_NAME s_scm_char_to_integer
  198. {
  199. SCM_VALIDATE_CHAR (1, chr);
  200. return scm_from_ulong (SCM_CHAR(chr));
  201. }
  202. #undef FUNC_NAME
  203. SCM_DEFINE (scm_integer_to_char, "integer->char", 1, 0, 0,
  204. (SCM n),
  205. "Return the character at position @var{n} in the ASCII sequence.")
  206. #define FUNC_NAME s_scm_integer_to_char
  207. {
  208. return SCM_MAKE_CHAR (scm_to_uchar (n));
  209. }
  210. #undef FUNC_NAME
  211. SCM_DEFINE (scm_char_upcase, "char-upcase", 1, 0, 0,
  212. (SCM chr),
  213. "Return the uppercase character version of @var{chr}.")
  214. #define FUNC_NAME s_scm_char_upcase
  215. {
  216. SCM_VALIDATE_CHAR (1, chr);
  217. return SCM_MAKE_CHAR (toupper (SCM_CHAR (chr)));
  218. }
  219. #undef FUNC_NAME
  220. SCM_DEFINE (scm_char_downcase, "char-downcase", 1, 0, 0,
  221. (SCM chr),
  222. "Return the lowercase character version of @var{chr}.")
  223. #define FUNC_NAME s_scm_char_downcase
  224. {
  225. SCM_VALIDATE_CHAR (1, chr);
  226. return SCM_MAKE_CHAR (tolower (SCM_CHAR(chr)));
  227. }
  228. #undef FUNC_NAME
  229. /*
  230. TODO: change name to scm_i_.. ? --hwn
  231. */
  232. int
  233. scm_c_upcase (unsigned int c)
  234. {
  235. if (c <= UCHAR_MAX)
  236. return toupper (c);
  237. else
  238. return c;
  239. }
  240. int
  241. scm_c_downcase (unsigned int c)
  242. {
  243. if (c <= UCHAR_MAX)
  244. return tolower (c);
  245. else
  246. return c;
  247. }
  248. #ifdef _DCC
  249. # define ASCII
  250. #else
  251. # if (('\n'=='\025') && (' '=='\100') && ('a'=='\201') && ('A'=='\301'))
  252. # define EBCDIC
  253. # endif /* (('\n'=='\025') && (' '=='\100') && ('a'=='\201') && ('A'=='\301')) */
  254. # if (('\n'=='\012') && (' '=='\040') && ('a'=='\141') && ('A'=='\101'))
  255. # define ASCII
  256. # endif /* (('\n'=='\012') && (' '=='\040') && ('a'=='\141') && ('A'=='\101')) */
  257. #endif /* def _DCC */
  258. #ifdef EBCDIC
  259. char *const scm_charnames[] =
  260. {
  261. "nul", "soh", "stx", "etx", "pf", "ht", "lc", "del",
  262. 0 , 0 , "smm", "vt", "ff", "cr", "so", "si",
  263. "dle", "dc1", "dc2", "dc3", "res", "nl", "bs", "il",
  264. "can", "em", "cc", 0 , "ifs", "igs", "irs", "ius",
  265. "ds", "sos", "fs", 0 , "byp", "lf", "eob", "pre",
  266. 0 , 0 , "sm", 0 , 0 , "enq", "ack", "bel",
  267. 0 , 0 , "syn", 0 , "pn", "rs", "uc", "eot",
  268. 0 , 0 , 0 , 0 , "dc4", "nak", 0 , "sub",
  269. "space", scm_s_newline, "tab", "backspace", "return", "page", "null"};
  270. const char scm_charnums[] =
  271. "\000\001\002\003\004\005\006\007\
  272. \010\011\012\013\014\015\016\017\
  273. \020\021\022\023\024\025\026\027\
  274. \030\031\032\033\034\035\036\037\
  275. \040\041\042\043\044\045\046\047\
  276. \050\051\052\053\054\055\056\057\
  277. \060\061\062\063\064\065\066\067\
  278. \070\071\072\073\074\075\076\077\
  279. \n\t\b\r\f\0";
  280. #endif /* def EBCDIC */
  281. #ifdef ASCII
  282. char *const scm_charnames[] =
  283. {
  284. "nul","soh","stx","etx","eot","enq","ack","bel",
  285. "bs", "ht", "newline", "vt", "np", "cr", "so", "si",
  286. "dle","dc1","dc2","dc3","dc4","nak","syn","etb",
  287. "can", "em","sub","esc", "fs", "gs", "rs", "us",
  288. "space", "sp", "nl", "tab", "backspace", "return", "page", "null", "del"};
  289. const char scm_charnums[] =
  290. "\000\001\002\003\004\005\006\007\
  291. \010\011\012\013\014\015\016\017\
  292. \020\021\022\023\024\025\026\027\
  293. \030\031\032\033\034\035\036\037\
  294. \n\t\b\r\f\0\177";
  295. #endif /* def ASCII */
  296. int scm_n_charnames = sizeof (scm_charnames) / sizeof (char *);
  297. void
  298. scm_init_chars ()
  299. {
  300. #include "libguile/chars.x"
  301. }
  302. /*
  303. Local Variables:
  304. c-file-style: "gnu"
  305. End:
  306. */