revert the ill-considered part of the 2001-05-24 changes
[bpt/guile.git] / libguile / strings.c
1 /* Copyright (C) 1995,1996,1998,2000,2001 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 * Boston, MA 02111-1307 USA
17 *
18 * As a special exception, the Free Software Foundation gives permission
19 * for additional uses of the text contained in its release of GUILE.
20 *
21 * The exception is that, if you link the GUILE library with other files
22 * to produce an executable, this does not by itself cause the
23 * resulting executable to be covered by the GNU General Public License.
24 * Your use of that executable is in no way restricted on account of
25 * linking the GUILE library code into it.
26 *
27 * This exception does not however invalidate any other reasons why
28 * the executable file might be covered by the GNU General Public License.
29 *
30 * This exception applies only to the code released by the
31 * Free Software Foundation under the name GUILE. If you copy
32 * code from other Free Software Foundation releases into a copy of
33 * GUILE, as the General Public License permits, the exception does
34 * not apply to the code that you add in this way. To avoid misleading
35 * anyone as to the status of such modified files, you must delete
36 * this exception notice from them.
37 *
38 * If you write modifications of your own for GUILE, it is your choice
39 * whether to permit this exception to apply to your modifications.
40 * If you do not wish that, delete this exception notice. */
41
42 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
43 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
44
45 \f
46
47 #include <string.h>
48
49 #include "libguile/_scm.h"
50 #include "libguile/chars.h"
51 #include "libguile/root.h"
52 #include "libguile/strings.h"
53 #include "libguile/validate.h"
54 \f
55
56 /* {Strings}
57 */
58
59 SCM_DEFINE (scm_string_p, "string?", 1, 0, 0,
60 (SCM obj),
61 "Return @code{#t} iff @var{obj} is a string, else returns\n"
62 "@code{#f}.")
63 #define FUNC_NAME s_scm_string_p
64 {
65 return SCM_BOOL (SCM_STRINGP (obj));
66 }
67 #undef FUNC_NAME
68
69 #if SCM_DEBUG_DEPRECATED == 0
70
71 /* The concept of read-only strings will disappear in next release
72 * of Guile.
73 */
74
75 SCM_DEFINE (scm_read_only_string_p, "read-only-string?", 1, 0, 0,
76 (SCM obj),
77 "Return @code{#t} if @var{obj} is either a string or a symbol,\n"
78 "otherwise return @code{#f}.")
79 #define FUNC_NAME s_scm_read_only_string_p
80 {
81 return SCM_BOOL(SCM_ROSTRINGP (obj));
82 }
83 #undef FUNC_NAME
84
85 #endif /* DEPRECATED */
86
87 SCM_REGISTER_PROC (s_scm_list_to_string, "list->string", 1, 0, 0, scm_string);
88
89 SCM_DEFINE (scm_string, "string", 0, 0, 1,
90 (SCM chrs),
91 "@deffnx primitive list->string chrs\n"
92 "Return a newly allocated string composed of the arguments,\n"
93 "@var{chrs}.")
94 #define FUNC_NAME s_scm_string
95 {
96 SCM result;
97
98 {
99 long i = scm_ilength (chrs);
100
101 SCM_ASSERT (i >= 0, chrs, SCM_ARGn, FUNC_NAME);
102 result = scm_allocate_string (i);
103 }
104
105 {
106 unsigned char *data = SCM_STRING_UCHARS (result);
107
108 while (SCM_NNULLP (chrs))
109 {
110 SCM elt = SCM_CAR (chrs);
111
112 SCM_VALIDATE_CHAR (SCM_ARGn, elt);
113 *data++ = SCM_CHAR (elt);
114 chrs = SCM_CDR (chrs);
115 }
116 }
117 return result;
118 }
119 #undef FUNC_NAME
120
121 #if (SCM_DEBUG_DEPRECATED == 0)
122
123 SCM
124 scm_makstr (size_t len, int dummy)
125 #define FUNC_NAME "scm_makstr"
126 {
127 SCM s;
128 char *mem;
129
130 SCM_ASSERT_RANGE (1, scm_long2num (len), len <= SCM_STRING_MAX_LENGTH);
131
132 mem = (char *) scm_must_malloc (len + 1, FUNC_NAME);
133 mem[len] = 0;
134
135 SCM_NEWCELL (s);
136 SCM_SET_STRING_CHARS (s, mem);
137 SCM_SET_STRING_LENGTH (s, len);
138
139 return s;
140 }
141 #undef FUNC_NAME
142
143 #endif /* SCM_DEBUG_DEPRECATED == 0 */
144
145 /* converts C scm_array of strings to SCM scm_list of strings. */
146 /* If argc < 0, a null terminated scm_array is assumed. */
147
148 SCM
149 scm_makfromstrs (int argc, char **argv)
150 {
151 int i = argc;
152 SCM lst = SCM_EOL;
153 if (0 > i)
154 for (i = 0; argv[i]; i++);
155 while (i--)
156 lst = scm_cons (scm_makfromstr (argv[i], (size_t) strlen (argv[i]), 0), lst);
157 return lst;
158 }
159
160
161 /* This function must only be applied to memory obtained via malloc,
162 since the GC is going to apply `free' to it when the string is
163 dropped.
164
165 Also, s[len] must be `\0', since we promise that strings are
166 null-terminated. Perhaps we could handle non-null-terminated
167 strings by claiming they're shared substrings of a string we just
168 made up. */
169 SCM
170 scm_take_str (char *s, size_t len)
171 #define FUNC_NAME "scm_take_str"
172 {
173 SCM answer;
174
175 SCM_ASSERT_RANGE (2, scm_ulong2num (len), len <= SCM_STRING_MAX_LENGTH);
176
177 SCM_NEWCELL (answer);
178 SCM_SET_STRING_CHARS (answer, s);
179 SCM_SET_STRING_LENGTH (answer, len);
180 scm_done_malloc (len + 1);
181
182 return answer;
183 }
184 #undef FUNC_NAME
185
186
187 /* `s' must be a malloc'd string. See scm_take_str. */
188 SCM
189 scm_take0str (char *s)
190 {
191 return scm_take_str (s, strlen (s));
192 }
193
194 SCM
195 scm_makfromstr (const char *src, size_t len, int dummy)
196 {
197 SCM s = scm_allocate_string (len);
198 char *dst = SCM_STRING_CHARS (s);
199
200 while (len--)
201 *dst++ = *src++;
202 return s;
203 }
204
205 SCM
206 scm_makfrom0str (const char *src)
207 {
208 if (!src) return SCM_BOOL_F;
209 return scm_makfromstr (src, (size_t) strlen (src), 0);
210 }
211
212
213 SCM
214 scm_makfrom0str_opt (const char *src)
215 {
216 return scm_makfrom0str (src);
217 }
218
219
220 SCM
221 scm_allocate_string (size_t len)
222 #define FUNC_NAME "scm_allocate_string"
223 {
224 char *mem;
225 SCM s;
226
227 SCM_ASSERT_RANGE (1, scm_long2num (len), len <= SCM_STRING_MAX_LENGTH);
228
229 mem = (char *) scm_must_malloc (len + 1, FUNC_NAME);
230 mem[len] = 0;
231
232 SCM_NEWCELL (s);
233 SCM_SET_STRING_CHARS (s, mem);
234 SCM_SET_STRING_LENGTH (s, len);
235
236 return s;
237 }
238 #undef FUNC_NAME
239
240
241 SCM_DEFINE (scm_make_string, "make-string", 1, 1, 0,
242 (SCM k, SCM chr),
243 "Return a newly allocated string of\n"
244 "length @var{k}. If @var{chr} is given, then all elements of\n"
245 "the string are initialized to @var{chr}, otherwise the contents\n"
246 "of the @var{string} are unspecified.\n")
247 #define FUNC_NAME s_scm_make_string
248 {
249 if (SCM_INUMP (k))
250 {
251 long int i = SCM_INUM (k);
252 SCM res;
253
254 SCM_ASSERT_RANGE (1, k, i >= 0);
255
256 res = scm_allocate_string (i);
257 if (!SCM_UNBNDP (chr))
258 {
259 unsigned char *dst;
260
261 SCM_VALIDATE_CHAR (2, chr);
262
263 dst = SCM_STRING_UCHARS (res);
264 memset (dst, SCM_CHAR (chr), i);
265 }
266
267 return res;
268 }
269 else if (SCM_BIGP (k))
270 SCM_OUT_OF_RANGE (1, k);
271 else
272 SCM_WRONG_TYPE_ARG (1, k);
273 }
274 #undef FUNC_NAME
275
276
277 SCM_DEFINE (scm_string_length, "string-length", 1, 0, 0,
278 (SCM string),
279 "Return the number of characters in @var{string}.")
280 #define FUNC_NAME s_scm_string_length
281 {
282 SCM_VALIDATE_STRING (1, string);
283 return SCM_MAKINUM (SCM_STRING_LENGTH (string));
284 }
285 #undef FUNC_NAME
286
287 SCM_DEFINE (scm_string_ref, "string-ref", 2, 0, 0,
288 (SCM str, SCM k),
289 "Return character @var{k} of @var{str} using zero-origin\n"
290 "indexing. @var{k} must be a valid index of @var{str}.")
291 #define FUNC_NAME s_scm_string_ref
292 {
293 long idx;
294
295 SCM_VALIDATE_STRING (1, str);
296 SCM_VALIDATE_INUM_COPY (2, k, idx);
297 SCM_ASSERT_RANGE (2, k, idx >= 0 && idx < SCM_STRING_LENGTH (str));
298 return SCM_MAKE_CHAR (SCM_STRING_UCHARS (str)[idx]);
299 }
300 #undef FUNC_NAME
301
302
303 SCM_DEFINE (scm_string_set_x, "string-set!", 3, 0, 0,
304 (SCM str, SCM k, SCM chr),
305 "Store @var{chr} in element @var{k} of @var{str} and return\n"
306 "an unspecified value. @var{k} must be a valid index of\n"
307 "@var{str}.")
308 #define FUNC_NAME s_scm_string_set_x
309 {
310 #if (SCM_DEBUG_DEPRECATED == 0)
311 SCM_VALIDATE_RWSTRING (1, str);
312 #else
313 SCM_VALIDATE_STRING (1, str);
314 #endif
315 SCM_VALIDATE_INUM_RANGE (2,k,0,SCM_STRING_LENGTH(str));
316 SCM_VALIDATE_CHAR (3,chr);
317 SCM_STRING_UCHARS (str)[SCM_INUM (k)] = SCM_CHAR (chr);
318 return SCM_UNSPECIFIED;
319 }
320 #undef FUNC_NAME
321
322
323 SCM_DEFINE (scm_substring, "substring", 2, 1, 0,
324 (SCM str, SCM start, SCM end),
325 "Return a newly allocated string formed from the characters\n"
326 "of @var{str} beginning with index @var{start} (inclusive) and\n"
327 "ending with index @var{end} (exclusive).\n"
328 "@var{str} must be a string, @var{start} and @var{end} must be\n"
329 "exact integers satisfying:\n\n"
330 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
331 #define FUNC_NAME s_scm_substring
332 {
333 long int from;
334 long int to;
335
336 SCM_VALIDATE_STRING (1, str);
337 SCM_VALIDATE_INUM (2, start);
338 SCM_VALIDATE_INUM_DEF (3, end, SCM_STRING_LENGTH (str));
339
340 from = SCM_INUM (start);
341 SCM_ASSERT_RANGE (2, start, 0 <= from && from <= SCM_STRING_LENGTH (str));
342 to = SCM_INUM (end);
343 SCM_ASSERT_RANGE (3, end, from <= to && to <= SCM_STRING_LENGTH (str));
344
345 return scm_makfromstr (&SCM_STRING_CHARS (str)[from], (size_t) (to - from), 0);
346 }
347 #undef FUNC_NAME
348
349
350 SCM_DEFINE (scm_string_append, "string-append", 0, 0, 1,
351 (SCM args),
352 "Return a newly allocated string whose characters form the\n"
353 "concatenation of the given strings, @var{args}.")
354 #define FUNC_NAME s_scm_string_append
355 {
356 SCM res;
357 size_t i = 0;
358 register SCM l, s;
359 register unsigned char *data;
360
361 SCM_VALIDATE_REST_ARGUMENT (args);
362 for (l = args; !SCM_NULLP (l); l = SCM_CDR (l)) {
363 s = SCM_CAR (l);
364 SCM_VALIDATE_STRING (SCM_ARGn,s);
365 i += SCM_STRING_LENGTH (s);
366 }
367 res = scm_allocate_string (i);
368 data = SCM_STRING_UCHARS (res);
369 for (l = args;SCM_NIMP (l);l = SCM_CDR (l)) {
370 s = SCM_CAR (l);
371 for (i = 0;i<SCM_STRING_LENGTH (s);i++) *data++ = SCM_STRING_UCHARS (s)[i];
372 }
373 return res;
374 }
375 #undef FUNC_NAME
376
377 #if SCM_DEBUG_DEPRECATED == 0
378
379 /* Explicit shared substrings will disappear from Guile.
380 *
381 * Instead, "normal" strings will be implemented using sharing
382 * internally, combined with a copy-on-write strategy.
383 */
384
385 SCM_DEFINE (scm_make_shared_substring, "make-shared-substring", 1, 2, 0,
386 (SCM str, SCM start, SCM end),
387 "Return a shared substring of @var{str}. The arguments are the\n"
388 "same as for the @code{substring} function: the shared substring\n"
389 "returned includes all of the text from @var{str} between\n"
390 "indexes @var{start} (inclusive) and @var{end} (exclusive). If\n"
391 "@var{end} is omitted, it defaults to the end of @var{str}. The\n"
392 "shared substring returned by @code{make-shared-substring}\n"
393 "occupies the same storage space as @var{str}.")
394 #define FUNC_NAME s_scm_make_shared_substring
395 {
396 long f;
397 long t;
398 SCM answer;
399 SCM len_str;
400
401 SCM_VALIDATE_ROSTRING (1,str);
402 SCM_VALIDATE_INUM_DEF_COPY (2,start,0,f);
403 SCM_VALIDATE_INUM_DEF_COPY (3,end,SCM_ROLENGTH(str),t);
404
405 SCM_ASSERT_RANGE (2,start,(f >= 0));
406 SCM_ASSERT_RANGE (3,end, (f <= t) && (t <= SCM_ROLENGTH (str)));
407
408 SCM_NEWCELL (answer);
409 SCM_NEWCELL (len_str);
410
411 SCM_DEFER_INTS;
412 if (SCM_SUBSTRP (str))
413 {
414 long offset;
415 offset = SCM_INUM (SCM_SUBSTR_OFFSET (str));
416 f += offset;
417 t += offset;
418 SCM_SETCAR (len_str, SCM_MAKINUM (f));
419 SCM_SETCDR (len_str, SCM_SUBSTR_STR (str));
420 SCM_SETCDR (answer, len_str);
421 SCM_SETLENGTH (answer, t - f, scm_tc7_substring);
422 }
423 else
424 {
425 SCM_SETCAR (len_str, SCM_MAKINUM (f));
426 SCM_SETCDR (len_str, str);
427 SCM_SETCDR (answer, len_str);
428 SCM_SETLENGTH (answer, t - f, scm_tc7_substring);
429 }
430 SCM_ALLOW_INTS;
431 return answer;
432 }
433 #undef FUNC_NAME
434
435 #endif /* DEPRECATED */
436
437 void
438 scm_init_strings ()
439 {
440 scm_nullstr = scm_allocate_string (0);
441
442 #ifndef SCM_MAGIC_SNARFER
443 #include "libguile/strings.x"
444 #endif
445 }
446
447
448 /*
449 Local Variables:
450 c-file-style: "gnu"
451 End:
452 */