1 /* Copyright (C) 1995,1996,1998,2000,2001, 2004, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 /* FIXME: Create an `scm_gc' equivalent to `GC_MALLOC_ATOMIC ()'. */
119 char *mem
= GC_MALLOC_ATOMIC (len
+ 1);/* scm_gc_malloc (len+1, "string"); */
121 return scm_double_cell (STRINGBUF_TAG
, (scm_t_bits
) mem
,
122 (scm_t_bits
) len
, (scm_t_bits
) 0);
126 /* Return a new stringbuf whose underlying storage consists of the LEN+1
127 octets pointed to by STR (the last octet is zero). */
128 SCM_C_INLINE_KEYWORD SCM
129 scm_i_take_stringbufn (char *str
, size_t len
)
131 scm_gc_register_collectable_memory (str
, len
+ 1, "stringbuf");
133 return scm_double_cell (STRINGBUF_TAG
, (scm_t_bits
) str
,
134 (scm_t_bits
) len
, (scm_t_bits
) 0);
138 scm_i_stringbuf_mark (SCM buf
)
144 scm_i_stringbuf_free (SCM buf
)
146 if (!STRINGBUF_INLINE (buf
))
147 scm_gc_free (STRINGBUF_OUTLINE_CHARS (buf
),
148 STRINGBUF_OUTLINE_LENGTH (buf
) + 1, "string");
151 scm_i_pthread_mutex_t stringbuf_write_mutex
= SCM_I_PTHREAD_MUTEX_INITIALIZER
;
153 /* Copy-on-write strings.
156 #define STRING_TAG scm_tc7_string
158 #define STRING_STRINGBUF(str) (SCM_CELL_OBJECT_1(str))
159 #define STRING_START(str) ((size_t)SCM_CELL_WORD_2(str))
160 #define STRING_LENGTH(str) ((size_t)SCM_CELL_WORD_3(str))
162 #define SET_STRING_STRINGBUF(str,buf) (SCM_SET_CELL_OBJECT_1(str,buf))
163 #define SET_STRING_START(str,start) (SCM_SET_CELL_WORD_2(str,start))
165 #define IS_STRING(str) (SCM_NIMP(str) && SCM_TYP7(str) == STRING_TAG)
167 /* Read-only strings.
170 #define RO_STRING_TAG (scm_tc7_string + 0x200)
171 #define IS_RO_STRING(str) (SCM_CELL_TYPE(str)==RO_STRING_TAG)
173 /* Mutation-sharing substrings
176 #define SH_STRING_TAG (scm_tc7_string + 0x100)
178 #define SH_STRING_STRING(sh) (SCM_CELL_OBJECT_1(sh))
179 /* START and LENGTH as for STRINGs. */
181 #define IS_SH_STRING(str) (SCM_CELL_TYPE(str)==SH_STRING_TAG)
184 scm_i_make_string (size_t len
, char **charsp
)
186 SCM buf
= make_stringbuf (len
);
189 *charsp
= STRINGBUF_CHARS (buf
);
190 res
= scm_double_cell (STRING_TAG
, SCM_UNPACK(buf
),
191 (scm_t_bits
)0, (scm_t_bits
) len
);
196 validate_substring_args (SCM str
, size_t start
, size_t end
)
198 if (!IS_STRING (str
))
199 scm_wrong_type_arg_msg (NULL
, 0, str
, "string");
200 if (start
> STRING_LENGTH (str
))
201 scm_out_of_range (NULL
, scm_from_size_t (start
));
202 if (end
> STRING_LENGTH (str
) || end
< start
)
203 scm_out_of_range (NULL
, scm_from_size_t (end
));
207 get_str_buf_start (SCM
*str
, SCM
*buf
, size_t *start
)
209 *start
= STRING_START (*str
);
210 if (IS_SH_STRING (*str
))
212 *str
= SH_STRING_STRING (*str
);
213 *start
+= STRING_START (*str
);
215 *buf
= STRING_STRINGBUF (*str
);
219 scm_i_substring (SCM str
, size_t start
, size_t end
)
223 get_str_buf_start (&str
, &buf
, &str_start
);
224 scm_i_pthread_mutex_lock (&stringbuf_write_mutex
);
225 SET_STRINGBUF_SHARED (buf
);
226 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex
);
227 return scm_double_cell (STRING_TAG
, SCM_UNPACK(buf
),
228 (scm_t_bits
)str_start
+ start
,
229 (scm_t_bits
) end
- start
);
233 scm_i_substring_read_only (SCM str
, size_t start
, size_t end
)
237 get_str_buf_start (&str
, &buf
, &str_start
);
238 scm_i_pthread_mutex_lock (&stringbuf_write_mutex
);
239 SET_STRINGBUF_SHARED (buf
);
240 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex
);
241 return scm_double_cell (RO_STRING_TAG
, SCM_UNPACK(buf
),
242 (scm_t_bits
)str_start
+ start
,
243 (scm_t_bits
) end
- start
);
247 scm_i_substring_copy (SCM str
, size_t start
, size_t end
)
249 size_t len
= end
- start
;
252 get_str_buf_start (&str
, &buf
, &str_start
);
253 my_buf
= make_stringbuf (len
);
254 memcpy (STRINGBUF_CHARS (my_buf
),
255 STRINGBUF_CHARS (buf
) + str_start
+ start
, len
);
256 scm_remember_upto_here_1 (buf
);
257 return scm_double_cell (STRING_TAG
, SCM_UNPACK(my_buf
),
258 (scm_t_bits
)0, (scm_t_bits
) len
);
262 scm_i_substring_shared (SCM str
, size_t start
, size_t end
)
264 if (start
== 0 && end
== STRING_LENGTH (str
))
268 size_t len
= end
- start
;
269 if (IS_SH_STRING (str
))
271 start
+= STRING_START (str
);
272 str
= SH_STRING_STRING (str
);
274 return scm_double_cell (SH_STRING_TAG
, SCM_UNPACK(str
),
275 (scm_t_bits
)start
, (scm_t_bits
) len
);
280 scm_c_substring (SCM str
, size_t start
, size_t end
)
282 validate_substring_args (str
, start
, end
);
283 return scm_i_substring (str
, start
, end
);
287 scm_c_substring_read_only (SCM str
, size_t start
, size_t end
)
289 validate_substring_args (str
, start
, end
);
290 return scm_i_substring_read_only (str
, start
, end
);
294 scm_c_substring_copy (SCM str
, size_t start
, size_t end
)
296 validate_substring_args (str
, start
, end
);
297 return scm_i_substring_copy (str
, start
, end
);
301 scm_c_substring_shared (SCM str
, size_t start
, size_t end
)
303 validate_substring_args (str
, start
, end
);
304 return scm_i_substring_shared (str
, start
, end
);
308 scm_i_string_mark (SCM str
)
310 if (IS_SH_STRING (str
))
311 return SH_STRING_STRING (str
);
313 return STRING_STRINGBUF (str
);
317 scm_i_string_free (SCM str
)
321 /* Internal accessors
325 scm_i_string_length (SCM str
)
327 return STRING_LENGTH (str
);
331 scm_i_string_chars (SCM str
)
335 get_str_buf_start (&str
, &buf
, &start
);
336 return STRINGBUF_CHARS (buf
) + start
;
340 scm_i_string_writable_chars (SCM orig_str
)
342 SCM buf
, str
= orig_str
;
345 get_str_buf_start (&str
, &buf
, &start
);
346 if (IS_RO_STRING (str
))
347 scm_misc_error (NULL
, "string is read-only: ~s", scm_list_1 (orig_str
));
349 scm_i_pthread_mutex_lock (&stringbuf_write_mutex
);
350 if (STRINGBUF_SHARED (buf
))
352 /* Clone stringbuf. For this, we put all threads to sleep.
355 size_t len
= STRING_LENGTH (str
);
358 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex
);
360 new_buf
= make_stringbuf (len
);
361 memcpy (STRINGBUF_CHARS (new_buf
),
362 STRINGBUF_CHARS (buf
) + STRING_START (str
), len
);
364 scm_i_thread_put_to_sleep ();
365 SET_STRING_STRINGBUF (str
, new_buf
);
366 start
-= STRING_START (str
);
367 SET_STRING_START (str
, 0);
368 scm_i_thread_wake_up ();
372 scm_i_pthread_mutex_lock (&stringbuf_write_mutex
);
375 return STRINGBUF_CHARS (buf
) + start
;
379 scm_i_string_stop_writing (void)
381 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex
);
386 Basic symbol creation and accessing is done here, the rest is in
387 symbols.[hc]. This has been done to keep stringbufs and the
388 internals of strings and string-like objects confined to this file.
391 #define SYMBOL_STRINGBUF SCM_CELL_OBJECT_1
394 scm_i_make_symbol (SCM name
, scm_t_bits flags
,
395 unsigned long hash
, SCM props
)
398 size_t start
= STRING_START (name
);
399 size_t length
= STRING_LENGTH (name
);
401 if (IS_SH_STRING (name
))
403 name
= SH_STRING_STRING (name
);
404 start
+= STRING_START (name
);
406 buf
= SYMBOL_STRINGBUF (name
);
408 if (start
== 0 && length
== STRINGBUF_LENGTH (buf
))
411 scm_i_pthread_mutex_lock (&stringbuf_write_mutex
);
412 SET_STRINGBUF_SHARED (buf
);
413 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex
);
418 SCM new_buf
= make_stringbuf (length
);
419 memcpy (STRINGBUF_CHARS (new_buf
),
420 STRINGBUF_CHARS (buf
) + start
, length
);
423 return scm_double_cell (scm_tc7_symbol
| flags
, SCM_UNPACK (buf
),
424 (scm_t_bits
) hash
, SCM_UNPACK (props
));
428 scm_i_c_make_symbol (const char *name
, size_t len
,
429 scm_t_bits flags
, unsigned long hash
, SCM props
)
431 SCM buf
= make_stringbuf (len
);
432 memcpy (STRINGBUF_CHARS (buf
), name
, len
);
434 return scm_double_cell (scm_tc7_symbol
| flags
, SCM_UNPACK (buf
),
435 (scm_t_bits
) hash
, SCM_UNPACK (props
));
438 /* Return a new symbol that uses the LEN bytes pointed to by NAME as its
439 underlying storage. */
441 scm_i_c_take_symbol (char *name
, size_t len
,
442 scm_t_bits flags
, unsigned long hash
, SCM props
)
444 SCM buf
= scm_i_take_stringbufn (name
, len
);
446 return scm_double_cell (scm_tc7_symbol
| flags
, SCM_UNPACK (buf
),
447 (scm_t_bits
) hash
, SCM_UNPACK (props
));
451 scm_i_symbol_length (SCM sym
)
453 return STRINGBUF_LENGTH (SYMBOL_STRINGBUF (sym
));
457 scm_i_symbol_chars (SCM sym
)
459 SCM buf
= SYMBOL_STRINGBUF (sym
);
460 return STRINGBUF_CHARS (buf
);
464 scm_i_symbol_mark (SCM sym
)
466 scm_gc_mark (SYMBOL_STRINGBUF (sym
));
467 return SCM_CELL_OBJECT_3 (sym
);
471 scm_i_symbol_free (SCM sym
)
476 scm_i_symbol_substring (SCM sym
, size_t start
, size_t end
)
478 SCM buf
= SYMBOL_STRINGBUF (sym
);
479 scm_i_pthread_mutex_lock (&stringbuf_write_mutex
);
480 SET_STRINGBUF_SHARED (buf
);
481 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex
);
482 return scm_double_cell (STRING_TAG
, SCM_UNPACK(buf
),
483 (scm_t_bits
)start
, (scm_t_bits
) end
- start
);
491 SCM
scm_sys_string_dump (SCM
);
492 SCM
scm_sys_symbol_dump (SCM
);
493 SCM
scm_sys_stringbuf_hist (void);
495 SCM_DEFINE (scm_sys_string_dump
, "%string-dump", 1, 0, 0,
498 #define FUNC_NAME s_scm_sys_string_dump
500 SCM_VALIDATE_STRING (1, str
);
501 fprintf (stderr
, "%p:\n", str
);
502 fprintf (stderr
, " start: %u\n", STRING_START (str
));
503 fprintf (stderr
, " len: %u\n", STRING_LENGTH (str
));
504 if (IS_SH_STRING (str
))
506 fprintf (stderr
, " string: %p\n", SH_STRING_STRING (str
));
507 fprintf (stderr
, "\n");
508 scm_sys_string_dump (SH_STRING_STRING (str
));
512 SCM buf
= STRING_STRINGBUF (str
);
513 fprintf (stderr
, " buf: %p\n", buf
);
514 fprintf (stderr
, " chars: %p\n", STRINGBUF_CHARS (buf
));
515 fprintf (stderr
, " length: %u\n", STRINGBUF_LENGTH (buf
));
516 fprintf (stderr
, " flags: %x\n", (SCM_CELL_WORD_0 (buf
) & 0x300));
518 return SCM_UNSPECIFIED
;
522 SCM_DEFINE (scm_sys_symbol_dump
, "%symbol-dump", 1, 0, 0,
525 #define FUNC_NAME s_scm_sys_symbol_dump
527 SCM_VALIDATE_SYMBOL (1, sym
);
528 fprintf (stderr
, "%p:\n", sym
);
529 fprintf (stderr
, " hash: %lu\n", scm_i_symbol_hash (sym
));
531 SCM buf
= SYMBOL_STRINGBUF (sym
);
532 fprintf (stderr
, " buf: %p\n", buf
);
533 fprintf (stderr
, " chars: %p\n", STRINGBUF_CHARS (buf
));
534 fprintf (stderr
, " length: %u\n", STRINGBUF_LENGTH (buf
));
535 fprintf (stderr
, " shared: %u\n", STRINGBUF_SHARED (buf
));
537 return SCM_UNSPECIFIED
;
541 SCM_DEFINE (scm_sys_stringbuf_hist
, "%stringbuf-hist", 0, 0, 0,
544 #define FUNC_NAME s_scm_sys_stringbuf_hist
547 for (i
= 0; i
< 1000; i
++)
549 fprintf (stderr
, " %3d: %u\n", i
, lenhist
[i
]);
550 fprintf (stderr
, ">999: %u\n", lenhist
[1000]);
551 return SCM_UNSPECIFIED
;
559 SCM_DEFINE (scm_string_p
, "string?", 1, 0, 0,
561 "Return @code{#t} if @var{obj} is a string, else @code{#f}.")
562 #define FUNC_NAME s_scm_string_p
564 return scm_from_bool (IS_STRING (obj
));
569 SCM_REGISTER_PROC (s_scm_list_to_string
, "list->string", 1, 0, 0, scm_string
);
571 SCM_DEFINE (scm_string
, "string", 0, 0, 1,
573 "@deffnx {Scheme Procedure} list->string chrs\n"
574 "Return a newly allocated string composed of the arguments,\n"
576 #define FUNC_NAME s_scm_string
583 long i
= scm_ilength (chrs
);
585 SCM_ASSERT (i
>= 0, chrs
, SCM_ARG1
, FUNC_NAME
);
589 result
= scm_i_make_string (len
, &data
);
590 while (len
> 0 && scm_is_pair (chrs
))
592 SCM elt
= SCM_CAR (chrs
);
594 SCM_VALIDATE_CHAR (SCM_ARGn
, elt
);
595 *data
++ = SCM_CHAR (elt
);
596 chrs
= SCM_CDR (chrs
);
600 scm_misc_error (NULL
, "list changed while constructing string", SCM_EOL
);
601 if (!scm_is_null (chrs
))
602 scm_wrong_type_arg_msg (NULL
, 0, chrs
, "proper list");
608 SCM_DEFINE (scm_make_string
, "make-string", 1, 1, 0,
610 "Return a newly allocated string of\n"
611 "length @var{k}. If @var{chr} is given, then all elements of\n"
612 "the string are initialized to @var{chr}, otherwise the contents\n"
613 "of the @var{string} are unspecified.")
614 #define FUNC_NAME s_scm_make_string
616 return scm_c_make_string (scm_to_size_t (k
), chr
);
621 scm_c_make_string (size_t len
, SCM chr
)
622 #define FUNC_NAME NULL
625 SCM res
= scm_i_make_string (len
, &dst
);
627 if (!SCM_UNBNDP (chr
))
629 SCM_VALIDATE_CHAR (0, chr
);
630 memset (dst
, SCM_CHAR (chr
), len
);
637 SCM_DEFINE (scm_string_length
, "string-length", 1, 0, 0,
639 "Return the number of characters in @var{string}.")
640 #define FUNC_NAME s_scm_string_length
642 SCM_VALIDATE_STRING (1, string
);
643 return scm_from_size_t (STRING_LENGTH (string
));
648 scm_c_string_length (SCM string
)
650 if (!IS_STRING (string
))
651 scm_wrong_type_arg_msg (NULL
, 0, string
, "string");
652 return STRING_LENGTH (string
);
655 SCM_DEFINE (scm_string_ref
, "string-ref", 2, 0, 0,
657 "Return character @var{k} of @var{str} using zero-origin\n"
658 "indexing. @var{k} must be a valid index of @var{str}.")
659 #define FUNC_NAME s_scm_string_ref
663 SCM_VALIDATE_STRING (1, str
);
664 idx
= scm_to_unsigned_integer (k
, 0, scm_i_string_length (str
)-1);
665 return SCM_MAKE_CHAR (scm_i_string_chars (str
)[idx
]);
670 scm_c_string_ref (SCM str
, size_t p
)
672 if (p
>= scm_i_string_length (str
))
673 scm_out_of_range (NULL
, scm_from_size_t (p
));
674 return SCM_MAKE_CHAR (scm_i_string_chars (str
)[p
]);
677 SCM_DEFINE (scm_string_set_x
, "string-set!", 3, 0, 0,
678 (SCM str
, SCM k
, SCM chr
),
679 "Store @var{chr} in element @var{k} of @var{str} and return\n"
680 "an unspecified value. @var{k} must be a valid index of\n"
682 #define FUNC_NAME s_scm_string_set_x
686 SCM_VALIDATE_STRING (1, str
);
687 idx
= scm_to_unsigned_integer (k
, 0, scm_i_string_length(str
)-1);
688 SCM_VALIDATE_CHAR (3, chr
);
690 char *dst
= scm_i_string_writable_chars (str
);
691 dst
[idx
] = SCM_CHAR (chr
);
692 scm_i_string_stop_writing ();
694 return SCM_UNSPECIFIED
;
699 scm_c_string_set_x (SCM str
, size_t p
, SCM chr
)
701 if (p
>= scm_i_string_length (str
))
702 scm_out_of_range (NULL
, scm_from_size_t (p
));
704 char *dst
= scm_i_string_writable_chars (str
);
705 dst
[p
] = SCM_CHAR (chr
);
706 scm_i_string_stop_writing ();
710 SCM_DEFINE (scm_substring
, "substring", 2, 1, 0,
711 (SCM str
, SCM start
, SCM end
),
712 "Return a newly allocated string formed from the characters\n"
713 "of @var{str} beginning with index @var{start} (inclusive) and\n"
714 "ending with index @var{end} (exclusive).\n"
715 "@var{str} must be a string, @var{start} and @var{end} must be\n"
716 "exact integers satisfying:\n\n"
717 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
718 #define FUNC_NAME s_scm_substring
720 size_t len
, from
, to
;
722 SCM_VALIDATE_STRING (1, str
);
723 len
= scm_i_string_length (str
);
724 from
= scm_to_unsigned_integer (start
, 0, len
);
725 if (SCM_UNBNDP (end
))
728 to
= scm_to_unsigned_integer (end
, from
, len
);
729 return scm_i_substring (str
, from
, to
);
733 SCM_DEFINE (scm_substring_read_only
, "substring/read-only", 2, 1, 0,
734 (SCM str
, SCM start
, SCM end
),
735 "Return a newly allocated string formed from the characters\n"
736 "of @var{str} beginning with index @var{start} (inclusive) and\n"
737 "ending with index @var{end} (exclusive).\n"
738 "@var{str} must be a string, @var{start} and @var{end} must be\n"
739 "exact integers satisfying:\n"
741 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).\n"
743 "The returned string is read-only.\n")
744 #define FUNC_NAME s_scm_substring_read_only
746 size_t len
, from
, to
;
748 SCM_VALIDATE_STRING (1, str
);
749 len
= scm_i_string_length (str
);
750 from
= scm_to_unsigned_integer (start
, 0, len
);
751 if (SCM_UNBNDP (end
))
754 to
= scm_to_unsigned_integer (end
, from
, len
);
755 return scm_i_substring_read_only (str
, from
, to
);
759 SCM_DEFINE (scm_substring_copy
, "substring/copy", 2, 1, 0,
760 (SCM str
, SCM start
, SCM end
),
761 "Return a newly allocated string formed from the characters\n"
762 "of @var{str} beginning with index @var{start} (inclusive) and\n"
763 "ending with index @var{end} (exclusive).\n"
764 "@var{str} must be a string, @var{start} and @var{end} must be\n"
765 "exact integers satisfying:\n\n"
766 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
767 #define FUNC_NAME s_scm_substring_copy
769 /* For the Scheme version, START is mandatory, but for the C
770 version, it is optional. See scm_string_copy in srfi-13.c for a
776 SCM_VALIDATE_STRING (1, str
);
777 scm_i_get_substring_spec (scm_i_string_length (str
),
778 start
, &from
, end
, &to
);
779 return scm_i_substring_copy (str
, from
, to
);
783 SCM_DEFINE (scm_substring_shared
, "substring/shared", 2, 1, 0,
784 (SCM str
, SCM start
, SCM end
),
785 "Return string that indirectly refers to the characters\n"
786 "of @var{str} beginning with index @var{start} (inclusive) and\n"
787 "ending with index @var{end} (exclusive).\n"
788 "@var{str} must be a string, @var{start} and @var{end} must be\n"
789 "exact integers satisfying:\n\n"
790 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
791 #define FUNC_NAME s_scm_substring_shared
793 size_t len
, from
, to
;
795 SCM_VALIDATE_STRING (1, str
);
796 len
= scm_i_string_length (str
);
797 from
= scm_to_unsigned_integer (start
, 0, len
);
798 if (SCM_UNBNDP (end
))
801 to
= scm_to_unsigned_integer (end
, from
, len
);
802 return scm_i_substring_shared (str
, from
, to
);
806 SCM_DEFINE (scm_string_append
, "string-append", 0, 0, 1,
808 "Return a newly allocated string whose characters form the\n"
809 "concatenation of the given strings, @var{args}.")
810 #define FUNC_NAME s_scm_string_append
817 SCM_VALIDATE_REST_ARGUMENT (args
);
818 for (l
= args
; !scm_is_null (l
); l
= SCM_CDR (l
))
821 SCM_VALIDATE_STRING (SCM_ARGn
, s
);
822 i
+= scm_i_string_length (s
);
824 res
= scm_i_make_string (i
, &data
);
825 for (l
= args
; !scm_is_null (l
); l
= SCM_CDR (l
))
829 SCM_VALIDATE_STRING (SCM_ARGn
, s
);
830 len
= scm_i_string_length (s
);
831 memcpy (data
, scm_i_string_chars (s
), len
);
833 scm_remember_upto_here_1 (s
);
840 scm_is_string (SCM obj
)
842 return IS_STRING (obj
);
846 scm_from_locale_stringn (const char *str
, size_t len
)
851 if (len
== (size_t)-1)
853 res
= scm_i_make_string (len
, &dst
);
854 memcpy (dst
, str
, len
);
859 scm_from_locale_string (const char *str
)
861 return scm_from_locale_stringn (str
, -1);
865 scm_take_locale_stringn (char *str
, size_t len
)
869 if (len
== (size_t)-1)
873 /* Ensure STR is null terminated. A realloc for 1 extra byte should
874 often be satisfied from the alignment padding after the block, with
875 no actual data movement. */
876 str
= scm_realloc (str
, len
+1);
880 buf
= scm_i_take_stringbufn (str
, len
);
881 res
= scm_double_cell (STRING_TAG
,
883 (scm_t_bits
) 0, (scm_t_bits
) len
);
888 scm_take_locale_string (char *str
)
890 return scm_take_locale_stringn (str
, -1);
894 scm_to_locale_stringn (SCM str
, size_t *lenp
)
899 if (!scm_is_string (str
))
900 scm_wrong_type_arg_msg (NULL
, 0, str
, "string");
901 len
= scm_i_string_length (str
);
902 res
= scm_malloc (len
+ ((lenp
==NULL
)? 1 : 0));
903 memcpy (res
, scm_i_string_chars (str
), len
);
907 if (strlen (res
) != len
)
910 scm_misc_error (NULL
,
911 "string contains #\\nul character: ~S",
918 scm_remember_upto_here_1 (str
);
923 scm_to_locale_string (SCM str
)
925 return scm_to_locale_stringn (str
, NULL
);
929 scm_to_locale_stringbuf (SCM str
, char *buf
, size_t max_len
)
933 if (!scm_is_string (str
))
934 scm_wrong_type_arg_msg (NULL
, 0, str
, "string");
935 len
= scm_i_string_length (str
);
936 memcpy (buf
, scm_i_string_chars (str
), (len
> max_len
)? max_len
: len
);
937 scm_remember_upto_here_1 (str
);
941 /* converts C scm_array of strings to SCM scm_list of strings. */
942 /* If argc < 0, a null terminated scm_array is assumed. */
944 scm_makfromstrs (int argc
, char **argv
)
949 for (i
= 0; argv
[i
]; i
++);
951 lst
= scm_cons (scm_from_locale_string (argv
[i
]), lst
);
955 /* Return a newly allocated array of char pointers to each of the strings
956 in args, with a terminating NULL pointer. */
959 scm_i_allocate_string_pointers (SCM list
)
962 int len
= scm_ilength (list
);
966 scm_wrong_type_arg_msg (NULL
, 0, list
, "proper list");
968 scm_dynwind_begin (0);
970 result
= (char **) scm_malloc ((len
+ 1) * sizeof (char *));
972 scm_dynwind_unwind_handler (free
, result
, 0);
974 /* The list might be have been modified in another thread, so
975 we check LIST before each access.
977 for (i
= 0; i
< len
&& scm_is_pair (list
); i
++)
979 result
[i
] = scm_to_locale_string (SCM_CAR (list
));
980 list
= SCM_CDR (list
);
988 scm_i_free_string_pointers (char **pointers
)
992 for (i
= 0; pointers
[i
]; i
++)
998 scm_i_get_substring_spec (size_t len
,
999 SCM start
, size_t *cstart
,
1000 SCM end
, size_t *cend
)
1002 if (SCM_UNBNDP (start
))
1005 *cstart
= scm_to_unsigned_integer (start
, 0, len
);
1007 if (SCM_UNBNDP (end
))
1010 *cend
= scm_to_unsigned_integer (end
, *cstart
, len
);
1013 #if SCM_ENABLE_DEPRECATED
1015 /* When these definitions are removed, it becomes reasonable to use
1016 read-only strings for string literals. For that, change the reader
1017 to create string literals with scm_c_substring_read_only instead of
1018 with scm_c_substring_copy.
1022 scm_i_deprecated_stringp (SCM str
)
1024 scm_c_issue_deprecation_warning
1025 ("SCM_STRINGP is deprecated. Use scm_is_string instead.");
1027 return scm_is_string (str
);
1031 scm_i_deprecated_string_chars (SCM str
)
1035 scm_c_issue_deprecation_warning
1036 ("SCM_STRING_CHARS is deprecated. See the manual for alternatives.");
1038 /* We don't accept shared substrings here since they are not
1041 if (IS_SH_STRING (str
))
1042 scm_misc_error (NULL
,
1043 "SCM_STRING_CHARS does not work with shared substrings.",
1046 /* We explicitely test for read-only strings to produce a better
1050 if (IS_RO_STRING (str
))
1051 scm_misc_error (NULL
,
1052 "SCM_STRING_CHARS does not work with read-only strings.",
1055 /* The following is still wrong, of course...
1057 chars
= scm_i_string_writable_chars (str
);
1058 scm_i_string_stop_writing ();
1063 scm_i_deprecated_string_length (SCM str
)
1065 scm_c_issue_deprecation_warning
1066 ("SCM_STRING_LENGTH is deprecated. Use scm_c_string_length instead.");
1067 return scm_c_string_length (str
);
1075 scm_nullstr
= scm_i_make_string (0, NULL
);
1077 #include "libguile/strings.x"