(string-any, string-every): Use a scheme
[bpt/guile.git] / libguile / srfi-13.c
1 /* srfi-13.c --- SRFI-13 procedures for Guile
2 *
3 * Copyright (C) 2001, 2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20
21 #include <string.h>
22 #include <ctype.h>
23
24 #include "libguile.h"
25
26 #include "libguile/srfi-13.h"
27 #include "libguile/srfi-14.h"
28
29 /* SCM_VALIDATE_SUBSTRING_SPEC_COPY is deprecated since it encourages
30 messing with the internal representation of strings. We define our
31 own version since we use it so much and are messing with Guile
32 internals anyway.
33 */
34
35 #define MY_VALIDATE_SUBSTRING_SPEC_COPY(pos_str, str, c_str, \
36 pos_start, start, c_start, \
37 pos_end, end, c_end) \
38 do { \
39 SCM_VALIDATE_STRING (pos_str, str); \
40 c_str = scm_i_string_chars (str); \
41 scm_i_get_substring_spec (scm_i_string_length (str), \
42 start, &c_start, end, &c_end); \
43 } while (0)
44
45 #define MY_VALIDATE_SUBSTRING_SPEC(pos_str, str, \
46 pos_start, start, c_start, \
47 pos_end, end, c_end) \
48 do { \
49 SCM_VALIDATE_STRING (pos_str, str); \
50 scm_i_get_substring_spec (scm_i_string_length (str), \
51 start, &c_start, end, &c_end); \
52 } while (0)
53
54 SCM_DEFINE (scm_string_null_p, "string-null?", 1, 0, 0,
55 (SCM str),
56 "Return @code{#t} if @var{str}'s length is zero, and\n"
57 "@code{#f} otherwise.\n"
58 "@lisp\n"
59 "(string-null? \"\") @result{} #t\n"
60 "y @result{} \"foo\"\n"
61 "(string-null? y) @result{} #f\n"
62 "@end lisp")
63 #define FUNC_NAME s_scm_string_null_p
64 {
65 SCM_VALIDATE_STRING (1, str);
66 return scm_from_bool (scm_i_string_length (str) == 0);
67 }
68 #undef FUNC_NAME
69
70 #if 0
71 static void
72 race_error ()
73 {
74 scm_misc_error (NULL, "race condition detected", SCM_EOL);
75 }
76 #endif
77
78 SCM_DEFINE (scm_string_any, "string-any-c-code", 2, 2, 0,
79 (SCM char_pred, SCM s, SCM start, SCM end),
80 "Check if the predicate @var{pred} is true for any character in\n"
81 "the string @var{s}.\n"
82 "\n"
83 "Calls to @var{pred} are made from left to right across @var{s}.\n"
84 "When it returns true (ie.@: non-@code{#f}), that return value\n"
85 "is the return from @code{string-any}.\n"
86 "\n"
87 "The SRFI-13 specification requires that the call to @var{pred}\n"
88 "on the last character of @var{s} (assuming that point is\n"
89 "reached) be a tail call, but currently in Guile this is not the\n"
90 "case.")
91 #define FUNC_NAME s_scm_string_any
92 {
93 const char *cstr;
94 size_t cstart, cend;
95 SCM res = SCM_BOOL_F;
96
97 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s, cstr,
98 3, start, cstart,
99 4, end, cend);
100
101 if (SCM_CHARP (char_pred))
102 {
103 res = (memchr (cstr+cstart, (int) SCM_CHAR (char_pred),
104 cend-cstart) == NULL
105 ? SCM_BOOL_F : SCM_BOOL_T);
106 }
107 else if (SCM_CHARSETP (char_pred))
108 {
109 size_t i;
110 for (i = cstart; i < cend; i++)
111 if (SCM_CHARSET_GET (char_pred, cstr[i]))
112 {
113 res = SCM_BOOL_T;
114 break;
115 }
116 }
117 else
118 {
119 SCM_VALIDATE_PROC (1, char_pred);
120
121 while (cstart < cend)
122 {
123 res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
124 if (scm_is_true (res))
125 break;
126 cstr = scm_i_string_chars (s);
127 cstart++;
128 }
129 }
130
131 scm_remember_upto_here_1 (s);
132 return res;
133 }
134 #undef FUNC_NAME
135
136
137 SCM_DEFINE (scm_string_every, "string-every-c-code", 2, 2, 0,
138 (SCM char_pred, SCM s, SCM start, SCM end),
139 "Check if the predicate @var{pred} is true for every character\n"
140 "in the string @var{s}.\n"
141 "\n"
142 "Calls to @var{pred} are made from left to right across @var{s}.\n"
143 "If the predicate is true for every character then the return\n"
144 "value from the last @var{pred} call is the return from\n"
145 "@code{string-every}.\n"
146 "\n"
147 "If there are no characters in @var{s} (ie.@: @var{start} equals\n"
148 "@var{end}) then the return is @code{#t}.\n"
149 "\n"
150 "The SRFI-13 specification requires that the call to @var{pred}\n"
151 "on the last character of @var{s} (assuming that point is\n"
152 "reached) be a tail call, but currently in Guile this is not the\n"
153 "case.")
154 #define FUNC_NAME s_scm_string_every
155 {
156 const char *cstr;
157 size_t cstart, cend;
158 SCM res = SCM_BOOL_T;
159
160 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s, cstr,
161 3, start, cstart,
162 4, end, cend);
163 if (SCM_CHARP (char_pred))
164 {
165 char cchr = SCM_CHAR (char_pred);
166 size_t i;
167 for (i = cstart; i < cend; i++)
168 if (cstr[i] != cchr)
169 {
170 res = SCM_BOOL_F;
171 break;
172 }
173 }
174 else if (SCM_CHARSETP (char_pred))
175 {
176 size_t i;
177 for (i = cstart; i < cend; i++)
178 if (!SCM_CHARSET_GET (char_pred, cstr[i]))
179 {
180 res = SCM_BOOL_F;
181 break;
182 }
183 }
184 else
185 {
186 SCM_VALIDATE_PROC (1, char_pred);
187
188 while (cstart < cend)
189 {
190 res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
191 if (scm_is_false (res))
192 break;
193 cstr = scm_i_string_chars (s);
194 cstart++;
195 }
196 }
197
198 scm_remember_upto_here_1 (s);
199 return res;
200 }
201 #undef FUNC_NAME
202
203
204 SCM_DEFINE (scm_string_tabulate, "string-tabulate", 2, 0, 0,
205 (SCM proc, SCM len),
206 "@var{proc} is an integer->char procedure. Construct a string\n"
207 "of size @var{len} by applying @var{proc} to each index to\n"
208 "produce the corresponding string element. The order in which\n"
209 "@var{proc} is applied to the indices is not specified.")
210 #define FUNC_NAME s_scm_string_tabulate
211 {
212 size_t clen, i;
213 SCM res;
214 SCM ch;
215 char *p;
216
217 SCM_VALIDATE_PROC (1, proc);
218 clen = scm_to_size_t (len);
219 SCM_ASSERT_RANGE (2, len, clen >= 0);
220
221 res = scm_i_make_string (clen, &p);
222 i = 0;
223 while (i < clen)
224 {
225 /* The RES string remains untouched since nobody knows about it
226 yet. No need to refetch P.
227 */
228 ch = scm_call_1 (proc, scm_from_size_t (i));
229 if (!SCM_CHARP (ch))
230 SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (proc));
231 *p++ = SCM_CHAR (ch);
232 i++;
233 }
234 return res;
235 }
236 #undef FUNC_NAME
237
238
239 SCM_DEFINE (scm_substring_to_list, "string->list", 1, 2, 0,
240 (SCM str, SCM start, SCM end),
241 "Convert the string @var{str} into a list of characters.")
242 #define FUNC_NAME s_scm_substring_to_list
243 {
244 const char *cstr;
245 size_t cstart, cend;
246 SCM result = SCM_EOL;
247
248 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
249 2, start, cstart,
250 3, end, cend);
251 while (cstart < cend)
252 {
253 cend--;
254 result = scm_cons (SCM_MAKE_CHAR (cstr[cend]), result);
255 cstr = scm_i_string_chars (str);
256 }
257 scm_remember_upto_here_1 (str);
258 return result;
259 }
260 #undef FUNC_NAME
261
262 /* We export scm_substring_to_list as "string->list" since it is
263 compatible and more general. This function remains for the benefit
264 of C code that used it.
265 */
266
267 SCM
268 scm_string_to_list (SCM str)
269 {
270 return scm_substring_to_list (str, SCM_UNDEFINED, SCM_UNDEFINED);
271 }
272
273 SCM_DEFINE (scm_reverse_list_to_string, "reverse-list->string", 1, 0, 0,
274 (SCM chrs),
275 "An efficient implementation of @code{(compose string->list\n"
276 "reverse)}:\n"
277 "\n"
278 "@smalllisp\n"
279 "(reverse-list->string '(#\\a #\\B #\\c)) @result{} \"cBa\"\n"
280 "@end smalllisp")
281 #define FUNC_NAME s_scm_reverse_list_to_string
282 {
283 SCM result;
284 long i = scm_ilength (chrs);
285 char *data;
286
287 if (i < 0)
288 SCM_WRONG_TYPE_ARG (1, chrs);
289 result = scm_i_make_string (i, &data);
290
291 {
292
293 data += i;
294 while (i > 0 && scm_is_pair (chrs))
295 {
296 SCM elt = SCM_CAR (chrs);
297
298 SCM_VALIDATE_CHAR (SCM_ARGn, elt);
299 data--;
300 *data = SCM_CHAR (elt);
301 chrs = SCM_CDR (chrs);
302 i--;
303 }
304 }
305
306 return result;
307 }
308 #undef FUNC_NAME
309
310
311 SCM_SYMBOL (scm_sym_infix, "infix");
312 SCM_SYMBOL (scm_sym_strict_infix, "strict-infix");
313 SCM_SYMBOL (scm_sym_suffix, "suffix");
314 SCM_SYMBOL (scm_sym_prefix, "prefix");
315
316 static void
317 append_string (char **sp, size_t *lp, SCM str)
318 {
319 size_t len;
320 len = scm_c_string_length (str);
321 if (len > *lp)
322 len = *lp;
323 memcpy (*sp, scm_i_string_chars (str), len);
324 *lp -= len;
325 *sp += len;
326 }
327
328 SCM_DEFINE (scm_string_join, "string-join", 1, 2, 0,
329 (SCM ls, SCM delimiter, SCM grammar),
330 "Append the string in the string list @var{ls}, using the string\n"
331 "@var{delim} as a delimiter between the elements of @var{ls}.\n"
332 "@var{grammar} is a symbol which specifies how the delimiter is\n"
333 "placed between the strings, and defaults to the symbol\n"
334 "@code{infix}.\n"
335 "\n"
336 "@table @code\n"
337 "@item infix\n"
338 "Insert the separator between list elements. An empty string\n"
339 "will produce an empty list.\n"
340 "@item string-infix\n"
341 "Like @code{infix}, but will raise an error if given the empty\n"
342 "list.\n"
343 "@item suffix\n"
344 "Insert the separator after every list element.\n"
345 "@item prefix\n"
346 "Insert the separator before each list element.\n"
347 "@end table")
348 #define FUNC_NAME s_scm_string_join
349 {
350 #define GRAM_INFIX 0
351 #define GRAM_STRICT_INFIX 1
352 #define GRAM_SUFFIX 2
353 #define GRAM_PREFIX 3
354 SCM tmp;
355 SCM result;
356 int gram = GRAM_INFIX;
357 size_t del_len = 0;
358 size_t len = 0;
359 char *p;
360 long strings = scm_ilength (ls);
361
362 /* Validate the string list. */
363 if (strings < 0)
364 SCM_WRONG_TYPE_ARG (1, ls);
365
366 /* Validate the delimiter and record its length. */
367 if (SCM_UNBNDP (delimiter))
368 {
369 delimiter = scm_from_locale_string (" ");
370 del_len = 1;
371 }
372 else
373 del_len = scm_c_string_length (delimiter);
374
375 /* Validate the grammar symbol and remember the grammar. */
376 if (SCM_UNBNDP (grammar))
377 gram = GRAM_INFIX;
378 else if (scm_is_eq (grammar, scm_sym_infix))
379 gram = GRAM_INFIX;
380 else if (scm_is_eq (grammar, scm_sym_strict_infix))
381 gram = GRAM_STRICT_INFIX;
382 else if (scm_is_eq (grammar, scm_sym_suffix))
383 gram = GRAM_SUFFIX;
384 else if (scm_is_eq (grammar, scm_sym_prefix))
385 gram = GRAM_PREFIX;
386 else
387 SCM_WRONG_TYPE_ARG (3, grammar);
388
389 /* Check grammar constraints and calculate the space required for
390 the delimiter(s). */
391 switch (gram)
392 {
393 case GRAM_INFIX:
394 if (!scm_is_null (ls))
395 len = (strings > 0) ? ((strings - 1) * del_len) : 0;
396 break;
397 case GRAM_STRICT_INFIX:
398 if (strings == 0)
399 SCM_MISC_ERROR ("strict-infix grammar requires non-empty list",
400 SCM_EOL);
401 len = (strings - 1) * del_len;
402 break;
403 default:
404 len = strings * del_len;
405 break;
406 }
407
408 tmp = ls;
409 while (scm_is_pair (tmp))
410 {
411 len += scm_c_string_length (SCM_CAR (tmp));
412 tmp = SCM_CDR (tmp);
413 }
414
415 result = scm_i_make_string (len, &p);
416
417 tmp = ls;
418 switch (gram)
419 {
420 case GRAM_INFIX:
421 case GRAM_STRICT_INFIX:
422 while (scm_is_pair (tmp))
423 {
424 append_string (&p, &len, SCM_CAR (tmp));
425 if (!scm_is_null (SCM_CDR (tmp)) && del_len > 0)
426 append_string (&p, &len, delimiter);
427 tmp = SCM_CDR (tmp);
428 }
429 break;
430 case GRAM_SUFFIX:
431 while (scm_is_pair (tmp))
432 {
433 append_string (&p, &len, SCM_CAR (tmp));
434 if (del_len > 0)
435 append_string (&p, &len, delimiter);
436 tmp = SCM_CDR (tmp);
437 }
438 break;
439 case GRAM_PREFIX:
440 while (scm_is_pair (tmp))
441 {
442 if (del_len > 0)
443 append_string (&p, &len, delimiter);
444 append_string (&p, &len, SCM_CAR (tmp));
445 tmp = SCM_CDR (tmp);
446 }
447 break;
448 }
449
450 return result;
451 #undef GRAM_INFIX
452 #undef GRAM_STRICT_INFIX
453 #undef GRAM_SUFFIX
454 #undef GRAM_PREFIX
455 }
456 #undef FUNC_NAME
457
458
459 /* There are a number of functions to consider here for Scheme and C:
460
461 string-copy STR [start [end]] ;; SRFI-13 variant of R5RS string-copy
462 substring/copy STR start [end] ;; Guile variant of R5RS substring
463
464 scm_string_copy (str) ;; Old function from Guile
465 scm_substring_copy (str, [start, [end]])
466 ;; C version of SRFI-13 string-copy
467 ;; and C version of substring/copy
468
469 The C function underlying string-copy is not exported to C
470 programs. scm_substring_copy is defined in strings.c as the
471 underlying function of substring/copy and allows an optional START
472 argument.
473 */
474
475 SCM scm_srfi13_substring_copy (SCM str, SCM start, SCM end);
476
477 SCM_DEFINE (scm_srfi13_substring_copy, "string-copy", 1, 2, 0,
478 (SCM str, SCM start, SCM end),
479 "Return a freshly allocated copy of the string @var{str}. If\n"
480 "given, @var{start} and @var{end} delimit the portion of\n"
481 "@var{str} which is copied.")
482 #define FUNC_NAME s_scm_srfi13_substring_copy
483 {
484 const char *cstr;
485 size_t cstart, cend;
486
487 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
488 2, start, cstart,
489 3, end, cend);
490 return scm_c_substring_copy (str, cstart, cend);
491 }
492 #undef FUNC_NAME
493
494 SCM
495 scm_string_copy (SCM str)
496 {
497 return scm_c_substring (str, 0, scm_c_string_length (str));
498 }
499
500 SCM_DEFINE (scm_string_copy_x, "string-copy!", 3, 2, 0,
501 (SCM target, SCM tstart, SCM s, SCM start, SCM end),
502 "Copy the sequence of characters from index range [@var{start},\n"
503 "@var{end}) in string @var{s} to string @var{target}, beginning\n"
504 "at index @var{tstart}. The characters are copied left-to-right\n"
505 "or right-to-left as needed -- the copy is guaranteed to work,\n"
506 "even if @var{target} and @var{s} are the same string. It is an\n"
507 "error if the copy operation runs off the end of the target\n"
508 "string.")
509 #define FUNC_NAME s_scm_string_copy_x
510 {
511 const char *cstr;
512 char *ctarget;
513 size_t cstart, cend, ctstart, dummy, len;
514 SCM sdummy = SCM_UNDEFINED;
515
516 MY_VALIDATE_SUBSTRING_SPEC (1, target,
517 2, tstart, ctstart,
518 2, sdummy, dummy);
519 MY_VALIDATE_SUBSTRING_SPEC_COPY (3, s, cstr,
520 4, start, cstart,
521 5, end, cend);
522 len = cend - cstart;
523 SCM_ASSERT_RANGE (3, s, len <= scm_i_string_length (target) - ctstart);
524
525 ctarget = scm_i_string_writable_chars (target);
526 memmove (ctarget + ctstart, cstr + cstart, len);
527 scm_i_string_stop_writing ();
528 scm_remember_upto_here_1 (target);
529
530 return SCM_UNSPECIFIED;
531 }
532 #undef FUNC_NAME
533
534 SCM_DEFINE (scm_substring_move_x, "substring-move!", 5, 0, 0,
535 (SCM str1, SCM start1, SCM end1, SCM str2, SCM start2),
536 "Copy the substring of @var{str1} bounded by @var{start1} and @var{end1}\n"
537 "into @var{str2} beginning at position @var{start2}.\n"
538 "@var{str1} and @var{str2} can be the same string.")
539 #define FUNC_NAME s_scm_substring_move_x
540 {
541 return scm_string_copy_x (str2, start2, str1, start1, end1);
542 }
543 #undef FUNC_NAME
544
545 SCM_DEFINE (scm_string_take, "string-take", 2, 0, 0,
546 (SCM s, SCM n),
547 "Return the @var{n} first characters of @var{s}.")
548 #define FUNC_NAME s_scm_string_take
549 {
550 return scm_substring (s, SCM_INUM0, n);
551 }
552 #undef FUNC_NAME
553
554
555 SCM_DEFINE (scm_string_drop, "string-drop", 2, 0, 0,
556 (SCM s, SCM n),
557 "Return all but the first @var{n} characters of @var{s}.")
558 #define FUNC_NAME s_scm_string_drop
559 {
560 return scm_substring (s, n, SCM_UNDEFINED);
561 }
562 #undef FUNC_NAME
563
564
565 SCM_DEFINE (scm_string_take_right, "string-take-right", 2, 0, 0,
566 (SCM s, SCM n),
567 "Return the @var{n} last characters of @var{s}.")
568 #define FUNC_NAME s_scm_string_take_right
569 {
570 return scm_substring (s,
571 scm_difference (scm_string_length (s), n),
572 SCM_UNDEFINED);
573 }
574 #undef FUNC_NAME
575
576
577 SCM_DEFINE (scm_string_drop_right, "string-drop-right", 2, 0, 0,
578 (SCM s, SCM n),
579 "Return all but the last @var{n} characters of @var{s}.")
580 #define FUNC_NAME s_scm_string_drop_right
581 {
582 return scm_substring (s,
583 SCM_INUM0,
584 scm_difference (scm_string_length (s), n));
585 }
586 #undef FUNC_NAME
587
588
589 SCM_DEFINE (scm_string_pad, "string-pad", 2, 3, 0,
590 (SCM s, SCM len, SCM chr, SCM start, SCM end),
591 "Take that characters from @var{start} to @var{end} from the\n"
592 "string @var{s} and return a new string, right-padded by the\n"
593 "character @var{chr} to length @var{len}. If the resulting\n"
594 "string is longer than @var{len}, it is truncated on the right.")
595 #define FUNC_NAME s_scm_string_pad
596 {
597 char cchr;
598 size_t cstart, cend, clen;
599
600 MY_VALIDATE_SUBSTRING_SPEC (1, s,
601 4, start, cstart,
602 5, end, cend);
603 clen = scm_to_size_t (len);
604
605 if (SCM_UNBNDP (chr))
606 cchr = ' ';
607 else
608 {
609 SCM_VALIDATE_CHAR (3, chr);
610 cchr = SCM_CHAR (chr);
611 }
612 if (clen < (cend - cstart))
613 return scm_c_substring (s, cend - clen, cend);
614 else
615 {
616 SCM result;
617 char *dst;
618
619 result = scm_i_make_string (clen, &dst);
620 memset (dst, cchr, (clen - (cend - cstart)));
621 memmove (dst + clen - (cend - cstart),
622 scm_i_string_chars (s) + cstart, cend - cstart);
623 return result;
624 }
625 }
626 #undef FUNC_NAME
627
628
629 SCM_DEFINE (scm_string_pad_right, "string-pad-right", 2, 3, 0,
630 (SCM s, SCM len, SCM chr, SCM start, SCM end),
631 "Take that characters from @var{start} to @var{end} from the\n"
632 "string @var{s} and return a new string, left-padded by the\n"
633 "character @var{chr} to length @var{len}. If the resulting\n"
634 "string is longer than @var{len}, it is truncated on the left.")
635 #define FUNC_NAME s_scm_string_pad_right
636 {
637 char cchr;
638 size_t cstart, cend, clen;
639
640 MY_VALIDATE_SUBSTRING_SPEC (1, s,
641 4, start, cstart,
642 5, end, cend);
643 clen = scm_to_size_t (len);
644
645 if (SCM_UNBNDP (chr))
646 cchr = ' ';
647 else
648 {
649 SCM_VALIDATE_CHAR (3, chr);
650 cchr = SCM_CHAR (chr);
651 }
652 if (clen < (cend - cstart))
653 return scm_c_substring (s, cstart, cstart + clen);
654 else
655 {
656 SCM result;
657 char *dst;
658
659 result = scm_i_make_string (clen, &dst);
660 memset (dst + (cend - cstart), cchr, clen - (cend - cstart));
661 memmove (dst, scm_i_string_chars (s) + cstart, cend - cstart);
662 return result;
663 }
664 }
665 #undef FUNC_NAME
666
667
668 SCM_DEFINE (scm_string_trim, "string-trim", 1, 3, 0,
669 (SCM s, SCM char_pred, SCM start, SCM end),
670 "Trim @var{s} by skipping over all characters on the left\n"
671 "that satisfy the parameter @var{char_pred}:\n"
672 "\n"
673 "@itemize @bullet\n"
674 "@item\n"
675 "if it is the character @var{ch}, characters equal to\n"
676 "@var{ch} are trimmed,\n"
677 "\n"
678 "@item\n"
679 "if it is a procedure @var{pred} characters that\n"
680 "satisfy @var{pred} are trimmed,\n"
681 "\n"
682 "@item\n"
683 "if it is a character set, characters in that set are trimmed.\n"
684 "@end itemize\n"
685 "\n"
686 "If called without a @var{char_pred} argument, all whitespace is\n"
687 "trimmed.")
688 #define FUNC_NAME s_scm_string_trim
689 {
690 const char *cstr;
691 size_t cstart, cend;
692
693 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
694 3, start, cstart,
695 4, end, cend);
696 if (SCM_UNBNDP (char_pred))
697 {
698 while (cstart < cend)
699 {
700 if (!isspace((int) (unsigned char) cstr[cstart]))
701 break;
702 cstart++;
703 }
704 }
705 else if (SCM_CHARP (char_pred))
706 {
707 char chr = SCM_CHAR (char_pred);
708 while (cstart < cend)
709 {
710 if (chr != cstr[cstart])
711 break;
712 cstart++;
713 }
714 }
715 else if (SCM_CHARSETP (char_pred))
716 {
717 while (cstart < cend)
718 {
719 if (!SCM_CHARSET_GET (char_pred, cstr[cstart]))
720 break;
721 cstart++;
722 }
723 }
724 else
725 {
726 SCM_VALIDATE_PROC (2, char_pred);
727 while (cstart < cend)
728 {
729 SCM res;
730
731 res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
732 if (scm_is_false (res))
733 break;
734 cstr = scm_i_string_chars (s);
735 cstart++;
736 }
737 }
738 return scm_c_substring (s, cstart, cend);
739 }
740 #undef FUNC_NAME
741
742
743 SCM_DEFINE (scm_string_trim_right, "string-trim-right", 1, 3, 0,
744 (SCM s, SCM char_pred, SCM start, SCM end),
745 "Trim @var{s} by skipping over all characters on the rightt\n"
746 "that satisfy the parameter @var{char_pred}:\n"
747 "\n"
748 "@itemize @bullet\n"
749 "@item\n"
750 "if it is the character @var{ch}, characters equal to @var{ch}\n"
751 "are trimmed,\n"
752 "\n"
753 "@item\n"
754 "if it is a procedure @var{pred} characters that satisfy\n"
755 "@var{pred} are trimmed,\n"
756 "\n"
757 "@item\n"
758 "if it is a character sets, all characters in that set are\n"
759 "trimmed.\n"
760 "@end itemize\n"
761 "\n"
762 "If called without a @var{char_pred} argument, all whitespace is\n"
763 "trimmed.")
764 #define FUNC_NAME s_scm_string_trim_right
765 {
766 const char *cstr;
767 size_t cstart, cend;
768
769 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
770 3, start, cstart,
771 4, end, cend);
772 if (SCM_UNBNDP (char_pred))
773 {
774 while (cstart < cend)
775 {
776 if (!isspace((int) (unsigned char) cstr[cend - 1]))
777 break;
778 cend--;
779 }
780 }
781 else if (SCM_CHARP (char_pred))
782 {
783 char chr = SCM_CHAR (char_pred);
784 while (cstart < cend)
785 {
786 if (chr != cstr[cend - 1])
787 break;
788 cend--;
789 }
790 }
791 else if (SCM_CHARSETP (char_pred))
792 {
793 while (cstart < cend)
794 {
795 if (!SCM_CHARSET_GET (char_pred, cstr[cend - 1]))
796 break;
797 cend--;
798 }
799 }
800 else
801 {
802 SCM_VALIDATE_PROC (2, char_pred);
803 while (cstart < cend)
804 {
805 SCM res;
806
807 res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cend - 1]));
808 if (scm_is_false (res))
809 break;
810 cstr = scm_i_string_chars (s);
811 cend--;
812 }
813 }
814 return scm_c_substring (s, cstart, cend);
815 }
816 #undef FUNC_NAME
817
818
819 SCM_DEFINE (scm_string_trim_both, "string-trim-both", 1, 3, 0,
820 (SCM s, SCM char_pred, SCM start, SCM end),
821 "Trim @var{s} by skipping over all characters on both sides of\n"
822 "the string that satisfy the parameter @var{char_pred}:\n"
823 "\n"
824 "@itemize @bullet\n"
825 "@item\n"
826 "if it is the character @var{ch}, characters equal to @var{ch}\n"
827 "are trimmed,\n"
828 "\n"
829 "@item\n"
830 "if it is a procedure @var{pred} characters that satisfy\n"
831 "@var{pred} are trimmed,\n"
832 "\n"
833 "@item\n"
834 "if it is a character set, the characters in the set are\n"
835 "trimmed.\n"
836 "@end itemize\n"
837 "\n"
838 "If called without a @var{char_pred} argument, all whitespace is\n"
839 "trimmed.")
840 #define FUNC_NAME s_scm_string_trim_both
841 {
842 const char *cstr;
843 size_t cstart, cend;
844
845 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
846 3, start, cstart,
847 4, end, cend);
848 if (SCM_UNBNDP (char_pred))
849 {
850 while (cstart < cend)
851 {
852 if (!isspace((int) (unsigned char) cstr[cstart]))
853 break;
854 cstart++;
855 }
856 while (cstart < cend)
857 {
858 if (!isspace((int) (unsigned char) cstr[cend - 1]))
859 break;
860 cend--;
861 }
862 }
863 else if (SCM_CHARP (char_pred))
864 {
865 char chr = SCM_CHAR (char_pred);
866 while (cstart < cend)
867 {
868 if (chr != cstr[cstart])
869 break;
870 cstart++;
871 }
872 while (cstart < cend)
873 {
874 if (chr != cstr[cend - 1])
875 break;
876 cend--;
877 }
878 }
879 else if (SCM_CHARSETP (char_pred))
880 {
881 while (cstart < cend)
882 {
883 if (!SCM_CHARSET_GET (char_pred, cstr[cstart]))
884 break;
885 cstart++;
886 }
887 while (cstart < cend)
888 {
889 if (!SCM_CHARSET_GET (char_pred, cstr[cend - 1]))
890 break;
891 cend--;
892 }
893 }
894 else
895 {
896 SCM_VALIDATE_PROC (2, char_pred);
897 while (cstart < cend)
898 {
899 SCM res;
900
901 res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
902 if (scm_is_false (res))
903 break;
904 cstr = scm_i_string_chars (s);
905 cstart++;
906 }
907 while (cstart < cend)
908 {
909 SCM res;
910
911 res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cend - 1]));
912 if (scm_is_false (res))
913 break;
914 cstr = scm_i_string_chars (s);
915 cend--;
916 }
917 }
918 return scm_c_substring (s, cstart, cend);
919 }
920 #undef FUNC_NAME
921
922
923 SCM_DEFINE (scm_substring_fill_x, "string-fill!", 2, 2, 0,
924 (SCM str, SCM chr, SCM start, SCM end),
925 "Stores @var{chr} in every element of the given @var{str} and\n"
926 "returns an unspecified value.")
927 #define FUNC_NAME s_scm_substring_fill_x
928 {
929 char *cstr;
930 size_t cstart, cend;
931 int c;
932 size_t k;
933
934 /* Older versions of Guile provided the function
935 scm_substring_fill_x with the following order of arguments:
936
937 str, start, end, chr
938
939 We accomodate this here by detecting such a usage and reordering
940 the arguments.
941 */
942 if (SCM_CHARP (end))
943 {
944 SCM tmp = end;
945 end = start;
946 start = chr;
947 chr = tmp;
948 }
949
950 MY_VALIDATE_SUBSTRING_SPEC (1, str,
951 3, start, cstart,
952 4, end, cend);
953 SCM_VALIDATE_CHAR_COPY (2, chr, c);
954
955 cstr = scm_i_string_writable_chars (str);
956 for (k = cstart; k < cend; k++)
957 cstr[k] = c;
958 scm_i_string_stop_writing ();
959 scm_remember_upto_here_1 (str);
960
961 return SCM_UNSPECIFIED;
962 }
963 #undef FUNC_NAME
964
965 SCM
966 scm_string_fill_x (SCM str, SCM chr)
967 {
968 return scm_substring_fill_x (str, chr, SCM_UNDEFINED, SCM_UNDEFINED);
969 }
970
971 SCM_DEFINE (scm_string_compare, "string-compare", 5, 4, 0,
972 (SCM s1, SCM s2, SCM proc_lt, SCM proc_eq, SCM proc_gt, SCM start1, SCM end1, SCM start2, SCM end2),
973 "Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the\n"
974 "mismatch index, depending upon whether @var{s1} is less than,\n"
975 "equal to, or greater than @var{s2}. The mismatch index is the\n"
976 "largest index @var{i} such that for every 0 <= @var{j} <\n"
977 "@var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] -- that is,\n"
978 "@var{i} is the first position that does not match.")
979 #define FUNC_NAME s_scm_string_compare
980 {
981 const char *cstr1, *cstr2;
982 size_t cstart1, cend1, cstart2, cend2;
983 SCM proc;
984
985 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
986 6, start1, cstart1,
987 7, end1, cend1);
988 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
989 8, start2, cstart2,
990 9, end2, cend2);
991 SCM_VALIDATE_PROC (3, proc_lt);
992 SCM_VALIDATE_PROC (4, proc_eq);
993 SCM_VALIDATE_PROC (5, proc_gt);
994
995 while (cstart1 < cend1 && cstart2 < cend2)
996 {
997 if (cstr1[cstart1] < cstr2[cstart2])
998 {
999 proc = proc_lt;
1000 goto ret;
1001 }
1002 else if (cstr1[cstart1] > cstr2[cstart2])
1003 {
1004 proc = proc_gt;
1005 goto ret;
1006 }
1007 cstart1++;
1008 cstart2++;
1009 }
1010 if (cstart1 < cend1)
1011 proc = proc_gt;
1012 else if (cstart2 < cend2)
1013 proc = proc_lt;
1014 else
1015 proc = proc_eq;
1016
1017 ret:
1018 scm_remember_upto_here_2 (s1, s2);
1019 return scm_call_1 (proc, scm_from_size_t (cstart1));
1020 }
1021 #undef FUNC_NAME
1022
1023
1024 SCM_DEFINE (scm_string_compare_ci, "string-compare-ci", 5, 4, 0,
1025 (SCM s1, SCM s2, SCM proc_lt, SCM proc_eq, SCM proc_gt, SCM start1, SCM end1, SCM start2, SCM end2),
1026 "Apply @var{proc_lt}, @var{proc_eq}, @var{proc_gt} to the\n"
1027 "mismatch index, depending upon whether @var{s1} is less than,\n"
1028 "equal to, or greater than @var{s2}. The mismatch index is the\n"
1029 "largest index @var{i} such that for every 0 <= @var{j} <\n"
1030 "@var{i}, @var{s1}[@var{j}] = @var{s2}[@var{j}] -- that is,\n"
1031 "@var{i} is the first position that does not match. The\n"
1032 "character comparison is done case-insensitively.")
1033 #define FUNC_NAME s_scm_string_compare_ci
1034 {
1035 const char *cstr1, *cstr2;
1036 size_t cstart1, cend1, cstart2, cend2;
1037 SCM proc;
1038
1039 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1040 6, start1, cstart1,
1041 7, end1, cend1);
1042 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1043 8, start2, cstart2,
1044 9, end2, cend2);
1045 SCM_VALIDATE_PROC (3, proc_lt);
1046 SCM_VALIDATE_PROC (4, proc_eq);
1047 SCM_VALIDATE_PROC (5, proc_gt);
1048
1049 while (cstart1 < cend1 && cstart2 < cend2)
1050 {
1051 if (scm_c_downcase (cstr1[cstart1]) < scm_c_downcase (cstr2[cstart2]))
1052 {
1053 proc = proc_lt;
1054 goto ret;
1055 }
1056 else if (scm_c_downcase (cstr1[cstart1])
1057 > scm_c_downcase (cstr2[cstart2]))
1058 {
1059 proc = proc_gt;
1060 goto ret;
1061 }
1062 cstart1++;
1063 cstart2++;
1064 }
1065
1066 if (cstart1 < cend1)
1067 proc = proc_gt;
1068 else if (cstart2 < cend2)
1069 proc = proc_lt;
1070 else
1071 proc = proc_eq;
1072
1073 ret:
1074 scm_remember_upto_here (s1, s2);
1075 return scm_call_1 (proc, scm_from_size_t (cstart1));
1076 }
1077 #undef FUNC_NAME
1078
1079
1080 SCM_DEFINE (scm_string_eq, "string=", 2, 4, 0,
1081 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1082 "Return @code{#f} if @var{s1} and @var{s2} are not equal, a true\n"
1083 "value otherwise.")
1084 #define FUNC_NAME s_scm_string_eq
1085 {
1086 const char *cstr1, *cstr2;
1087 size_t cstart1, cend1, cstart2, cend2;
1088
1089 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1090 3, start1, cstart1,
1091 4, end1, cend1);
1092 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1093 5, start2, cstart2,
1094 6, end2, cend2);
1095
1096 if ((cend1 - cstart1) != (cend2 - cstart2))
1097 goto false;
1098
1099 while (cstart1 < cend1)
1100 {
1101 if (cstr1[cstart1] < cstr2[cstart2])
1102 goto false;
1103 else if (cstr1[cstart1] > cstr2[cstart2])
1104 goto false;
1105 cstart1++;
1106 cstart2++;
1107 }
1108
1109 scm_remember_upto_here_2 (s1, s2);
1110 return scm_from_size_t (cstart1);
1111
1112 false:
1113 scm_remember_upto_here_2 (s1, s2);
1114 return SCM_BOOL_F;
1115 }
1116 #undef FUNC_NAME
1117
1118
1119 SCM_DEFINE (scm_string_neq, "string<>", 2, 4, 0,
1120 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1121 "Return @code{#f} if @var{s1} and @var{s2} are equal, a true\n"
1122 "value otherwise.")
1123 #define FUNC_NAME s_scm_string_neq
1124 {
1125 const char *cstr1, *cstr2;
1126 size_t cstart1, cend1, cstart2, cend2;
1127
1128 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1129 3, start1, cstart1,
1130 4, end1, cend1);
1131 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1132 5, start2, cstart2,
1133 6, end2, cend2);
1134
1135 while (cstart1 < cend1 && cstart2 < cend2)
1136 {
1137 if (cstr1[cstart1] < cstr2[cstart2])
1138 goto true;
1139 else if (cstr1[cstart1] > cstr2[cstart2])
1140 goto true;
1141 cstart1++;
1142 cstart2++;
1143 }
1144 if (cstart1 < cend1)
1145 goto true;
1146 else if (cstart2 < cend2)
1147 goto true;
1148 else
1149 goto false;
1150
1151 true:
1152 scm_remember_upto_here_2 (s1, s2);
1153 return scm_from_size_t (cstart1);
1154
1155 false:
1156 scm_remember_upto_here_2 (s1, s2);
1157 return SCM_BOOL_F;
1158 }
1159 #undef FUNC_NAME
1160
1161
1162 SCM_DEFINE (scm_string_lt, "string<", 2, 4, 0,
1163 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1164 "Return @code{#f} if @var{s1} is greater or equal to @var{s2}, a\n"
1165 "true value otherwise.")
1166 #define FUNC_NAME s_scm_string_lt
1167 {
1168 const char *cstr1, *cstr2;
1169 size_t cstart1, cend1, cstart2, cend2;
1170
1171 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1172 3, start1, cstart1,
1173 4, end1, cend1);
1174 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1175 5, start2, cstart2,
1176 6, end2, cend2);
1177
1178 while (cstart1 < cend1 && cstart2 < cend2)
1179 {
1180 if (cstr1[cstart1] < cstr2[cstart2])
1181 goto true;
1182 else if (cstr1[cstart1] > cstr2[cstart2])
1183 goto false;
1184 cstart1++;
1185 cstart2++;
1186 }
1187 if (cstart1 < cend1)
1188 goto false;
1189 else if (cstart2 < cend2)
1190 goto true;
1191 else
1192 goto false;
1193
1194 true:
1195 scm_remember_upto_here_2 (s1, s2);
1196 return scm_from_size_t (cstart1);
1197
1198 false:
1199 scm_remember_upto_here_2 (s1, s2);
1200 return SCM_BOOL_F;
1201 }
1202 #undef FUNC_NAME
1203
1204
1205 SCM_DEFINE (scm_string_gt, "string>", 2, 4, 0,
1206 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1207 "Return @code{#f} if @var{s1} is less or equal to @var{s2}, a\n"
1208 "true value otherwise.")
1209 #define FUNC_NAME s_scm_string_gt
1210 {
1211 const char *cstr1, *cstr2;
1212 size_t cstart1, cend1, cstart2, cend2;
1213
1214 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1215 3, start1, cstart1,
1216 4, end1, cend1);
1217 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1218 5, start2, cstart2,
1219 6, end2, cend2);
1220
1221 while (cstart1 < cend1 && cstart2 < cend2)
1222 {
1223 if (cstr1[cstart1] < cstr2[cstart2])
1224 goto false;
1225 else if (cstr1[cstart1] > cstr2[cstart2])
1226 goto true;
1227 cstart1++;
1228 cstart2++;
1229 }
1230 if (cstart1 < cend1)
1231 goto true;
1232 else if (cstart2 < cend2)
1233 goto false;
1234 else
1235 goto false;
1236
1237 true:
1238 scm_remember_upto_here_2 (s1, s2);
1239 return scm_from_size_t (cstart1);
1240
1241 false:
1242 scm_remember_upto_here_2 (s1, s2);
1243 return SCM_BOOL_F;
1244 }
1245 #undef FUNC_NAME
1246
1247
1248 SCM_DEFINE (scm_string_le, "string<=", 2, 4, 0,
1249 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1250 "Return @code{#f} if @var{s1} is greater to @var{s2}, a true\n"
1251 "value otherwise.")
1252 #define FUNC_NAME s_scm_string_le
1253 {
1254 const char *cstr1, *cstr2;
1255 size_t cstart1, cend1, cstart2, cend2;
1256
1257 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1258 3, start1, cstart1,
1259 4, end1, cend1);
1260 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1261 5, start2, cstart2,
1262 6, end2, cend2);
1263
1264 while (cstart1 < cend1 && cstart2 < cend2)
1265 {
1266 if (cstr1[cstart1] < cstr2[cstart2])
1267 goto true;
1268 else if (cstr1[cstart1] > cstr2[cstart2])
1269 goto false;
1270 cstart1++;
1271 cstart2++;
1272 }
1273 if (cstart1 < cend1)
1274 goto false;
1275 else if (cstart2 < cend2)
1276 goto true;
1277 else
1278 goto true;
1279
1280 true:
1281 scm_remember_upto_here_2 (s1, s2);
1282 return scm_from_size_t (cstart1);
1283
1284 false:
1285 scm_remember_upto_here_2 (s1, s2);
1286 return SCM_BOOL_F;
1287 }
1288 #undef FUNC_NAME
1289
1290
1291 SCM_DEFINE (scm_string_ge, "string>=", 2, 4, 0,
1292 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1293 "Return @code{#f} if @var{s1} is less to @var{s2}, a true value\n"
1294 "otherwise.")
1295 #define FUNC_NAME s_scm_string_ge
1296 {
1297 const char *cstr1, *cstr2;
1298 size_t cstart1, cend1, cstart2, cend2;
1299
1300 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1301 3, start1, cstart1,
1302 4, end1, cend1);
1303 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1304 5, start2, cstart2,
1305 6, end2, cend2);
1306
1307 while (cstart1 < cend1 && cstart2 < cend2)
1308 {
1309 if (cstr1[cstart1] < cstr2[cstart2])
1310 goto false;
1311 else if (cstr1[cstart1] > cstr2[cstart2])
1312 goto true;
1313 cstart1++;
1314 cstart2++;
1315 }
1316 if (cstart1 < cend1)
1317 goto true;
1318 else if (cstart2 < cend2)
1319 goto false;
1320 else
1321 goto true;
1322
1323 true:
1324 scm_remember_upto_here_2 (s1, s2);
1325 return scm_from_size_t (cstart1);
1326
1327 false:
1328 scm_remember_upto_here_2 (s1, s2);
1329 return SCM_BOOL_F;
1330 }
1331 #undef FUNC_NAME
1332
1333
1334 SCM_DEFINE (scm_string_ci_eq, "string-ci=", 2, 4, 0,
1335 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1336 "Return @code{#f} if @var{s1} and @var{s2} are not equal, a true\n"
1337 "value otherwise. The character comparison is done\n"
1338 "case-insensitively.")
1339 #define FUNC_NAME s_scm_string_ci_eq
1340 {
1341 const char *cstr1, *cstr2;
1342 size_t cstart1, cend1, cstart2, cend2;
1343
1344 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1345 3, start1, cstart1,
1346 4, end1, cend1);
1347 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1348 5, start2, cstart2,
1349 6, end2, cend2);
1350
1351 while (cstart1 < cend1 && cstart2 < cend2)
1352 {
1353 if (scm_c_downcase (cstr1[cstart1]) < scm_c_downcase (cstr2[cstart2]))
1354 goto false;
1355 else if (scm_c_downcase (cstr1[cstart1]) > scm_c_downcase (cstr2[cstart2]))
1356 goto false;
1357 cstart1++;
1358 cstart2++;
1359 }
1360 if (cstart1 < cend1)
1361 goto false;
1362 else if (cstart2 < cend2)
1363 goto false;
1364 else
1365 goto true;
1366
1367 true:
1368 scm_remember_upto_here_2 (s1, s2);
1369 return scm_from_size_t (cstart1);
1370
1371 false:
1372 scm_remember_upto_here_2 (s1, s2);
1373 return SCM_BOOL_F;
1374 }
1375 #undef FUNC_NAME
1376
1377
1378 SCM_DEFINE (scm_string_ci_neq, "string-ci<>", 2, 4, 0,
1379 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1380 "Return @code{#f} if @var{s1} and @var{s2} are equal, a true\n"
1381 "value otherwise. The character comparison is done\n"
1382 "case-insensitively.")
1383 #define FUNC_NAME s_scm_string_ci_neq
1384 {
1385 const char *cstr1, *cstr2;
1386 size_t cstart1, cend1, cstart2, cend2;
1387
1388 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1389 3, start1, cstart1,
1390 4, end1, cend1);
1391 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1392 5, start2, cstart2,
1393 6, end2, cend2);
1394
1395 while (cstart1 < cend1 && cstart2 < cend2)
1396 {
1397 if (scm_c_downcase (cstr1[cstart1]) < scm_c_downcase (cstr2[cstart2]))
1398 goto true;
1399 else if (scm_c_downcase (cstr1[cstart1]) > scm_c_downcase (cstr2[cstart2]))
1400 goto true;
1401 cstart1++;
1402 cstart2++;
1403 }
1404 if (cstart1 < cend1)
1405 goto true;
1406 else if (cstart2 < cend2)
1407 goto true;
1408 else
1409 goto false;
1410
1411 true:
1412 scm_remember_upto_here_2 (s1, s2);
1413 return scm_from_size_t (cstart1);
1414
1415 false:
1416 scm_remember_upto_here_2 (s1, s2);
1417 return SCM_BOOL_F;
1418 }
1419 #undef FUNC_NAME
1420
1421
1422 SCM_DEFINE (scm_string_ci_lt, "string-ci<", 2, 4, 0,
1423 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1424 "Return @code{#f} if @var{s1} is greater or equal to @var{s2}, a\n"
1425 "true value otherwise. The character comparison is done\n"
1426 "case-insensitively.")
1427 #define FUNC_NAME s_scm_string_ci_lt
1428 {
1429 const char *cstr1, *cstr2;
1430 size_t cstart1, cend1, cstart2, cend2;
1431
1432 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1433 3, start1, cstart1,
1434 4, end1, cend1);
1435 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1436 5, start2, cstart2,
1437 6, end2, cend2);
1438
1439 while (cstart1 < cend1 && cstart2 < cend2)
1440 {
1441 if (scm_c_downcase (cstr1[cstart1]) < scm_c_downcase (cstr2[cstart2]))
1442 goto true;
1443 else if (scm_c_downcase (cstr1[cstart1]) > scm_c_downcase (cstr2[cstart2]))
1444 goto false;
1445 cstart1++;
1446 cstart2++;
1447 }
1448 if (cstart1 < cend1)
1449 goto false;
1450 else if (cstart2 < cend2)
1451 goto true;
1452 else
1453 goto false;
1454
1455 true:
1456 scm_remember_upto_here_2 (s1, s2);
1457 return scm_from_size_t (cstart1);
1458
1459 false:
1460 scm_remember_upto_here_2 (s1, s2);
1461 return SCM_BOOL_F;
1462 }
1463 #undef FUNC_NAME
1464
1465
1466 SCM_DEFINE (scm_string_ci_gt, "string-ci>", 2, 4, 0,
1467 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1468 "Return @code{#f} if @var{s1} is less or equal to @var{s2}, a\n"
1469 "true value otherwise. The character comparison is done\n"
1470 "case-insensitively.")
1471 #define FUNC_NAME s_scm_string_ci_gt
1472 {
1473 const char *cstr1, *cstr2;
1474 size_t cstart1, cend1, cstart2, cend2;
1475
1476 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1477 3, start1, cstart1,
1478 4, end1, cend1);
1479 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1480 5, start2, cstart2,
1481 6, end2, cend2);
1482
1483 while (cstart1 < cend1 && cstart2 < cend2)
1484 {
1485 if (scm_c_downcase (cstr1[cstart1]) < scm_c_downcase (cstr2[cstart2]))
1486 goto false;
1487 else if (scm_c_downcase (cstr1[cstart1]) > scm_c_downcase (cstr2[cstart2]))
1488 goto true;
1489 cstart1++;
1490 cstart2++;
1491 }
1492 if (cstart1 < cend1)
1493 goto true;
1494 else if (cstart2 < cend2)
1495 goto false;
1496 else
1497 goto false;
1498
1499 true:
1500 scm_remember_upto_here_2 (s1, s2);
1501 return scm_from_size_t (cstart1);
1502
1503 false:
1504 scm_remember_upto_here_2 (s1, s2);
1505 return SCM_BOOL_F;
1506 }
1507 #undef FUNC_NAME
1508
1509
1510 SCM_DEFINE (scm_string_ci_le, "string-ci<=", 2, 4, 0,
1511 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1512 "Return @code{#f} if @var{s1} is greater to @var{s2}, a true\n"
1513 "value otherwise. The character comparison is done\n"
1514 "case-insensitively.")
1515 #define FUNC_NAME s_scm_string_ci_le
1516 {
1517 const char *cstr1, *cstr2;
1518 size_t cstart1, cend1, cstart2, cend2;
1519
1520 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1521 3, start1, cstart1,
1522 4, end1, cend1);
1523 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1524 5, start2, cstart2,
1525 6, end2, cend2);
1526
1527 while (cstart1 < cend1 && cstart2 < cend2)
1528 {
1529 if (scm_c_downcase (cstr1[cstart1]) < scm_c_downcase (cstr2[cstart2]))
1530 goto true;
1531 else if (scm_c_downcase (cstr1[cstart1]) > scm_c_downcase (cstr2[cstart2]))
1532 goto false;
1533 cstart1++;
1534 cstart2++;
1535 }
1536 if (cstart1 < cend1)
1537 goto false;
1538 else if (cstart2 < cend2)
1539 goto true;
1540 else
1541 goto true;
1542
1543 true:
1544 scm_remember_upto_here_2 (s1, s2);
1545 return scm_from_size_t (cstart1);
1546
1547 false:
1548 scm_remember_upto_here_2 (s1, s2);
1549 return SCM_BOOL_F;
1550 }
1551 #undef FUNC_NAME
1552
1553
1554 SCM_DEFINE (scm_string_ci_ge, "string-ci>=", 2, 4, 0,
1555 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1556 "Return @code{#f} if @var{s1} is less to @var{s2}, a true value\n"
1557 "otherwise. The character comparison is done\n"
1558 "case-insensitively.")
1559 #define FUNC_NAME s_scm_string_ci_ge
1560 {
1561 const char *cstr1, *cstr2;
1562 size_t cstart1, cend1, cstart2, cend2;
1563
1564 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1565 3, start1, cstart1,
1566 4, end1, cend1);
1567 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1568 5, start2, cstart2,
1569 6, end2, cend2);
1570
1571 while (cstart1 < cend1 && cstart2 < cend2)
1572 {
1573 if (scm_c_downcase (cstr1[cstart1]) < scm_c_downcase (cstr2[cstart2]))
1574 goto false;
1575 else if (scm_c_downcase (cstr1[cstart1]) > scm_c_downcase (cstr2[cstart2]))
1576 goto true;
1577 cstart1++;
1578 cstart2++;
1579 }
1580 if (cstart1 < cend1)
1581 goto true;
1582 else if (cstart2 < cend2)
1583 goto false;
1584 else
1585 goto true;
1586
1587 true:
1588 scm_remember_upto_here_2 (s1, s2);
1589 return scm_from_size_t (cstart1);
1590
1591 false:
1592 scm_remember_upto_here_2 (s1, s2);
1593 return SCM_BOOL_F;
1594 }
1595 #undef FUNC_NAME
1596
1597 SCM_DEFINE (scm_substring_hash, "string-hash", 1, 3, 0,
1598 (SCM s, SCM bound, SCM start, SCM end),
1599 "Compute a hash value for @var{S}. the optional argument "
1600 "@var{bound} is a non-negative exact "
1601 "integer specifying the range of the hash function. "
1602 "A positive value restricts the return value to the "
1603 "range [0,bound).")
1604 #define FUNC_NAME s_scm_substring_hash
1605 {
1606 if (SCM_UNBNDP (bound))
1607 bound = scm_from_intmax (SCM_MOST_POSITIVE_FIXNUM);
1608 if (SCM_UNBNDP (start))
1609 start = SCM_INUM0;
1610 return scm_hash (scm_substring_shared (s, start, end), bound);
1611 }
1612 #undef FUNC_NAME
1613
1614 SCM_DEFINE (scm_substring_hash_ci, "string-hash-ci", 1, 3, 0,
1615 (SCM s, SCM bound, SCM start, SCM end),
1616 "Compute a hash value for @var{S}. the optional argument "
1617 "@var{bound} is a non-negative exact "
1618 "integer specifying the range of the hash function. "
1619 "A positive value restricts the return value to the "
1620 "range [0,bound).")
1621 #define FUNC_NAME s_scm_substring_hash_ci
1622 {
1623 return scm_substring_hash (scm_substring_downcase (s, start, end),
1624 bound,
1625 SCM_UNDEFINED, SCM_UNDEFINED);
1626 }
1627 #undef FUNC_NAME
1628
1629 SCM_DEFINE (scm_string_prefix_length, "string-prefix-length", 2, 4, 0,
1630 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1631 "Return the length of the longest common prefix of the two\n"
1632 "strings.")
1633 #define FUNC_NAME s_scm_string_prefix_length
1634 {
1635 const char *cstr1, *cstr2;
1636 size_t cstart1, cend1, cstart2, cend2;
1637 size_t len = 0;
1638
1639 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1640 3, start1, cstart1,
1641 4, end1, cend1);
1642 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1643 5, start2, cstart2,
1644 6, end2, cend2);
1645 while (cstart1 < cend1 && cstart2 < cend2)
1646 {
1647 if (cstr1[cstart1] != cstr2[cstart2])
1648 goto ret;
1649 len++;
1650 cstart1++;
1651 cstart2++;
1652 }
1653
1654 ret:
1655 scm_remember_upto_here_2 (s1, s2);
1656 return scm_from_size_t (len);
1657 }
1658 #undef FUNC_NAME
1659
1660
1661 SCM_DEFINE (scm_string_prefix_length_ci, "string-prefix-length-ci", 2, 4, 0,
1662 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1663 "Return the length of the longest common prefix of the two\n"
1664 "strings, ignoring character case.")
1665 #define FUNC_NAME s_scm_string_prefix_length_ci
1666 {
1667 const char *cstr1, *cstr2;
1668 size_t cstart1, cend1, cstart2, cend2;
1669 size_t len = 0;
1670
1671 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1672 3, start1, cstart1,
1673 4, end1, cend1);
1674 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1675 5, start2, cstart2,
1676 6, end2, cend2);
1677 while (cstart1 < cend1 && cstart2 < cend2)
1678 {
1679 if (scm_c_downcase (cstr1[cstart1]) != scm_c_downcase (cstr2[cstart2]))
1680 goto ret;
1681 len++;
1682 cstart1++;
1683 cstart2++;
1684 }
1685
1686 ret:
1687 scm_remember_upto_here_2 (s1, s2);
1688 return scm_from_size_t (len);
1689 }
1690 #undef FUNC_NAME
1691
1692
1693 SCM_DEFINE (scm_string_suffix_length, "string-suffix-length", 2, 4, 0,
1694 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1695 "Return the length of the longest common suffix of the two\n"
1696 "strings.")
1697 #define FUNC_NAME s_scm_string_suffix_length
1698 {
1699 const char *cstr1, *cstr2;
1700 size_t cstart1, cend1, cstart2, cend2;
1701 size_t len = 0;
1702
1703 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1704 3, start1, cstart1,
1705 4, end1, cend1);
1706 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1707 5, start2, cstart2,
1708 6, end2, cend2);
1709 while (cstart1 < cend1 && cstart2 < cend2)
1710 {
1711 cend1--;
1712 cend2--;
1713 if (cstr1[cend1] != cstr2[cend2])
1714 goto ret;
1715 len++;
1716 }
1717
1718 ret:
1719 scm_remember_upto_here_2 (s1, s2);
1720 return scm_from_size_t (len);
1721 }
1722 #undef FUNC_NAME
1723
1724
1725 SCM_DEFINE (scm_string_suffix_length_ci, "string-suffix-length-ci", 2, 4, 0,
1726 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1727 "Return the length of the longest common suffix of the two\n"
1728 "strings, ignoring character case.")
1729 #define FUNC_NAME s_scm_string_suffix_length_ci
1730 {
1731 const char *cstr1, *cstr2;
1732 size_t cstart1, cend1, cstart2, cend2;
1733 size_t len = 0;
1734
1735 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1736 3, start1, cstart1,
1737 4, end1, cend1);
1738 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1739 5, start2, cstart2,
1740 6, end2, cend2);
1741 while (cstart1 < cend1 && cstart2 < cend2)
1742 {
1743 cend1--;
1744 cend2--;
1745 if (scm_c_downcase (cstr1[cend1]) != scm_c_downcase (cstr2[cend2]))
1746 goto ret;
1747 len++;
1748 }
1749
1750 ret:
1751 scm_remember_upto_here_2 (s1, s2);
1752 return scm_from_size_t (len);
1753 }
1754 #undef FUNC_NAME
1755
1756
1757 SCM_DEFINE (scm_string_prefix_p, "string-prefix?", 2, 4, 0,
1758 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1759 "Is @var{s1} a prefix of @var{s2}?")
1760 #define FUNC_NAME s_scm_string_prefix_p
1761 {
1762 const char *cstr1, *cstr2;
1763 size_t cstart1, cend1, cstart2, cend2;
1764 size_t len = 0, len1;
1765
1766 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1767 3, start1, cstart1,
1768 4, end1, cend1);
1769 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1770 5, start2, cstart2,
1771 6, end2, cend2);
1772 len1 = cend1 - cstart1;
1773 while (cstart1 < cend1 && cstart2 < cend2)
1774 {
1775 if (cstr1[cstart1] != cstr2[cstart2])
1776 goto ret;
1777 len++;
1778 cstart1++;
1779 cstart2++;
1780 }
1781
1782 ret:
1783 scm_remember_upto_here_2 (s1, s2);
1784 return scm_from_bool (len == len1);
1785 }
1786 #undef FUNC_NAME
1787
1788
1789 SCM_DEFINE (scm_string_prefix_ci_p, "string-prefix-ci?", 2, 4, 0,
1790 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1791 "Is @var{s1} a prefix of @var{s2}, ignoring character case?")
1792 #define FUNC_NAME s_scm_string_prefix_ci_p
1793 {
1794 const char *cstr1, *cstr2;
1795 size_t cstart1, cend1, cstart2, cend2;
1796 size_t len = 0, len1;
1797
1798 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1799 3, start1, cstart1,
1800 4, end1, cend1);
1801 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1802 5, start2, cstart2,
1803 6, end2, cend2);
1804 len1 = cend1 - cstart1;
1805 while (cstart1 < cend1 && cstart2 < cend2)
1806 {
1807 if (scm_c_downcase (cstr1[cstart1]) != scm_c_downcase (cstr2[cstart2]))
1808 goto ret;
1809 len++;
1810 cstart1++;
1811 cstart2++;
1812 }
1813
1814 ret:
1815 scm_remember_upto_here_2 (s1, s2);
1816 return scm_from_bool (len == len1);
1817 }
1818 #undef FUNC_NAME
1819
1820
1821 SCM_DEFINE (scm_string_suffix_p, "string-suffix?", 2, 4, 0,
1822 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1823 "Is @var{s1} a suffix of @var{s2}?")
1824 #define FUNC_NAME s_scm_string_suffix_p
1825 {
1826 const char *cstr1, *cstr2;
1827 size_t cstart1, cend1, cstart2, cend2;
1828 size_t len = 0, len1;
1829
1830 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1831 3, start1, cstart1,
1832 4, end1, cend1);
1833 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1834 5, start2, cstart2,
1835 6, end2, cend2);
1836 len1 = cend1 - cstart1;
1837 while (cstart1 < cend1 && cstart2 < cend2)
1838 {
1839 cend1--;
1840 cend2--;
1841 if (cstr1[cend1] != cstr2[cend2])
1842 goto ret;
1843 len++;
1844 }
1845
1846 ret:
1847 scm_remember_upto_here_2 (s1, s2);
1848 return scm_from_bool (len == len1);
1849 }
1850 #undef FUNC_NAME
1851
1852
1853 SCM_DEFINE (scm_string_suffix_ci_p, "string-suffix-ci?", 2, 4, 0,
1854 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
1855 "Is @var{s1} a suffix of @var{s2}, ignoring character case?")
1856 #define FUNC_NAME s_scm_string_suffix_ci_p
1857 {
1858 const char *cstr1, *cstr2;
1859 size_t cstart1, cend1, cstart2, cend2;
1860 size_t len = 0, len1;
1861
1862 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cstr1,
1863 3, start1, cstart1,
1864 4, end1, cend1);
1865 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cstr2,
1866 5, start2, cstart2,
1867 6, end2, cend2);
1868 len1 = cend1 - cstart1;
1869 while (cstart1 < cend1 && cstart2 < cend2)
1870 {
1871 cend1--;
1872 cend2--;
1873 if (scm_c_downcase (cstr1[cend1]) != scm_c_downcase (cstr2[cend2]))
1874 goto ret;
1875 len++;
1876 }
1877
1878 ret:
1879 scm_remember_upto_here_2 (s1, s2);
1880 return scm_from_bool (len == len1);
1881 }
1882 #undef FUNC_NAME
1883
1884
1885 SCM_DEFINE (scm_string_index, "string-index", 2, 2, 0,
1886 (SCM s, SCM char_pred, SCM start, SCM end),
1887 "Search through the string @var{s} from left to right, returning\n"
1888 "the index of the first occurence of a character which\n"
1889 "\n"
1890 "@itemize @bullet\n"
1891 "@item\n"
1892 "equals @var{char_pred}, if it is character,\n"
1893 "\n"
1894 "@item\n"
1895 "satisifies the predicate @var{char_pred}, if it is a procedure,\n"
1896 "\n"
1897 "@item\n"
1898 "is in the set @var{char_pred}, if it is a character set.\n"
1899 "@end itemize")
1900 #define FUNC_NAME s_scm_string_index
1901 {
1902 const char *cstr;
1903 size_t cstart, cend;
1904
1905 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
1906 3, start, cstart,
1907 4, end, cend);
1908 if (SCM_CHARP (char_pred))
1909 {
1910 char cchr = SCM_CHAR (char_pred);
1911 while (cstart < cend)
1912 {
1913 if (cchr == cstr[cstart])
1914 goto found;
1915 cstart++;
1916 }
1917 }
1918 else if (SCM_CHARSETP (char_pred))
1919 {
1920 while (cstart < cend)
1921 {
1922 if (SCM_CHARSET_GET (char_pred, cstr[cstart]))
1923 goto found;
1924 cstart++;
1925 }
1926 }
1927 else
1928 {
1929 SCM_VALIDATE_PROC (2, char_pred);
1930 while (cstart < cend)
1931 {
1932 SCM res;
1933 res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
1934 if (scm_is_true (res))
1935 goto found;
1936 cstr = scm_i_string_chars (s);
1937 cstart++;
1938 }
1939 }
1940
1941 scm_remember_upto_here_1 (s);
1942 return SCM_BOOL_F;
1943
1944 found:
1945 scm_remember_upto_here_1 (s);
1946 return scm_from_size_t (cstart);
1947 }
1948 #undef FUNC_NAME
1949
1950 SCM_DEFINE (scm_string_index_right, "string-index-right", 2, 2, 0,
1951 (SCM s, SCM char_pred, SCM start, SCM end),
1952 "Search through the string @var{s} from right to left, returning\n"
1953 "the index of the last occurence of a character which\n"
1954 "\n"
1955 "@itemize @bullet\n"
1956 "@item\n"
1957 "equals @var{char_pred}, if it is character,\n"
1958 "\n"
1959 "@item\n"
1960 "satisifies the predicate @var{char_pred}, if it is a procedure,\n"
1961 "\n"
1962 "@item\n"
1963 "is in the set if @var{char_pred} is a character set.\n"
1964 "@end itemize")
1965 #define FUNC_NAME s_scm_string_index_right
1966 {
1967 const char *cstr;
1968 size_t cstart, cend;
1969
1970 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
1971 3, start, cstart,
1972 4, end, cend);
1973 if (SCM_CHARP (char_pred))
1974 {
1975 char cchr = SCM_CHAR (char_pred);
1976 while (cstart < cend)
1977 {
1978 cend--;
1979 if (cchr == cstr[cend])
1980 goto found;
1981 }
1982 }
1983 else if (SCM_CHARSETP (char_pred))
1984 {
1985 while (cstart < cend)
1986 {
1987 cend--;
1988 if (SCM_CHARSET_GET (char_pred, cstr[cend]))
1989 goto found;
1990 }
1991 }
1992 else
1993 {
1994 SCM_VALIDATE_PROC (2, char_pred);
1995 while (cstart < cend)
1996 {
1997 SCM res;
1998 cend--;
1999 res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cend]));
2000 if (scm_is_true (res))
2001 goto found;
2002 cstr = scm_i_string_chars (s);
2003 }
2004 }
2005
2006 scm_remember_upto_here_1 (s);
2007 return SCM_BOOL_F;
2008
2009 found:
2010 scm_remember_upto_here_1 (s);
2011 return scm_from_size_t (cend);
2012 }
2013 #undef FUNC_NAME
2014
2015 SCM_DEFINE (scm_string_rindex, "string-rindex", 2, 2, 0,
2016 (SCM s, SCM char_pred, SCM start, SCM end),
2017 "Search through the string @var{s} from right to left, returning\n"
2018 "the index of the last occurence of a character which\n"
2019 "\n"
2020 "@itemize @bullet\n"
2021 "@item\n"
2022 "equals @var{char_pred}, if it is character,\n"
2023 "\n"
2024 "@item\n"
2025 "satisifies the predicate @var{char_pred}, if it is a procedure,\n"
2026 "\n"
2027 "@item\n"
2028 "is in the set if @var{char_pred} is a character set.\n"
2029 "@end itemize")
2030 #define FUNC_NAME s_scm_string_rindex
2031 {
2032 return scm_string_index_right (s, char_pred, start, end);
2033 }
2034 #undef FUNC_NAME
2035
2036 SCM_DEFINE (scm_string_skip, "string-skip", 2, 2, 0,
2037 (SCM s, SCM char_pred, SCM start, SCM end),
2038 "Search through the string @var{s} from left to right, returning\n"
2039 "the index of the first occurence of a character which\n"
2040 "\n"
2041 "@itemize @bullet\n"
2042 "@item\n"
2043 "does not equal @var{char_pred}, if it is character,\n"
2044 "\n"
2045 "@item\n"
2046 "does not satisify the predicate @var{char_pred}, if it is a\n"
2047 "procedure,\n"
2048 "\n"
2049 "@item\n"
2050 "is not in the set if @var{char_pred} is a character set.\n"
2051 "@end itemize")
2052 #define FUNC_NAME s_scm_string_skip
2053 {
2054 const char *cstr;
2055 size_t cstart, cend;
2056
2057 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
2058 3, start, cstart,
2059 4, end, cend);
2060 if (SCM_CHARP (char_pred))
2061 {
2062 char cchr = SCM_CHAR (char_pred);
2063 while (cstart < cend)
2064 {
2065 if (cchr != cstr[cstart])
2066 goto found;
2067 cstart++;
2068 }
2069 }
2070 else if (SCM_CHARSETP (char_pred))
2071 {
2072 while (cstart < cend)
2073 {
2074 if (!SCM_CHARSET_GET (char_pred, cstr[cstart]))
2075 goto found;
2076 cstart++;
2077 }
2078 }
2079 else
2080 {
2081 SCM_VALIDATE_PROC (2, char_pred);
2082 while (cstart < cend)
2083 {
2084 SCM res;
2085 res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
2086 if (scm_is_false (res))
2087 goto found;
2088 cstr = scm_i_string_chars (s);
2089 cstart++;
2090 }
2091 }
2092
2093 scm_remember_upto_here_1 (s);
2094 return SCM_BOOL_F;
2095
2096 found:
2097 scm_remember_upto_here_1 (s);
2098 return scm_from_size_t (cstart);
2099 }
2100 #undef FUNC_NAME
2101
2102
2103 SCM_DEFINE (scm_string_skip_right, "string-skip-right", 2, 2, 0,
2104 (SCM s, SCM char_pred, SCM start, SCM end),
2105 "Search through the string @var{s} from right to left, returning\n"
2106 "the index of the last occurence of a character which\n"
2107 "\n"
2108 "@itemize @bullet\n"
2109 "@item\n"
2110 "does not equal @var{char_pred}, if it is character,\n"
2111 "\n"
2112 "@item\n"
2113 "does not satisfy the predicate @var{char_pred}, if it is a\n"
2114 "procedure,\n"
2115 "\n"
2116 "@item\n"
2117 "is not in the set if @var{char_pred} is a character set.\n"
2118 "@end itemize")
2119 #define FUNC_NAME s_scm_string_skip_right
2120 {
2121 const char *cstr;
2122 size_t cstart, cend;
2123
2124 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
2125 3, start, cstart,
2126 4, end, cend);
2127 if (SCM_CHARP (char_pred))
2128 {
2129 char cchr = SCM_CHAR (char_pred);
2130 while (cstart < cend)
2131 {
2132 cend--;
2133 if (cchr != cstr[cend])
2134 goto found;
2135 }
2136 }
2137 else if (SCM_CHARSETP (char_pred))
2138 {
2139 while (cstart < cend)
2140 {
2141 cend--;
2142 if (!SCM_CHARSET_GET (char_pred, cstr[cend]))
2143 goto found;
2144 }
2145 }
2146 else
2147 {
2148 SCM_VALIDATE_PROC (2, char_pred);
2149 while (cstart < cend)
2150 {
2151 SCM res;
2152 cend--;
2153 res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cend]));
2154 if (scm_is_false (res))
2155 goto found;
2156 cstr = scm_i_string_chars (s);
2157 }
2158 }
2159
2160 scm_remember_upto_here_1 (s);
2161 return SCM_BOOL_F;
2162
2163 found:
2164 scm_remember_upto_here_1 (s);
2165 return scm_from_size_t (cend);
2166
2167 }
2168 #undef FUNC_NAME
2169
2170
2171 SCM_DEFINE (scm_string_count, "string-count", 2, 2, 0,
2172 (SCM s, SCM char_pred, SCM start, SCM end),
2173 "Return the count of the number of characters in the string\n"
2174 "@var{s} which\n"
2175 "\n"
2176 "@itemize @bullet\n"
2177 "@item\n"
2178 "equals @var{char_pred}, if it is character,\n"
2179 "\n"
2180 "@item\n"
2181 "satisifies the predicate @var{char_pred}, if it is a procedure.\n"
2182 "\n"
2183 "@item\n"
2184 "is in the set @var{char_pred}, if it is a character set.\n"
2185 "@end itemize")
2186 #define FUNC_NAME s_scm_string_count
2187 {
2188 const char *cstr;
2189 size_t cstart, cend;
2190 size_t count = 0;
2191
2192 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
2193 3, start, cstart,
2194 4, end, cend);
2195 if (SCM_CHARP (char_pred))
2196 {
2197 char cchr = SCM_CHAR (char_pred);
2198 while (cstart < cend)
2199 {
2200 if (cchr == cstr[cstart])
2201 count++;
2202 cstart++;
2203 }
2204 }
2205 else if (SCM_CHARSETP (char_pred))
2206 {
2207 while (cstart < cend)
2208 {
2209 if (SCM_CHARSET_GET (char_pred, cstr[cstart]))
2210 count++;
2211 cstart++;
2212 }
2213 }
2214 else
2215 {
2216 SCM_VALIDATE_PROC (2, char_pred);
2217 while (cstart < cend)
2218 {
2219 SCM res;
2220 res = scm_call_1 (char_pred, SCM_MAKE_CHAR (cstr[cstart]));
2221 if (scm_is_true (res))
2222 count++;
2223 cstr = scm_i_string_chars (s);
2224 cstart++;
2225 }
2226 }
2227
2228 scm_remember_upto_here_1 (s);
2229 return scm_from_size_t (count);
2230 }
2231 #undef FUNC_NAME
2232
2233
2234 /* FIXME::martin: This should definitely get implemented more
2235 efficiently -- maybe with Knuth-Morris-Pratt, like in the reference
2236 implementation. */
2237 SCM_DEFINE (scm_string_contains, "string-contains", 2, 4, 0,
2238 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
2239 "Does string @var{s1} contain string @var{s2}? Return the index\n"
2240 "in @var{s1} where @var{s2} occurs as a substring, or false.\n"
2241 "The optional start/end indices restrict the operation to the\n"
2242 "indicated substrings.")
2243 #define FUNC_NAME s_scm_string_contains
2244 {
2245 const char *cs1, * cs2;
2246 size_t cstart1, cend1, cstart2, cend2;
2247 size_t len2, i, j;
2248
2249 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cs1,
2250 3, start1, cstart1,
2251 4, end1, cend1);
2252 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cs2,
2253 5, start2, cstart2,
2254 6, end2, cend2);
2255 len2 = cend2 - cstart2;
2256 if (cend1 - cstart1 >= len2)
2257 while (cstart1 <= cend1 - len2)
2258 {
2259 i = cstart1;
2260 j = cstart2;
2261 while (i < cend1 && j < cend2 && cs1[i] == cs2[j])
2262 {
2263 i++;
2264 j++;
2265 }
2266 if (j == cend2)
2267 {
2268 scm_remember_upto_here_2 (s1, s2);
2269 return scm_from_size_t (cstart1);
2270 }
2271 cstart1++;
2272 }
2273
2274 scm_remember_upto_here_2 (s1, s2);
2275 return SCM_BOOL_F;
2276 }
2277 #undef FUNC_NAME
2278
2279
2280 /* FIXME::martin: This should definitely get implemented more
2281 efficiently -- maybe with Knuth-Morris-Pratt, like in the reference
2282 implementation. */
2283 SCM_DEFINE (scm_string_contains_ci, "string-contains-ci", 2, 4, 0,
2284 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
2285 "Does string @var{s1} contain string @var{s2}? Return the index\n"
2286 "in @var{s1} where @var{s2} occurs as a substring, or false.\n"
2287 "The optional start/end indices restrict the operation to the\n"
2288 "indicated substrings. Character comparison is done\n"
2289 "case-insensitively.")
2290 #define FUNC_NAME s_scm_string_contains_ci
2291 {
2292 const char *cs1, * cs2;
2293 size_t cstart1, cend1, cstart2, cend2;
2294 size_t len2, i, j;
2295
2296 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s1, cs1,
2297 3, start1, cstart1,
2298 4, end1, cend1);
2299 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s2, cs2,
2300 5, start2, cstart2,
2301 6, end2, cend2);
2302 len2 = cend2 - cstart2;
2303 if (cend1 - cstart1 >= len2)
2304 while (cstart1 <= cend1 - len2)
2305 {
2306 i = cstart1;
2307 j = cstart2;
2308 while (i < cend1 && j < cend2 &&
2309 scm_c_downcase (cs1[i]) == scm_c_downcase (cs2[j]))
2310 {
2311 i++;
2312 j++;
2313 }
2314 if (j == cend2)
2315 {
2316 scm_remember_upto_here_2 (s1, s2);
2317 return scm_from_size_t (cstart1);
2318 }
2319 cstart1++;
2320 }
2321
2322 scm_remember_upto_here_2 (s1, s2);
2323 return SCM_BOOL_F;
2324 }
2325 #undef FUNC_NAME
2326
2327
2328 /* Helper function for the string uppercase conversion functions.
2329 * No argument checking is performed. */
2330 static SCM
2331 string_upcase_x (SCM v, size_t start, size_t end)
2332 {
2333 size_t k;
2334 char *dst;
2335
2336 dst = scm_i_string_writable_chars (v);
2337 for (k = start; k < end; ++k)
2338 dst[k] = scm_c_upcase (dst[k]);
2339 scm_i_string_stop_writing ();
2340 scm_remember_upto_here_1 (v);
2341
2342 return v;
2343 }
2344
2345 SCM_DEFINE (scm_substring_upcase_x, "string-upcase!", 1, 2, 0,
2346 (SCM str, SCM start, SCM end),
2347 "Destructively upcase every character in @code{str}.\n"
2348 "\n"
2349 "@lisp\n"
2350 "(string-upcase! y)\n"
2351 "@result{} \"ARRDEFG\"\n"
2352 "y\n"
2353 "@result{} \"ARRDEFG\"\n"
2354 "@end lisp")
2355 #define FUNC_NAME s_scm_substring_upcase_x
2356 {
2357 const char *cstr;
2358 size_t cstart, cend;
2359
2360 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
2361 2, start, cstart,
2362 3, end, cend);
2363 return string_upcase_x (str, cstart, cend);
2364 }
2365 #undef FUNC_NAME
2366
2367 SCM
2368 scm_string_upcase_x (SCM str)
2369 {
2370 return scm_substring_upcase_x (str, SCM_UNDEFINED, SCM_UNDEFINED);
2371 }
2372
2373 SCM_DEFINE (scm_substring_upcase, "string-upcase", 1, 2, 0,
2374 (SCM str, SCM start, SCM end),
2375 "Upcase every character in @code{str}.")
2376 #define FUNC_NAME s_scm_substring_upcase
2377 {
2378 const char *cstr;
2379 size_t cstart, cend;
2380
2381 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
2382 2, start, cstart,
2383 3, end, cend);
2384 return string_upcase_x (scm_string_copy (str), cstart, cend);
2385 }
2386 #undef FUNC_NAME
2387
2388 SCM
2389 scm_string_upcase (SCM str)
2390 {
2391 return scm_substring_upcase (str, SCM_UNDEFINED, SCM_UNDEFINED);
2392 }
2393
2394 /* Helper function for the string lowercase conversion functions.
2395 * No argument checking is performed. */
2396 static SCM
2397 string_downcase_x (SCM v, size_t start, size_t end)
2398 {
2399 size_t k;
2400 char *dst;
2401
2402 dst = scm_i_string_writable_chars (v);
2403 for (k = start; k < end; ++k)
2404 dst[k] = scm_c_downcase (dst[k]);
2405 scm_i_string_stop_writing ();
2406 scm_remember_upto_here_1 (v);
2407
2408 return v;
2409 }
2410
2411 SCM_DEFINE (scm_substring_downcase_x, "string-downcase!", 1, 2, 0,
2412 (SCM str, SCM start, SCM end),
2413 "Destructively downcase every character in @var{str}.\n"
2414 "\n"
2415 "@lisp\n"
2416 "y\n"
2417 "@result{} \"ARRDEFG\"\n"
2418 "(string-downcase! y)\n"
2419 "@result{} \"arrdefg\"\n"
2420 "y\n"
2421 "@result{} \"arrdefg\"\n"
2422 "@end lisp")
2423 #define FUNC_NAME s_scm_substring_downcase_x
2424 {
2425 const char *cstr;
2426 size_t cstart, cend;
2427
2428 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
2429 2, start, cstart,
2430 3, end, cend);
2431 return string_downcase_x (str, cstart, cend);
2432 }
2433 #undef FUNC_NAME
2434
2435 SCM
2436 scm_string_downcase_x (SCM str)
2437 {
2438 return scm_substring_downcase_x (str, SCM_UNDEFINED, SCM_UNDEFINED);
2439 }
2440
2441 SCM_DEFINE (scm_substring_downcase, "string-downcase", 1, 2, 0,
2442 (SCM str, SCM start, SCM end),
2443 "Downcase every character in @var{str}.")
2444 #define FUNC_NAME s_scm_substring_downcase
2445 {
2446 const char *cstr;
2447 size_t cstart, cend;
2448
2449 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
2450 2, start, cstart,
2451 3, end, cend);
2452 return string_downcase_x (scm_string_copy (str), cstart, cend);
2453 }
2454 #undef FUNC_NAME
2455
2456 SCM
2457 scm_string_downcase (SCM str)
2458 {
2459 return scm_substring_downcase (str, SCM_UNDEFINED, SCM_UNDEFINED);
2460 }
2461
2462 /* Helper function for the string capitalization functions.
2463 * No argument checking is performed. */
2464 static SCM
2465 string_titlecase_x (SCM str, size_t start, size_t end)
2466 {
2467 unsigned char *sz;
2468 size_t i;
2469 int in_word = 0;
2470
2471 sz = scm_i_string_writable_chars (str);
2472 for(i = start; i < end; i++)
2473 {
2474 if (scm_is_true (scm_char_alphabetic_p (SCM_MAKE_CHAR (sz[i]))))
2475 {
2476 if (!in_word)
2477 {
2478 sz[i] = scm_c_upcase(sz[i]);
2479 in_word = 1;
2480 }
2481 else
2482 {
2483 sz[i] = scm_c_downcase(sz[i]);
2484 }
2485 }
2486 else
2487 in_word = 0;
2488 }
2489 scm_i_string_stop_writing ();
2490 scm_remember_upto_here_1 (str);
2491
2492 return str;
2493 }
2494
2495
2496 SCM_DEFINE (scm_string_titlecase_x, "string-titlecase!", 1, 2, 0,
2497 (SCM str, SCM start, SCM end),
2498 "Destructively titlecase every first character in a word in\n"
2499 "@var{str}.")
2500 #define FUNC_NAME s_scm_string_titlecase_x
2501 {
2502 const char *cstr;
2503 size_t cstart, cend;
2504
2505 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
2506 2, start, cstart,
2507 3, end, cend);
2508 return string_titlecase_x (str, cstart, cend);
2509 }
2510 #undef FUNC_NAME
2511
2512
2513 SCM_DEFINE (scm_string_titlecase, "string-titlecase", 1, 2, 0,
2514 (SCM str, SCM start, SCM end),
2515 "Titlecase every first character in a word in @var{str}.")
2516 #define FUNC_NAME s_scm_string_titlecase
2517 {
2518 const char *cstr;
2519 size_t cstart, cend;
2520
2521 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
2522 2, start, cstart,
2523 3, end, cend);
2524 return string_titlecase_x (scm_string_copy (str), cstart, cend);
2525 }
2526 #undef FUNC_NAME
2527
2528 SCM_DEFINE (scm_string_capitalize_x, "string-capitalize!", 1, 0, 0,
2529 (SCM str),
2530 "Upcase the first character of every word in @var{str}\n"
2531 "destructively and return @var{str}.\n"
2532 "\n"
2533 "@lisp\n"
2534 "y @result{} \"hello world\"\n"
2535 "(string-capitalize! y) @result{} \"Hello World\"\n"
2536 "y @result{} \"Hello World\"\n"
2537 "@end lisp")
2538 #define FUNC_NAME s_scm_string_capitalize_x
2539 {
2540 return scm_string_titlecase_x (str, SCM_UNDEFINED, SCM_UNDEFINED);
2541 }
2542 #undef FUNC_NAME
2543
2544
2545 SCM_DEFINE (scm_string_capitalize, "string-capitalize", 1, 0, 0,
2546 (SCM str),
2547 "Return a freshly allocated string with the characters in\n"
2548 "@var{str}, where the first character of every word is\n"
2549 "capitalized.")
2550 #define FUNC_NAME s_scm_string_capitalize
2551 {
2552 return scm_string_capitalize_x (scm_string_copy (str));
2553 }
2554 #undef FUNC_NAME
2555
2556
2557 /* Reverse the portion of @var{str} between str[cstart] (including)
2558 and str[cend] excluding. */
2559 static void
2560 string_reverse_x (char * str, size_t cstart, size_t cend)
2561 {
2562 char tmp;
2563
2564 if (cend > 0)
2565 {
2566 cend--;
2567 while (cstart < cend)
2568 {
2569 tmp = str[cstart];
2570 str[cstart] = str[cend];
2571 str[cend] = tmp;
2572 cstart++;
2573 cend--;
2574 }
2575 }
2576 }
2577
2578
2579 SCM_DEFINE (scm_string_reverse, "string-reverse", 1, 2, 0,
2580 (SCM str, SCM start, SCM end),
2581 "Reverse the string @var{str}. The optional arguments\n"
2582 "@var{start} and @var{end} delimit the region of @var{str} to\n"
2583 "operate on.")
2584 #define FUNC_NAME s_scm_string_reverse
2585 {
2586 const char *cstr;
2587 char *ctarget;
2588 size_t cstart, cend;
2589 SCM result;
2590
2591 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, str, cstr,
2592 2, start, cstart,
2593 3, end, cend);
2594 result = scm_string_copy (str);
2595 ctarget = scm_i_string_writable_chars (result);
2596 string_reverse_x (ctarget, cstart, cend);
2597 scm_i_string_stop_writing ();
2598 scm_remember_upto_here_1 (str);
2599 return result;
2600 }
2601 #undef FUNC_NAME
2602
2603
2604 SCM_DEFINE (scm_string_reverse_x, "string-reverse!", 1, 2, 0,
2605 (SCM str, SCM start, SCM end),
2606 "Reverse the string @var{str} in-place. The optional arguments\n"
2607 "@var{start} and @var{end} delimit the region of @var{str} to\n"
2608 "operate on. The return value is unspecified.")
2609 #define FUNC_NAME s_scm_string_reverse_x
2610 {
2611 char *cstr;
2612 size_t cstart, cend;
2613
2614 MY_VALIDATE_SUBSTRING_SPEC (1, str,
2615 2, start, cstart,
2616 3, end, cend);
2617
2618 cstr = scm_i_string_writable_chars (str);
2619 string_reverse_x (cstr, cstart, cend);
2620 scm_i_string_stop_writing ();
2621 scm_remember_upto_here_1 (str);
2622 return SCM_UNSPECIFIED;
2623 }
2624 #undef FUNC_NAME
2625
2626
2627 SCM_DEFINE (scm_string_append_shared, "string-append/shared", 0, 0, 1,
2628 (SCM ls),
2629 "Like @code{string-append}, but the result may share memory\n"
2630 "with the argument strings.")
2631 #define FUNC_NAME s_scm_string_append_shared
2632 {
2633 long i;
2634
2635 SCM_VALIDATE_REST_ARGUMENT (ls);
2636
2637 /* Optimize the one-argument case. */
2638 i = scm_ilength (ls);
2639 if (i == 1)
2640 return SCM_CAR (ls);
2641 else
2642 return scm_string_append (ls);
2643 }
2644 #undef FUNC_NAME
2645
2646
2647 SCM_DEFINE (scm_string_concatenate, "string-concatenate", 1, 0, 0,
2648 (SCM ls),
2649 "Append the elements of @var{ls} (which must be strings)\n"
2650 "together into a single string. Guaranteed to return a freshly\n"
2651 "allocated string.")
2652 #define FUNC_NAME s_scm_string_concatenate
2653 {
2654 return scm_string_append (ls);
2655 }
2656 #undef FUNC_NAME
2657
2658
2659 SCM_DEFINE (scm_string_concatenate_reverse, "string-concatenate-reverse", 1, 2, 0,
2660 (SCM ls, SCM final_string, SCM end),
2661 "Without optional arguments, this procedure is equivalent to\n"
2662 "\n"
2663 "@smalllisp\n"
2664 "(string-concatenate (reverse ls))\n"
2665 "@end smalllisp\n"
2666 "\n"
2667 "If the optional argument @var{final_string} is specified, it is\n"
2668 "consed onto the beginning to @var{ls} before performing the\n"
2669 "list-reverse and string-concatenate operations. If @var{end}\n"
2670 "is given, only the characters of @var{final_string} up to index\n"
2671 "@var{end} are used.\n"
2672 "\n"
2673 "Guaranteed to return a freshly allocated string.")
2674 #define FUNC_NAME s_scm_string_concatenate_reverse
2675 {
2676 if (!SCM_UNBNDP (end))
2677 final_string = scm_substring (final_string, SCM_INUM0, end);
2678
2679 if (!SCM_UNBNDP (final_string))
2680 ls = scm_cons (final_string, ls);
2681
2682 return scm_string_concatenate (scm_reverse (ls));
2683 }
2684 #undef FUNC_NAME
2685
2686
2687 SCM_DEFINE (scm_string_concatenate_shared, "string-concatenate/shared", 1, 0, 0,
2688 (SCM ls),
2689 "Like @code{string-concatenate}, but the result may share memory\n"
2690 "with the strings in the list @var{ls}.")
2691 #define FUNC_NAME s_scm_string_concatenate_shared
2692 {
2693 return scm_string_append_shared (ls);
2694 }
2695 #undef FUNC_NAME
2696
2697
2698 SCM_DEFINE (scm_string_concatenate_reverse_shared, "string-concatenate-reverse/shared", 1, 2, 0,
2699 (SCM ls, SCM final_string, SCM end),
2700 "Like @code{string-concatenate-reverse}, but the result may\n"
2701 "share memory with the the strings in the @var{ls} arguments.")
2702 #define FUNC_NAME s_scm_string_concatenate_reverse_shared
2703 {
2704 /* Just call the non-sharing version. */
2705 return scm_string_concatenate_reverse (ls, final_string, end);
2706 }
2707 #undef FUNC_NAME
2708
2709
2710 SCM_DEFINE (scm_string_map, "string-map", 2, 2, 0,
2711 (SCM proc, SCM s, SCM start, SCM end),
2712 "@var{proc} is a char->char procedure, it is mapped over\n"
2713 "@var{s}. The order in which the procedure is applied to the\n"
2714 "string elements is not specified.")
2715 #define FUNC_NAME s_scm_string_map
2716 {
2717 char *p;
2718 size_t cstart, cend;
2719 SCM result;
2720
2721 SCM_VALIDATE_PROC (1, proc);
2722 MY_VALIDATE_SUBSTRING_SPEC (2, s,
2723 3, start, cstart,
2724 4, end, cend);
2725 result = scm_i_make_string (cend - cstart, &p);
2726 while (cstart < cend)
2727 {
2728 SCM ch = scm_call_1 (proc, scm_c_string_ref (s, cstart));
2729 if (!SCM_CHARP (ch))
2730 SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (proc));
2731 cstart++;
2732 *p++ = SCM_CHAR (ch);
2733 }
2734 return result;
2735 }
2736 #undef FUNC_NAME
2737
2738
2739 SCM_DEFINE (scm_string_map_x, "string-map!", 2, 2, 0,
2740 (SCM proc, SCM s, SCM start, SCM end),
2741 "@var{proc} is a char->char procedure, it is mapped over\n"
2742 "@var{s}. The order in which the procedure is applied to the\n"
2743 "string elements is not specified. The string @var{s} is\n"
2744 "modified in-place, the return value is not specified.")
2745 #define FUNC_NAME s_scm_string_map_x
2746 {
2747 size_t cstart, cend;
2748
2749 SCM_VALIDATE_PROC (1, proc);
2750 MY_VALIDATE_SUBSTRING_SPEC (2, s,
2751 3, start, cstart,
2752 4, end, cend);
2753 while (cstart < cend)
2754 {
2755 SCM ch = scm_call_1 (proc, scm_c_string_ref (s, cstart));
2756 if (!SCM_CHARP (ch))
2757 SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (proc));
2758 scm_c_string_set_x (s, cstart, ch);
2759 cstart++;
2760 }
2761 return SCM_UNSPECIFIED;
2762 }
2763 #undef FUNC_NAME
2764
2765
2766 SCM_DEFINE (scm_string_fold, "string-fold", 3, 2, 0,
2767 (SCM kons, SCM knil, SCM s, SCM start, SCM end),
2768 "Fold @var{kons} over the characters of @var{s}, with @var{knil}\n"
2769 "as the terminating element, from left to right. @var{kons}\n"
2770 "must expect two arguments: The actual character and the last\n"
2771 "result of @var{kons}' application.")
2772 #define FUNC_NAME s_scm_string_fold
2773 {
2774 const char *cstr;
2775 size_t cstart, cend;
2776 SCM result;
2777
2778 SCM_VALIDATE_PROC (1, kons);
2779 MY_VALIDATE_SUBSTRING_SPEC_COPY (3, s, cstr,
2780 4, start, cstart,
2781 5, end, cend);
2782 result = knil;
2783 while (cstart < cend)
2784 {
2785 unsigned int c = (unsigned char) cstr[cstart];
2786 result = scm_call_2 (kons, SCM_MAKE_CHAR (c), result);
2787 cstr = scm_i_string_chars (s);
2788 cstart++;
2789 }
2790
2791 scm_remember_upto_here_1 (s);
2792 return result;
2793 }
2794 #undef FUNC_NAME
2795
2796
2797 SCM_DEFINE (scm_string_fold_right, "string-fold-right", 3, 2, 0,
2798 (SCM kons, SCM knil, SCM s, SCM start, SCM end),
2799 "Fold @var{kons} over the characters of @var{s}, with @var{knil}\n"
2800 "as the terminating element, from right to left. @var{kons}\n"
2801 "must expect two arguments: The actual character and the last\n"
2802 "result of @var{kons}' application.")
2803 #define FUNC_NAME s_scm_string_fold_right
2804 {
2805 const char *cstr;
2806 size_t cstart, cend;
2807 SCM result;
2808
2809 SCM_VALIDATE_PROC (1, kons);
2810 MY_VALIDATE_SUBSTRING_SPEC_COPY (3, s, cstr,
2811 4, start, cstart,
2812 5, end, cend);
2813 result = knil;
2814 while (cstart < cend)
2815 {
2816 unsigned int c = (unsigned char) cstr[cend - 1];
2817 result = scm_call_2 (kons, SCM_MAKE_CHAR (c), result);
2818 cstr = scm_i_string_chars (s);
2819 cend--;
2820 }
2821
2822 scm_remember_upto_here_1 (s);
2823 return result;
2824 }
2825 #undef FUNC_NAME
2826
2827
2828 SCM_DEFINE (scm_string_unfold, "string-unfold", 4, 2, 0,
2829 (SCM p, SCM f, SCM g, SCM seed, SCM base, SCM make_final),
2830 "@itemize @bullet\n"
2831 "@item @var{g} is used to generate a series of @emph{seed}\n"
2832 "values from the initial @var{seed}: @var{seed}, (@var{g}\n"
2833 "@var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),\n"
2834 "@dots{}\n"
2835 "@item @var{p} tells us when to stop -- when it returns true\n"
2836 "when applied to one of these seed values.\n"
2837 "@item @var{f} maps each seed value to the corresponding\n"
2838 "character in the result string. These chars are assembled\n"
2839 "into the string in a left-to-right order.\n"
2840 "@item @var{base} is the optional initial/leftmost portion\n"
2841 "of the constructed string; it default to the empty\n"
2842 "string.\n"
2843 "@item @var{make_final} is applied to the terminal seed\n"
2844 "value (on which @var{p} returns true) to produce\n"
2845 "the final/rightmost portion of the constructed string.\n"
2846 "It defaults to @code{(lambda (x) "")}.\n"
2847 "@end itemize")
2848 #define FUNC_NAME s_scm_string_unfold
2849 {
2850 SCM res, ans;
2851
2852 SCM_VALIDATE_PROC (1, p);
2853 SCM_VALIDATE_PROC (2, f);
2854 SCM_VALIDATE_PROC (3, g);
2855 if (!SCM_UNBNDP (base))
2856 {
2857 SCM_VALIDATE_STRING (5, base);
2858 ans = base;
2859 }
2860 else
2861 ans = scm_i_make_string (0, NULL);
2862 if (!SCM_UNBNDP (make_final))
2863 SCM_VALIDATE_PROC (6, make_final);
2864
2865 res = scm_call_1 (p, seed);
2866 while (scm_is_false (res))
2867 {
2868 SCM str;
2869 char *ptr;
2870 SCM ch = scm_call_1 (f, seed);
2871 if (!SCM_CHARP (ch))
2872 SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (f));
2873 str = scm_i_make_string (1, &ptr);
2874 *ptr = SCM_CHAR (ch);
2875
2876 ans = scm_string_append (scm_list_2 (ans, str));
2877 seed = scm_call_1 (g, seed);
2878 res = scm_call_1 (p, seed);
2879 }
2880 if (!SCM_UNBNDP (make_final))
2881 {
2882 res = scm_call_1 (make_final, seed);
2883 return scm_string_append (scm_list_2 (ans, res));
2884 }
2885 else
2886 return ans;
2887 }
2888 #undef FUNC_NAME
2889
2890
2891 SCM_DEFINE (scm_string_unfold_right, "string-unfold-right", 4, 2, 0,
2892 (SCM p, SCM f, SCM g, SCM seed, SCM base, SCM make_final),
2893 "@itemize @bullet\n"
2894 "@item @var{g} is used to generate a series of @emph{seed}\n"
2895 "values from the initial @var{seed}: @var{seed}, (@var{g}\n"
2896 "@var{seed}), (@var{g}^2 @var{seed}), (@var{g}^3 @var{seed}),\n"
2897 "@dots{}\n"
2898 "@item @var{p} tells us when to stop -- when it returns true\n"
2899 "when applied to one of these seed values.\n"
2900 "@item @var{f} maps each seed value to the corresponding\n"
2901 "character in the result string. These chars are assembled\n"
2902 "into the string in a right-to-left order.\n"
2903 "@item @var{base} is the optional initial/rightmost portion\n"
2904 "of the constructed string; it default to the empty\n"
2905 "string.\n"
2906 "@item @var{make_final} is applied to the terminal seed\n"
2907 "value (on which @var{p} returns true) to produce\n"
2908 "the final/leftmost portion of the constructed string.\n"
2909 "It defaults to @code{(lambda (x) "")}.\n"
2910 "@end itemize")
2911 #define FUNC_NAME s_scm_string_unfold_right
2912 {
2913 SCM res, ans;
2914
2915 SCM_VALIDATE_PROC (1, p);
2916 SCM_VALIDATE_PROC (2, f);
2917 SCM_VALIDATE_PROC (3, g);
2918 if (!SCM_UNBNDP (base))
2919 {
2920 SCM_VALIDATE_STRING (5, base);
2921 ans = base;
2922 }
2923 else
2924 ans = scm_i_make_string (0, NULL);
2925 if (!SCM_UNBNDP (make_final))
2926 SCM_VALIDATE_PROC (6, make_final);
2927
2928 res = scm_call_1 (p, seed);
2929 while (scm_is_false (res))
2930 {
2931 SCM str;
2932 char *ptr;
2933 SCM ch = scm_call_1 (f, seed);
2934 if (!SCM_CHARP (ch))
2935 SCM_MISC_ERROR ("procedure ~S returned non-char", scm_list_1 (f));
2936 str = scm_i_make_string (1, &ptr);
2937 *ptr = SCM_CHAR (ch);
2938
2939 ans = scm_string_append (scm_list_2 (str, ans));
2940 seed = scm_call_1 (g, seed);
2941 res = scm_call_1 (p, seed);
2942 }
2943 if (!SCM_UNBNDP (make_final))
2944 {
2945 res = scm_call_1 (make_final, seed);
2946 return scm_string_append (scm_list_2 (res, ans));
2947 }
2948 else
2949 return ans;
2950 }
2951 #undef FUNC_NAME
2952
2953
2954 SCM_DEFINE (scm_string_for_each, "string-for-each", 2, 2, 0,
2955 (SCM proc, SCM s, SCM start, SCM end),
2956 "@var{proc} is mapped over @var{s} in left-to-right order. The\n"
2957 "return value is not specified.")
2958 #define FUNC_NAME s_scm_string_for_each
2959 {
2960 const char *cstr;
2961 size_t cstart, cend;
2962
2963 SCM_VALIDATE_PROC (1, proc);
2964 MY_VALIDATE_SUBSTRING_SPEC_COPY (2, s, cstr,
2965 3, start, cstart,
2966 4, end, cend);
2967 while (cstart < cend)
2968 {
2969 unsigned int c = (unsigned char) cstr[cstart];
2970 scm_call_1 (proc, SCM_MAKE_CHAR (c));
2971 cstr = scm_i_string_chars (s);
2972 cstart++;
2973 }
2974
2975 scm_remember_upto_here_1 (s);
2976 return SCM_UNSPECIFIED;
2977 }
2978 #undef FUNC_NAME
2979
2980 SCM_DEFINE (scm_string_for_each_index, "string-for-each-index", 2, 2, 0,
2981 (SCM proc, SCM s, SCM start, SCM end),
2982 "@var{proc} is mapped over @var{s} in left-to-right order. The\n"
2983 "return value is not specified.")
2984 #define FUNC_NAME s_scm_string_for_each_index
2985 {
2986 size_t cstart, cend;
2987
2988 SCM_VALIDATE_PROC (1, proc);
2989 MY_VALIDATE_SUBSTRING_SPEC (2, s,
2990 3, start, cstart,
2991 4, end, cend);
2992
2993 while (cstart < cend)
2994 {
2995 scm_call_1 (proc, scm_from_size_t (cstart));
2996 cstart++;
2997 }
2998
2999 scm_remember_upto_here_1 (s);
3000 return SCM_UNSPECIFIED;
3001 }
3002 #undef FUNC_NAME
3003
3004 SCM_DEFINE (scm_xsubstring, "xsubstring", 2, 3, 0,
3005 (SCM s, SCM from, SCM to, SCM start, SCM end),
3006 "This is the @emph{extended substring} procedure that implements\n"
3007 "replicated copying of a substring of some string.\n"
3008 "\n"
3009 "@var{s} is a string, @var{start} and @var{end} are optional\n"
3010 "arguments that demarcate a substring of @var{s}, defaulting to\n"
3011 "0 and the length of @var{s}. Replicate this substring up and\n"
3012 "down index space, in both the positive and negative directions.\n"
3013 "@code{xsubstring} returns the substring of this string\n"
3014 "beginning at index @var{from}, and ending at @var{to}, which\n"
3015 "defaults to @var{from} + (@var{end} - @var{start}).")
3016 #define FUNC_NAME s_scm_xsubstring
3017 {
3018 const char *cs;
3019 char *p;
3020 size_t cstart, cend;
3021 int cfrom, cto;
3022 SCM result;
3023
3024 MY_VALIDATE_SUBSTRING_SPEC (1, s,
3025 4, start, cstart,
3026 5, end, cend);
3027
3028 cfrom = scm_to_int (from);
3029 if (SCM_UNBNDP (to))
3030 cto = cfrom + (cend - cstart);
3031 else
3032 cto = scm_to_int (to);
3033 if (cstart == cend && cfrom != cto)
3034 SCM_MISC_ERROR ("start and end indices must not be equal", SCM_EOL);
3035
3036 result = scm_i_make_string (cto - cfrom, &p);
3037
3038 cs = scm_i_string_chars (s);
3039 while (cfrom < cto)
3040 {
3041 size_t t = ((cfrom < 0) ? -cfrom : cfrom) % (cend - cstart);
3042 if (cfrom < 0)
3043 *p = cs[(cend - cstart) - t];
3044 else
3045 *p = cs[t];
3046 cfrom++;
3047 p++;
3048 }
3049
3050 scm_remember_upto_here_1 (s);
3051 return result;
3052 }
3053 #undef FUNC_NAME
3054
3055
3056 SCM_DEFINE (scm_string_xcopy_x, "string-xcopy!", 4, 3, 0,
3057 (SCM target, SCM tstart, SCM s, SCM sfrom, SCM sto, SCM start, SCM end),
3058 "Exactly the same as @code{xsubstring}, but the extracted text\n"
3059 "is written into the string @var{target} starting at index\n"
3060 "@var{tstart}. The operation is not defined if @code{(eq?\n"
3061 "@var{target} @var{s})} or these arguments share storage -- you\n"
3062 "cannot copy a string on top of itself.")
3063 #define FUNC_NAME s_scm_string_xcopy_x
3064 {
3065 char *p;
3066 const char *cs;
3067 size_t ctstart, cstart, cend;
3068 int csfrom, csto;
3069 SCM dummy = SCM_UNDEFINED;
3070 size_t cdummy;
3071
3072 MY_VALIDATE_SUBSTRING_SPEC (1, target,
3073 2, tstart, ctstart,
3074 2, dummy, cdummy);
3075 MY_VALIDATE_SUBSTRING_SPEC (3, s,
3076 6, start, cstart,
3077 7, end, cend);
3078 csfrom = scm_to_int (sfrom);
3079 if (SCM_UNBNDP (sto))
3080 csto = csfrom + (cend - cstart);
3081 else
3082 csto = scm_to_int (sto);
3083 if (cstart == cend && csfrom != csto)
3084 SCM_MISC_ERROR ("start and end indices must not be equal", SCM_EOL);
3085 SCM_ASSERT_RANGE (1, tstart,
3086 ctstart + (csto - csfrom) <= scm_i_string_length (target));
3087
3088 p = scm_i_string_writable_chars (target) + ctstart;
3089 cs = scm_i_string_chars (s);
3090 while (csfrom < csto)
3091 {
3092 size_t t = ((csfrom < 0) ? -csfrom : csfrom) % (cend - cstart);
3093 if (csfrom < 0)
3094 *p = cs[(cend - cstart) - t];
3095 else
3096 *p = cs[t];
3097 csfrom++;
3098 p++;
3099 }
3100 scm_i_string_stop_writing ();
3101
3102 scm_remember_upto_here_2 (target, s);
3103 return SCM_UNSPECIFIED;
3104 }
3105 #undef FUNC_NAME
3106
3107
3108 SCM_DEFINE (scm_string_replace, "string-replace", 2, 4, 0,
3109 (SCM s1, SCM s2, SCM start1, SCM end1, SCM start2, SCM end2),
3110 "Return the string @var{s1}, but with the characters\n"
3111 "@var{start1} @dots{} @var{end1} replaced by the characters\n"
3112 "@var{start2} @dots{} @var{end2} from @var{s2}.")
3113 #define FUNC_NAME s_scm_string_replace
3114 {
3115 const char *cstr1, *cstr2;
3116 char *p;
3117 size_t cstart1, cend1, cstart2, cend2;
3118 SCM result;
3119
3120 MY_VALIDATE_SUBSTRING_SPEC (1, s1,
3121 3, start1, cstart1,
3122 4, end1, cend1);
3123 MY_VALIDATE_SUBSTRING_SPEC (2, s2,
3124 5, start2, cstart2,
3125 6, end2, cend2);
3126 result = scm_i_make_string (cstart1 + (cend2 - cstart2) +
3127 scm_i_string_length (s1) - cend1, &p);
3128 cstr1 = scm_i_string_chars (s1);
3129 cstr2 = scm_i_string_chars (s2);
3130 memmove (p, cstr1, cstart1 * sizeof (char));
3131 memmove (p + cstart1, cstr2 + cstart2, (cend2 - cstart2) * sizeof (char));
3132 memmove (p + cstart1 + (cend2 - cstart2),
3133 cstr1 + cend1,
3134 (scm_i_string_length (s1) - cend1) * sizeof (char));
3135 scm_remember_upto_here_2 (s1, s2);
3136 return result;
3137 }
3138 #undef FUNC_NAME
3139
3140
3141 SCM_DEFINE (scm_string_tokenize, "string-tokenize", 1, 3, 0,
3142 (SCM s, SCM token_set, SCM start, SCM end),
3143 "Split the string @var{s} into a list of substrings, where each\n"
3144 "substring is a maximal non-empty contiguous sequence of\n"
3145 "characters from the character set @var{token_set}, which\n"
3146 "defaults to @code{char-set:graphic}.\n"
3147 "If @var{start} or @var{end} indices are provided, they restrict\n"
3148 "@code{string-tokenize} to operating on the indicated substring\n"
3149 "of @var{s}.")
3150 #define FUNC_NAME s_scm_string_tokenize
3151 {
3152 const char *cstr;
3153 size_t cstart, cend;
3154 SCM result = SCM_EOL;
3155
3156 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
3157 3, start, cstart,
3158 4, end, cend);
3159
3160 if (SCM_UNBNDP (token_set))
3161 token_set = scm_char_set_graphic;
3162
3163 if (SCM_CHARSETP (token_set))
3164 {
3165 size_t idx;
3166
3167 while (cstart < cend)
3168 {
3169 while (cstart < cend)
3170 {
3171 if (SCM_CHARSET_GET (token_set, cstr[cend - 1]))
3172 break;
3173 cend--;
3174 }
3175 if (cstart >= cend)
3176 break;
3177 idx = cend;
3178 while (cstart < cend)
3179 {
3180 if (!SCM_CHARSET_GET (token_set, cstr[cend - 1]))
3181 break;
3182 cend--;
3183 }
3184 result = scm_cons (scm_c_substring (s, cend, idx), result);
3185 cstr = scm_i_string_chars (s);
3186 }
3187 }
3188 else
3189 SCM_WRONG_TYPE_ARG (2, token_set);
3190
3191 scm_remember_upto_here_1 (s);
3192 return result;
3193 }
3194 #undef FUNC_NAME
3195
3196 SCM_DEFINE (scm_string_split, "string-split", 2, 0, 0,
3197 (SCM str, SCM chr),
3198 "Split the string @var{str} into the a list of the substrings delimited\n"
3199 "by appearances of the character @var{chr}. Note that an empty substring\n"
3200 "between separator characters will result in an empty string in the\n"
3201 "result list.\n"
3202 "\n"
3203 "@lisp\n"
3204 "(string-split \"root:x:0:0:root:/root:/bin/bash\" #\\:)\n"
3205 "@result{}\n"
3206 "(\"root\" \"x\" \"0\" \"0\" \"root\" \"/root\" \"/bin/bash\")\n"
3207 "\n"
3208 "(string-split \"::\" #\\:)\n"
3209 "@result{}\n"
3210 "(\"\" \"\" \"\")\n"
3211 "\n"
3212 "(string-split \"\" #\\:)\n"
3213 "@result{}\n"
3214 "(\"\")\n"
3215 "@end lisp")
3216 #define FUNC_NAME s_scm_string_split
3217 {
3218 long idx, last_idx;
3219 const char * p;
3220 int ch;
3221 SCM res = SCM_EOL;
3222
3223 SCM_VALIDATE_STRING (1, str);
3224 SCM_VALIDATE_CHAR (2, chr);
3225
3226 idx = scm_i_string_length (str);
3227 p = scm_i_string_chars (str);
3228 ch = SCM_CHAR (chr);
3229 while (idx >= 0)
3230 {
3231 last_idx = idx;
3232 while (idx > 0 && p[idx - 1] != ch)
3233 idx--;
3234 if (idx >= 0)
3235 {
3236 res = scm_cons (scm_c_substring (str, idx, last_idx), res);
3237 p = scm_i_string_chars (str);
3238 idx--;
3239 }
3240 }
3241 scm_remember_upto_here_1 (str);
3242 return res;
3243 }
3244 #undef FUNC_NAME
3245
3246
3247 SCM_DEFINE (scm_string_filter, "string-filter", 2, 2, 0,
3248 (SCM s, SCM char_pred, SCM start, SCM end),
3249 "Filter the string @var{s}, retaining only those characters that\n"
3250 "satisfy the @var{char_pred} argument. If the argument is a\n"
3251 "procedure, it is applied to each character as a predicate, if\n"
3252 "it is a character, it is tested for equality and if it is a\n"
3253 "character set, it is tested for membership.")
3254 #define FUNC_NAME s_scm_string_filter
3255 {
3256 const char *cstr;
3257 size_t cstart, cend;
3258 SCM result;
3259 size_t idx;
3260
3261 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
3262 3, start, cstart,
3263 4, end, cend);
3264 if (SCM_CHARP (char_pred))
3265 {
3266 SCM ls = SCM_EOL;
3267 char chr;
3268
3269 chr = SCM_CHAR (char_pred);
3270 idx = cstart;
3271 while (idx < cend)
3272 {
3273 if (cstr[idx] == chr)
3274 ls = scm_cons (SCM_MAKE_CHAR (cstr[idx]), ls);
3275 cstr = scm_i_string_chars (s);
3276 idx++;
3277 }
3278 result = scm_reverse_list_to_string (ls);
3279 }
3280 else if (SCM_CHARSETP (char_pred))
3281 {
3282 SCM ls = SCM_EOL;
3283
3284 idx = cstart;
3285 while (idx < cend)
3286 {
3287 if (SCM_CHARSET_GET (char_pred, cstr[idx]))
3288 ls = scm_cons (SCM_MAKE_CHAR (cstr[idx]), ls);
3289 cstr = scm_i_string_chars (s);
3290 idx++;
3291 }
3292 result = scm_reverse_list_to_string (ls);
3293 }
3294 else
3295 {
3296 SCM ls = SCM_EOL;
3297
3298 SCM_VALIDATE_PROC (2, char_pred);
3299 idx = cstart;
3300 while (idx < cend)
3301 {
3302 SCM res, ch;
3303 ch = SCM_MAKE_CHAR (cstr[idx]);
3304 res = scm_call_1 (char_pred, ch);
3305 if (scm_is_true (res))
3306 ls = scm_cons (ch, ls);
3307 cstr = scm_i_string_chars (s);
3308 idx++;
3309 }
3310 result = scm_reverse_list_to_string (ls);
3311 }
3312
3313 scm_remember_upto_here_1 (s);
3314 return result;
3315 }
3316 #undef FUNC_NAME
3317
3318
3319 SCM_DEFINE (scm_string_delete, "string-delete", 2, 2, 0,
3320 (SCM s, SCM char_pred, SCM start, SCM end),
3321 "Filter the string @var{s}, retaining only those characters that\n"
3322 "do not satisfy the @var{char_pred} argument. If the argument\n"
3323 "is a procedure, it is applied to each character as a predicate,\n"
3324 "if it is a character, it is tested for equality and if it is a\n"
3325 "character set, it is tested for membership.")
3326 #define FUNC_NAME s_scm_string_delete
3327 {
3328 const char *cstr;
3329 size_t cstart, cend;
3330 SCM result;
3331 size_t idx;
3332
3333 MY_VALIDATE_SUBSTRING_SPEC_COPY (1, s, cstr,
3334 3, start, cstart,
3335 4, end, cend);
3336 if (SCM_CHARP (char_pred))
3337 {
3338 SCM ls = SCM_EOL;
3339 char chr;
3340
3341 chr = SCM_CHAR (char_pred);
3342 idx = cstart;
3343 while (idx < cend)
3344 {
3345 if (cstr[idx] != chr)
3346 ls = scm_cons (SCM_MAKE_CHAR (cstr[idx]), ls);
3347 cstr = scm_i_string_chars (s);
3348 idx++;
3349 }
3350 result = scm_reverse_list_to_string (ls);
3351 }
3352 else if (SCM_CHARSETP (char_pred))
3353 {
3354 SCM ls = SCM_EOL;
3355
3356 idx = cstart;
3357 while (idx < cend)
3358 {
3359 if (!SCM_CHARSET_GET (char_pred, cstr[idx]))
3360 ls = scm_cons (SCM_MAKE_CHAR (cstr[idx]), ls);
3361 cstr = scm_i_string_chars (s);
3362 idx++;
3363 }
3364 result = scm_reverse_list_to_string (ls);
3365 }
3366 else
3367 {
3368 SCM ls = SCM_EOL;
3369
3370 SCM_VALIDATE_PROC (2, char_pred);
3371 idx = cstart;
3372 while (idx < cend)
3373 {
3374 SCM res, ch = SCM_MAKE_CHAR (cstr[idx]);
3375 res = scm_call_1 (char_pred, ch);
3376 if (scm_is_false (res))
3377 ls = scm_cons (ch, ls);
3378 cstr = scm_i_string_chars (s);
3379 idx++;
3380 }
3381 result = scm_reverse_list_to_string (ls);
3382 }
3383
3384 scm_remember_upto_here_1 (s);
3385 return result;
3386 }
3387 #undef FUNC_NAME
3388
3389 void
3390 scm_init_srfi_13 (void)
3391 {
3392 #include "libguile/srfi-13.x"
3393 }
3394
3395 /* End of srfi-13.c. */