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)
156 scm_i_make_string (size_t len
, char **charsp
)
158 SCM buf
= make_stringbuf (len
);
161 *charsp
= STRINGBUF_CHARS (buf
);
162 res
= scm_double_cell (STRING_TAG
, SCM_UNPACK(buf
),
163 (scm_t_bits
)0, (scm_t_bits
) len
);
168 validate_substring_args (SCM str
, size_t start
, size_t end
)
170 if (!IS_STRING (str
))
171 scm_wrong_type_arg_msg (NULL
, 0, str
, "string");
172 if (start
> STRING_LENGTH (str
))
173 scm_out_of_range (NULL
, scm_from_size_t (start
));
174 if (end
> STRING_LENGTH (str
) || end
< start
)
175 scm_out_of_range (NULL
, scm_from_size_t (end
));
179 scm_i_substring (SCM str
, size_t start
, size_t end
)
181 SCM buf
= STRING_STRINGBUF (str
);
182 scm_i_plugin_mutex_lock (&stringbuf_write_mutex
);
183 SET_STRINGBUF_SHARED (buf
);
184 scm_i_plugin_mutex_unlock (&stringbuf_write_mutex
);
185 return scm_double_cell (STRING_TAG
, SCM_UNPACK(buf
),
186 (scm_t_bits
)start
, (scm_t_bits
) end
- start
);
190 scm_i_substring_copy (SCM str
, size_t start
, size_t end
)
192 size_t len
= end
- start
;
193 SCM buf
= STRING_STRINGBUF (str
);
194 SCM my_buf
= make_stringbuf (len
);
195 memcpy (STRINGBUF_CHARS (my_buf
), STRINGBUF_CHARS (buf
) + start
, len
);
196 scm_remember_upto_here_1 (buf
);
197 return scm_double_cell (STRING_TAG
, SCM_UNPACK(my_buf
),
198 (scm_t_bits
)0, (scm_t_bits
) len
);
202 scm_c_substring (SCM str
, size_t start
, size_t end
)
204 validate_substring_args (str
, start
, end
);
205 return scm_i_substring (str
, start
, end
);
209 scm_c_substring_copy (SCM str
, size_t start
, size_t end
)
211 validate_substring_args (str
, start
, end
);
212 return scm_i_substring_copy (str
, start
, end
);
215 /* Mutation-sharing substrings
218 #define SH_STRING_TAG (scm_tc7_string + 0x100)
220 #define SH_STRING_STRING(sh) (SCM_CELL_OBJECT_1(sh))
221 /* START and LENGTH as for STRINGs. */
223 #define IS_SH_STRING(str) (SCM_CELL_TYPE(str)==SH_STRING_TAG)
226 scm_i_substring_shared (SCM str
, size_t start
, size_t end
)
228 if (start
== 0 && end
== STRING_LENGTH (str
))
232 SCM res
= scm_double_cell (SH_STRING_TAG
, SCM_UNPACK(str
),
233 (scm_t_bits
)start
, (scm_t_bits
) end
- start
);
239 scm_c_substring_shared (SCM str
, size_t start
, size_t end
)
241 validate_substring_args (str
, start
, end
);
242 return scm_i_substring_shared (str
, start
, end
);
246 scm_i_string_mark (SCM str
)
248 if (IS_SH_STRING (str
))
249 return SH_STRING_STRING (str
);
251 return STRING_STRINGBUF (str
);
255 scm_i_string_free (SCM str
)
259 /* Internal accessors
263 scm_i_string_length (SCM str
)
265 return STRING_LENGTH (str
);
269 scm_i_string_chars (SCM str
)
272 size_t start
= STRING_START(str
);
273 if (IS_SH_STRING (str
))
275 str
= SH_STRING_STRING (str
);
276 start
+= STRING_START (str
);
278 buf
= STRING_STRINGBUF (str
);
279 return STRINGBUF_CHARS (buf
) + start
;
283 scm_i_string_writable_chars (SCM str
)
286 size_t start
= STRING_START(str
);
287 if (IS_SH_STRING (str
))
289 str
= SH_STRING_STRING (str
);
290 start
+= STRING_START (str
);
292 buf
= STRING_STRINGBUF (str
);
293 scm_i_plugin_mutex_lock (&stringbuf_write_mutex
);
294 if (STRINGBUF_SHARED (buf
))
296 /* Clone stringbuf. For this, we put all threads to sleep.
299 size_t len
= STRING_LENGTH (str
);
302 scm_i_plugin_mutex_unlock (&stringbuf_write_mutex
);
304 new_buf
= make_stringbuf (len
);
305 memcpy (STRINGBUF_CHARS (new_buf
),
306 STRINGBUF_CHARS (buf
) + STRING_START (str
), len
);
308 scm_i_thread_put_to_sleep ();
309 SET_STRING_STRINGBUF (str
, new_buf
);
310 start
-= STRING_START (str
);
311 SET_STRING_START (str
, 0);
312 scm_i_thread_wake_up ();
316 scm_i_plugin_mutex_lock (&stringbuf_write_mutex
);
319 return STRINGBUF_CHARS (buf
) + start
;
323 scm_i_string_stop_writing (void)
325 scm_i_plugin_mutex_unlock (&stringbuf_write_mutex
);
330 Basic symbol creation and accessing is done here, the rest is in
331 symbols.[hc]. This has been done to keep stringbufs and the
332 internals of strings and string-like objects confined to this file.
335 #define SYMBOL_STRINGBUF SCM_CELL_OBJECT_1
338 scm_i_make_symbol (SCM name
, unsigned long hash
, SCM props
)
341 size_t start
= STRING_START (name
);
342 size_t length
= STRING_LENGTH (name
);
344 if (IS_SH_STRING (name
))
346 name
= SH_STRING_STRING (name
);
347 start
+= STRING_START (name
);
349 buf
= SYMBOL_STRINGBUF (name
);
351 if (start
== 0 && length
== STRINGBUF_LENGTH (buf
))
354 scm_i_plugin_mutex_lock (&stringbuf_write_mutex
);
355 SET_STRINGBUF_SHARED (buf
);
356 scm_i_plugin_mutex_unlock (&stringbuf_write_mutex
);
361 SCM new_buf
= make_stringbuf (length
);
362 memcpy (STRINGBUF_CHARS (new_buf
),
363 STRINGBUF_CHARS (buf
) + start
, length
);
366 return scm_double_cell (scm_tc7_symbol
, SCM_UNPACK (buf
),
367 (scm_t_bits
) hash
, SCM_UNPACK (props
));
371 scm_i_symbol_length (SCM sym
)
373 return STRINGBUF_LENGTH (SYMBOL_STRINGBUF (sym
));
377 scm_i_symbol_chars (SCM sym
)
379 SCM buf
= SYMBOL_STRINGBUF (sym
);
380 return STRINGBUF_CHARS (buf
);
384 scm_i_symbol_mark (SCM sym
)
386 scm_gc_mark (SYMBOL_STRINGBUF (sym
));
387 return SCM_CELL_OBJECT_3 (sym
);
391 scm_i_symbol_free (SCM sym
)
396 scm_i_symbol_substring (SCM sym
, size_t start
, size_t end
)
398 SCM buf
= SYMBOL_STRINGBUF (sym
);
399 scm_i_plugin_mutex_lock (&stringbuf_write_mutex
);
400 SET_STRINGBUF_SHARED (buf
);
401 scm_i_plugin_mutex_unlock (&stringbuf_write_mutex
);
402 return scm_double_cell (STRING_TAG
, SCM_UNPACK(buf
),
403 (scm_t_bits
)start
, (scm_t_bits
) end
- start
);
411 SCM
scm_sys_string_dump (SCM
);
412 SCM
scm_sys_symbol_dump (SCM
);
413 SCM
scm_sys_stringbuf_hist (void);
415 SCM_DEFINE (scm_sys_string_dump
, "%string-dump", 1, 0, 0,
418 #define FUNC_NAME s_scm_sys_string_dump
420 SCM_VALIDATE_STRING (1, str
);
421 fprintf (stderr
, "%p:\n", str
);
422 fprintf (stderr
, " start: %u\n", STRING_START (str
));
423 fprintf (stderr
, " len: %u\n", STRING_LENGTH (str
));
424 if (IS_SH_STRING (str
))
426 fprintf (stderr
, " string: %p\n", SH_STRING_STRING (str
));
427 fprintf (stderr
, "\n");
428 scm_sys_string_dump (SH_STRING_STRING (str
));
432 SCM buf
= STRING_STRINGBUF (str
);
433 fprintf (stderr
, " buf: %p\n", buf
);
434 fprintf (stderr
, " chars: %p\n", STRINGBUF_CHARS (buf
));
435 fprintf (stderr
, " length: %u\n", STRINGBUF_LENGTH (buf
));
436 fprintf (stderr
, " flags: %x\n", (SCM_CELL_WORD_0 (buf
) & 0x300));
438 return SCM_UNSPECIFIED
;
442 SCM_DEFINE (scm_sys_symbol_dump
, "%symbol-dump", 1, 0, 0,
445 #define FUNC_NAME s_scm_sys_symbol_dump
447 SCM_VALIDATE_SYMBOL (1, sym
);
448 fprintf (stderr
, "%p:\n", sym
);
449 fprintf (stderr
, " hash: %lu\n", scm_i_symbol_hash (sym
));
451 SCM buf
= SYMBOL_STRINGBUF (sym
);
452 fprintf (stderr
, " buf: %p\n", buf
);
453 fprintf (stderr
, " chars: %p\n", STRINGBUF_CHARS (buf
));
454 fprintf (stderr
, " length: %u\n", STRINGBUF_LENGTH (buf
));
455 fprintf (stderr
, " shared: %u\n", STRINGBUF_SHARED (buf
));
457 return SCM_UNSPECIFIED
;
461 SCM_DEFINE (scm_sys_stringbuf_hist
, "%stringbuf-hist", 0, 0, 0,
464 #define FUNC_NAME s_scm_sys_string_dump
467 for (i
= 0; i
< 1000; i
++)
469 fprintf (stderr
, " %3d: %u\n", i
, lenhist
[i
]);
470 fprintf (stderr
, ">999: %u\n", lenhist
[1000]);
471 return SCM_UNSPECIFIED
;
479 SCM_DEFINE (scm_string_p
, "string?", 1, 0, 0,
481 "Return @code{#t} if @var{obj} is a string, else @code{#f}.")
482 #define FUNC_NAME s_scm_string_p
484 return scm_from_bool (IS_STRING (obj
));
489 SCM_REGISTER_PROC (s_scm_list_to_string
, "list->string", 1, 0, 0, scm_string
);
491 SCM_DEFINE (scm_string
, "string", 0, 0, 1,
493 "@deffnx {Scheme Procedure} list->string chrs\n"
494 "Return a newly allocated string composed of the arguments,\n"
496 #define FUNC_NAME s_scm_string
503 long i
= scm_ilength (chrs
);
505 SCM_ASSERT (i
>= 0, chrs
, SCM_ARG1
, FUNC_NAME
);
509 result
= scm_i_make_string (len
, &data
);
510 while (len
> 0 && SCM_CONSP (chrs
))
512 SCM elt
= SCM_CAR (chrs
);
514 SCM_VALIDATE_CHAR (SCM_ARGn
, elt
);
515 *data
++ = SCM_CHAR (elt
);
516 chrs
= SCM_CDR (chrs
);
520 scm_misc_error (NULL
, "list changed while constructing string", SCM_EOL
);
521 if (!SCM_NULLP (chrs
))
522 scm_wrong_type_arg_msg (NULL
, 0, chrs
, "proper list");
528 SCM_DEFINE (scm_make_string
, "make-string", 1, 1, 0,
530 "Return a newly allocated string of\n"
531 "length @var{k}. If @var{chr} is given, then all elements of\n"
532 "the string are initialized to @var{chr}, otherwise the contents\n"
533 "of the @var{string} are unspecified.")
534 #define FUNC_NAME s_scm_make_string
536 return scm_c_make_string (scm_to_size_t (k
), chr
);
541 scm_c_make_string (size_t len
, SCM chr
)
542 #define FUNC_NAME NULL
545 SCM res
= scm_i_make_string (len
, &dst
);
547 if (!SCM_UNBNDP (chr
))
549 SCM_VALIDATE_CHAR (0, chr
);
550 memset (dst
, SCM_CHAR (chr
), len
);
557 SCM_DEFINE (scm_string_length
, "string-length", 1, 0, 0,
559 "Return the number of characters in @var{string}.")
560 #define FUNC_NAME s_scm_string_length
562 SCM_VALIDATE_STRING (1, string
);
563 return scm_from_size_t (STRING_LENGTH (string
));
568 scm_c_string_length (SCM string
)
570 if (!IS_STRING (string
))
571 scm_wrong_type_arg_msg (NULL
, 0, string
, "string");
572 return STRING_LENGTH (string
);
575 SCM_DEFINE (scm_string_ref
, "string-ref", 2, 0, 0,
577 "Return character @var{k} of @var{str} using zero-origin\n"
578 "indexing. @var{k} must be a valid index of @var{str}.")
579 #define FUNC_NAME s_scm_string_ref
583 SCM_VALIDATE_STRING (1, str
);
584 idx
= scm_to_unsigned_integer (k
, 0, scm_i_string_length (str
)-1);
585 return SCM_MAKE_CHAR (scm_i_string_chars (str
)[idx
]);
590 scm_c_string_ref (SCM str
, size_t p
)
592 if (p
>= scm_i_string_length (str
))
593 scm_out_of_range (NULL
, scm_from_size_t (p
));
594 return SCM_MAKE_CHAR (scm_i_string_chars (str
)[p
]);
597 SCM_DEFINE (scm_string_set_x
, "string-set!", 3, 0, 0,
598 (SCM str
, SCM k
, SCM chr
),
599 "Store @var{chr} in element @var{k} of @var{str} and return\n"
600 "an unspecified value. @var{k} must be a valid index of\n"
602 #define FUNC_NAME s_scm_string_set_x
606 SCM_VALIDATE_STRING (1, str
);
607 idx
= scm_to_unsigned_integer (k
, 0, scm_i_string_length(str
)-1);
608 SCM_VALIDATE_CHAR (3, chr
);
610 char *dst
= scm_i_string_writable_chars (str
);
611 dst
[idx
] = SCM_CHAR (chr
);
612 scm_i_string_stop_writing ();
614 return SCM_UNSPECIFIED
;
619 scm_c_string_set_x (SCM str
, size_t p
, SCM chr
)
621 if (p
>= scm_i_string_length (str
))
622 scm_out_of_range (NULL
, scm_from_size_t (p
));
624 char *dst
= scm_i_string_writable_chars (str
);
625 dst
[p
] = SCM_CHAR (chr
);
626 scm_i_string_stop_writing ();
630 SCM_DEFINE (scm_substring
, "substring", 2, 1, 0,
631 (SCM str
, SCM start
, SCM end
),
632 "Return a newly allocated string formed from the characters\n"
633 "of @var{str} beginning with index @var{start} (inclusive) and\n"
634 "ending with index @var{end} (exclusive).\n"
635 "@var{str} must be a string, @var{start} and @var{end} must be\n"
636 "exact integers satisfying:\n\n"
637 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
638 #define FUNC_NAME s_scm_substring
640 size_t len
, from
, to
;
642 SCM_VALIDATE_STRING (1, str
);
643 len
= scm_i_string_length (str
);
644 from
= scm_to_unsigned_integer (start
, 0, len
);
645 if (SCM_UNBNDP (end
))
648 to
= scm_to_unsigned_integer (end
, from
, len
);
649 return scm_i_substring (str
, from
, to
);
653 SCM_DEFINE (scm_substring_copy
, "substring/copy", 2, 1, 0,
654 (SCM str
, SCM start
, SCM end
),
655 "Return a newly allocated string formed from the characters\n"
656 "of @var{str} beginning with index @var{start} (inclusive) and\n"
657 "ending with index @var{end} (exclusive).\n"
658 "@var{str} must be a string, @var{start} and @var{end} must be\n"
659 "exact integers satisfying:\n\n"
660 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
661 #define FUNC_NAME s_scm_substring_copy
663 size_t len
, from
, to
;
665 SCM_VALIDATE_STRING (1, str
);
666 len
= scm_i_string_length (str
);
667 from
= scm_to_unsigned_integer (start
, 0, len
);
668 if (SCM_UNBNDP (end
))
671 to
= scm_to_unsigned_integer (end
, from
, len
);
672 return scm_i_substring_copy (str
, from
, to
);
676 SCM_DEFINE (scm_substring_shared
, "substring/shared", 2, 1, 0,
677 (SCM str
, SCM start
, SCM end
),
678 "Return string that indirectly refers to the characters\n"
679 "of @var{str} beginning with index @var{start} (inclusive) and\n"
680 "ending with index @var{end} (exclusive).\n"
681 "@var{str} must be a string, @var{start} and @var{end} must be\n"
682 "exact integers satisfying:\n\n"
683 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
684 #define FUNC_NAME s_scm_substring_shared
686 size_t len
, from
, to
;
688 SCM_VALIDATE_STRING (1, str
);
689 len
= scm_i_string_length (str
);
690 from
= scm_to_unsigned_integer (start
, 0, len
);
691 if (SCM_UNBNDP (end
))
694 to
= scm_to_unsigned_integer (end
, from
, len
);
695 return scm_i_substring_shared (str
, from
, to
);
699 SCM_DEFINE (scm_string_append
, "string-append", 0, 0, 1,
701 "Return a newly allocated string whose characters form the\n"
702 "concatenation of the given strings, @var{args}.")
703 #define FUNC_NAME s_scm_string_append
710 SCM_VALIDATE_REST_ARGUMENT (args
);
711 for (l
= args
; !SCM_NULLP (l
); l
= SCM_CDR (l
))
714 SCM_VALIDATE_STRING (SCM_ARGn
, s
);
715 i
+= scm_i_string_length (s
);
717 res
= scm_i_make_string (i
, &data
);
718 for (l
= args
; !SCM_NULLP (l
); l
= SCM_CDR (l
))
721 SCM_VALIDATE_STRING (SCM_ARGn
, s
);
722 size_t len
= scm_i_string_length (s
);
723 memcpy (data
, scm_i_string_chars (s
), len
);
725 scm_remember_upto_here_1 (s
);
732 scm_is_string (SCM obj
)
734 return IS_STRING (obj
);
738 scm_from_locale_stringn (const char *str
, size_t len
)
743 if (len
== (size_t)-1)
745 res
= scm_i_make_string (len
, &dst
);
746 memcpy (dst
, str
, len
);
751 scm_from_locale_string (const char *str
)
753 return scm_from_locale_stringn (str
, -1);
757 scm_take_locale_stringn (char *str
, size_t len
)
759 if (len
== (size_t)-1)
760 return scm_take_locale_string (str
);
763 /* STR might not be zero terminated and we are not allowed to
764 look at str[len], so we have to make a new one...
766 SCM res
= scm_from_locale_stringn (str
, len
);
773 scm_take_locale_string (char *str
)
775 size_t len
= strlen (str
);
778 buf
= scm_double_cell (STRINGBUF_TAG
, (scm_t_bits
) str
,
779 (scm_t_bits
) len
, (scm_t_bits
) 0);
780 res
= scm_double_cell (STRING_TAG
,
782 (scm_t_bits
) 0, (scm_t_bits
) len
);
783 scm_gc_register_collectable_memory (str
, len
+1, "string");
788 scm_to_locale_stringn (SCM str
, size_t *lenp
)
793 if (!scm_is_string (str
))
794 scm_wrong_type_arg_msg (NULL
, 0, str
, "string");
795 len
= scm_i_string_length (str
);
796 res
= scm_malloc (len
+ ((lenp
==NULL
)? 1 : 0));
797 memcpy (res
, scm_i_string_chars (str
), len
);
801 if (strlen (res
) != len
)
804 scm_misc_error (NULL
,
805 "string contains #\\nul character: ~S",
812 scm_remember_upto_here_1 (str
);
817 scm_to_locale_string (SCM str
)
819 return scm_to_locale_stringn (str
, NULL
);
823 scm_to_locale_stringbuf (SCM str
, char *buf
, size_t max_len
)
827 if (!scm_is_string (str
))
828 scm_wrong_type_arg_msg (NULL
, 0, str
, "string");
829 len
= scm_i_string_length (str
);
830 memcpy (buf
, scm_i_string_chars (str
), (len
> max_len
)? max_len
: len
);
831 scm_remember_upto_here_1 (str
);
835 /* converts C scm_array of strings to SCM scm_list of strings. */
836 /* If argc < 0, a null terminated scm_array is assumed. */
838 scm_makfromstrs (int argc
, char **argv
)
843 for (i
= 0; argv
[i
]; i
++);
845 lst
= scm_cons (scm_from_locale_string (argv
[i
]), lst
);
849 /* Return a newly allocated array of char pointers to each of the strings
850 in args, with a terminating NULL pointer. */
853 scm_i_allocate_string_pointers (SCM list
)
856 int len
= scm_ilength (list
);
860 scm_wrong_type_arg_msg (NULL
, 0, list
, "proper list");
864 result
= (char **) scm_malloc ((len
+ 1) * sizeof (char *));
866 scm_frame_unwind_handler (free
, result
, 0);
868 /* The list might be have been modified in another thread, so
869 we check LIST before each access.
871 for (i
= 0; i
< len
&& SCM_CONSP (list
); i
++)
873 result
[i
] = scm_to_locale_string (SCM_CAR (list
));
874 list
= SCM_CDR (list
);
882 scm_i_free_string_pointers (char **pointers
)
886 for (i
= 0; pointers
[i
]; i
++)
892 scm_i_get_substring_spec (size_t len
,
893 SCM start
, size_t *cstart
,
894 SCM end
, size_t *cend
)
896 if (SCM_UNBNDP (start
))
899 *cstart
= scm_to_unsigned_integer (start
, 0, len
);
901 if (SCM_UNBNDP (end
))
904 *cend
= scm_to_unsigned_integer (end
, *cstart
, len
);
907 #if SCM_ENABLE_DEPRECATED
910 SCM_STRINGP (SCM str
)
912 scm_c_issue_deprecation_warning
913 ("SCM_STRINGP is deprecated. Use scm_is_string instead.");
915 /* We don't accept shared substrings here since they are not
919 return IS_STRING (str
) && !IS_SH_STRING (str
);
923 SCM_STRING_CHARS (SCM str
)
927 scm_c_issue_deprecation_warning
928 ("SCM_STRING_CHARS is deprecated. See the manual for alternatives.");
930 /* The following is wrong, of course...
932 chars
= scm_i_string_writable_chars (str
);
933 scm_i_string_stop_writing ();
938 SCM_STRING_LENGTH (SCM str
)
940 scm_c_issue_deprecation_warning
941 ("SCM_STRING_LENGTH is deprecated. Use scm_c_string_length instead.");
942 return scm_c_string_length (str
);
950 scm_nullstr
= scm_i_make_string (0, NULL
);
952 #include "libguile/strings.x"