srfi-14.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  1. /* srfi-14.c --- SRFI-14 procedures for Guile
  2. *
  3. * Copyright (C) 2001, 2004, 2006, 2008 Free Software Foundation, Inc.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include "libguile.h"
  25. #include "libguile/srfi-14.h"
  26. #define SCM_CHARSET_SET(cs, idx) \
  27. (((long *) SCM_SMOB_DATA (cs))[(idx) / SCM_BITS_PER_LONG] |= \
  28. (1L << ((idx) % SCM_BITS_PER_LONG)))
  29. #define SCM_CHARSET_UNSET(cs, idx) \
  30. (((long *) SCM_SMOB_DATA (cs))[(idx) / SCM_BITS_PER_LONG] &= \
  31. (~(1L << ((idx) % SCM_BITS_PER_LONG))))
  32. #define BYTES_PER_CHARSET (SCM_CHARSET_SIZE / 8)
  33. #define LONGS_PER_CHARSET (SCM_CHARSET_SIZE / SCM_BITS_PER_LONG)
  34. /* Smob type code for character sets. */
  35. int scm_tc16_charset = 0;
  36. /* Smob print hook for character sets. */
  37. static int
  38. charset_print (SCM charset, SCM port, scm_print_state *pstate SCM_UNUSED)
  39. {
  40. int i;
  41. int first = 1;
  42. scm_puts ("#<charset {", port);
  43. for (i = 0; i < SCM_CHARSET_SIZE; i++)
  44. if (SCM_CHARSET_GET (charset, i))
  45. {
  46. if (first)
  47. first = 0;
  48. else
  49. scm_puts (" ", port);
  50. scm_write (SCM_MAKE_CHAR (i), port);
  51. }
  52. scm_puts ("}>", port);
  53. return 1;
  54. }
  55. /* Smob free hook for character sets. */
  56. static size_t
  57. charset_free (SCM charset)
  58. {
  59. return scm_smob_free (charset);
  60. }
  61. /* Create a new, empty character set. */
  62. static SCM
  63. make_char_set (const char * func_name)
  64. {
  65. long * p;
  66. p = scm_gc_malloc (BYTES_PER_CHARSET, "character-set");
  67. memset (p, 0, BYTES_PER_CHARSET);
  68. SCM_RETURN_NEWSMOB (scm_tc16_charset, p);
  69. }
  70. SCM_DEFINE (scm_char_set_p, "char-set?", 1, 0, 0,
  71. (SCM obj),
  72. "Return @code{#t} if @var{obj} is a character set, @code{#f}\n"
  73. "otherwise.")
  74. #define FUNC_NAME s_scm_char_set_p
  75. {
  76. return scm_from_bool (SCM_SMOB_PREDICATE (scm_tc16_charset, obj));
  77. }
  78. #undef FUNC_NAME
  79. SCM_DEFINE (scm_char_set_eq, "char-set=", 0, 0, 1,
  80. (SCM char_sets),
  81. "Return @code{#t} if all given character sets are equal.")
  82. #define FUNC_NAME s_scm_char_set_eq
  83. {
  84. int argnum = 1;
  85. long *cs1_data = NULL;
  86. SCM_VALIDATE_REST_ARGUMENT (char_sets);
  87. while (!scm_is_null (char_sets))
  88. {
  89. SCM csi = SCM_CAR (char_sets);
  90. long *csi_data;
  91. SCM_VALIDATE_SMOB (argnum, csi, charset);
  92. argnum++;
  93. csi_data = (long *) SCM_SMOB_DATA (csi);
  94. if (cs1_data == NULL)
  95. cs1_data = csi_data;
  96. else if (memcmp (cs1_data, csi_data, BYTES_PER_CHARSET) != 0)
  97. return SCM_BOOL_F;
  98. char_sets = SCM_CDR (char_sets);
  99. }
  100. return SCM_BOOL_T;
  101. }
  102. #undef FUNC_NAME
  103. SCM_DEFINE (scm_char_set_leq, "char-set<=", 0, 0, 1,
  104. (SCM char_sets),
  105. "Return @code{#t} if every character set @var{cs}i is a subset\n"
  106. "of character set @var{cs}i+1.")
  107. #define FUNC_NAME s_scm_char_set_leq
  108. {
  109. int argnum = 1;
  110. long *prev_data = NULL;
  111. SCM_VALIDATE_REST_ARGUMENT (char_sets);
  112. while (!scm_is_null (char_sets))
  113. {
  114. SCM csi = SCM_CAR (char_sets);
  115. long *csi_data;
  116. SCM_VALIDATE_SMOB (argnum, csi, charset);
  117. argnum++;
  118. csi_data = (long *) SCM_SMOB_DATA (csi);
  119. if (prev_data)
  120. {
  121. int k;
  122. for (k = 0; k < LONGS_PER_CHARSET; k++)
  123. {
  124. if ((prev_data[k] & csi_data[k]) != prev_data[k])
  125. return SCM_BOOL_F;
  126. }
  127. }
  128. prev_data = csi_data;
  129. char_sets = SCM_CDR (char_sets);
  130. }
  131. return SCM_BOOL_T;
  132. }
  133. #undef FUNC_NAME
  134. SCM_DEFINE (scm_char_set_hash, "char-set-hash", 1, 1, 0,
  135. (SCM cs, SCM bound),
  136. "Compute a hash value for the character set @var{cs}. If\n"
  137. "@var{bound} is given and non-zero, it restricts the\n"
  138. "returned value to the range 0 @dots{} @var{bound - 1}.")
  139. #define FUNC_NAME s_scm_char_set_hash
  140. {
  141. const unsigned long default_bnd = 871;
  142. unsigned long bnd;
  143. long * p;
  144. unsigned long val = 0;
  145. int k;
  146. SCM_VALIDATE_SMOB (1, cs, charset);
  147. if (SCM_UNBNDP (bound))
  148. bnd = default_bnd;
  149. else
  150. {
  151. bnd = scm_to_ulong (bound);
  152. if (bnd == 0)
  153. bnd = default_bnd;
  154. }
  155. p = (long *) SCM_SMOB_DATA (cs);
  156. for (k = 0; k < LONGS_PER_CHARSET; k++)
  157. {
  158. if (p[k] != 0)
  159. val = p[k] + (val << 1);
  160. }
  161. return scm_from_ulong (val % bnd);
  162. }
  163. #undef FUNC_NAME
  164. SCM_DEFINE (scm_char_set_cursor, "char-set-cursor", 1, 0, 0,
  165. (SCM cs),
  166. "Return a cursor into the character set @var{cs}.")
  167. #define FUNC_NAME s_scm_char_set_cursor
  168. {
  169. int idx;
  170. SCM_VALIDATE_SMOB (1, cs, charset);
  171. for (idx = 0; idx < SCM_CHARSET_SIZE; idx++)
  172. {
  173. if (SCM_CHARSET_GET (cs, idx))
  174. break;
  175. }
  176. return SCM_I_MAKINUM (idx);
  177. }
  178. #undef FUNC_NAME
  179. SCM_DEFINE (scm_char_set_ref, "char-set-ref", 2, 0, 0,
  180. (SCM cs, SCM cursor),
  181. "Return the character at the current cursor position\n"
  182. "@var{cursor} in the character set @var{cs}. It is an error to\n"
  183. "pass a cursor for which @code{end-of-char-set?} returns true.")
  184. #define FUNC_NAME s_scm_char_set_ref
  185. {
  186. size_t ccursor = scm_to_size_t (cursor);
  187. SCM_VALIDATE_SMOB (1, cs, charset);
  188. if (ccursor >= SCM_CHARSET_SIZE || !SCM_CHARSET_GET (cs, ccursor))
  189. SCM_MISC_ERROR ("invalid character set cursor: ~A", scm_list_1 (cursor));
  190. return SCM_MAKE_CHAR (ccursor);
  191. }
  192. #undef FUNC_NAME
  193. SCM_DEFINE (scm_char_set_cursor_next, "char-set-cursor-next", 2, 0, 0,
  194. (SCM cs, SCM cursor),
  195. "Advance the character set cursor @var{cursor} to the next\n"
  196. "character in the character set @var{cs}. It is an error if the\n"
  197. "cursor given satisfies @code{end-of-char-set?}.")
  198. #define FUNC_NAME s_scm_char_set_cursor_next
  199. {
  200. size_t ccursor = scm_to_size_t (cursor);
  201. SCM_VALIDATE_SMOB (1, cs, charset);
  202. if (ccursor >= SCM_CHARSET_SIZE || !SCM_CHARSET_GET (cs, ccursor))
  203. SCM_MISC_ERROR ("invalid character set cursor: ~A", scm_list_1 (cursor));
  204. for (ccursor++; ccursor < SCM_CHARSET_SIZE; ccursor++)
  205. {
  206. if (SCM_CHARSET_GET (cs, ccursor))
  207. break;
  208. }
  209. return SCM_I_MAKINUM (ccursor);
  210. }
  211. #undef FUNC_NAME
  212. SCM_DEFINE (scm_end_of_char_set_p, "end-of-char-set?", 1, 0, 0,
  213. (SCM cursor),
  214. "Return @code{#t} if @var{cursor} has reached the end of a\n"
  215. "character set, @code{#f} otherwise.")
  216. #define FUNC_NAME s_scm_end_of_char_set_p
  217. {
  218. size_t ccursor = scm_to_size_t (cursor);
  219. return scm_from_bool (ccursor >= SCM_CHARSET_SIZE);
  220. }
  221. #undef FUNC_NAME
  222. SCM_DEFINE (scm_char_set_fold, "char-set-fold", 3, 0, 0,
  223. (SCM kons, SCM knil, SCM cs),
  224. "Fold the procedure @var{kons} over the character set @var{cs},\n"
  225. "initializing it with @var{knil}.")
  226. #define FUNC_NAME s_scm_char_set_fold
  227. {
  228. int k;
  229. SCM_VALIDATE_PROC (1, kons);
  230. SCM_VALIDATE_SMOB (3, cs, charset);
  231. for (k = 0; k < SCM_CHARSET_SIZE; k++)
  232. if (SCM_CHARSET_GET (cs, k))
  233. {
  234. knil = scm_call_2 (kons, SCM_MAKE_CHAR (k), knil);
  235. }
  236. return knil;
  237. }
  238. #undef FUNC_NAME
  239. SCM_DEFINE (scm_char_set_unfold, "char-set-unfold", 4, 1, 0,
  240. (SCM p, SCM f, SCM g, SCM seed, SCM base_cs),
  241. "This is a fundamental constructor for character sets.\n"
  242. "@itemize @bullet\n"
  243. "@item @var{g} is used to generate a series of ``seed'' values\n"
  244. "from the initial seed: @var{seed}, (@var{g} @var{seed}),\n"
  245. "(@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), @dots{}\n"
  246. "@item @var{p} tells us when to stop -- when it returns true\n"
  247. "when applied to one of the seed values.\n"
  248. "@item @var{f} maps each seed value to a character. These\n"
  249. "characters are added to the base character set @var{base_cs} to\n"
  250. "form the result; @var{base_cs} defaults to the empty set.\n"
  251. "@end itemize")
  252. #define FUNC_NAME s_scm_char_set_unfold
  253. {
  254. SCM result, tmp;
  255. SCM_VALIDATE_PROC (1, p);
  256. SCM_VALIDATE_PROC (2, f);
  257. SCM_VALIDATE_PROC (3, g);
  258. if (!SCM_UNBNDP (base_cs))
  259. {
  260. SCM_VALIDATE_SMOB (5, base_cs, charset);
  261. result = scm_char_set_copy (base_cs);
  262. }
  263. else
  264. result = make_char_set (FUNC_NAME);
  265. tmp = scm_call_1 (p, seed);
  266. while (scm_is_false (tmp))
  267. {
  268. SCM ch = scm_call_1 (f, seed);
  269. if (!SCM_CHARP (ch))
  270. SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (f));
  271. SCM_CHARSET_SET (result, SCM_CHAR (ch));
  272. seed = scm_call_1 (g, seed);
  273. tmp = scm_call_1 (p, seed);
  274. }
  275. return result;
  276. }
  277. #undef FUNC_NAME
  278. SCM_DEFINE (scm_char_set_unfold_x, "char-set-unfold!", 5, 0, 0,
  279. (SCM p, SCM f, SCM g, SCM seed, SCM base_cs),
  280. "This is a fundamental constructor for character sets.\n"
  281. "@itemize @bullet\n"
  282. "@item @var{g} is used to generate a series of ``seed'' values\n"
  283. "from the initial seed: @var{seed}, (@var{g} @var{seed}),\n"
  284. "(@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), @dots{}\n"
  285. "@item @var{p} tells us when to stop -- when it returns true\n"
  286. "when applied to one of the seed values.\n"
  287. "@item @var{f} maps each seed value to a character. These\n"
  288. "characters are added to the base character set @var{base_cs} to\n"
  289. "form the result; @var{base_cs} defaults to the empty set.\n"
  290. "@end itemize")
  291. #define FUNC_NAME s_scm_char_set_unfold_x
  292. {
  293. SCM tmp;
  294. SCM_VALIDATE_PROC (1, p);
  295. SCM_VALIDATE_PROC (2, f);
  296. SCM_VALIDATE_PROC (3, g);
  297. SCM_VALIDATE_SMOB (5, base_cs, charset);
  298. tmp = scm_call_1 (p, seed);
  299. while (scm_is_false (tmp))
  300. {
  301. SCM ch = scm_call_1 (f, seed);
  302. if (!SCM_CHARP (ch))
  303. SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (f));
  304. SCM_CHARSET_SET (base_cs, SCM_CHAR (ch));
  305. seed = scm_call_1 (g, seed);
  306. tmp = scm_call_1 (p, seed);
  307. }
  308. return base_cs;
  309. }
  310. #undef FUNC_NAME
  311. SCM_DEFINE (scm_char_set_for_each, "char-set-for-each", 2, 0, 0,
  312. (SCM proc, SCM cs),
  313. "Apply @var{proc} to every character in the character set\n"
  314. "@var{cs}. The return value is not specified.")
  315. #define FUNC_NAME s_scm_char_set_for_each
  316. {
  317. int k;
  318. SCM_VALIDATE_PROC (1, proc);
  319. SCM_VALIDATE_SMOB (2, cs, charset);
  320. for (k = 0; k < SCM_CHARSET_SIZE; k++)
  321. if (SCM_CHARSET_GET (cs, k))
  322. scm_call_1 (proc, SCM_MAKE_CHAR (k));
  323. return SCM_UNSPECIFIED;
  324. }
  325. #undef FUNC_NAME
  326. SCM_DEFINE (scm_char_set_map, "char-set-map", 2, 0, 0,
  327. (SCM proc, SCM cs),
  328. "Map the procedure @var{proc} over every character in @var{cs}.\n"
  329. "@var{proc} must be a character -> character procedure.")
  330. #define FUNC_NAME s_scm_char_set_map
  331. {
  332. SCM result;
  333. int k;
  334. SCM_VALIDATE_PROC (1, proc);
  335. SCM_VALIDATE_SMOB (2, cs, charset);
  336. result = make_char_set (FUNC_NAME);
  337. for (k = 0; k < SCM_CHARSET_SIZE; k++)
  338. if (SCM_CHARSET_GET (cs, k))
  339. {
  340. SCM ch = scm_call_1 (proc, SCM_MAKE_CHAR (k));
  341. if (!SCM_CHARP (ch))
  342. SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (proc));
  343. SCM_CHARSET_SET (result, SCM_CHAR (ch));
  344. }
  345. return result;
  346. }
  347. #undef FUNC_NAME
  348. SCM_DEFINE (scm_char_set_copy, "char-set-copy", 1, 0, 0,
  349. (SCM cs),
  350. "Return a newly allocated character set containing all\n"
  351. "characters in @var{cs}.")
  352. #define FUNC_NAME s_scm_char_set_copy
  353. {
  354. SCM ret;
  355. long * p1, * p2;
  356. int k;
  357. SCM_VALIDATE_SMOB (1, cs, charset);
  358. ret = make_char_set (FUNC_NAME);
  359. p1 = (long *) SCM_SMOB_DATA (cs);
  360. p2 = (long *) SCM_SMOB_DATA (ret);
  361. for (k = 0; k < LONGS_PER_CHARSET; k++)
  362. p2[k] = p1[k];
  363. return ret;
  364. }
  365. #undef FUNC_NAME
  366. SCM_DEFINE (scm_char_set, "char-set", 0, 0, 1,
  367. (SCM rest),
  368. "Return a character set containing all given characters.")
  369. #define FUNC_NAME s_scm_char_set
  370. {
  371. SCM cs;
  372. long * p;
  373. int argnum = 1;
  374. SCM_VALIDATE_REST_ARGUMENT (rest);
  375. cs = make_char_set (FUNC_NAME);
  376. p = (long *) SCM_SMOB_DATA (cs);
  377. while (!scm_is_null (rest))
  378. {
  379. int c;
  380. SCM_VALIDATE_CHAR_COPY (argnum, SCM_CAR (rest), c);
  381. argnum++;
  382. rest = SCM_CDR (rest);
  383. p[c / SCM_BITS_PER_LONG] |= 1L << (c % SCM_BITS_PER_LONG);
  384. }
  385. return cs;
  386. }
  387. #undef FUNC_NAME
  388. SCM_DEFINE (scm_list_to_char_set, "list->char-set", 1, 1, 0,
  389. (SCM list, SCM base_cs),
  390. "Convert the character list @var{list} to a character set. If\n"
  391. "the character set @var{base_cs} is given, the character in this\n"
  392. "set are also included in the result.")
  393. #define FUNC_NAME s_scm_list_to_char_set
  394. {
  395. SCM cs;
  396. long * p;
  397. SCM_VALIDATE_LIST (1, list);
  398. if (SCM_UNBNDP (base_cs))
  399. cs = make_char_set (FUNC_NAME);
  400. else
  401. {
  402. SCM_VALIDATE_SMOB (2, base_cs, charset);
  403. cs = scm_char_set_copy (base_cs);
  404. }
  405. p = (long *) SCM_SMOB_DATA (cs);
  406. while (!scm_is_null (list))
  407. {
  408. SCM chr = SCM_CAR (list);
  409. int c;
  410. SCM_VALIDATE_CHAR_COPY (0, chr, c);
  411. list = SCM_CDR (list);
  412. p[c / SCM_BITS_PER_LONG] |= 1L << (c % SCM_BITS_PER_LONG);
  413. }
  414. return cs;
  415. }
  416. #undef FUNC_NAME
  417. SCM_DEFINE (scm_list_to_char_set_x, "list->char-set!", 2, 0, 0,
  418. (SCM list, SCM base_cs),
  419. "Convert the character list @var{list} to a character set. The\n"
  420. "characters are added to @var{base_cs} and @var{base_cs} is\n"
  421. "returned.")
  422. #define FUNC_NAME s_scm_list_to_char_set_x
  423. {
  424. long * p;
  425. SCM_VALIDATE_LIST (1, list);
  426. SCM_VALIDATE_SMOB (2, base_cs, charset);
  427. p = (long *) SCM_SMOB_DATA (base_cs);
  428. while (!scm_is_null (list))
  429. {
  430. SCM chr = SCM_CAR (list);
  431. int c;
  432. SCM_VALIDATE_CHAR_COPY (0, chr, c);
  433. list = SCM_CDR (list);
  434. p[c / SCM_BITS_PER_LONG] |= 1L << (c % SCM_BITS_PER_LONG);
  435. }
  436. return base_cs;
  437. }
  438. #undef FUNC_NAME
  439. SCM_DEFINE (scm_string_to_char_set, "string->char-set", 1, 1, 0,
  440. (SCM str, SCM base_cs),
  441. "Convert the string @var{str} to a character set. If the\n"
  442. "character set @var{base_cs} is given, the characters in this\n"
  443. "set are also included in the result.")
  444. #define FUNC_NAME s_scm_string_to_char_set
  445. {
  446. SCM cs;
  447. long * p;
  448. const char * s;
  449. size_t k = 0, len;
  450. SCM_VALIDATE_STRING (1, str);
  451. if (SCM_UNBNDP (base_cs))
  452. cs = make_char_set (FUNC_NAME);
  453. else
  454. {
  455. SCM_VALIDATE_SMOB (2, base_cs, charset);
  456. cs = scm_char_set_copy (base_cs);
  457. }
  458. p = (long *) SCM_SMOB_DATA (cs);
  459. s = scm_i_string_chars (str);
  460. len = scm_i_string_length (str);
  461. while (k < len)
  462. {
  463. int c = s[k++];
  464. p[c / SCM_BITS_PER_LONG] |= 1L << (c % SCM_BITS_PER_LONG);
  465. }
  466. scm_remember_upto_here_1 (str);
  467. return cs;
  468. }
  469. #undef FUNC_NAME
  470. SCM_DEFINE (scm_string_to_char_set_x, "string->char-set!", 2, 0, 0,
  471. (SCM str, SCM base_cs),
  472. "Convert the string @var{str} to a character set. The\n"
  473. "characters from the string are added to @var{base_cs}, and\n"
  474. "@var{base_cs} is returned.")
  475. #define FUNC_NAME s_scm_string_to_char_set_x
  476. {
  477. long * p;
  478. const char * s;
  479. size_t k = 0, len;
  480. SCM_VALIDATE_STRING (1, str);
  481. SCM_VALIDATE_SMOB (2, base_cs, charset);
  482. p = (long *) SCM_SMOB_DATA (base_cs);
  483. s = scm_i_string_chars (str);
  484. len = scm_i_string_length (str);
  485. while (k < len)
  486. {
  487. int c = s[k++];
  488. p[c / SCM_BITS_PER_LONG] |= 1L << (c % SCM_BITS_PER_LONG);
  489. }
  490. scm_remember_upto_here_1 (str);
  491. return base_cs;
  492. }
  493. #undef FUNC_NAME
  494. SCM_DEFINE (scm_char_set_filter, "char-set-filter", 2, 1, 0,
  495. (SCM pred, SCM cs, SCM base_cs),
  496. "Return a character set containing every character from @var{cs}\n"
  497. "so that it satisfies @var{pred}. If provided, the characters\n"
  498. "from @var{base_cs} are added to the result.")
  499. #define FUNC_NAME s_scm_char_set_filter
  500. {
  501. SCM ret;
  502. int k;
  503. long * p;
  504. SCM_VALIDATE_PROC (1, pred);
  505. SCM_VALIDATE_SMOB (2, cs, charset);
  506. if (!SCM_UNBNDP (base_cs))
  507. {
  508. SCM_VALIDATE_SMOB (3, base_cs, charset);
  509. ret = scm_char_set_copy (base_cs);
  510. }
  511. else
  512. ret = make_char_set (FUNC_NAME);
  513. p = (long *) SCM_SMOB_DATA (ret);
  514. for (k = 0; k < SCM_CHARSET_SIZE; k++)
  515. {
  516. if (SCM_CHARSET_GET (cs, k))
  517. {
  518. SCM res = scm_call_1 (pred, SCM_MAKE_CHAR (k));
  519. if (scm_is_true (res))
  520. p[k / SCM_BITS_PER_LONG] |= 1L << (k % SCM_BITS_PER_LONG);
  521. }
  522. }
  523. return ret;
  524. }
  525. #undef FUNC_NAME
  526. SCM_DEFINE (scm_char_set_filter_x, "char-set-filter!", 3, 0, 0,
  527. (SCM pred, SCM cs, SCM base_cs),
  528. "Return a character set containing every character from @var{cs}\n"
  529. "so that it satisfies @var{pred}. The characters are added to\n"
  530. "@var{base_cs} and @var{base_cs} is returned.")
  531. #define FUNC_NAME s_scm_char_set_filter_x
  532. {
  533. int k;
  534. long * p;
  535. SCM_VALIDATE_PROC (1, pred);
  536. SCM_VALIDATE_SMOB (2, cs, charset);
  537. SCM_VALIDATE_SMOB (3, base_cs, charset);
  538. p = (long *) SCM_SMOB_DATA (base_cs);
  539. for (k = 0; k < SCM_CHARSET_SIZE; k++)
  540. {
  541. if (SCM_CHARSET_GET (cs, k))
  542. {
  543. SCM res = scm_call_1 (pred, SCM_MAKE_CHAR (k));
  544. if (scm_is_true (res))
  545. p[k / SCM_BITS_PER_LONG] |= 1L << (k % SCM_BITS_PER_LONG);
  546. }
  547. }
  548. return base_cs;
  549. }
  550. #undef FUNC_NAME
  551. SCM_DEFINE (scm_ucs_range_to_char_set, "ucs-range->char-set", 2, 2, 0,
  552. (SCM lower, SCM upper, SCM error, SCM base_cs),
  553. "Return a character set containing all characters whose\n"
  554. "character codes lie in the half-open range\n"
  555. "[@var{lower},@var{upper}).\n"
  556. "\n"
  557. "If @var{error} is a true value, an error is signalled if the\n"
  558. "specified range contains characters which are not contained in\n"
  559. "the implemented character range. If @var{error} is @code{#f},\n"
  560. "these characters are silently left out of the resultung\n"
  561. "character set.\n"
  562. "\n"
  563. "The characters in @var{base_cs} are added to the result, if\n"
  564. "given.")
  565. #define FUNC_NAME s_scm_ucs_range_to_char_set
  566. {
  567. SCM cs;
  568. size_t clower, cupper;
  569. long * p;
  570. clower = scm_to_size_t (lower);
  571. cupper = scm_to_size_t (upper);
  572. SCM_ASSERT_RANGE (2, upper, cupper >= clower);
  573. if (!SCM_UNBNDP (error))
  574. {
  575. if (scm_is_true (error))
  576. {
  577. SCM_ASSERT_RANGE (1, lower, clower <= SCM_CHARSET_SIZE);
  578. SCM_ASSERT_RANGE (2, upper, cupper <= SCM_CHARSET_SIZE);
  579. }
  580. }
  581. if (clower > SCM_CHARSET_SIZE)
  582. clower = SCM_CHARSET_SIZE;
  583. if (cupper > SCM_CHARSET_SIZE)
  584. cupper = SCM_CHARSET_SIZE;
  585. if (SCM_UNBNDP (base_cs))
  586. cs = make_char_set (FUNC_NAME);
  587. else
  588. {
  589. SCM_VALIDATE_SMOB (4, base_cs, charset);
  590. cs = scm_char_set_copy (base_cs);
  591. }
  592. p = (long *) SCM_SMOB_DATA (cs);
  593. while (clower < cupper)
  594. {
  595. p[clower / SCM_BITS_PER_LONG] |= 1L << (clower % SCM_BITS_PER_LONG);
  596. clower++;
  597. }
  598. return cs;
  599. }
  600. #undef FUNC_NAME
  601. SCM_DEFINE (scm_ucs_range_to_char_set_x, "ucs-range->char-set!", 4, 0, 0,
  602. (SCM lower, SCM upper, SCM error, SCM base_cs),
  603. "Return a character set containing all characters whose\n"
  604. "character codes lie in the half-open range\n"
  605. "[@var{lower},@var{upper}).\n"
  606. "\n"
  607. "If @var{error} is a true value, an error is signalled if the\n"
  608. "specified range contains characters which are not contained in\n"
  609. "the implemented character range. If @var{error} is @code{#f},\n"
  610. "these characters are silently left out of the resultung\n"
  611. "character set.\n"
  612. "\n"
  613. "The characters are added to @var{base_cs} and @var{base_cs} is\n"
  614. "returned.")
  615. #define FUNC_NAME s_scm_ucs_range_to_char_set_x
  616. {
  617. size_t clower, cupper;
  618. long * p;
  619. clower = scm_to_size_t (lower);
  620. cupper = scm_to_size_t (upper);
  621. SCM_ASSERT_RANGE (2, upper, cupper >= clower);
  622. if (scm_is_true (error))
  623. {
  624. SCM_ASSERT_RANGE (1, lower, clower <= SCM_CHARSET_SIZE);
  625. SCM_ASSERT_RANGE (2, upper, cupper <= SCM_CHARSET_SIZE);
  626. }
  627. if (clower > SCM_CHARSET_SIZE)
  628. clower = SCM_CHARSET_SIZE;
  629. if (cupper > SCM_CHARSET_SIZE)
  630. cupper = SCM_CHARSET_SIZE;
  631. p = (long *) SCM_SMOB_DATA (base_cs);
  632. while (clower < cupper)
  633. {
  634. p[clower / SCM_BITS_PER_LONG] |= 1L << (clower % SCM_BITS_PER_LONG);
  635. clower++;
  636. }
  637. return base_cs;
  638. }
  639. #undef FUNC_NAME
  640. SCM_DEFINE (scm_to_char_set, "->char-set", 1, 0, 0,
  641. (SCM x),
  642. "Coerces x into a char-set. @var{x} may be a string, character or char-set. A string is converted to the set of its constituent characters; a character is converted to a singleton set; a char-set is returned as-is.")
  643. #define FUNC_NAME s_scm_to_char_set
  644. {
  645. if (scm_is_string (x))
  646. return scm_string_to_char_set (x, SCM_UNDEFINED);
  647. else if (SCM_CHARP (x))
  648. return scm_char_set (scm_list_1 (x));
  649. else if (SCM_SMOB_PREDICATE (scm_tc16_charset, x))
  650. return x;
  651. else
  652. scm_wrong_type_arg (NULL, 0, x);
  653. }
  654. #undef FUNC_NAME
  655. SCM_DEFINE (scm_char_set_size, "char-set-size", 1, 0, 0,
  656. (SCM cs),
  657. "Return the number of elements in character set @var{cs}.")
  658. #define FUNC_NAME s_scm_char_set_size
  659. {
  660. int k, count = 0;
  661. SCM_VALIDATE_SMOB (1, cs, charset);
  662. for (k = 0; k < SCM_CHARSET_SIZE; k++)
  663. if (SCM_CHARSET_GET (cs, k))
  664. count++;
  665. return SCM_I_MAKINUM (count);
  666. }
  667. #undef FUNC_NAME
  668. SCM_DEFINE (scm_char_set_count, "char-set-count", 2, 0, 0,
  669. (SCM pred, SCM cs),
  670. "Return the number of the elements int the character set\n"
  671. "@var{cs} which satisfy the predicate @var{pred}.")
  672. #define FUNC_NAME s_scm_char_set_count
  673. {
  674. int k, count = 0;
  675. SCM_VALIDATE_PROC (1, pred);
  676. SCM_VALIDATE_SMOB (2, cs, charset);
  677. for (k = 0; k < SCM_CHARSET_SIZE; k++)
  678. if (SCM_CHARSET_GET (cs, k))
  679. {
  680. SCM res = scm_call_1 (pred, SCM_MAKE_CHAR (k));
  681. if (scm_is_true (res))
  682. count++;
  683. }
  684. return SCM_I_MAKINUM (count);
  685. }
  686. #undef FUNC_NAME
  687. SCM_DEFINE (scm_char_set_to_list, "char-set->list", 1, 0, 0,
  688. (SCM cs),
  689. "Return a list containing the elements of the character set\n"
  690. "@var{cs}.")
  691. #define FUNC_NAME s_scm_char_set_to_list
  692. {
  693. int k;
  694. SCM result = SCM_EOL;
  695. SCM_VALIDATE_SMOB (1, cs, charset);
  696. for (k = SCM_CHARSET_SIZE; k > 0; k--)
  697. if (SCM_CHARSET_GET (cs, k - 1))
  698. result = scm_cons (SCM_MAKE_CHAR (k - 1), result);
  699. return result;
  700. }
  701. #undef FUNC_NAME
  702. SCM_DEFINE (scm_char_set_to_string, "char-set->string", 1, 0, 0,
  703. (SCM cs),
  704. "Return a string containing the elements of the character set\n"
  705. "@var{cs}. The order in which the characters are placed in the\n"
  706. "string is not defined.")
  707. #define FUNC_NAME s_scm_char_set_to_string
  708. {
  709. int k;
  710. int count = 0;
  711. int idx = 0;
  712. SCM result;
  713. char * p;
  714. SCM_VALIDATE_SMOB (1, cs, charset);
  715. for (k = 0; k < SCM_CHARSET_SIZE; k++)
  716. if (SCM_CHARSET_GET (cs, k))
  717. count++;
  718. result = scm_i_make_string (count, &p);
  719. for (k = 0; k < SCM_CHARSET_SIZE; k++)
  720. if (SCM_CHARSET_GET (cs, k))
  721. p[idx++] = k;
  722. return result;
  723. }
  724. #undef FUNC_NAME
  725. SCM_DEFINE (scm_char_set_contains_p, "char-set-contains?", 2, 0, 0,
  726. (SCM cs, SCM ch),
  727. "Return @code{#t} iff the character @var{ch} is contained in the\n"
  728. "character set @var{cs}.")
  729. #define FUNC_NAME s_scm_char_set_contains_p
  730. {
  731. SCM_VALIDATE_SMOB (1, cs, charset);
  732. SCM_VALIDATE_CHAR (2, ch);
  733. return scm_from_bool (SCM_CHARSET_GET (cs, SCM_CHAR (ch)));
  734. }
  735. #undef FUNC_NAME
  736. SCM_DEFINE (scm_char_set_every, "char-set-every", 2, 0, 0,
  737. (SCM pred, SCM cs),
  738. "Return a true value if every character in the character set\n"
  739. "@var{cs} satisfies the predicate @var{pred}.")
  740. #define FUNC_NAME s_scm_char_set_every
  741. {
  742. int k;
  743. SCM res = SCM_BOOL_T;
  744. SCM_VALIDATE_PROC (1, pred);
  745. SCM_VALIDATE_SMOB (2, cs, charset);
  746. for (k = 0; k < SCM_CHARSET_SIZE; k++)
  747. if (SCM_CHARSET_GET (cs, k))
  748. {
  749. res = scm_call_1 (pred, SCM_MAKE_CHAR (k));
  750. if (scm_is_false (res))
  751. return res;
  752. }
  753. return res;
  754. }
  755. #undef FUNC_NAME
  756. SCM_DEFINE (scm_char_set_any, "char-set-any", 2, 0, 0,
  757. (SCM pred, SCM cs),
  758. "Return a true value if any character in the character set\n"
  759. "@var{cs} satisfies the predicate @var{pred}.")
  760. #define FUNC_NAME s_scm_char_set_any
  761. {
  762. int k;
  763. SCM_VALIDATE_PROC (1, pred);
  764. SCM_VALIDATE_SMOB (2, cs, charset);
  765. for (k = 0; k < SCM_CHARSET_SIZE; k++)
  766. if (SCM_CHARSET_GET (cs, k))
  767. {
  768. SCM res = scm_call_1 (pred, SCM_MAKE_CHAR (k));
  769. if (scm_is_true (res))
  770. return res;
  771. }
  772. return SCM_BOOL_F;
  773. }
  774. #undef FUNC_NAME
  775. SCM_DEFINE (scm_char_set_adjoin, "char-set-adjoin", 1, 0, 1,
  776. (SCM cs, SCM rest),
  777. "Add all character arguments to the first argument, which must\n"
  778. "be a character set.")
  779. #define FUNC_NAME s_scm_char_set_adjoin
  780. {
  781. long * p;
  782. SCM_VALIDATE_SMOB (1, cs, charset);
  783. SCM_VALIDATE_REST_ARGUMENT (rest);
  784. cs = scm_char_set_copy (cs);
  785. p = (long *) SCM_SMOB_DATA (cs);
  786. while (!scm_is_null (rest))
  787. {
  788. SCM chr = SCM_CAR (rest);
  789. int c;
  790. SCM_VALIDATE_CHAR_COPY (1, chr, c);
  791. rest = SCM_CDR (rest);
  792. p[c / SCM_BITS_PER_LONG] |= 1L << (c % SCM_BITS_PER_LONG);
  793. }
  794. return cs;
  795. }
  796. #undef FUNC_NAME
  797. SCM_DEFINE (scm_char_set_delete, "char-set-delete", 1, 0, 1,
  798. (SCM cs, SCM rest),
  799. "Delete all character arguments from the first argument, which\n"
  800. "must be a character set.")
  801. #define FUNC_NAME s_scm_char_set_delete
  802. {
  803. long * p;
  804. SCM_VALIDATE_SMOB (1, cs, charset);
  805. SCM_VALIDATE_REST_ARGUMENT (rest);
  806. cs = scm_char_set_copy (cs);
  807. p = (long *) SCM_SMOB_DATA (cs);
  808. while (!scm_is_null (rest))
  809. {
  810. SCM chr = SCM_CAR (rest);
  811. int c;
  812. SCM_VALIDATE_CHAR_COPY (1, chr, c);
  813. rest = SCM_CDR (rest);
  814. p[c / SCM_BITS_PER_LONG] &= ~(1L << (c % SCM_BITS_PER_LONG));
  815. }
  816. return cs;
  817. }
  818. #undef FUNC_NAME
  819. SCM_DEFINE (scm_char_set_adjoin_x, "char-set-adjoin!", 1, 0, 1,
  820. (SCM cs, SCM rest),
  821. "Add all character arguments to the first argument, which must\n"
  822. "be a character set.")
  823. #define FUNC_NAME s_scm_char_set_adjoin_x
  824. {
  825. long * p;
  826. SCM_VALIDATE_SMOB (1, cs, charset);
  827. SCM_VALIDATE_REST_ARGUMENT (rest);
  828. p = (long *) SCM_SMOB_DATA (cs);
  829. while (!scm_is_null (rest))
  830. {
  831. SCM chr = SCM_CAR (rest);
  832. int c;
  833. SCM_VALIDATE_CHAR_COPY (1, chr, c);
  834. rest = SCM_CDR (rest);
  835. p[c / SCM_BITS_PER_LONG] |= 1L << (c % SCM_BITS_PER_LONG);
  836. }
  837. return cs;
  838. }
  839. #undef FUNC_NAME
  840. SCM_DEFINE (scm_char_set_delete_x, "char-set-delete!", 1, 0, 1,
  841. (SCM cs, SCM rest),
  842. "Delete all character arguments from the first argument, which\n"
  843. "must be a character set.")
  844. #define FUNC_NAME s_scm_char_set_delete_x
  845. {
  846. long * p;
  847. SCM_VALIDATE_SMOB (1, cs, charset);
  848. SCM_VALIDATE_REST_ARGUMENT (rest);
  849. p = (long *) SCM_SMOB_DATA (cs);
  850. while (!scm_is_null (rest))
  851. {
  852. SCM chr = SCM_CAR (rest);
  853. int c;
  854. SCM_VALIDATE_CHAR_COPY (1, chr, c);
  855. rest = SCM_CDR (rest);
  856. p[c / SCM_BITS_PER_LONG] &= ~(1L << (c % SCM_BITS_PER_LONG));
  857. }
  858. return cs;
  859. }
  860. #undef FUNC_NAME
  861. SCM_DEFINE (scm_char_set_complement, "char-set-complement", 1, 0, 0,
  862. (SCM cs),
  863. "Return the complement of the character set @var{cs}.")
  864. #define FUNC_NAME s_scm_char_set_complement
  865. {
  866. int k;
  867. SCM res;
  868. long * p, * q;
  869. SCM_VALIDATE_SMOB (1, cs, charset);
  870. res = make_char_set (FUNC_NAME);
  871. p = (long *) SCM_SMOB_DATA (res);
  872. q = (long *) SCM_SMOB_DATA (cs);
  873. for (k = 0; k < LONGS_PER_CHARSET; k++)
  874. p[k] = ~q[k];
  875. return res;
  876. }
  877. #undef FUNC_NAME
  878. SCM_DEFINE (scm_char_set_union, "char-set-union", 0, 0, 1,
  879. (SCM rest),
  880. "Return the union of all argument character sets.")
  881. #define FUNC_NAME s_scm_char_set_union
  882. {
  883. int c = 1;
  884. SCM res;
  885. long * p;
  886. SCM_VALIDATE_REST_ARGUMENT (rest);
  887. res = make_char_set (FUNC_NAME);
  888. p = (long *) SCM_SMOB_DATA (res);
  889. while (!scm_is_null (rest))
  890. {
  891. int k;
  892. SCM cs = SCM_CAR (rest);
  893. SCM_VALIDATE_SMOB (c, cs, charset);
  894. c++;
  895. rest = SCM_CDR (rest);
  896. for (k = 0; k < LONGS_PER_CHARSET; k++)
  897. p[k] |= ((long *) SCM_SMOB_DATA (cs))[k];
  898. }
  899. return res;
  900. }
  901. #undef FUNC_NAME
  902. SCM_DEFINE (scm_char_set_intersection, "char-set-intersection", 0, 0, 1,
  903. (SCM rest),
  904. "Return the intersection of all argument character sets.")
  905. #define FUNC_NAME s_scm_char_set_intersection
  906. {
  907. SCM res;
  908. SCM_VALIDATE_REST_ARGUMENT (rest);
  909. if (scm_is_null (rest))
  910. res = make_char_set (FUNC_NAME);
  911. else
  912. {
  913. long *p;
  914. int argnum = 2;
  915. res = scm_char_set_copy (SCM_CAR (rest));
  916. p = (long *) SCM_SMOB_DATA (res);
  917. rest = SCM_CDR (rest);
  918. while (scm_is_pair (rest))
  919. {
  920. int k;
  921. SCM cs = SCM_CAR (rest);
  922. long *cs_data;
  923. SCM_VALIDATE_SMOB (argnum, cs, charset);
  924. argnum++;
  925. cs_data = (long *) SCM_SMOB_DATA (cs);
  926. rest = SCM_CDR (rest);
  927. for (k = 0; k < LONGS_PER_CHARSET; k++)
  928. p[k] &= cs_data[k];
  929. }
  930. }
  931. return res;
  932. }
  933. #undef FUNC_NAME
  934. SCM_DEFINE (scm_char_set_difference, "char-set-difference", 1, 0, 1,
  935. (SCM cs1, SCM rest),
  936. "Return the difference of all argument character sets.")
  937. #define FUNC_NAME s_scm_char_set_difference
  938. {
  939. int c = 2;
  940. SCM res;
  941. long * p;
  942. SCM_VALIDATE_SMOB (1, cs1, charset);
  943. SCM_VALIDATE_REST_ARGUMENT (rest);
  944. res = scm_char_set_copy (cs1);
  945. p = (long *) SCM_SMOB_DATA (res);
  946. while (!scm_is_null (rest))
  947. {
  948. int k;
  949. SCM cs = SCM_CAR (rest);
  950. SCM_VALIDATE_SMOB (c, cs, charset);
  951. c++;
  952. rest = SCM_CDR (rest);
  953. for (k = 0; k < LONGS_PER_CHARSET; k++)
  954. p[k] &= ~((long *) SCM_SMOB_DATA (cs))[k];
  955. }
  956. return res;
  957. }
  958. #undef FUNC_NAME
  959. SCM_DEFINE (scm_char_set_xor, "char-set-xor", 0, 0, 1,
  960. (SCM rest),
  961. "Return the exclusive-or of all argument character sets.")
  962. #define FUNC_NAME s_scm_char_set_xor
  963. {
  964. SCM res;
  965. SCM_VALIDATE_REST_ARGUMENT (rest);
  966. if (scm_is_null (rest))
  967. res = make_char_set (FUNC_NAME);
  968. else
  969. {
  970. int argnum = 2;
  971. long * p;
  972. res = scm_char_set_copy (SCM_CAR (rest));
  973. p = (long *) SCM_SMOB_DATA (res);
  974. rest = SCM_CDR (rest);
  975. while (scm_is_pair (rest))
  976. {
  977. SCM cs = SCM_CAR (rest);
  978. long *cs_data;
  979. int k;
  980. SCM_VALIDATE_SMOB (argnum, cs, charset);
  981. argnum++;
  982. cs_data = (long *) SCM_SMOB_DATA (cs);
  983. rest = SCM_CDR (rest);
  984. for (k = 0; k < LONGS_PER_CHARSET; k++)
  985. p[k] ^= cs_data[k];
  986. }
  987. }
  988. return res;
  989. }
  990. #undef FUNC_NAME
  991. SCM_DEFINE (scm_char_set_diff_plus_intersection, "char-set-diff+intersection", 1, 0, 1,
  992. (SCM cs1, SCM rest),
  993. "Return the difference and the intersection of all argument\n"
  994. "character sets.")
  995. #define FUNC_NAME s_scm_char_set_diff_plus_intersection
  996. {
  997. int c = 2;
  998. SCM res1, res2;
  999. long * p, * q;
  1000. SCM_VALIDATE_SMOB (1, cs1, charset);
  1001. SCM_VALIDATE_REST_ARGUMENT (rest);
  1002. res1 = scm_char_set_copy (cs1);
  1003. res2 = make_char_set (FUNC_NAME);
  1004. p = (long *) SCM_SMOB_DATA (res1);
  1005. q = (long *) SCM_SMOB_DATA (res2);
  1006. while (!scm_is_null (rest))
  1007. {
  1008. int k;
  1009. SCM cs = SCM_CAR (rest);
  1010. long *r;
  1011. SCM_VALIDATE_SMOB (c, cs, charset);
  1012. c++;
  1013. r = (long *) SCM_SMOB_DATA (cs);
  1014. for (k = 0; k < LONGS_PER_CHARSET; k++)
  1015. {
  1016. q[k] |= p[k] & r[k];
  1017. p[k] &= ~r[k];
  1018. }
  1019. rest = SCM_CDR (rest);
  1020. }
  1021. return scm_values (scm_list_2 (res1, res2));
  1022. }
  1023. #undef FUNC_NAME
  1024. SCM_DEFINE (scm_char_set_complement_x, "char-set-complement!", 1, 0, 0,
  1025. (SCM cs),
  1026. "Return the complement of the character set @var{cs}.")
  1027. #define FUNC_NAME s_scm_char_set_complement_x
  1028. {
  1029. int k;
  1030. long * p;
  1031. SCM_VALIDATE_SMOB (1, cs, charset);
  1032. p = (long *) SCM_SMOB_DATA (cs);
  1033. for (k = 0; k < LONGS_PER_CHARSET; k++)
  1034. p[k] = ~p[k];
  1035. return cs;
  1036. }
  1037. #undef FUNC_NAME
  1038. SCM_DEFINE (scm_char_set_union_x, "char-set-union!", 1, 0, 1,
  1039. (SCM cs1, SCM rest),
  1040. "Return the union of all argument character sets.")
  1041. #define FUNC_NAME s_scm_char_set_union_x
  1042. {
  1043. int c = 2;
  1044. long * p;
  1045. SCM_VALIDATE_SMOB (1, cs1, charset);
  1046. SCM_VALIDATE_REST_ARGUMENT (rest);
  1047. p = (long *) SCM_SMOB_DATA (cs1);
  1048. while (!scm_is_null (rest))
  1049. {
  1050. int k;
  1051. SCM cs = SCM_CAR (rest);
  1052. SCM_VALIDATE_SMOB (c, cs, charset);
  1053. c++;
  1054. rest = SCM_CDR (rest);
  1055. for (k = 0; k < LONGS_PER_CHARSET; k++)
  1056. p[k] |= ((long *) SCM_SMOB_DATA (cs))[k];
  1057. }
  1058. return cs1;
  1059. }
  1060. #undef FUNC_NAME
  1061. SCM_DEFINE (scm_char_set_intersection_x, "char-set-intersection!", 1, 0, 1,
  1062. (SCM cs1, SCM rest),
  1063. "Return the intersection of all argument character sets.")
  1064. #define FUNC_NAME s_scm_char_set_intersection_x
  1065. {
  1066. int c = 2;
  1067. long * p;
  1068. SCM_VALIDATE_SMOB (1, cs1, charset);
  1069. SCM_VALIDATE_REST_ARGUMENT (rest);
  1070. p = (long *) SCM_SMOB_DATA (cs1);
  1071. while (!scm_is_null (rest))
  1072. {
  1073. int k;
  1074. SCM cs = SCM_CAR (rest);
  1075. SCM_VALIDATE_SMOB (c, cs, charset);
  1076. c++;
  1077. rest = SCM_CDR (rest);
  1078. for (k = 0; k < LONGS_PER_CHARSET; k++)
  1079. p[k] &= ((long *) SCM_SMOB_DATA (cs))[k];
  1080. }
  1081. return cs1;
  1082. }
  1083. #undef FUNC_NAME
  1084. SCM_DEFINE (scm_char_set_difference_x, "char-set-difference!", 1, 0, 1,
  1085. (SCM cs1, SCM rest),
  1086. "Return the difference of all argument character sets.")
  1087. #define FUNC_NAME s_scm_char_set_difference_x
  1088. {
  1089. int c = 2;
  1090. long * p;
  1091. SCM_VALIDATE_SMOB (1, cs1, charset);
  1092. SCM_VALIDATE_REST_ARGUMENT (rest);
  1093. p = (long *) SCM_SMOB_DATA (cs1);
  1094. while (!scm_is_null (rest))
  1095. {
  1096. int k;
  1097. SCM cs = SCM_CAR (rest);
  1098. SCM_VALIDATE_SMOB (c, cs, charset);
  1099. c++;
  1100. rest = SCM_CDR (rest);
  1101. for (k = 0; k < LONGS_PER_CHARSET; k++)
  1102. p[k] &= ~((long *) SCM_SMOB_DATA (cs))[k];
  1103. }
  1104. return cs1;
  1105. }
  1106. #undef FUNC_NAME
  1107. SCM_DEFINE (scm_char_set_xor_x, "char-set-xor!", 1, 0, 1,
  1108. (SCM cs1, SCM rest),
  1109. "Return the exclusive-or of all argument character sets.")
  1110. #define FUNC_NAME s_scm_char_set_xor_x
  1111. {
  1112. /* a side-effecting variant should presumably give consistent results:
  1113. (define a (char-set #\a))
  1114. (char-set-xor a a a) -> char set #\a
  1115. (char-set-xor! a a a) -> char set #\a
  1116. */
  1117. return scm_char_set_xor (scm_cons (cs1, rest));
  1118. #if 0
  1119. /* this would give (char-set-xor! a a a) -> empty char set. */
  1120. int c = 2;
  1121. long * p;
  1122. SCM_VALIDATE_SMOB (1, cs1, charset);
  1123. SCM_VALIDATE_REST_ARGUMENT (rest);
  1124. p = (long *) SCM_SMOB_DATA (cs1);
  1125. while (!scm_is_null (rest))
  1126. {
  1127. int k;
  1128. SCM cs = SCM_CAR (rest);
  1129. SCM_VALIDATE_SMOB (c, cs, charset);
  1130. c++;
  1131. rest = SCM_CDR (rest);
  1132. for (k = 0; k < LONGS_PER_CHARSET; k++)
  1133. p[k] ^= ((long *) SCM_SMOB_DATA (cs))[k];
  1134. }
  1135. return cs1;
  1136. #endif
  1137. }
  1138. #undef FUNC_NAME
  1139. SCM_DEFINE (scm_char_set_diff_plus_intersection_x, "char-set-diff+intersection!", 2, 0, 1,
  1140. (SCM cs1, SCM cs2, SCM rest),
  1141. "Return the difference and the intersection of all argument\n"
  1142. "character sets.")
  1143. #define FUNC_NAME s_scm_char_set_diff_plus_intersection_x
  1144. {
  1145. int c = 3;
  1146. long * p, * q;
  1147. int k;
  1148. SCM_VALIDATE_SMOB (1, cs1, charset);
  1149. SCM_VALIDATE_SMOB (2, cs2, charset);
  1150. SCM_VALIDATE_REST_ARGUMENT (rest);
  1151. p = (long *) SCM_SMOB_DATA (cs1);
  1152. q = (long *) SCM_SMOB_DATA (cs2);
  1153. if (p == q)
  1154. {
  1155. /* (char-set-diff+intersection! a a ...): can't share storage,
  1156. but we know the answer without checking for further
  1157. arguments. */
  1158. return scm_values (scm_list_2 (make_char_set (FUNC_NAME), cs1));
  1159. }
  1160. for (k = 0; k < LONGS_PER_CHARSET; k++)
  1161. {
  1162. long t = p[k];
  1163. p[k] &= ~q[k];
  1164. q[k] = t & q[k];
  1165. }
  1166. while (!scm_is_null (rest))
  1167. {
  1168. SCM cs = SCM_CAR (rest);
  1169. long *r;
  1170. SCM_VALIDATE_SMOB (c, cs, charset);
  1171. c++;
  1172. r = (long *) SCM_SMOB_DATA (cs);
  1173. for (k = 0; k < LONGS_PER_CHARSET; k++)
  1174. {
  1175. q[k] |= p[k] & r[k];
  1176. p[k] &= ~r[k];
  1177. }
  1178. rest = SCM_CDR (rest);
  1179. }
  1180. return scm_values (scm_list_2 (cs1, cs2));
  1181. }
  1182. #undef FUNC_NAME
  1183. /* Standard character sets. */
  1184. SCM scm_char_set_lower_case;
  1185. SCM scm_char_set_upper_case;
  1186. SCM scm_char_set_title_case;
  1187. SCM scm_char_set_letter;
  1188. SCM scm_char_set_digit;
  1189. SCM scm_char_set_letter_and_digit;
  1190. SCM scm_char_set_graphic;
  1191. SCM scm_char_set_printing;
  1192. SCM scm_char_set_whitespace;
  1193. SCM scm_char_set_iso_control;
  1194. SCM scm_char_set_punctuation;
  1195. SCM scm_char_set_symbol;
  1196. SCM scm_char_set_hex_digit;
  1197. SCM scm_char_set_blank;
  1198. SCM scm_char_set_ascii;
  1199. SCM scm_char_set_empty;
  1200. SCM scm_char_set_full;
  1201. /* Create an empty character set and return it after binding it to NAME. */
  1202. static inline SCM
  1203. define_charset (const char *name)
  1204. {
  1205. SCM cs = make_char_set (NULL);
  1206. scm_c_define (name, cs);
  1207. return scm_permanent_object (cs);
  1208. }
  1209. /* Membership predicates for the various char sets.
  1210. XXX: The `punctuation' and `symbol' char sets have no direct equivalent in
  1211. <ctype.h>. Thus, the predicates below yield correct results for ASCII,
  1212. but they do not provide the result described by the SRFI for Latin-1. The
  1213. correct Latin-1 result could only be obtained by hard-coding the
  1214. characters listed by the SRFI, but the problem would remain for other
  1215. 8-bit charsets.
  1216. Similarly, character 0xA0 in Latin-1 (unbreakable space, `#\0240') should
  1217. be part of `char-set:blank'. However, glibc's current (2006/09) Latin-1
  1218. locales (which use the ISO 14652 "i18n" FDCC-set) do not consider it
  1219. `blank' so it ends up in `char-set:punctuation'. */
  1220. #ifdef HAVE_ISBLANK
  1221. # define CSET_BLANK_PRED(c) (isblank (c))
  1222. #else
  1223. # define CSET_BLANK_PRED(c) \
  1224. (((c) == ' ') || ((c) == '\t'))
  1225. #endif
  1226. #define CSET_SYMBOL_PRED(c) \
  1227. (((c) != '\0') && (strchr ("$+<=>^`|~", (c)) != NULL))
  1228. #define CSET_PUNCT_PRED(c) \
  1229. ((ispunct (c)) && (!CSET_SYMBOL_PRED (c)))
  1230. #define CSET_LOWER_PRED(c) (islower (c))
  1231. #define CSET_UPPER_PRED(c) (isupper (c))
  1232. #define CSET_LETTER_PRED(c) (isalpha (c))
  1233. #define CSET_DIGIT_PRED(c) (isdigit (c))
  1234. #define CSET_WHITESPACE_PRED(c) (isspace (c))
  1235. #define CSET_CONTROL_PRED(c) (iscntrl (c))
  1236. #define CSET_HEX_DIGIT_PRED(c) (isxdigit (c))
  1237. #define CSET_ASCII_PRED(c) (isascii (c))
  1238. /* Some char sets are explicitly defined by the SRFI as a union of other char
  1239. sets so we try to follow this closely. */
  1240. #define CSET_LETTER_AND_DIGIT_PRED(c) \
  1241. (CSET_LETTER_PRED (c) || CSET_DIGIT_PRED (c))
  1242. #define CSET_GRAPHIC_PRED(c) \
  1243. (CSET_LETTER_PRED (c) || CSET_DIGIT_PRED (c) \
  1244. || CSET_PUNCT_PRED (c) || CSET_SYMBOL_PRED (c))
  1245. #define CSET_PRINTING_PRED(c) \
  1246. (CSET_GRAPHIC_PRED (c) || CSET_WHITESPACE_PRED (c))
  1247. /* False and true predicates. */
  1248. #define CSET_TRUE_PRED(c) (1)
  1249. #define CSET_FALSE_PRED(c) (0)
  1250. /* Compute the contents of all the standard character sets. Computation may
  1251. need to be re-done at `setlocale'-time because some char sets (e.g.,
  1252. `char-set:letter') need to reflect the character set supported by Guile.
  1253. For instance, at startup time, the "C" locale is used, thus Guile supports
  1254. only ASCII; therefore, `char-set:letter' only contains English letters.
  1255. The user can change this by invoking `setlocale' and specifying a locale
  1256. with an 8-bit charset, thereby augmenting some of the SRFI-14 standard
  1257. character sets.
  1258. This works because some of the predicates used below to construct
  1259. character sets (e.g., `isalpha(3)') are locale-dependent (so
  1260. charset-dependent, though generally not language-dependent). For details,
  1261. please see the `guile-devel' mailing list archive of September 2006. */
  1262. void
  1263. scm_srfi_14_compute_char_sets (void)
  1264. {
  1265. #define UPDATE_CSET(c, cset, pred) \
  1266. do \
  1267. { \
  1268. if (pred (c)) \
  1269. SCM_CHARSET_SET ((cset), (c)); \
  1270. else \
  1271. SCM_CHARSET_UNSET ((cset), (c)); \
  1272. } \
  1273. while (0)
  1274. register int ch;
  1275. for (ch = 0; ch < 256; ch++)
  1276. {
  1277. UPDATE_CSET (ch, scm_char_set_upper_case, CSET_UPPER_PRED);
  1278. UPDATE_CSET (ch, scm_char_set_lower_case, CSET_LOWER_PRED);
  1279. UPDATE_CSET (ch, scm_char_set_title_case, CSET_FALSE_PRED);
  1280. UPDATE_CSET (ch, scm_char_set_letter, CSET_LETTER_PRED);
  1281. UPDATE_CSET (ch, scm_char_set_digit, CSET_DIGIT_PRED);
  1282. UPDATE_CSET (ch, scm_char_set_letter_and_digit,
  1283. CSET_LETTER_AND_DIGIT_PRED);
  1284. UPDATE_CSET (ch, scm_char_set_graphic, CSET_GRAPHIC_PRED);
  1285. UPDATE_CSET (ch, scm_char_set_printing, CSET_PRINTING_PRED);
  1286. UPDATE_CSET (ch, scm_char_set_whitespace, CSET_WHITESPACE_PRED);
  1287. UPDATE_CSET (ch, scm_char_set_iso_control, CSET_CONTROL_PRED);
  1288. UPDATE_CSET (ch, scm_char_set_punctuation, CSET_PUNCT_PRED);
  1289. UPDATE_CSET (ch, scm_char_set_symbol, CSET_SYMBOL_PRED);
  1290. UPDATE_CSET (ch, scm_char_set_hex_digit, CSET_HEX_DIGIT_PRED);
  1291. UPDATE_CSET (ch, scm_char_set_blank, CSET_BLANK_PRED);
  1292. UPDATE_CSET (ch, scm_char_set_ascii, CSET_ASCII_PRED);
  1293. UPDATE_CSET (ch, scm_char_set_empty, CSET_FALSE_PRED);
  1294. UPDATE_CSET (ch, scm_char_set_full, CSET_TRUE_PRED);
  1295. }
  1296. #undef UPDATE_CSET
  1297. }
  1298. void
  1299. scm_init_srfi_14 (void)
  1300. {
  1301. scm_tc16_charset = scm_make_smob_type ("character-set",
  1302. BYTES_PER_CHARSET);
  1303. scm_set_smob_free (scm_tc16_charset, charset_free);
  1304. scm_set_smob_print (scm_tc16_charset, charset_print);
  1305. scm_char_set_upper_case = define_charset ("char-set:upper-case");
  1306. scm_char_set_lower_case = define_charset ("char-set:lower-case");
  1307. scm_char_set_title_case = define_charset ("char-set:title-case");
  1308. scm_char_set_letter = define_charset ("char-set:letter");
  1309. scm_char_set_digit = define_charset ("char-set:digit");
  1310. scm_char_set_letter_and_digit = define_charset ("char-set:letter+digit");
  1311. scm_char_set_graphic = define_charset ("char-set:graphic");
  1312. scm_char_set_printing = define_charset ("char-set:printing");
  1313. scm_char_set_whitespace = define_charset ("char-set:whitespace");
  1314. scm_char_set_iso_control = define_charset ("char-set:iso-control");
  1315. scm_char_set_punctuation = define_charset ("char-set:punctuation");
  1316. scm_char_set_symbol = define_charset ("char-set:symbol");
  1317. scm_char_set_hex_digit = define_charset ("char-set:hex-digit");
  1318. scm_char_set_blank = define_charset ("char-set:blank");
  1319. scm_char_set_ascii = define_charset ("char-set:ascii");
  1320. scm_char_set_empty = define_charset ("char-set:empty");
  1321. scm_char_set_full = define_charset ("char-set:full");
  1322. scm_srfi_14_compute_char_sets ();
  1323. #include "libguile/srfi-14.x"
  1324. }
  1325. /* End of srfi-14.c. */