Add Unicode strings and symbols
[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 License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * 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
16 * 02110-1301 USA
17 */
18
19
20 \f
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <string.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <unistr.h>
29
30 #include "libguile/_scm.h"
31 #include "libguile/chars.h"
32 #include "libguile/root.h"
33 #include "libguile/strings.h"
34 #include "libguile/deprecation.h"
35 #include "libguile/validate.h"
36 #include "libguile/dynwind.h"
37
38 \f
39
40 /* {Strings}
41 */
42
43
44 /* Stringbufs
45 *
46 * XXX - keeping an accurate refcount during GC seems to be quite
47 * tricky, so we just keep score of whether a stringbuf might be
48 * shared, not wether it definitely is.
49 *
50 * The scheme I (mvo) tried to keep an accurate reference count would
51 * recount all strings that point to a stringbuf during the mark-phase
52 * of the GC. This was done since one cannot access the stringbuf of
53 * a string when that string is freed (in order to decrease the
54 * reference count). The memory of the stringbuf might have been
55 * reused already for something completely different.
56 *
57 * This recounted worked for a small number of threads beating on
58 * cow-strings, but it failed randomly with more than 10 threads, say.
59 * I couldn't figure out what went wrong, so I used the conservative
60 * approach implemented below.
61 *
62 * A stringbuf needs to know its length, but only so that it can be
63 * reported when the stringbuf is freed.
64 *
65 * Stringbufs (and strings) are not stored very compactly: a stringbuf
66 * has room for about 2*sizeof(scm_t_bits)-1 bytes additional
67 * information. As a compensation, the code below is made more
68 * complicated by storing small strings inline in the double cell of a
69 * stringbuf. So we have fixstrings and bigstrings...
70 */
71
72 #define STRINGBUF_F_SHARED 0x100
73 #define STRINGBUF_F_INLINE 0x200
74 #define STRINGBUF_F_WIDE 0x400
75
76 #define STRINGBUF_TAG scm_tc7_stringbuf
77 #define STRINGBUF_SHARED(buf) (SCM_CELL_WORD_0(buf) & STRINGBUF_F_SHARED)
78 #define STRINGBUF_INLINE(buf) (SCM_CELL_WORD_0(buf) & STRINGBUF_F_INLINE)
79 #define STRINGBUF_WIDE(buf) (SCM_CELL_WORD_0(buf) & STRINGBUF_F_WIDE)
80
81 #define STRINGBUF_OUTLINE_CHARS(buf) ((char *)SCM_CELL_WORD_1(buf))
82 #define STRINGBUF_OUTLINE_LENGTH(buf) (SCM_CELL_WORD_2(buf))
83 #define STRINGBUF_INLINE_CHARS(buf) ((char *)SCM_CELL_OBJECT_LOC(buf,1))
84 #define STRINGBUF_INLINE_LENGTH(buf) (((size_t)SCM_CELL_WORD_0(buf))>>16)
85
86 #define STRINGBUF_CHARS(buf) (STRINGBUF_INLINE (buf) \
87 ? STRINGBUF_INLINE_CHARS (buf) \
88 : STRINGBUF_OUTLINE_CHARS (buf))
89 #define STRINGBUF_WIDE_CHARS(buf) ((scm_t_wchar *)SCM_CELL_WORD_1(buf))
90 #define STRINGBUF_LENGTH(buf) (STRINGBUF_INLINE (buf) \
91 ? STRINGBUF_INLINE_LENGTH (buf) \
92 : STRINGBUF_OUTLINE_LENGTH (buf))
93
94 #define STRINGBUF_MAX_INLINE_LEN (3*sizeof(scm_t_bits))
95
96 #define SET_STRINGBUF_SHARED(buf) \
97 (SCM_SET_CELL_WORD_0 ((buf), SCM_CELL_WORD_0 (buf) | STRINGBUF_F_SHARED))
98
99 #if SCM_DEBUG
100 static size_t lenhist[1001];
101 #endif
102
103 static SCM
104 make_stringbuf (size_t len)
105 {
106 /* XXX - for the benefit of SCM_STRING_CHARS, SCM_SYMBOL_CHARS and
107 scm_i_symbol_chars, all stringbufs are null-terminated. Once
108 SCM_STRING_CHARS and SCM_SYMBOL_CHARS are removed and the code
109 has been changed for scm_i_symbol_chars, this null-termination
110 can be dropped.
111 */
112
113 #if SCM_DEBUG
114 if (len < 1000)
115 lenhist[len]++;
116 else
117 lenhist[1000]++;
118 #endif
119
120 if (len <= STRINGBUF_MAX_INLINE_LEN-1)
121 {
122 return scm_double_cell (STRINGBUF_TAG | STRINGBUF_F_INLINE | (len << 16),
123 0, 0, 0);
124 }
125 else
126 {
127 char *mem = scm_gc_malloc (len+1, "string");
128 mem[len] = '\0';
129 return scm_double_cell (STRINGBUF_TAG, (scm_t_bits) mem,
130 (scm_t_bits) len, (scm_t_bits) 0);
131 }
132 }
133
134 static SCM
135 make_wide_stringbuf (size_t len)
136 {
137 scm_t_wchar *mem;
138 #if SCM_DEBUG
139 if (len < 1000)
140 lenhist[len]++;
141 else
142 lenhist[1000]++;
143 #endif
144
145 mem = scm_gc_malloc (sizeof (scm_t_wchar) * (len + 1), "string");
146 mem[len] = 0;
147 return scm_double_cell (STRINGBUF_TAG | STRINGBUF_F_WIDE, (scm_t_bits) mem,
148 (scm_t_bits) len, (scm_t_bits) 0);
149 }
150
151 /* Return a new stringbuf whose underlying storage consists of the LEN+1
152 octets pointed to by STR (the last octet is zero). */
153 SCM
154 scm_i_take_stringbufn (char *str, size_t len)
155 {
156 scm_gc_register_collectable_memory (str, len + 1, "stringbuf");
157
158 return scm_double_cell (STRINGBUF_TAG, (scm_t_bits) str,
159 (scm_t_bits) len, (scm_t_bits) 0);
160 }
161
162 SCM
163 scm_i_stringbuf_mark (SCM buf)
164 {
165 return SCM_BOOL_F;
166 }
167
168 void
169 scm_i_stringbuf_free (SCM buf)
170 {
171 if (!STRINGBUF_INLINE (buf))
172 {
173 if (!STRINGBUF_WIDE (buf))
174 scm_gc_free (STRINGBUF_OUTLINE_CHARS (buf),
175 STRINGBUF_OUTLINE_LENGTH (buf) + 1, "string");
176 else
177 scm_gc_free (STRINGBUF_OUTLINE_CHARS (buf),
178 sizeof (scm_t_wchar) * (STRINGBUF_OUTLINE_LENGTH (buf)
179 + 1), "string");
180 }
181
182 }
183
184 static void
185 widen_stringbuf (SCM buf)
186 {
187 size_t i, len;
188 scm_t_wchar *mem;
189
190 if (STRINGBUF_WIDE (buf))
191 return;
192
193 if (STRINGBUF_INLINE (buf))
194 {
195 len = STRINGBUF_INLINE_LENGTH (buf);
196
197 mem = scm_gc_malloc (sizeof (scm_t_wchar) * (len + 1), "string");
198 for (i = 0; i < len; i++)
199 mem[i] =
200 (scm_t_wchar) (unsigned char) STRINGBUF_INLINE_CHARS (buf)[i];
201 mem[len] = 0;
202
203 SCM_SET_CELL_WORD_0 (buf, SCM_CELL_WORD_0 (buf) ^ STRINGBUF_F_INLINE);
204 SCM_SET_CELL_WORD_0 (buf, SCM_CELL_WORD_0 (buf) | STRINGBUF_F_WIDE);
205 SCM_SET_CELL_WORD_1 (buf, mem);
206 SCM_SET_CELL_WORD_2 (buf, len);
207 }
208 else
209 {
210 len = STRINGBUF_OUTLINE_LENGTH (buf);
211
212 mem = scm_gc_malloc (sizeof (scm_t_wchar) * (len + 1), "string");
213 for (i = 0; i < len; i++)
214 mem[i] =
215 (scm_t_wchar) (unsigned char) STRINGBUF_OUTLINE_CHARS (buf)[i];
216 mem[len] = 0;
217
218 scm_gc_free (STRINGBUF_OUTLINE_CHARS (buf), len + 1, "string");
219
220 SCM_SET_CELL_WORD_0 (buf, SCM_CELL_WORD_0 (buf) | STRINGBUF_F_WIDE);
221 SCM_SET_CELL_WORD_1 (buf, mem);
222 SCM_SET_CELL_WORD_2 (buf, len);
223 }
224 }
225
226 scm_i_pthread_mutex_t stringbuf_write_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER;
227
228 /* Copy-on-write strings.
229 */
230
231 #define STRING_TAG scm_tc7_string
232
233 #define STRING_STRINGBUF(str) (SCM_CELL_OBJECT_1(str))
234 #define STRING_START(str) ((size_t)SCM_CELL_WORD_2(str))
235 #define STRING_LENGTH(str) ((size_t)SCM_CELL_WORD_3(str))
236
237 #define SET_STRING_STRINGBUF(str,buf) (SCM_SET_CELL_OBJECT_1(str,buf))
238 #define SET_STRING_START(str,start) (SCM_SET_CELL_WORD_2(str,start))
239
240 #define IS_STRING(str) (SCM_NIMP(str) && SCM_TYP7(str) == STRING_TAG)
241
242 /* Read-only strings.
243 */
244
245 #define RO_STRING_TAG (scm_tc7_string + 0x200)
246 #define IS_RO_STRING(str) (SCM_CELL_TYPE(str)==RO_STRING_TAG)
247
248 /* Mutation-sharing substrings
249 */
250
251 #define SH_STRING_TAG (scm_tc7_string + 0x100)
252
253 #define SH_STRING_STRING(sh) (SCM_CELL_OBJECT_1(sh))
254 /* START and LENGTH as for STRINGs. */
255
256 #define IS_SH_STRING(str) (SCM_CELL_TYPE(str)==SH_STRING_TAG)
257
258 SCM
259 scm_i_make_string (size_t len, char **charsp)
260 {
261 SCM buf = make_stringbuf (len);
262 SCM res;
263 if (charsp)
264 *charsp = STRINGBUF_CHARS (buf);
265 res = scm_double_cell (STRING_TAG, SCM_UNPACK(buf),
266 (scm_t_bits)0, (scm_t_bits) len);
267 return res;
268 }
269
270 SCM
271 scm_i_make_wide_string (size_t len, scm_t_wchar ** charsp)
272 {
273 SCM buf = make_wide_stringbuf (len);
274 SCM res;
275 if (charsp)
276 *charsp = STRINGBUF_WIDE_CHARS (buf);
277 res = scm_double_cell (STRING_TAG, SCM_UNPACK (buf),
278 (scm_t_bits) 0, (scm_t_bits) len);
279 return res;
280 }
281
282 static void
283 validate_substring_args (SCM str, size_t start, size_t end)
284 {
285 if (!IS_STRING (str))
286 scm_wrong_type_arg_msg (NULL, 0, str, "string");
287 if (start > STRING_LENGTH (str))
288 scm_out_of_range (NULL, scm_from_size_t (start));
289 if (end > STRING_LENGTH (str) || end < start)
290 scm_out_of_range (NULL, scm_from_size_t (end));
291 }
292
293 static inline void
294 get_str_buf_start (SCM *str, SCM *buf, size_t *start)
295 {
296 *start = STRING_START (*str);
297 if (IS_SH_STRING (*str))
298 {
299 *str = SH_STRING_STRING (*str);
300 *start += STRING_START (*str);
301 }
302 *buf = STRING_STRINGBUF (*str);
303 }
304
305 SCM
306 scm_i_substring (SCM str, size_t start, size_t end)
307 {
308 SCM buf;
309 size_t str_start;
310 get_str_buf_start (&str, &buf, &str_start);
311 scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
312 SET_STRINGBUF_SHARED (buf);
313 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
314 return scm_double_cell (STRING_TAG, SCM_UNPACK(buf),
315 (scm_t_bits)str_start + start,
316 (scm_t_bits) end - start);
317 }
318
319 SCM
320 scm_i_substring_read_only (SCM str, size_t start, size_t end)
321 {
322 SCM buf;
323 size_t str_start;
324 get_str_buf_start (&str, &buf, &str_start);
325 scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
326 SET_STRINGBUF_SHARED (buf);
327 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
328 return scm_double_cell (RO_STRING_TAG, SCM_UNPACK(buf),
329 (scm_t_bits)str_start + start,
330 (scm_t_bits) end - start);
331 }
332
333 SCM
334 scm_i_substring_copy (SCM str, size_t start, size_t end)
335 {
336 size_t len = end - start;
337 SCM buf, my_buf;
338 size_t str_start;
339 get_str_buf_start (&str, &buf, &str_start);
340 if (scm_i_is_narrow_string (str))
341 {
342 my_buf = make_stringbuf (len);
343 memcpy (STRINGBUF_CHARS (my_buf),
344 STRINGBUF_CHARS (buf) + str_start + start, len);
345 }
346 else
347 {
348 my_buf = make_wide_stringbuf (len);
349 u32_cpy ((scm_t_uint32 *) STRINGBUF_WIDE_CHARS (my_buf),
350 (scm_t_uint32 *) (STRINGBUF_WIDE_CHARS (buf) + str_start
351 + start), len);
352 /* Even though this string is wide, the substring may be narrow.
353 Consider adding code to narrow string. */
354 }
355 scm_remember_upto_here_1 (buf);
356 return scm_double_cell (STRING_TAG, SCM_UNPACK (my_buf),
357 (scm_t_bits) 0, (scm_t_bits) len);
358 }
359
360 SCM
361 scm_i_substring_shared (SCM str, size_t start, size_t end)
362 {
363 if (start == 0 && end == STRING_LENGTH (str))
364 return str;
365 else
366 {
367 size_t len = end - start;
368 if (IS_SH_STRING (str))
369 {
370 start += STRING_START (str);
371 str = SH_STRING_STRING (str);
372 }
373 return scm_double_cell (SH_STRING_TAG, SCM_UNPACK(str),
374 (scm_t_bits)start, (scm_t_bits) len);
375 }
376 }
377
378 SCM
379 scm_c_substring (SCM str, size_t start, size_t end)
380 {
381 validate_substring_args (str, start, end);
382 return scm_i_substring (str, start, end);
383 }
384
385 SCM
386 scm_c_substring_read_only (SCM str, size_t start, size_t end)
387 {
388 validate_substring_args (str, start, end);
389 return scm_i_substring_read_only (str, start, end);
390 }
391
392 SCM
393 scm_c_substring_copy (SCM str, size_t start, size_t end)
394 {
395 validate_substring_args (str, start, end);
396 return scm_i_substring_copy (str, start, end);
397 }
398
399 SCM
400 scm_c_substring_shared (SCM str, size_t start, size_t end)
401 {
402 validate_substring_args (str, start, end);
403 return scm_i_substring_shared (str, start, end);
404 }
405
406 SCM
407 scm_i_string_mark (SCM str)
408 {
409 if (IS_SH_STRING (str))
410 return SH_STRING_STRING (str);
411 else
412 return STRING_STRINGBUF (str);
413 }
414
415 void
416 scm_i_string_free (SCM str)
417 {
418 }
419
420 /* Internal accessors
421 */
422
423 size_t
424 scm_i_string_length (SCM str)
425 {
426 return STRING_LENGTH (str);
427 }
428
429 int
430 scm_i_is_narrow_string (SCM str)
431 {
432 return !STRINGBUF_WIDE (STRING_STRINGBUF (str));
433 }
434
435 const char *
436 scm_i_string_chars (SCM str)
437 {
438 SCM buf;
439 size_t start;
440 get_str_buf_start (&str, &buf, &start);
441 if (scm_i_is_narrow_string (str))
442 return STRINGBUF_CHARS (buf) + start;
443 else
444 scm_misc_error (NULL, "Invalid read access of chars of wide string: ~s",
445 scm_list_1 (str));
446 return NULL;
447 }
448
449 const scm_t_wchar *
450 scm_i_string_wide_chars (SCM str)
451 {
452 SCM buf;
453 size_t start;
454
455 get_str_buf_start (&str, &buf, &start);
456 if (!scm_i_is_narrow_string (str))
457 return STRINGBUF_WIDE_CHARS (buf) + start;
458 else
459 scm_misc_error (NULL, "Invalid read access of chars of narrow string: ~s",
460 scm_list_1 (str));
461 }
462
463 /* If the buffer in ORIG_STR is shared, copy ORIG_STR's characters to
464 a new string buffer, so that it can be modified without modifying
465 other strings. */
466 SCM
467 scm_i_string_start_writing (SCM orig_str)
468 {
469 SCM buf, str = orig_str;
470 size_t start;
471
472 get_str_buf_start (&str, &buf, &start);
473 if (IS_RO_STRING (str))
474 scm_misc_error (NULL, "string is read-only: ~s", scm_list_1 (orig_str));
475
476 scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
477 if (STRINGBUF_SHARED (buf))
478 {
479 /* Clone the stringbuf. */
480 size_t len = STRING_LENGTH (str);
481 SCM new_buf;
482
483 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
484
485 if (scm_i_is_narrow_string (str))
486 {
487 new_buf = make_stringbuf (len);
488 memcpy (STRINGBUF_CHARS (new_buf),
489 STRINGBUF_CHARS (buf) + STRING_START (str), len);
490
491 }
492 else
493 {
494 new_buf = make_wide_stringbuf (len);
495 u32_cpy ((scm_t_uint32 *) STRINGBUF_WIDE_CHARS (new_buf),
496 (scm_t_uint32 *) (STRINGBUF_WIDE_CHARS (buf)
497 + STRING_START (str)), len);
498 }
499 scm_i_thread_put_to_sleep ();
500 SET_STRING_STRINGBUF (str, new_buf);
501 start -= STRING_START (str);
502 SET_STRING_START (str, 0);
503 scm_i_thread_wake_up ();
504
505 buf = new_buf;
506
507 scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
508 }
509 return orig_str;
510 }
511
512 /* Return a pointer to the chars of a string that fits in a Latin-1
513 encoding. */
514 char *
515 scm_i_string_writable_chars (SCM str)
516 {
517 SCM buf;
518 size_t start;
519
520 get_str_buf_start (&str, &buf, &start);
521 if (scm_i_is_narrow_string (str))
522 return STRINGBUF_CHARS (buf) + start;
523 else
524 scm_misc_error (NULL, "Invalid write access of chars of wide string: ~s",
525 scm_list_1 (str));
526 return NULL;
527 }
528
529 /* Return a pointer to the Unicode codepoints of a string. */
530 static scm_t_wchar *
531 scm_i_string_writable_wide_chars (SCM str)
532 {
533 SCM buf;
534 size_t start;
535
536 get_str_buf_start (&str, &buf, &start);
537 if (!scm_i_is_narrow_string (str))
538 return STRINGBUF_WIDE_CHARS (buf) + start;
539 else
540 scm_misc_error (NULL, "Invalid read access of chars of narrow string: ~s",
541 scm_list_1 (str));
542 }
543
544 void
545 scm_i_string_stop_writing (void)
546 {
547 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
548 }
549
550 /* Return the Xth character is C. */
551 scm_t_wchar
552 scm_i_string_ref (SCM str, size_t x)
553 {
554 if (scm_i_is_narrow_string (str))
555 return (scm_t_wchar) (unsigned char) (scm_i_string_chars (str)[x]);
556 else
557 return scm_i_string_wide_chars (str)[x];
558 }
559
560 void
561 scm_i_string_set_x (SCM str, size_t p, scm_t_wchar chr)
562 {
563 if (chr > 0xFF && scm_i_is_narrow_string (str))
564 widen_stringbuf (STRING_STRINGBUF (str));
565
566 if (scm_i_is_narrow_string (str))
567 {
568 char *dst = scm_i_string_writable_chars (str);
569 dst[p] = (char) (unsigned char) chr;
570 }
571 else
572 {
573 scm_t_wchar *dst = scm_i_string_writable_wide_chars (str);
574 dst[p] = chr;
575 }
576 }
577
578 /* Symbols.
579
580 Basic symbol creation and accessing is done here, the rest is in
581 symbols.[hc]. This has been done to keep stringbufs and the
582 internals of strings and string-like objects confined to this file.
583 */
584
585 #define SYMBOL_STRINGBUF SCM_CELL_OBJECT_1
586
587 SCM
588 scm_i_make_symbol (SCM name, scm_t_bits flags,
589 unsigned long hash, SCM props)
590 {
591 SCM buf;
592 size_t start = STRING_START (name);
593 size_t length = STRING_LENGTH (name);
594
595 if (IS_SH_STRING (name))
596 {
597 name = SH_STRING_STRING (name);
598 start += STRING_START (name);
599 }
600 buf = SYMBOL_STRINGBUF (name);
601
602 if (start == 0 && length == STRINGBUF_LENGTH (buf))
603 {
604 /* reuse buf. */
605 scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
606 SET_STRINGBUF_SHARED (buf);
607 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
608 }
609 else
610 {
611 /* make new buf. */
612 if (scm_i_is_narrow_string (name))
613 {
614 SCM new_buf = make_stringbuf (length);
615 memcpy (STRINGBUF_CHARS (new_buf),
616 STRINGBUF_CHARS (buf) + start, length);
617 buf = new_buf;
618 }
619 else
620 {
621 SCM new_buf = make_wide_stringbuf (length);
622 u32_cpy ((scm_t_uint32 *) STRINGBUF_WIDE_CHARS (new_buf),
623 (scm_t_uint32 *) STRINGBUF_WIDE_CHARS (buf) + start,
624 length);
625 buf = new_buf;
626 }
627 }
628 return scm_double_cell (scm_tc7_symbol | flags, SCM_UNPACK (buf),
629 (scm_t_bits) hash, SCM_UNPACK (props));
630 }
631
632 SCM
633 scm_i_c_make_symbol (const char *name, size_t len,
634 scm_t_bits flags, unsigned long hash, SCM props)
635 {
636 SCM buf = make_stringbuf (len);
637 memcpy (STRINGBUF_CHARS (buf), name, len);
638
639 return scm_double_cell (scm_tc7_symbol | flags, SCM_UNPACK (buf),
640 (scm_t_bits) hash, SCM_UNPACK (props));
641 }
642
643 /* Return a new symbol that uses the LEN bytes pointed to by NAME as its
644 underlying storage. */
645 SCM
646 scm_i_c_take_symbol (char *name, size_t len,
647 scm_t_bits flags, unsigned long hash, SCM props)
648 {
649 SCM buf = scm_i_take_stringbufn (name, len);
650
651 return scm_double_cell (scm_tc7_symbol | flags, SCM_UNPACK (buf),
652 (scm_t_bits) hash, SCM_UNPACK (props));
653 }
654
655 size_t
656 scm_i_symbol_length (SCM sym)
657 {
658 return STRINGBUF_LENGTH (SYMBOL_STRINGBUF (sym));
659 }
660
661 size_t
662 scm_c_symbol_length (SCM sym)
663 #define FUNC_NAME "scm_c_symbol_length"
664 {
665 SCM_VALIDATE_SYMBOL (1, sym);
666
667 return STRINGBUF_LENGTH (SYMBOL_STRINGBUF (sym));
668 }
669 #undef FUNC_NAME
670
671 int
672 scm_i_is_narrow_symbol (SCM sym)
673 {
674 SCM buf;
675
676 buf = SYMBOL_STRINGBUF (sym);
677 return !STRINGBUF_WIDE (buf);
678 }
679
680 const char *
681 scm_i_symbol_chars (SCM sym)
682 {
683 SCM buf;
684
685 buf = SYMBOL_STRINGBUF (sym);
686 if (!STRINGBUF_WIDE (buf))
687 return STRINGBUF_CHARS (buf);
688 else
689 scm_misc_error (NULL, "Invalid access of chars of a wide symbol ~S",
690 scm_list_1 (sym));
691 }
692
693 /* Return a pointer to the Unicode codepoints of a symbol's name. */
694 const scm_t_wchar *
695 scm_i_symbol_wide_chars (SCM sym)
696 {
697 SCM buf;
698
699 buf = SYMBOL_STRINGBUF (sym);
700 if (STRINGBUF_WIDE (buf))
701 return STRINGBUF_WIDE_CHARS (buf);
702 else
703 scm_misc_error (NULL, "Invalid access of chars of a narrow symbol ~S",
704 scm_list_1 (sym));
705 }
706
707 SCM
708 scm_i_symbol_mark (SCM sym)
709 {
710 scm_gc_mark (SYMBOL_STRINGBUF (sym));
711 return SCM_CELL_OBJECT_3 (sym);
712 }
713
714 void
715 scm_i_symbol_free (SCM sym)
716 {
717 }
718
719 SCM
720 scm_i_symbol_substring (SCM sym, size_t start, size_t end)
721 {
722 SCM buf = SYMBOL_STRINGBUF (sym);
723 scm_i_pthread_mutex_lock (&stringbuf_write_mutex);
724 SET_STRINGBUF_SHARED (buf);
725 scm_i_pthread_mutex_unlock (&stringbuf_write_mutex);
726 return scm_double_cell (RO_STRING_TAG, SCM_UNPACK (buf),
727 (scm_t_bits)start, (scm_t_bits) end - start);
728 }
729
730 scm_t_wchar
731 scm_i_symbol_ref (SCM sym, size_t x)
732 {
733 if (scm_i_is_narrow_symbol (sym))
734 return (scm_t_wchar) (unsigned char) (scm_i_symbol_chars (sym)[x]);
735 else
736 return scm_i_symbol_wide_chars (sym)[x];
737 }
738
739 /* Debugging
740 */
741
742 #if SCM_DEBUG
743
744 SCM scm_sys_string_dump (SCM);
745 SCM scm_sys_symbol_dump (SCM);
746 SCM scm_sys_stringbuf_hist (void);
747
748 SCM_DEFINE (scm_sys_string_dump, "%string-dump", 1, 0, 0, (SCM str), "")
749 #define FUNC_NAME s_scm_sys_string_dump
750 {
751 SCM_VALIDATE_STRING (1, str);
752 fprintf (stderr, "%p:\n", str);
753 fprintf (stderr, " start: %u\n", STRING_START (str));
754 fprintf (stderr, " len: %u\n", STRING_LENGTH (str));
755 if (scm_i_is_narrow_string (str))
756 fprintf (stderr, " format: narrow\n");
757 else
758 fprintf (stderr, " format: wide\n");
759 if (IS_SH_STRING (str))
760 {
761 fprintf (stderr, " string: %p\n", SH_STRING_STRING (str));
762 fprintf (stderr, "\n");
763 scm_sys_string_dump (SH_STRING_STRING (str));
764 }
765 else
766 {
767 SCM buf = STRING_STRINGBUF (str);
768 fprintf (stderr, " buf: %p\n", buf);
769 if (scm_i_is_narrow_string (str))
770 fprintf (stderr, " chars: %p\n", STRINGBUF_CHARS (buf));
771 else
772 fprintf (stderr, " chars: %p\n", STRINGBUF_WIDE_CHARS (buf));
773 fprintf (stderr, " length: %u\n", STRINGBUF_LENGTH (buf));
774 if (STRINGBUF_SHARED (buf))
775 fprintf (stderr, " shared: true\n");
776 else
777 fprintf (stderr, " shared: false\n");
778 if (STRINGBUF_INLINE (buf))
779 fprintf (stderr, " inline: true\n");
780 else
781 fprintf (stderr, " inline: false\n");
782
783 }
784 return SCM_UNSPECIFIED;
785 }
786 #undef FUNC_NAME
787
788 SCM_DEFINE (scm_sys_symbol_dump, "%symbol-dump", 1, 0, 0, (SCM sym), "")
789 #define FUNC_NAME s_scm_sys_symbol_dump
790 {
791 SCM_VALIDATE_SYMBOL (1, sym);
792 fprintf (stderr, "%p:\n", sym);
793 fprintf (stderr, " hash: %lu\n", scm_i_symbol_hash (sym));
794 if (scm_i_is_narrow_symbol (sym))
795 fprintf (stderr, " format: narrow\n");
796 else
797 fprintf (stderr, " format: wide\n");
798 {
799 SCM buf = SYMBOL_STRINGBUF (sym);
800 fprintf (stderr, " buf: %p\n", buf);
801 if (scm_i_is_narrow_symbol (sym))
802 fprintf (stderr, " chars: %p\n", STRINGBUF_CHARS (buf));
803 else
804 fprintf (stderr, " chars: %p\n", STRINGBUF_WIDE_CHARS (buf));
805 fprintf (stderr, " length: %u\n", STRINGBUF_LENGTH (buf));
806 if (STRINGBUF_SHARED (buf))
807 fprintf (stderr, " shared: true\n");
808 else
809 fprintf (stderr, " shared: false\n");
810
811 }
812 return SCM_UNSPECIFIED;
813 }
814 #undef FUNC_NAME
815
816 SCM_DEFINE (scm_sys_stringbuf_hist, "%stringbuf-hist", 0, 0, 0, (void), "")
817 #define FUNC_NAME s_scm_sys_stringbuf_hist
818 {
819 int i;
820 for (i = 0; i < 1000; i++)
821 if (lenhist[i])
822 fprintf (stderr, " %3d: %u\n", i, lenhist[i]);
823 fprintf (stderr, ">999: %u\n", lenhist[1000]);
824 return SCM_UNSPECIFIED;
825 }
826 #undef FUNC_NAME
827
828 #endif
829
830 \f
831
832 SCM_DEFINE (scm_string_p, "string?", 1, 0, 0,
833 (SCM obj),
834 "Return @code{#t} if @var{obj} is a string, else @code{#f}.")
835 #define FUNC_NAME s_scm_string_p
836 {
837 return scm_from_bool (IS_STRING (obj));
838 }
839 #undef FUNC_NAME
840
841
842 SCM_REGISTER_PROC (s_scm_list_to_string, "list->string", 1, 0, 0, scm_string);
843
844 SCM_DEFINE (scm_string, "string", 0, 0, 1,
845 (SCM chrs),
846 "@deffnx {Scheme Procedure} list->string chrs\n"
847 "Return a newly allocated string composed of the arguments,\n"
848 "@var{chrs}.")
849 #define FUNC_NAME s_scm_string
850 {
851 SCM result;
852 SCM rest;
853 size_t len;
854 size_t p = 0;
855 long i;
856
857 /* Verify that this is a list of chars. */
858 i = scm_ilength (chrs);
859 len = (size_t) i;
860 rest = chrs;
861
862 SCM_ASSERT (len >= 0, chrs, SCM_ARG1, FUNC_NAME);
863 while (len > 0 && scm_is_pair (rest))
864 {
865 SCM elt = SCM_CAR (rest);
866 SCM_VALIDATE_CHAR (SCM_ARGn, elt);
867 rest = SCM_CDR (rest);
868 len--;
869 scm_remember_upto_here_1 (elt);
870 }
871
872 /* Construct a string containing this list of chars. */
873 len = (size_t) i;
874 rest = chrs;
875
876 result = scm_i_make_string (len, NULL);
877 result = scm_i_string_start_writing (result);
878 while (len > 0 && scm_is_pair (rest))
879 {
880 SCM elt = SCM_CAR (rest);
881 scm_i_string_set_x (result, p, SCM_CHAR (elt));
882 p++;
883 rest = SCM_CDR (rest);
884 len--;
885 scm_remember_upto_here_1 (elt);
886 }
887 scm_i_string_stop_writing ();
888
889 if (len > 0)
890 scm_misc_error (NULL, "list changed while constructing string", SCM_EOL);
891 if (!scm_is_null (rest))
892 scm_wrong_type_arg_msg (NULL, 0, chrs, "proper list");
893
894 return result;
895 }
896 #undef FUNC_NAME
897
898 SCM_DEFINE (scm_make_string, "make-string", 1, 1, 0,
899 (SCM k, SCM chr),
900 "Return a newly allocated string of\n"
901 "length @var{k}. If @var{chr} is given, then all elements of\n"
902 "the string are initialized to @var{chr}, otherwise the contents\n"
903 "of the @var{string} are unspecified.")
904 #define FUNC_NAME s_scm_make_string
905 {
906 return scm_c_make_string (scm_to_size_t (k), chr);
907 }
908 #undef FUNC_NAME
909
910 SCM
911 scm_c_make_string (size_t len, SCM chr)
912 #define FUNC_NAME NULL
913 {
914 size_t p;
915 SCM res = scm_i_make_string (len, NULL);
916
917 if (!SCM_UNBNDP (chr))
918 {
919 SCM_VALIDATE_CHAR (0, chr);
920 res = scm_i_string_start_writing (res);
921 for (p = 0; p < len; p++)
922 scm_i_string_set_x (res, p, SCM_CHAR (chr));
923 scm_i_string_stop_writing ();
924 }
925
926 return res;
927 }
928 #undef FUNC_NAME
929
930 SCM_DEFINE (scm_string_length, "string-length", 1, 0, 0,
931 (SCM string),
932 "Return the number of characters in @var{string}.")
933 #define FUNC_NAME s_scm_string_length
934 {
935 SCM_VALIDATE_STRING (1, string);
936 return scm_from_size_t (STRING_LENGTH (string));
937 }
938 #undef FUNC_NAME
939
940 SCM_DEFINE (scm_string_width, "string-width", 1, 0, 0,
941 (SCM string),
942 "Return the bytes used to represent a character in @var{string}."
943 "This will return 1 or 4.")
944 #define FUNC_NAME s_scm_string_width
945 {
946 SCM_VALIDATE_STRING (1, string);
947 if (!scm_i_is_narrow_string (string))
948 return scm_from_int (4);
949
950 return scm_from_int (1);
951 }
952 #undef FUNC_NAME
953
954 size_t
955 scm_c_string_length (SCM string)
956 {
957 if (!IS_STRING (string))
958 scm_wrong_type_arg_msg (NULL, 0, string, "string");
959 return STRING_LENGTH (string);
960 }
961
962 SCM_DEFINE (scm_string_ref, "string-ref", 2, 0, 0,
963 (SCM str, SCM k),
964 "Return character @var{k} of @var{str} using zero-origin\n"
965 "indexing. @var{k} must be a valid index of @var{str}.")
966 #define FUNC_NAME s_scm_string_ref
967 {
968 size_t len;
969 unsigned long idx;
970
971 SCM_VALIDATE_STRING (1, str);
972
973 len = scm_i_string_length (str);
974 if (SCM_LIKELY (len > 0))
975 idx = scm_to_unsigned_integer (k, 0, len - 1);
976 else
977 scm_out_of_range (NULL, k);
978
979 if (scm_i_is_narrow_string (str))
980 return SCM_MAKE_CHAR (scm_i_string_chars (str)[idx]);
981 else
982 return SCM_MAKE_CHAR (scm_i_string_wide_chars (str)[idx]);
983 }
984 #undef FUNC_NAME
985
986 SCM
987 scm_c_string_ref (SCM str, size_t p)
988 {
989 if (p >= scm_i_string_length (str))
990 scm_out_of_range (NULL, scm_from_size_t (p));
991 if (scm_i_is_narrow_string (str))
992 return SCM_MAKE_CHAR (scm_i_string_chars (str)[p]);
993 else
994 return SCM_MAKE_CHAR (scm_i_string_wide_chars (str)[p]);
995
996 }
997
998 SCM_DEFINE (scm_string_set_x, "string-set!", 3, 0, 0,
999 (SCM str, SCM k, SCM chr),
1000 "Store @var{chr} in element @var{k} of @var{str} and return\n"
1001 "an unspecified value. @var{k} must be a valid index of\n"
1002 "@var{str}.")
1003 #define FUNC_NAME s_scm_string_set_x
1004 {
1005 size_t len;
1006 unsigned long idx;
1007
1008 SCM_VALIDATE_STRING (1, str);
1009
1010 len = scm_i_string_length (str);
1011 if (SCM_LIKELY (len > 0))
1012 idx = scm_to_unsigned_integer (k, 0, len - 1);
1013 else
1014 scm_out_of_range (NULL, k);
1015
1016 SCM_VALIDATE_CHAR (3, chr);
1017 str = scm_i_string_start_writing (str);
1018 scm_i_string_set_x (str, idx, SCM_CHAR (chr));
1019 scm_i_string_stop_writing ();
1020
1021 return SCM_UNSPECIFIED;
1022 }
1023 #undef FUNC_NAME
1024
1025 void
1026 scm_c_string_set_x (SCM str, size_t p, SCM chr)
1027 {
1028 if (p >= scm_i_string_length (str))
1029 scm_out_of_range (NULL, scm_from_size_t (p));
1030 str = scm_i_string_start_writing (str);
1031 scm_i_string_set_x (str, p, SCM_CHAR (chr));
1032 scm_i_string_stop_writing ();
1033 }
1034
1035 SCM_DEFINE (scm_substring, "substring", 2, 1, 0,
1036 (SCM str, SCM start, SCM end),
1037 "Return a newly allocated string formed from the characters\n"
1038 "of @var{str} beginning with index @var{start} (inclusive) and\n"
1039 "ending with index @var{end} (exclusive).\n"
1040 "@var{str} must be a string, @var{start} and @var{end} must be\n"
1041 "exact integers satisfying:\n\n"
1042 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
1043 #define FUNC_NAME s_scm_substring
1044 {
1045 size_t len, from, to;
1046
1047 SCM_VALIDATE_STRING (1, str);
1048 len = scm_i_string_length (str);
1049 from = scm_to_unsigned_integer (start, 0, len);
1050 if (SCM_UNBNDP (end))
1051 to = len;
1052 else
1053 to = scm_to_unsigned_integer (end, from, len);
1054 return scm_i_substring (str, from, to);
1055 }
1056 #undef FUNC_NAME
1057
1058 SCM_DEFINE (scm_substring_read_only, "substring/read-only", 2, 1, 0,
1059 (SCM str, SCM start, SCM end),
1060 "Return a newly allocated string formed from the characters\n"
1061 "of @var{str} beginning with index @var{start} (inclusive) and\n"
1062 "ending with index @var{end} (exclusive).\n"
1063 "@var{str} must be a string, @var{start} and @var{end} must be\n"
1064 "exact integers satisfying:\n"
1065 "\n"
1066 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).\n"
1067 "\n"
1068 "The returned string is read-only.\n")
1069 #define FUNC_NAME s_scm_substring_read_only
1070 {
1071 size_t len, from, to;
1072
1073 SCM_VALIDATE_STRING (1, str);
1074 len = scm_i_string_length (str);
1075 from = scm_to_unsigned_integer (start, 0, len);
1076 if (SCM_UNBNDP (end))
1077 to = len;
1078 else
1079 to = scm_to_unsigned_integer (end, from, len);
1080 return scm_i_substring_read_only (str, from, to);
1081 }
1082 #undef FUNC_NAME
1083
1084 SCM_DEFINE (scm_substring_copy, "substring/copy", 2, 1, 0,
1085 (SCM str, SCM start, SCM end),
1086 "Return a newly allocated string formed from the characters\n"
1087 "of @var{str} beginning with index @var{start} (inclusive) and\n"
1088 "ending with index @var{end} (exclusive).\n"
1089 "@var{str} must be a string, @var{start} and @var{end} must be\n"
1090 "exact integers satisfying:\n\n"
1091 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
1092 #define FUNC_NAME s_scm_substring_copy
1093 {
1094 /* For the Scheme version, START is mandatory, but for the C
1095 version, it is optional. See scm_string_copy in srfi-13.c for a
1096 rationale.
1097 */
1098
1099 size_t from, to;
1100
1101 SCM_VALIDATE_STRING (1, str);
1102 scm_i_get_substring_spec (scm_i_string_length (str),
1103 start, &from, end, &to);
1104 return scm_i_substring_copy (str, from, to);
1105 }
1106 #undef FUNC_NAME
1107
1108 SCM_DEFINE (scm_substring_shared, "substring/shared", 2, 1, 0,
1109 (SCM str, SCM start, SCM end),
1110 "Return string that indirectly refers to the characters\n"
1111 "of @var{str} beginning with index @var{start} (inclusive) and\n"
1112 "ending with index @var{end} (exclusive).\n"
1113 "@var{str} must be a string, @var{start} and @var{end} must be\n"
1114 "exact integers satisfying:\n\n"
1115 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
1116 #define FUNC_NAME s_scm_substring_shared
1117 {
1118 size_t len, from, to;
1119
1120 SCM_VALIDATE_STRING (1, str);
1121 len = scm_i_string_length (str);
1122 from = scm_to_unsigned_integer (start, 0, len);
1123 if (SCM_UNBNDP (end))
1124 to = len;
1125 else
1126 to = scm_to_unsigned_integer (end, from, len);
1127 return scm_i_substring_shared (str, from, to);
1128 }
1129 #undef FUNC_NAME
1130
1131 SCM_DEFINE (scm_string_append, "string-append", 0, 0, 1,
1132 (SCM args),
1133 "Return a newly allocated string whose characters form the\n"
1134 "concatenation of the given strings, @var{args}.")
1135 #define FUNC_NAME s_scm_string_append
1136 {
1137 SCM res;
1138 size_t len = 0;
1139 int wide = 0;
1140 SCM l, s;
1141 char *data;
1142 scm_t_wchar *wdata;
1143 int i;
1144
1145 SCM_VALIDATE_REST_ARGUMENT (args);
1146 for (l = args; !scm_is_null (l); l = SCM_CDR (l))
1147 {
1148 s = SCM_CAR (l);
1149 SCM_VALIDATE_STRING (SCM_ARGn, s);
1150 len += scm_i_string_length (s);
1151 if (!scm_i_is_narrow_string (s))
1152 wide = 1;
1153 }
1154 if (!wide)
1155 res = scm_i_make_string (len, &data);
1156 else
1157 res = scm_i_make_wide_string (len, &wdata);
1158
1159 for (l = args; !scm_is_null (l); l = SCM_CDR (l))
1160 {
1161 size_t len;
1162 s = SCM_CAR (l);
1163 SCM_VALIDATE_STRING (SCM_ARGn, s);
1164 len = scm_i_string_length (s);
1165 if (!wide)
1166 {
1167 memcpy (data, scm_i_string_chars (s), len);
1168 data += len;
1169 }
1170 else
1171 {
1172 if (scm_i_is_narrow_string (s))
1173 {
1174 for (i = 0; i < scm_i_string_length (s); i++)
1175 wdata[i] = (unsigned char) scm_i_string_chars (s)[i];
1176 }
1177 else
1178 u32_cpy ((scm_t_uint32 *) wdata,
1179 (scm_t_uint32 *) scm_i_string_wide_chars (s), len);
1180 wdata += len;
1181 }
1182 scm_remember_upto_here_1 (s);
1183 }
1184 return res;
1185 }
1186 #undef FUNC_NAME
1187
1188 int
1189 scm_is_string (SCM obj)
1190 {
1191 return IS_STRING (obj);
1192 }
1193
1194 SCM
1195 scm_from_locale_stringn (const char *str, size_t len)
1196 {
1197 SCM res;
1198 char *dst;
1199
1200 if (len == (size_t) -1)
1201 len = strlen (str);
1202 if (len == 0)
1203 return scm_nullstr;
1204
1205 res = scm_i_make_string (len, &dst);
1206 memcpy (dst, str, len);
1207 return res;
1208 }
1209
1210 SCM
1211 scm_from_locale_string (const char *str)
1212 {
1213 if (str == NULL)
1214 return scm_nullstr;
1215
1216 return scm_from_locale_stringn (str, -1);
1217 }
1218
1219 SCM
1220 scm_take_locale_stringn (char *str, size_t len)
1221 {
1222 SCM buf, res;
1223
1224 if (len == (size_t) -1)
1225 len = strlen (str);
1226 else
1227 {
1228 /* Ensure STR is null terminated. A realloc for 1 extra byte should
1229 often be satisfied from the alignment padding after the block, with
1230 no actual data movement. */
1231 str = scm_realloc (str, len + 1);
1232 str[len] = '\0';
1233 }
1234
1235 buf = scm_i_take_stringbufn (str, len);
1236 res = scm_double_cell (STRING_TAG,
1237 SCM_UNPACK (buf), (scm_t_bits) 0, (scm_t_bits) len);
1238 return res;
1239 }
1240
1241 SCM
1242 scm_take_locale_string (char *str)
1243 {
1244 return scm_take_locale_stringn (str, -1);
1245 }
1246
1247 /* Change libunistring escapes (\uXXXX and \UXXXXXXXX) to \xXX \uXXXX
1248 and \UXXXXXX. */
1249 static void
1250 unistring_escapes_to_guile_escapes (char **bufp, size_t *lenp)
1251 {
1252 char *before, *after;
1253 size_t i, j;
1254
1255 before = *bufp;
1256 after = *bufp;
1257 i = 0;
1258 j = 0;
1259 while (i < *lenp)
1260 {
1261 if ((i <= *lenp - 6)
1262 && before[i] == '\\'
1263 && before[i + 1] == 'u'
1264 && before[i + 2] == '0' && before[i + 3] == '0')
1265 {
1266 /* Convert \u00NN to \xNN */
1267 after[j] = '\\';
1268 after[j + 1] = 'x';
1269 after[j + 2] = tolower (before[i + 4]);
1270 after[j + 3] = tolower (before[i + 5]);
1271 i += 6;
1272 j += 4;
1273 }
1274 else if ((i <= *lenp - 10)
1275 && before[i] == '\\'
1276 && before[i + 1] == 'U'
1277 && before[i + 2] == '0' && before[i + 3] == '0')
1278 {
1279 /* Convert \U00NNNNNN to \UNNNNNN */
1280 after[j] = '\\';
1281 after[j + 1] = 'U';
1282 after[j + 2] = tolower (before[i + 4]);
1283 after[j + 3] = tolower (before[i + 5]);
1284 after[j + 4] = tolower (before[i + 6]);
1285 after[j + 5] = tolower (before[i + 7]);
1286 after[j + 6] = tolower (before[i + 8]);
1287 after[j + 7] = tolower (before[i + 9]);
1288 i += 10;
1289 j += 8;
1290 }
1291 else
1292 {
1293 after[j] = before[i];
1294 i++;
1295 j++;
1296 }
1297 }
1298 *lenp = j;
1299 after = scm_realloc (after, j);
1300 }
1301
1302 char *
1303 scm_to_locale_stringn (SCM str, size_t * lenp)
1304 {
1305 const char *enc;
1306
1307 /* In the future, enc will hold the port's encoding. */
1308 enc = NULL;
1309
1310 return scm_to_stringn (str, lenp, enc, iconveh_escape_sequence);
1311 }
1312
1313 /* Low-level scheme to C string conversion function. */
1314 char *
1315 scm_to_stringn (SCM str, size_t * lenp, const char *encoding,
1316 enum iconv_ilseq_handler handler)
1317 {
1318 static const char iso[11] = "ISO-8859-1";
1319 char *buf;
1320 size_t ilen, len, i;
1321
1322 if (!scm_is_string (str))
1323 scm_wrong_type_arg_msg (NULL, 0, str, "string");
1324 ilen = scm_i_string_length (str);
1325
1326 if (ilen == 0)
1327 {
1328 buf = scm_malloc (1);
1329 buf[0] = '\0';
1330 if (lenp)
1331 *lenp = 0;
1332 return buf;
1333 }
1334
1335 if (lenp == NULL)
1336 for (i = 0; i < ilen; i++)
1337 if (scm_i_string_ref (str, i) == '\0')
1338 scm_misc_error (NULL,
1339 "string contains #\\nul character: ~S",
1340 scm_list_1 (str));
1341
1342 if (scm_i_is_narrow_string (str))
1343 {
1344 if (lenp)
1345 {
1346 buf = scm_malloc (ilen);
1347 memcpy (buf, scm_i_string_chars (str), ilen);
1348 *lenp = ilen;
1349 return buf;
1350 }
1351 else
1352 {
1353 buf = scm_malloc (ilen + 1);
1354 memcpy (buf, scm_i_string_chars (str), ilen);
1355 buf[ilen] = '\0';
1356 return buf;
1357 }
1358 }
1359
1360
1361 buf = NULL;
1362 len = 0;
1363 buf = u32_conv_to_encoding (iso,
1364 handler,
1365 (scm_t_uint32 *) scm_i_string_wide_chars (str),
1366 ilen, NULL, NULL, &len);
1367 if (buf == NULL)
1368 scm_misc_error (NULL, "cannot convert to output locale ~s: \"~s\"",
1369 scm_list_2 (scm_from_locale_string (iso), str));
1370
1371 if (handler == iconveh_escape_sequence)
1372 unistring_escapes_to_guile_escapes (&buf, &len);
1373
1374 if (lenp)
1375 *lenp = len;
1376 else
1377 {
1378 buf = scm_realloc (buf, len + 1);
1379 buf[len] = '\0';
1380 }
1381
1382 scm_remember_upto_here_1 (str);
1383 return buf;
1384 }
1385
1386 char *
1387 scm_to_locale_string (SCM str)
1388 {
1389 return scm_to_locale_stringn (str, NULL);
1390 }
1391
1392 size_t
1393 scm_to_locale_stringbuf (SCM str, char *buf, size_t max_len)
1394 {
1395 size_t len;
1396 char *result = NULL;
1397 if (!scm_is_string (str))
1398 scm_wrong_type_arg_msg (NULL, 0, str, "string");
1399 result = scm_to_locale_stringn (str, &len);
1400
1401 memcpy (buf, result, (len > max_len) ? max_len : len);
1402 free (result);
1403
1404 scm_remember_upto_here_1 (str);
1405 return len;
1406 }
1407
1408 /* converts C scm_array of strings to SCM scm_list of strings. */
1409 /* If argc < 0, a null terminated scm_array is assumed. */
1410 SCM
1411 scm_makfromstrs (int argc, char **argv)
1412 {
1413 int i = argc;
1414 SCM lst = SCM_EOL;
1415 if (0 > i)
1416 for (i = 0; argv[i]; i++);
1417 while (i--)
1418 lst = scm_cons (scm_from_locale_string (argv[i]), lst);
1419 return lst;
1420 }
1421
1422 /* Return a newly allocated array of char pointers to each of the strings
1423 in args, with a terminating NULL pointer. */
1424
1425 char **
1426 scm_i_allocate_string_pointers (SCM list)
1427 {
1428 char **result;
1429 int len = scm_ilength (list);
1430 int i;
1431
1432 if (len < 0)
1433 scm_wrong_type_arg_msg (NULL, 0, list, "proper list");
1434
1435 scm_dynwind_begin (0);
1436
1437 result = (char **) scm_malloc ((len + 1) * sizeof (char *));
1438 result[len] = NULL;
1439 scm_dynwind_unwind_handler (free, result, 0);
1440
1441 /* The list might be have been modified in another thread, so
1442 we check LIST before each access.
1443 */
1444 for (i = 0; i < len && scm_is_pair (list); i++)
1445 {
1446 result[i] = scm_to_locale_string (SCM_CAR (list));
1447 list = SCM_CDR (list);
1448 }
1449
1450 scm_dynwind_end ();
1451 return result;
1452 }
1453
1454 void
1455 scm_i_free_string_pointers (char **pointers)
1456 {
1457 int i;
1458
1459 for (i = 0; pointers[i]; i++)
1460 free (pointers[i]);
1461 free (pointers);
1462 }
1463
1464 void
1465 scm_i_get_substring_spec (size_t len,
1466 SCM start, size_t *cstart,
1467 SCM end, size_t *cend)
1468 {
1469 if (SCM_UNBNDP (start))
1470 *cstart = 0;
1471 else
1472 *cstart = scm_to_unsigned_integer (start, 0, len);
1473
1474 if (SCM_UNBNDP (end))
1475 *cend = len;
1476 else
1477 *cend = scm_to_unsigned_integer (end, *cstart, len);
1478 }
1479
1480 #if SCM_ENABLE_DEPRECATED
1481
1482 /* When these definitions are removed, it becomes reasonable to use
1483 read-only strings for string literals. For that, change the reader
1484 to create string literals with scm_c_substring_read_only instead of
1485 with scm_c_substring_copy.
1486 */
1487
1488 int
1489 scm_i_deprecated_stringp (SCM str)
1490 {
1491 scm_c_issue_deprecation_warning
1492 ("SCM_STRINGP is deprecated. Use scm_is_string instead.");
1493
1494 return scm_is_string (str);
1495 }
1496
1497 char *
1498 scm_i_deprecated_string_chars (SCM str)
1499 {
1500 char *chars;
1501
1502 scm_c_issue_deprecation_warning
1503 ("SCM_STRING_CHARS is deprecated. See the manual for alternatives.");
1504
1505 /* We don't accept shared substrings here since they are not
1506 null-terminated.
1507 */
1508 if (IS_SH_STRING (str))
1509 scm_misc_error (NULL,
1510 "SCM_STRING_CHARS does not work with shared substrings.",
1511 SCM_EOL);
1512
1513 /* We explicitly test for read-only strings to produce a better
1514 error message.
1515 */
1516
1517 if (IS_RO_STRING (str))
1518 scm_misc_error (NULL,
1519 "SCM_STRING_CHARS does not work with read-only strings.",
1520 SCM_EOL);
1521
1522 /* The following is still wrong, of course...
1523 */
1524 str = scm_i_string_start_writing (str);
1525 chars = scm_i_string_writable_chars (str);
1526 scm_i_string_stop_writing ();
1527 return chars;
1528 }
1529
1530 size_t
1531 scm_i_deprecated_string_length (SCM str)
1532 {
1533 scm_c_issue_deprecation_warning
1534 ("SCM_STRING_LENGTH is deprecated. Use scm_c_string_length instead.");
1535 return scm_c_string_length (str);
1536 }
1537
1538 #endif
1539
1540 void
1541 scm_init_strings ()
1542 {
1543 scm_nullstr = scm_i_make_string (0, NULL);
1544
1545 #include "libguile/strings.x"
1546 }
1547
1548
1549 /*
1550 Local Variables:
1551 c-file-style: "gnu"
1552 End:
1553 */