1 /* Copyright (C) 1995,1996,1998,2000,2001, 2004 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "libguile/_scm.h"
25 #include "libguile/chars.h"
26 #include "libguile/root.h"
27 #include "libguile/strings.h"
28 #include "libguile/deprecation.h"
29 #include "libguile/validate.h"
30 #include "libguile/dynwind.h"
40 * XXX - keeping an accurate refcount during GC seems to be quite
41 * tricky, so we just keep score of whether a stringbuf might be
42 * shared, not wether it definitely is.
44 * The scheme I (mvo) tried to keep an accurate reference count would
45 * recount all strings that point to a stringbuf during the mark-phase
46 * of the GC. This was done since one cannot access the stringbuf of
47 * a string when that string is freed (in order to decrease the
48 * reference count). The memory of the stringbuf might have been
49 * reused already for something completely different.
51 * This recounted worked for a small number of threads beating on
52 * cow-strings, but it failed randomly with more than 10 threads, say.
53 * I couldn't figure out what went wrong, so I used the conservative
54 * approach implemented below.
56 * A stringbuf needs to know its length, but only so that it can be
57 * reported when the stringbuf is freed.
59 * Stringbufs (and strings) are not stored very compactly: a stringbuf
60 * has room for about 2*sizeof(scm_t_bits)-1 bytes additional
61 * information. As a compensation, the code below is made more
62 * complicated by storing small strings inline in the double cell of a
63 * stringbuf. So we have fixstrings and bigstrings...
66 #define STRINGBUF_F_SHARED 0x100
67 #define STRINGBUF_F_INLINE 0x200
69 #define STRINGBUF_TAG scm_tc7_stringbuf
70 #define STRINGBUF_SHARED(buf) (SCM_CELL_WORD_0(buf) & STRINGBUF_F_SHARED)
71 #define STRINGBUF_INLINE(buf) (SCM_CELL_WORD_0(buf) & STRINGBUF_F_INLINE)
73 #define STRINGBUF_OUTLINE_CHARS(buf) ((char *)SCM_CELL_WORD_1(buf))
74 #define STRINGBUF_OUTLINE_LENGTH(buf) (SCM_CELL_WORD_2(buf))
75 #define STRINGBUF_INLINE_CHARS(buf) ((char *)SCM_CELL_OBJECT_LOC(buf,1))
76 #define STRINGBUF_INLINE_LENGTH(buf) (((size_t)SCM_CELL_WORD_0(buf))>>16)
78 #define STRINGBUF_CHARS(buf) (STRINGBUF_INLINE (buf) \
79 ? STRINGBUF_INLINE_CHARS (buf) \
80 : STRINGBUF_OUTLINE_CHARS (buf))
81 #define STRINGBUF_LENGTH(buf) (STRINGBUF_INLINE (buf) \
82 ? STRINGBUF_INLINE_LENGTH (buf) \
83 : STRINGBUF_OUTLINE_LENGTH (buf))
85 #define STRINGBUF_MAX_INLINE_LEN (3*sizeof(scm_t_bits))
87 #define SET_STRINGBUF_SHARED(buf) \
88 (SCM_SET_CELL_WORD_0 ((buf), SCM_CELL_WORD_0 (buf) | STRINGBUF_F_SHARED))
91 static size_t lenhist
[1001];
95 make_stringbuf (size_t len
)
97 /* XXX - for the benefit of SCM_STRING_CHARS, SCM_SYMBOL_CHARS and
98 scm_i_symbol_chars, all stringbufs are null-terminated. Once
99 SCM_STRING_CHARS and SCM_SYMBOL_CHARS are removed and the code
100 has been changed for scm_i_symbol_chars, this null-termination
111 if (len
<= STRINGBUF_MAX_INLINE_LEN
-1)
113 return scm_double_cell (STRINGBUF_TAG
| STRINGBUF_F_INLINE
| (len
<< 16),
118 char *mem
= scm_gc_malloc (len
+1, "string");
120 return scm_double_cell (STRINGBUF_TAG
, (scm_t_bits
) mem
,
121 (scm_t_bits
) len
, (scm_t_bits
) 0);
126 scm_i_stringbuf_mark (SCM buf
)
132 scm_i_stringbuf_free (SCM buf
)
134 if (!STRINGBUF_INLINE (buf
))
135 scm_gc_free (STRINGBUF_OUTLINE_CHARS (buf
),
136 STRINGBUF_OUTLINE_LENGTH (buf
) + 1, "string");
139 SCM_MUTEX (stringbuf_write_mutex
);
141 /* Copy-on-write strings.
144 #define STRING_TAG scm_tc7_string
146 #define STRING_STRINGBUF(str) (SCM_CELL_OBJECT_1(str))
147 #define STRING_START(str) ((size_t)SCM_CELL_WORD_2(str))
148 #define STRING_LENGTH(str) ((size_t)SCM_CELL_WORD_3(str))
150 #define SET_STRING_STRINGBUF(str,buf) (SCM_SET_CELL_OBJECT_1(str,buf))
151 #define SET_STRING_START(str,start) (SCM_SET_CELL_WORD_2(str,start))
153 #define IS_STRING(str) (SCM_NIMP(str) && SCM_TYP7(str) == STRING_TAG)
155 /* Read-only strings.
158 #define RO_STRING_TAG (scm_tc7_string + 0x200)
159 #define IS_RO_STRING(str) (SCM_CELL_TYPE(str)==RO_STRING_TAG)
161 /* Mutation-sharing substrings
164 #define SH_STRING_TAG (scm_tc7_string + 0x100)
166 #define SH_STRING_STRING(sh) (SCM_CELL_OBJECT_1(sh))
167 /* START and LENGTH as for STRINGs. */
169 #define IS_SH_STRING(str) (SCM_CELL_TYPE(str)==SH_STRING_TAG)
172 scm_i_make_string (size_t len
, char **charsp
)
174 SCM buf
= make_stringbuf (len
);
177 *charsp
= STRINGBUF_CHARS (buf
);
178 res
= scm_double_cell (STRING_TAG
, SCM_UNPACK(buf
),
179 (scm_t_bits
)0, (scm_t_bits
) len
);
184 validate_substring_args (SCM str
, size_t start
, size_t end
)
186 if (!IS_STRING (str
))
187 scm_wrong_type_arg_msg (NULL
, 0, str
, "string");
188 if (start
> STRING_LENGTH (str
))
189 scm_out_of_range (NULL
, scm_from_size_t (start
));
190 if (end
> STRING_LENGTH (str
) || end
< start
)
191 scm_out_of_range (NULL
, scm_from_size_t (end
));
195 get_str_buf_start (SCM
*str
, SCM
*buf
, size_t *start
)
197 *start
= STRING_START (*str
);
198 if (IS_SH_STRING (*str
))
200 *str
= SH_STRING_STRING (*str
);
201 *start
+= STRING_START (*str
);
203 *buf
= STRING_STRINGBUF (*str
);
207 scm_i_substring (SCM str
, size_t start
, size_t end
)
211 get_str_buf_start (&str
, &buf
, &str_start
);
212 scm_i_plugin_mutex_lock (&stringbuf_write_mutex
);
213 SET_STRINGBUF_SHARED (buf
);
214 scm_i_plugin_mutex_unlock (&stringbuf_write_mutex
);
215 return scm_double_cell (STRING_TAG
, SCM_UNPACK(buf
),
216 (scm_t_bits
)str_start
+ start
,
217 (scm_t_bits
) end
- start
);
221 scm_i_substring_read_only (SCM str
, size_t start
, size_t end
)
225 get_str_buf_start (&str
, &buf
, &str_start
);
226 scm_i_plugin_mutex_lock (&stringbuf_write_mutex
);
227 SET_STRINGBUF_SHARED (buf
);
228 scm_i_plugin_mutex_unlock (&stringbuf_write_mutex
);
229 return scm_double_cell (RO_STRING_TAG
, SCM_UNPACK(buf
),
230 (scm_t_bits
)str_start
+ start
,
231 (scm_t_bits
) end
- start
);
235 scm_i_substring_copy (SCM str
, size_t start
, size_t end
)
237 size_t len
= end
- start
;
240 get_str_buf_start (&str
, &buf
, &str_start
);
241 my_buf
= make_stringbuf (len
);
242 memcpy (STRINGBUF_CHARS (my_buf
),
243 STRINGBUF_CHARS (buf
) + str_start
+ start
, len
);
244 scm_remember_upto_here_1 (buf
);
245 return scm_double_cell (STRING_TAG
, SCM_UNPACK(my_buf
),
246 (scm_t_bits
)0, (scm_t_bits
) len
);
250 scm_i_substring_shared (SCM str
, size_t start
, size_t end
)
252 if (start
== 0 && end
== STRING_LENGTH (str
))
256 size_t len
= end
- start
;
257 if (IS_SH_STRING (str
))
259 start
+= STRING_START (str
);
260 str
= SH_STRING_STRING (str
);
262 return scm_double_cell (SH_STRING_TAG
, SCM_UNPACK(str
),
263 (scm_t_bits
)start
, (scm_t_bits
) len
);
268 scm_c_substring (SCM str
, size_t start
, size_t end
)
270 validate_substring_args (str
, start
, end
);
271 return scm_i_substring (str
, start
, end
);
275 scm_c_substring_read_only (SCM str
, size_t start
, size_t end
)
277 validate_substring_args (str
, start
, end
);
278 return scm_i_substring_read_only (str
, start
, end
);
282 scm_c_substring_copy (SCM str
, size_t start
, size_t end
)
284 validate_substring_args (str
, start
, end
);
285 return scm_i_substring_copy (str
, start
, end
);
289 scm_c_substring_shared (SCM str
, size_t start
, size_t end
)
291 validate_substring_args (str
, start
, end
);
292 return scm_i_substring_shared (str
, start
, end
);
296 scm_i_string_mark (SCM str
)
298 if (IS_SH_STRING (str
))
299 return SH_STRING_STRING (str
);
301 return STRING_STRINGBUF (str
);
305 scm_i_string_free (SCM str
)
309 /* Internal accessors
313 scm_i_string_length (SCM str
)
315 return STRING_LENGTH (str
);
319 scm_i_string_chars (SCM str
)
323 get_str_buf_start (&str
, &buf
, &start
);
324 return STRINGBUF_CHARS (buf
) + start
;
328 scm_i_string_writable_chars (SCM orig_str
)
330 SCM buf
, str
= orig_str
;
333 get_str_buf_start (&str
, &buf
, &start
);
334 if (IS_RO_STRING (str
))
335 scm_misc_error (NULL
, "string is read-only: ~s", scm_list_1 (orig_str
));
337 scm_i_plugin_mutex_lock (&stringbuf_write_mutex
);
338 if (STRINGBUF_SHARED (buf
))
340 /* Clone stringbuf. For this, we put all threads to sleep.
343 size_t len
= STRING_LENGTH (str
);
346 scm_i_plugin_mutex_unlock (&stringbuf_write_mutex
);
348 new_buf
= make_stringbuf (len
);
349 memcpy (STRINGBUF_CHARS (new_buf
),
350 STRINGBUF_CHARS (buf
) + STRING_START (str
), len
);
352 scm_i_thread_put_to_sleep ();
353 SET_STRING_STRINGBUF (str
, new_buf
);
354 start
-= STRING_START (str
);
355 SET_STRING_START (str
, 0);
356 scm_i_thread_wake_up ();
360 scm_i_plugin_mutex_lock (&stringbuf_write_mutex
);
363 return STRINGBUF_CHARS (buf
) + start
;
367 scm_i_string_stop_writing (void)
369 scm_i_plugin_mutex_unlock (&stringbuf_write_mutex
);
374 Basic symbol creation and accessing is done here, the rest is in
375 symbols.[hc]. This has been done to keep stringbufs and the
376 internals of strings and string-like objects confined to this file.
379 #define SYMBOL_STRINGBUF SCM_CELL_OBJECT_1
382 scm_i_make_symbol (SCM name
, scm_t_bits flags
,
383 unsigned long hash
, SCM props
)
386 size_t start
= STRING_START (name
);
387 size_t length
= STRING_LENGTH (name
);
389 if (IS_SH_STRING (name
))
391 name
= SH_STRING_STRING (name
);
392 start
+= STRING_START (name
);
394 buf
= SYMBOL_STRINGBUF (name
);
396 if (start
== 0 && length
== STRINGBUF_LENGTH (buf
))
399 scm_i_plugin_mutex_lock (&stringbuf_write_mutex
);
400 SET_STRINGBUF_SHARED (buf
);
401 scm_i_plugin_mutex_unlock (&stringbuf_write_mutex
);
406 SCM new_buf
= make_stringbuf (length
);
407 memcpy (STRINGBUF_CHARS (new_buf
),
408 STRINGBUF_CHARS (buf
) + start
, length
);
411 return scm_double_cell (scm_tc7_symbol
| flags
, SCM_UNPACK (buf
),
412 (scm_t_bits
) hash
, SCM_UNPACK (props
));
416 scm_i_symbol_length (SCM sym
)
418 return STRINGBUF_LENGTH (SYMBOL_STRINGBUF (sym
));
422 scm_i_symbol_chars (SCM sym
)
424 SCM buf
= SYMBOL_STRINGBUF (sym
);
425 return STRINGBUF_CHARS (buf
);
429 scm_i_symbol_mark (SCM sym
)
431 scm_gc_mark (SYMBOL_STRINGBUF (sym
));
432 return SCM_CELL_OBJECT_3 (sym
);
436 scm_i_symbol_free (SCM sym
)
441 scm_i_symbol_substring (SCM sym
, size_t start
, size_t end
)
443 SCM buf
= SYMBOL_STRINGBUF (sym
);
444 scm_i_plugin_mutex_lock (&stringbuf_write_mutex
);
445 SET_STRINGBUF_SHARED (buf
);
446 scm_i_plugin_mutex_unlock (&stringbuf_write_mutex
);
447 return scm_double_cell (STRING_TAG
, SCM_UNPACK(buf
),
448 (scm_t_bits
)start
, (scm_t_bits
) end
- start
);
456 SCM
scm_sys_string_dump (SCM
);
457 SCM
scm_sys_symbol_dump (SCM
);
458 SCM
scm_sys_stringbuf_hist (void);
460 SCM_DEFINE (scm_sys_string_dump
, "%string-dump", 1, 0, 0,
463 #define FUNC_NAME s_scm_sys_string_dump
465 SCM_VALIDATE_STRING (1, str
);
466 fprintf (stderr
, "%p:\n", str
);
467 fprintf (stderr
, " start: %u\n", STRING_START (str
));
468 fprintf (stderr
, " len: %u\n", STRING_LENGTH (str
));
469 if (IS_SH_STRING (str
))
471 fprintf (stderr
, " string: %p\n", SH_STRING_STRING (str
));
472 fprintf (stderr
, "\n");
473 scm_sys_string_dump (SH_STRING_STRING (str
));
477 SCM buf
= STRING_STRINGBUF (str
);
478 fprintf (stderr
, " buf: %p\n", buf
);
479 fprintf (stderr
, " chars: %p\n", STRINGBUF_CHARS (buf
));
480 fprintf (stderr
, " length: %u\n", STRINGBUF_LENGTH (buf
));
481 fprintf (stderr
, " flags: %x\n", (SCM_CELL_WORD_0 (buf
) & 0x300));
483 return SCM_UNSPECIFIED
;
487 SCM_DEFINE (scm_sys_symbol_dump
, "%symbol-dump", 1, 0, 0,
490 #define FUNC_NAME s_scm_sys_symbol_dump
492 SCM_VALIDATE_SYMBOL (1, sym
);
493 fprintf (stderr
, "%p:\n", sym
);
494 fprintf (stderr
, " hash: %lu\n", scm_i_symbol_hash (sym
));
496 SCM buf
= SYMBOL_STRINGBUF (sym
);
497 fprintf (stderr
, " buf: %p\n", buf
);
498 fprintf (stderr
, " chars: %p\n", STRINGBUF_CHARS (buf
));
499 fprintf (stderr
, " length: %u\n", STRINGBUF_LENGTH (buf
));
500 fprintf (stderr
, " shared: %u\n", STRINGBUF_SHARED (buf
));
502 return SCM_UNSPECIFIED
;
506 SCM_DEFINE (scm_sys_stringbuf_hist
, "%stringbuf-hist", 0, 0, 0,
509 #define FUNC_NAME s_scm_sys_stringbuf_hist
512 for (i
= 0; i
< 1000; i
++)
514 fprintf (stderr
, " %3d: %u\n", i
, lenhist
[i
]);
515 fprintf (stderr
, ">999: %u\n", lenhist
[1000]);
516 return SCM_UNSPECIFIED
;
524 SCM_DEFINE (scm_string_p
, "string?", 1, 0, 0,
526 "Return @code{#t} if @var{obj} is a string, else @code{#f}.")
527 #define FUNC_NAME s_scm_string_p
529 return scm_from_bool (IS_STRING (obj
));
534 SCM_REGISTER_PROC (s_scm_list_to_string
, "list->string", 1, 0, 0, scm_string
);
536 SCM_DEFINE (scm_string
, "string", 0, 0, 1,
538 "@deffnx {Scheme Procedure} list->string chrs\n"
539 "Return a newly allocated string composed of the arguments,\n"
541 #define FUNC_NAME s_scm_string
548 long i
= scm_ilength (chrs
);
550 SCM_ASSERT (i
>= 0, chrs
, SCM_ARG1
, FUNC_NAME
);
554 result
= scm_i_make_string (len
, &data
);
555 while (len
> 0 && scm_is_pair (chrs
))
557 SCM elt
= SCM_CAR (chrs
);
559 SCM_VALIDATE_CHAR (SCM_ARGn
, elt
);
560 *data
++ = SCM_CHAR (elt
);
561 chrs
= SCM_CDR (chrs
);
565 scm_misc_error (NULL
, "list changed while constructing string", SCM_EOL
);
566 if (!scm_is_null (chrs
))
567 scm_wrong_type_arg_msg (NULL
, 0, chrs
, "proper list");
573 SCM_DEFINE (scm_make_string
, "make-string", 1, 1, 0,
575 "Return a newly allocated string of\n"
576 "length @var{k}. If @var{chr} is given, then all elements of\n"
577 "the string are initialized to @var{chr}, otherwise the contents\n"
578 "of the @var{string} are unspecified.")
579 #define FUNC_NAME s_scm_make_string
581 return scm_c_make_string (scm_to_size_t (k
), chr
);
586 scm_c_make_string (size_t len
, SCM chr
)
587 #define FUNC_NAME NULL
590 SCM res
= scm_i_make_string (len
, &dst
);
592 if (!SCM_UNBNDP (chr
))
594 SCM_VALIDATE_CHAR (0, chr
);
595 memset (dst
, SCM_CHAR (chr
), len
);
602 SCM_DEFINE (scm_string_length
, "string-length", 1, 0, 0,
604 "Return the number of characters in @var{string}.")
605 #define FUNC_NAME s_scm_string_length
607 SCM_VALIDATE_STRING (1, string
);
608 return scm_from_size_t (STRING_LENGTH (string
));
613 scm_c_string_length (SCM string
)
615 if (!IS_STRING (string
))
616 scm_wrong_type_arg_msg (NULL
, 0, string
, "string");
617 return STRING_LENGTH (string
);
620 SCM_DEFINE (scm_string_ref
, "string-ref", 2, 0, 0,
622 "Return character @var{k} of @var{str} using zero-origin\n"
623 "indexing. @var{k} must be a valid index of @var{str}.")
624 #define FUNC_NAME s_scm_string_ref
628 SCM_VALIDATE_STRING (1, str
);
629 idx
= scm_to_unsigned_integer (k
, 0, scm_i_string_length (str
)-1);
630 return SCM_MAKE_CHAR (scm_i_string_chars (str
)[idx
]);
635 scm_c_string_ref (SCM str
, size_t p
)
637 if (p
>= scm_i_string_length (str
))
638 scm_out_of_range (NULL
, scm_from_size_t (p
));
639 return SCM_MAKE_CHAR (scm_i_string_chars (str
)[p
]);
642 SCM_DEFINE (scm_string_set_x
, "string-set!", 3, 0, 0,
643 (SCM str
, SCM k
, SCM chr
),
644 "Store @var{chr} in element @var{k} of @var{str} and return\n"
645 "an unspecified value. @var{k} must be a valid index of\n"
647 #define FUNC_NAME s_scm_string_set_x
651 SCM_VALIDATE_STRING (1, str
);
652 idx
= scm_to_unsigned_integer (k
, 0, scm_i_string_length(str
)-1);
653 SCM_VALIDATE_CHAR (3, chr
);
655 char *dst
= scm_i_string_writable_chars (str
);
656 dst
[idx
] = SCM_CHAR (chr
);
657 scm_i_string_stop_writing ();
659 return SCM_UNSPECIFIED
;
664 scm_c_string_set_x (SCM str
, size_t p
, SCM chr
)
666 if (p
>= scm_i_string_length (str
))
667 scm_out_of_range (NULL
, scm_from_size_t (p
));
669 char *dst
= scm_i_string_writable_chars (str
);
670 dst
[p
] = SCM_CHAR (chr
);
671 scm_i_string_stop_writing ();
675 SCM_DEFINE (scm_substring
, "substring", 2, 1, 0,
676 (SCM str
, SCM start
, SCM end
),
677 "Return a newly allocated string formed from the characters\n"
678 "of @var{str} beginning with index @var{start} (inclusive) and\n"
679 "ending with index @var{end} (exclusive).\n"
680 "@var{str} must be a string, @var{start} and @var{end} must be\n"
681 "exact integers satisfying:\n\n"
682 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
683 #define FUNC_NAME s_scm_substring
685 size_t len
, from
, to
;
687 SCM_VALIDATE_STRING (1, str
);
688 len
= scm_i_string_length (str
);
689 from
= scm_to_unsigned_integer (start
, 0, len
);
690 if (SCM_UNBNDP (end
))
693 to
= scm_to_unsigned_integer (end
, from
, len
);
694 return scm_i_substring (str
, from
, to
);
698 SCM_DEFINE (scm_substring_read_only
, "substring/read-only", 2, 1, 0,
699 (SCM str
, SCM start
, SCM end
),
700 "Return a newly allocated string formed from the characters\n"
701 "of @var{str} beginning with index @var{start} (inclusive) and\n"
702 "ending with index @var{end} (exclusive).\n"
703 "@var{str} must be a string, @var{start} and @var{end} must be\n"
704 "exact integers satisfying:\n"
706 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).\n"
708 "The returned string is read-only.\n")
709 #define FUNC_NAME s_scm_substring_read_only
711 size_t len
, from
, to
;
713 SCM_VALIDATE_STRING (1, str
);
714 len
= scm_i_string_length (str
);
715 from
= scm_to_unsigned_integer (start
, 0, len
);
716 if (SCM_UNBNDP (end
))
719 to
= scm_to_unsigned_integer (end
, from
, len
);
720 return scm_i_substring_read_only (str
, from
, to
);
724 SCM_DEFINE (scm_substring_copy
, "substring/copy", 2, 1, 0,
725 (SCM str
, SCM start
, SCM end
),
726 "Return a newly allocated string formed from the characters\n"
727 "of @var{str} beginning with index @var{start} (inclusive) and\n"
728 "ending with index @var{end} (exclusive).\n"
729 "@var{str} must be a string, @var{start} and @var{end} must be\n"
730 "exact integers satisfying:\n\n"
731 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
732 #define FUNC_NAME s_scm_substring_copy
734 /* For the Scheme version, START is mandatory, but for the C
735 version, it is optional. See scm_string_copy in srfi-13.c for a
741 SCM_VALIDATE_STRING (1, str
);
742 scm_i_get_substring_spec (scm_i_string_length (str
),
743 start
, &from
, end
, &to
);
744 return scm_i_substring_copy (str
, from
, to
);
748 SCM_DEFINE (scm_substring_shared
, "substring/shared", 2, 1, 0,
749 (SCM str
, SCM start
, SCM end
),
750 "Return string that indirectly refers to the characters\n"
751 "of @var{str} beginning with index @var{start} (inclusive) and\n"
752 "ending with index @var{end} (exclusive).\n"
753 "@var{str} must be a string, @var{start} and @var{end} must be\n"
754 "exact integers satisfying:\n\n"
755 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
756 #define FUNC_NAME s_scm_substring_shared
758 size_t len
, from
, to
;
760 SCM_VALIDATE_STRING (1, str
);
761 len
= scm_i_string_length (str
);
762 from
= scm_to_unsigned_integer (start
, 0, len
);
763 if (SCM_UNBNDP (end
))
766 to
= scm_to_unsigned_integer (end
, from
, len
);
767 return scm_i_substring_shared (str
, from
, to
);
771 SCM_DEFINE (scm_string_append
, "string-append", 0, 0, 1,
773 "Return a newly allocated string whose characters form the\n"
774 "concatenation of the given strings, @var{args}.")
775 #define FUNC_NAME s_scm_string_append
782 SCM_VALIDATE_REST_ARGUMENT (args
);
783 for (l
= args
; !scm_is_null (l
); l
= SCM_CDR (l
))
786 SCM_VALIDATE_STRING (SCM_ARGn
, s
);
787 i
+= scm_i_string_length (s
);
789 res
= scm_i_make_string (i
, &data
);
790 for (l
= args
; !scm_is_null (l
); l
= SCM_CDR (l
))
794 SCM_VALIDATE_STRING (SCM_ARGn
, s
);
795 len
= scm_i_string_length (s
);
796 memcpy (data
, scm_i_string_chars (s
), len
);
798 scm_remember_upto_here_1 (s
);
805 scm_is_string (SCM obj
)
807 return IS_STRING (obj
);
811 scm_from_locale_stringn (const char *str
, size_t len
)
816 if (len
== (size_t)-1)
818 res
= scm_i_make_string (len
, &dst
);
819 memcpy (dst
, str
, len
);
824 scm_from_locale_string (const char *str
)
826 return scm_from_locale_stringn (str
, -1);
830 scm_take_locale_stringn (char *str
, size_t len
)
832 if (len
== (size_t)-1)
833 return scm_take_locale_string (str
);
836 /* STR might not be zero terminated and we are not allowed to
837 look at str[len], so we have to make a new one...
839 SCM res
= scm_from_locale_stringn (str
, len
);
846 scm_take_locale_string (char *str
)
848 size_t len
= strlen (str
);
851 buf
= scm_double_cell (STRINGBUF_TAG
, (scm_t_bits
) str
,
852 (scm_t_bits
) len
, (scm_t_bits
) 0);
853 res
= scm_double_cell (STRING_TAG
,
855 (scm_t_bits
) 0, (scm_t_bits
) len
);
856 scm_gc_register_collectable_memory (str
, len
+1, "string");
861 scm_to_locale_stringn (SCM str
, size_t *lenp
)
866 if (!scm_is_string (str
))
867 scm_wrong_type_arg_msg (NULL
, 0, str
, "string");
868 len
= scm_i_string_length (str
);
869 res
= scm_malloc (len
+ ((lenp
==NULL
)? 1 : 0));
870 memcpy (res
, scm_i_string_chars (str
), len
);
874 if (strlen (res
) != len
)
877 scm_misc_error (NULL
,
878 "string contains #\\nul character: ~S",
885 scm_remember_upto_here_1 (str
);
890 scm_to_locale_string (SCM str
)
892 return scm_to_locale_stringn (str
, NULL
);
896 scm_to_locale_stringbuf (SCM str
, char *buf
, size_t max_len
)
900 if (!scm_is_string (str
))
901 scm_wrong_type_arg_msg (NULL
, 0, str
, "string");
902 len
= scm_i_string_length (str
);
903 memcpy (buf
, scm_i_string_chars (str
), (len
> max_len
)? max_len
: len
);
904 scm_remember_upto_here_1 (str
);
908 /* converts C scm_array of strings to SCM scm_list of strings. */
909 /* If argc < 0, a null terminated scm_array is assumed. */
911 scm_makfromstrs (int argc
, char **argv
)
916 for (i
= 0; argv
[i
]; i
++);
918 lst
= scm_cons (scm_from_locale_string (argv
[i
]), lst
);
922 /* Return a newly allocated array of char pointers to each of the strings
923 in args, with a terminating NULL pointer. */
926 scm_i_allocate_string_pointers (SCM list
)
929 int len
= scm_ilength (list
);
933 scm_wrong_type_arg_msg (NULL
, 0, list
, "proper list");
937 result
= (char **) scm_malloc ((len
+ 1) * sizeof (char *));
939 scm_frame_unwind_handler (free
, result
, 0);
941 /* The list might be have been modified in another thread, so
942 we check LIST before each access.
944 for (i
= 0; i
< len
&& scm_is_pair (list
); i
++)
946 result
[i
] = scm_to_locale_string (SCM_CAR (list
));
947 list
= SCM_CDR (list
);
955 scm_i_free_string_pointers (char **pointers
)
959 for (i
= 0; pointers
[i
]; i
++)
965 scm_i_get_substring_spec (size_t len
,
966 SCM start
, size_t *cstart
,
967 SCM end
, size_t *cend
)
969 if (SCM_UNBNDP (start
))
972 *cstart
= scm_to_unsigned_integer (start
, 0, len
);
974 if (SCM_UNBNDP (end
))
977 *cend
= scm_to_unsigned_integer (end
, *cstart
, len
);
980 #if SCM_ENABLE_DEPRECATED
982 /* When these definitions are removed, it becomes reasonable to use
983 read-only strings for string literals. For that, change the reader
984 to create string literals with scm_c_substring_read_only instead of
985 with scm_c_substring_copy.
989 SCM_STRINGP (SCM str
)
991 scm_c_issue_deprecation_warning
992 ("SCM_STRINGP is deprecated. Use scm_is_string instead.");
994 return scm_is_string (str
);
998 SCM_STRING_CHARS (SCM str
)
1002 scm_c_issue_deprecation_warning
1003 ("SCM_STRING_CHARS is deprecated. See the manual for alternatives.");
1005 /* We don't accept shared substrings here since they are not
1008 if (IS_SH_STRING (str
))
1009 scm_misc_error (NULL
,
1010 "SCM_STRING_CHARS does not work with shared substrings.",
1013 /* We explicitely test for read-only strings to produce a better
1017 if (IS_RO_STRING (str
))
1018 scm_misc_error (NULL
,
1019 "SCM_STRING_CHARS does not work with read-only strings.",
1022 /* The following is still wrong, of course...
1024 chars
= scm_i_string_writable_chars (str
);
1025 scm_i_string_stop_writing ();
1030 SCM_STRING_LENGTH (SCM str
)
1032 scm_c_issue_deprecation_warning
1033 ("SCM_STRING_LENGTH is deprecated. Use scm_c_string_length instead.");
1034 return scm_c_string_length (str
);
1042 scm_nullstr
= scm_i_make_string (0, NULL
);
1044 #include "libguile/strings.x"