3874b4a05c32a702df4b353a4bb834a7798ec4ac
[bpt/guile.git] / libguile / strings.c
1 /* Copyright (C) 1995,1996,1998,2000 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 <stdio.h>
48 #include <string.h>
49
50 #include "libguile/_scm.h"
51 #include "libguile/chars.h"
52
53 #include "libguile/strings.h"
54 #include "libguile/validate.h"
55 \f
56
57 /* {Strings}
58 */
59
60 SCM_DEFINE (scm_string_p, "string?", 1, 0, 0,
61 (SCM obj),
62 "Returns @code{#t} iff @var{obj} is a string, else returns\n"
63 "@code{#f}.")
64 #define FUNC_NAME s_scm_string_p
65 {
66 return SCM_BOOL (SCM_STRINGP (obj));
67 }
68 #undef FUNC_NAME
69
70 #if SCM_DEBUG_DEPRECATED == 0
71
72 /* The concept of read-only strings will disappear in next release
73 * of Guile.
74 */
75
76 SCM_DEFINE (scm_read_only_string_p, "read-only-string?", 1, 0, 0,
77 (SCM obj),
78 "Return true if @var{obj} can be read as a string,\n\n"
79 "This illustrates the difference between @code{string?} and\n"
80 "@code{read-only-string?}:\n\n"
81 "@example\n"
82 "(string? \"a string\") @result{} #t\n"
83 "(string? 'a-symbol) @result{} #f\n\n"
84 "(read-only-string? \"a string\") @result{} #t\n"
85 "(read-only-string? 'a-symbol) @result{} #t\n"
86 "@end example")
87 #define FUNC_NAME s_scm_read_only_string_p
88 {
89 return SCM_BOOL(SCM_ROSTRINGP (obj));
90 }
91 #undef FUNC_NAME
92
93 #endif /* DEPRECATED */
94
95 SCM_REGISTER_PROC (s_scm_list_to_string, "list->string", 1, 0, 0, scm_string);
96
97 SCM_DEFINE (scm_string, "string", 0, 0, 1,
98 (SCM chrs),
99 "@deffnx primitive list->string chrs\n"
100 "Returns a newly allocated string composed of the arguments,\n"
101 "@var{chrs}.")
102 #define FUNC_NAME s_scm_string
103 {
104 SCM result;
105
106 {
107 long i = scm_ilength (chrs);
108
109 SCM_ASSERT (i >= 0, chrs, SCM_ARGn, FUNC_NAME);
110 result = scm_makstr (i, 0);
111 }
112
113 {
114 unsigned char *data = SCM_STRING_UCHARS (result);
115
116 while (SCM_NNULLP (chrs))
117 {
118 SCM elt = SCM_CAR (chrs);
119
120 SCM_VALIDATE_CHAR (SCM_ARGn, elt);
121 *data++ = SCM_CHAR (elt);
122 chrs = SCM_CDR (chrs);
123 }
124 }
125 return result;
126 }
127 #undef FUNC_NAME
128
129
130 SCM
131 scm_makstr (long len, int dummy)
132 #define FUNC_NAME "scm_makstr"
133 {
134 SCM s;
135 char *mem;
136
137 SCM_ASSERT_RANGE (1, scm_long2num (len), len <= SCM_STRING_MAX_LENGTH);
138
139 mem = (char *) scm_must_malloc (len + 1, FUNC_NAME);
140 mem[len] = 0;
141
142 SCM_NEWCELL (s);
143 SCM_SET_STRING_CHARS (s, mem);
144 SCM_SET_STRING_LENGTH (s, len);
145
146 return s;
147 }
148 #undef FUNC_NAME
149
150
151 /* converts C scm_array of strings to SCM scm_list of strings. */
152 /* If argc < 0, a null terminated scm_array is assumed. */
153
154 SCM
155 scm_makfromstrs (int argc, char **argv)
156 {
157 int i = argc;
158 SCM lst = SCM_EOL;
159 if (0 > i)
160 for (i = 0; argv[i]; i++);
161 while (i--)
162 lst = scm_cons (scm_makfromstr (argv[i], (scm_sizet) strlen (argv[i]), 0), lst);
163 return lst;
164 }
165
166
167 /* This function must only be applied to memory obtained via malloc,
168 since the GC is going to apply `free' to it when the string is
169 dropped.
170
171 Also, s[len] must be `\0', since we promise that strings are
172 null-terminated. Perhaps we could handle non-null-terminated
173 strings by claiming they're shared substrings of a string we just
174 made up. */
175 SCM
176 scm_take_str (char *s, int len)
177 #define FUNC_NAME "scm_take_str"
178 {
179 SCM answer;
180
181 SCM_ASSERT_RANGE (2, scm_ulong2num (len), len <= SCM_STRING_MAX_LENGTH);
182
183 SCM_NEWCELL (answer);
184 SCM_SET_STRING_CHARS (answer, s);
185 SCM_SET_STRING_LENGTH (answer, len);
186 scm_done_malloc (len + 1);
187
188 return answer;
189 }
190 #undef FUNC_NAME
191
192
193 /* `s' must be a malloc'd string. See scm_take_str. */
194 SCM
195 scm_take0str (char *s)
196 {
197 return scm_take_str (s, strlen (s));
198 }
199
200 SCM
201 scm_makfromstr (const char *src, scm_sizet len, int dummy)
202 {
203 SCM s = scm_makstr (len, 0);
204 char *dst = SCM_STRING_CHARS (s);
205
206 while (len--)
207 *dst++ = *src++;
208 return s;
209 }
210
211 SCM
212 scm_makfrom0str (const char *src)
213 {
214 if (!src) return SCM_BOOL_F;
215 return scm_makfromstr (src, (scm_sizet) strlen (src), 0);
216 }
217
218
219 SCM
220 scm_makfrom0str_opt (const char *src)
221 {
222 return scm_makfrom0str (src);
223 }
224
225
226 SCM_DEFINE (scm_make_string, "make-string", 1, 1, 0,
227 (SCM k, SCM chr),
228 "Return a newly allocated string of\n"
229 "length @var{k}. If @var{chr} is given, then all elements of\n"
230 "the string are initialized to @var{chr}, otherwise the contents\n"
231 "of the @var{string} are unspecified.\n")
232 #define FUNC_NAME s_scm_make_string
233 {
234 if (SCM_INUMP (k))
235 {
236 long int i = SCM_INUM (k);
237 SCM res;
238
239 SCM_ASSERT_RANGE (1, k, i >= 0);
240
241 res = scm_makstr (i, 0);
242 if (!SCM_UNBNDP (chr))
243 {
244 unsigned char *dst;
245
246 SCM_VALIDATE_CHAR (2, chr);
247
248 dst = SCM_STRING_UCHARS (res);
249 memset (dst, SCM_CHAR (chr), i);
250 }
251
252 return res;
253 }
254 else if (SCM_BIGP (k))
255 SCM_OUT_OF_RANGE (1, k);
256 else
257 SCM_WRONG_TYPE_ARG (1, k);
258 }
259 #undef FUNC_NAME
260
261
262 SCM_DEFINE (scm_string_length, "string-length", 1, 0, 0,
263 (SCM string),
264 "Return the number of characters in @var{string}.")
265 #define FUNC_NAME s_scm_string_length
266 {
267 SCM_VALIDATE_STRING (1, string);
268 return SCM_MAKINUM (SCM_STRING_LENGTH (string));
269 }
270 #undef FUNC_NAME
271
272 SCM_DEFINE (scm_string_ref, "string-ref", 2, 0, 0,
273 (SCM str, SCM k),
274 "Return character @var{k} of @var{str} using zero-origin\n"
275 "indexing. @var{k} must be a valid index of @var{str}.")
276 #define FUNC_NAME s_scm_string_ref
277 {
278 int idx;
279
280 SCM_VALIDATE_STRING (1, str);
281 SCM_VALIDATE_INUM_COPY (2, k, idx);
282 SCM_ASSERT_RANGE (2, k, idx >= 0 && idx < SCM_STRING_LENGTH (str));
283 return SCM_MAKE_CHAR (SCM_STRING_UCHARS (str)[idx]);
284 }
285 #undef FUNC_NAME
286
287
288 SCM_DEFINE (scm_string_set_x, "string-set!", 3, 0, 0,
289 (SCM str, SCM k, SCM chr),
290 "Store @var{chr} in element @var{k} of @var{str} and return\n"
291 "an unspecified value. @var{k} must be a valid index of\n"
292 "@var{str}.")
293 #define FUNC_NAME s_scm_string_set_x
294 {
295 #if (SCM_DEBUG_DEPRECATED == 0)
296 SCM_VALIDATE_RWSTRING (1, str);
297 #else
298 SCM_VALIDATE_STRING (1, str);
299 #endif
300 SCM_VALIDATE_INUM_RANGE (2,k,0,SCM_STRING_LENGTH(str));
301 SCM_VALIDATE_CHAR (3,chr);
302 SCM_STRING_UCHARS (str)[SCM_INUM (k)] = SCM_CHAR (chr);
303 return SCM_UNSPECIFIED;
304 }
305 #undef FUNC_NAME
306
307
308 SCM_DEFINE (scm_substring, "substring", 2, 1, 0,
309 (SCM str, SCM start, SCM end),
310 "Return a newly allocated string formed from the characters\n"
311 "of @var{str} beginning with index @var{start} (inclusive) and\n"
312 "ending with index @var{end} (exclusive).\n"
313 "@var{str} must be a string, @var{start} and @var{end} must be\n"
314 "exact integers satisfying:\n\n"
315 "0 <= @var{start} <= @var{end} <= (string-length @var{str}).")
316 #define FUNC_NAME s_scm_substring
317 {
318 long int from;
319 long int to;
320
321 SCM_VALIDATE_STRING (1, str);
322 SCM_VALIDATE_INUM (2, start);
323 SCM_VALIDATE_INUM_DEF (3, end, SCM_STRING_LENGTH (str));
324
325 from = SCM_INUM (start);
326 SCM_ASSERT_RANGE (2, start, 0 <= from && from <= SCM_STRING_LENGTH (str));
327 to = SCM_INUM (end);
328 SCM_ASSERT_RANGE (3, end, from <= to && to <= SCM_STRING_LENGTH (str));
329
330 return scm_makfromstr (&SCM_STRING_CHARS (str)[from], (scm_sizet) (to - from), 0);
331 }
332 #undef FUNC_NAME
333
334
335 SCM_DEFINE (scm_string_append, "string-append", 0, 0, 1,
336 (SCM args),
337 "Return a newly allocated string whose characters form the\n"
338 "concatenation of the given strings, @var{args}.")
339 #define FUNC_NAME s_scm_string_append
340 {
341 SCM res;
342 register long i = 0;
343 register SCM l, s;
344 register unsigned char *data;
345
346 SCM_VALIDATE_REST_ARGUMENT (args);
347 for (l = args; !SCM_NULLP (l); l = SCM_CDR (l)) {
348 s = SCM_CAR (l);
349 SCM_VALIDATE_STRING (SCM_ARGn,s);
350 i += SCM_STRING_LENGTH (s);
351 }
352 res = scm_makstr (i, 0);
353 data = SCM_STRING_UCHARS (res);
354 for (l = args;SCM_NIMP (l);l = SCM_CDR (l)) {
355 s = SCM_CAR (l);
356 for (i = 0;i<SCM_STRING_LENGTH (s);i++) *data++ = SCM_STRING_UCHARS (s)[i];
357 }
358 return res;
359 }
360 #undef FUNC_NAME
361
362 #if SCM_DEBUG_DEPRECATED == 0
363
364 /* Explicit shared substrings will disappear from Guile.
365 *
366 * Instead, "normal" strings will be implemented using sharing
367 * internally, combined with a copy-on-write strategy.
368 */
369
370 SCM_DEFINE (scm_make_shared_substring, "make-shared-substring", 1, 2, 0,
371 (SCM str, SCM frm, SCM to),
372 "Return a shared substring of @var{str}. The semantics are the same as\n"
373 "for the @code{substring} function: the shared substring returned\n"
374 "includes all of the text from @var{str} between indexes @var{start}\n"
375 "(inclusive) and @var{end} (exclusive). If @var{end} is omitted, it\n"
376 "defaults to the end of @var{str}. The shared substring returned by\n"
377 "@code{make-shared-substring} occupies the same storage space as\n"
378 "@var{str}.")
379 #define FUNC_NAME s_scm_make_shared_substring
380 {
381 long f;
382 long t;
383 SCM answer;
384 SCM len_str;
385
386 SCM_VALIDATE_ROSTRING (1,str);
387 SCM_VALIDATE_INUM_DEF_COPY (2,frm,0,f);
388 SCM_VALIDATE_INUM_DEF_COPY (3,to,SCM_ROLENGTH(str),t);
389
390 SCM_ASSERT_RANGE (2,frm,(f >= 0));
391 SCM_ASSERT_RANGE (3,to, (f <= t) && (t <= SCM_ROLENGTH (str)));
392
393 SCM_NEWCELL (answer);
394 SCM_NEWCELL (len_str);
395
396 SCM_DEFER_INTS;
397 if (SCM_SUBSTRP (str))
398 {
399 long offset;
400 offset = SCM_INUM (SCM_SUBSTR_OFFSET (str));
401 f += offset;
402 t += offset;
403 SCM_SETCAR (len_str, SCM_MAKINUM (f));
404 SCM_SETCDR (len_str, SCM_SUBSTR_STR (str));
405 SCM_SETCDR (answer, len_str);
406 SCM_SETLENGTH (answer, t - f, scm_tc7_substring);
407 }
408 else
409 {
410 SCM_SETCAR (len_str, SCM_MAKINUM (f));
411 SCM_SETCDR (len_str, str);
412 SCM_SETCDR (answer, len_str);
413 SCM_SETLENGTH (answer, t - f, scm_tc7_substring);
414 }
415 SCM_ALLOW_INTS;
416 return answer;
417 }
418 #undef FUNC_NAME
419
420 #endif /* DEPRECATED */
421
422 void
423 scm_init_strings ()
424 {
425 #ifndef SCM_MAGIC_SNARFER
426 #include "libguile/strings.x"
427 #endif
428 }
429
430
431 /*
432 Local Variables:
433 c-file-style: "gnu"
434 End:
435 */