Commit | Line | Data |
---|---|---|
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 | |
94 | static size_t lenhist[1001]; | |
95 | #endif | |
96 | ||
97 | static SCM | |
98 | make_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 | 130 | SCM |
fd0a5bbc HWN |
131 | scm_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 |
139 | SCM |
140 | scm_i_stringbuf_mark (SCM buf) | |
141 | { | |
142 | return SCM_BOOL_F; | |
143 | } | |
1bbd0b84 | 144 | |
3ee86942 MV |
145 | void |
146 | scm_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 | 153 | scm_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 |
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; | |
0f2d19dd JB |
195 | } |
196 | ||
3ee86942 MV |
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 | } | |
0f2d19dd | 207 | |
e1b29f6a MV |
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 | ||
3ee86942 MV |
220 | SCM |
221 | scm_i_substring (SCM str, size_t start, size_t end) | |
0f2d19dd | 222 | { |
e1b29f6a MV |
223 | SCM buf; |
224 | size_t str_start; | |
225 | get_str_buf_start (&str, &buf, &str_start); | |
9de87eea | 226 | scm_i_pthread_mutex_lock (&stringbuf_write_mutex); |
3ee86942 | 227 | SET_STRINGBUF_SHARED (buf); |
9de87eea | 228 | scm_i_pthread_mutex_unlock (&stringbuf_write_mutex); |
3ee86942 | 229 | return scm_double_cell (STRING_TAG, SCM_UNPACK(buf), |
e1b29f6a MV |
230 | (scm_t_bits)str_start + start, |
231 | (scm_t_bits) end - start); | |
0f2d19dd JB |
232 | } |
233 | ||
ed35de72 MV |
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); | |
9de87eea | 240 | scm_i_pthread_mutex_lock (&stringbuf_write_mutex); |
ed35de72 | 241 | SET_STRINGBUF_SHARED (buf); |
9de87eea | 242 | scm_i_pthread_mutex_unlock (&stringbuf_write_mutex); |
ed35de72 MV |
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 | ||
3ee86942 MV |
248 | SCM |
249 | scm_i_substring_copy (SCM str, size_t start, size_t end) | |
250 | { | |
251 | size_t len = end - start; | |
edea856c | 252 | SCM buf, my_buf; |
e1b29f6a MV |
253 | size_t str_start; |
254 | get_str_buf_start (&str, &buf, &str_start); | |
edea856c | 255 | my_buf = make_stringbuf (len); |
e1b29f6a MV |
256 | memcpy (STRINGBUF_CHARS (my_buf), |
257 | STRINGBUF_CHARS (buf) + str_start + start, len); | |
3ee86942 MV |
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 | } | |
0f2d19dd | 262 | |
e1b29f6a MV |
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 | ||
3ee86942 MV |
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 | } | |
ee149d03 | 287 | |
ed35de72 MV |
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 | ||
0f2d19dd | 295 | SCM |
3ee86942 | 296 | scm_c_substring_copy (SCM str, size_t start, size_t end) |
0f2d19dd | 297 | { |
3ee86942 MV |
298 | validate_substring_args (str, start, end); |
299 | return scm_i_substring_copy (str, start, end); | |
300 | } | |
301 | ||
3ee86942 MV |
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 | } | |
0f2d19dd | 308 | |
ee149d03 | 309 | SCM |
3ee86942 | 310 | scm_i_string_mark (SCM str) |
ee149d03 | 311 | { |
3ee86942 MV |
312 | if (IS_SH_STRING (str)) |
313 | return SH_STRING_STRING (str); | |
314 | else | |
315 | return STRING_STRINGBUF (str); | |
ee149d03 JB |
316 | } |
317 | ||
3ee86942 MV |
318 | void |
319 | scm_i_string_free (SCM str) | |
320 | { | |
321 | } | |
36284627 | 322 | |
3ee86942 MV |
323 | /* Internal accessors |
324 | */ | |
325 | ||
326 | size_t | |
327 | scm_i_string_length (SCM str) | |
0f2d19dd | 328 | { |
3ee86942 | 329 | return STRING_LENGTH (str); |
0f2d19dd JB |
330 | } |
331 | ||
3ee86942 MV |
332 | const char * |
333 | scm_i_string_chars (SCM str) | |
334 | { | |
335 | SCM buf; | |
e1b29f6a MV |
336 | size_t start; |
337 | get_str_buf_start (&str, &buf, &start); | |
3ee86942 MV |
338 | return STRINGBUF_CHARS (buf) + start; |
339 | } | |
b00418df | 340 | |
3ee86942 | 341 | char * |
ed35de72 | 342 | scm_i_string_writable_chars (SCM orig_str) |
b00418df | 343 | { |
ed35de72 | 344 | SCM buf, str = orig_str; |
e1b29f6a | 345 | size_t start; |
ed35de72 | 346 | |
e1b29f6a | 347 | get_str_buf_start (&str, &buf, &start); |
ed35de72 MV |
348 | if (IS_RO_STRING (str)) |
349 | scm_misc_error (NULL, "string is read-only: ~s", scm_list_1 (orig_str)); | |
350 | ||
9de87eea | 351 | scm_i_pthread_mutex_lock (&stringbuf_write_mutex); |
3ee86942 MV |
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 | ||
9de87eea | 360 | scm_i_pthread_mutex_unlock (&stringbuf_write_mutex); |
3ee86942 MV |
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 | ||
9de87eea | 374 | scm_i_pthread_mutex_lock (&stringbuf_write_mutex); |
3ee86942 MV |
375 | } |
376 | ||
377 | return STRINGBUF_CHARS (buf) + start; | |
b00418df DH |
378 | } |
379 | ||
3ee86942 MV |
380 | void |
381 | scm_i_string_stop_writing (void) | |
382 | { | |
9de87eea | 383 | scm_i_pthread_mutex_unlock (&stringbuf_write_mutex); |
3ee86942 | 384 | } |
b00418df | 385 | |
3ee86942 MV |
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 | |
6869328b MV |
396 | scm_i_make_symbol (SCM name, scm_t_bits flags, |
397 | unsigned long hash, SCM props) | |
3ee86942 MV |
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. */ | |
9de87eea | 413 | scm_i_pthread_mutex_lock (&stringbuf_write_mutex); |
3ee86942 | 414 | SET_STRINGBUF_SHARED (buf); |
9de87eea | 415 | scm_i_pthread_mutex_unlock (&stringbuf_write_mutex); |
3ee86942 MV |
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 | } | |
6869328b | 425 | return scm_double_cell (scm_tc7_symbol | flags, SCM_UNPACK (buf), |
3ee86942 MV |
426 | (scm_t_bits) hash, SCM_UNPACK (props)); |
427 | } | |
428 | ||
fd0a5bbc HWN |
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 | ||
3ee86942 MV |
452 | size_t |
453 | scm_i_symbol_length (SCM sym) | |
0f2d19dd | 454 | { |
3ee86942 | 455 | return STRINGBUF_LENGTH (SYMBOL_STRINGBUF (sym)); |
0f2d19dd JB |
456 | } |
457 | ||
071bb6a8 LC |
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 | ||
3ee86942 MV |
468 | const char * |
469 | scm_i_symbol_chars (SCM sym) | |
470 | { | |
471 | SCM buf = SYMBOL_STRINGBUF (sym); | |
472 | return STRINGBUF_CHARS (buf); | |
473 | } | |
1cc91f1b | 474 | |
3ee86942 MV |
475 | SCM |
476 | scm_i_symbol_mark (SCM sym) | |
0f2d19dd | 477 | { |
3ee86942 MV |
478 | scm_gc_mark (SYMBOL_STRINGBUF (sym)); |
479 | return SCM_CELL_OBJECT_3 (sym); | |
0f2d19dd JB |
480 | } |
481 | ||
3ee86942 MV |
482 | void |
483 | scm_i_symbol_free (SCM sym) | |
484 | { | |
485 | } | |
0f2d19dd | 486 | |
be54b15d | 487 | SCM |
3ee86942 | 488 | scm_i_symbol_substring (SCM sym, size_t start, size_t end) |
be54b15d | 489 | { |
3ee86942 | 490 | SCM buf = SYMBOL_STRINGBUF (sym); |
9de87eea | 491 | scm_i_pthread_mutex_lock (&stringbuf_write_mutex); |
3ee86942 | 492 | SET_STRINGBUF_SHARED (buf); |
9de87eea | 493 | scm_i_pthread_mutex_unlock (&stringbuf_write_mutex); |
3ee86942 MV |
494 | return scm_double_cell (STRING_TAG, SCM_UNPACK(buf), |
495 | (scm_t_bits)start, (scm_t_bits) end - start); | |
496 | } | |
be54b15d | 497 | |
3ee86942 MV |
498 | /* Debugging |
499 | */ | |
be54b15d | 500 | |
3ee86942 | 501 | #if SCM_DEBUG |
be54b15d | 502 | |
3ee86942 MV |
503 | SCM scm_sys_string_dump (SCM); |
504 | SCM scm_sys_symbol_dump (SCM); | |
505 | SCM scm_sys_stringbuf_hist (void); | |
be54b15d | 506 | |
3ee86942 MV |
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 | "") | |
e1b29f6a | 556 | #define FUNC_NAME s_scm_sys_stringbuf_hist |
3ee86942 MV |
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; | |
be54b15d DH |
564 | } |
565 | #undef FUNC_NAME | |
566 | ||
3ee86942 MV |
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); | |
d2e53ed6 | 602 | while (len > 0 && scm_is_pair (chrs)) |
3ee86942 MV |
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); | |
d2e53ed6 | 613 | if (!scm_is_null (chrs)) |
3ee86942 MV |
614 | scm_wrong_type_arg_msg (NULL, 0, chrs, "proper list"); |
615 | ||
616 | return result; | |
617 | } | |
618 | #undef FUNC_NAME | |
be54b15d | 619 | |
3b3b36dd | 620 | SCM_DEFINE (scm_make_string, "make-string", 1, 1, 0, |
6fa73e72 | 621 | (SCM k, SCM chr), |
0d26a824 MG |
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" | |
9401323e | 625 | "of the @var{string} are unspecified.") |
1bbd0b84 | 626 | #define FUNC_NAME s_scm_make_string |
0f2d19dd | 627 | { |
3ee86942 MV |
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); | |
cb0d8be2 | 638 | |
e11e83f3 MV |
639 | if (!SCM_UNBNDP (chr)) |
640 | { | |
3ee86942 MV |
641 | SCM_VALIDATE_CHAR (0, chr); |
642 | memset (dst, SCM_CHAR (chr), len); | |
0f2d19dd | 643 | } |
e11e83f3 MV |
644 | |
645 | return res; | |
0f2d19dd | 646 | } |
1bbd0b84 | 647 | #undef FUNC_NAME |
0f2d19dd | 648 | |
3b3b36dd | 649 | SCM_DEFINE (scm_string_length, "string-length", 1, 0, 0, |
0d26a824 MG |
650 | (SCM string), |
651 | "Return the number of characters in @var{string}.") | |
1bbd0b84 | 652 | #define FUNC_NAME s_scm_string_length |
0f2d19dd | 653 | { |
d1ca2c64 | 654 | SCM_VALIDATE_STRING (1, string); |
3ee86942 | 655 | return scm_from_size_t (STRING_LENGTH (string)); |
0f2d19dd | 656 | } |
1bbd0b84 | 657 | #undef FUNC_NAME |
0f2d19dd | 658 | |
3ee86942 MV |
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 | ||
bd9e24b3 | 667 | SCM_DEFINE (scm_string_ref, "string-ref", 2, 0, 0, |
6fa73e72 | 668 | (SCM str, SCM k), |
0d26a824 MG |
669 | "Return character @var{k} of @var{str} using zero-origin\n" |
670 | "indexing. @var{k} must be a valid index of @var{str}.") | |
1bbd0b84 | 671 | #define FUNC_NAME s_scm_string_ref |
0f2d19dd | 672 | { |
a55c2b68 | 673 | unsigned long idx; |
bd9e24b3 | 674 | |
d1ca2c64 | 675 | SCM_VALIDATE_STRING (1, str); |
3ee86942 MV |
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]); | |
0f2d19dd | 678 | } |
1bbd0b84 | 679 | #undef FUNC_NAME |
0f2d19dd | 680 | |
3ee86942 MV |
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 | } | |
f0942910 | 688 | |
3b3b36dd | 689 | SCM_DEFINE (scm_string_set_x, "string-set!", 3, 0, 0, |
6fa73e72 | 690 | (SCM str, SCM k, SCM chr), |
0d26a824 MG |
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}.") | |
1bbd0b84 | 694 | #define FUNC_NAME s_scm_string_set_x |
0f2d19dd | 695 | { |
a55c2b68 MV |
696 | unsigned long idx; |
697 | ||
f0942910 | 698 | SCM_VALIDATE_STRING (1, str); |
3ee86942 | 699 | idx = scm_to_unsigned_integer (k, 0, scm_i_string_length(str)-1); |
34d19ef6 | 700 | SCM_VALIDATE_CHAR (3, chr); |
3ee86942 MV |
701 | { |
702 | char *dst = scm_i_string_writable_chars (str); | |
703 | dst[idx] = SCM_CHAR (chr); | |
704 | scm_i_string_stop_writing (); | |
705 | } | |
0f2d19dd JB |
706 | return SCM_UNSPECIFIED; |
707 | } | |
1bbd0b84 | 708 | #undef FUNC_NAME |
0f2d19dd | 709 | |
3ee86942 MV |
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 | } | |
0f2d19dd | 721 | |
3b3b36dd | 722 | SCM_DEFINE (scm_substring, "substring", 2, 1, 0, |
0d26a824 MG |
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}).") | |
1bbd0b84 | 730 | #define FUNC_NAME s_scm_substring |
0f2d19dd | 731 | { |
3ee86942 | 732 | size_t len, from, to; |
685c0d71 | 733 | |
d1ca2c64 | 734 | SCM_VALIDATE_STRING (1, str); |
3ee86942 MV |
735 | len = scm_i_string_length (str); |
736 | from = scm_to_unsigned_integer (start, 0, len); | |
a55c2b68 | 737 | if (SCM_UNBNDP (end)) |
3ee86942 | 738 | to = len; |
a55c2b68 | 739 | else |
3ee86942 MV |
740 | to = scm_to_unsigned_integer (end, from, len); |
741 | return scm_i_substring (str, from, to); | |
0f2d19dd | 742 | } |
1bbd0b84 | 743 | #undef FUNC_NAME |
0f2d19dd | 744 | |
ed35de72 MV |
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 | ||
3ee86942 MV |
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 | { | |
e1b29f6a MV |
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; | |
3ee86942 MV |
787 | |
788 | SCM_VALIDATE_STRING (1, str); | |
e1b29f6a MV |
789 | scm_i_get_substring_spec (scm_i_string_length (str), |
790 | start, &from, end, &to); | |
3ee86942 MV |
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 | |
685c0d71 | 817 | |
3b3b36dd | 818 | SCM_DEFINE (scm_string_append, "string-append", 0, 0, 1, |
6fa73e72 | 819 | (SCM args), |
0d26a824 MG |
820 | "Return a newly allocated string whose characters form the\n" |
821 | "concatenation of the given strings, @var{args}.") | |
1bbd0b84 | 822 | #define FUNC_NAME s_scm_string_append |
0f2d19dd JB |
823 | { |
824 | SCM res; | |
1be6b49c | 825 | size_t i = 0; |
c829a427 MV |
826 | SCM l, s; |
827 | char *data; | |
af45e3b0 DH |
828 | |
829 | SCM_VALIDATE_REST_ARGUMENT (args); | |
d2e53ed6 | 830 | for (l = args; !scm_is_null (l); l = SCM_CDR (l)) |
c829a427 MV |
831 | { |
832 | s = SCM_CAR (l); | |
833 | SCM_VALIDATE_STRING (SCM_ARGn, s); | |
3ee86942 | 834 | i += scm_i_string_length (s); |
c829a427 | 835 | } |
3ee86942 | 836 | res = scm_i_make_string (i, &data); |
d2e53ed6 | 837 | for (l = args; !scm_is_null (l); l = SCM_CDR (l)) |
c829a427 | 838 | { |
edea856c | 839 | size_t len; |
c829a427 | 840 | s = SCM_CAR (l); |
3ee86942 | 841 | SCM_VALIDATE_STRING (SCM_ARGn, s); |
edea856c | 842 | len = scm_i_string_length (s); |
3ee86942 MV |
843 | memcpy (data, scm_i_string_chars (s), len); |
844 | data += len; | |
c829a427 MV |
845 | scm_remember_upto_here_1 (s); |
846 | } | |
0f2d19dd JB |
847 | return res; |
848 | } | |
1bbd0b84 | 849 | #undef FUNC_NAME |
0f2d19dd | 850 | |
c829a427 MV |
851 | int |
852 | scm_is_string (SCM obj) | |
853 | { | |
3ee86942 | 854 | return IS_STRING (obj); |
c829a427 | 855 | } |
24933780 | 856 | |
c829a427 MV |
857 | SCM |
858 | scm_from_locale_stringn (const char *str, size_t len) | |
859 | { | |
860 | SCM res; | |
861 | char *dst; | |
4d4528e7 | 862 | |
c829a427 MV |
863 | if (len == (size_t)-1) |
864 | len = strlen (str); | |
3ee86942 | 865 | res = scm_i_make_string (len, &dst); |
c829a427 MV |
866 | memcpy (dst, str, len); |
867 | return res; | |
868 | } | |
4d4528e7 | 869 | |
c829a427 MV |
870 | SCM |
871 | scm_from_locale_string (const char *str) | |
4d4528e7 | 872 | { |
c829a427 MV |
873 | return scm_from_locale_stringn (str, -1); |
874 | } | |
4d4528e7 | 875 | |
c829a427 MV |
876 | SCM |
877 | scm_take_locale_stringn (char *str, size_t len) | |
878 | { | |
48ddf0d9 KR |
879 | SCM buf, res; |
880 | ||
c829a427 | 881 | if (len == (size_t)-1) |
48ddf0d9 | 882 | len = strlen (str); |
c829a427 MV |
883 | else |
884 | { | |
48ddf0d9 KR |
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'; | |
c829a427 | 890 | } |
c829a427 | 891 | |
fd0a5bbc | 892 | buf = scm_i_take_stringbufn (str, len); |
3ee86942 | 893 | res = scm_double_cell (STRING_TAG, |
48ddf0d9 KR |
894 | SCM_UNPACK (buf), |
895 | (scm_t_bits) 0, (scm_t_bits) len); | |
c829a427 MV |
896 | return res; |
897 | } | |
898 | ||
48ddf0d9 KR |
899 | SCM |
900 | scm_take_locale_string (char *str) | |
901 | { | |
902 | return scm_take_locale_stringn (str, -1); | |
903 | } | |
904 | ||
c829a427 MV |
905 | char * |
906 | scm_to_locale_stringn (SCM str, size_t *lenp) | |
907 | { | |
908 | char *res; | |
909 | size_t len; | |
4d4528e7 | 910 | |
3ee86942 | 911 | if (!scm_is_string (str)) |
c829a427 | 912 | scm_wrong_type_arg_msg (NULL, 0, str, "string"); |
3ee86942 | 913 | len = scm_i_string_length (str); |
c829a427 | 914 | res = scm_malloc (len + ((lenp==NULL)? 1 : 0)); |
3ee86942 | 915 | memcpy (res, scm_i_string_chars (str), len); |
c829a427 MV |
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 | |
4d4528e7 | 928 | *lenp = len; |
24933780 | 929 | |
c829a427 MV |
930 | scm_remember_upto_here_1 (str); |
931 | return res; | |
4d4528e7 | 932 | } |
af68e5e5 | 933 | |
c829a427 MV |
934 | char * |
935 | scm_to_locale_string (SCM str) | |
936 | { | |
937 | return scm_to_locale_stringn (str, NULL); | |
938 | } | |
af68e5e5 | 939 | |
c829a427 MV |
940 | size_t |
941 | scm_to_locale_stringbuf (SCM str, char *buf, size_t max_len) | |
942 | { | |
943 | size_t len; | |
944 | ||
3ee86942 | 945 | if (!scm_is_string (str)) |
c829a427 | 946 | scm_wrong_type_arg_msg (NULL, 0, str, "string"); |
3ee86942 MV |
947 | len = scm_i_string_length (str); |
948 | memcpy (buf, scm_i_string_chars (str), (len > max_len)? max_len : len); | |
c829a427 MV |
949 | scm_remember_upto_here_1 (str); |
950 | return len; | |
951 | } | |
af68e5e5 | 952 | |
3ee86942 MV |
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 | ||
c829a427 MV |
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) | |
af68e5e5 | 972 | { |
c829a427 MV |
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 | ||
661ae7ab | 980 | scm_dynwind_begin (0); |
c829a427 MV |
981 | |
982 | result = (char **) scm_malloc ((len + 1) * sizeof (char *)); | |
983 | result[len] = NULL; | |
661ae7ab | 984 | scm_dynwind_unwind_handler (free, result, 0); |
c829a427 MV |
985 | |
986 | /* The list might be have been modified in another thread, so | |
987 | we check LIST before each access. | |
988 | */ | |
d2e53ed6 | 989 | for (i = 0; i < len && scm_is_pair (list); i++) |
c829a427 MV |
990 | { |
991 | result[i] = scm_to_locale_string (SCM_CAR (list)); | |
992 | list = SCM_CDR (list); | |
993 | } | |
994 | ||
661ae7ab | 995 | scm_dynwind_end (); |
c829a427 | 996 | return result; |
af68e5e5 | 997 | } |
e53cc817 | 998 | |
c829a427 MV |
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 | } | |
24933780 | 1008 | |
6f14f578 MV |
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 | ||
3ee86942 MV |
1025 | #if SCM_ENABLE_DEPRECATED |
1026 | ||
556d75db MV |
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 | ||
3ee86942 | 1033 | int |
fe78c51a | 1034 | scm_i_deprecated_stringp (SCM str) |
3ee86942 MV |
1035 | { |
1036 | scm_c_issue_deprecation_warning | |
1037 | ("SCM_STRINGP is deprecated. Use scm_is_string instead."); | |
1038 | ||
2616f0e0 | 1039 | return scm_is_string (str); |
3ee86942 MV |
1040 | } |
1041 | ||
1042 | char * | |
fe78c51a | 1043 | scm_i_deprecated_string_chars (SCM str) |
3ee86942 MV |
1044 | { |
1045 | char *chars; | |
1046 | ||
1047 | scm_c_issue_deprecation_warning | |
1048 | ("SCM_STRING_CHARS is deprecated. See the manual for alternatives."); | |
1049 | ||
2616f0e0 MV |
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 | ||
556d75db MV |
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 | ||
2616f0e0 | 1067 | /* The following is still wrong, of course... |
3ee86942 MV |
1068 | */ |
1069 | chars = scm_i_string_writable_chars (str); | |
1070 | scm_i_string_stop_writing (); | |
1071 | return chars; | |
1072 | } | |
1073 | ||
1074 | size_t | |
fe78c51a | 1075 | scm_i_deprecated_string_length (SCM str) |
3ee86942 MV |
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 | ||
0f2d19dd JB |
1084 | void |
1085 | scm_init_strings () | |
0f2d19dd | 1086 | { |
3ee86942 | 1087 | scm_nullstr = scm_i_make_string (0, NULL); |
7c33806a | 1088 | |
a0599745 | 1089 | #include "libguile/strings.x" |
0f2d19dd JB |
1090 | } |
1091 | ||
89e00824 ML |
1092 | |
1093 | /* | |
1094 | Local Variables: | |
1095 | c-file-style: "gnu" | |
1096 | End: | |
1097 | */ |