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