1 /* srfi-14.c --- SRFI-14 procedures for Guile
3 * Copyright (C) 2001 Free Software Foundation, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software; see the file COPYING. If not, write to
17 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307 USA
20 * As a special exception, the Free Software Foundation gives
21 * permission for additional uses of the text contained in its release
24 * The exception is that, if you link the GUILE library with other
25 * files to produce an executable, this does not by itself cause the
26 * resulting executable to be covered by the GNU General Public
27 * License. Your use of that executable is in no way restricted on
28 * account of linking the GUILE library code into it.
30 * This exception does not however invalidate any other reasons why
31 * the executable file might be covered by the GNU General Public
34 * This exception applies only to the code released by the Free
35 * Software Foundation under the name GUILE. If you copy code from
36 * other Free Software Foundation releases into a copy of GUILE, as
37 * the General Public License permits, the exception does not apply to
38 * the code that you add in this way. To avoid misleading anyone as
39 * to the status of such modified files, you must delete this
40 * exception notice from them.
42 * If you write modifications of your own for GUILE, it is your choice
43 * whether to permit this exception to apply to your modifications.
44 * If you do not wish that, delete this exception notice. */
54 #define SCM_CHARSET_SET(cs, idx) \
55 (((long *) SCM_SMOB_DATA (cs))[(idx) / SCM_BITS_PER_LONG] |= \
56 (1L << ((idx) % SCM_BITS_PER_LONG)))
58 #define BYTES_PER_CHARSET (SCM_CHARSET_SIZE / 8)
59 #define LONGS_PER_CHARSET (SCM_CHARSET_SIZE / SCM_BITS_PER_LONG)
62 /* Smob type code for character sets. */
63 int scm_tc16_charset
= 0;
66 /* Smob print hook for character sets. */
68 charset_print (SCM charset
, SCM port
, scm_print_state
*pstate SCM_UNUSED
)
73 scm_puts ("#<charset {", port
);
74 for (i
= 0; i
< SCM_CHARSET_SIZE
; i
++)
75 if (SCM_CHARSET_GET (charset
, i
))
81 scm_write (SCM_MAKE_CHAR (i
), port
);
83 scm_puts ("}>", port
);
88 /* Smob free hook for character sets. */
90 charset_free (SCM charset
)
92 return scm_smob_free (charset
);
96 /* Create a new, empty character set. */
98 make_char_set (const char * func_name
)
102 p
= scm_gc_malloc (BYTES_PER_CHARSET
, "character-set");
103 memset (p
, 0, BYTES_PER_CHARSET
);
104 SCM_RETURN_NEWSMOB (scm_tc16_charset
, p
);
108 SCM_DEFINE (scm_char_set_p
, "char-set?", 1, 0, 0,
110 "Return @code{#t} if @var{obj} is a character set, @code{#f}\n"
112 #define FUNC_NAME s_scm_char_set_p
114 return SCM_BOOL (SCM_SMOB_PREDICATE (scm_tc16_charset
, obj
));
119 SCM_DEFINE (scm_char_set_eq
, "char-set=", 0, 0, 1,
121 "Return @code{#t} if all given character sets are equal.")
122 #define FUNC_NAME s_scm_char_set_eq
125 long *cs1_data
= NULL
;
127 SCM_VALIDATE_REST_ARGUMENT (char_sets
);
129 while (!SCM_NULLP (char_sets
))
131 SCM csi
= SCM_CAR (char_sets
);
134 SCM_VALIDATE_SMOB (argnum
, csi
, charset
);
136 csi_data
= (long *) SCM_SMOB_DATA (csi
);
137 if (cs1_data
== NULL
)
139 else if (memcmp (cs1_data
, csi_data
, BYTES_PER_CHARSET
) != 0)
141 char_sets
= SCM_CDR (char_sets
);
148 SCM_DEFINE (scm_char_set_leq
, "char-set<=", 0, 0, 1,
150 "Return @code{#t} if every character set @var{cs}i is a subset\n"
151 "of character set @var{cs}i+1.")
152 #define FUNC_NAME s_scm_char_set_leq
155 long *prev_data
= NULL
;
157 SCM_VALIDATE_REST_ARGUMENT (char_sets
);
159 while (!SCM_NULLP (char_sets
))
161 SCM csi
= SCM_CAR (char_sets
);
164 SCM_VALIDATE_SMOB (argnum
, csi
, charset
);
166 csi_data
= (long *) SCM_SMOB_DATA (csi
);
171 for (k
= 0; k
< LONGS_PER_CHARSET
; k
++)
173 if ((prev_data
[k
] & csi_data
[k
]) != prev_data
[k
])
177 prev_data
= csi_data
;
178 char_sets
= SCM_CDR (char_sets
);
185 SCM_DEFINE (scm_char_set_hash
, "char-set-hash", 1, 1, 0,
187 "Compute a hash value for the character set @var{cs}. If\n"
188 "@var{bound} is given and non-zero, it restricts the\n"
189 "returned value to the range 0 @dots{} @var{bound - 1}.")
190 #define FUNC_NAME s_scm_char_set_hash
192 const int default_bnd
= 871;
195 unsigned long val
= 0;
198 SCM_VALIDATE_SMOB (1, cs
, charset
);
200 if (SCM_UNBNDP (bound
))
204 SCM_VALIDATE_INUM_MIN_COPY (2, bound
, 0, bnd
);
209 p
= (long *) SCM_SMOB_DATA (cs
);
210 for (k
= 0; k
< LONGS_PER_CHARSET
; k
++)
213 val
= p
[k
] + (val
<< 1);
215 return SCM_MAKINUM (val
% bnd
);
220 SCM_DEFINE (scm_char_set_cursor
, "char-set-cursor", 1, 0, 0,
222 "Return a cursor into the character set @var{cs}.")
223 #define FUNC_NAME s_scm_char_set_cursor
227 SCM_VALIDATE_SMOB (1, cs
, charset
);
228 for (idx
= 0; idx
< SCM_CHARSET_SIZE
; idx
++)
230 if (SCM_CHARSET_GET (cs
, idx
))
233 return SCM_MAKINUM (idx
);
238 SCM_DEFINE (scm_char_set_ref
, "char-set-ref", 2, 0, 0,
239 (SCM cs
, SCM cursor
),
240 "Return the character at the current cursor position\n"
241 "@var{cursor} in the character set @var{cs}. It is an error to\n"
242 "pass a cursor for which @code{end-of-char-set?} returns true.")
243 #define FUNC_NAME s_scm_char_set_ref
247 SCM_VALIDATE_SMOB (1, cs
, charset
);
248 SCM_VALIDATE_INUM_MIN_COPY (2, cursor
, 0, ccursor
);
250 if (ccursor
>= SCM_CHARSET_SIZE
|| !SCM_CHARSET_GET (cs
, ccursor
))
251 SCM_MISC_ERROR ("invalid character set cursor: ~A", scm_list_1 (cursor
));
252 return SCM_MAKE_CHAR (ccursor
);
257 SCM_DEFINE (scm_char_set_cursor_next
, "char-set-cursor-next", 2, 0, 0,
258 (SCM cs
, SCM cursor
),
259 "Advance the character set cursor @var{cursor} to the next\n"
260 "character in the character set @var{cs}. It is an error if the\n"
261 "cursor given satisfies @code{end-of-char-set?}.")
262 #define FUNC_NAME s_scm_char_set_cursor_next
266 SCM_VALIDATE_SMOB (1, cs
, charset
);
267 SCM_VALIDATE_INUM_MIN_COPY (2, cursor
, 0, ccursor
);
269 if (ccursor
>= SCM_CHARSET_SIZE
|| !SCM_CHARSET_GET (cs
, ccursor
))
270 SCM_MISC_ERROR ("invalid character set cursor: ~A", scm_list_1 (cursor
));
271 for (ccursor
++; ccursor
< SCM_CHARSET_SIZE
; ccursor
++)
273 if (SCM_CHARSET_GET (cs
, ccursor
))
276 return SCM_MAKINUM (ccursor
);
281 SCM_DEFINE (scm_end_of_char_set_p
, "end-of-char-set?", 1, 0, 0,
283 "Return @code{#t} if @var{cursor} has reached the end of a\n"
284 "character set, @code{#f} otherwise.")
285 #define FUNC_NAME s_scm_end_of_char_set_p
289 SCM_VALIDATE_INUM_MIN_COPY (1, cursor
, 0, ccursor
);
290 return SCM_BOOL (ccursor
>= SCM_CHARSET_SIZE
);
295 SCM_DEFINE (scm_char_set_fold
, "char-set-fold", 3, 0, 0,
296 (SCM kons
, SCM knil
, SCM cs
),
297 "Fold the procedure @var{kons} over the character set @var{cs},\n"
298 "initializing it with @var{knil}.")
299 #define FUNC_NAME s_scm_char_set_fold
303 SCM_VALIDATE_PROC (1, kons
);
304 SCM_VALIDATE_SMOB (3, cs
, charset
);
306 for (k
= 0; k
< SCM_CHARSET_SIZE
; k
++)
307 if (SCM_CHARSET_GET (cs
, k
))
309 knil
= scm_call_2 (kons
, SCM_MAKE_CHAR (k
), knil
);
316 SCM_DEFINE (scm_char_set_unfold
, "char-set-unfold", 4, 1, 0,
317 (SCM p
, SCM f
, SCM g
, SCM seed
, SCM base_cs
),
318 "This is a fundamental constructor for character sets.\n"
320 "@item @var{g} is used to generate a series of ``seed'' values\n"
321 "from the initial seed: @var{seed}, (@var{g} @var{seed}),\n"
322 "(@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), @dots{}\n"
323 "@item @var{p} tells us when to stop -- when it returns true\n"
324 "when applied to one of the seed values.\n"
325 "@item @var{f} maps each seed value to a character. These\n"
326 "characters are added to the base character set @var{base_cs} to\n"
327 "form the result; @var{base_cs} defaults to the empty set.\n"
329 #define FUNC_NAME s_scm_char_set_unfold
333 SCM_VALIDATE_PROC (1, p
);
334 SCM_VALIDATE_PROC (2, f
);
335 SCM_VALIDATE_PROC (3, g
);
336 if (!SCM_UNBNDP (base_cs
))
338 SCM_VALIDATE_SMOB (5, base_cs
, charset
);
339 result
= scm_char_set_copy (base_cs
);
342 result
= make_char_set (FUNC_NAME
);
344 tmp
= scm_call_1 (p
, seed
);
345 while (SCM_FALSEP (tmp
))
347 SCM ch
= scm_call_1 (f
, seed
);
349 SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (f
));
350 SCM_CHARSET_SET (result
, SCM_CHAR (ch
));
352 seed
= scm_call_1 (g
, seed
);
353 tmp
= scm_call_1 (p
, seed
);
360 SCM_DEFINE (scm_char_set_unfold_x
, "char-set-unfold!", 5, 0, 0,
361 (SCM p
, SCM f
, SCM g
, SCM seed
, SCM base_cs
),
362 "This is a fundamental constructor for character sets.\n"
364 "@item @var{g} is used to generate a series of ``seed'' values\n"
365 "from the initial seed: @var{seed}, (@var{g} @var{seed}),\n"
366 "(@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}), @dots{}\n"
367 "@item @var{p} tells us when to stop -- when it returns true\n"
368 "when applied to one of the seed values.\n"
369 "@item @var{f} maps each seed value to a character. These\n"
370 "characters are added to the base character set @var{base_cs} to\n"
371 "form the result; @var{base_cs} defaults to the empty set.\n"
373 #define FUNC_NAME s_scm_char_set_unfold_x
377 SCM_VALIDATE_PROC (1, p
);
378 SCM_VALIDATE_PROC (2, f
);
379 SCM_VALIDATE_PROC (3, g
);
380 SCM_VALIDATE_SMOB (5, base_cs
, charset
);
382 tmp
= scm_call_1 (p
, seed
);
383 while (SCM_FALSEP (tmp
))
385 SCM ch
= scm_call_1 (f
, seed
);
387 SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (f
));
388 SCM_CHARSET_SET (base_cs
, SCM_CHAR (ch
));
390 seed
= scm_call_1 (g
, seed
);
391 tmp
= scm_call_1 (p
, seed
);
398 SCM_DEFINE (scm_char_set_for_each
, "char-set-for-each", 2, 0, 0,
400 "Apply @var{proc} to every character in the character set\n"
401 "@var{cs}. The return value is not specified.")
402 #define FUNC_NAME s_scm_char_set_for_each
406 SCM_VALIDATE_PROC (1, proc
);
407 SCM_VALIDATE_SMOB (2, cs
, charset
);
409 for (k
= 0; k
< SCM_CHARSET_SIZE
; k
++)
410 if (SCM_CHARSET_GET (cs
, k
))
411 scm_call_1 (proc
, SCM_MAKE_CHAR (k
));
412 return SCM_UNSPECIFIED
;
417 SCM_DEFINE (scm_char_set_map
, "char-set-map", 2, 0, 0,
419 "Map the procedure @var{proc} over every character in @var{cs}.\n"
420 "@var{proc} must be a character -> character procedure.")
421 #define FUNC_NAME s_scm_char_set_map
426 SCM_VALIDATE_PROC (1, proc
);
427 SCM_VALIDATE_SMOB (2, cs
, charset
);
429 result
= make_char_set (FUNC_NAME
);
430 for (k
= 0; k
< SCM_CHARSET_SIZE
; k
++)
431 if (SCM_CHARSET_GET (cs
, k
))
433 SCM ch
= scm_call_1 (proc
, SCM_MAKE_CHAR (k
));
435 SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (proc
));
436 SCM_CHARSET_SET (result
, SCM_CHAR (ch
));
443 SCM_DEFINE (scm_char_set_copy
, "char-set-copy", 1, 0, 0,
445 "Return a newly allocated character set containing all\n"
446 "characters in @var{cs}.")
447 #define FUNC_NAME s_scm_char_set_copy
453 SCM_VALIDATE_SMOB (1, cs
, charset
);
454 ret
= make_char_set (FUNC_NAME
);
455 p1
= (long *) SCM_SMOB_DATA (cs
);
456 p2
= (long *) SCM_SMOB_DATA (ret
);
457 for (k
= 0; k
< LONGS_PER_CHARSET
; k
++)
464 SCM_DEFINE (scm_char_set
, "char-set", 0, 0, 1,
466 "Return a character set containing all given characters.")
467 #define FUNC_NAME s_scm_char_set
473 SCM_VALIDATE_REST_ARGUMENT (rest
);
474 cs
= make_char_set (FUNC_NAME
);
475 p
= (long *) SCM_SMOB_DATA (cs
);
476 while (!SCM_NULLP (rest
))
480 SCM_VALIDATE_CHAR_COPY (argnum
, SCM_CAR (rest
), c
);
482 rest
= SCM_CDR (rest
);
483 p
[c
/ SCM_BITS_PER_LONG
] |= 1L << (c
% SCM_BITS_PER_LONG
);
490 SCM_DEFINE (scm_list_to_char_set
, "list->char-set", 1, 1, 0,
491 (SCM list
, SCM base_cs
),
492 "Convert the character list @var{list} to a character set. If\n"
493 "the character set @var{base_cs} is given, the character in this\n"
494 "set are also included in the result.")
495 #define FUNC_NAME s_scm_list_to_char_set
500 SCM_VALIDATE_LIST (1, list
);
501 if (SCM_UNBNDP (base_cs
))
502 cs
= make_char_set (FUNC_NAME
);
505 SCM_VALIDATE_SMOB (2, base_cs
, charset
);
506 cs
= scm_char_set_copy (base_cs
);
508 p
= (long *) SCM_SMOB_DATA (cs
);
509 while (!SCM_NULLP (list
))
511 SCM chr
= SCM_CAR (list
);
514 SCM_VALIDATE_CHAR_COPY (0, chr
, c
);
515 list
= SCM_CDR (list
);
517 p
[c
/ SCM_BITS_PER_LONG
] |= 1L << (c
% SCM_BITS_PER_LONG
);
524 SCM_DEFINE (scm_list_to_char_set_x
, "list->char-set!", 2, 0, 0,
525 (SCM list
, SCM base_cs
),
526 "Convert the character list @var{list} to a character set. The\n"
527 "characters are added to @var{base_cs} and @var{base_cs} is\n"
529 #define FUNC_NAME s_scm_list_to_char_set_x
533 SCM_VALIDATE_LIST (1, list
);
534 SCM_VALIDATE_SMOB (2, base_cs
, charset
);
535 p
= (long *) SCM_SMOB_DATA (base_cs
);
536 while (!SCM_NULLP (list
))
538 SCM chr
= SCM_CAR (list
);
541 SCM_VALIDATE_CHAR_COPY (0, chr
, c
);
542 list
= SCM_CDR (list
);
544 p
[c
/ SCM_BITS_PER_LONG
] |= 1L << (c
% SCM_BITS_PER_LONG
);
551 SCM_DEFINE (scm_string_to_char_set
, "string->char-set", 1, 1, 0,
552 (SCM str
, SCM base_cs
),
553 "Convert the string @var{str} to a character set. If the\n"
554 "character set @var{base_cs} is given, the characters in this\n"
555 "set are also included in the result.")
556 #define FUNC_NAME s_scm_string_to_char_set
563 SCM_VALIDATE_STRING (1, str
);
564 if (SCM_UNBNDP (base_cs
))
565 cs
= make_char_set (FUNC_NAME
);
568 SCM_VALIDATE_SMOB (2, base_cs
, charset
);
569 cs
= scm_char_set_copy (base_cs
);
571 p
= (long *) SCM_SMOB_DATA (cs
);
572 s
= SCM_STRING_CHARS (str
);
573 while (k
< SCM_STRING_LENGTH (str
))
576 p
[c
/ SCM_BITS_PER_LONG
] |= 1L << (c
% SCM_BITS_PER_LONG
);
583 SCM_DEFINE (scm_string_to_char_set_x
, "string->char-set!", 2, 0, 0,
584 (SCM str
, SCM base_cs
),
585 "Convert the string @var{str} to a character set. The\n"
586 "characters from the string are added to @var{base_cs}, and\n"
587 "@var{base_cs} is returned.")
588 #define FUNC_NAME s_scm_string_to_char_set_x
594 SCM_VALIDATE_STRING (1, str
);
595 SCM_VALIDATE_SMOB (2, base_cs
, charset
);
596 p
= (long *) SCM_SMOB_DATA (base_cs
);
597 s
= SCM_STRING_CHARS (str
);
598 while (k
< SCM_STRING_LENGTH (str
))
601 p
[c
/ SCM_BITS_PER_LONG
] |= 1L << (c
% SCM_BITS_PER_LONG
);
608 SCM_DEFINE (scm_char_set_filter
, "char-set-filter", 2, 1, 0,
609 (SCM pred
, SCM cs
, SCM base_cs
),
610 "Return a character set containing every character from @var{cs}\n"
611 "so that it satisfies @var{pred}. If provided, the characters\n"
612 "from @var{base_cs} are added to the result.")
613 #define FUNC_NAME s_scm_char_set_filter
619 SCM_VALIDATE_PROC (1, pred
);
620 SCM_VALIDATE_SMOB (2, cs
, charset
);
621 if (!SCM_UNBNDP (base_cs
))
623 SCM_VALIDATE_SMOB (3, base_cs
, charset
);
624 ret
= scm_char_set_copy (base_cs
);
627 ret
= make_char_set (FUNC_NAME
);
628 p
= (long *) SCM_SMOB_DATA (ret
);
629 for (k
= 0; k
< SCM_CHARSET_SIZE
; k
++)
631 if (SCM_CHARSET_GET (cs
, k
))
633 SCM res
= scm_call_1 (pred
, SCM_MAKE_CHAR (k
));
635 if (!SCM_FALSEP (res
))
636 p
[k
/ SCM_BITS_PER_LONG
] |= 1L << (k
% SCM_BITS_PER_LONG
);
644 SCM_DEFINE (scm_char_set_filter_x
, "char-set-filter!", 3, 0, 0,
645 (SCM pred
, SCM cs
, SCM base_cs
),
646 "Return a character set containing every character from @var{cs}\n"
647 "so that it satisfies @var{pred}. The characters are added to\n"
648 "@var{base_cs} and @var{base_cs} is returned.")
649 #define FUNC_NAME s_scm_char_set_filter_x
654 SCM_VALIDATE_PROC (1, pred
);
655 SCM_VALIDATE_SMOB (2, cs
, charset
);
656 SCM_VALIDATE_SMOB (3, base_cs
, charset
);
657 p
= (long *) SCM_SMOB_DATA (base_cs
);
658 for (k
= 0; k
< SCM_CHARSET_SIZE
; k
++)
660 if (SCM_CHARSET_GET (cs
, k
))
662 SCM res
= scm_call_1 (pred
, SCM_MAKE_CHAR (k
));
664 if (!SCM_FALSEP (res
))
665 p
[k
/ SCM_BITS_PER_LONG
] |= 1L << (k
% SCM_BITS_PER_LONG
);
673 SCM_DEFINE (scm_ucs_range_to_char_set
, "ucs-range->char-set", 2, 2, 0,
674 (SCM lower
, SCM upper
, SCM error
, SCM base_cs
),
675 "Return a character set containing all characters whose\n"
676 "character codes lie in the half-open range\n"
677 "[@var{lower},@var{upper}).\n"
679 "If @var{error} is a true value, an error is signalled if the\n"
680 "specified range contains characters which are not contained in\n"
681 "the implemented character range. If @var{error} is @code{#f},\n"
682 "these characters are silently left out of the resultung\n"
685 "The characters in @var{base_cs} are added to the result, if\n"
687 #define FUNC_NAME s_scm_ucs_range_to_char_set
693 SCM_VALIDATE_INUM_COPY (1, lower
, clower
);
694 SCM_VALIDATE_INUM_COPY (2, upper
, cupper
);
695 SCM_ASSERT_RANGE (1, lower
, clower
>= 0);
696 SCM_ASSERT_RANGE (2, upper
, cupper
>= 0 && cupper
>= clower
);
697 if (!SCM_UNBNDP (error
))
699 if (!SCM_FALSEP (error
))
701 SCM_ASSERT_RANGE (1, lower
, clower
<= SCM_CHARSET_SIZE
);
702 SCM_ASSERT_RANGE (2, upper
, cupper
<= SCM_CHARSET_SIZE
);
705 if (clower
> SCM_CHARSET_SIZE
)
706 clower
= SCM_CHARSET_SIZE
;
707 if (cupper
> SCM_CHARSET_SIZE
)
708 cupper
= SCM_CHARSET_SIZE
;
709 if (SCM_UNBNDP (base_cs
))
710 cs
= make_char_set (FUNC_NAME
);
713 SCM_VALIDATE_SMOB (2, base_cs
, charset
);
714 cs
= scm_char_set_copy (base_cs
);
716 p
= (long *) SCM_SMOB_DATA (cs
);
717 while (clower
< cupper
)
719 p
[clower
/ SCM_BITS_PER_LONG
] |= 1L << (clower
% SCM_BITS_PER_LONG
);
727 SCM_DEFINE (scm_ucs_range_to_char_set_x
, "ucs-range->char-set!", 4, 0, 0,
728 (SCM lower
, SCM upper
, SCM error
, SCM base_cs
),
729 "Return a character set containing all characters whose\n"
730 "character codes lie in the half-open range\n"
731 "[@var{lower},@var{upper}).\n"
733 "If @var{error} is a true value, an error is signalled if the\n"
734 "specified range contains characters which are not contained in\n"
735 "the implemented character range. If @var{error} is @code{#f},\n"
736 "these characters are silently left out of the resultung\n"
739 "The characters are added to @var{base_cs} and @var{base_cs} is\n"
741 #define FUNC_NAME s_scm_ucs_range_to_char_set_x
746 SCM_VALIDATE_INUM_COPY (1, lower
, clower
);
747 SCM_VALIDATE_INUM_COPY (2, upper
, cupper
);
748 SCM_ASSERT_RANGE (1, lower
, clower
>= 0);
749 SCM_ASSERT_RANGE (2, upper
, cupper
>= 0 && cupper
>= clower
);
750 if (!SCM_FALSEP (error
))
752 SCM_ASSERT_RANGE (1, lower
, clower
<= SCM_CHARSET_SIZE
);
753 SCM_ASSERT_RANGE (2, upper
, cupper
<= SCM_CHARSET_SIZE
);
755 if (clower
> SCM_CHARSET_SIZE
)
756 clower
= SCM_CHARSET_SIZE
;
757 if (cupper
> SCM_CHARSET_SIZE
)
758 cupper
= SCM_CHARSET_SIZE
;
759 p
= (long *) SCM_SMOB_DATA (base_cs
);
760 while (clower
< cupper
)
762 p
[clower
/ SCM_BITS_PER_LONG
] |= 1L << (clower
% SCM_BITS_PER_LONG
);
770 SCM_DEFINE (scm_char_set_size
, "char-set-size", 1, 0, 0,
772 "Return the number of elements in character set @var{cs}.")
773 #define FUNC_NAME s_scm_char_set_size
777 SCM_VALIDATE_SMOB (1, cs
, charset
);
778 for (k
= 0; k
< SCM_CHARSET_SIZE
; k
++)
779 if (SCM_CHARSET_GET (cs
, k
))
781 return SCM_MAKINUM (count
);
786 SCM_DEFINE (scm_char_set_count
, "char-set-count", 2, 0, 0,
788 "Return the number of the elements int the character set\n"
789 "@var{cs} which satisfy the predicate @var{pred}.")
790 #define FUNC_NAME s_scm_char_set_count
794 SCM_VALIDATE_PROC (1, pred
);
795 SCM_VALIDATE_SMOB (2, cs
, charset
);
797 for (k
= 0; k
< SCM_CHARSET_SIZE
; k
++)
798 if (SCM_CHARSET_GET (cs
, k
))
800 SCM res
= scm_call_1 (pred
, SCM_MAKE_CHAR (k
));
801 if (!SCM_FALSEP (res
))
804 return SCM_MAKINUM (count
);
809 SCM_DEFINE (scm_char_set_to_list
, "char-set->list", 1, 0, 0,
811 "Return a list containing the elements of the character set\n"
813 #define FUNC_NAME s_scm_char_set_to_list
816 SCM result
= SCM_EOL
;
818 SCM_VALIDATE_SMOB (1, cs
, charset
);
819 for (k
= SCM_CHARSET_SIZE
; k
> 0; k
--)
820 if (SCM_CHARSET_GET (cs
, k
- 1))
821 result
= scm_cons (SCM_MAKE_CHAR (k
- 1), result
);
827 SCM_DEFINE (scm_char_set_to_string
, "char-set->string", 1, 0, 0,
829 "Return a string containing the elements of the character set\n"
830 "@var{cs}. The order in which the characters are placed in the\n"
831 "string is not defined.")
832 #define FUNC_NAME s_scm_char_set_to_string
840 SCM_VALIDATE_SMOB (1, cs
, charset
);
841 for (k
= 0; k
< SCM_CHARSET_SIZE
; k
++)
842 if (SCM_CHARSET_GET (cs
, k
))
844 result
= scm_allocate_string (count
);
845 p
= SCM_STRING_CHARS (result
);
846 for (k
= 0; k
< SCM_CHARSET_SIZE
; k
++)
847 if (SCM_CHARSET_GET (cs
, k
))
854 SCM_DEFINE (scm_char_set_contains_p
, "char-set-contains?", 2, 0, 0,
856 "Return @code{#t} iff the character @var{ch} is contained in the\n"
857 "character set @var{cs}.")
858 #define FUNC_NAME s_scm_char_set_contains_p
860 SCM_VALIDATE_SMOB (1, cs
, charset
);
861 SCM_VALIDATE_CHAR (2, ch
);
862 return SCM_BOOL (SCM_CHARSET_GET (cs
, SCM_CHAR (ch
)));
867 SCM_DEFINE (scm_char_set_every
, "char-set-every", 2, 0, 0,
869 "Return a true value if every character in the character set\n"
870 "@var{cs} satisfies the predicate @var{pred}.")
871 #define FUNC_NAME s_scm_char_set_every
874 SCM res
= SCM_BOOL_T
;
876 SCM_VALIDATE_PROC (1, pred
);
877 SCM_VALIDATE_SMOB (2, cs
, charset
);
879 for (k
= 0; k
< SCM_CHARSET_SIZE
; k
++)
880 if (SCM_CHARSET_GET (cs
, k
))
882 res
= scm_call_1 (pred
, SCM_MAKE_CHAR (k
));
883 if (SCM_FALSEP (res
))
891 SCM_DEFINE (scm_char_set_any
, "char-set-any", 2, 0, 0,
893 "Return a true value if any character in the character set\n"
894 "@var{cs} satisfies the predicate @var{pred}.")
895 #define FUNC_NAME s_scm_char_set_any
899 SCM_VALIDATE_PROC (1, pred
);
900 SCM_VALIDATE_SMOB (2, cs
, charset
);
902 for (k
= 0; k
< SCM_CHARSET_SIZE
; k
++)
903 if (SCM_CHARSET_GET (cs
, k
))
905 SCM res
= scm_call_1 (pred
, SCM_MAKE_CHAR (k
));
906 if (!SCM_FALSEP (res
))
914 SCM_DEFINE (scm_char_set_adjoin
, "char-set-adjoin", 1, 0, 1,
916 "Add all character arguments to the first argument, which must\n"
917 "be a character set.")
918 #define FUNC_NAME s_scm_char_set_adjoin
922 SCM_VALIDATE_SMOB (1, cs
, charset
);
923 SCM_VALIDATE_REST_ARGUMENT (rest
);
924 cs
= scm_char_set_copy (cs
);
926 p
= (long *) SCM_SMOB_DATA (cs
);
927 while (!SCM_NULLP (rest
))
929 SCM chr
= SCM_CAR (rest
);
932 SCM_VALIDATE_CHAR_COPY (1, chr
, c
);
933 rest
= SCM_CDR (rest
);
935 p
[c
/ SCM_BITS_PER_LONG
] |= 1L << (c
% SCM_BITS_PER_LONG
);
942 SCM_DEFINE (scm_char_set_delete
, "char-set-delete", 1, 0, 1,
944 "Delete all character arguments from the first argument, which\n"
945 "must be a character set.")
946 #define FUNC_NAME s_scm_char_set_delete
950 SCM_VALIDATE_SMOB (1, cs
, charset
);
951 SCM_VALIDATE_REST_ARGUMENT (rest
);
952 cs
= scm_char_set_copy (cs
);
954 p
= (long *) SCM_SMOB_DATA (cs
);
955 while (!SCM_NULLP (rest
))
957 SCM chr
= SCM_CAR (rest
);
960 SCM_VALIDATE_CHAR_COPY (1, chr
, c
);
961 rest
= SCM_CDR (rest
);
963 p
[c
/ SCM_BITS_PER_LONG
] &= ~(1L << (c
% SCM_BITS_PER_LONG
));
970 SCM_DEFINE (scm_char_set_adjoin_x
, "char-set-adjoin!", 1, 0, 1,
972 "Add all character arguments to the first argument, which must\n"
973 "be a character set.")
974 #define FUNC_NAME s_scm_char_set_adjoin_x
978 SCM_VALIDATE_SMOB (1, cs
, charset
);
979 SCM_VALIDATE_REST_ARGUMENT (rest
);
981 p
= (long *) SCM_SMOB_DATA (cs
);
982 while (!SCM_NULLP (rest
))
984 SCM chr
= SCM_CAR (rest
);
987 SCM_VALIDATE_CHAR_COPY (1, chr
, c
);
988 rest
= SCM_CDR (rest
);
990 p
[c
/ SCM_BITS_PER_LONG
] |= 1L << (c
% SCM_BITS_PER_LONG
);
997 SCM_DEFINE (scm_char_set_delete_x
, "char-set-delete!", 1, 0, 1,
999 "Delete all character arguments from the first argument, which\n"
1000 "must be a character set.")
1001 #define FUNC_NAME s_scm_char_set_delete_x
1005 SCM_VALIDATE_SMOB (1, cs
, charset
);
1006 SCM_VALIDATE_REST_ARGUMENT (rest
);
1008 p
= (long *) SCM_SMOB_DATA (cs
);
1009 while (!SCM_NULLP (rest
))
1011 SCM chr
= SCM_CAR (rest
);
1014 SCM_VALIDATE_CHAR_COPY (1, chr
, c
);
1015 rest
= SCM_CDR (rest
);
1017 p
[c
/ SCM_BITS_PER_LONG
] &= ~(1L << (c
% SCM_BITS_PER_LONG
));
1024 SCM_DEFINE (scm_char_set_complement
, "char-set-complement", 1, 0, 0,
1026 "Return the complement of the character set @var{cs}.")
1027 #define FUNC_NAME s_scm_char_set_complement
1033 SCM_VALIDATE_SMOB (1, cs
, charset
);
1035 res
= make_char_set (FUNC_NAME
);
1036 p
= (long *) SCM_SMOB_DATA (res
);
1037 q
= (long *) SCM_SMOB_DATA (cs
);
1038 for (k
= 0; k
< LONGS_PER_CHARSET
; k
++)
1045 SCM_DEFINE (scm_char_set_union
, "char-set-union", 0, 0, 1,
1047 "Return the union of all argument character sets.")
1048 #define FUNC_NAME s_scm_char_set_union
1054 SCM_VALIDATE_REST_ARGUMENT (rest
);
1056 res
= make_char_set (FUNC_NAME
);
1057 p
= (long *) SCM_SMOB_DATA (res
);
1058 while (!SCM_NULLP (rest
))
1061 SCM cs
= SCM_CAR (rest
);
1062 SCM_VALIDATE_SMOB (c
, cs
, charset
);
1064 rest
= SCM_CDR (rest
);
1066 for (k
= 0; k
< LONGS_PER_CHARSET
; k
++)
1067 p
[k
] |= ((long *) SCM_SMOB_DATA (cs
))[k
];
1074 SCM_DEFINE (scm_char_set_intersection
, "char-set-intersection", 0, 0, 1,
1076 "Return the intersection of all argument character sets.")
1077 #define FUNC_NAME s_scm_char_set_intersection
1081 SCM_VALIDATE_REST_ARGUMENT (rest
);
1083 if (SCM_NULLP (rest
))
1084 res
= make_char_set (FUNC_NAME
);
1090 res
= scm_char_set_copy (SCM_CAR (rest
));
1091 p
= (long *) SCM_SMOB_DATA (res
);
1092 rest
= SCM_CDR (rest
);
1094 while (SCM_CONSP (rest
))
1097 SCM cs
= SCM_CAR (rest
);
1100 SCM_VALIDATE_SMOB (argnum
, cs
, charset
);
1102 cs_data
= (long *) SCM_SMOB_DATA (cs
);
1103 rest
= SCM_CDR (rest
);
1104 for (k
= 0; k
< LONGS_PER_CHARSET
; k
++)
1114 SCM_DEFINE (scm_char_set_difference
, "char-set-difference", 1, 0, 1,
1115 (SCM cs1
, SCM rest
),
1116 "Return the difference of all argument character sets.")
1117 #define FUNC_NAME s_scm_char_set_difference
1123 SCM_VALIDATE_SMOB (1, cs1
, charset
);
1124 SCM_VALIDATE_REST_ARGUMENT (rest
);
1126 res
= scm_char_set_copy (cs1
);
1127 p
= (long *) SCM_SMOB_DATA (res
);
1128 while (!SCM_NULLP (rest
))
1131 SCM cs
= SCM_CAR (rest
);
1132 SCM_VALIDATE_SMOB (c
, cs
, charset
);
1134 rest
= SCM_CDR (rest
);
1136 for (k
= 0; k
< LONGS_PER_CHARSET
; k
++)
1137 p
[k
] &= ~((long *) SCM_SMOB_DATA (cs
))[k
];
1144 SCM_DEFINE (scm_char_set_xor
, "char-set-xor", 0, 0, 1,
1146 "Return the exclusive-or of all argument character sets.")
1147 #define FUNC_NAME s_scm_char_set_xor
1151 SCM_VALIDATE_REST_ARGUMENT (rest
);
1153 if (SCM_NULLP (rest
))
1154 res
= make_char_set (FUNC_NAME
);
1160 res
= scm_char_set_copy (SCM_CAR (rest
));
1161 p
= (long *) SCM_SMOB_DATA (res
);
1162 rest
= SCM_CDR (rest
);
1164 while (SCM_CONSP (rest
))
1166 SCM cs
= SCM_CAR (rest
);
1170 SCM_VALIDATE_SMOB (argnum
, cs
, charset
);
1172 cs_data
= (long *) SCM_SMOB_DATA (cs
);
1173 rest
= SCM_CDR (rest
);
1175 for (k
= 0; k
< LONGS_PER_CHARSET
; k
++)
1184 SCM_DEFINE (scm_char_set_diff_plus_intersection
, "char-set-diff+intersection", 1, 0, 1,
1185 (SCM cs1
, SCM rest
),
1186 "Return the difference and the intersection of all argument\n"
1188 #define FUNC_NAME s_scm_char_set_diff_plus_intersection
1194 SCM_VALIDATE_SMOB (1, cs1
, charset
);
1195 SCM_VALIDATE_REST_ARGUMENT (rest
);
1197 res1
= scm_char_set_copy (cs1
);
1198 res2
= make_char_set (FUNC_NAME
);
1199 p
= (long *) SCM_SMOB_DATA (res1
);
1200 q
= (long *) SCM_SMOB_DATA (res2
);
1201 while (!SCM_NULLP (rest
))
1204 SCM cs
= SCM_CAR (rest
);
1207 SCM_VALIDATE_SMOB (c
, cs
, charset
);
1209 r
= (long *) SCM_SMOB_DATA (cs
);
1211 for (k
= 0; k
< LONGS_PER_CHARSET
; k
++)
1213 q
[k
] |= p
[k
] & r
[k
];
1216 rest
= SCM_CDR (rest
);
1218 return scm_values (scm_list_2 (res1
, res2
));
1223 SCM_DEFINE (scm_char_set_complement_x
, "char-set-complement!", 1, 0, 0,
1225 "Return the complement of the character set @var{cs}.")
1226 #define FUNC_NAME s_scm_char_set_complement_x
1231 SCM_VALIDATE_SMOB (1, cs
, charset
);
1232 p
= (long *) SCM_SMOB_DATA (cs
);
1233 for (k
= 0; k
< LONGS_PER_CHARSET
; k
++)
1240 SCM_DEFINE (scm_char_set_union_x
, "char-set-union!", 1, 0, 1,
1241 (SCM cs1
, SCM rest
),
1242 "Return the union of all argument character sets.")
1243 #define FUNC_NAME s_scm_char_set_union_x
1248 SCM_VALIDATE_SMOB (1, cs1
, charset
);
1249 SCM_VALIDATE_REST_ARGUMENT (rest
);
1251 p
= (long *) SCM_SMOB_DATA (cs1
);
1252 while (!SCM_NULLP (rest
))
1255 SCM cs
= SCM_CAR (rest
);
1256 SCM_VALIDATE_SMOB (c
, cs
, charset
);
1258 rest
= SCM_CDR (rest
);
1260 for (k
= 0; k
< LONGS_PER_CHARSET
; k
++)
1261 p
[k
] |= ((long *) SCM_SMOB_DATA (cs
))[k
];
1268 SCM_DEFINE (scm_char_set_intersection_x
, "char-set-intersection!", 1, 0, 1,
1269 (SCM cs1
, SCM rest
),
1270 "Return the intersection of all argument character sets.")
1271 #define FUNC_NAME s_scm_char_set_intersection_x
1276 SCM_VALIDATE_SMOB (1, cs1
, charset
);
1277 SCM_VALIDATE_REST_ARGUMENT (rest
);
1279 p
= (long *) SCM_SMOB_DATA (cs1
);
1280 while (!SCM_NULLP (rest
))
1283 SCM cs
= SCM_CAR (rest
);
1284 SCM_VALIDATE_SMOB (c
, cs
, charset
);
1286 rest
= SCM_CDR (rest
);
1288 for (k
= 0; k
< LONGS_PER_CHARSET
; k
++)
1289 p
[k
] &= ((long *) SCM_SMOB_DATA (cs
))[k
];
1296 SCM_DEFINE (scm_char_set_difference_x
, "char-set-difference!", 1, 0, 1,
1297 (SCM cs1
, SCM rest
),
1298 "Return the difference of all argument character sets.")
1299 #define FUNC_NAME s_scm_char_set_difference_x
1304 SCM_VALIDATE_SMOB (1, cs1
, charset
);
1305 SCM_VALIDATE_REST_ARGUMENT (rest
);
1307 p
= (long *) SCM_SMOB_DATA (cs1
);
1308 while (!SCM_NULLP (rest
))
1311 SCM cs
= SCM_CAR (rest
);
1312 SCM_VALIDATE_SMOB (c
, cs
, charset
);
1314 rest
= SCM_CDR (rest
);
1316 for (k
= 0; k
< LONGS_PER_CHARSET
; k
++)
1317 p
[k
] &= ~((long *) SCM_SMOB_DATA (cs
))[k
];
1324 SCM_DEFINE (scm_char_set_xor_x
, "char-set-xor!", 1, 0, 1,
1325 (SCM cs1
, SCM rest
),
1326 "Return the exclusive-or of all argument character sets.")
1327 #define FUNC_NAME s_scm_char_set_xor_x
1329 /* a side-effecting variant should presumably give consistent results:
1330 (define a (char-set #\a))
1331 (char-set-xor a a a) -> char set #\a
1332 (char-set-xor! a a a) -> char set #\a
1334 return scm_char_set_xor (scm_cons (cs1
, rest
));
1337 /* this would give (char-set-xor! a a a) -> empty char set. */
1341 SCM_VALIDATE_SMOB (1, cs1
, charset
);
1342 SCM_VALIDATE_REST_ARGUMENT (rest
);
1344 p
= (long *) SCM_SMOB_DATA (cs1
);
1345 while (!SCM_NULLP (rest
))
1348 SCM cs
= SCM_CAR (rest
);
1349 SCM_VALIDATE_SMOB (c
, cs
, charset
);
1351 rest
= SCM_CDR (rest
);
1353 for (k
= 0; k
< LONGS_PER_CHARSET
; k
++)
1354 p
[k
] ^= ((long *) SCM_SMOB_DATA (cs
))[k
];
1362 SCM_DEFINE (scm_char_set_diff_plus_intersection_x
, "char-set-diff+intersection!", 2, 0, 1,
1363 (SCM cs1
, SCM cs2
, SCM rest
),
1364 "Return the difference and the intersection of all argument\n"
1366 #define FUNC_NAME s_scm_char_set_diff_plus_intersection_x
1372 SCM_VALIDATE_SMOB (1, cs1
, charset
);
1373 SCM_VALIDATE_SMOB (2, cs2
, charset
);
1374 SCM_VALIDATE_REST_ARGUMENT (rest
);
1376 p
= (long *) SCM_SMOB_DATA (cs1
);
1377 q
= (long *) SCM_SMOB_DATA (cs2
);
1380 /* (char-set-diff+intersection! a a ...): can't share storage,
1381 but we know the answer without checking for further
1383 return scm_values (scm_list_2 (make_char_set (FUNC_NAME
), cs1
));
1385 for (k
= 0; k
< LONGS_PER_CHARSET
; k
++)
1392 while (!SCM_NULLP (rest
))
1394 SCM cs
= SCM_CAR (rest
);
1397 SCM_VALIDATE_SMOB (c
, cs
, charset
);
1399 r
= (long *) SCM_SMOB_DATA (cs
);
1401 for (k
= 0; k
< LONGS_PER_CHARSET
; k
++)
1403 q
[k
] |= p
[k
] & r
[k
];
1406 rest
= SCM_CDR (rest
);
1408 return scm_values (scm_list_2 (cs1
, cs2
));
1413 /* Create the charset smob type. */
1415 scm_c_init_srfi_14 (void)
1417 /* Charset smob creation is protected by this variable because this
1418 function can be both called from the SRFI-13 and SRFI-14
1419 initialization functions. This is because the SRFI-13 procedures
1420 access the charset smob type code. */
1421 static int initialized
= 0;
1425 scm_tc16_charset
= scm_make_smob_type ("character-set",
1427 scm_set_smob_free (scm_tc16_charset
, charset_free
);
1428 scm_set_smob_print (scm_tc16_charset
, charset_print
);
1434 /* Initialize the SRFI-14 module. This function will be called by the
1435 loading Scheme module. */
1437 scm_init_srfi_14 (void)
1440 fprintf(stderr
, "bytes-per-charset: %d\n", BYTES_PER_CHARSET
);
1441 fprintf(stderr
, "bits-per-long: %d\n", SCM_BITS_PER_LONG
);
1442 fprintf(stderr
, "longs-per-charset: %d\n", LONGS_PER_CHARSET
);
1446 /* Do the smob type initialization. */
1447 scm_c_init_srfi_14 ();
1449 /* Install the charset primitives. */
1450 #ifndef SCM_MAGIC_SNARFER
1451 #include "srfi/srfi-14.x"
1455 /* End of srfi-14.c. */