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