7399d88310bde2d70d0ab7801c20987fa9189b65
[bpt/guile.git] / libguile / strings.c
1 /* Copyright (C) 1995,1996,1998,2000,2001, 2004, 2006, 2008 Free Software Foundation, Inc.
2 *
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.
7 *
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.
12 *
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
16 */
17
18
19 \f
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <string.h>
25 #include <stdio.h>
26
27 #include "libguile/_scm.h"
28 #include "libguile/chars.h"
29 #include "libguile/root.h"
30 #include "libguile/strings.h"
31 #include "libguile/deprecation.h"
32 #include "libguile/validate.h"
33 #include "libguile/dynwind.h"
34
35 \f
36
37 /* {Strings}
38 */
39
40
41 /* Stringbufs
42 *
43 * XXX - keeping an accurate refcount during GC seems to be quite
44 * tricky, so we just keep score of whether a stringbuf might be
45 * shared, not wether it definitely is.
46 *
47 * The scheme I (mvo) tried to keep an accurate reference count would
48 * recount all strings that point to a stringbuf during the mark-phase
49 * of the GC. This was done since one cannot access the stringbuf of
50 * a string when that string is freed (in order to decrease the
51 * reference count). The memory of the stringbuf might have been
52 * reused already for something completely different.
53 *
54 * This recounted worked for a small number of threads beating on
55 * cow-strings, but it failed randomly with more than 10 threads, say.
56 * I couldn't figure out what went wrong, so I used the conservative
57 * approach implemented below.
58 *
59 * A stringbuf needs to know its length, but only so that it can be
60 * reported when the stringbuf is freed.
61 *
62 * Stringbufs (and strings) are not stored very compactly: a stringbuf
63 * has room for about 2*sizeof(scm_t_bits)-1 bytes additional
64 * information. As a compensation, the code below is made more
65 * complicated by storing small strings inline in the double cell of a
66 * stringbuf. So we have fixstrings and bigstrings...
67 */
68
69 #define STRINGBUF_F_SHARED 0x100
70 #define STRINGBUF_F_INLINE 0x200
71
72 #define STRINGBUF_TAG scm_tc7_stringbuf
73 #define STRINGBUF_SHARED(buf) (SCM_CELL_WORD_0(buf) & STRINGBUF_F_SHARED)
74 #define STRINGBUF_INLINE(buf) (SCM_CELL_WORD_0(buf) & STRINGBUF_F_INLINE)
75
76 #define STRINGBUF_OUTLINE_CHARS(buf) ((char *)SCM_CELL_WORD_1(buf))
77 #define STRINGBUF_OUTLINE_LENGTH(buf) (SCM_CELL_WORD_2(buf))
78 #define STRINGBUF_INLINE_CHARS(buf) ((char *)SCM_CELL_OBJECT_LOC(buf,1))
79 #define STRINGBUF_INLINE_LENGTH(buf) (((size_t)SCM_CELL_WORD_0(buf))>>16)
80
81 #define STRINGBUF_CHARS(buf) (STRINGBUF_INLINE (buf) \
82 ? STRINGBUF_INLINE_CHARS (buf) \
83 : STRINGBUF_OUTLINE_CHARS (buf))
84 #define STRINGBUF_LENGTH(buf) (STRINGBUF_INLINE (buf) \
85 ? STRINGBUF_INLINE_LENGTH (buf) \
86 : STRINGBUF_OUTLINE_LENGTH (buf))
87
88 #define STRINGBUF_MAX_INLINE_LEN (3*sizeof(scm_t_bits))
89
90 #define SET_STRINGBUF_SHARED(buf) \
91 (SCM_SET_CELL_WORD_0 ((buf), SCM_CELL_WORD_0 (buf) | STRINGBUF_F_SHARED))
92
93 #if SCM_DEBUG
94 static size_t lenhist[1001];
95 #endif
96
97 static SCM
98 make_stringbuf (size_t len)
99 {
100 /* XXX - for the benefit of SCM_STRING_CHARS, SCM_SYMBOL_CHARS and
101 scm_i_symbol_chars, all stringbufs are null-terminated. Once
102 SCM_STRING_CHARS and SCM_SYMBOL_CHARS are removed and the code
103 has been changed for scm_i_symbol_chars, this null-termination
104 can be dropped.
105 */
106
107 #if SCM_DEBUG
108 if (len < 1000)
109 lenhist[len]++;
110 else
111 lenhist[1000]++;
112 #endif
113
114 if (len <= STRINGBUF_MAX_INLINE_LEN-1)
115 {
116 return scm_double_cell (STRINGBUF_TAG | STRINGBUF_F_INLINE | (len << 16),
117 0, 0, 0);
118 }
119 else
120 {
121 char *mem = scm_gc_malloc (len+1, "string");
122 mem[len] = '\0';
123 return scm_double_cell (STRINGBUF_TAG, (scm_t_bits) mem,
124 (scm_t_bits) len, (scm_t_bits) 0);
125 }
126 }
127
128 /* Return a new stringbuf whose underlying storage consists of the LEN+1
129 octets pointed to by STR (the last octet is zero). */
130 SCM
131 scm_i_take_stringbufn (char *str, size_t len)
132 {
133 scm_gc_register_collectable_memory (str, len + 1, "stringbuf");
134
135 return scm_double_cell (STRINGBUF_TAG, (scm_t_bits) str,
136 (scm_t_bits) len, (scm_t_bits) 0);
137 }
138
139 SCM
140 scm_i_stringbuf_mark (SCM buf)
141 {
142 return SCM_BOOL_F;
143 }
144
145 void
146 scm_i_stringbuf_free (SCM buf)
147 {
148 if (!STRINGBUF_INLINE (buf))
149 scm_gc_free (STRINGBUF_OUTLINE_CHARS (buf),
150 STRINGBUF_OUTLINE_LENGTH (buf) + 1, "string");
151 }
152
153 scm_i_pthread_mutex_t stringbuf_write_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
154
155 /* Copy-on-write strings.
156 */
157
158 #define STRING_TAG scm_tc7_string
159
160 #define STRING_STRINGBUF(str) (SCM_CELL_OBJECT_1(str))
161 #define STRING_START(str) ((size_t)SCM_CELL_WORD_2(str))
162 #define STRING_LENGTH(str) ((size_t)SCM_CELL_WORD_3(str))
163
164 #define SET_STRING_STRINGBUF(str,buf) (SCM_SET_CELL_OBJECT_1(str,buf))
165 #define SET_STRING_START(str,start) (SCM_SET_CELL_WORD_2(str,start))
166
167 #define IS_STRING(str) (SCM_NIMP(str) && SCM_TYP7(str) == STRING_TAG)
168
169 /* Read-only strings.
170 */
171
172 #define RO_STRING_TAG (scm_tc7_string + 0x200)
173 #define IS_RO_STRING(str) (SCM_CELL_TYPE(str)==RO_STRING_TAG)
174
175 /* Mutation-sharing substrings
176 */
177
178 #define SH_STRING_TAG (scm_tc7_string + 0x100)
179
180 #define SH_STRING_STRING(sh) (SCM_CELL_OBJECT_1(sh))
181 /* START and LENGTH as for STRINGs. */
182
183 #define IS_SH_STRING(str) (SCM_CELL_TYPE(str)==SH_STRING_TAG)
184
185 SCM
186 scm_i_make_string (size_t len, char **charsp)
187 {
188 SCM buf = make_stringbuf (len);
189 SCM res;
190 if (charsp)
191 *charsp = STRINGBUF_CHARS (buf);
192 res = scm_double_cell (STRING_TAG, SCM_UNPACK(buf),
193 (scm_t_bits)0, (scm_t_bits) len);
194 return res;
195 }
196
197 static void
198 validate_substring_args (SCM str, size_t start, size_t end)
199 {
200 if (!IS_STRING (str))
201 scm_wrong_type_arg_msg (NULL, 0, str, "string");
202 if (start > STRING_LENGTH (str))
203 scm_out_of_range (NULL, scm_from_size_t (start));
204 if (end > STRING_LENGTH (str) || end < start)
205 scm_out_of_range (NULL, scm_from_size_t (end));
206 }
207
208 static inline void
209 get_str_buf_start (SCM *str, SCM *buf, size_t *start)
210 {
211 *start = STRING_START (*str);
212 if (IS_SH_STRING (*str))
213 {
214 *str = SH_STRING_STRING (*str);
215 *start += STRING_START (*str);
216 }
217 *buf = STRING_STRINGBUF (*str);
218 }
219
220 SCM
221 scm_i_substring (SCM str, size_t start, size_t end)
222 {
223 SCM buf;
224 size_t str_start;
225 get_str_buf_start (&str, &buf, &str_start);
226 scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
227 SET_STRINGBUF_SHARED (buf);
228 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
229 return scm_double_cell (STRING_TAG, SCM_UNPACK(buf),
230 (scm_t_bits)str_start + start,
231 (scm_t_bits) end - start);
232 }
233
234 SCM
235 scm_i_substring_read_only (SCM str, size_t start, size_t end)
236 {
237 SCM buf;
238 size_t str_start;
239 get_str_buf_start (&str, &buf, &str_start);
240 scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
241 SET_STRINGBUF_SHARED (buf);
242 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
243 return scm_double_cell (RO_STRING_TAG, SCM_UNPACK(buf),
244 (scm_t_bits)str_start + start,
245 (scm_t_bits) end - start);
246 }
247
248 SCM
249 scm_i_substring_copy (SCM str, size_t start, size_t end)
250 {
251 size_t len = end - start;
252 SCM buf, my_buf;
253 size_t str_start;
254 get_str_buf_start (&str, &buf, &str_start);
255 my_buf = make_stringbuf (len);
256 memcpy (STRINGBUF_CHARS (my_buf),
257 STRINGBUF_CHARS (buf) + str_start + start, len);
258 scm_remember_upto_here_1 (buf);
259 return scm_double_cell (STRING_TAG, SCM_UNPACK(my_buf),
260 (scm_t_bits)0, (scm_t_bits) len);
261 }
262
263 SCM
264 scm_i_substring_shared (SCM str, size_t start, size_t end)
265 {
266 if (start == 0 && end == STRING_LENGTH (str))
267 return str;
268 else
269 {
270 size_t len = end - start;
271 if (IS_SH_STRING (str))
272 {
273 start += STRING_START (str);
274 str = SH_STRING_STRING (str);
275 }
276 return scm_double_cell (SH_STRING_TAG, SCM_UNPACK(str),
277 (scm_t_bits)start, (scm_t_bits) len);
278 }
279 }
280
281 SCM
282 scm_c_substring (SCM str, size_t start, size_t end)
283 {
284 validate_substring_args (str, start, end);
285 return scm_i_substring (str, start, end);
286 }
287
288 SCM
289 scm_c_substring_read_only (SCM str, size_t start, size_t end)
290 {
291 validate_substring_args (str, start, end);
292 return scm_i_substring_read_only (str, start, end);
293 }
294
295 SCM
296 scm_c_substring_copy (SCM str, size_t start, size_t end)
297 {
298 validate_substring_args (str, start, end);
299 return scm_i_substring_copy (str, start, end);
300 }
301
302 SCM
303 scm_c_substring_shared (SCM str, size_t start, size_t end)
304 {
305 validate_substring_args (str, start, end);
306 return scm_i_substring_shared (str, start, end);
307 }
308
309 SCM
310 scm_i_string_mark (SCM str)
311 {
312 if (IS_SH_STRING (str))
313 return SH_STRING_STRING (str);
314 else
315 return STRING_STRINGBUF (str);
316 }
317
318 void
319 scm_i_string_free (SCM str)
320 {
321 }
322
323 /* Internal accessors
324 */
325
326 size_t
327 scm_i_string_length (SCM str)
328 {
329 return STRING_LENGTH (str);
330 }
331
332 const char *
333 scm_i_string_chars (SCM str)
334 {
335 SCM buf;
336 size_t start;
337 get_str_buf_start (&str, &buf, &start);
338 return STRINGBUF_CHARS (buf) + start;
339 }
340
341 char *
342 scm_i_string_writable_chars (SCM orig_str)
343 {
344 SCM buf, str = orig_str;
345 size_t start;
346
347 get_str_buf_start (&str, &buf, &start);
348 if (IS_RO_STRING (str))
349 scm_misc_error (NULL, "string is read-only: ~s", scm_list_1 (orig_str));
350
351 scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
352 if (STRINGBUF_SHARED (buf))
353 {
354 /* Clone stringbuf. For this, we put all threads to sleep.
355 */
356
357 size_t len = STRING_LENGTH (str);
358 SCM new_buf;
359
360 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
361
362 new_buf = make_stringbuf (len);
363 memcpy (STRINGBUF_CHARS (new_buf),
364 STRINGBUF_CHARS (buf) + STRING_START (str), len);
365
366 scm_i_thread_put_to_sleep ();
367 SET_STRING_STRINGBUF (str, new_buf);
368 start -= STRING_START (str);
369 SET_STRING_START (str, 0);
370 scm_i_thread_wake_up ();
371
372 buf = new_buf;
373
374 scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
375 }
376
377 return STRINGBUF_CHARS (buf) + start;
378 }
379
380 void
381 scm_i_string_stop_writing (void)
382 {
383 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
384 }
385
386 /* Symbols.
387
388 Basic symbol creation and accessing is done here, the rest is in
389 symbols.[hc]. This has been done to keep stringbufs and the
390 internals of strings and string-like objects confined to this file.
391 */
392
393 #define SYMBOL_STRINGBUF SCM_CELL_OBJECT_1
394
395 SCM
396 scm_i_make_symbol (SCM name, scm_t_bits flags,
397 unsigned long hash, SCM props)
398 {
399 SCM buf;
400 size_t start = STRING_START (name);
401 size_t length = STRING_LENGTH (name);
402
403 if (IS_SH_STRING (name))
404 {
405 name = SH_STRING_STRING (name);
406 start += STRING_START (name);
407 }
408 buf = SYMBOL_STRINGBUF (name);
409
410 if (start == 0 && length == STRINGBUF_LENGTH (buf))
411 {
412 /* reuse buf. */
413 scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
414 SET_STRINGBUF_SHARED (buf);
415 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
416 }
417 else
418 {
419 /* make new buf. */
420 SCM new_buf = make_stringbuf (length);
421 memcpy (STRINGBUF_CHARS (new_buf),
422 STRINGBUF_CHARS (buf) + start, length);
423 buf = new_buf;
424 }
425 return scm_double_cell (scm_tc7_symbol | flags, SCM_UNPACK (buf),
426 (scm_t_bits) hash, SCM_UNPACK (props));
427 }
428
429 SCM
430 scm_i_c_make_symbol (const char *name, size_t len,
431 scm_t_bits flags, unsigned long hash, SCM props)
432 {
433 SCM buf = make_stringbuf (len);
434 memcpy (STRINGBUF_CHARS (buf), name, len);
435
436 return scm_double_cell (scm_tc7_symbol | flags, SCM_UNPACK (buf),
437 (scm_t_bits) hash, SCM_UNPACK (props));
438 }
439
440 /* Return a new symbol that uses the LEN bytes pointed to by NAME as its
441 underlying storage. */
442 SCM
443 scm_i_c_take_symbol (char *name, size_t len,
444 scm_t_bits flags, unsigned long hash, SCM props)
445 {
446 SCM buf = scm_i_take_stringbufn (name, len);
447
448 return scm_double_cell (scm_tc7_symbol | flags, SCM_UNPACK (buf),
449 (scm_t_bits) hash, SCM_UNPACK (props));
450 }
451
452 size_t
453 scm_i_symbol_length (SCM sym)
454 {
455 return STRINGBUF_LENGTH (SYMBOL_STRINGBUF (sym));
456 }
457
458 size_t
459 scm_c_symbol_length (SCM sym)
460 #define FUNC_NAME "scm_c_symbol_length"
461 {
462 SCM_VALIDATE_SYMBOL (1, sym);
463
464 return STRINGBUF_LENGTH (SYMBOL_STRINGBUF (sym));
465 }
466 #undef FUNC_NAME
467
468 const char *
469 scm_i_symbol_chars (SCM sym)
470 {
471 SCM buf = SYMBOL_STRINGBUF (sym);
472 return STRINGBUF_CHARS (buf);
473 }
474
475 SCM
476 scm_i_symbol_mark (SCM sym)
477 {
478 scm_gc_mark (SYMBOL_STRINGBUF (sym));
479 return SCM_CELL_OBJECT_3 (sym);
480 }
481
482 void
483 scm_i_symbol_free (SCM sym)
484 {
485 }
486
487 SCM
488 scm_i_symbol_substring (SCM sym, size_t start, size_t end)
489 {
490 SCM buf = SYMBOL_STRINGBUF (sym);
491 scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
492 SET_STRINGBUF_SHARED (buf);
493 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
494 return scm_double_cell (RO_STRING_TAG, SCM_UNPACK (buf),
495 (scm_t_bits)start, (scm_t_bits) end - start);
496 }
497
498 /* Debugging
499 */
500
501 #if SCM_DEBUG
502
503 SCM scm_sys_string_dump (SCM);
504 SCM scm_sys_symbol_dump (SCM);
505 SCM scm_sys_stringbuf_hist (void);
506
507 SCM_DEFINE (scm_sys_string_dump, "%string-dump", 1, 0, 0,
508 (SCM str),
509 "")
510 #define FUNC_NAME s_scm_sys_string_dump
511 {
512 SCM_VALIDATE_STRING (1, str);
513 fprintf (stderr, "%p:\n", str);
514 fprintf (stderr, " start: %u\n", STRING_START (str));
515 fprintf (stderr, " len: %u\n", STRING_LENGTH (str));
516 if (IS_SH_STRING (str))
517 {
518 fprintf (stderr, " string: %p\n", SH_STRING_STRING (str));
519 fprintf (stderr, "\n");
520 scm_sys_string_dump (SH_STRING_STRING (str));
521 }
522 else
523 {
524 SCM buf = STRING_STRINGBUF (str);
525 fprintf (stderr, " buf: %p\n", buf);
526 fprintf (stderr, " chars: %p\n", STRINGBUF_CHARS (buf));
527 fprintf (stderr, " length: %u\n", STRINGBUF_LENGTH (buf));
528 fprintf (stderr, " flags: %x\n", (SCM_CELL_WORD_0 (buf) & 0x300));
529 }
530 return SCM_UNSPECIFIED;
531 }
532 #undef FUNC_NAME
533
534 SCM_DEFINE (scm_sys_symbol_dump, "%symbol-dump", 1, 0, 0,
535 (SCM sym),
536 "")
537 #define FUNC_NAME s_scm_sys_symbol_dump
538 {
539 SCM_VALIDATE_SYMBOL (1, sym);
540 fprintf (stderr, "%p:\n", sym);
541 fprintf (stderr, " hash: %lu\n", scm_i_symbol_hash (sym));
542 {
543 SCM buf = SYMBOL_STRINGBUF (sym);
544 fprintf (stderr, " buf: %p\n", buf);
545 fprintf (stderr, " chars: %p\n", STRINGBUF_CHARS (buf));
546 fprintf (stderr, " length: %u\n", STRINGBUF_LENGTH (buf));
547 fprintf (stderr, " shared: %u\n", STRINGBUF_SHARED (buf));
548 }
549 return SCM_UNSPECIFIED;
550 }
551 #undef FUNC_NAME
552
553 SCM_DEFINE (scm_sys_stringbuf_hist, "%stringbuf-hist", 0, 0, 0,
554 (void),
555 "")
556 #define FUNC_NAME s_scm_sys_stringbuf_hist
557 {
558 int i;
559 for (i = 0; i < 1000; i++)
560 if (lenhist[i])
561 fprintf (stderr, " %3d: %u\n", i, lenhist[i]);
562 fprintf (stderr, ">999: %u\n", lenhist[1000]);
563 return SCM_UNSPECIFIED;
564 }
565 #undef FUNC_NAME
566
567 #endif
568
569 \f
570
571 SCM_DEFINE (scm_string_p, "string?", 1, 0, 0,
572 (SCM obj),
573 "Return @code{#t} if @var{obj} is a string, else @code{#f}.")
574 #define FUNC_NAME s_scm_string_p
575 {
576 return scm_from_bool (IS_STRING (obj));
577 }
578 #undef FUNC_NAME
579
580
581 SCM_REGISTER_PROC (s_scm_list_to_string, "list->string", 1, 0, 0, scm_string);
582
583 SCM_DEFINE (scm_string, "string", 0, 0, 1,
584 (SCM chrs),
585 "@deffnx {Scheme Procedure} list->string chrs\n"
586 "Return a newly allocated string composed of the arguments,\n"
587 "@var{chrs}.")
588 #define FUNC_NAME s_scm_string
589 {
590 SCM result;
591 size_t len;
592 char *data;
593
594 {
595 long i = scm_ilength (chrs);
596
597 SCM_ASSERT (i >= 0, chrs, SCM_ARG1, FUNC_NAME);
598 len = i;
599 }
600
601 result = scm_i_make_string (len, &data);
602 while (len > 0 && scm_is_pair (chrs))
603 {
604 SCM elt = SCM_CAR (chrs);
605
606 SCM_VALIDATE_CHAR (SCM_ARGn, elt);
607 *data++ = SCM_CHAR (elt);
608 chrs = SCM_CDR (chrs);
609 len--;
610 }
611 if (len > 0)
612 scm_misc_error (NULL, "list changed while constructing string", SCM_EOL);
613 if (!scm_is_null (chrs))
614 scm_wrong_type_arg_msg (NULL, 0, chrs, "proper list");
615
616 return result;
617 }
618 #undef FUNC_NAME
619
620 SCM_DEFINE (scm_make_string, "make-string", 1, 1, 0,
621 (SCM k, SCM chr),
622 "Return a newly allocated string of\n"
623 "length @var{k}. If @var{chr} is given, then all elements of\n"
624 "the string are initialized to @var{chr}, otherwise the contents\n"
625 "of the @var{string} are unspecified.")
626 #define FUNC_NAME s_scm_make_string
627 {
628 return scm_c_make_string (scm_to_size_t (k), chr);
629 }
630 #undef FUNC_NAME
631
632 SCM
633 scm_c_make_string (size_t len, SCM chr)
634 #define FUNC_NAME NULL
635 {
636 char *dst;
637 SCM res = scm_i_make_string (len, &dst);
638
639 if (!SCM_UNBNDP (chr))
640 {
641 SCM_VALIDATE_CHAR (0, chr);
642 memset (dst, SCM_CHAR (chr), len);
643 }
644
645 return res;
646 }
647 #undef FUNC_NAME
648
649 SCM_DEFINE (scm_string_length, "string-length", 1, 0, 0,
650 (SCM string),
651 "Return the number of characters in @var{string}.")
652 #define FUNC_NAME s_scm_string_length
653 {
654 SCM_VALIDATE_STRING (1, string);
655 return scm_from_size_t (STRING_LENGTH (string));
656 }
657 #undef FUNC_NAME
658
659 size_t
660 scm_c_string_length (SCM string)
661 {
662 if (!IS_STRING (string))
663 scm_wrong_type_arg_msg (NULL, 0, string, "string");
664 return STRING_LENGTH (string);
665 }
666
667 SCM_DEFINE (scm_string_ref, "string-ref", 2, 0, 0,
668 (SCM str, SCM k),
669 "Return character @var{k} of @var{str} using zero-origin\n"
670 "indexing. @var{k} must be a valid index of @var{str}.")
671 #define FUNC_NAME s_scm_string_ref
672 {
673 unsigned long idx;
674
675 SCM_VALIDATE_STRING (1, str);
676 idx = scm_to_unsigned_integer (k, 0, scm_i_string_length (str)-1);
677 return SCM_MAKE_CHAR (scm_i_string_chars (str)[idx]);
678 }
679 #undef FUNC_NAME
680
681 SCM
682 scm_c_string_ref (SCM str, size_t p)
683 {
684 if (p >= scm_i_string_length (str))
685 scm_out_of_range (NULL, scm_from_size_t (p));
686 return SCM_MAKE_CHAR (scm_i_string_chars (str)[p]);
687 }
688
689 SCM_DEFINE (scm_string_set_x, "string-set!", 3, 0, 0,
690 (SCM str, SCM k, SCM chr),
691 "Store @var{chr} in element @var{k} of @var{str} and return\n"
692 "an unspecified value. @var{k} must be a valid index of\n"
693 "@var{str}.")
694 #define FUNC_NAME s_scm_string_set_x
695 {
696 unsigned long idx;
697
698 SCM_VALIDATE_STRING (1, str);
699 idx = scm_to_unsigned_integer (k, 0, scm_i_string_length(str)-1);
700 SCM_VALIDATE_CHAR (3, chr);
701 {
702 char *dst = scm_i_string_writable_chars (str);
703 dst[idx] = SCM_CHAR (chr);
704 scm_i_string_stop_writing ();
705 }
706 return SCM_UNSPECIFIED;
707 }
708 #undef FUNC_NAME
709
710 void
711 scm_c_string_set_x (SCM str, size_t p, SCM chr)
712 {
713 if (p >= scm_i_string_length (str))
714 scm_out_of_range (NULL, scm_from_size_t (p));
715 {
716 char *dst = scm_i_string_writable_chars (str);
717 dst[p] = SCM_CHAR (chr);
718 scm_i_string_stop_writing ();
719 }
720 }
721
722 SCM_DEFINE (scm_substring, "substring", 2, 1, 0,
723 (SCM str, SCM start, SCM end),
724 "Return a newly allocated string formed from the characters\n"
725 "of @var{str} beginning with index @var{start} (inclusive) and\n"
726 "ending with index @var{end} (exclusive).\n"
727 "@var{str} must be a string, @var{start} and @var{end} must be\n"
728 "exact integers satisfying:\n\n"
729 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
730 #define FUNC_NAME s_scm_substring
731 {
732 size_t len, from, to;
733
734 SCM_VALIDATE_STRING (1, str);
735 len = scm_i_string_length (str);
736 from = scm_to_unsigned_integer (start, 0, len);
737 if (SCM_UNBNDP (end))
738 to = len;
739 else
740 to = scm_to_unsigned_integer (end, from, len);
741 return scm_i_substring (str, from, to);
742 }
743 #undef FUNC_NAME
744
745 SCM_DEFINE (scm_substring_read_only, "substring/read-only", 2, 1, 0,
746 (SCM str, SCM start, SCM end),
747 "Return a newly allocated string formed from the characters\n"
748 "of @var{str} beginning with index @var{start} (inclusive) and\n"
749 "ending with index @var{end} (exclusive).\n"
750 "@var{str} must be a string, @var{start} and @var{end} must be\n"
751 "exact integers satisfying:\n"
752 "\n"
753 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).\n"
754 "\n"
755 "The returned string is read-only.\n")
756 #define FUNC_NAME s_scm_substring_read_only
757 {
758 size_t len, from, to;
759
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))
764 to = len;
765 else
766 to = scm_to_unsigned_integer (end, from, len);
767 return scm_i_substring_read_only (str, from, to);
768 }
769 #undef FUNC_NAME
770
771 SCM_DEFINE (scm_substring_copy, "substring/copy", 2, 1, 0,
772 (SCM str, SCM start, SCM end),
773 "Return a newly allocated string formed from the characters\n"
774 "of @var{str} beginning with index @var{start} (inclusive) and\n"
775 "ending with index @var{end} (exclusive).\n"
776 "@var{str} must be a string, @var{start} and @var{end} must be\n"
777 "exact integers satisfying:\n\n"
778 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
779 #define FUNC_NAME s_scm_substring_copy
780 {
781 /* For the Scheme version, START is mandatory, but for the C
782 version, it is optional. See scm_string_copy in srfi-13.c for a
783 rationale.
784 */
785
786 size_t from, to;
787
788 SCM_VALIDATE_STRING (1, str);
789 scm_i_get_substring_spec (scm_i_string_length (str),
790 start, &from, end, &to);
791 return scm_i_substring_copy (str, from, to);
792 }
793 #undef FUNC_NAME
794
795 SCM_DEFINE (scm_substring_shared, "substring/shared", 2, 1, 0,
796 (SCM str, SCM start, SCM end),
797 "Return string that indirectly refers to the characters\n"
798 "of @var{str} beginning with index @var{start} (inclusive) and\n"
799 "ending with index @var{end} (exclusive).\n"
800 "@var{str} must be a string, @var{start} and @var{end} must be\n"
801 "exact integers satisfying:\n\n"
802 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
803 #define FUNC_NAME s_scm_substring_shared
804 {
805 size_t len, from, to;
806
807 SCM_VALIDATE_STRING (1, str);
808 len = scm_i_string_length (str);
809 from = scm_to_unsigned_integer (start, 0, len);
810 if (SCM_UNBNDP (end))
811 to = len;
812 else
813 to = scm_to_unsigned_integer (end, from, len);
814 return scm_i_substring_shared (str, from, to);
815 }
816 #undef FUNC_NAME
817
818 SCM_DEFINE (scm_string_append, "string-append", 0, 0, 1,
819 (SCM args),
820 "Return a newly allocated string whose characters form the\n"
821 "concatenation of the given strings, @var{args}.")
822 #define FUNC_NAME s_scm_string_append
823 {
824 SCM res;
825 size_t i = 0;
826 SCM l, s;
827 char *data;
828
829 SCM_VALIDATE_REST_ARGUMENT (args);
830 for (l = args; !scm_is_null (l); l = SCM_CDR (l))
831 {
832 s = SCM_CAR (l);
833 SCM_VALIDATE_STRING (SCM_ARGn, s);
834 i += scm_i_string_length (s);
835 }
836 res = scm_i_make_string (i, &data);
837 for (l = args; !scm_is_null (l); l = SCM_CDR (l))
838 {
839 size_t len;
840 s = SCM_CAR (l);
841 SCM_VALIDATE_STRING (SCM_ARGn, s);
842 len = scm_i_string_length (s);
843 memcpy (data, scm_i_string_chars (s), len);
844 data += len;
845 scm_remember_upto_here_1 (s);
846 }
847 return res;
848 }
849 #undef FUNC_NAME
850
851 int
852 scm_is_string (SCM obj)
853 {
854 return IS_STRING (obj);
855 }
856
857 SCM
858 scm_from_locale_stringn (const char *str, size_t len)
859 {
860 SCM res;
861 char *dst;
862
863 if (len == (size_t)-1)
864 len = strlen (str);
865 res = scm_i_make_string (len, &dst);
866 memcpy (dst, str, len);
867 return res;
868 }
869
870 SCM
871 scm_from_locale_string (const char *str)
872 {
873 return scm_from_locale_stringn (str, -1);
874 }
875
876 SCM
877 scm_take_locale_stringn (char *str, size_t len)
878 {
879 SCM buf, res;
880
881 if (len == (size_t)-1)
882 len = strlen (str);
883 else
884 {
885 /* Ensure STR is null terminated. A realloc for 1 extra byte should
886 often be satisfied from the alignment padding after the block, with
887 no actual data movement. */
888 str = scm_realloc (str, len+1);
889 str[len] = '\0';
890 }
891
892 buf = scm_i_take_stringbufn (str, len);
893 res = scm_double_cell (STRING_TAG,
894 SCM_UNPACK (buf),
895 (scm_t_bits) 0, (scm_t_bits) len);
896 return res;
897 }
898
899 SCM
900 scm_take_locale_string (char *str)
901 {
902 return scm_take_locale_stringn (str, -1);
903 }
904
905 char *
906 scm_to_locale_stringn (SCM str, size_t *lenp)
907 {
908 char *res;
909 size_t len;
910
911 if (!scm_is_string (str))
912 scm_wrong_type_arg_msg (NULL, 0, str, "string");
913 len = scm_i_string_length (str);
914 res = scm_malloc (len + ((lenp==NULL)? 1 : 0));
915 memcpy (res, scm_i_string_chars (str), len);
916 if (lenp == NULL)
917 {
918 res[len] = '\0';
919 if (strlen (res) != len)
920 {
921 free (res);
922 scm_misc_error (NULL,
923 "string contains #\\nul character: ~S",
924 scm_list_1 (str));
925 }
926 }
927 else
928 *lenp = len;
929
930 scm_remember_upto_here_1 (str);
931 return res;
932 }
933
934 char *
935 scm_to_locale_string (SCM str)
936 {
937 return scm_to_locale_stringn (str, NULL);
938 }
939
940 size_t
941 scm_to_locale_stringbuf (SCM str, char *buf, size_t max_len)
942 {
943 size_t len;
944
945 if (!scm_is_string (str))
946 scm_wrong_type_arg_msg (NULL, 0, str, "string");
947 len = scm_i_string_length (str);
948 memcpy (buf, scm_i_string_chars (str), (len > max_len)? max_len : len);
949 scm_remember_upto_here_1 (str);
950 return len;
951 }
952
953 /* converts C scm_array of strings to SCM scm_list of strings. */
954 /* If argc < 0, a null terminated scm_array is assumed. */
955 SCM
956 scm_makfromstrs (int argc, char **argv)
957 {
958 int i = argc;
959 SCM lst = SCM_EOL;
960 if (0 > i)
961 for (i = 0; argv[i]; i++);
962 while (i--)
963 lst = scm_cons (scm_from_locale_string (argv[i]), lst);
964 return lst;
965 }
966
967 /* Return a newly allocated array of char pointers to each of the strings
968 in args, with a terminating NULL pointer. */
969
970 char **
971 scm_i_allocate_string_pointers (SCM list)
972 {
973 char **result;
974 int len = scm_ilength (list);
975 int i;
976
977 if (len < 0)
978 scm_wrong_type_arg_msg (NULL, 0, list, "proper list");
979
980 scm_dynwind_begin (0);
981
982 result = (char **) scm_malloc ((len + 1) * sizeof (char *));
983 result[len] = NULL;
984 scm_dynwind_unwind_handler (free, result, 0);
985
986 /* The list might be have been modified in another thread, so
987 we check LIST before each access.
988 */
989 for (i = 0; i < len && scm_is_pair (list); i++)
990 {
991 result[i] = scm_to_locale_string (SCM_CAR (list));
992 list = SCM_CDR (list);
993 }
994
995 scm_dynwind_end ();
996 return result;
997 }
998
999 void
1000 scm_i_free_string_pointers (char **pointers)
1001 {
1002 int i;
1003
1004 for (i = 0; pointers[i]; i++)
1005 free (pointers[i]);
1006 free (pointers);
1007 }
1008
1009 void
1010 scm_i_get_substring_spec (size_t len,
1011 SCM start, size_t *cstart,
1012 SCM end, size_t *cend)
1013 {
1014 if (SCM_UNBNDP (start))
1015 *cstart = 0;
1016 else
1017 *cstart = scm_to_unsigned_integer (start, 0, len);
1018
1019 if (SCM_UNBNDP (end))
1020 *cend = len;
1021 else
1022 *cend = scm_to_unsigned_integer (end, *cstart, len);
1023 }
1024
1025 #if SCM_ENABLE_DEPRECATED
1026
1027 /* When these definitions are removed, it becomes reasonable to use
1028 read-only strings for string literals. For that, change the reader
1029 to create string literals with scm_c_substring_read_only instead of
1030 with scm_c_substring_copy.
1031 */
1032
1033 int
1034 scm_i_deprecated_stringp (SCM str)
1035 {
1036 scm_c_issue_deprecation_warning
1037 ("SCM_STRINGP is deprecated. Use scm_is_string instead.");
1038
1039 return scm_is_string (str);
1040 }
1041
1042 char *
1043 scm_i_deprecated_string_chars (SCM str)
1044 {
1045 char *chars;
1046
1047 scm_c_issue_deprecation_warning
1048 ("SCM_STRING_CHARS is deprecated. See the manual for alternatives.");
1049
1050 /* We don't accept shared substrings here since they are not
1051 null-terminated.
1052 */
1053 if (IS_SH_STRING (str))
1054 scm_misc_error (NULL,
1055 "SCM_STRING_CHARS does not work with shared substrings.",
1056 SCM_EOL);
1057
1058 /* We explicitely test for read-only strings to produce a better
1059 error message.
1060 */
1061
1062 if (IS_RO_STRING (str))
1063 scm_misc_error (NULL,
1064 "SCM_STRING_CHARS does not work with read-only strings.",
1065 SCM_EOL);
1066
1067 /* The following is still wrong, of course...
1068 */
1069 chars = scm_i_string_writable_chars (str);
1070 scm_i_string_stop_writing ();
1071 return chars;
1072 }
1073
1074 size_t
1075 scm_i_deprecated_string_length (SCM str)
1076 {
1077 scm_c_issue_deprecation_warning
1078 ("SCM_STRING_LENGTH is deprecated. Use scm_c_string_length instead.");
1079 return scm_c_string_length (str);
1080 }
1081
1082 #endif
1083
1084 void
1085 scm_init_strings ()
1086 {
1087 scm_nullstr = scm_i_make_string (0, NULL);
1088
1089 #include "libguile/strings.x"
1090 }
1091
1092
1093 /*
1094 Local Variables:
1095 c-file-style: "gnu"
1096 End:
1097 */