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> |
9c44cd45 MG |
27 | #include <ctype.h> |
28 | #include <unistr.h> | |
faf2c9d7 | 29 | |
a0599745 MD |
30 | #include "libguile/_scm.h" |
31 | #include "libguile/chars.h" | |
7c33806a | 32 | #include "libguile/root.h" |
a0599745 | 33 | #include "libguile/strings.h" |
1afff620 | 34 | #include "libguile/deprecation.h" |
a0599745 | 35 | #include "libguile/validate.h" |
c829a427 | 36 | #include "libguile/dynwind.h" |
1afff620 | 37 | |
0f2d19dd JB |
38 | \f |
39 | ||
40 | /* {Strings} | |
41 | */ | |
42 | ||
3ee86942 MV |
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 | |
9c44cd45 | 74 | #define STRINGBUF_F_WIDE 0x400 |
3ee86942 MV |
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) | |
9c44cd45 | 79 | #define STRINGBUF_WIDE(buf) (SCM_CELL_WORD_0(buf) & STRINGBUF_F_WIDE) |
3ee86942 MV |
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)) | |
9c44cd45 | 89 | #define STRINGBUF_WIDE_CHARS(buf) ((scm_t_wchar *)SCM_CELL_WORD_1(buf)) |
3ee86942 MV |
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) | |
0f2d19dd | 105 | { |
3ee86942 MV |
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 | |
0f2d19dd | 119 | |
3ee86942 MV |
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 | } | |
e53cc817 | 133 | |
9c44cd45 MG |
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 | ||
2b829bbb KR |
151 | /* Return a new stringbuf whose underlying storage consists of the LEN+1 |
152 | octets pointed to by STR (the last octet is zero). */ | |
7f74cf9a | 153 | SCM |
fd0a5bbc HWN |
154 | scm_i_take_stringbufn (char *str, size_t len) |
155 | { | |
2b829bbb | 156 | scm_gc_register_collectable_memory (str, len + 1, "stringbuf"); |
fd0a5bbc HWN |
157 | |
158 | return scm_double_cell (STRINGBUF_TAG, (scm_t_bits) str, | |
159 | (scm_t_bits) len, (scm_t_bits) 0); | |
160 | } | |
161 | ||
3ee86942 MV |
162 | SCM |
163 | scm_i_stringbuf_mark (SCM buf) | |
164 | { | |
165 | return SCM_BOOL_F; | |
166 | } | |
1bbd0b84 | 167 | |
3ee86942 MV |
168 | void |
169 | scm_i_stringbuf_free (SCM buf) | |
0f2d19dd | 170 | { |
3ee86942 | 171 | if (!STRINGBUF_INLINE (buf)) |
9c44cd45 MG |
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 | } | |
3ee86942 | 224 | } |
bd9e24b3 | 225 | |
9de87eea | 226 | scm_i_pthread_mutex_t stringbuf_write_mutex = SCM_I_PTHREAD_MUTEX_INITIALIZER; |
bd9e24b3 | 227 | |
3ee86942 MV |
228 | /* Copy-on-write strings. |
229 | */ | |
bd9e24b3 | 230 | |
3ee86942 | 231 | #define STRING_TAG scm_tc7_string |
bd9e24b3 | 232 | |
3ee86942 MV |
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)) | |
bd9e24b3 | 236 | |
3ee86942 MV |
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 | ||
ed35de72 MV |
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 | ||
e1b29f6a MV |
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 | ||
3ee86942 MV |
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; | |
0f2d19dd JB |
268 | } |
269 | ||
9c44cd45 MG |
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 | ||
3ee86942 MV |
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 | } | |
0f2d19dd | 292 | |
e1b29f6a MV |
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 | ||
3ee86942 MV |
305 | SCM |
306 | scm_i_substring (SCM str, size_t start, size_t end) | |
0f2d19dd | 307 | { |
e1b29f6a MV |
308 | SCM buf; |
309 | size_t str_start; | |
310 | get_str_buf_start (&str, &buf, &str_start); | |
9de87eea | 311 | scm_i_pthread_mutex_lock (&stringbuf_write_mutex); |
3ee86942 | 312 | SET_STRINGBUF_SHARED (buf); |
9de87eea | 313 | scm_i_pthread_mutex_unlock (&stringbuf_write_mutex); |
3ee86942 | 314 | return scm_double_cell (STRING_TAG, SCM_UNPACK(buf), |
e1b29f6a MV |
315 | (scm_t_bits)str_start + start, |
316 | (scm_t_bits) end - start); | |
0f2d19dd JB |
317 | } |
318 | ||
ed35de72 MV |
319 | SCM |
320 | scm_i_substring_read_only (SCM str, size_t start, size_t end) | |
321 | { | |
45a9f430 LC |
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); | |
ed35de72 MV |
331 | } |
332 | ||
3ee86942 MV |
333 | SCM |
334 | scm_i_substring_copy (SCM str, size_t start, size_t end) | |
335 | { | |
336 | size_t len = end - start; | |
edea856c | 337 | SCM buf, my_buf; |
e1b29f6a MV |
338 | size_t str_start; |
339 | get_str_buf_start (&str, &buf, &str_start); | |
9c44cd45 MG |
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 | } | |
3ee86942 | 355 | scm_remember_upto_here_1 (buf); |
9c44cd45 MG |
356 | return scm_double_cell (STRING_TAG, SCM_UNPACK (my_buf), |
357 | (scm_t_bits) 0, (scm_t_bits) len); | |
3ee86942 | 358 | } |
0f2d19dd | 359 | |
e1b29f6a MV |
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 | ||
3ee86942 MV |
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 | } | |
ee149d03 | 384 | |
ed35de72 MV |
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 | ||
0f2d19dd | 392 | SCM |
3ee86942 | 393 | scm_c_substring_copy (SCM str, size_t start, size_t end) |
0f2d19dd | 394 | { |
3ee86942 MV |
395 | validate_substring_args (str, start, end); |
396 | return scm_i_substring_copy (str, start, end); | |
397 | } | |
398 | ||
3ee86942 MV |
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 | } | |
0f2d19dd | 405 | |
ee149d03 | 406 | SCM |
3ee86942 | 407 | scm_i_string_mark (SCM str) |
ee149d03 | 408 | { |
3ee86942 MV |
409 | if (IS_SH_STRING (str)) |
410 | return SH_STRING_STRING (str); | |
411 | else | |
412 | return STRING_STRINGBUF (str); | |
ee149d03 JB |
413 | } |
414 | ||
3ee86942 MV |
415 | void |
416 | scm_i_string_free (SCM str) | |
417 | { | |
418 | } | |
36284627 | 419 | |
3ee86942 MV |
420 | /* Internal accessors |
421 | */ | |
422 | ||
423 | size_t | |
424 | scm_i_string_length (SCM str) | |
0f2d19dd | 425 | { |
3ee86942 | 426 | return STRING_LENGTH (str); |
0f2d19dd JB |
427 | } |
428 | ||
9c44cd45 MG |
429 | int |
430 | scm_i_is_narrow_string (SCM str) | |
431 | { | |
432 | return !STRINGBUF_WIDE (STRING_STRINGBUF (str)); | |
433 | } | |
434 | ||
3ee86942 MV |
435 | const char * |
436 | scm_i_string_chars (SCM str) | |
437 | { | |
438 | SCM buf; | |
e1b29f6a MV |
439 | size_t start; |
440 | get_str_buf_start (&str, &buf, &start); | |
9c44cd45 MG |
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; | |
3ee86942 | 447 | } |
b00418df | 448 | |
9c44cd45 MG |
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) | |
b00418df | 468 | { |
ed35de72 | 469 | SCM buf, str = orig_str; |
e1b29f6a | 470 | size_t start; |
ed35de72 | 471 | |
e1b29f6a | 472 | get_str_buf_start (&str, &buf, &start); |
ed35de72 MV |
473 | if (IS_RO_STRING (str)) |
474 | scm_misc_error (NULL, "string is read-only: ~s", scm_list_1 (orig_str)); | |
475 | ||
9de87eea | 476 | scm_i_pthread_mutex_lock (&stringbuf_write_mutex); |
3ee86942 MV |
477 | if (STRINGBUF_SHARED (buf)) |
478 | { | |
9c44cd45 | 479 | /* Clone the stringbuf. */ |
3ee86942 MV |
480 | size_t len = STRING_LENGTH (str); |
481 | SCM new_buf; | |
482 | ||
9de87eea | 483 | scm_i_pthread_mutex_unlock (&stringbuf_write_mutex); |
3ee86942 | 484 | |
9c44cd45 MG |
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 | } | |
3ee86942 MV |
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 | ||
9de87eea | 507 | scm_i_pthread_mutex_lock (&stringbuf_write_mutex); |
3ee86942 | 508 | } |
9c44cd45 MG |
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; | |
3ee86942 | 519 | |
9c44cd45 MG |
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)); | |
b00418df DH |
542 | } |
543 | ||
3ee86942 MV |
544 | void |
545 | scm_i_string_stop_writing (void) | |
546 | { | |
9de87eea | 547 | scm_i_pthread_mutex_unlock (&stringbuf_write_mutex); |
3ee86942 | 548 | } |
b00418df | 549 | |
9c44cd45 MG |
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 | ||
3ee86942 MV |
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 | |
6869328b MV |
588 | scm_i_make_symbol (SCM name, scm_t_bits flags, |
589 | unsigned long hash, SCM props) | |
3ee86942 MV |
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. */ | |
9de87eea | 605 | scm_i_pthread_mutex_lock (&stringbuf_write_mutex); |
3ee86942 | 606 | SET_STRINGBUF_SHARED (buf); |
9de87eea | 607 | scm_i_pthread_mutex_unlock (&stringbuf_write_mutex); |
3ee86942 MV |
608 | } |
609 | else | |
610 | { | |
611 | /* make new buf. */ | |
9c44cd45 MG |
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 | } | |
3ee86942 | 627 | } |
6869328b | 628 | return scm_double_cell (scm_tc7_symbol | flags, SCM_UNPACK (buf), |
3ee86942 MV |
629 | (scm_t_bits) hash, SCM_UNPACK (props)); |
630 | } | |
631 | ||
fd0a5bbc HWN |
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 | ||
3ee86942 MV |
655 | size_t |
656 | scm_i_symbol_length (SCM sym) | |
0f2d19dd | 657 | { |
3ee86942 | 658 | return STRINGBUF_LENGTH (SYMBOL_STRINGBUF (sym)); |
0f2d19dd JB |
659 | } |
660 | ||
071bb6a8 LC |
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 | ||
9c44cd45 MG |
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 | ||
3ee86942 MV |
680 | const char * |
681 | scm_i_symbol_chars (SCM sym) | |
682 | { | |
9c44cd45 MG |
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)); | |
3ee86942 | 705 | } |
1cc91f1b | 706 | |
3ee86942 MV |
707 | SCM |
708 | scm_i_symbol_mark (SCM sym) | |
0f2d19dd | 709 | { |
3ee86942 MV |
710 | scm_gc_mark (SYMBOL_STRINGBUF (sym)); |
711 | return SCM_CELL_OBJECT_3 (sym); | |
0f2d19dd JB |
712 | } |
713 | ||
3ee86942 MV |
714 | void |
715 | scm_i_symbol_free (SCM sym) | |
716 | { | |
717 | } | |
0f2d19dd | 718 | |
be54b15d | 719 | SCM |
3ee86942 | 720 | scm_i_symbol_substring (SCM sym, size_t start, size_t end) |
be54b15d | 721 | { |
3ee86942 | 722 | SCM buf = SYMBOL_STRINGBUF (sym); |
9de87eea | 723 | scm_i_pthread_mutex_lock (&stringbuf_write_mutex); |
3ee86942 | 724 | SET_STRINGBUF_SHARED (buf); |
9de87eea | 725 | scm_i_pthread_mutex_unlock (&stringbuf_write_mutex); |
fd2b17b9 | 726 | return scm_double_cell (RO_STRING_TAG, SCM_UNPACK (buf), |
3ee86942 MV |
727 | (scm_t_bits)start, (scm_t_bits) end - start); |
728 | } | |
be54b15d | 729 | |
9c44cd45 MG |
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 | ||
3ee86942 MV |
739 | /* Debugging |
740 | */ | |
be54b15d | 741 | |
3ee86942 | 742 | #if SCM_DEBUG |
be54b15d | 743 | |
3ee86942 MV |
744 | SCM scm_sys_string_dump (SCM); |
745 | SCM scm_sys_symbol_dump (SCM); | |
746 | SCM scm_sys_stringbuf_hist (void); | |
be54b15d | 747 | |
9c44cd45 | 748 | SCM_DEFINE (scm_sys_string_dump, "%string-dump", 1, 0, 0, (SCM str), "") |
3ee86942 MV |
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)); | |
9c44cd45 MG |
755 | if (scm_i_is_narrow_string (str)) |
756 | fprintf (stderr, " format: narrow\n"); | |
757 | else | |
758 | fprintf (stderr, " format: wide\n"); | |
3ee86942 MV |
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); | |
9c44cd45 MG |
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)); | |
3ee86942 | 773 | fprintf (stderr, " length: %u\n", STRINGBUF_LENGTH (buf)); |
9c44cd45 MG |
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 | ||
3ee86942 MV |
783 | } |
784 | return SCM_UNSPECIFIED; | |
785 | } | |
786 | #undef FUNC_NAME | |
787 | ||
9c44cd45 | 788 | SCM_DEFINE (scm_sys_symbol_dump, "%symbol-dump", 1, 0, 0, (SCM sym), "") |
3ee86942 MV |
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)); | |
9c44cd45 MG |
794 | if (scm_i_is_narrow_symbol (sym)) |
795 | fprintf (stderr, " format: narrow\n"); | |
796 | else | |
797 | fprintf (stderr, " format: wide\n"); | |
3ee86942 MV |
798 | { |
799 | SCM buf = SYMBOL_STRINGBUF (sym); | |
800 | fprintf (stderr, " buf: %p\n", buf); | |
9c44cd45 MG |
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)); | |
3ee86942 | 805 | fprintf (stderr, " length: %u\n", STRINGBUF_LENGTH (buf)); |
9c44cd45 MG |
806 | if (STRINGBUF_SHARED (buf)) |
807 | fprintf (stderr, " shared: true\n"); | |
808 | else | |
809 | fprintf (stderr, " shared: false\n"); | |
810 | ||
3ee86942 MV |
811 | } |
812 | return SCM_UNSPECIFIED; | |
813 | } | |
814 | #undef FUNC_NAME | |
815 | ||
9c44cd45 | 816 | SCM_DEFINE (scm_sys_stringbuf_hist, "%stringbuf-hist", 0, 0, 0, (void), "") |
e1b29f6a | 817 | #define FUNC_NAME s_scm_sys_stringbuf_hist |
3ee86942 MV |
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; | |
be54b15d DH |
825 | } |
826 | #undef FUNC_NAME | |
827 | ||
3ee86942 MV |
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; | |
9c44cd45 | 852 | SCM rest; |
3ee86942 | 853 | size_t len; |
9c44cd45 MG |
854 | size_t p = 0; |
855 | long i; | |
3ee86942 | 856 | |
9c44cd45 MG |
857 | /* Verify that this is a list of chars. */ |
858 | i = scm_ilength (chrs); | |
859 | len = (size_t) i; | |
860 | rest = chrs; | |
3ee86942 | 861 | |
9c44cd45 MG |
862 | SCM_ASSERT (len >= 0, chrs, SCM_ARG1, FUNC_NAME); |
863 | while (len > 0 && scm_is_pair (rest)) | |
3ee86942 | 864 | { |
9c44cd45 | 865 | SCM elt = SCM_CAR (rest); |
3ee86942 | 866 | SCM_VALIDATE_CHAR (SCM_ARGn, elt); |
9c44cd45 MG |
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); | |
3ee86942 | 884 | len--; |
9c44cd45 | 885 | scm_remember_upto_here_1 (elt); |
3ee86942 | 886 | } |
9c44cd45 MG |
887 | scm_i_string_stop_writing (); |
888 | ||
3ee86942 MV |
889 | if (len > 0) |
890 | scm_misc_error (NULL, "list changed while constructing string", SCM_EOL); | |
9c44cd45 | 891 | if (!scm_is_null (rest)) |
3ee86942 MV |
892 | scm_wrong_type_arg_msg (NULL, 0, chrs, "proper list"); |
893 | ||
894 | return result; | |
895 | } | |
896 | #undef FUNC_NAME | |
be54b15d | 897 | |
3b3b36dd | 898 | SCM_DEFINE (scm_make_string, "make-string", 1, 1, 0, |
6fa73e72 | 899 | (SCM k, SCM chr), |
0d26a824 MG |
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" | |
9401323e | 903 | "of the @var{string} are unspecified.") |
1bbd0b84 | 904 | #define FUNC_NAME s_scm_make_string |
0f2d19dd | 905 | { |
3ee86942 MV |
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 | { | |
9c44cd45 MG |
914 | size_t p; |
915 | SCM res = scm_i_make_string (len, NULL); | |
cb0d8be2 | 916 | |
e11e83f3 MV |
917 | if (!SCM_UNBNDP (chr)) |
918 | { | |
3ee86942 | 919 | SCM_VALIDATE_CHAR (0, chr); |
9c44cd45 MG |
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 (); | |
0f2d19dd | 924 | } |
e11e83f3 MV |
925 | |
926 | return res; | |
0f2d19dd | 927 | } |
1bbd0b84 | 928 | #undef FUNC_NAME |
0f2d19dd | 929 | |
3b3b36dd | 930 | SCM_DEFINE (scm_string_length, "string-length", 1, 0, 0, |
0d26a824 MG |
931 | (SCM string), |
932 | "Return the number of characters in @var{string}.") | |
1bbd0b84 | 933 | #define FUNC_NAME s_scm_string_length |
0f2d19dd | 934 | { |
d1ca2c64 | 935 | SCM_VALIDATE_STRING (1, string); |
3ee86942 | 936 | return scm_from_size_t (STRING_LENGTH (string)); |
0f2d19dd | 937 | } |
1bbd0b84 | 938 | #undef FUNC_NAME |
0f2d19dd | 939 | |
9c44cd45 MG |
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 | ||
3ee86942 MV |
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 | ||
bd9e24b3 | 962 | SCM_DEFINE (scm_string_ref, "string-ref", 2, 0, 0, |
6fa73e72 | 963 | (SCM str, SCM k), |
9c44cd45 MG |
964 | "Return character @var{k} of @var{str} using zero-origin\n" |
965 | "indexing. @var{k} must be a valid index of @var{str}.") | |
1bbd0b84 | 966 | #define FUNC_NAME s_scm_string_ref |
0f2d19dd | 967 | { |
3ae3166b | 968 | size_t len; |
a55c2b68 | 969 | unsigned long idx; |
bd9e24b3 | 970 | |
d1ca2c64 | 971 | SCM_VALIDATE_STRING (1, str); |
3ae3166b LC |
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 | ||
9c44cd45 MG |
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]); | |
0f2d19dd | 983 | } |
1bbd0b84 | 984 | #undef FUNC_NAME |
0f2d19dd | 985 | |
3ee86942 MV |
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)); | |
9c44cd45 MG |
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 | ||
3ee86942 | 996 | } |
f0942910 | 997 | |
3b3b36dd | 998 | SCM_DEFINE (scm_string_set_x, "string-set!", 3, 0, 0, |
6fa73e72 | 999 | (SCM str, SCM k, SCM chr), |
9c44cd45 MG |
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}.") | |
1bbd0b84 | 1003 | #define FUNC_NAME s_scm_string_set_x |
0f2d19dd | 1004 | { |
3ae3166b | 1005 | size_t len; |
a55c2b68 MV |
1006 | unsigned long idx; |
1007 | ||
f0942910 | 1008 | SCM_VALIDATE_STRING (1, str); |
3ae3166b LC |
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 | ||
34d19ef6 | 1016 | SCM_VALIDATE_CHAR (3, chr); |
9c44cd45 MG |
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 | ||
0f2d19dd JB |
1021 | return SCM_UNSPECIFIED; |
1022 | } | |
1bbd0b84 | 1023 | #undef FUNC_NAME |
0f2d19dd | 1024 | |
3ee86942 MV |
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)); | |
9c44cd45 MG |
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 (); | |
3ee86942 | 1033 | } |
0f2d19dd | 1034 | |
3b3b36dd | 1035 | SCM_DEFINE (scm_substring, "substring", 2, 1, 0, |
0d26a824 MG |
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}).") | |
1bbd0b84 | 1043 | #define FUNC_NAME s_scm_substring |
0f2d19dd | 1044 | { |
3ee86942 | 1045 | size_t len, from, to; |
685c0d71 | 1046 | |
d1ca2c64 | 1047 | SCM_VALIDATE_STRING (1, str); |
3ee86942 MV |
1048 | len = scm_i_string_length (str); |
1049 | from = scm_to_unsigned_integer (start, 0, len); | |
a55c2b68 | 1050 | if (SCM_UNBNDP (end)) |
3ee86942 | 1051 | to = len; |
a55c2b68 | 1052 | else |
3ee86942 MV |
1053 | to = scm_to_unsigned_integer (end, from, len); |
1054 | return scm_i_substring (str, from, to); | |
0f2d19dd | 1055 | } |
1bbd0b84 | 1056 | #undef FUNC_NAME |
0f2d19dd | 1057 | |
ed35de72 MV |
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 | ||
3ee86942 MV |
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 | { | |
e1b29f6a MV |
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; | |
3ee86942 MV |
1100 | |
1101 | SCM_VALIDATE_STRING (1, str); | |
e1b29f6a MV |
1102 | scm_i_get_substring_spec (scm_i_string_length (str), |
1103 | start, &from, end, &to); | |
3ee86942 MV |
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 | |
685c0d71 | 1130 | |
3b3b36dd | 1131 | SCM_DEFINE (scm_string_append, "string-append", 0, 0, 1, |
6fa73e72 | 1132 | (SCM args), |
9c44cd45 | 1133 | "Return a newly allocated string whose characters form the\n" |
0d26a824 | 1134 | "concatenation of the given strings, @var{args}.") |
1bbd0b84 | 1135 | #define FUNC_NAME s_scm_string_append |
0f2d19dd JB |
1136 | { |
1137 | SCM res; | |
9c44cd45 MG |
1138 | size_t len = 0; |
1139 | int wide = 0; | |
c829a427 MV |
1140 | SCM l, s; |
1141 | char *data; | |
9c44cd45 MG |
1142 | scm_t_wchar *wdata; |
1143 | int i; | |
af45e3b0 DH |
1144 | |
1145 | SCM_VALIDATE_REST_ARGUMENT (args); | |
9c44cd45 | 1146 | for (l = args; !scm_is_null (l); l = SCM_CDR (l)) |
c829a427 MV |
1147 | { |
1148 | s = SCM_CAR (l); | |
1149 | SCM_VALIDATE_STRING (SCM_ARGn, s); | |
9c44cd45 MG |
1150 | len += scm_i_string_length (s); |
1151 | if (!scm_i_is_narrow_string (s)) | |
1152 | wide = 1; | |
c829a427 | 1153 | } |
9c44cd45 MG |
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)) | |
c829a427 | 1160 | { |
edea856c | 1161 | size_t len; |
c829a427 | 1162 | s = SCM_CAR (l); |
3ee86942 | 1163 | SCM_VALIDATE_STRING (SCM_ARGn, s); |
edea856c | 1164 | len = scm_i_string_length (s); |
9c44cd45 MG |
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 | } | |
c829a427 MV |
1182 | scm_remember_upto_here_1 (s); |
1183 | } | |
0f2d19dd JB |
1184 | return res; |
1185 | } | |
1bbd0b84 | 1186 | #undef FUNC_NAME |
0f2d19dd | 1187 | |
c829a427 MV |
1188 | int |
1189 | scm_is_string (SCM obj) | |
1190 | { | |
3ee86942 | 1191 | return IS_STRING (obj); |
c829a427 | 1192 | } |
24933780 | 1193 | |
c829a427 MV |
1194 | SCM |
1195 | scm_from_locale_stringn (const char *str, size_t len) | |
1196 | { | |
1197 | SCM res; | |
1198 | char *dst; | |
4d4528e7 | 1199 | |
9c44cd45 | 1200 | if (len == (size_t) -1) |
c829a427 | 1201 | len = strlen (str); |
9c44cd45 MG |
1202 | if (len == 0) |
1203 | return scm_nullstr; | |
1204 | ||
3ee86942 | 1205 | res = scm_i_make_string (len, &dst); |
c829a427 MV |
1206 | memcpy (dst, str, len); |
1207 | return res; | |
1208 | } | |
4d4528e7 | 1209 | |
c829a427 MV |
1210 | SCM |
1211 | scm_from_locale_string (const char *str) | |
4d4528e7 | 1212 | { |
9c44cd45 MG |
1213 | if (str == NULL) |
1214 | return scm_nullstr; | |
1215 | ||
c829a427 MV |
1216 | return scm_from_locale_stringn (str, -1); |
1217 | } | |
4d4528e7 | 1218 | |
c829a427 MV |
1219 | SCM |
1220 | scm_take_locale_stringn (char *str, size_t len) | |
1221 | { | |
48ddf0d9 KR |
1222 | SCM buf, res; |
1223 | ||
9c44cd45 | 1224 | if (len == (size_t) -1) |
48ddf0d9 | 1225 | len = strlen (str); |
c829a427 MV |
1226 | else |
1227 | { | |
48ddf0d9 KR |
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. */ | |
9c44cd45 | 1231 | str = scm_realloc (str, len + 1); |
48ddf0d9 | 1232 | str[len] = '\0'; |
c829a427 | 1233 | } |
c829a427 | 1234 | |
fd0a5bbc | 1235 | buf = scm_i_take_stringbufn (str, len); |
3ee86942 | 1236 | res = scm_double_cell (STRING_TAG, |
9c44cd45 | 1237 | SCM_UNPACK (buf), (scm_t_bits) 0, (scm_t_bits) len); |
c829a427 MV |
1238 | return res; |
1239 | } | |
1240 | ||
48ddf0d9 KR |
1241 | SCM |
1242 | scm_take_locale_string (char *str) | |
1243 | { | |
1244 | return scm_take_locale_stringn (str, -1); | |
1245 | } | |
1246 | ||
9c44cd45 MG |
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 | ||
c829a427 | 1302 | char * |
9c44cd45 | 1303 | scm_to_locale_stringn (SCM str, size_t * lenp) |
c829a427 | 1304 | { |
9c44cd45 MG |
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; | |
4d4528e7 | 1321 | |
3ee86942 | 1322 | if (!scm_is_string (str)) |
c829a427 | 1323 | scm_wrong_type_arg_msg (NULL, 0, str, "string"); |
9c44cd45 MG |
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 | ||
c829a427 | 1335 | if (lenp == NULL) |
9c44cd45 MG |
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)) | |
c829a427 | 1343 | { |
9c44cd45 MG |
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 | } | |
c829a427 | 1358 | } |
9c44cd45 MG |
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) | |
4d4528e7 | 1375 | *lenp = len; |
9c44cd45 MG |
1376 | else |
1377 | { | |
1378 | buf = scm_realloc (buf, len + 1); | |
1379 | buf[len] = '\0'; | |
1380 | } | |
24933780 | 1381 | |
c829a427 | 1382 | scm_remember_upto_here_1 (str); |
9c44cd45 | 1383 | return buf; |
4d4528e7 | 1384 | } |
af68e5e5 | 1385 | |
c829a427 MV |
1386 | char * |
1387 | scm_to_locale_string (SCM str) | |
1388 | { | |
1389 | return scm_to_locale_stringn (str, NULL); | |
1390 | } | |
af68e5e5 | 1391 | |
c829a427 MV |
1392 | size_t |
1393 | scm_to_locale_stringbuf (SCM str, char *buf, size_t max_len) | |
1394 | { | |
1395 | size_t len; | |
9c44cd45 | 1396 | char *result = NULL; |
3ee86942 | 1397 | if (!scm_is_string (str)) |
c829a427 | 1398 | scm_wrong_type_arg_msg (NULL, 0, str, "string"); |
9c44cd45 MG |
1399 | result = scm_to_locale_stringn (str, &len); |
1400 | ||
1401 | memcpy (buf, result, (len > max_len) ? max_len : len); | |
1402 | free (result); | |
1403 | ||
c829a427 MV |
1404 | scm_remember_upto_here_1 (str); |
1405 | return len; | |
1406 | } | |
af68e5e5 | 1407 | |
3ee86942 MV |
1408 | /* converts C scm_array of strings to SCM scm_list of strings. */ |
1409 | /* If argc < 0, a null terminated scm_array is assumed. */ | |
9c44cd45 | 1410 | SCM |
3ee86942 MV |
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 | ||
c829a427 MV |
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) | |
af68e5e5 | 1427 | { |
c829a427 MV |
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 | ||
661ae7ab | 1435 | scm_dynwind_begin (0); |
c829a427 MV |
1436 | |
1437 | result = (char **) scm_malloc ((len + 1) * sizeof (char *)); | |
1438 | result[len] = NULL; | |
661ae7ab | 1439 | scm_dynwind_unwind_handler (free, result, 0); |
c829a427 MV |
1440 | |
1441 | /* The list might be have been modified in another thread, so | |
1442 | we check LIST before each access. | |
1443 | */ | |
d2e53ed6 | 1444 | for (i = 0; i < len && scm_is_pair (list); i++) |
c829a427 MV |
1445 | { |
1446 | result[i] = scm_to_locale_string (SCM_CAR (list)); | |
1447 | list = SCM_CDR (list); | |
1448 | } | |
1449 | ||
661ae7ab | 1450 | scm_dynwind_end (); |
c829a427 | 1451 | return result; |
af68e5e5 | 1452 | } |
e53cc817 | 1453 | |
c829a427 MV |
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 | } | |
24933780 | 1463 | |
6f14f578 MV |
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 | ||
3ee86942 MV |
1480 | #if SCM_ENABLE_DEPRECATED |
1481 | ||
556d75db MV |
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 | ||
3ee86942 | 1488 | int |
fe78c51a | 1489 | scm_i_deprecated_stringp (SCM str) |
3ee86942 MV |
1490 | { |
1491 | scm_c_issue_deprecation_warning | |
1492 | ("SCM_STRINGP is deprecated. Use scm_is_string instead."); | |
1493 | ||
2616f0e0 | 1494 | return scm_is_string (str); |
3ee86942 MV |
1495 | } |
1496 | ||
1497 | char * | |
fe78c51a | 1498 | scm_i_deprecated_string_chars (SCM str) |
3ee86942 MV |
1499 | { |
1500 | char *chars; | |
1501 | ||
1502 | scm_c_issue_deprecation_warning | |
1503 | ("SCM_STRING_CHARS is deprecated. See the manual for alternatives."); | |
1504 | ||
2616f0e0 MV |
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 | ||
877f06c3 | 1513 | /* We explicitly test for read-only strings to produce a better |
556d75db MV |
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 | ||
2616f0e0 | 1522 | /* The following is still wrong, of course... |
3ee86942 | 1523 | */ |
9c44cd45 | 1524 | str = scm_i_string_start_writing (str); |
3ee86942 MV |
1525 | chars = scm_i_string_writable_chars (str); |
1526 | scm_i_string_stop_writing (); | |
1527 | return chars; | |
1528 | } | |
1529 | ||
1530 | size_t | |
fe78c51a | 1531 | scm_i_deprecated_string_length (SCM str) |
3ee86942 MV |
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 | ||
0f2d19dd JB |
1540 | void |
1541 | scm_init_strings () | |
0f2d19dd | 1542 | { |
3ee86942 | 1543 | scm_nullstr = scm_i_make_string (0, NULL); |
7c33806a | 1544 | |
a0599745 | 1545 | #include "libguile/strings.x" |
0f2d19dd JB |
1546 | } |
1547 | ||
89e00824 ML |
1548 | |
1549 | /* | |
1550 | Local Variables: | |
1551 | c-file-style: "gnu" | |
1552 | End: | |
1553 | */ |